| 1 | /* |
| 2 | First run tutorial.glade through gtk-builder-convert with this command: |
| 3 | gtk-builder-convert tutorial.glade tutorial.xml |
| 4 | |
| 5 | Then save this file as main.c and compile it using this command |
| 6 | (those are backticks, not single quotes): |
| 7 | gcc -Wall -g -o tutorial main.c `pkg-config --cflags --libs gtk+-2.0` -export-dynamic |
| 8 | |
| 9 | Then execute it using: |
| 10 | ./tutorial |
| 11 | */ |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | #include <math.h> |
| 17 | #include <gtk/gtk.h> |
| 18 | |
| 19 | GtkBuilder *builder; |
| 20 | GtkWidget *window, *entry_a, *entry_b, *entry_calc, *entry_mem, *label_op; |
| 21 | char tempbuf[128], *ptempchar, nextop; |
| 22 | int calculated; |
| 23 | |
| 24 | void on_button_no_clicked(GtkWidget *widget, gpointer user_data) { |
| 25 | gtk_entry_append_text(entry_calc, gtk_button_get_label(widget)); |
| 26 | } |
| 27 | |
| 28 | void calculate() { |
| 29 | double a, b, e; |
| 30 | if(calculated) return; |
| 31 | gtk_entry_set_text(entry_b, gtk_entry_get_text(entry_calc)); |
| 32 | gtk_entry_set_text(entry_calc, ""); |
| 33 | |
| 34 | a = strtod(gtk_entry_get_text(entry_a), NULL); |
| 35 | b = strtod(gtk_entry_get_text(entry_b), NULL); |
| 36 | |
| 37 | switch(nextop) { |
| 38 | case '+': e = a + b; break; |
| 39 | case '-': e = a - b; break; |
| 40 | case '*': e = a * b; break; |
| 41 | case '/': e = a / b; break; |
| 42 | case '^': e = pow(a, b); break; |
| 43 | case 'S': e = a + b; break; |
| 44 | } |
| 45 | sprintf(tempbuf, "%lf", e); |
| 46 | //printf("%f %c %f = %f\n", a, nextop, b, e); |
| 47 | gtk_entry_set_text(entry_calc, tempbuf); |
| 48 | calculated = 1; |
| 49 | } |
| 50 | |
| 51 | void on_button_op_clicked(GtkWidget *widget, gpointer user_data) { |
| 52 | char *oplbl = gtk_button_get_label(widget); |
| 53 | |
| 54 | switch(oplbl[0]) { |
| 55 | case '+': case '-': case '*': case '/': case '^': case 'S': |
| 56 | if(!calculated) calculate(); |
| 57 | gtk_entry_set_text(entry_a, gtk_entry_get_text(entry_calc)); |
| 58 | gtk_entry_set_text(entry_calc, ""); |
| 59 | gtk_entry_set_text(entry_b, ""); |
| 60 | gtk_label_set_label(label_op, oplbl); |
| 61 | nextop = oplbl[0]; |
| 62 | calculated = 0; |
| 63 | break; |
| 64 | |
| 65 | case '=': |
| 66 | if(!calculated) calculate(); |
| 67 | gtk_entry_set_text(entry_calc, gtk_entry_get_text(entry_calc)); |
| 68 | calculated = 1; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void on_button_mem_clicked(GtkWidget *widget, gpointer user_data) { |
| 74 | char *oplbl = gtk_button_get_label(widget); |
| 75 | double e = 0, m = 0; |
| 76 | e = strtod(gtk_entry_get_text(entry_calc), NULL); |
| 77 | m = strtod(gtk_entry_get_text(entry_mem), NULL); |
| 78 | switch(oplbl[0]) { |
| 79 | case '+': m += e; break; |
| 80 | case '-': m -= e; break; |
| 81 | case 'C': m = 0; break; |
| 82 | } |
| 83 | sprintf(tempbuf, "%lf", m); |
| 84 | gtk_entry_set_text(entry_mem, tempbuf); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | void on_button_clear_clicked() { |
| 89 | /*ptempchar = gtk_entry_get_text(entry_calc); |
| 90 | ptempchar[strlen(ptempchar)-1] = '\x00'; |
| 91 | gtk_entry_set_text(entry_calc, ptempchar);*/ |
| 92 | gtk_entry_append_text(entry_calc, "\r"); |
| 93 | } |
| 94 | |
| 95 | void reset() { |
| 96 | calculated = 1; |
| 97 | gtk_label_set_label(label_op, "?"); |
| 98 | gtk_entry_set_text(entry_calc, ""); |
| 99 | gtk_entry_set_text(entry_a, ""); |
| 100 | gtk_entry_set_text(entry_b, ""); |
| 101 | } |
| 102 | |
| 103 | void on_window_destroy (GtkObject *object, gpointer user_data) |
| 104 | { |
| 105 | gtk_main_quit (); |
| 106 | } |
| 107 | |
| 108 | int main (int argc, char *argv[]) |
| 109 | { |
| 110 | gtk_init (&argc, &argv); |
| 111 | |
| 112 | builder = gtk_builder_new (); |
| 113 | gtk_builder_add_from_file (builder, "calc.xml", NULL); |
| 114 | window = GTK_WIDGET (gtk_builder_get_object (builder, "window")); |
| 115 | |
| 116 | entry_a = GTK_WIDGET (gtk_builder_get_object (builder, "entry_a")); |
| 117 | entry_b = GTK_WIDGET (gtk_builder_get_object (builder, "entry_b")); |
| 118 | entry_calc = GTK_WIDGET (gtk_builder_get_object (builder, "entry_calc")); |
| 119 | entry_mem = GTK_WIDGET (gtk_builder_get_object (builder, "entry_mem")); |
| 120 | label_op = GTK_WIDGET (gtk_builder_get_object (builder, "label_op")); |
| 121 | |
| 122 | gtk_builder_connect_signals (builder, NULL); |
| 123 | g_object_unref (G_OBJECT (builder)); |
| 124 | |
| 125 | reset(); |
| 126 | gtk_widget_show (window); |
| 127 | gtk_main (); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |