-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
81 lines (68 loc) · 2.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "postfix.h"
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *button;
GtkWidget *entry;
GtkWidget *vbox;
GtkWidget *hbox1;
/*initialise main window*/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete-event",
G_CALLBACK(delete_event), NULL);
g_signal_connect(window, "destroy",
G_CALLBACK(destroy), NULL);
gtk_window_set_title (GTK_WINDOW (window), "Postfix Calculator");
gtk_container_set_border_width(GTK_CONTAINER(window), 15);
/*initialise layout*/
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show(vbox);
hbox1 = gtk_hbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(vbox), hbox1);
/*initialise text field*/
entry = gtk_entry_new();
g_signal_connect(entry, "activate",
G_CALLBACK(button_callback), entry); //signal for enter action
gtk_entry_set_text (GTK_ENTRY (entry), "Enter Expression:");
gtk_editable_set_editable(GTK_EDITABLE(entry), TRUE);
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 0);
gtk_widget_show(entry);
/*initialise calculate button*/
button = gtk_button_new_with_label("=");
g_signal_connect(button, "clicked",
G_CALLBACK(button_callback), entry);
gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 0);
gtk_widget_show(button);
gtk_widget_show(hbox1);
gtk_widget_show(vbox);
gtk_widget_show(window);
gtk_main();
return 0;
}
/*GTK SHIT*/
/*delete event is called by window manager
*returning true means you don't want window destroyed*/
static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
g_print("delete event occured\n");
return FALSE;
}
/*destory callback*/
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
static void button_callback(GtkWidget *widget, GtkWidget *entry)
{
const gchar *entry_text;
char str[30];
int result;
entry_text = gtk_entry_get_text (GTK_ENTRY (entry));
printf("called by button - entry contents: %s\n", entry_text);
result = evaluatePostfixExpression(entry_text);
sprintf(str, "%d", result);
gtk_entry_set_text(GTK_ENTRY(entry), str);
gtk_widget_grab_focus(entry);
}