Anda di halaman 1dari 14

Um exemplo GTK + MYSQL em C

#include <string.h> #include <gtk/gtk.h> #include <mysql/mysql.h> GtkWidget *window_cadastro, *window_incluir_s, *window_alterar, *window_clist; *entrada_celular, *entrada_residencia, *entrada_nome; GtkWidget *altera_celular, *altera_nome, *altera_residencia; int tem_janela = FALSE; int tem_janela_incluir = FALSE; int tem_janela_alterar = FALSE; MYSQL *conexao_mysql; MYSQL_RES *res_mysql; static static static static static static static static static static static static static static static static static static void executa_query(gchar *query); void confirma_alteracao(GtkWidget *win); void cancela_alteracao(GtkWidget *win); void dialogo(GtkWidget *win); void alterar (GtkWidget *clist, gint row, gint column); void excluir (GtkWidget *clist, gint row, gint column); void Incluir(GtkWidget *win); GtkWidget *create_window_incluir(); void main_window(); GtkWidget *create_window_cadastro(); void cadastro(); void display_message(GtkWidget *actual_window, gchar *message); int crialista(gpointer data); gboolean delet_event_main(); void delete_event_cadastro(GtkWidget *win); void confirma_inclusao(); int incluir_no_bd(); void saida_incluir();

GtkWidget

void converte_para_maisculo (GtkEntry *entry, const gchar *text, gint length, gint *position, gpointer data) { GtkEditable *editable = GTK_EDITABLE(entry); int i, count=0; gchar *result = g_new (gchar, length);

for (i=0; i < length; i++) { if (isspace(text[i])) { result[count++] = text[i]; continue; } if (!isalpha(text[i])) continue; result[count++] = islower(text[i]) ? toupper(text[i]) : text[i]; } if (count > 0) { g_signal_handlers_block_by_func (G_OBJECT (editable), G_CALLBACK (converte_para_maisculo), data); gtk_editable_insert_text (editable, result, count, position); g_signal_handlers_unblock_by_func (G_OBJECT (editable), G_CALLBACK (converte_para_maisculo), data); } g_signal_stop_emission_by_name (G_OBJECT (editable), "insert_text"); g_free (result); } void entry_only_number (GtkEntry *entry, const gchar *text, gint length, gint *position, gpointer data) { GtkEditable *editable = GTK_EDITABLE(entry); int i, count=0; gchar *result = g_new (gchar, length); for (i=0; i < length; i++) { if (isalpha(text[i])) continue; result[count++] = islower(text[i]) ? toupper(text[i]) : text[i]; } if (count > 0) { g_signal_handlers_block_by_func (G_OBJECT (editable), G_CALLBACK (entry_only_number), data); gtk_editable_insert_text (editable, result, count, position); g_signal_handlers_unblock_by_func (G_OBJECT (editable), G_CALLBACK (entry_only_number), data); } g_signal_stop_emission_by_name (G_OBJECT (editable), "insert_text"); g_free (result); }

int main(int argc, char *argv[] ) { gtk_init(&argc, &argv); conexao_mysql = mysql_init(0L); if (conexao_mysql == 0L) {g_print("erro de conexao"); return; } mysql_real_connect(conexao_mysql, "localhost", "usuario", "senha", "agen datel", 0, 0L, 0); if (conexao_mysql == 0L) { g_print("erro ao conectar"); return; } main_window(); gtk_main(); mysql_close(conexao_mysql); } void main_window() { GtkWidget *window1 (GTK_WINDOW_TOPLEVEL), *label1 bel_new("Agenda de Telefone"), *box = gtk_vbox_new(FALSE,5), *hbox = gtk_hbox_new(FALSE, 1), *botao_cadastro = gtk_button_new_with_la bel("Consulta"), *botao_consulta = gtk_button_new_with_la bel("Incluir "), *botao_fim tton_new_with_label(" Sair "), *img = gtk_image_new_from_file("imagem.jpg"); gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER); gtk_widget_set_size_request(GTK_WIDGET(window1), 580,460); gtk_window_set_title(GTK_WINDOW(window1), "Agenda de Telefone"); gtk_container_set_border_width(GTK_CONTAINER(box), 10); gtk_container_add(GTK_CONTAINER(window1), box); gtk_box_pack_start(GTK_BOX(box), img, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(box),label1, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox), botao_cadastro, TRUE, TRUE, 1); gtk_box_pack_start(GTK_BOX(hbox), botao_consulta, TRUE, TRUE, 2); gtk_box_pack_start(GTK_BOX(hbox), botao_fim , TRUE, TRUE, 1); gtk_signal_connect_object(GTK_OBJECT(window1), "delete_event", = gtk_bu = gtk_window_new = gtk_la

G_CALLBA CK(delet_event_main), NULL); gtk_signal_connect_object(GTK_OBJECT(botao_cadastro), "clicked", GTK_SIGN AL_FUNC(cadastro), NULL); gtk_signal_connect_object(GTK_OBJECT(botao_consulta), "clicked", GTK_SIGN AL_FUNC(Incluir), (gpointer) window1); gtk_signal_connect_object(GTK_OBJECT(botao_fim), "clicked", GTK_SIGN AL_FUNC(gtk_main_quit), NULL); gtk_widget_show_all(window1); } static gboolean delet_event_main() { return TRUE; } static void Incluir(GtkWidget *win) { if (!tem_janela) { if (!tem_janela_incluir) { window_incluir_s = create_window_incluir (); else ; } else { gtk_window_present(GTK_WINDOW(window_cadastro)); display_message(window_cadastro, "Opo Invalido, Vo ce est Consultando"); } } void delete_event_incluir(GtkWidget *win) { tem_janela_incluir = FALSE; gtk_widget_destroy(window_incluir_s); } static GtkWidget *create_window_incluir() { GtkWidget *window_incluir = gtk_window_new(GTK_WINDOW_TOPLEVEL), gtk_widget_show_all(window_incluir_s); } gtk_window_present(GTK_WINDOW(window_incluir_s))

*vbox = gtk_vbox_new(FALSE,1), *hbox_nome = gtk_hbox_new(FALSE, 1), *hbox_celular = gtk_hbox_new(FALSE, 1), *hbox_residencia = gtk_hbox_new(FALSE, 1 ), *hbox_botao = gtk_hbox_new(TRUE, 1); GtkWidget ), *label_residencia = gtk_label_new("Resid encial"), *entry_nome = gtk_entry_new_with_max_len gth(30), *entry_celular = gtk_entry_new_with_max_ length(13), *entry_residencia = gtk_entry_new_with_m ax_length(13), *botao_confirma = gtk_button_new_with_la bel("Confirma"), *botao_saida = gtk_button_new_with_label ("Cancelar"); gtk_window_set_title(GTK_WINDOW(window_incluir), "Inclusao"); gtk_widget_set_usize(GTK_WIDGET(window_incluir), 300, 200); gtk_window_set_position(GTK_WINDOW(window_incluir), GTK_WIN_POS_CENTER); gtk_container_set_border_width(GTK_CONTAINER(vbox), 10); gtk_container_add(GTK_CONTAINER(window_incluir), vbox); gtk_box_pack_start(GTK_BOX(vbox), hbox_nome, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_nome), label_nome, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_nome), entry_nome, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox_celular, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_celular),label_celular, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_celular),entry_celular, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox_residencia, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_residencia), label_residencia, TRUE, TRU E, 0); gtk_box_pack_start(GTK_BOX(hbox_residencia), entry_residencia, TRUE, TRU E, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox_botao, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox_botao), botao_confirma, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_botao), botao_saida, TRUE, TRUE,0); *label_nome = gtk_label_new("Nome"), *label_celular = gtk_label_new("Celular"

entrada_celular = entry_celular; entrada_nome = entry_nome; entrada_residencia = entry_residencia; g_signal_connect(G_OBJECT(entry_nome), "insert_text", G_CALLBACK(converte_para_maisculo), NULL); g_signal_connect(G_OBJECT(entry_celular), "insert_text", G_CALLBACK(entry_only_number), NULL); g_signal_connect(G_OBJECT(entry_residencia), "insert_text",

G_CALLBACK(entry_only_number), NULL); gtk_signal_connect(GTK_OBJECT(window_incluir), "delete_event", G_CALLBACK(delete_event_ incluir), NULL) ; gtk_signal_connect(GTK_OBJECT(botao_confirma), "clicked", G_CALLBACK(confirma_incl usao), NULL); gtk_signal_connect(GTK_OBJECT(botao_saida), "clicked", G_CALLBACK(delete_event_ incluir), NULL); tem_janela_incluir = TRUE; return window_incluir; } static void confirma_inclusao() { if(gtk_entry_get_text_length(GTK_ENTRY(entrada_nome)) == 0) { display_message(window_incluir_s, "Nome Obrigatorio"); } else { if (incluir_no_bd()) { tem_janela_incluir = FALSE; gtk_widget_destroy(window_incluir_s); } } } static int incluir_no_bd() { MYSQL *conx; MYSQL_RES *result_set; gchar *erromysql; gchar *query TO lista (nome, celular, telefone) VALUES ('", gtk_entry_get_text(GTK_ENTRY(entrada_nome)), "','", gtk_entry_get_text(GTK_ENTRY(entrada_celular)), "','", gtk_entry_get_text(GTK_ENTRY(entrada_residencia)), "')", 0L); gchar ERE nome = '", *query_busca = g_strconcat("SELECT nome FROM lista WH

= g_strconcat("INSERT IN

gtk_entry_get_text(GTK_ENTRY(entrada_nome)), "'",0L);

if (mysql_query(conexao_mysql, query) != 0) { if(mysql_errno(conexao_mysql) == 1062) { erromysql = g_strconcat(gtk_entry_get_te xt(GTK_ENTRY(entrada_nome)), " ja existe no cadastro",0L); } else erromysql = g_strconcat("", mysql_error(con exao_mysql), 0L); display_message(window_incluir_s, erromysql); return FALSE; } return TRUE; } void selecionado(GtkWidget row, gint column, GdkEventButton *event, gpointer data) { gchar int GtkWidget *text_celular, *text_residencia, *text_nome ; opc = 0; *opcoes = gtk_dialog_new(), *linha1, *linha2, *linha3; *clist, gint

if (!tem_janela_alterar) { gtk_clist_get_text(GTK_CLIST(clist),row ,0, &text_nome); gtk_clist_get_text(GTK_CLIST(clist),row ,1, &text_celula r); gtk_clist_get_text(GTK_CLIST(clist),row ,2, &text_reside ncia); linha1 = gtk_label_new(g_strconcat(text_nome, 0L)); linha2 = gtk_label_new(g_strconcat(text_celular, 0L)); linha3 = gtk_label_new(g_strconcat(text_residencia,0L)); gtk_dialog_add_button(GTK_DIALOG(opcoes), "Alterar", 1); gtk_dialog_add_button(GTK_DIALOG(opcoes), "Excluir", 2); gtk_dialog_add_button(GTK_DIALOG(opcoes), "Cancelar", 3); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(opcoes)->vbox), linha 1); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(opcoes)->vbox), linha 2); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(opcoes)->vbox), linha

3); gtk_window_set_position(GTK_WINDOW(opcoes), GTK_WIN_POS_CENTER); gtk_widget_show_all(opcoes); opc = gtk_dialog_run(GTK_DIALOG(opcoes)); gtk_widget_destroy(opcoes); if (opc == 1) { tem_janela_alterar = TRUE; alterar(clist, row, column); } else if (opc == 2) { excluir(clist, row, column); } } else { display_message(window_alterar, "Opao Invalida, Voce tem alterao Pendente"); gtk_window_present(GTK_WINDOW(window_alterar)); } return; } static void alterar(GtkWidget *clist, gint row, gint column) { GtkWidget DOW_TOPLEVEL), = gtk_vbox_new(FALSE,1), *hbox_nome ox_new(FALSE,1), *hbox_celular ALSE,1), *hbox_residencia ALSE,1), *hbox_button RUE,1), *label_nome bel_new("Nome"), *label_celular "Celular"), *label_residencia "Residencial"), *button_confirma _with_label("Confirma"), *button_cancela = gtk_button_new_with_la bel("Cancela"), *entry_nome try_new_with_max_length(30), *entry_celular with_max_length(13), *entry_residencia = gtk_entry_new_with_m ax_length(13); gchar *text_nome, *text_celular, *text_residencia; = gtk_entry_new_ = gtk_en = gtk_button_new = gtk_label_new( = gtk_label_new( = gtk_la = gtk_hbox_new(T = gtk_hbox_new(F = gtk_hbox_new(F = gtk_hb *window *vbox = gtk_window_new(GTK_WIN

gtk_clist_get_text(GTK_CLIST(clist),row ,0, &text_nome); gtk_clist_get_text(GTK_CLIST(clist),row ,1, &text_celular); gtk_clist_get_text(GTK_CLIST(clist),row ,2, &text_residencia); gtk_entry_set_text(GTK_ENTRY(entry_nome), text_nome); gtk_entry_set_text(GTK_ENTRY(entry_celular), text_celular); gtk_entry_set_text(GTK_ENTRY(entry_residencia), text_residencia); gtk_entry_set_editable(GTK_ENTRY(entry_nome), FALSE); altera_nome = entry_nome; altera_celular = entry_celular; altera_residencia = entry_residencia; gtk_window_set_title(GTK_WINDOW(window), "Alterao"); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); gtk_widget_set_size_request(GTK_WIDGET(window), 300, 200); gtk_container_set_border_width(GTK_CONTAINER(vbox), 10); gtk_container_add(GTK_CONTAINER(window),vbox); gtk_box_pack_start(GTK_BOX(vbox), hbox_nome, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox_celular, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox_residencia, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox_button, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox_nome), label_nome, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_nome), entry_nome, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_celular), label_celular, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_celular), entry_celular, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(hbox_residencia), label_residencia, TRUE, TRU E, 0); gtk_box_pack_start(GTK_BOX(hbox_residencia), entry_residencia, TRUE, TRU E, 0); gtk_box_pack_start(GTK_BOX(hbox_button), button_confirma, FALSE, TRUE, 0 ); gtk_box_pack_start(GTK_BOX(hbox_button), button_cancela, TRUE, TRUE, 0); gtk_widget_show_all(window); window_alterar = window; gtk_signal_connect_object(GTK_OBJECT(window), "delete_event", G_CALLBACK(cance la_alteracao), window); gtk_signal_connect_object(GTK_OBJECT(button_confirma),"clicked", G_CALLBACK(confi rma_alteracao), window); gtk_signal_connect_object(GTK_OBJECT(button_cancela), "clicked", G_CALLBACK(cance la_alteracao), window); g_signal_connect(GTK_OBJECT(entry_celular), "insert_text", G_CALLBACK(entry_only_number), NULL); g_signal_connect(GTK_OBJECT(entry_residencia), "insert_text", G_CALLBACK(entry_only_number), NULL); }

static void confirma_alteracao(GtkWidget *win) { gchar *query = g_strconcat("UPDATE lista SET celular = '", gtk_entry_get _text(GTK_ENTRY(altera_celular)), "', telefone = '", gtk_entry_get_text(GTK_ENTRY(altera_residencia)), "' WHERE nome = '", gtk_entry_get_text(GTK_ENTRY(altera_nome)), "'", 0L);

tem_janela_alterar = FALSE; tem_janela = FALSE; executa_query(query); gtk_widget_destroy(window_alterar); gtk_widget_destroy(window_cadastro);

} static void cancela_alteracao(GtkWidget *win) { gtk_widget_destroy(win); tem_janela_alterar = FALSE; } static void excluir(GtkWidget *clist, gint row, gint column) { GtkWidget *opcoes = gtk_dialog_new_with_buttons ("Excluir ?", GTK_WINDOW(wind ow_cadastro), GTK_DIAL OG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOC K_OK, GTK_RESP ONSE_ACCEPT, GTK_STOC K_CANCEL, GTK_RESP ONSE_REJECT, NULL); GtkWidget *linha1, *linha2, *linha3, *linha4; gchar *text_nome, *text_celular, *text_residencia, *query; int opcao; gtk_clist_get_text(GTK_CLIST(clist),row ,0, &text_nome); gtk_clist_get_text(GTK_CLIST(clist),row ,1, &text_celular); gtk_clist_get_text(GTK_CLIST(clist),row ,2, &text_residencia);

linha1 linha2 linha3 linha4

= = = =

gtk_label_new(g_strconcat(text_nome, 0L)); gtk_label_new(g_strconcat(text_celular, 0L)); gtk_label_new(g_strconcat(text_residencia,0L)); gtk_label_new("Deseja Realmente Excluir ?");

gtk_container_add(GTK_CONTAINER(GTK_DIALOG(opcoes)->vbox), linha 4); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(opcoes)->vbox), linha 1); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(opcoes)->vbox), linha 2); gtk_container_add(GTK_CONTAINER(GTK_DIALOG(opcoes)->vbox), linha 3); gtk_widget_show_all(opcoes); opcao = gtk_dialog_run(GTK_DIALOG(opcoes)); gtk_widget_destroy(opcoes); if(opcao == -3) { query = g_strconcat("DELETE FROM lista WHERE nom e = '", text_nome, "'",0L); executa_query(query); tem_janela = FALSE; gtk_widget_destroy(window_cadastro); } } static void executa_query(gchar *query) { char *erromysql; if (mysql_query(conexao_mysql, query) != 0) { erromysql = g_strconcat("", mysql_error(conexao_mysql), 0L); display_message(window_alterar, erromysql); } return; } static void cadastro() { if (!tem_janela_incluir) { if (!tem_janela) {

window_cadastro = create_window_cadastro(); if (window_cadastro != NULL) { gtk_widget_show_all(window_cadastro); } } else { gtk_window_present(GTK_WINDOW(window_cadastro)); } } else { gtk_window_present(GTK_WINDOW(window_incluir_s)); display_message(window_incluir_s, "Opao Invalida. Voce es t Incluindo"); } return; } static GtkWidget *create_window_cadastro() { gchar GtkWidget *titulo[3] = {"Nome", "Celular", "Residencia"}; *window_cad = gtk_window_new(GTK_WINDOW_TOPLEVEL), *vbox_cad = gtk_vbox_new(FALSE,1), *hbox_cad = gtk_hbox_new(TRUE,0), *scroll_window = gtk_scrolled_window_new *clist_cad = gtk_clist_new_with_titles(3 , titulo); window_cad = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window_cad),"Consulta"); gtk_widget_set_size_request(GTK_WIDGET(window_cad), 400,200); gtk_window_set_position(GTK_WINDOW(window_cad), GTK_WIN_POS_CENTER); gtk_container_add(GTK_CONTAINER(window_cad), vbox_cad); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS) ; gtk_box_pack_start(GTK_BOX(vbox_cad), scroll_window, TRUE, TRUE,0); gtk_clist_set_shadow_type(GTK_CLIST(clist_cad), GTK_SHADOW_OUT); gtk_clist_set_column_width (GTK_CLIST(clist_cad), 0, 150); gtk_clist_set_column_width (GTK_CLIST(clist_cad), 1, 100); gtk_clist_set_column_width (GTK_CLIST(clist_cad), 2, 100); gtk_clist_column_title_passive(GTK_CLIST(clist_cad), 0); gtk_clist_column_title_passive(GTK_CLIST(clist_cad), 1); gtk_clist_column_title_passive(GTK_CLIST(clist_cad), 2); gtk_container_add(GTK_CONTAINER(scroll_window), clist_cad); gtk_box_pack_start(GTK_BOX(vbox_cad), hbox_cad, FALSE, TRUE,0); gtk_signal_connect(GTK_OBJECT(window_cad), "delete_event", G_CALLBACK(de lete_event_cadastro), (gpointer) window_cad) ; gtk_signal_connect(GTK_OBJECT(clist_cad), "select_row", GTK_SIGNAL_FUNC(selecionado), NU

(NULL,NULL),

LL); if (!crialista((gpointer) clist_cad) ) { gtk_widget_destroy(window_cad); return NULL; } else tem_janela = TRUE; return window_cad; } static void delete_event_cadastro(GtkWidget *win) { tem_janela = FALSE; gtk_widget_destroy(win); } static int crialista(gpointer data) { MYSQL_RES *result_set; MYSQL_ROW db_row; MYSQL_FIELD *field; gchar *row[3] = {" ", " ", " ",}; int cols, counter; if(mysql_query(conexao_mysql, "SELECT * FROM lista ORDER BY nome") != 0) { display_message(window_cadastro, "Erro na Query"); return FALSE; } result_set = mysql_store_result(conexao_mysql); cols = mysql_num_fields(result_set); while ((db_row = mysql_fetch_row(result_set)) != NULL) { for(counter = 0; counter < cols; counter++) { row[counter] = db_row[counter]; } gtk_clist_append(GTK_CLIST(data), row); } return TRUE; } static void display_message(GtkWidget *actual_window, gchar *message) {

gint tecla; GtkWidget al_window), *window_message = gtk_message_dialog_new(GTK_WINDOW(actu

GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, message); tecla = gtk_dialog_run(GTK_DIALOG(window_message)); gtk_widget_destroy(window_message); }

Anda mungkin juga menyukai