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

gui.c

Go to the documentation of this file.
00001 
00005 /* $Id: gui_8c-source.html,v 1.4 2004/05/09 09:31:56 darryll Exp $
00006  * GtkSQL -- an interactive graphical query tool for PostgreSQL
00007  * Copyright (C) 1998-2003 Lionel Ulmer, Darryl Luff
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00022  */
00023 
00024 #include "../config.h"
00025 
00026 #include <string.h>
00027 #include "buf.h"
00028 #include "callbacks.h"
00029 #include "dialogs.h"
00030 #include "export.h"
00031 #include "guiapi.h"
00032 #include "luaif.h"
00033 #include "mytoolbar.h"
00034 #include "queries.h"
00035 #include "status.h"
00036 #include "support.h"
00037 #include "tables.h"
00038 #include "workspace.h"
00039 
00040 /* Icons for the toolbar */
00041 #include "../Pics/connect.xpm"
00042 #include "../Pics/disconnect.xpm"
00043 #include "../Pics/send.xpm"
00044 #include "../Pics/new.xpm"
00045 #include "../Pics/save.xpm"
00046 #include "../Pics/open.xpm"
00047 #include "../Pics/delete.xpm"
00048 #include "../Pics/rename.xpm"
00049 #include "../Pics/export.xpm"
00050 #include "../Pics/refresh.xpm"
00051 
00052 #include "../Pics/connect-grey.xpm"
00053 #include "../Pics/disconnect-grey.xpm"
00054 #include "../Pics/send-grey.xpm"
00055 #include "../Pics/new-grey.xpm"
00056 #include "../Pics/save-grey.xpm"
00057 #include "../Pics/open-grey.xpm"
00058 #include "../Pics/delete-grey.xpm"
00059 #include "../Pics/rename-grey.xpm"
00060 #include "../Pics/export-grey.xpm"
00061 #include "../Pics/refresh-grey.xpm"
00062 
00063 
00065 DBConnection *guiConnection = NULL;
00066 
00068 static ABuf *rcname = NULL;
00069 
00070 /* Widgets  */
00072 GtkWidget *tree = NULL;
00073 
00075 typedef struct _activator {
00076     GtkWidget **widget;     
00077     int (*activator) ();    
00078 } Activator;
00079 
00080 /* Buttons  */
00081 GtkWidget *toolbarAddButton;            
00082 GtkWidget *toolbarConnectButton;        
00083 GtkWidget *toolbarCloseButton;          
00084 GtkWidget *toolbarDisconnectButton;     
00085 GtkWidget *toolbarExportButton;         
00086 GtkWidget *toolbarLoadButton;           
00087 GtkWidget *toolbarRefreshButton;        
00088 GtkWidget *toolbarSaveButton;           
00089 GtkWidget *toolbarSaveAsButton;         
00090 GtkWidget *toolbarSendButton;           
00093 ToolBar toolbardef[] = {
00094     /* Connection operations. */
00095     {"Connect", connect_xpm, connect_grey_xpm,
00096      "Connect to a new database", on_databaseConnect_activate, NULL,
00097      &toolbarConnectButton},
00098 
00099     {"Disconnect", disconnect_xpm, disconnect_grey_xpm,
00100      "Disconnect from the current database", on_databaseDisconnect_activate,
00101      NULL,
00102      &toolbarDisconnectButton},
00103 
00104     {"", NULL, NULL, NULL, NULL, NULL},
00105 
00106     /* Query operations. */
00107     {"Send", send_xpm, send_grey_xpm,
00108      "[Alt-S] Send the query to the database", on_querySend_activate, NULL,
00109      &toolbarSendButton},
00110 
00111     {"New", new_xpm, new_grey_xpm,
00112      "[Alt-N] Add a new query", on_queryNew_activate, NULL, &toolbarAddButton},
00113 
00114     {"Load", open_xpm, open_grey_xpm,
00115      "Load a query from disk", on_queryLoad_activate, NULL,
00116      &toolbarLoadButton},
00117 
00118     {"Save", save_xpm, save_grey_xpm,
00119      "Save the current query", on_querySave_activate, NULL,
00120      &toolbarSaveButton},
00121 
00122     {"Save As", rename_xpm, rename_grey_xpm,
00123      "Save query under a new name", on_querySaveAs_activate, NULL,
00124      &toolbarSaveAsButton},
00125 
00126     {"Close", delete_xpm, delete_grey_xpm,
00127      "[Alt-D] Close current query", on_queryClose_activate, NULL,
00128      &toolbarCloseButton},
00129 
00130     {"", NULL, NULL, NULL, NULL, NULL},
00131 
00132     /* Result operations. */
00133     {"Export", export_xpm, export_grey_xpm,
00134      "[Alt-E] Export the current query results", do_export_dialog, NULL,
00135      &toolbarExportButton},
00136 
00137     {"", NULL, NULL, NULL, NULL, NULL},
00138 
00139     /* Table operations. */
00140     {"Refresh", refresh_xpm, refresh_grey_xpm,
00141      "Refresh the table display", on_tableRefresh_activate, NULL,
00142      &toolbarRefreshButton},
00143 
00144     {NULL, NULL, NULL, NULL, NULL, NULL}
00145 };
00146 
00147 static int gui_connection_bad(void);
00148 static int query_exists(void);
00149 static int result_export_ok(void);
00150 
00152 Activator sensWidget[] = {
00153     {&mainmenu_databaseConnect, gui_connection_bad},
00154     {&mainmenu_databaseDisconnect, gui_connection_ok},
00155     {&mainmenu_fileQueryImport, query_exists},
00156     {&mainmenu_fileQueryLoad, gui_connection_ok},
00157     {&mainmenu_fileQuerySave, query_exists},
00158     {&mainmenu_fileQuerySaveAs, query_exists},
00159     {&mainmenu_fileResultExport, result_export_ok},
00160     {&mainmenu_fileScriptLoad, lua_supported},
00161     {&mainmenu_fileScriptSave, gui_connection_ok},
00162     {&mainmenu_queryClear, query_exists},
00163     {&mainmenu_queryClose, query_exists},
00164     {&mainmenu_queryNew, gui_connection_ok},
00165     {&mainmenu_querySend, query_exists},
00166     {&mainmenu_tableRefresh, gui_connection_ok},
00167     {&queryPopupClearItem, query_exists},
00168     {&queryPopupCloseItem, query_exists},
00169     {&queryPopupImportItem, query_exists},
00170     {&queryPopupNewItem, gui_connection_ok},
00171     {&queryPopupSaveItem, query_exists},
00172     {&queryPopupSaveAsItem, query_exists},
00173     {&queryPopupSendItem, query_exists},
00174     {&toolbarAddButton, gui_connection_ok},
00175     {&toolbarConnectButton, gui_connection_bad},
00176     {&toolbarCloseButton, query_exists},
00177     {&toolbarDisconnectButton, gui_connection_ok},
00178     {&toolbarExportButton, result_export_ok},
00179     {&toolbarLoadButton, gui_connection_ok},
00180     {&toolbarRefreshButton, gui_connection_ok},
00181     {&toolbarSaveButton, query_exists},
00182     {&toolbarSaveAsButton, query_exists},
00183     {&toolbarSendButton, query_exists},
00184     {NULL, NULL}
00185 };
00186 
00187 
00189 static GtkWidget *create_toolbar(GtkWidget * mainvbox)
00190 {
00191     GtkWidget *toolbar;
00192     int tbc = GTK_TOOLBAR_BOTH;
00193 
00194     toolbar = create_my_toolbar(mainvbox, toolbardef);
00195     gtk_widget_show(toolbar);
00196 
00197     set_my_toolbar_config(tbc);
00198     update_button_sensitivity();
00199     return toolbar;
00200 }
00201 
00204 void disconnect_callback(void *data)
00205 {
00206     gui_disconnect();
00207     gtk_window_set_title(GTK_WINDOW(mainWindow), "GtkSQL");
00208     update_button_sensitivity();
00209 }
00210 
00215 int disconnect_first(void)
00216 {
00218 #define DISCONNECTFIRST_S   " You must disconnect first \n from database %s."
00219     ABuf *buf;
00220     DBConnection *conn = gui_get_connection();
00221     if (conn == NULL) {
00222         return 1;
00223     }
00224     else {
00225         buf =
00226             buf_new(strlen(DISCONNECTFIRST_S) +
00227                     strlen(conn->DBget_db_name(conn)));
00228         if (!buf)
00229             g_warning
00230                 ("disconnectFirst() could not allocate memory for a buffer\n");
00231         else {
00232             snprintf(buf->b_dat, buf->b_len, DISCONNECTFIRST_S,
00233                      conn->DBget_db_name(conn));
00234             display_error("Disconnect", buf->b_dat);
00235             buf_free(buf);
00236         }
00237     }
00238     return 0;
00239 }
00240 
00243 char *get_rcname(void)
00244 {
00245     rcname = buf_strcat(rcname, getenv("HOME"));
00246     rcname = buf_strcat(rcname, "/.gtksqlrc");
00247     return rcname->b_dat;
00248 }
00249 
00251 void gui_get_workspace()
00252 {
00253     /* Load the workspace from the file. */
00254     ws_load_file(NULL);
00255 }
00256 
00259 static int gui_connection_bad(void)
00260 {
00261     DBConnection *conn = gui_get_connection();
00262 
00263     if (conn == NULL)
00264         return 1;
00265     else
00266         return 0;
00267 }
00268 
00272 GtkWidget *init_top_hpane()
00273 {
00274     GtkWidget *hpane = lookup_widget(mainWindow, "hpane");
00275 
00276     gtk_paned_add1(GTK_PANED(hpane), query_pane_init());
00277     gtk_paned_add2(GTK_PANED(hpane), init_tables_pane());
00278 
00279     gtk_widget_show(hpane);
00280     return hpane;
00281 }
00282 
00286 GtkWidget *init_mainvpane()
00287 {
00288     GtkWidget *mainvpane = lookup_widget(mainWindow, "mainvpane");
00289 
00290     gtk_paned_add1(GTK_PANED(mainvpane), init_top_hpane());
00291     gtk_paned_add2(GTK_PANED(mainvpane), result_pane_init());
00292 
00293     gtk_widget_show(mainvpane);
00294     return mainvpane;
00295 }
00296 
00299 static GtkWidget *init_statusbar(void)
00300 {
00301     GtkWidget *statusbar = status_bar_new();
00302     status_push("Welcome to " APPNAME " version " VERSION);
00303     progress_reset();
00304     gtk_widget_show(statusbar);
00305     return statusbar;
00306 }
00307 
00309 GtkWidget *populate_mainvbox(GtkWidget * mainvbox)
00310 {
00311     GtkWidget *tb;
00312     if (mainvbox == NULL)
00313         return NULL;
00314 
00315     tb = create_toolbar(mainvbox);
00316     gtk_box_pack_start(GTK_BOX(mainvbox), tb, FALSE, FALSE, 0);
00317     gtk_box_reorder_child(GTK_BOX(mainvbox), tb, 1);
00318 
00319     /* Build main hpane including server tree. */
00320     init_mainvpane();
00321 
00322     gtk_box_pack_end(GTK_BOX(mainvbox), init_statusbar(), FALSE, FALSE, 0);
00323     return mainvbox;
00324 }
00325 
00337 void populate_mainWindow()
00338 {
00339     /* Top level layout. Have to split mainvbox creation and population
00340      * because pixmaps can't be created properly until their container is
00341      * added to the window. */
00342     GtkWidget *mainvbox = lookup_widget(mainWindow, "mainvbox");
00343     if (!mainvbox) {
00344         g_warning("mainvbox = NULL\n");
00345     }
00346     populate_mainvbox(mainvbox);
00347 }
00348 
00351 static int query_exists(void)
00352 {
00353     DBConnection *conn = gui_get_connection();
00354     if (conn == NULL)
00355         return 0;
00356 
00357     if (query_count() > 0)
00358         return 1;
00359 
00360     return 0;
00361 }
00362 
00365 static int result_export_ok(void)
00366 {
00367     DBConnection *conn = gui_get_connection();
00368     if (conn == NULL)
00369         return 0;
00370 
00371     if (query_count() > 0)
00372         return !(export_in_progress());
00373 
00374     return 0;
00375 }
00376 
00378 void update_button_sensitivity(void)
00379 {
00380     int i = 0;
00381     while (sensWidget[i].widget != NULL) {
00382         int act = (sensWidget[i].activator) ();
00383         GtkWidget *w = *(sensWidget[i].widget);
00384         if (w != NULL)
00385             gtk_widget_set_sensitive(w, act);
00386         i++;
00387     }
00388 }
00389 
00390 #ifdef USE_EXPERIMENTAL
00391 
00393 GtkWidget *init_tree()
00394 {
00395     GtkTreeItem *item;
00396     GtkWidget *subtree;
00397     GtkWidget *dbitem;
00398     GtkWidget *label;
00399     static gchar *itemtext1[] = { "server1" };
00400     static gchar *itemtext2[] = { "andrew" };
00401     static gchar *dbname1[] = { "logs", "racedb" };
00402 
00403     tree = gtk_ctree_new(1, 0);
00404     gtk_widget_show(GTK_WIDGET(tree));
00405 
00406     subtree = gtk_ctree_new(1, 0);
00407     gtk_widget_show(GTK_WIDGET(subtree));
00408 
00409     item =
00410         (GtkTreeItem *) gtk_ctree_insert_node(GTK_CTREE(tree), NULL, NULL,
00411                                               &itemtext1, 5, NULL, NULL, NULL,
00412                                               NULL, 0, 1);
00413     gtk_widget_show(GTK_WIDGET(item));
00414 
00415     dbitem =
00416         GTK_WIDGET(gtk_ctree_insert_node
00417                    (GTK_CTREE(tree), GTK_CTREE_NODE(item), NULL, &dbname1[0],
00418                     5, NULL, NULL, NULL, NULL, 1, 1));
00419     gtk_widget_show(GTK_WIDGET(dbitem));
00420 
00421     dbitem =
00422         GTK_WIDGET(gtk_ctree_insert_node
00423                    (GTK_CTREE(tree), GTK_CTREE_NODE(item),
00424                     GTK_CTREE_NODE(dbitem), &dbname1[1], 5, NULL, NULL, NULL,
00425                     NULL, 1, 1));
00426     gtk_widget_show(GTK_WIDGET(dbitem));
00427 
00428     item =
00429         GTK_TREE_ITEM(gtk_ctree_insert_node
00430                       (GTK_CTREE(tree), NULL, NULL, &itemtext2, 5, NULL, NULL,
00431                        NULL, NULL, 0, 1));
00432     gtk_widget_show(GTK_WIDGET(item));
00433 
00434     label = gtk_label_new("OK");
00435     gtk_widget_show(GTK_WIDGET(label));
00436 
00437     gtk_container_add(GTK_CONTAINER(item), label);
00438 
00439     return tree;
00440 }
00441 
00442 #endif

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