From fdf53c31d4dd34c2d69d3434c8e0423728ede0e7 Mon Sep 17 00:00:00 2001 From: Jakuje Date: Tue, 3 May 2011 14:18:06 +0200 Subject: [PATCH] GUI by Michal --- Makefile | 33 ++++ kal.glade | 326 ++++++++++++++++++++++++++++++++++ main.c | 596 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 26 --- 4 files changed, 955 insertions(+), 26 deletions(-) create mode 100644 Makefile create mode 100644 kal.glade create mode 100644 main.c delete mode 100644 makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d2bda71 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ + +CC=gcc +CFLAGS=-std=c99 -g -pedantic -Wall -lm -export-dynamic `pkg-config --cflags --libs gtk+-2.0` + +OBJFILES = testy.o knihovna.o +.PHONY: dep clean + +BIN=KAL_BETON + +#%.o : %.c +# $(CC) $(CFLAGS) -c $< + +all: $(BIN) + +dep: + $(CC) -MM *.c > dep.list + +-include dep.list + +testy: $(OBJFILES) + $(CC) $(CFLAGS) $(OBJFILES) -o $@ + +#knihovna: knihovna.o +# $(CC) $(CFLAGS) $^ -o $@ + +clean: + rm -f knihovna testy *.o $(BIN) + + +$(BIN): main.c kal.glade knihovna.o +# gtk-builder-convert IVS_example.glade IVS_example.xml + $(CC) $(CFLAGS) -o $@ main.c knihovna.o + diff --git a/kal.glade b/kal.glade new file mode 100644 index 0000000..50b6c06 --- /dev/null +++ b/kal.glade @@ -0,0 +1,326 @@ + + + + + + BETON + + + True + True + + + True + 2 + 4 + + + + + True + True + True + + + + + + - + True + True + True + + + + 1 + 2 + + + + + * + True + True + True + + + + 2 + 3 + + + + + : + True + True + True + + + + 3 + 4 + + + + + X^n + True + True + True + + + + 1 + 2 + + + + + log + True + True + True + + + + 1 + 2 + 1 + 2 + + + + + n! + True + True + True + + + + 2 + 3 + 1 + 2 + + + + + CE + True + True + True + + + + 3 + 4 + 1 + 2 + + + + + False + True + + + + + True + True + + + True + Kalkulačka BETON + + + False + True + + + + + True + 3 + 4 + + + 1 + True + True + True + + + + + + 2 + True + True + True + + + + 1 + 2 + + + + + 3 + True + True + True + + + + 2 + 3 + + + + + 4 + True + True + True + + + + 3 + 4 + + + + + 5 + True + True + True + + + + 1 + 2 + + + + + 6 + True + True + True + + + + 1 + 2 + 1 + 2 + + + + + 7 + True + True + True + + + + 2 + 3 + 1 + 2 + + + + + 8 + True + True + True + + + + 3 + 4 + 1 + 2 + + + + + 0 + True + True + True + + + + 1 + 2 + 2 + 3 + + + + + , + True + True + True + + + + 2 + 3 + 2 + 3 + + + + + 9 + True + True + True + + + + 2 + 3 + + + + + = + True + True + True + + + + 3 + 4 + 2 + 3 + + + + + True + True + + + + + True + True + + + + + + diff --git a/main.c b/main.c new file mode 100644 index 0000000..67aea7b --- /dev/null +++ b/main.c @@ -0,0 +1,596 @@ +#include +#include +#include +#include +#include "knihovna.h" + +/* location of UI XML file relative to path in which program is running */ +#define BUILDER_XML_FILE "kal.glade" + +#define MAX_VALUE 9999 + + +// struktura s ukazateli na jednotlive widgety v aplikaci +typedef struct +{ + GtkWidget *window; + GtkWidget *button0; + GtkWidget *button1; + GtkWidget *button2; + GtkWidget *button3; + GtkWidget *button4; + GtkWidget *button5; + GtkWidget *button6; + GtkWidget *button7; + GtkWidget *button8; + GtkWidget *button9; + GtkWidget *button10; + GtkWidget *button11; + GtkWidget *button12; + GtkWidget *button13; + GtkWidget *button14; + GtkWidget *button15; + GtkWidget *button16; + GtkWidget *button17; + GtkWidget *button18; + GtkWidget *button19; + GtkLabel *label1; +} App; + +//deklarace globalnich promenych pro vysledky kalkulacky + Tvysledek hodnota1; + + Tvysledek hodnota2; + + int operace=0; //udaj o tom, co se ma delat + int desetina=0; //pri hodnote 1 se už tvori desetina cisla + int cislic=0; + int omezeni=8; //omezeni kolik maximalne muze uzivatel zadat cislic na jedno cislo + /* + * 0 - nic, nebylo zadano + * 1 - scitani + * 2 - odcitani + * 3 - nasobeni + * 4 - deleni + * 5 - umocneni + * 6 - logaritmus + * 7 - faktorial + */ + +// handler, ktery je v glade prirazen stisknuti tlacitka +void on_button0_clicked (GtkObject *object, App *app) +{ + if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo + operace=1; + cislic=0; + desetina=0; + } + else{ //uz bylo vybrano a tak se to pouzije... + //zde by mela byt moznost scitani + } + /*int x; + x = atoi(gtk_entry_get_text(app->entry)); + x++; + if(x > MAX_VALUE) + return; + + char str[10]; + snprintf(str,10,"%d",x); + gtk_entry_set_text(app->entry,str);*/ + //app->label1->set_label("tlacitko 1"); + //app->label1 = gtk_label_new("Cold was my"); + //gtk_label_set_text(app->label1,"pokus"); +} + +void on_button1_clicked (GtkObject *object, App *app) +{ + if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo + operace=2; + cislic=0; + desetina=0; + } + else{ //uz bylo vybrano a tak se to pouzije... + //zde by mela byt moznost scitani + } +} +void on_button2_clicked (GtkObject *object, App *app) +{ + if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo + operace=3; + cislic=0; + desetina=0; + } + else{ //uz bylo vybrano a tak se to pouzije... + //zde by mela byt moznost scitani + } +} +void on_button3_clicked (GtkObject *object, App *app) +{ + if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo + operace=4; + cislic=0; + desetina=0; + } + else{ //uz bylo vybrano a tak se to pouzije... + //zde by mela byt moznost scitani + } +} +void on_button4_clicked (GtkObject *object, App *app) +{ + if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo + operace=5; + cislic=0; + desetina=0; + } + else{ //uz bylo vybrano a tak se to pouzije... + //zde by mela byt moznost scitani + } +} +void on_button5_clicked (GtkObject *object, App *app) +{ + hodnota1=logaritmus(hodnota1.result); + char* x = malloc(sizeof(char)*15); + sprintf(x,"%.5f",hodnota1.result); + gtk_label_set_text(app->label1,x); + free(x); + operace=0; //uz nebude mozne zadavat hodnotu 1 + cislic=8; + desetina=1; +} +void on_button6_clicked (GtkObject *object, App *app) +{ + hodnota1=faktorial(hodnota1.result); + char* x = malloc(sizeof(char)*15); + sprintf(x,"%.5f",hodnota1.result); + gtk_label_set_text(app->label1,x); + free(x); + operace=0; //uz nebude mozne zadavat hodnotu 1 + cislic=8; + desetina=1; +} +void on_button7_clicked (GtkObject *object, App *app) +{ + operace=0; + desetina=0; + hodnota1.result=0; + hodnota1.error=0; + hodnota2.result=0; + hodnota2.error=0; + cislic=0; + gtk_label_set_text(app->label1,"Kalkulacka BETON"); +} +void on_button8_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 1; + } + else{ + hodnota2.result=hodnota2.result + (1.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button9_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 2; + } + else{ + hodnota2.result=hodnota2.result + (2.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button10_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 3; + } + else{ + hodnota2.result=hodnota2.result + (3.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button11_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 4; + } + else{ + hodnota2.result=hodnota2.result + (4.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button12_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 5; + } + else{ + hodnota2.result=hodnota2.result + (5.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button13_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 6; + } + else{ + hodnota2.result=hodnota2.result + (6.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button14_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 7; + } + else{ + hodnota2.result=hodnota2.result + (7.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button15_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 8; + } + else{ + hodnota2.result=hodnota2.result + (8.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button16_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 0; + } + else{ + hodnota2.result=hodnota2.result + (0.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button17_clicked (GtkObject *object, App *app) +{ + if (desetina==0){ + desetina=1; + //mozna jeste neco + } + else{ //vypsani chyby pokud se vickrat klikne na desetinu + + } +} +void on_button18_clicked (GtkObject *object, App *app) +{ + if (cisliclabel1,x); + } + else{ + if (desetina==0){ + hodnota2.result=hodnota2.result * 10 + 9; + } + else{ + hodnota2.result=hodnota2.result + (9.0/pow(10,desetina)); + desetina++; + } + sprintf(x,"%.5f",hodnota2.result); + gtk_label_set_text(app->label1,x); + } + free(x); + } +} +void on_button19_clicked (GtkObject *object, App *app) +{ + /* * 0 - nic, nebylo zadano + * 1 - scitani + * 2 - odcitani + * 3 - nasobeni + * 4 - deleni + * 5 - umocneni + * 6 - logaritmus + * 7 - faktorial + * 8 - rovnase - hodnota pro zmenu + */ + char* x = malloc(sizeof(char)*15); + switch (operace){ + case 1: + hodnota1=scitani(hodnota1.result, hodnota2.result); + sprintf(x,"%.5f",hodnota1.result); + gtk_label_set_text(app->label1,x); + hodnota2.result=0; + break; + + case 2: + hodnota1=odcitani(hodnota1.result, hodnota2.result); + sprintf(x,"%.5f",hodnota1.result); + gtk_label_set_text(app->label1,x); + hodnota2.result=0; + break; + + case 3: + hodnota1=nasobeni(hodnota1.result, hodnota2.result); + sprintf(x,"%.5f",hodnota1.result); + gtk_label_set_text(app->label1,x); + hodnota2.result=0; + break; + + case 4: + hodnota1=deleni(hodnota1.result, hodnota2.result); + if (hodnota1.error!=0){ + gtk_label_set_text(app->label1,"EROR"); + operace=0; + desetina=0; + hodnota1.result=0; + hodnota2.result=0; + cislic=0; + } + else{ + sprintf(x,"%.5f",hodnota1.result); + gtk_label_set_text(app->label1,x); + hodnota2.result=0; + } + break; + + case 5: + hodnota1=umocneni(hodnota1.result, hodnota2.result); + sprintf(x,"%.5f",hodnota1.result); + gtk_label_set_text(app->label1,x); + hodnota2.result=0; + break; + + } + free(x); + operace=8; +} + + +// ukonci hlavni smycku gtk pri zavreni okna +void on_window_destroy (GtkObject *object, gpointer user_data) +{ + gtk_main_quit (); +} + + +int main (int argc, char *argv[]) +{ + hodnota1.result=0; + hodnota1.error=0; + hodnota2.result=0; + hodnota2.error=0; + App *app; + GtkBuilder *builder; + + // alokuje pamet pro app + app = g_slice_new(App); + + gtk_init (&argc, &argv); + + // nacte GUI z XML + builder = gtk_builder_new (); + gtk_builder_add_from_file (builder, BUILDER_XML_FILE, NULL); + + // priradi do struktury jednotlive widgety + app->window = GTK_WIDGET (gtk_builder_get_object (builder, "window1")); + app->button0 = GTK_WIDGET (gtk_builder_get_object (builder, "button0")); + app->button1 = GTK_WIDGET (gtk_builder_get_object (builder, "button1")); + app->button2 = GTK_WIDGET (gtk_builder_get_object (builder, "button2")); + app->button3 = GTK_WIDGET (gtk_builder_get_object (builder, "button3")); + app->button4 = GTK_WIDGET (gtk_builder_get_object (builder, "button4")); + app->button5 = GTK_WIDGET (gtk_builder_get_object (builder, "button5")); + app->button6 = GTK_WIDGET (gtk_builder_get_object (builder, "button6")); + app->button7 = GTK_WIDGET (gtk_builder_get_object (builder, "button7")); + app->button8 = GTK_WIDGET (gtk_builder_get_object (builder, "button8")); + app->button9 = GTK_WIDGET (gtk_builder_get_object (builder, "button9")); + app->button10 = GTK_WIDGET (gtk_builder_get_object (builder, "button10")); + app->button11 = GTK_WIDGET (gtk_builder_get_object (builder, "button11")); + app->button12 = GTK_WIDGET (gtk_builder_get_object (builder, "button12")); + app->button13 = GTK_WIDGET (gtk_builder_get_object (builder, "button13")); + app->button14 = GTK_WIDGET (gtk_builder_get_object (builder, "button14")); + app->button15 = GTK_WIDGET (gtk_builder_get_object (builder, "button15")); + app->button16 = GTK_WIDGET (gtk_builder_get_object (builder, "button16")); + app->button17 = GTK_WIDGET (gtk_builder_get_object (builder, "button17")); + app->button18 = GTK_WIDGET (gtk_builder_get_object (builder, "button18")); + app->button19 = GTK_WIDGET (gtk_builder_get_object (builder, "button19")); + app->label1 = GTK_LABEL (gtk_builder_get_object (builder, "label1")); + + gtk_builder_connect_signals (builder, app); + + g_object_unref (G_OBJECT (builder)); + + gtk_widget_show (app->window); + + //hlavni smycka gtk + gtk_main (); + + return 0; +} diff --git a/makefile b/makefile deleted file mode 100644 index 60eaf83..0000000 --- a/makefile +++ /dev/null @@ -1,26 +0,0 @@ - -CC=gcc -CFLAGS=-std=c99 -g -pedantic -Wall -lm - -OBJFILES = testy.o knihovna.o -.PHONY: dep clean - -#%.o : %.c -# $(CC) $(CFLAGS) -c $< - -all: testy - -dep: - $(CC) -MM *.c > dep.list - --include dep.list - -testy: $(OBJFILES) - $(CC) $(CFLAGS) $(OBJFILES) -o $@ - -#knihovna: knihovna.o -# $(CC) $(CFLAGS) $^ -o $@ - -clean: - rm -f knihovna testy *.o - -- 2.11.4.GIT