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

queries.c

Go to the documentation of this file.
00001 
00005 /*
00006  * GtkSQL -- an interactive graphical query tool for PostgreSQL and MySQL.
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 tdo_he 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 <gdk/gdkkeysyms.h>
00025 #include <string.h>
00026 
00027 #include "buf.h"
00028 #include "callbacks.h"
00029 #include "drag.h"
00030 #include "export.h"
00031 #include "guiapi.h"
00032 #include "menus.h"
00033 #include "queries.h"
00034 #include "query_import_dlg.h"
00035 #include "query_save_dlg.h"
00036 #include "status.h"
00037 #include "tables.h"
00038 #include "utils.h"
00039 
00040 /* Keys for widget data.    */
00041 #define POSITION_KEY "Position"
00042 
00043 #define STATUS_SIZE 64
00044 
00045 #define IS_SEPARATOR(c) ((c == ' ')  || (c == '.')  || (c == '\r') || \
00046              (c == '\n') || (c == '\t') || (c == ',')  || \
00047              (c == '(')  || (c == '('))
00048 
00049 /* Popup menu   */
00050 static GtkWidget *queryPopup = NULL;    
00051 static GtkWidget *resultPopup = NULL;   
00053 /* Variables    */
00054 static Keyword *lastCompletion = NULL;
00055 static int lastIndex = 0;
00057 int lastQuery = 0;
00058 static char *lastStart = NULL;
00059 int nbQueries = 0;          
00060 int queryStatus = 0;
00061 guint queryNotebookPageChangeHandler = 0;
00062 
00063 /* Query pane popup */
00064 GtkWidget *queryPopupClearItem;     
00065 GtkWidget *queryPopupCloseItem;     
00066 GtkWidget *queryPopupImportItem;    
00067 GtkWidget *queryPopupNewItem;       
00068 GtkWidget *queryPopupSaveItem;      
00069 GtkWidget *queryPopupSaveAsItem;    
00070 GtkWidget *queryPopupSendItem;      
00073 SubMenu queryPopupTemplate[] = {
00074     {"Send", on_querySend_activate, NULL, &queryPopupSendItem, NORM_BUTTON},
00075     {"New", on_queryNew_activate, NULL, &queryPopupNewItem, NORM_BUTTON},
00076     {"Clear", on_queryClear_activate, NULL, &queryPopupClearItem, NORM_BUTTON},
00077     {"Import ...", on_queryImport_activate, NULL, &queryPopupImportItem, NORM_BUTTON},
00078     {"Save", on_querySave_activate, NULL, &queryPopupSaveItem, NORM_BUTTON},
00079     {"Save As ...", on_querySaveAs_activate, NULL, &queryPopupSaveAsItem, NORM_BUTTON},
00080     {"Close", on_queryClose_activate, NULL, &queryPopupCloseItem, NORM_BUTTON},
00081     {NULL, NULL, NULL, NULL, NORM_BUTTON}
00082 };
00083 
00084 /* Event response routines  */
00085 static gint changed_event(GtkWidget * widget, gpointer data);
00086 static gint key_press_event(GtkWidget * widget, GdkEventKey * event,
00087                             gpointer data);
00088 
00089 /* Dialog callbacks */
00090 gboolean on_query_button_press_event(GtkWidget * widget,
00091                                      GdkEventButton * event,
00092                                      gpointer user_data);
00093 
00094 gboolean on_result_button_press_event(GtkWidget * widget,
00095                                       GdkEventButton * event,
00096                                       gpointer user_data);
00097 
00099 GtkWidget *resultPopupExportItem = NULL;
00100 SubMenu resultPopupMenu[] = {
00101     {"Export...", do_export_dialog, NULL, &resultPopupExportItem, NORM_BUTTON},
00102     {NULL, NULL, NULL, NULL, NORM_BUTTON}
00103 };
00104 
00109 void query_set_name(int position, char *name) {
00110     if ((name != NULL) && (strlen(name) != 0)) {
00111         GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(queryNotebook), position);
00112         gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(queryNotebook), page, name);
00113         gtk_notebook_set_menu_label_text(GTK_NOTEBOOK(queryNotebook), page, name);
00114     }
00115 }
00116 
00121 static gint changed_event(GtkWidget * widget, gpointer data)
00122 {
00123     GtkWidget *text = GTK_WIDGET(data);
00124     gint index;
00125     DBConnection *conn = gui_get_connection();
00126     QueryData *qd;
00127 
00128     index = gtk_text_get_point(GTK_TEXT(text));
00129 
00130     /* This is in case of a cursor movement without any changes */
00131     if (index != lastIndex + 1) {
00132         lastCompletion = NULL;
00133         g_free(lastStart);
00134         lastStart = NULL;
00135     }
00136 
00137     /* Mark the query as changed.   */
00138     qd = query_get_querydata(text);
00139     if (qd) {
00140         querydata_set_changed(qd, 1);
00141         update_button_sensitivity();
00142     }
00143 
00144     switch (GTK_TEXT_INDEX(GTK_TEXT(text), index - 1)) {
00145     case '\t':{                    /* Tabulation */
00146             int lstart_length, linserted_length, word_start;
00147             Keyword *cur;
00148 
00149             gtk_text_freeze(GTK_TEXT(text));
00150 
00151             /* First, we test if it the first time we press on TAB */
00152             if (lastCompletion != NULL) {
00153                 /* Not the first time -> we use the same "start letters" as
00154                  * before */
00155                 cur = lastCompletion->next;
00156                 linserted_length = strlen(lastCompletion->key);
00157             }
00158             else {
00159                 /* We first search for the characters to complete */
00160                 word_start = index - 2;
00161                 while (word_start >= 0) {
00162                     char c = GTK_TEXT_INDEX(GTK_TEXT(text), word_start);
00163                     if (IS_SEPARATOR(c))
00164                         break;
00165                     word_start--;
00166                 }
00167                 word_start++;
00168                 /* Now, we get the characters and compare them to the
00169                  * keywords */
00170                 lastStart =
00171                     gtk_editable_get_chars(GTK_EDITABLE(text), word_start,
00172                                            index - 1);
00173                 linserted_length = -1;
00174 
00175                 /* We search the whole list of keywords */
00176                 cur = conn->keylist;
00177             }
00178             /* We search for a keyword starting with lastStart */
00179             if ((lastStart != NULL)
00180                 && ((lstart_length = strlen(lastStart)) > 0)) {
00181                 if (linserted_length < 0)
00182                     linserted_length = lstart_length;
00183 
00184                 lastCompletion = NULL;
00185                 while (cur != NULL) {
00186                     if (!strncasecmp(lastStart, cur->key, lstart_length)) {
00187                         lastCompletion = cur;
00188                         break;
00189                     }
00190 
00191                     cur = cur->next;
00192                 }
00193             }
00194 
00195             /* Now we change the text */
00196             if (linserted_length < 0)
00197                 linserted_length = 0;
00198 
00199             gtk_text_set_point(GTK_TEXT(text), index - 1 - linserted_length);
00200             if (lastCompletion != NULL) {
00201                 /* We insert the new keyword */
00202                 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, cur->key,
00203                                 -1);
00204                 gtk_text_set_point(GTK_TEXT(text), index + strlen(cur->key));
00205                 lastIndex = index - 1 - linserted_length + strlen(cur->key);
00206             }
00207             else {
00208                 if (lastStart != NULL) {
00209                     /* Or the original letters */
00210                     gdk_beep();
00211                     gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
00212                                     lastStart, -1);
00213                     gtk_text_set_point(GTK_TEXT(text),
00214                                        index + strlen(lastStart));
00215                     lastIndex =
00216                         index - 1 - linserted_length + strlen(lastStart);
00217                 }
00218                 else {
00219                     lastIndex = index - 1 - linserted_length;
00220                 }
00221             }
00222             gtk_text_backward_delete(GTK_TEXT(text), linserted_length + 1);
00223 
00224             gtk_text_thaw(GTK_TEXT(text));
00225             break;
00226         }
00227 
00228     default:
00229         lastCompletion = NULL;
00230         g_free(lastStart);
00231         lastStart = NULL;
00232         lastIndex = index;
00233         break;
00234     }
00235 
00236     /* Now, syntax coloring */
00237     return (FALSE);
00238 }
00239 
00241 void query_clear(void)
00242 {
00243     GtkWidget *query = query_get_current();
00244     gtk_editable_delete_text(GTK_EDITABLE(query), 0, -1);
00245 }
00246 
00249 GtkWidget *result_popup_create(void)
00250 {
00251     return create_menu(resultPopupMenu);
00252 }
00253 
00257 int query_delete_at_position(int position)
00258 {
00259     GtkWidget *query;
00260     void *result;
00261     gchar *status;
00262     DBConnection *conn = gui_get_connection();
00263     /* QueryData *qd;   */
00264     GtkWidget *scroller;
00265 
00266     scroller = gtk_notebook_get_nth_page(GTK_NOTEBOOK(queryNotebook), position);
00267     query = query_get_no(position);
00268 
00269     /* Does it need to be saved?    */
00270     /* auto-save disabled.
00271     qd = query_get_querydata(query);
00272     if (qd != NULL) {
00273         if (qd->changed)
00274             query_save(query);  Autosave disabled.
00275     }
00276     */
00277 
00278     /* Free the data items. */
00279     result = (void *)gtk_object_get_data(GTK_OBJECT(query), RESULT_KEY);
00280     status = (gchar *)gtk_object_get_data(GTK_OBJECT(query), STATUS_KEY);
00281 
00282     if (status != NULL)
00283         g_free(status);
00284 
00285     if (result != NULL)
00286         conn->DBclear_result(conn, result);
00287 
00288     if (position > 0)
00289         gtk_widget_grab_focus(gtk_notebook_get_nth_page
00290                               (GTK_NOTEBOOK(queryNotebook), position - 1));
00291     else if (nbQueries > 1)
00292         gtk_widget_grab_focus(gtk_notebook_get_nth_page
00293                               (GTK_NOTEBOOK(queryNotebook), position + 1));
00294 
00295     gtk_notebook_remove_page(GTK_NOTEBOOK(queryNotebook), position);
00296     gtk_notebook_remove_page(GTK_NOTEBOOK(resultNotebook), position);
00297     nbQueries--;
00298 
00299     return nbQueries;
00300 }
00301 
00312 void results_display(void *result, int why, int position)
00313 {
00314     DBConnection *conn = gui_get_connection();
00315     GtkWidget *result_widget = NULL;
00316 
00317     if (why == QUERY_FAKE) {           /* Add a dummy message */
00318         result_widget = gtk_label_new("No query.");
00319         gtk_widget_show(result_widget);
00320         if (position >= 0) {
00321             gtk_notebook_remove_page(GTK_NOTEBOOK(resultNotebook), position);
00322             gtk_notebook_insert_page(GTK_NOTEBOOK(resultNotebook), result_widget, NULL, position);
00323         }
00324         else {
00325             gtk_notebook_append_page(GTK_NOTEBOOK(resultNotebook), result_widget, NULL);
00326             position = notebook_page_count(GTK_NOTEBOOK(resultNotebook))-1;
00327         }
00328         gtk_notebook_set_page(GTK_NOTEBOOK(resultNotebook), position);      
00329         gtk_widget_show(result_widget);
00330     }
00331     else if (why == QUERY_SWITCH) {     /* Just switch to the right page.   */
00332         gtk_notebook_set_page(GTK_NOTEBOOK(resultNotebook), position);
00333     }
00334     else {                              /* Both ERROR NEW need some extra processing.   */
00335         if (why == QUERY_ERROR) {
00336             if (result == NULL)
00337                 result_widget = gtk_label_new("Error in query.");
00338             else
00339                 result_widget = gtk_label_new((char *) result);
00340         }
00341         else if (why == QUERY_NEW) {
00342             GtkWidget *result_clist = NULL;
00343             int nb_tuples, nb_cols, i, j;
00344             int maxwidth;
00345             int stat;
00346             char **col_titles;
00347             GdkFont *font;
00348 
00349             status_push("Displaying query results...");
00350             stat = conn->DBget_query_status(conn, result);
00351             if (stat == DB_QUERY_RECORDS_OK) {
00352                 nb_tuples = conn->DBget_record_number(conn, result);
00353                 nb_cols = conn->DBget_field_number(conn, result);
00354 
00355                 /* Init the CList */
00356                 col_titles = (char **) g_malloc(nb_cols * sizeof(char *));
00357                 for (i = 0; i < nb_cols; i++)
00358                     col_titles[i] = conn->DBget_field_name(conn, result, i);
00359 
00360                 result_widget = gtk_scrolled_window_new(NULL, NULL);
00361                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
00362                                                (result_widget),
00363                                                GTK_POLICY_AUTOMATIC,
00364                                                GTK_POLICY_AUTOMATIC);
00365 
00366                 result_clist = gtk_clist_new_with_titles(nb_cols, col_titles);
00367                 gtk_signal_connect(GTK_OBJECT(result_clist),
00368                                    "button_press_event",
00369                                    GTK_SIGNAL_FUNC
00370                                    (on_result_button_press_event),
00371                                    result_clist);
00372                 gtk_container_add(GTK_CONTAINER(result_widget), result_clist);
00373 
00374                 gtk_object_set_data(GTK_OBJECT(result_widget), RESULT_KEY, result_clist);
00375 
00376                 font = gtk_widget_get_style(result_clist)->font;
00377                 maxwidth = MAXCOLWID * gdk_char_width(font, 'X');
00378                 for (i = 0; i < nb_cols; i++)
00379                     gtk_clist_set_column_width(GTK_CLIST(result_clist), i,
00380                                                gdk_string_width(font,
00381                                                                 col_titles
00382                                                                 [i]));
00383 
00384                 /* Fill the CList */
00385                 gtk_clist_freeze(GTK_CLIST(result_clist));
00386                 for (i = 0; i < nb_tuples; i++) {
00387                     for (j = 0; j < nb_cols; j++) {
00388                         int width;
00389 
00390                         col_titles[j] =
00391                             conn->DBget_field_value(conn, result, i, j);
00392 
00393                         /* take care of null fields */
00394                         if (col_titles[j] == NULL)
00395                             col_titles[j] = "";
00396 
00397                         width = gdk_string_width(font, col_titles[j]);
00398 
00399                         if (width > GTK_CLIST(result_clist)->column[j].width) {
00400                             if (width > maxwidth)
00401                                 gtk_clist_set_column_width(GTK_CLIST
00402                                                            (result_clist), j,
00403                                                            maxwidth);
00404                             else
00405                                 gtk_clist_set_column_width(GTK_CLIST
00406                                                            (result_clist), j,
00407                                                            width);
00408                         }
00409                     }
00410                     gtk_clist_append(GTK_CLIST(result_clist), col_titles);
00411                 }
00412                 gtk_clist_thaw(GTK_CLIST(result_clist));
00413                 g_free(col_titles);
00414                 gtk_widget_show(result_clist);
00415             }
00416             else if (conn->DBget_query_status(conn, result) ==
00417                      DB_QUERY_COMMAND_OK)
00418                 result_widget = gtk_label_new("Command ok.");
00419             else if (conn->DBget_query_status(conn, result) == DB_QUERY_EMPTY)
00420                 result_widget = gtk_label_new("Empty query.");
00421             else
00422                 result_widget = gtk_label_new("Error in query.");
00423 
00424             status_pop();
00425         }
00426         gtk_widget_show(result_widget);
00427         gtk_notebook_remove_page(GTK_NOTEBOOK(resultNotebook), position);
00428         gtk_notebook_insert_page(GTK_NOTEBOOK(resultNotebook), result_widget,
00429                                  NULL, position);
00430         gtk_notebook_set_page(GTK_NOTEBOOK(resultNotebook), position);
00431     }
00432 }
00433 
00436 void *query_get_current_result(void)
00437 {
00438     return query_get_result(query_get_current());
00439 }
00440 
00444 void do_export_dialog(GtkWidget * widget, gpointer data)
00445 {
00446     result_export();
00447     update_button_sensitivity();
00448 }
00449 
00454 static gint key_press_event(GtkWidget * widget, GdkEventKey * event,
00455                             gpointer data)
00456 {
00457     GtkWidget *text = GTK_WIDGET(data);
00458 
00459     if (event->state == GDK_MOD1_MASK) {
00460         /* We do not transmit to the Text Widget any of the Alt event */
00461         gtk_signal_emit_stop_by_name(GTK_OBJECT(data), "key_press_event");
00462 
00463         /* Alt - t = Display table */
00464         if (event->keyval == 't') {
00465             gint index, start, end, length;
00466 
00467             index = gtk_text_get_point(GTK_TEXT(text));
00468             length = gtk_text_get_length(GTK_TEXT(text));
00469             start = index;
00470             end = index;
00471             while ((start >= 0) &&
00472                    (!IS_SEPARATOR(GTK_TEXT_INDEX(GTK_TEXT(text), start))))
00473                 start--;
00474             start++;
00475             while ((end < length) &&
00476                    (!IS_SEPARATOR(GTK_TEXT_INDEX(GTK_TEXT(text), end))))
00477                 end++;
00478 
00479             display_table_named(gtk_editable_get_chars(GTK_EDITABLE(text),
00480                                                        start, end));
00481 
00482             return (TRUE);
00483         }
00484         /* Alt - n = New query */
00485         if (event->keyval == 'n') {
00486             query_add();
00487             return (TRUE);
00488         }
00489         /* Alt - d = Delete query */
00490         if (event->keyval == 'd') {
00491             if (nbQueries > 1) {
00492                 query_close();
00493             }
00494             return (TRUE);
00495         }
00496         /* Alt - s = Send Query */
00497         if (event->keyval == 's') {
00498             gui_send_query();
00499             return (TRUE);
00500         }
00501         /* Alt - e = Export Query */
00502         if (event->keyval == 'e') {
00503             result_export();
00504             return (TRUE);
00505         }
00506 
00507         /* Alt arrows = switch between queries */
00508         if (event->keyval == GDK_Left) {
00509             int curpage;
00510 
00511             gtk_notebook_prev_page(GTK_NOTEBOOK(queryNotebook));
00512 
00513             curpage = gtk_notebook_current_page(GTK_NOTEBOOK(queryNotebook));
00514             gtk_widget_grab_focus(gtk_notebook_get_nth_page
00515                                   (GTK_NOTEBOOK(queryNotebook), curpage));
00516 
00517             return (TRUE);
00518         }
00519         if (event->keyval == GDK_Right) {
00520             int curpage;
00521 
00522             gtk_notebook_next_page(GTK_NOTEBOOK(queryNotebook));
00523 
00524             curpage = gtk_notebook_current_page(GTK_NOTEBOOK(queryNotebook));
00525             gtk_widget_grab_focus(gtk_notebook_get_nth_page
00526                                   (GTK_NOTEBOOK(queryNotebook), curpage));
00527 
00528             return (TRUE);
00529         }
00530     }
00531     return (FALSE);
00532 }
00533 
00539 void notebook_selection_callback(GtkNotebook * notebook, GtkNotebookPage * page, gint page_num, gpointer data)
00540 {
00541     GList *children;
00542     GtkWidget *query;
00543     char *status;
00544 
00545     GtkWidget *scroller = page->child;
00546     if (scroller == NULL)
00547         return;
00548 
00549     children = gtk_container_children(GTK_CONTAINER(scroller));
00550     if (children == NULL)
00551         return;
00552 
00553     query = GTK_WIDGET(children->data);
00554     if (!query)
00555         return;
00556 
00557     status = (char *) gtk_object_get_data(GTK_OBJECT(query), STATUS_KEY);
00558 
00559     results_display(NULL, QUERY_SWITCH, page_num);
00560 
00561     update_button_sensitivity();
00562     if (status != NULL) {
00563         if (queryStatus == 1)
00564             status_pop();
00565         status_push(status);
00566         queryStatus = 1;
00567     }
00568     else
00569         queryStatus = 0;
00570 }
00571 
00574 static GtkWidget *query_popup_create(void)
00575 {
00576     return create_menu(queryPopupTemplate);
00577 }
00578 
00584 gboolean on_query_button_press_event(GtkWidget * widget,
00585                                      GdkEventButton * event,
00586                                      gpointer user_data)
00587 {
00588     if (event->button == 3) {
00589         if (queryPopup == NULL) {
00590             queryPopup = query_popup_create();
00591             if (queryPopup == NULL) {
00592                 g_error("Couldnt create menu\n");
00593                 return FALSE;
00594             }
00595         }
00596         gtk_menu_popup(GTK_MENU(queryPopup), NULL, NULL, NULL, NULL,
00597                        event->button, event->time);
00598         return TRUE;
00599     }
00600     return FALSE;
00601 }
00602 
00607 gboolean on_result_button_press_event(GtkWidget * widget,
00608                                       GdkEventButton * event,
00609                                       gpointer user_data)
00610 {
00611     if (event->button == 3) {
00612         if (resultPopup == NULL) {
00613             resultPopup = result_popup_create();
00614             if (resultPopup == NULL) {
00615                 g_error("Couldnt create menu\n");
00616                 return FALSE;
00617             }
00618         }
00619         gtk_menu_popup(GTK_MENU(resultPopup), NULL, NULL, NULL, NULL,
00620                        event->button, event->time);
00621         return TRUE;
00622     }
00623     return FALSE;
00624 }
00625 
00629 char *query_get_text(GtkWidget * query)
00630 {
00631     return gtk_editable_get_chars(GTK_EDITABLE(query), 0, -1);
00632 }
00633 
00634 void query_save_to_file(GtkWidget *query, char *fn) {
00635     FILE *fp;
00636     char *cquery;
00637     char *name;
00638 
00639     if ((query == NULL) || (fn == NULL)) {
00640         g_warning("query_save_to_file(%p, %p)\n", query, fn);
00641         return;
00642     }
00643 
00644     if (strlen(fn) < 1) {
00645         g_warning("query_save_to_file(). Ignoring null filename\n");
00646         return;
00647     }
00648 
00649     cquery = gtk_editable_get_chars(GTK_EDITABLE(query), 0, -1);
00650 
00651     fp = fopen(fn, "wb");
00652     if (fp != NULL) {
00653         fprintf(fp, "%s\n", cquery);
00654         fclose(fp);
00655         name = get_short_filename(fn);
00656         if (name == NULL)
00657             name = "Untitled";
00658     
00659         gui_set_query_name(name);
00660     }
00661 }
00662 
00664 GtkWidget *query_add(void)
00665 {
00666     GtkWidget *label;
00667     GtkWidget *newQuery;
00668     GtkWidget *scroller;
00669     QueryData *qd;
00670     ABuf *buf = buf_new(64);
00671     static GtkTargetEntry target = {
00672         "text/plain", 0, 0
00673     };
00674     char *status_buffer;
00675 
00676     /* Scroller */
00677     scroller = gtk_scrolled_window_new(NULL, NULL);
00678     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller),
00679                                    GTK_POLICY_AUTOMATIC,
00680                                    GTK_POLICY_AUTOMATIC);
00681 
00682     /* The query text zone */
00683     newQuery = gtk_text_new(NULL, NULL);
00684 
00685     gtk_text_set_editable(GTK_TEXT(newQuery), TRUE);
00686     gtk_signal_connect(GTK_OBJECT(newQuery), "changed",
00687                        GTK_SIGNAL_FUNC(changed_event), newQuery);
00688     gtk_signal_connect(GTK_OBJECT(newQuery), "key_press_event",
00689                        GTK_SIGNAL_FUNC(key_press_event), newQuery);
00690     gtk_signal_connect(GTK_OBJECT(newQuery), "button_press_event",
00691                        GTK_SIGNAL_FUNC(on_query_button_press_event),
00692                        newQuery);
00693     gtk_widget_show(newQuery);
00694 
00695     qd = querydata_new();
00696     gtk_object_set_data(GTK_OBJECT(newQuery), QDATA_KEY, qd);
00697     gtk_object_set_data(GTK_OBJECT(newQuery), RESULT_KEY, NULL);
00698 
00699     /* Setup to receive dropped text */
00700     gtk_signal_connect(GTK_OBJECT(newQuery), "drag_data_received",
00701                        GTK_SIGNAL_FUNC(receive_callback), NULL);
00702     gtk_drag_dest_set(GTK_WIDGET(newQuery),
00703                       GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT |
00704                       GTK_DEST_DEFAULT_DROP, &target, 1, GDK_ACTION_COPY);
00705 
00706     /* Add the text field to the scroller */
00707     gtk_container_add(GTK_CONTAINER(scroller), newQuery);
00708     gtk_widget_show(scroller);
00709 
00710     /* Add the query to the notebook */
00711     snprintf(buf->b_dat, buf->b_len, "Query%d", ++lastQuery);
00712     label = gtk_label_new(buf->b_dat);
00713     gtk_widget_show(GTK_WIDGET(label));
00714     gtk_notebook_append_page(GTK_NOTEBOOK(queryNotebook), scroller, label);
00715     gtk_object_set_data(GTK_OBJECT(newQuery), POSITION_KEY, (void *)nbQueries);
00716     gtk_notebook_set_page(GTK_NOTEBOOK(queryNotebook), nbQueries);
00717 
00718     /* Add an empty result to the result notebook */
00719     results_display(NULL, QUERY_FAKE, -1);
00720 
00721     /* Add to the query a buffer to display a query status */
00722     status_buffer = (char *) malloc(STATUS_SIZE * sizeof(char));
00723     strcpy(status_buffer, "Ready");
00724     gtk_object_set_data(GTK_OBJECT(newQuery), STATUS_KEY,
00725                         (void *) status_buffer);
00726     if (nbQueries == 0) {
00727         status_push(status_buffer);
00728         queryStatus = 1;
00729     }
00730 
00731     nbQueries++;
00732     buf_free(buf);
00733 
00734     if (queryNotebookPageChangeHandler == 0) {
00735         queryNotebookPageChangeHandler = gtk_signal_connect(GTK_OBJECT(queryNotebook), "switch_page",
00736                            GTK_SIGNAL_FUNC(notebook_selection_callback), NULL);
00737     }
00738 
00739     //results_display(NULL, QUERY_FAKE, nbQueries-1);
00740     update_button_sensitivity();
00741     return scroller;
00742 }
00743 
00746 int query_close()
00747 {
00748     gint current_page;
00749 
00750     current_page = gtk_notebook_current_page(GTK_NOTEBOOK(queryNotebook));
00751 
00752     if (queryStatus == 1) {
00753         queryStatus = 0;
00754         status_pop();
00755     }
00756     return query_delete_at_position(current_page);
00757 }
00758 
00760 GtkWidget *query_get_current()
00761 {
00762     /* The notebook page is the scroller. */
00763     int cur_page = gtk_notebook_current_page(GTK_NOTEBOOK(queryNotebook));
00764     GtkWidget *scroller =
00765         gtk_notebook_get_nth_page(GTK_NOTEBOOK(queryNotebook), cur_page);
00766     GList *children;
00767 
00768     if (scroller == NULL)
00769         return NULL;
00770 
00771     children = gtk_container_children(GTK_CONTAINER(scroller));
00772     if (children == NULL)
00773         return NULL;
00774 
00775     return GTK_WIDGET(children->data);
00776 }
00777 
00781 GtkWidget *query_get_no(int n)
00782 {
00783     /* The notebook page is the scroller. */
00784     GtkWidget *scroller =
00785         gtk_notebook_get_nth_page(GTK_NOTEBOOK(queryNotebook), n);
00786     GList *children;
00787 
00788     if (scroller == NULL)
00789         return NULL;
00790 
00791     children = gtk_container_children(GTK_CONTAINER(scroller));
00792     if (children == NULL)
00793         return NULL;
00794 
00795     return GTK_WIDGET(children->data);
00796 }
00797 
00801 char *query_get_text_no(int n)
00802 {
00803     GtkWidget *qry = query_get_no(n);
00804     if (!qry)
00805         return NULL;
00806 
00807     return query_get_text(qry);
00808 }
00809 
00811 char *query_get_current_name()
00812 {
00813     GtkWidget *label;
00814     int position = gtk_notebook_current_page(GTK_NOTEBOOK(queryNotebook));
00815     char *qname;
00816 
00817     label = gtk_notebook_get_tab_label(GTK_NOTEBOOK(queryNotebook),
00818                                        gtk_notebook_get_nth_page(GTK_NOTEBOOK
00819                                                                  (queryNotebook),
00820                                                                  position));
00821     gtk_label_get(GTK_LABEL(label), &qname);
00822 
00823     return qname;
00824 }
00825 
00829 QueryData *query_get_querydata(GtkWidget *query) {
00830     QueryData *qd = gtk_object_get_data(GTK_OBJECT(query), QDATA_KEY);
00831     if (qd == NULL)
00832         qd = querydata_new();
00833 
00834     return qd;
00835 }
00836 
00840 void *query_get_result(GtkWidget * query)
00841 {
00842     if (query == NULL)
00843         return NULL;
00844 
00845     return (void *) gtk_object_get_data(GTK_OBJECT(query), RESULT_KEY);
00846 }
00847 
00849 void queries_init()
00850 {
00851     lastQuery = 0;
00852     if (queryNotebookPageChangeHandler == 0) {
00853         queryNotebookPageChangeHandler = gtk_signal_connect(GTK_OBJECT(queryNotebook), "switch_page",
00854                            GTK_SIGNAL_FUNC(notebook_selection_callback), NULL);
00855     }
00856 }
00857 
00860 GtkWidget *query_pane_init()
00861 {
00862     /* The query notebook */
00863     queryNotebook = gtk_notebook_new();
00864     gtk_widget_set_name(GTK_WIDGET(queryNotebook), "queries");
00865     gtk_notebook_set_tab_pos(GTK_NOTEBOOK(queryNotebook), GTK_POS_TOP);
00866     gtk_notebook_set_scrollable(GTK_NOTEBOOK(queryNotebook), TRUE);
00867     gtk_notebook_popup_enable(GTK_NOTEBOOK(queryNotebook));
00868     gtk_widget_set_usize(queryNotebook, 250, 130);
00869 
00870     queryNotebookPageChangeHandler =
00871         gtk_signal_connect(GTK_OBJECT(queryNotebook), "switch_page",
00872                            GTK_SIGNAL_FUNC(notebook_selection_callback),
00873                            NULL);
00874     gtk_widget_show(queryNotebook);
00875     return queryNotebook;
00876 }
00877 
00880 GtkWidget *result_pane_init()
00881 {
00882     GtkWidget *container = gtk_vbox_new(FALSE, 0);
00883 
00884     resultNotebook = gtk_notebook_new();
00885     gtk_widget_set_name(GTK_WIDGET(resultNotebook), "results");
00886     gtk_notebook_popup_enable(GTK_NOTEBOOK(resultNotebook));
00887     gtk_box_pack_start(GTK_BOX(container), resultNotebook, TRUE, TRUE, 0);
00888     gtk_notebook_set_show_tabs(GTK_NOTEBOOK(resultNotebook), FALSE);
00889 
00890     gtk_widget_show(container);
00891     gtk_widget_show(resultNotebook);
00892     return container;
00893 }
00894 
00896 void query_import(void)
00897 {
00898     query_import_dialog_show();
00899 }
00900 
00902 int query_count()
00903 {
00904     return nbQueries;
00905 }
00906 
00909 void query_save(GtkWidget *qry)
00910 {
00911     GtkWidget *query;
00912     QueryData *qd;
00913 
00914     if (qry == NULL)
00915         query = query_get_current();
00916     else
00917         query = qry;
00918 
00919     if (query == NULL)
00920         return;
00921 
00922     qd = query_get_querydata(query);
00923     if (! qd)
00924         return;
00925 
00926     if (qd->filename == NULL)
00927         query_save_as(NULL);
00928     else
00929         query_save_to_file(query, qd->filename);
00930 }
00931 
00933 void query_save_as(GtkWidget *qry)
00934 {
00935     query_save_dialog_show(qry);
00936 }

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