Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

tables.c

Go to the documentation of this file.
00001 
00005 /* GtkSQL -- an interactive graphical query tool for PostgreSQL
00006  * Copyright (C) 1998-2003 Lionel ULMER, Darryl Luff
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00027 #include <string.h>
00028 #include "callbacks.h"
00029 #include "dialogs.h"
00030 #include "drag.h"
00031 #include "guiapi.h"
00032 #include "menus.h"
00033 #include "queries.h"
00034 #include "tables.h"
00035 
00037 GtkWidget *tableNotebook;
00038 
00040 GtkWidget *tablePopupDeleteQueryItem;   
00041 GtkWidget *tablePopupInsertQueryItem;   
00042 GtkWidget *tablePopupRefreshItem;       
00043 GtkWidget *tablePopupSelectQueryItem;   
00044 GtkWidget *tablePopupUpdateQueryItem;   
00047 SubMenu tablePopupTemplate[] = {
00048     {"<- SELECT Query", on_select_query_activate, NULL, &tablePopupSelectQueryItem, NORM_BUTTON},
00049     {"<- INSERT Query", on_insert_query_activate, NULL, &tablePopupInsertQueryItem, NORM_BUTTON},
00050     {"<- UPDATE Query", on_update_query_activate, NULL, &tablePopupUpdateQueryItem, NORM_BUTTON},
00051     {"<- DELETE Query", on_delete_query_activate, NULL, &tablePopupDeleteQueryItem, NORM_BUTTON},
00052     {"", NULL, NULL, NULL, NORM_BUTTON},
00053     {"Refresh", on_tableRefresh_activate, NULL, &tablePopupRefreshItem, NORM_BUTTON},
00054     {NULL, NULL, NULL, NULL, NORM_BUTTON}
00055 };
00056 
00058 static int tablesInList = 0;
00059 
00061 static GtkWidget *container;
00062 
00064 static GtkWidget *tablePopup = NULL;
00065 
00066 GtkWidget *tables_create_popup(void);
00067 static GtkWidget *create_table_info(DBTableDef * tbd);
00068 gboolean on_info_clist_button_press_event(GtkWidget * widget,
00069                                           GdkEventButton * event,
00070                                           gpointer user_data);
00071 
00076 int display_table_named(char *tablename)
00077 {
00078     int i;
00079 
00080     for (i = 0; i < tablesInList; i++) {
00081         char *name;
00082         GtkWidget *label =
00083             gtk_notebook_get_tab_label(GTK_NOTEBOOK(tableNotebook),
00084                                        gtk_notebook_get_nth_page(GTK_NOTEBOOK
00085                                                                  (tableNotebook),
00086                                                                  i));
00087         gtk_label_get(GTK_LABEL(label), &name);
00088 
00089         if (!strcmp(name, tablename)) {
00090             gtk_notebook_set_page(GTK_NOTEBOOK(tableNotebook), i);
00091             return 1;
00092         }
00093     }
00094     return 0;
00095 }
00096 
00099 GtkCList *get_current_table(void)
00100 {
00101     /* GtkWidget *widget; */
00102 
00103     int cur_page = gtk_notebook_current_page(GTK_NOTEBOOK(tableNotebook));
00104     return GTK_CLIST(get_table_no(cur_page));
00105 }
00106 
00108 char *get_current_table_name(void)
00109 {
00110     GtkWidget *page;
00111     GtkWidget *label;
00112     char *name;
00113     int curpage = gtk_notebook_current_page(GTK_NOTEBOOK(tableNotebook));
00114     page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(tableNotebook), curpage);
00115     if (!page)
00116         return NULL;
00117 
00118     label = gtk_notebook_get_tab_label(GTK_NOTEBOOK(tableNotebook), page);
00119     if (!label)
00120         return NULL;
00121 
00122     gtk_label_get(GTK_LABEL(label), &name);
00123     return name;
00124 }
00125 
00128 char *get_selected_table_fields(void)
00129 {
00130     GtkCList *clist;
00131     GList *list;
00132     int row = 0;
00133     static char fields[2048] = "";
00134     char *field;
00135     int focus = -1;
00136 
00137     clist = (GtkCList *) get_current_table();
00138     if (!clist)
00139         return NULL;
00140 
00141     focus = clist->focus_row;
00142     gtk_clist_get_text(clist, focus, 0, &field);
00143     strcpy(fields, field);
00144 
00145     list = clist->selection;
00146     if (!list)
00147         strcpy(fields, "*");
00148     else {
00149         while (list) {
00150             row = (int) list->data;
00151             if (row != focus) {
00152                 gtk_clist_get_text(clist, row, 0, &field);
00153                 strcat(fields, ", ");
00154                 strcat(fields, field);
00155             }
00156             list = list->next;
00157         }
00158     }
00159     return fields;
00160 }
00161 
00165 GtkCList *get_table_no(int n)
00166 {
00167     GtkWidget *widget;
00168     GList *list;                /* child list */
00169 
00170     if (!(widget = gtk_notebook_get_nth_page(GTK_NOTEBOOK(tableNotebook), n)))
00171         return NULL;
00172 
00173     /* widget should be the scrolled window */
00174     if (!(list = gtk_container_children(GTK_CONTAINER(widget))))
00175         return NULL;
00176 
00177     if (!(widget = list[0].data))
00178         return NULL;
00179 
00180     /* widget is now the viewport */
00181     if (!(list = gtk_container_children(GTK_CONTAINER(widget))))
00182         return NULL;
00183     else
00184         widget = list[0].data;
00185 
00186     /* widget SHOULD now be the CList */
00187     return GTK_CLIST(widget);
00188 }
00189 
00192 GtkWidget *init_tables_pane(void)
00193 {
00194     container = gtk_vbox_new(FALSE, 0);
00195 
00196     tableNotebook = gtk_notebook_new();
00197     gtk_widget_set_name(GTK_WIDGET(tableNotebook), "tables");
00198     gtk_notebook_set_tab_pos(GTK_NOTEBOOK(tableNotebook), GTK_POS_TOP);
00199     gtk_notebook_set_scrollable(GTK_NOTEBOOK(tableNotebook), TRUE);
00200     gtk_notebook_popup_enable(GTK_NOTEBOOK(tableNotebook));
00201     gtk_box_pack_start(GTK_BOX(container), tableNotebook, TRUE, TRUE, 0);
00202 
00203     gtk_widget_show(container);
00204     gtk_widget_show(tableNotebook);
00205     return container;
00206 }
00207 
00210 void tables_connection_closed(void)
00211 {
00212     int i;
00213     DBConnection *conn = gui_get_connection();
00214     if (conn != NULL)
00215         conn->DBfree_table_def(conn);
00216 
00217     for (i = 0; i < tablesInList; i++) {
00218         gtk_notebook_remove_page(GTK_NOTEBOOK(tableNotebook), 0);
00219     }
00220 }
00221 
00224 void tables_new_connection(void)
00225 {
00226     DBConnection *conn = gui_get_connection();
00227     if (conn == NULL)
00228         return;
00229 
00230     if (conn->DBget_table_def(conn) < 0) {
00231         GtkWidget *dialog = create_standard_dialog("Error retrieving table list", "Error retrieving table list", 1, NULL, "Ok", NULL, NULL, NULL, NULL);
00232         gtk_widget_show(dialog);
00233     }
00234     else {
00235         int i;
00236         tablesInList = conn->DBget_table_num(conn);
00237 
00238         for (i = 0; i < tablesInList; i++) {
00239             DBTableDef *tbd = conn->DBget_table(conn, i);
00240             GtkWidget *label;
00241 
00242             label = gtk_label_new(tbd->name);
00243             gtk_widget_show(label);
00244             gtk_notebook_append_page(GTK_NOTEBOOK(tableNotebook),
00245                                      create_table_info(tbd), label);
00246         }
00247     }
00248 }
00249 
00253 GtkWidget *tables_create_popup(void)
00254 {
00255     return create_menu(tablePopupTemplate);
00256 }
00257 
00261 static GtkWidget *create_table_info(DBTableDef * tbd)
00262 {
00263     GtkWidget *info_clist;
00264     GtkWidget *info_scroll;
00265     static GtkTargetEntry target = { "text/plain", 0, 0 };
00266     int i;
00267     char *col_titles[] = {
00268         "Field", "Type", "Length"
00269     };
00270 
00271     /* CList initialisation */
00272     info_scroll = gtk_scrolled_window_new(NULL, NULL);
00273     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(info_scroll),
00274                                    GTK_POLICY_AUTOMATIC,
00275                                    GTK_POLICY_AUTOMATIC);
00276 
00277     info_clist = gtk_clist_new_with_titles(3, col_titles);
00278     gtk_clist_set_selection_mode((GtkCList *) info_clist,
00279                                  GTK_SELECTION_MULTIPLE);
00280     gtk_signal_connect(GTK_OBJECT(info_clist), "drag_data_get",
00281                        GTK_SIGNAL_FUNC(send_callback), NULL);
00282     gtk_signal_connect(GTK_OBJECT(info_clist), "button_press_event",
00283                        GTK_SIGNAL_FUNC(on_info_clist_button_press_event),
00284                        NULL);
00285     gtk_drag_source_set(info_clist, GDK_BUTTON1_MASK, &target, 1,
00286                         GDK_ACTION_COPY);
00287     gtk_widget_show(info_clist);
00288     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(info_scroll),
00289                                           info_clist);
00290 
00291     gtk_clist_set_column_width(GTK_CLIST(info_clist), 0, 130);
00292     gtk_clist_set_column_width(GTK_CLIST(info_clist), 1, 130);
00293 
00294     for (i = 0; i < tbd->field_num; i++) {
00295         char *line[3];
00296 
00297         line[0] = tbd->field_def[i].name;
00298         line[1] = tbd->field_def[i].type;
00299         line[2] = tbd->field_def[i].length;
00300 
00301         gtk_clist_append(GTK_CLIST(info_clist), line);
00302     }
00303 
00304     gtk_widget_show(info_scroll);
00305     return info_scroll;
00306 }
00307 
00311 void on_delete_query_activate(GtkMenuItem * menuitem, gpointer user_data)
00312 {
00313     char buff[2048] = "";
00314     GtkWidget *query = NULL;
00315     char *table = get_current_table_name();
00316 
00317     if (!table) {
00318         g_error("Could not get current table name\n");
00319         return;
00320     }
00321 
00322     snprintf(buff, sizeof(buff), "delete from %s\nwhere ...", table);
00323 
00324     query = query_get_current();
00325     if (query) {
00326         gtk_editable_delete_text(GTK_EDITABLE(query), 0, -1);
00327         gtk_text_insert(GTK_TEXT(query), NULL, NULL, NULL, buff, -1);
00328     }
00329 }
00330 
00334 gboolean on_info_clist_button_press_event(GtkWidget * widget,
00335                                           GdkEventButton * event,
00336                                           gpointer user_data)
00337 {
00338     if (event->button == 3) {
00339         if (tablePopup == NULL) {
00340             tablePopup = tables_create_popup();
00341             if (tablePopup == NULL) {
00342                 printf("Couldnt create menu\n");
00343                 return FALSE;
00344             }
00345         }
00346         gtk_menu_popup(GTK_MENU(tablePopup), NULL, NULL, NULL, NULL,
00347                        event->button, event->time);
00348         return TRUE;
00349     }
00350     return FALSE;
00351 }
00352 
00356 void on_insert_query_activate(GtkMenuItem * menuitem, gpointer user_data)
00357 {
00358     char buff[2048] = "";
00359     GtkWidget *query = NULL;
00360     char *table = get_current_table_name();
00361     char *fields = get_selected_table_fields();
00362 
00363     if (!fields) {
00364         g_error("Invalid fields\n");
00365         return;
00366     }
00367 
00368     if (!table) {
00369         g_error("Invalid tablename\n");
00370         return;
00371     }
00372 
00373     if (strcmp(fields, "*") == 0)
00374         snprintf(buff, sizeof(buff), "insert into %s\nvalues(...)\n", table);
00375     else
00376         snprintf(buff, sizeof(buff), "insert into %s (%s)\nvalues(...)\n",
00377                  table, fields);
00378 
00379     query = query_get_current();
00380     if (query) {
00381         gtk_editable_delete_text(GTK_EDITABLE(query), 0, -1);
00382         gtk_text_insert(GTK_TEXT(query), NULL, NULL, NULL, buff, -1);
00383     }
00384 }
00385 
00389 void on_select_query_activate(GtkMenuItem * menuitem, gpointer user_data)
00390 {
00391     char buff[2048] = "";
00392     GtkWidget *query = NULL;
00393     char *table = get_current_table_name();
00394     char *fields = get_selected_table_fields();
00395 
00396     if (!fields) {
00397         g_error("Invalid fields\n");
00398         return;
00399     }
00400 
00401     if (!table) {
00402         g_error("Invalid tablename\n");
00403         return;
00404     }
00405 
00406     snprintf(buff, sizeof(buff), "select %s\nfrom %s\n", fields, table);
00407 
00408     query = query_get_current();
00409     if (query) {
00410         gtk_editable_delete_text(GTK_EDITABLE(query), 0, -1);
00411         gtk_text_insert(GTK_TEXT(query), NULL, NULL, NULL, buff, -1);
00412     }
00413 }
00414 
00419 void on_update_query_activate(GtkMenuItem * menuitem, gpointer user_data)
00420 {
00421     char buff[2048] = "";
00422     GtkWidget *query = NULL;
00423     char *table = get_current_table_name();
00424 
00425     if (!table) {
00426         g_error("Invalid tablename\n");
00427         return;
00428     }
00429 
00430     snprintf(buff, sizeof(buff), "update %s\nset ...\nwhere ...\n", table);
00431 
00432     query = query_get_current();
00433     if (query) {
00434         gtk_editable_delete_text(GTK_EDITABLE(query), 0, -1);
00435         gtk_text_insert(GTK_TEXT(query), NULL, NULL, NULL, buff, -1);
00436     }
00437 }

Generated on Sun May 9 19:19:17 2004 for GtkSQL by doxygen1.2.18