GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Sep 22, 2014 4:15 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Help With Calculator Program: Setting Text in Entry Widget
PostPosted: Sun Oct 21, 2007 3:36 pm 
Offline

Joined: Sun Oct 21, 2007 3:20 pm
Posts: 3
I am new to GTK and am using C to create a simple calculator that adds and subtracts numbers. I got the window set up and the button clicks are being processed correctly. However, I am stuck on how to change or set the text of my entry widget.

My callback function that processes the "clicked" signal sets "text" with numbers that were clicked.

so I have this line later in the program, but it does not set the gtk entry widget with the new text:
gtk_entry_set_text(GTK_ENTRY (text_display), text);

Is it possible to create a global pointer to the entry widget so that I can use it directly in the callback function? Thanks.


Here is my code :
Code:
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXCHARS 100
#define ROWS 4
#define COLS 3

gchar text[50] = "0"; //variable that will hold the numbers to display, initially 0
gchar buffer1[50];
gchar buffer2[50];
gint tmp_pos;

//Callbacks
void callback( GtkWidget *widget, gpointer data )
{
gchar *num = (gchar*) (data);

strcpy(buffer1, text);

//g_print("buffer1 has %s. \n", buffer1);

//put number into buffer2 if *num is not '+' or "-"
gchar* tmpStr[1];


if( (*num != '+') && (*num != '-') )
{
  //g_print("*num has %c \n", *num);
  g_sprintf(tmpStr,"%c", *num);

  strcat(buffer2, tmpStr);
  strcpy (text, buffer2);
  g_print("text has %s \n", text);
}

else
  g_print("%c was pressed \n", *num);
}

void destroy_window(GtkWidget *widget, gpointer data )
{
    gtk_main_quit ();
}


int main(int argc, char *argv[])
{

//create pointers to GTK_Widgets
GtkWidget *window;
GtkWidget *button;
GtkWidget *quit_button;
GtkWidget *clear_button;
GtkWidget *text_display;
GtkWidget *vbox, *hbox;
GtkWidget *table;
 
gint i=0, j=0, k=0;
gint button_id[ROWS * COLS];
gchar str[10];

gtk_init(&argc, &argv);
GtkAttachOptions howToAttach;

//create the window and set it up
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW (window), "Simple Calculator");

gint width = 600;
gint height = 600;

gtk_window_set_default_size(GTK_WINDOW (window), width, height);
//gtk_container_set_border_width (GTK_CONTAINER (window), 20);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);

//create buttons
quit_button = gtk_button_new_with_label("Quit");
clear_button = gtk_button_new_with_label("Clear");
gtk_widget_show(quit_button);
gtk_widget_show(clear_button);

//create a text entry widget which will only be used to display
//results, initially displaying "0"
text_display = gtk_entry_new( );

gtk_entry_set_max_length (GTK_ENTRY (text_display), MAXCHARS);

gtk_entry_set_text(GTK_ENTRY (text_display), text);

gtk_widget_show (text_display);


//create an hbox to pack entry widget
hbox = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (hbox), text_display, FALSE, FALSE, 0);

gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);


  /* Create a 4x3 table */
    table = gtk_table_new (ROWS, COLS, FALSE);

    /* Put the table in the main window */
    gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
   
    //create array of labels
    gchar labels[4][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'+', '0', '-'} };

    /* create buttons and put them in the table, numbered 0 to 9 and +, - */
   for (; j < 3; j++)
   {
     i = 0 ; 
        for (; i < 4;  i++)
        {
         button_id [k] = labels[i][j];
       
            g_sprintf(str, "%c", button_id[k]);
            button = gtk_button_new_with_label (str);         

            gtk_table_attach (GTK_TABLE (table), button, (j), j+1, (i), i+1, GTK_FILL, howToAttach, 2, 2);

            g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback), (gpointer) &(button_id[k]));
           
            gtk_widget_show (button);

            k++;
        }
     }

//add buttons to vbox
gtk_box_pack_start (GTK_BOX (vbox), quit_button, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), clear_button, FALSE, FALSE, 0);
gtk_widget_show (vbox);

//connect signals
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy_window), NULL);

g_signal_connect (G_OBJECT (quit_button), "clicked", G_CALLBACK (destroy_window), NULL);

//show window
gtk_widget_show(table);
gtk_widget_show(window);

gtk_main();

return 0;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 4:27 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Yes, you could declare text_display globally. However, a lot of C programmers try to avoid using globals whenever possible. I find that I almost NEVER need to declare any global variables.

What many people do, is create structures to hold data we need to pass around. Then, we pass that structure as user_data to the callback.

So if you had, for example,

Code:
typdef struct {
  GtkWidget *button;
  gint *button_id;
  GtkWidget *text_display;
} ButtonData;


You typically allocate and deallocate memory for your structures in GTK using:

Code:
// allocate
ButtonData *data = g_slice_new (ButtonData);

// use
data->button = gtk_button_new (...);
// etc.

// free
g_slice_free (ButtonData, data);

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group