GUI by Michal
[BETON.git] / main.c
blob67aea7b89c29b673424a97bf7ae3663d1130e7ab
1 #include <gtk/gtk.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <limits.h>
5 #include "knihovna.h"
7 /* location of UI XML file relative to path in which program is running */
8 #define BUILDER_XML_FILE "kal.glade"
10 #define MAX_VALUE 9999
13 // struktura s ukazateli na jednotlive widgety v aplikaci
14 typedef struct
16 GtkWidget *window;
17 GtkWidget *button0;
18 GtkWidget *button1;
19 GtkWidget *button2;
20 GtkWidget *button3;
21 GtkWidget *button4;
22 GtkWidget *button5;
23 GtkWidget *button6;
24 GtkWidget *button7;
25 GtkWidget *button8;
26 GtkWidget *button9;
27 GtkWidget *button10;
28 GtkWidget *button11;
29 GtkWidget *button12;
30 GtkWidget *button13;
31 GtkWidget *button14;
32 GtkWidget *button15;
33 GtkWidget *button16;
34 GtkWidget *button17;
35 GtkWidget *button18;
36 GtkWidget *button19;
37 GtkLabel *label1;
38 } App;
40 //deklarace globalnich promenych pro vysledky kalkulacky
41 Tvysledek hodnota1;
43 Tvysledek hodnota2;
45 int operace=0; //udaj o tom, co se ma delat
46 int desetina=0; //pri hodnote 1 se už tvori desetina cisla
47 int cislic=0;
48 int omezeni=8; //omezeni kolik maximalne muze uzivatel zadat cislic na jedno cislo
50 * 0 - nic, nebylo zadano
51 * 1 - scitani
52 * 2 - odcitani
53 * 3 - nasobeni
54 * 4 - deleni
55 * 5 - umocneni
56 * 6 - logaritmus
57 * 7 - faktorial
60 // handler, ktery je v glade prirazen stisknuti tlacitka
61 void on_button0_clicked (GtkObject *object, App *app)
63 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
64 operace=1;
65 cislic=0;
66 desetina=0;
68 else{ //uz bylo vybrano a tak se to pouzije...
69 //zde by mela byt moznost scitani
71 /*int x;
72 x = atoi(gtk_entry_get_text(app->entry));
73 x++;
74 if(x > MAX_VALUE)
75 return;
77 char str[10];
78 snprintf(str,10,"%d",x);
79 gtk_entry_set_text(app->entry,str);*/
80 //app->label1->set_label("tlacitko 1");
81 //app->label1 = gtk_label_new("Cold was my");
82 //gtk_label_set_text(app->label1,"pokus");
85 void on_button1_clicked (GtkObject *object, App *app)
87 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
88 operace=2;
89 cislic=0;
90 desetina=0;
92 else{ //uz bylo vybrano a tak se to pouzije...
93 //zde by mela byt moznost scitani
96 void on_button2_clicked (GtkObject *object, App *app)
98 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
99 operace=3;
100 cislic=0;
101 desetina=0;
103 else{ //uz bylo vybrano a tak se to pouzije...
104 //zde by mela byt moznost scitani
107 void on_button3_clicked (GtkObject *object, App *app)
109 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
110 operace=4;
111 cislic=0;
112 desetina=0;
114 else{ //uz bylo vybrano a tak se to pouzije...
115 //zde by mela byt moznost scitani
118 void on_button4_clicked (GtkObject *object, App *app)
120 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
121 operace=5;
122 cislic=0;
123 desetina=0;
125 else{ //uz bylo vybrano a tak se to pouzije...
126 //zde by mela byt moznost scitani
129 void on_button5_clicked (GtkObject *object, App *app)
131 hodnota1=logaritmus(hodnota1.result);
132 char* x = malloc(sizeof(char)*15);
133 sprintf(x,"%.5f",hodnota1.result);
134 gtk_label_set_text(app->label1,x);
135 free(x);
136 operace=0; //uz nebude mozne zadavat hodnotu 1
137 cislic=8;
138 desetina=1;
140 void on_button6_clicked (GtkObject *object, App *app)
142 hodnota1=faktorial(hodnota1.result);
143 char* x = malloc(sizeof(char)*15);
144 sprintf(x,"%.5f",hodnota1.result);
145 gtk_label_set_text(app->label1,x);
146 free(x);
147 operace=0; //uz nebude mozne zadavat hodnotu 1
148 cislic=8;
149 desetina=1;
151 void on_button7_clicked (GtkObject *object, App *app)
153 operace=0;
154 desetina=0;
155 hodnota1.result=0;
156 hodnota1.error=0;
157 hodnota2.result=0;
158 hodnota2.error=0;
159 cislic=0;
160 gtk_label_set_text(app->label1,"Kalkulacka BETON");
162 void on_button8_clicked (GtkObject *object, App *app)
164 if (cislic<omezeni){
165 cislic++;
166 char* x = malloc(sizeof(char)*15);
167 if (operace==0) {
168 if (desetina==0){
169 hodnota1.result=hodnota1.result * 10 + 1;
171 else{
172 hodnota1.result=hodnota1.result + (1.0/pow(10,desetina));
173 desetina++;
175 sprintf(x,"%.5f",hodnota1.result);
176 gtk_label_set_text(app->label1,x);
178 else{
179 if (desetina==0){
180 hodnota2.result=hodnota2.result * 10 + 1;
182 else{
183 hodnota2.result=hodnota2.result + (1.0/pow(10,desetina));
184 desetina++;
186 sprintf(x,"%.5f",hodnota2.result);
187 gtk_label_set_text(app->label1,x);
189 free(x);
192 void on_button9_clicked (GtkObject *object, App *app)
194 if (cislic<omezeni){
195 cislic++;
196 char* x = malloc(sizeof(char)*15);
197 if (operace==0) {
198 if (desetina==0){
199 hodnota1.result=hodnota1.result * 10 + 2;
201 else{
202 hodnota1.result=hodnota1.result + (2.0/pow(10,desetina));
203 desetina++;
205 sprintf(x,"%.5f",hodnota1.result);
206 gtk_label_set_text(app->label1,x);
208 else{
209 if (desetina==0){
210 hodnota2.result=hodnota2.result * 10 + 2;
212 else{
213 hodnota2.result=hodnota2.result + (2.0/pow(10,desetina));
214 desetina++;
216 sprintf(x,"%.5f",hodnota2.result);
217 gtk_label_set_text(app->label1,x);
219 free(x);
222 void on_button10_clicked (GtkObject *object, App *app)
224 if (cislic<omezeni){
225 cislic++;
226 char* x = malloc(sizeof(char)*15);
227 if (operace==0) {
228 if (desetina==0){
229 hodnota1.result=hodnota1.result * 10 + 3;
231 else{
232 hodnota1.result=hodnota1.result + (3.0/pow(10,desetina));
233 desetina++;
235 sprintf(x,"%.5f",hodnota1.result);
236 gtk_label_set_text(app->label1,x);
238 else{
239 if (desetina==0){
240 hodnota2.result=hodnota2.result * 10 + 3;
242 else{
243 hodnota2.result=hodnota2.result + (3.0/pow(10,desetina));
244 desetina++;
246 sprintf(x,"%.5f",hodnota2.result);
247 gtk_label_set_text(app->label1,x);
249 free(x);
252 void on_button11_clicked (GtkObject *object, App *app)
254 if (cislic<omezeni){
255 cislic++;
256 char* x = malloc(sizeof(char)*15);
257 if (operace==0) {
258 if (desetina==0){
259 hodnota1.result=hodnota1.result * 10 + 4;
261 else{
262 hodnota1.result=hodnota1.result + (4.0/pow(10,desetina));
263 desetina++;
265 sprintf(x,"%.5f",hodnota1.result);
266 gtk_label_set_text(app->label1,x);
268 else{
269 if (desetina==0){
270 hodnota2.result=hodnota2.result * 10 + 4;
272 else{
273 hodnota2.result=hodnota2.result + (4.0/pow(10,desetina));
274 desetina++;
276 sprintf(x,"%.5f",hodnota2.result);
277 gtk_label_set_text(app->label1,x);
279 free(x);
282 void on_button12_clicked (GtkObject *object, App *app)
284 if (cislic<omezeni){
285 cislic++;
286 char* x = malloc(sizeof(char)*15);
287 if (operace==0) {
288 if (desetina==0){
289 hodnota1.result=hodnota1.result * 10 + 5;
291 else{
292 hodnota1.result=hodnota1.result + (5.0/pow(10,desetina));
293 desetina++;
295 sprintf(x,"%.5f",hodnota1.result);
296 gtk_label_set_text(app->label1,x);
298 else{
299 if (desetina==0){
300 hodnota2.result=hodnota2.result * 10 + 5;
302 else{
303 hodnota2.result=hodnota2.result + (5.0/pow(10,desetina));
304 desetina++;
306 sprintf(x,"%.5f",hodnota2.result);
307 gtk_label_set_text(app->label1,x);
309 free(x);
312 void on_button13_clicked (GtkObject *object, App *app)
314 if (cislic<omezeni){
315 cislic++;
316 char* x = malloc(sizeof(char)*15);
317 if (operace==0) {
318 if (desetina==0){
319 hodnota1.result=hodnota1.result * 10 + 6;
321 else{
322 hodnota1.result=hodnota1.result + (6.0/pow(10,desetina));
323 desetina++;
325 sprintf(x,"%.5f",hodnota1.result);
326 gtk_label_set_text(app->label1,x);
328 else{
329 if (desetina==0){
330 hodnota2.result=hodnota2.result * 10 + 6;
332 else{
333 hodnota2.result=hodnota2.result + (6.0/pow(10,desetina));
334 desetina++;
336 sprintf(x,"%.5f",hodnota2.result);
337 gtk_label_set_text(app->label1,x);
339 free(x);
342 void on_button14_clicked (GtkObject *object, App *app)
344 if (cislic<omezeni){
345 cislic++;
346 char* x = malloc(sizeof(char)*15);
347 if (operace==0) {
348 if (desetina==0){
349 hodnota1.result=hodnota1.result * 10 + 7;
351 else{
352 hodnota1.result=hodnota1.result + (7.0/pow(10,desetina));
353 desetina++;
355 sprintf(x,"%.5f",hodnota1.result);
356 gtk_label_set_text(app->label1,x);
358 else{
359 if (desetina==0){
360 hodnota2.result=hodnota2.result * 10 + 7;
362 else{
363 hodnota2.result=hodnota2.result + (7.0/pow(10,desetina));
364 desetina++;
366 sprintf(x,"%.5f",hodnota2.result);
367 gtk_label_set_text(app->label1,x);
369 free(x);
372 void on_button15_clicked (GtkObject *object, App *app)
374 if (cislic<omezeni){
375 cislic++;
376 char* x = malloc(sizeof(char)*15);
377 if (operace==0) {
378 if (desetina==0){
379 hodnota1.result=hodnota1.result * 10 + 8;
381 else{
382 hodnota1.result=hodnota1.result + (8.0/pow(10,desetina));
383 desetina++;
385 sprintf(x,"%.5f",hodnota1.result);
386 gtk_label_set_text(app->label1,x);
388 else{
389 if (desetina==0){
390 hodnota2.result=hodnota2.result * 10 + 8;
392 else{
393 hodnota2.result=hodnota2.result + (8.0/pow(10,desetina));
394 desetina++;
396 sprintf(x,"%.5f",hodnota2.result);
397 gtk_label_set_text(app->label1,x);
399 free(x);
402 void on_button16_clicked (GtkObject *object, App *app)
404 if (cislic<omezeni){
405 cislic++;
406 char* x = malloc(sizeof(char)*15);
407 if (operace==0) {
408 if (desetina==0){
409 hodnota1.result=hodnota1.result * 10 + 0;
411 else{
412 hodnota1.result=hodnota1.result + (0.0/pow(10,desetina));
413 desetina++;
415 sprintf(x,"%.5f",hodnota1.result);
416 gtk_label_set_text(app->label1,x);
418 else{
419 if (desetina==0){
420 hodnota2.result=hodnota2.result * 10 + 0;
422 else{
423 hodnota2.result=hodnota2.result + (0.0/pow(10,desetina));
424 desetina++;
426 sprintf(x,"%.5f",hodnota2.result);
427 gtk_label_set_text(app->label1,x);
429 free(x);
432 void on_button17_clicked (GtkObject *object, App *app)
434 if (desetina==0){
435 desetina=1;
436 //mozna jeste neco
438 else{ //vypsani chyby pokud se vickrat klikne na desetinu
442 void on_button18_clicked (GtkObject *object, App *app)
444 if (cislic<omezeni){
445 cislic++;
446 char* x = malloc(sizeof(char)*15);
447 if (operace==0) {
448 if (desetina==0){
449 hodnota1.result=hodnota1.result * 10 + 9;
451 else{
452 hodnota1.result=hodnota1.result + (9.0/pow(10,desetina));
453 desetina++;
455 sprintf(x,"%.5f",hodnota1.result);
456 gtk_label_set_text(app->label1,x);
458 else{
459 if (desetina==0){
460 hodnota2.result=hodnota2.result * 10 + 9;
462 else{
463 hodnota2.result=hodnota2.result + (9.0/pow(10,desetina));
464 desetina++;
466 sprintf(x,"%.5f",hodnota2.result);
467 gtk_label_set_text(app->label1,x);
469 free(x);
472 void on_button19_clicked (GtkObject *object, App *app)
474 /* * 0 - nic, nebylo zadano
475 * 1 - scitani
476 * 2 - odcitani
477 * 3 - nasobeni
478 * 4 - deleni
479 * 5 - umocneni
480 * 6 - logaritmus
481 * 7 - faktorial
482 * 8 - rovnase - hodnota pro zmenu
484 char* x = malloc(sizeof(char)*15);
485 switch (operace){
486 case 1:
487 hodnota1=scitani(hodnota1.result, hodnota2.result);
488 sprintf(x,"%.5f",hodnota1.result);
489 gtk_label_set_text(app->label1,x);
490 hodnota2.result=0;
491 break;
493 case 2:
494 hodnota1=odcitani(hodnota1.result, hodnota2.result);
495 sprintf(x,"%.5f",hodnota1.result);
496 gtk_label_set_text(app->label1,x);
497 hodnota2.result=0;
498 break;
500 case 3:
501 hodnota1=nasobeni(hodnota1.result, hodnota2.result);
502 sprintf(x,"%.5f",hodnota1.result);
503 gtk_label_set_text(app->label1,x);
504 hodnota2.result=0;
505 break;
507 case 4:
508 hodnota1=deleni(hodnota1.result, hodnota2.result);
509 if (hodnota1.error!=0){
510 gtk_label_set_text(app->label1,"EROR");
511 operace=0;
512 desetina=0;
513 hodnota1.result=0;
514 hodnota2.result=0;
515 cislic=0;
517 else{
518 sprintf(x,"%.5f",hodnota1.result);
519 gtk_label_set_text(app->label1,x);
520 hodnota2.result=0;
522 break;
524 case 5:
525 hodnota1=umocneni(hodnota1.result, hodnota2.result);
526 sprintf(x,"%.5f",hodnota1.result);
527 gtk_label_set_text(app->label1,x);
528 hodnota2.result=0;
529 break;
532 free(x);
533 operace=8;
537 // ukonci hlavni smycku gtk pri zavreni okna
538 void on_window_destroy (GtkObject *object, gpointer user_data)
540 gtk_main_quit ();
544 int main (int argc, char *argv[])
546 hodnota1.result=0;
547 hodnota1.error=0;
548 hodnota2.result=0;
549 hodnota2.error=0;
550 App *app;
551 GtkBuilder *builder;
553 // alokuje pamet pro app
554 app = g_slice_new(App);
556 gtk_init (&argc, &argv);
558 // nacte GUI z XML
559 builder = gtk_builder_new ();
560 gtk_builder_add_from_file (builder, BUILDER_XML_FILE, NULL);
562 // priradi do struktury jednotlive widgety
563 app->window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
564 app->button0 = GTK_WIDGET (gtk_builder_get_object (builder, "button0"));
565 app->button1 = GTK_WIDGET (gtk_builder_get_object (builder, "button1"));
566 app->button2 = GTK_WIDGET (gtk_builder_get_object (builder, "button2"));
567 app->button3 = GTK_WIDGET (gtk_builder_get_object (builder, "button3"));
568 app->button4 = GTK_WIDGET (gtk_builder_get_object (builder, "button4"));
569 app->button5 = GTK_WIDGET (gtk_builder_get_object (builder, "button5"));
570 app->button6 = GTK_WIDGET (gtk_builder_get_object (builder, "button6"));
571 app->button7 = GTK_WIDGET (gtk_builder_get_object (builder, "button7"));
572 app->button8 = GTK_WIDGET (gtk_builder_get_object (builder, "button8"));
573 app->button9 = GTK_WIDGET (gtk_builder_get_object (builder, "button9"));
574 app->button10 = GTK_WIDGET (gtk_builder_get_object (builder, "button10"));
575 app->button11 = GTK_WIDGET (gtk_builder_get_object (builder, "button11"));
576 app->button12 = GTK_WIDGET (gtk_builder_get_object (builder, "button12"));
577 app->button13 = GTK_WIDGET (gtk_builder_get_object (builder, "button13"));
578 app->button14 = GTK_WIDGET (gtk_builder_get_object (builder, "button14"));
579 app->button15 = GTK_WIDGET (gtk_builder_get_object (builder, "button15"));
580 app->button16 = GTK_WIDGET (gtk_builder_get_object (builder, "button16"));
581 app->button17 = GTK_WIDGET (gtk_builder_get_object (builder, "button17"));
582 app->button18 = GTK_WIDGET (gtk_builder_get_object (builder, "button18"));
583 app->button19 = GTK_WIDGET (gtk_builder_get_object (builder, "button19"));
584 app->label1 = GTK_LABEL (gtk_builder_get_object (builder, "label1"));
586 gtk_builder_connect_signals (builder, app);
588 g_object_unref (G_OBJECT (builder));
590 gtk_widget_show (app->window);
592 //hlavni smycka gtk
593 gtk_main ();
595 return 0;