Paste2 Logo
  1. /*
  2. Programma: RedCalc
  3. Versione: 1.0
  4. Autore: Red Skull
  5. Descrizione: Semplice calcolatrice grafica creata con l'utilizzo delle gtk come esercizio.
  6. Web: http://redskull92.wordpress.com
  7. Mail: RedSkull92[AT]gmail[DOT]com
  8. Altro: Per compilare il sorgente basta dare questo da shell:
  9. gcc RedCalc.c -o RedCalc `pkg-config --libs --cflags gtk+-2.0`
  10. */
  11.  
  12. #include <gtk/gtk.h>
  13. #include <stdlib.h>
  14. #define VERSIONE "1.0"
  15.  
  16. //Var globali
  17. GtkWidget *input;
  18. int flop = 0;
  19. char prim = '0';
  20. long buff = 0;
  21. char *valori;
  22.  
  23. //Prototipi
  24. void premuto (GtkWidget *, gpointer);
  25. void calcolo (char);
  26.  
  27. //Funzione che riceve i pulsanti premuti
  28. void
  29. premuto (GtkWidget * widget, gpointer data)
  30. {
  31.   static int lung = 0;
  32.   char *stringa = (char *) data, op;
  33.   valori = (char *) gtk_entry_get_text (GTK_ENTRY (input));
  34.  
  35.   switch (stringa[0])
  36.     {
  37.     case '+':
  38.     case '-':
  39.     case '*':
  40.     case '/':
  41.  
  42.       if (prim == '0')
  43.         {
  44.           buff = atol (valori);
  45.           gtk_entry_set_text (GTK_ENTRY (input), "");
  46.         }
  47.       else
  48.       {
  49.         calcolo (prim);
  50.       }
  51.       prim = stringa[0];
  52.       break;
  53.     case '=':
  54.       calcolo (stringa[0]);
  55.       break;
  56.     default:
  57.       if (flop == 1)
  58.         {
  59.           lung = 0;
  60.           gtk_entry_set_text (GTK_ENTRY (input), "");
  61.           gtk_editable_insert_text (GTK_EDITABLE (input), data, -1, &lung);
  62.           lung++;
  63.           flop = 0;
  64.         }
  65.       else
  66.         {
  67.           gtk_editable_insert_text (GTK_EDITABLE (input), data, -1, &lung);
  68.           lung++;
  69.         }
  70.       break;
  71.     }
  72. }
  73.  
  74. void
  75. calcolo (char op)
  76. {
  77.   char ris[90] = "";
  78.  
  79.  
  80.   switch (op)
  81.     {
  82.     case '+':
  83.     case '-':
  84.     case '*':
  85.     case '/':
  86.       if (op == '-')
  87.         {
  88.           buff -= atol (valori);
  89.         }
  90.       else if (op == '+')
  91.         {
  92.           buff += atol (valori);
  93.         }
  94.       else if (op == '*')
  95.         {
  96.           buff *= atol (valori);
  97.         }
  98.       else if (op == '/')
  99.         {
  100.           if (atoi (valori) == 0)
  101.             {
  102.               g_print ("Impossibile dividere per 0\n");
  103.             }
  104.           else
  105.             {
  106.               buff /= atol (valori);
  107.             }
  108.         }
  109.       gtk_entry_set_text (GTK_ENTRY (input), "");
  110.       break;
  111.     case '=':
  112.       if (prim == '-')
  113.         {
  114.           buff -= atol (valori);
  115.         }
  116.       else if (prim == '+')
  117.         {
  118.           buff += atol (valori);
  119.         }
  120.       else if (prim == '*')
  121.         {
  122.           buff *= atol (valori);
  123.         }
  124.       else if (prim == '/')
  125.         {
  126.           if (atoi (valori) == 0)
  127.             {
  128.               g_print ("Impossibile dividere per 0\n");
  129.             }
  130.           else
  131.             {
  132.               buff /= atol (valori);
  133.             }
  134.         }
  135.       sprintf (ris, "%d", buff);
  136.       gtk_entry_set_text (GTK_ENTRY (input), ris);
  137.       buff = 0;
  138.       prim = '0';
  139.       flop = 1;
  140.       break;
  141.     }
  142. }
  143.  
  144. int
  145. main (int argc, char *argv[])
  146. {
  147.   //Widget Finestra
  148.   GtkWidget *win;
  149.   //Widget pulsanti e entry
  150.   GtkWidget *button[15];
  151.   //Widget tabella
  152.   GtkWidget *table;
  153.  
  154.  
  155.   int a;
  156.   // Definisco i tasti
  157.   static const char *tastini[15] = {
  158.     "0", "1", "2", "3",
  159.     "4", "5", "6", "7",
  160.     "8", "9", "+", "-",
  161.     "*", "/", "="
  162.   };
  163.   //Inizializzo gtk
  164.   gtk_init (&argc, &argv);
  165.  
  166.   //Inizio creazione finestra
  167.   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  168.   gtk_window_set_title (GTK_WINDOW (win), "RedCalc v. " VERSIONE);
  169.   gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
  170.   gtk_window_set_default_size (GTK_WINDOW (win), 200, 200);
  171.   gtk_container_set_border_width (GTK_CONTAINER (win), 1);
  172.   //Fine Creazione finestra
  173.  
  174.   //Creo la tabella 4 x 5 e setto gli spazi
  175.   table = gtk_table_new (4, 5, TRUE);
  176.   gtk_table_set_row_spacings (GTK_TABLE (table), 1);
  177.   gtk_table_set_col_spacings (GTK_TABLE (table), 1);
  178.   gtk_container_add (GTK_CONTAINER (win), table);
  179.  
  180.   //Creo i pulsanti e li memorizzo in button
  181.   for (a = 0; a < 15; a++)
  182.     {
  183.       button[a] = gtk_button_new_with_label (tastini[a]);
  184.     }
  185.   //Attacco i pulsanti alla tabella sistemandoli
  186.   gtk_table_attach (GTK_TABLE (table), button[0], 0, 3, 4, 5,
  187.                     GTK_EXPAND | GTK_FILL, GTK_EXPAND, 5, 5);
  188.   gtk_table_attach (GTK_TABLE (table), button[1], 0, 1, 3, 4,
  189.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  190.   gtk_table_attach (GTK_TABLE (table), button[2], 1, 2, 3, 4,
  191.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  192.   gtk_table_attach (GTK_TABLE (table), button[3], 2, 3, 3, 4,
  193.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  194.   gtk_table_attach (GTK_TABLE (table), button[4], 0, 1, 2, 3,
  195.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  196.   gtk_table_attach (GTK_TABLE (table), button[5], 1, 2, 2, 3,
  197.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  198.   gtk_table_attach (GTK_TABLE (table), button[6], 2, 3, 2, 3,
  199.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  200.   gtk_table_attach (GTK_TABLE (table), button[7], 0, 1, 1, 2,
  201.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  202.   gtk_table_attach (GTK_TABLE (table), button[8], 1, 2, 1, 2,
  203.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  204.   gtk_table_attach (GTK_TABLE (table), button[9], 2, 3, 1, 2,
  205.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  206.   gtk_table_attach (GTK_TABLE (table), button[10], 3, 4, 0, 1,
  207.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  208.   gtk_table_attach (GTK_TABLE (table), button[11], 3, 4, 1, 2,
  209.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  210.   gtk_table_attach (GTK_TABLE (table), button[12], 3, 4, 2, 3,
  211.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  212.   gtk_table_attach (GTK_TABLE (table), button[13], 3, 4, 3, 4,
  213.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  214.   gtk_table_attach (GTK_TABLE (table), button[14], 3, 4, 4, 5,
  215.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 5);
  216.  
  217.  
  218.   for (a = 0; a < 15; a++)
  219.     {
  220.       g_signal_connect (G_OBJECT (button[a]), "clicked",
  221.                         G_CALLBACK (premuto), (gpointer) tastini[a]);
  222.     }
  223.  
  224.   //Creo l'entry e lo rendo non editabile
  225.   input = gtk_entry_new ();
  226.   gtk_editable_set_editable (GTK_EDITABLE (input), FALSE);
  227.   //Sistemo l'entry nella tabella
  228.   gtk_table_attach (GTK_TABLE (table), input, 0, 3, 0, 1,
  229.                     GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 5, 1);
  230.  
  231.   //Se la finestra viene chiusa il programma termina
  232.   g_signal_connect (G_OBJECT (win), "delete-event",
  233.                     G_CALLBACK (gtk_main_quit), NULL);
  234.  
  235.   //Rendo visibili tutti i widget presenti nella finestar win
  236.   gtk_widget_show_all (win);
  237.  
  238.   //Funzione per far rimanere la finestra aperta
  239.   gtk_main ();
  240.   return 0;
  241.   //FINE
  242. }

Simple calculator with GTK+ 2.0

By Red Skull