- /*
- Programma: RedCalc
- Versione: 1.0
- Autore: Red Skull
- Descrizione: Semplice calcolatrice grafica creata con l'utilizzo delle gtk come esercizio.
- Web: http://redskull92.wordpress.com
- Mail: RedSkull92[AT]gmail[DOT]com
- Altro: Per compilare il sorgente basta dare questo da shell:
- gcc RedCalc.c -o RedCalc `pkg-config --libs --cflags gtk+-2.0`
- */
- #include <gtk/gtk.h>
- #include <stdlib.h>
- #define VERSIONE "1.0"
- //Var globali
- GtkWidget *input;
- int flop = 0;
- char prim = '0';
- long buff = 0;
- char *valori;
- //Prototipi
- void premuto (GtkWidget *, gpointer);
- void calcolo (char);
- //Funzione che riceve i pulsanti premuti
- void
- premuto (GtkWidget * widget, gpointer data)
- {
- static int lung = 0;
- char *stringa = (char *) data, op;
- valori = (char *) gtk_entry_get_text (GTK_ENTRY (input));
- switch (stringa[0])
- {
- case '+':
- case '-':
- case '*':
- case '/':
- if (prim == '0')
- {
- buff = atol (valori);
- gtk_entry_set_text (GTK_ENTRY (input), "");
- }
- else
- {
- calcolo (prim);
- }
- prim = stringa[0];
- break;
- case '=':
- calcolo (stringa[0]);
- break;
- default:
- if (flop == 1)
- {
- lung = 0;
- gtk_entry_set_text (GTK_ENTRY (input), "");
- gtk_editable_insert_text (GTK_EDITABLE (input), data, -1, &lung);
- lung++;
- flop = 0;
- }
- else
- {
- gtk_editable_insert_text (GTK_EDITABLE (input), data, -1, &lung);
- lung++;
- }
- break;
- }
- }
- void
- calcolo (char op)
- {
- char ris[90] = "";
- switch (op)
- {
- case '+':
- case '-':
- case '*':
- case '/':
- if (op == '-')
- {
- buff -= atol (valori);
- }
- else if (op == '+')
- {
- buff += atol (valori);
- }
- else if (op == '*')
- {
- buff *= atol (valori);
- }
- else if (op == '/')
- {
- if (atoi (valori) == 0)
- {
- g_print ("Impossibile dividere per 0\n");
- }
- else
- {
- buff /= atol (valori);
- }
- }
- gtk_entry_set_text (GTK_ENTRY (input), "");
- break;
- case '=':
- if (prim == '-')
- {
- buff -= atol (valori);
- }
- else if (prim == '+')
- {
- buff += atol (valori);
- }
- else if (prim == '*')
- {
- buff *= atol (valori);
- }
- else if (prim == '/')
- {
- if (atoi (valori) == 0)
- {
- g_print ("Impossibile dividere per 0\n");
- }
- else
- {
- buff /= atol (valori);
- }
- }
- sprintf (ris, "%d", buff);
- gtk_entry_set_text (GTK_ENTRY (input), ris);
- buff = 0;
- prim = '0';
- flop = 1;
- break;
- }
- }
- int
- main (int argc, char *argv[])
- {
- //Widget Finestra
- GtkWidget *win;
- //Widget pulsanti e entry
- GtkWidget *button[15];
- //Widget tabella
- GtkWidget *table;
- int a;
- // Definisco i tasti
- static const char *tastini[15] = {
- "0", "1", "2", "3",
- "4", "5", "6", "7",
- "8", "9", "+", "-",
- "*", "/", "="
- };
- //Inizializzo gtk
- gtk_init (&argc, &argv);
- //Inizio creazione finestra
- win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- gtk_window_set_title (GTK_WINDOW (win), "RedCalc v. " VERSIONE);
- gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
- gtk_window_set_default_size (GTK_WINDOW (win), 200, 200);
- gtk_container_set_border_width (GTK_CONTAINER (win), 1);
- //Fine Creazione finestra
- //Creo la tabella 4 x 5 e setto gli spazi
- table = gtk_table_new (4, 5, TRUE);
- gtk_table_set_row_spacings (GTK_TABLE (table), 1);
- gtk_table_set_col_spacings (GTK_TABLE (table), 1);
- gtk_container_add (GTK_CONTAINER (win), table);
- //Creo i pulsanti e li memorizzo in button
- for (a = 0; a < 15; a++)
- {
- button[a] = gtk_button_new_with_label (tastini[a]);
- }
- //Attacco i pulsanti alla tabella sistemandoli
- gtk_table_attach (GTK_TABLE (table), button[0], 0, 3, 4, 5,
- GTK_EXPAND | GTK_FILL, GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[1], 0, 1, 3, 4,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[2], 1, 2, 3, 4,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[3], 2, 3, 3, 4,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[4], 0, 1, 2, 3,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[5], 1, 2, 2, 3,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[6], 2, 3, 2, 3,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[7], 0, 1, 1, 2,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[8], 1, 2, 1, 2,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[9], 2, 3, 1, 2,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[10], 3, 4, 0, 1,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[11], 3, 4, 1, 2,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[12], 3, 4, 2, 3,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[13], 3, 4, 3, 4,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- gtk_table_attach (GTK_TABLE (table), button[14], 3, 4, 4, 5,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
- for (a = 0; a < 15; a++)
- {
- g_signal_connect (G_OBJECT (button[a]), "clicked",
- G_CALLBACK (premuto), (gpointer) tastini[a]);
- }
- //Creo l'entry e lo rendo non editabile
- input = gtk_entry_new ();
- gtk_editable_set_editable (GTK_EDITABLE (input), FALSE);
- //Sistemo l'entry nella tabella
- gtk_table_attach (GTK_TABLE (table), input, 0, 3, 0, 1,
- GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 1);
- //Se la finestra viene chiusa il programma termina
- g_signal_connect (G_OBJECT (win), "delete-event",
- G_CALLBACK (gtk_main_quit), NULL);
- //Rendo visibili tutti i widget presenti nella finestar win
- gtk_widget_show_all (win);
- //Funzione per far rimanere la finestra aperta
- gtk_main ();
- return 0;
- //FINE
- }
Simple calculator with GTK+ 2.0
By Red Skull
