Created
March 26, 2012 16:47
-
-
Save Phitherek/2206441 to your computer and use it in GitHub Desktop.
Phitherek_' s Decider v. 0.1 GTK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Kompiluj przy użyciu/compile using: | |
g++ -o decider_gtk decider_gtk.cpp `pkg-config --cflags --libs gtk+-2.0` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <sstream> | |
#include <string> | |
#include <gtk/gtk.h> | |
using namespace std; | |
string res, line; | |
int p, d, t, sum, lim; | |
GtkTextBuffer *output; // Na output | |
GtkTextIter ti; // Wiadomo... | |
GtkWidget *tv; // TextView na output | |
GtkWidget *pentry; // Na TextEntry - wyniki, żeby ładnie wyglądały | |
GtkWidget *dentry; | |
GtkWidget *tentry; | |
GtkWidget *sumentry; | |
GtkWidget *limentry; | |
GtkWidget *reslabel; // Result Label | |
// Callbacki: | |
static gboolean delete_event( GtkWidget *widget, | |
GdkEvent *event, | |
gpointer data ) | |
{ | |
gtk_main_quit (); | |
return false; | |
} | |
static void exit(GtkWidget *widget, gpointer data) { | |
gtk_text_iter_free(&ti); | |
gtk_main_quit(); | |
} | |
static void test(GtkWidget *widget, gpointer data) { | |
p = rand()%20+1; | |
d = rand()%20+1; | |
t = rand()%20+1; | |
string pc, dc, tc, sumc, limc, tt; | |
stringstream pss, dss, tss, sumss, limss, ttss; | |
pss << p; | |
pc = pss.str(); | |
dss << d; | |
dc = dss.str(); | |
tss << t; | |
tc = tss.str(); | |
sum = p+d+t; | |
sumss << sum; | |
sumc = sumss.str(); | |
ttss << (gchar*)data; | |
tt = ttss.str(); | |
if(tt == "l") { | |
lim = 15; | |
} else if(tt == "s") { | |
lim = 30; | |
} else if(tt == "t") { | |
lim = 50; | |
} | |
if(sum>=lim) { | |
res = "pozytywny"; | |
} else { | |
res = "negatywny"; | |
} | |
limss << lim; | |
limc = limss.str(); | |
line = "Poziom trudności testu: "; | |
line += tt; | |
line += '\n'; | |
gtk_text_buffer_set_text(output, line.c_str(), -1); | |
gtk_text_buffer_get_end_iter(output, &ti); | |
gtk_entry_set_text(GTK_ENTRY(pentry), pc.c_str()); | |
line = "Wylosowane wyniki: "; | |
line += pc; | |
line += ", "; | |
gtk_entry_set_text(GTK_ENTRY(dentry), dc.c_str()); | |
line += dc; | |
line += ", "; | |
gtk_entry_set_text(GTK_ENTRY(tentry), tc.c_str()); | |
line += tc; | |
line += '\n'; | |
gtk_text_buffer_insert(output, &ti, line.c_str(), -1); | |
gtk_entry_set_text(GTK_ENTRY(sumentry), sumc.c_str()); | |
line = "Suma: "; | |
line += sumc; | |
line += '\n'; | |
gtk_text_buffer_insert(output, &ti, line.c_str(), -1); | |
gtk_entry_set_text(GTK_ENTRY(limentry), limc.c_str()); | |
line = "Próg testu: "; | |
line += limc; | |
line += '\n'; | |
gtk_label_set_text(GTK_LABEL(reslabel), res.c_str()); | |
line += "Wynik testu: "; | |
line += res; | |
gtk_text_buffer_insert(output, &ti, line.c_str(), -1); | |
} | |
int main(int argc, char** argv) { | |
char nullsign = '-'; | |
string l = "l"; | |
string s = "s"; | |
string t = "t"; | |
// Widgety GTK+: | |
GtkWidget *window; // Okno | |
GtkWidget *vbox; // Główny vbox | |
GtkWidget *hbox; // Do tworzenia hboxów dla poszczególnych elementów | |
GtkWidget *button; // Do tworzenia przycisków | |
GtkWidget *label; // Na labelki | |
GtkWidget *hsep; // Na separator | |
srand(time(NULL)); | |
// GTK Init: | |
gtk_init(&argc, &argv); | |
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
gtk_window_set_title(GTK_WINDOW(window), "Phitherek_' s Decider v. 0.1 GTK (c) 2012 by Phitherek_"); | |
g_signal_connect(window, "delete-event", G_CALLBACK (delete_event), NULL); | |
vbox = gtk_vbox_new(false, 5); | |
hbox = gtk_hbox_new(false, 5); | |
output = gtk_text_buffer_new(NULL); | |
gtk_text_buffer_set_text(output, "--------Brak wyników--------", -1); | |
label = gtk_label_new("Wybierz poziom trudności testu: "); | |
gtk_box_pack_start(GTK_BOX(hbox), label, true, true, 5); | |
gtk_widget_show(label); | |
button = gtk_button_new_with_label("Łatwy"); | |
g_signal_connect(button, "clicked", G_CALLBACK(test), (gpointer) l.c_str()); | |
gtk_box_pack_start(GTK_BOX(hbox), button, true, true, 5); | |
gtk_widget_show(button); | |
button = gtk_button_new_with_label("Średni"); | |
g_signal_connect(button, "clicked", G_CALLBACK(test), (gpointer) s.c_str()); | |
gtk_box_pack_start(GTK_BOX(hbox), button, true, true, 5); | |
gtk_widget_show(button); | |
button = gtk_button_new_with_label("Trudny"); | |
g_signal_connect(button, "clicked", G_CALLBACK(test), (gpointer) t.c_str()); | |
gtk_box_pack_start(GTK_BOX(hbox), button, true, true, 5); | |
gtk_widget_show(button); | |
gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 5); | |
gtk_widget_show(hbox); | |
hsep = gtk_hseparator_new(); | |
gtk_box_pack_start(GTK_BOX(vbox), hsep, true, true, 5); | |
gtk_widget_show(hsep); | |
hbox = gtk_hbox_new(false, 5); | |
label = gtk_label_new("Wylosowane wyniki: "); | |
gtk_box_pack_start(GTK_BOX(hbox), label, true, true, 5); | |
gtk_widget_show(label); | |
pentry = gtk_entry_new(); | |
gtk_entry_set_text(GTK_ENTRY(pentry), &nullsign); | |
gtk_entry_set_editable(GTK_ENTRY(pentry), 0); | |
gtk_box_pack_start(GTK_BOX(hbox), pentry, true, true, 5); | |
gtk_widget_show(pentry); | |
dentry = gtk_entry_new(); | |
gtk_entry_set_text(GTK_ENTRY(dentry), &nullsign); | |
gtk_entry_set_editable(GTK_ENTRY(dentry), 0); | |
gtk_box_pack_start(GTK_BOX(hbox), dentry, true, true, 5); | |
gtk_widget_show(dentry); | |
tentry = gtk_entry_new(); | |
gtk_entry_set_text(GTK_ENTRY(tentry), &nullsign); | |
gtk_entry_set_editable(GTK_ENTRY(tentry), 0); | |
gtk_box_pack_start(GTK_BOX(hbox), tentry, true, true, 5); | |
gtk_widget_show(tentry); | |
gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 5); | |
gtk_widget_show(hbox); | |
hbox = gtk_hbox_new(false, 5); | |
label = gtk_label_new("Suma: "); | |
gtk_box_pack_start(GTK_BOX(hbox), label, true, true, 5); | |
gtk_widget_show(label); | |
sumentry = gtk_entry_new(); | |
gtk_entry_set_text(GTK_ENTRY(sumentry), &nullsign); | |
gtk_entry_set_editable(GTK_ENTRY(sumentry), 0); | |
gtk_box_pack_start(GTK_BOX(hbox), sumentry, true, true, 5); | |
gtk_widget_show(sumentry); | |
gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 5); | |
gtk_widget_show(hbox); | |
hbox = gtk_hbox_new(false, 5); | |
label = gtk_label_new("Próg testu: "); | |
gtk_box_pack_start(GTK_BOX(hbox), label, true, true, 5); | |
gtk_widget_show(label); | |
limentry = gtk_entry_new(); | |
gtk_entry_set_text(GTK_ENTRY(limentry), &nullsign); | |
gtk_entry_set_editable(GTK_ENTRY(limentry), 0); | |
gtk_box_pack_start(GTK_BOX(hbox), limentry, true, true, 5); | |
gtk_widget_show(limentry); | |
gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 5); | |
gtk_widget_show(hbox); | |
hbox = gtk_hbox_new(false, 5); | |
label = gtk_label_new("Wynik testu: "); | |
gtk_box_pack_start(GTK_BOX(hbox), label, true, true, 5); | |
gtk_widget_show(label); | |
reslabel = gtk_label_new("---BRAK-WYNIKU---"); | |
gtk_box_pack_start(GTK_BOX(hbox), reslabel, true, true, 5); | |
gtk_widget_show(reslabel); | |
gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 5); | |
gtk_widget_show(hbox); | |
hbox = gtk_hbox_new(false, 5); | |
label = gtk_label_new("Output (do ewenualnego skopiowania): "); | |
gtk_box_pack_start(GTK_BOX(hbox), label, true, true, 5); | |
gtk_widget_show(label); | |
output = gtk_text_buffer_new(NULL); | |
gtk_text_buffer_set_text(output, "---BRAK-WYNIKU---", -1); | |
tv = gtk_text_view_new_with_buffer(output); | |
gtk_text_view_set_editable(GTK_TEXT_VIEW(tv), 0); | |
gtk_box_pack_start(GTK_BOX(hbox), tv, true, true, 5); | |
gtk_widget_show(tv); | |
gtk_box_pack_start(GTK_BOX(vbox), hbox, true, true, 5); | |
gtk_widget_show(hbox); | |
hsep = gtk_hseparator_new(); | |
gtk_box_pack_start(GTK_BOX(vbox), hsep, true, true, 5); | |
gtk_widget_show(hsep); | |
button = gtk_button_new_with_label("Zakończ"); | |
g_signal_connect(button, "clicked", G_CALLBACK("exit"), NULL); | |
gtk_box_pack_start(GTK_BOX(vbox), button, true, true, 5); | |
gtk_widget_show(button); | |
gtk_container_add(GTK_CONTAINER(window), vbox); | |
gtk_widget_show(vbox); | |
gtk_widget_show(window); | |
gtk_main(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment