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;
}