heraia_ui.c

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /*
00003   heraia_ui.c
00004   main menus, callback and utility functions
00005 
00006   (C) Copyright 2005 - 2009 Olivier Delhomme
00007   e-mail : heraia@delhomme.org
00008   URL    : http://heraia.tuxfamily.org
00009 
00010   This program is free software; you can redistribute it and/or modify
00011   it under the terms of the GNU General Public License as published by
00012   the Free Software Foundation; either version 2, or  (at your option)
00013   any later version.
00014 
00015   This program is distributed in the hope that it will be useful,
00016   but WITHOUT ANY WARRANTY;  without even the implied warranty of
00017   MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the
00018   GNU General Public License for more details.
00019 
00020   You should have received a copy of the GNU General Public License
00021   along with this program; if not, write to the Free Software
00022   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023 */
00024 /** @file heraia_ui.c
00025  * This file has all the functions to manage heraia's ui
00026  * - signals definitions and functions
00027  * - widgets activations
00028  * - closing / openning windows
00029  */
00030 
00031 #include <libheraia.h>
00032 
00033 static void set_a_propos_properties(GtkWidget *about_dialog);
00034 static gboolean load_heraia_glade_xml(heraia_window_t *main_window);
00035 static void heraia_ui_connect_signals(heraia_window_t *main_window);
00036 static void record_and_hide_about_box(heraia_window_t *main_window);
00037 static void close_heraia(heraia_window_t *main_window);
00038 
00039 /**
00040  * @fn void on_quit_activate(GtkWidget *widget, gpointer data)
00041  *  Quit, file menu
00042  * @param widget : the widget that issued the signal
00043  * @param data : user data MUST be heraia_window_t *main_window main structure
00044  */
00045 void on_quit_activate(GtkWidget *widget, gpointer data)
00046 {
00047         heraia_window_t *main_window = (heraia_window_t *) data;
00048 
00049         close_heraia(main_window);
00050         gtk_main_quit();
00051 }
00052 
00053 /**
00054  * @fn void on_new_activate(GtkWidget *widget, gpointer data)
00055  *  New, file menu
00056  * @param widget : the widget that issued the signal
00057  * @param data : user data MUST be heraia_window_t *main_window main structure
00058  */
00059 void on_new_activate(GtkWidget *widget, gpointer data)
00060 {
00061         heraia_window_t *main_window = (heraia_window_t *) data;
00062 
00063         log_message(main_window, G_LOG_LEVEL_WARNING, "Not implemented Yet (Please contribute !)");
00064 }
00065 
00066 /**
00067  * @fn void on_preferences_activate(GtkWidget *widget, gpointer data)
00068  *  Preferences, file menu :
00069  *  Displays the preference window (as a modal window)
00070  * @param widget : the widget that issued the signal
00071  * @param data : user data MUST be heraia_window_t *main_window main structure
00072  */
00073 void on_preferences_activate(GtkWidget *widget, gpointer data)
00074 {
00075         heraia_window_t *main_window = (heraia_window_t *) data;
00076         GtkWidget *pref_window = NULL;
00077 
00078         pref_window = heraia_get_widget(main_window->xmls->main, "main_preferences_window");
00079 
00080         if (pref_window != NULL)
00081         {
00082                 move_and_show_dialog_box(pref_window, main_window->win_prop->main_pref_window);
00083         }
00084 
00085 }
00086 
00087 /**
00088  * @fn void set_a_propos_properties(GtkWidget *about_dialog)
00089  * Sets name and version in the dialog box
00090  * @param about_dialog the widget that contain all the about box
00091  */
00092 static void set_a_propos_properties(GtkWidget *about_dialog)
00093 {
00094 
00095         if (about_dialog != NULL)
00096         {
00097                 if (GTK_MINOR_VERSION >= 12)
00098                 {
00099                     gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(about_dialog), PACKAGE_NAME);
00100                 }
00101                 if (GTK_MINOR_VERSION >= 6)
00102                 {
00103                         gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about_dialog), PACKAGE_VERSION);
00104                 }
00105         }
00106 }
00107 
00108 
00109 /**
00110  * @fn void a_propos_activate(GtkWidget *widget, gpointer data)
00111  *  Shows apropos's dialog box
00112  * @param widget : the widget that issued the signal
00113  * @param data : user data MUST be heraia_window_t *main_window main structure
00114  */
00115 void a_propos_activate(GtkWidget *widget, gpointer data)
00116 {
00117         heraia_window_t *main_window = (heraia_window_t *) data;
00118         GtkWidget *about_dialog = NULL;
00119 
00120         about_dialog = heraia_get_widget(main_window->xmls->main, "about_dialog");
00121 
00122         if (about_dialog != NULL)
00123         {
00124                 set_a_propos_properties(about_dialog);
00125 
00126                 move_and_show_dialog_box(about_dialog, main_window->win_prop->about_box);
00127         }
00128 }
00129 
00130 /**
00131  * @fn void move_and_show_dialog_box(GtkWidget *dialog_box, window_prop_t *dialog_prop)
00132  *  Move the dialog box to the wanted position, shows it and says it in the displayed prop
00133  * @param dialog_box : the dialog box we want to move and show
00134  * @param dialog_prop : window_prop_t properties structure corresponding to the dialog box
00135  */
00136 void move_and_show_dialog_box(GtkWidget *dialog_box, window_prop_t *dialog_prop)
00137 {
00138         if (dialog_prop->displayed == FALSE)
00139         {
00140                 gtk_window_move(GTK_WINDOW(dialog_box), dialog_prop->x, dialog_prop->y);
00141                 gtk_window_resize(GTK_WINDOW(dialog_box), dialog_prop->width, dialog_prop->height);
00142                 gtk_widget_show_all(dialog_box);
00143                 dialog_prop->displayed = TRUE;
00144         }
00145 }
00146 
00147 /**
00148  * @fn void record_dialog_box_position(GtkWidget *dialog_box, window_prop_t *dialog_prop)
00149  * Records one dialog position
00150  * @param dialog_box : a dialog box from which we want to record the position
00151  * @param[in,out] dialog_prop : window_prop_t properties structure corresponding to the dialog box
00152  */
00153 void record_dialog_box_position(GtkWidget *dialog_box, window_prop_t *dialog_prop)
00154 {
00155         gint x = 0;
00156         gint y = 0;
00157         gint width = WPT_DEFAULT_WIDTH;
00158         gint height = WPT_DEFAULT_HEIGHT;
00159 
00160         if (dialog_prop != NULL && dialog_prop->displayed == TRUE)
00161         {
00162                 if (dialog_box != NULL)
00163                 {
00164                         gtk_window_get_position(GTK_WINDOW(dialog_box), &x, &y);
00165                         gtk_window_get_size(GTK_WINDOW(dialog_box), &width, &height);
00166                         dialog_prop->x = x;
00167                         dialog_prop->y = y;
00168                         dialog_prop->width = width;
00169                         dialog_prop->height = height;
00170                 }
00171         }
00172 }
00173 
00174 
00175 /**
00176  * @fn void record_all_dialog_box_positions(heraia_window_t *main_window)
00177  * Records all the positions of the displayed windows
00178  * @param[in,out] main_window : main structure
00179  */
00180 void record_all_dialog_box_positions(heraia_window_t *main_window)
00181 {
00182         GtkWidget *dialog_box = NULL;
00183 
00184         if (main_window != NULL &&
00185                 main_window->xmls != NULL &&
00186                 main_window->xmls->main != NULL &&
00187                 main_window->win_prop != NULL &&
00188                 main_window->current_DW != NULL)
00189         {
00190                 /* data interpretor */
00191                 dialog_box = main_window->current_DW->diw;
00192                 record_dialog_box_position(dialog_box, main_window->win_prop->data_interpretor);
00193 
00194                 /* About box */
00195                 dialog_box = heraia_get_widget (main_window->xmls->main, "about_dialog");
00196                 record_dialog_box_position(dialog_box, main_window->win_prop->about_box);
00197 
00198                 /* Log window */
00199                 dialog_box = heraia_get_widget (main_window->xmls->main, "log_window");
00200                 record_dialog_box_position(dialog_box, main_window->win_prop->log_box);
00201 
00202                 /* main_dialog */
00203                 dialog_box = heraia_get_widget (main_window->xmls->main, "main_window");
00204                 record_dialog_box_position(dialog_box, main_window->win_prop->main_dialog);
00205 
00206                 /* plugin_list */
00207                 dialog_box = heraia_get_widget (main_window->xmls->main, "plugin_list_window");
00208                 record_dialog_box_position(dialog_box, main_window->win_prop->plugin_list);
00209 
00210                 /* list data types */
00211                 dialog_box = heraia_get_widget (main_window->xmls->main, "list_data_types_window");
00212                 record_dialog_box_position(dialog_box, main_window->win_prop->ldt);
00213 
00214                 /* main_preferences */
00215                 dialog_box = heraia_get_widget (main_window->xmls->main, "main_preferences_window");
00216                 record_dialog_box_position(dialog_box, main_window->win_prop->main_pref_window);
00217         }
00218 }
00219 
00220 
00221 /**
00222  * @fn void record_and_hide_dialog_box(GtkWidget *dialog_box, window_prop_t *dialog_prop)
00223  *  Record position and hide a dialog box
00224  * @param dialog_box : the dialog box we want to record its position and then hide
00225  * @param dialog_prop : window_prop_t properties structure corresponding to the dialog box
00226  */
00227 void record_and_hide_dialog_box(GtkWidget *dialog_box, window_prop_t *dialog_prop)
00228 {
00229 
00230         if (dialog_prop->displayed == TRUE)
00231         {
00232                 record_dialog_box_position(dialog_box, dialog_prop);
00233 
00234                 gtk_widget_hide(dialog_box);
00235                 dialog_prop->displayed = FALSE;
00236         }
00237 }
00238 
00239 
00240 /**
00241  * @fn static void record_and_hide_about_box(heraia_window_t *main_window)
00242  *  Record position and hide about dialog box
00243  * @param [in,out] main_window : main structure
00244  */
00245 static void record_and_hide_about_box(heraia_window_t *main_window)
00246 {
00247         GtkWidget *about_dialog = NULL;
00248 
00249         about_dialog = heraia_get_widget(main_window->xmls->main, "about_dialog");
00250 
00251         if (about_dialog != NULL)
00252         {
00253                 record_and_hide_dialog_box(about_dialog, main_window->win_prop->about_box);
00254         }
00255 }
00256 
00257 
00258 /**
00259  * @fn void a_propos_response(GtkWidget *widget, gint response, gpointer data)
00260  *  To close the A propos dialog box (with the "close" button)
00261  * @param widget : calling widget (may be NULL as we don't use this)
00262  * @param response : may be whatever you want as we neither use this !
00263  * @param data : MUST be heraia_window_t *main_window main structure
00264  */
00265 static void a_propos_response(GtkWidget *widget, gint response, gpointer data)
00266 {
00267         heraia_window_t *main_window = (heraia_window_t *) data;
00268         record_and_hide_about_box(main_window);
00269 }
00270 
00271 /**
00272  * @fn void a_propos_close(GtkWidget *widget, gpointer data)
00273  *  To close the A propos dialog box
00274  * @param widget : calling widget (may be NULL as we don't use this)
00275  * @param data : MUST be heraia_window_t *main_window main structure
00276  */
00277 static void a_propos_close(GtkWidget *widget, gpointer data)
00278 {
00279         heraia_window_t *main_window = (heraia_window_t *) data;
00280         record_and_hide_about_box(main_window);
00281 }
00282 
00283 
00284 /**
00285  * @fn gboolean a_propos_delete(GtkWidget *widget, GdkEvent  *event, gpointer data)
00286  *  To close the A propos dialog box
00287  * @param widget : calling widget (may be NULL as we don't use this)
00288  * @param event : event associated (may be NULL as we don't use this)
00289  * @param data : MUST be heraia_window_t *main_window main structure
00290  * @return returns TRUE in order to allow other functions to do something with
00291  *         that event.
00292  */
00293 static gboolean a_propos_delete(GtkWidget *widget, GdkEvent  *event, gpointer data)
00294 {
00295         heraia_window_t *main_window = (heraia_window_t *) data;
00296         record_and_hide_about_box(main_window);
00297 
00298         return TRUE;
00299 }
00300 
00301 
00302 /**
00303  * @fn void on_delete_activate(GtkWidget *widget, gpointer data)
00304  *  Delete, edit menu
00305  * @warning Not yet implemented
00306  * @todo Write a usefull function here :)
00307  * @param widget : the widget that issued the signal
00308  * @param data : user data MUST be heraia_window_t *main_window main structure
00309  */
00310 void on_delete_activate(GtkWidget *widget, gpointer data)
00311 {
00312         heraia_window_t *main_window = (heraia_window_t *) data;
00313 
00314         log_message(main_window, G_LOG_LEVEL_WARNING, "Not implemented Yet (Please contribute !)");
00315 }
00316 
00317 /**
00318  * @fn void on_cut_activate(GtkWidget *widget, gpointer data)
00319  *  Cut, edit menu
00320  * @warning Not yet implemented
00321  * @todo Write a usefull function here :)
00322  * @param widget : the widget that issued the signal
00323  * @param data : user data MUST be heraia_window_t *main_window main structure
00324  */
00325 void on_cut_activate(GtkWidget *widget, gpointer data)
00326 {
00327         heraia_window_t *main_window = (heraia_window_t *) data;
00328 
00329         log_message(main_window, G_LOG_LEVEL_WARNING, "Not implemented Yet (Please contribute !)");
00330 }
00331 
00332 /**
00333  * @fn void on_copy_activate( GtkWidget *widget, gpointer data )
00334  *  Copy, edit menu
00335  * @warning Not yet implemented
00336  * @todo Write a usefull function here :)
00337  * @param widget : the widget that issued the signal
00338  * @param data : user data MUST be heraia_window_t *main_window main structure
00339  */
00340 void on_copy_activate(GtkWidget *widget, gpointer data)
00341 {
00342         heraia_window_t *main_window = (heraia_window_t *) data;
00343 
00344         log_message(main_window, G_LOG_LEVEL_WARNING, "Not implemented Yet (Please contribute !)");
00345 }
00346 
00347 
00348 /**
00349  * @fn void on_paste_activate( GtkWidget *widget, gpointer data )
00350  *  Paste, edit menu
00351  * @warning Not yet implemented
00352  * @todo Write a usefull function here :)
00353  * @param widget : the widget that issued the signal
00354  * @param data : user data MUST be heraia_window_t *main_window main structure
00355  */
00356 void on_paste_activate(GtkWidget *widget, gpointer data)
00357 {
00358         heraia_window_t *main_window = (heraia_window_t *) data;
00359 
00360         log_message(main_window, G_LOG_LEVEL_WARNING, "Not implemented Yet (Please contribute !)");
00361 }
00362 
00363 
00364 /**
00365  * @fn void refresh_file_labels(heraia_window_t *main_window)
00366  *  This function is refreshing the labels on the main
00367  *  window in order to reflect cursor position, selected
00368  *  positions and total selected size
00369  * @param main_window : main structure
00370  */
00371 void refresh_file_labels(heraia_window_t *main_window)
00372 {
00373         GtkWidget *label = NULL;
00374         guint64 position = 0;
00375         gchar *text = NULL;
00376 
00377         if (main_window != NULL)
00378                 {
00379                         if (main_window->current_doc != NULL && main_window->current_doc->hex_widget != NULL)
00380                                 {
00381                                         label = heraia_get_widget(main_window->xmls->main, "file_position_label");
00382                                         position = ghex_get_cursor_position(main_window->current_doc->hex_widget);
00383                                         /* position begins at 0 and this is not really human readable */
00384                                         /* it's more confusing than anything so we do + 1             */
00385                                         /* To translators : do not translate <small> and such         */
00386 
00387                                         if (is_toggle_button_activated(main_window->xmls->main, "mp_thousand_bt") == TRUE)
00388                                         {
00389                                                 text = g_strdup_printf("<small>%'lld</small>", position + 1);
00390                                         }
00391                                         else
00392                                         {
00393                                                 text = g_strdup_printf("<small>%lld</small>", position + 1);
00394                                         }
00395                                         gtk_label_set_markup(GTK_LABEL(label), text);
00396                                         g_free(text);
00397                                 }
00398                         else
00399                                 {
00400                                         label = heraia_get_widget(main_window->xmls->main, "file_position_label");
00401                                         gtk_label_set_text(GTK_LABEL(label), "");
00402                                 }
00403                 }
00404 }
00405 
00406 
00407 /**
00408  * @fn void refresh_event_handler(GtkWidget *widget, gpointer data)
00409  *  This function is here to ensure that everything will be
00410  *  refreshed upon a signal event.
00411  * @warning This function is not thread safe (do not use in a thread)
00412  * @todo try to put some mutexes on main_window->event to make this
00413  *       thread safe some way
00414  * @param widget : the widget that issued the signal
00415  * @param data : user data MUST be heraia_window_t *main_window main structure
00416  */
00417 void refresh_event_handler(GtkWidget *widget, gpointer data)
00418 {
00419         heraia_window_t *main_window = (heraia_window_t *) data;
00420 
00421         if (main_window != NULL)
00422         {
00423                 /* Beware, this mechanism is not thread safe ! */
00424                 if (main_window->event == HERAIA_REFRESH_NOTHING)
00425                 {
00426                         main_window->event = HERAIA_REFRESH_CURSOR_MOVE;
00427                 }
00428 
00429                 refresh_data_interpretor_window(widget, main_window);
00430                 refresh_all_plugins(main_window);
00431                 refresh_file_labels(main_window);
00432 
00433                 main_window->event = HERAIA_REFRESH_NOTHING;
00434         }
00435 }
00436 
00437 
00438 /**
00439  * @fn void on_open_activate(GtkWidget *widget, gpointer data)
00440  *  This handles the menuitem "Ouvrir" to open a file
00441  * @warning This function is not thread safe (do not use in a thread)
00442  * @todo try to put some mutexes on main_window->event to make this
00443  *       thread safe some way
00444  * @param widget : the widget that issued the signal
00445  * @param data : user data MUST be heraia_window_t *main_window main structure
00446  */
00447 void on_open_activate(GtkWidget *widget, gpointer data)
00448 {
00449         heraia_window_t *main_window = (heraia_window_t *) data;
00450         gchar *filename = NULL;
00451         gboolean success = FALSE;
00452 
00453         filename = select_file_to_load(main_window);
00454         if (filename != NULL)
00455         {
00456                 success = load_file_to_analyse(main_window, filename);
00457                 if (success == TRUE && main_window->current_doc != NULL)
00458                  {
00459                         /* Not thread safe here ? */
00460                         main_window->event = HERAIA_REFRESH_NEW_FILE;
00461                         refresh_event_handler(main_window->current_doc->hex_widget, main_window);
00462                  }
00463         }
00464 }
00465 
00466 
00467 /**
00468  * @fn void on_save_activate(GtkWidget *widget, gpointer data)
00469  *  Here we attemp to save the edited file
00470  *  @todo be more accurate on error (error type, message and filename) returns
00471  *        we should return something at least ...
00472  * @param widget : the widget that issued the signal
00473  * @param data : user data MUST be heraia_window_t *main_window main structure
00474  */
00475 void on_save_activate(GtkWidget *widget, gpointer data)
00476 {
00477         heraia_window_t *main_window = (heraia_window_t *) data;
00478         HERAIA_ERROR erreur = HERAIA_NOERR;
00479         gchar *filename = NULL;
00480 
00481         if (main_window != NULL && main_window->current_doc != NULL)
00482         {
00483                 erreur = heraia_hex_document_save(main_window);
00484 
00485                 if (erreur != HERAIA_NOERR)
00486                 {
00487                         filename = doc_t_document_get_filename(main_window->current_doc);
00488                         log_message(main_window, G_LOG_LEVEL_ERROR, "Error while saving file %s !", filename);
00489                 }
00490         }
00491 }
00492 
00493 /**
00494  * @fn void on_save_as_activate(GtkWidget *widget, gpointer data)
00495  *  This handle the save_as menu entry (here the filename changes)
00496  * @param widget : the widget that issued the signal
00497  * @param data : user data MUST be heraia_window_t *main_window main structure
00498  */
00499 void on_save_as_activate(GtkWidget *widget, gpointer data)
00500 {
00501         heraia_window_t *main_window = (heraia_window_t *) data;
00502         HERAIA_ERROR erreur = HERAIA_NOERR;
00503         gchar *filename = NULL;  /* Auto malloc'ed, do not free */
00504 
00505         if (main_window != NULL && main_window->current_doc != NULL)
00506         {
00507                 filename = select_a_file_to_save(main_window);
00508 
00509                 if (filename != NULL)
00510                 {
00511                         erreur = heraia_hex_document_save_as(main_window, filename);
00512                 }
00513                 else
00514                 {
00515                         erreur = HERAIA_CANCELLED;
00516                 }
00517 
00518                 if (erreur != HERAIA_NOERR)
00519                 {
00520                         if (erreur == HERAIA_CANCELLED)
00521                         {
00522                                 log_message(main_window, G_LOG_LEVEL_DEBUG, "Saving file as... : operation cancelled.");
00523                         }
00524                         else
00525                         {
00526                                 log_message(main_window, G_LOG_LEVEL_ERROR, "Error while saving file as %s", doc_t_document_get_filename(main_window->current_doc));
00527                         }
00528                 }
00529                 else
00530                 {
00531                         /* updating the window name and tab's name */
00532                         update_main_window_name(main_window);
00533                         set_notebook_tab_name(main_window);
00534                         log_message(main_window, G_LOG_LEVEL_DEBUG, "File %s saved and now edited.", doc_t_document_get_filename(main_window->current_doc));
00535                 }
00536         }
00537 }
00538 
00539 
00540 /**
00541  * @fn void on_DIMenu_activate(GtkWidget *widget, gpointer data)
00542  *  This handles the menuitem "Data Interpretor" that
00543  *  shows or hides the data interpretor window
00544  * @param widget : the widget that issued the signal
00545  * @param data : user data MUST be heraia_window_t *main_window main structure
00546  */
00547 void on_DIMenu_activate(GtkWidget *widget, gpointer data)
00548 {
00549 
00550         heraia_window_t *main_window = (heraia_window_t *) data;
00551         data_window_t *dw = NULL;      /* program structure           */
00552         GtkNotebook *notebook = NULL;  /* data interpretor's notebook */
00553 
00554         if (main_window != NULL)
00555         {
00556                 dw = main_window->current_DW;
00557 
00558                 if (dw != NULL)
00559                 {
00560                         if (dw->diw == NULL)
00561                         {
00562                                 dw->diw = heraia_get_widget(main_window->xmls->main, "data_interpretor_window");
00563                         }
00564 
00565                         if (dw->diw != NULL)
00566                         {
00567                                 /* dw->window_displayed = !(dw->window_displayed); */
00568                                 notebook = GTK_NOTEBOOK(heraia_get_widget(main_window->xmls->main, "diw_notebook"));
00569 
00570                         if (main_window->win_prop->data_interpretor->displayed == FALSE)
00571                                 {
00572                                         /* Setting the first page of the notebook as default (Numbers) */
00573                                         gtk_notebook_set_current_page(notebook, dw->tab_displayed);
00574 
00575                                         /* moving to the right position */
00576                                         move_and_show_dialog_box(dw->diw, main_window->win_prop->data_interpretor);
00577 
00578                                         refresh_data_interpretor_window(widget, data);
00579                                 }
00580                                 else
00581                                 {
00582                                         /* recording some prefs from the dialog : position + opened tab */
00583                                         dw->tab_displayed = gtk_notebook_get_current_page(notebook);
00584                                         record_and_hide_dialog_box(dw->diw, main_window->win_prop->data_interpretor);
00585                                 }
00586                         }
00587                 }
00588         }
00589 }
00590 
00591 
00592 /**
00593  * @fn delete_main_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data)
00594  *  When the user destroys or delete the main window
00595  * @param widget : calling widget
00596  * @param event : event associated (may be NULL as we don't use this here)
00597  * @param data : MUST be heraia_window_t *main_window main structure
00598  */
00599 gboolean delete_main_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data)
00600 {
00601 
00602         on_quit_activate(widget, data);
00603 
00604         return FALSE;
00605 }
00606 
00607 
00608 /**
00609  * @fn gboolean delete_dt_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data)
00610  *  call back function for the data interpretor window destruction
00611  * @param widget : calling widget (may be NULL as we don't use this here)
00612  * @param event : event associated (may be NULL as we don't use this here)
00613  * @param data : MUST be heraia_window_t *main_window main structure and not NULL
00614  */
00615 gboolean delete_dt_window_event(GtkWidget *widget, GdkEvent  *event, gpointer data)
00616 {
00617         heraia_window_t *main_window = (heraia_window_t *) data;
00618 
00619         g_signal_emit_by_name(heraia_get_widget(main_window->xmls->main, "DIMenu"), "activate");
00620 
00621         return TRUE;
00622 }
00623 
00624 /**
00625  * @fn void destroy_dt_window(GtkWidget *widget, GdkEvent  *event, gpointer data)
00626  *  call back function for the data interpretor window destruction
00627  * @param widget : calling widget (may be NULL as we don't use this here)
00628  * @param event : event associated (may be NULL as we don't use this here)
00629  * @param data : MUST be heraia_window_t *main_window main structure and not NULL
00630  */
00631 void destroy_dt_window(GtkWidget *widget, GdkEvent  *event, gpointer data)
00632 {
00633         heraia_window_t *main_window = (heraia_window_t *) data;
00634 
00635         g_signal_emit_by_name(heraia_get_widget(main_window->xmls->main, "DIMenu"), "activate");
00636 }
00637 
00638 
00639 
00640 /**
00641  * What to do when a change occurs in tabs (user selected a particular
00642  * tab
00643  * @param notebook : the widget that issued this signal
00644  * @param page : the new current page
00645  * @param tab_num : index of this page
00646  * @param data : MUST be heraia_window_t *main_window !
00647  */
00648 gboolean file_notebook_tab_changed(GtkNotebook *notebook, GtkNotebookPage *page, gint tab_num, gpointer data)
00649 {
00650         heraia_window_t *main_window = (heraia_window_t *) data;
00651 
00652         if (main_window != NULL)
00653         {
00654                 main_window->current_doc = g_ptr_array_index(main_window->documents, tab_num);
00655                 update_main_window_name(main_window);
00656                 main_window->event = HERAIA_REFRESH_TAB_CHANGED;
00657                 refresh_event_handler(GTK_WIDGET(notebook), main_window);
00658                 main_window->event = HERAIA_REFRESH_NOTHING;
00659 
00660         }
00661 
00662         return TRUE;
00663 }
00664 
00665 /* End of call back functions that handle the data interpretor window */
00666 
00667 
00668 /**
00669  * @fn static gchar *make_absolute_path(gchar *filename)
00670  *  Returns an absolute path to the filename
00671  *  the string should be freed when no longer needed
00672  *  very UGLy !
00673  * @todo do something without any system calls !!!
00674  * @param filename : relative notation filename from which to extract an
00675  *        absolute path
00676  * @return returns a string with the absolute path which should be freed when
00677  *         no longer needed
00678  */
00679 static gchar *make_absolute_path(gchar *filename)
00680 {
00681         gchar *current_dir = NULL;
00682         gchar *new_dir = NULL;
00683 
00684         if (g_path_is_absolute(filename) == TRUE)
00685         {
00686                 /* if the filename is already in an absolute format */
00687                 return  g_path_get_dirname(filename);
00688         }
00689         else
00690         {
00691                 current_dir = g_get_current_dir();
00692                 new_dir = g_path_get_dirname(filename);
00693 
00694                 if (g_chdir(new_dir) == 0)
00695                 {
00696                         g_free(new_dir);
00697                         new_dir = g_get_current_dir();
00698                         g_chdir(current_dir);
00699                         g_free(current_dir);
00700 
00701                         return new_dir;
00702                 }
00703                 else
00704                 {
00705                         g_free(current_dir);
00706 
00707                         return NULL;
00708                 }
00709         }
00710 }
00711 
00712 
00713 /**
00714  * @fn void set_the_working_directory(GtkFileChooser *file_chooser, gchar *filename)
00715  *  Sets the working directory for the file chooser to the directory of the
00716  *  filename (even if filename is a relative filename such as ../docs/test_file)
00717  * @param file_chooser : An initialized GtkFileChooser
00718  * @param filename : a filename (one previously openned)
00719  */
00720 static void set_the_working_directory(GtkFileChooser *file_chooser, gchar *filename)
00721 {
00722         gchar *dirname = NULL;    /* directory where we want to be, at first, in the file chooser */
00723 
00724         dirname = make_absolute_path(filename);
00725 
00726         if (dirname != NULL)
00727         {
00728                 gtk_file_chooser_set_current_folder(file_chooser, dirname);
00729                 g_free(dirname);
00730         }
00731 }
00732 
00733 
00734 /**
00735  * @fn gboolean select_file_to_load(heraia_window_t *main_window)
00736  *  This function does open a file selector dialog box and returns the selected
00737  *  filename. @todo enable multiple selections
00738  *  We do fill the main_window->filename parameter here !
00739  * @param main_window : main structure
00740  * @return returns TRUE if everything went ok, FALSE otherwise
00741  */
00742 gchar *select_file_to_load(heraia_window_t *main_window)
00743 {
00744         GtkWidget *parent = NULL; /* A parent window (we use main_window)      */
00745         GtkFileChooser *file_chooser = NULL;
00746         gchar *filename = NULL;   /* filename selected (if any) to be openned  */
00747 
00748         parent = heraia_get_widget(main_window->xmls->main, "main_window");
00749 
00750         file_chooser = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new("Select a file to analyse",
00751                                                                                                                                 GTK_WINDOW(parent),
00752                                                                                                                                 GTK_FILE_CHOOSER_ACTION_OPEN,
00753                                                                                                                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00754                                                                                                                                 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
00755                                                                                                                                 NULL));
00756 
00757         /**
00758          *  for the moment we do not want to retrieve multiples selections
00759          *  but this could be a valuable thing in the future
00760          */
00761         gtk_window_set_modal(GTK_WINDOW(file_chooser), TRUE);
00762         gtk_file_chooser_set_select_multiple(file_chooser, FALSE);
00763 
00764         /**
00765          *  We want the file selection path to be the one of the previous
00766          *  openned file if any !
00767          */
00768         if (doc_t_document_get_filename(main_window->current_doc) != NULL)
00769            {
00770                         set_the_working_directory(file_chooser, doc_t_document_get_filename(main_window->current_doc));
00771            }
00772 
00773         switch (gtk_dialog_run(GTK_DIALOG(file_chooser)))
00774           {
00775                 case GTK_RESPONSE_OK:
00776                         filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
00777                         log_message(main_window, G_LOG_LEVEL_DEBUG, "filename selected : %s", filename);
00778 
00779                         /* this should be managed with lists and not here !!
00780                         if (main_window->filename != NULL)
00781                        {
00782                                         g_free(main_window->filename);
00783                        }
00784                         */
00785 
00786                         gtk_widget_destroy(GTK_WIDGET(file_chooser));
00787                         return filename;
00788                  break;
00789 
00790                 case GTK_RESPONSE_CANCEL:
00791                 default:
00792                         gtk_widget_destroy(GTK_WIDGET(file_chooser));
00793                         return NULL;
00794                  break;
00795            }
00796 }
00797 
00798 /**
00799  * @fn gchar *select_a_file_to_save(heraia_window_t *main_window)
00800  *  This function opens a dialog box that allow one to choose a
00801  *  file name to the file which is about to be saved
00802  * @param main_window : main structure
00803  * @return returns complete filename (path and filename)
00804  */
00805 gchar *select_a_file_to_save(heraia_window_t *main_window)
00806 {
00807         GtkWidget *parent = NULL;     /* A parent window (we use main_window) */
00808         GtkFileChooser *fcd = NULL;
00809         gchar *filename = NULL;
00810 
00811         parent = heraia_get_widget(main_window->xmls->main, "main_window");
00812 
00813         /* Selection a name to the file to save */
00814         fcd = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new("Save As...",
00815                                                                                                            GTK_WINDOW(parent),
00816                                                                                                            GTK_FILE_CHOOSER_ACTION_SAVE,
00817                                                                                                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00818                                                                                                            GTK_STOCK_SAVE, GTK_RESPONSE_OK,
00819                                                                                                            NULL));
00820 
00821         /* window properties : modal, without multi-selection and with confirmation */
00822         gtk_window_set_modal(GTK_WINDOW(fcd), TRUE);
00823         gtk_file_chooser_set_select_multiple(fcd, FALSE);
00824         gtk_file_chooser_set_do_overwrite_confirmation(fcd, TRUE);
00825 
00826         /* we do want to have the file's directory where to save the new file */
00827         if (doc_t_document_get_filename(main_window->current_doc) != NULL)
00828            {
00829                         set_the_working_directory(fcd, doc_t_document_get_filename(main_window->current_doc));
00830            }
00831 
00832         switch(gtk_dialog_run(GTK_DIALOG(fcd)))
00833                 {
00834                 case GTK_RESPONSE_OK:
00835                         /* retrieving the filename */
00836                         filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fcd));
00837                         break;
00838                 default:
00839                         filename = NULL;
00840                         break;
00841                 }
00842 
00843         gtk_widget_destroy(GTK_WIDGET(fcd));
00844 
00845         return filename;
00846 }
00847 
00848 
00849 /**
00850  * @fn void update_main_window_name(heraia_window_t *main_window)
00851  *  Update main window heraia's name to reflect the current edited file
00852  * @param main_window : main structure
00853  */
00854 void update_main_window_name(heraia_window_t *main_window)
00855 {
00856         GtkWidget *widget = NULL;
00857         gchar *filename = NULL;
00858         gchar *whole_filename = NULL;
00859 
00860         if (main_window != NULL && main_window->current_doc != NULL)
00861            {
00862                         widget = heraia_get_widget(main_window->xmls->main, "main_window");
00863 
00864                         whole_filename = doc_t_document_get_filename(main_window->current_doc);
00865 
00866                     filename = g_filename_display_basename(whole_filename);
00867 
00868                         gtk_window_set_title(GTK_WINDOW(widget), filename);
00869            }
00870 }
00871 
00872 /**
00873  * @fn void set_notebook_tab_name(heraia_window_t *main_window)
00874  *  Sets notebook's tab's name. This function should only be called
00875  *  when a new filename was set (open and save as functions)
00876  * @param main_window : main structure
00877  */
00878 void set_notebook_tab_name(heraia_window_t *main_window)
00879 {
00880         GtkWidget *notebook = NULL; /* file notebook in main window       */
00881         GtkWidget *page = NULL;     /* Current page for the file notebook */
00882         GtkWidget *label = NULL;    /* tab's label                        */
00883         doc_t *doc = NULL;                      /* corresponding tab's document       */
00884         gchar *filename = NULL;
00885         gchar *whole_filename;
00886         gint current = 0;
00887 
00888         if (main_window != NULL && main_window->current_doc != NULL)
00889            {
00890                    notebook = heraia_get_widget(main_window->xmls->main, "file_notebook");
00891                    current = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
00892                    page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(notebook), current);
00893                    label = gtk_notebook_get_tab_label(GTK_NOTEBOOK(notebook), page);
00894 
00895                    doc = g_ptr_array_index(main_window->documents, current);
00896                    whole_filename = doc_t_document_get_filename(doc);
00897 
00898                    if (whole_filename != NULL)
00899                    {
00900                                 filename = g_filename_display_basename(whole_filename);
00901                                 gtk_label_set_text(GTK_LABEL(label), filename);
00902 
00903                                 /* gtk_widget_set_tooltip_text is available since gtk 2.12 */
00904                                 if (GTK_MINOR_VERSION >= 12)
00905                                 {
00906                                         gtk_widget_set_tooltip_text(label, g_filename_display_name(whole_filename));
00907                                 }
00908                    }
00909            }
00910 }
00911 
00912 
00913 /**
00914  * @fn init_heraia_interface(heraia_window_t *main_window)
00915  *  Here we might init some call backs and menu options
00916  *  and display the interface (main && sub-windows)
00917  *  This function should be called only once at main program's
00918  *  init time
00919  * @param main_window : main structure
00920  */
00921 void init_heraia_interface(heraia_window_t *main_window)
00922 {
00923         GtkWidget *notebook = NULL;  /* file notebook in main window */
00924 
00925         if (main_window != NULL)
00926         {
00927                 /* inits window states (shows or hide windows) */
00928                 init_window_states(main_window);
00929 
00930                 /* Notebook selection */
00931                 notebook = heraia_get_widget(main_window->xmls->main, "file_notebook");
00932                 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0);
00933 
00934                 if (main_window->current_doc != NULL)
00935                 {
00936                         gtk_widget_show(notebook);
00937                 }
00938                 else
00939                 {
00940                         /** Hide notebook and menus @todo put these lines in a specific function */
00941                         gtk_widget_set_sensitive(heraia_get_widget(main_window->xmls->main, "save"), FALSE);
00942                         gtk_widget_set_sensitive(heraia_get_widget(main_window->xmls->main, "save_as"), FALSE);
00943                         gtk_widget_hide(notebook);
00944                 }
00945 
00946                 refresh_file_labels(main_window);
00947         }
00948 }
00949 
00950 
00951 /**
00952  * @fn gboolean load_heraia_glade_xml(heraia_window_t *main_window)
00953  *  Loads the glade xml files that describes the heraia project
00954  *  tries the following paths in that order :
00955  *  - /etc/heraia/heraia.glade
00956  *  - /home/[user]/.heraia/heraia.glade
00957  *  - PWD/heraia.glade
00958  * @param main_window : main structure
00959  * @return TRUE if everything went ok, FALSE otherwise
00960  */
00961 static gboolean load_heraia_glade_xml(heraia_window_t *main_window)
00962 {
00963         gchar *filename = NULL;
00964 
00965         if (main_window != NULL && main_window->xmls != NULL)
00966         {
00967                 filename = g_strdup_printf("heraia.glade");
00968                 main_window->xmls->main = load_glade_xml_file(main_window->location_list, filename);
00969                 g_free(filename);
00970 
00971                 if (main_window->xmls->main == NULL)
00972                 {
00973                         return FALSE;
00974                 }
00975                 else
00976                 {
00977                         return TRUE;
00978                 }
00979         }
00980         else
00981         {
00982                 return FALSE;
00983         }
00984 }
00985 
00986 /**
00987  * @fn connect_cursor_moved_signal(heraia_window_t *main_window)
00988  *  Connects the signal that the cursor has moved to
00989  *  the refreshing function
00990  * @param main_window : main structure
00991  */
00992 void connect_cursor_moved_signal(heraia_window_t *main_window, GtkWidget *hex_widget)
00993 {
00994         g_signal_connect(G_OBJECT(hex_widget), "cursor_moved",
00995                                          G_CALLBACK(refresh_event_handler), main_window);
00996 }
00997 
00998 
00999 /**
01000  * @fn void heraia_ui_connect_signals(heraia_window_t *main_window)
01001  *  Connect the signals at the interface
01002  * @param main_window : main structure
01003  */
01004 static void heraia_ui_connect_signals(heraia_window_t *main_window)
01005 {
01006 
01007         /* the data interpretor menu */
01008         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "DIMenu")), "activate",
01009                                           G_CALLBACK (on_DIMenu_activate), main_window);
01010 
01011         /* Quit, file menu */
01012         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "quit")), "activate",
01013                                           G_CALLBACK (on_quit_activate), main_window);
01014 
01015         /* New, file menu */
01016         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "new")), "activate",
01017                                           G_CALLBACK (on_new_activate), main_window);
01018 
01019         /* Open, file menu */
01020         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "open")), "activate",
01021                                           G_CALLBACK (on_open_activate), main_window);
01022 
01023         /* Save, file menu */
01024         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "save")), "activate",
01025                                           G_CALLBACK (on_save_activate), main_window);
01026 
01027         /* Save As, file menu */
01028         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "save_as")), "activate",
01029                                           G_CALLBACK (on_save_as_activate), main_window);
01030 
01031         /* Preferences, file menu ; See main_pref_window.c for main_pref_window's signals */
01032         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "preferences")), "activate",
01033                                           G_CALLBACK (on_preferences_activate), main_window);
01034 
01035         /* Cut, edit menu */
01036         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "cut")), "activate",
01037                                           G_CALLBACK (on_cut_activate), main_window);
01038 
01039         /* Copy, edit menu */
01040         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "copy")), "activate",
01041                                           G_CALLBACK (on_copy_activate), main_window);
01042 
01043         /* Paste, edit menu */
01044         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "paste")), "activate",
01045                                           G_CALLBACK (on_paste_activate), main_window);
01046 
01047         /* Delete, edit menu */
01048         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "delete")), "activate",
01049                                           G_CALLBACK (on_delete_activate), main_window);
01050 
01051 
01052         /* about dialog box */
01053         g_signal_connect (G_OBJECT(heraia_get_widget(main_window->xmls->main, "a_propos")), "activate",
01054                                           G_CALLBACK(a_propos_activate), main_window);
01055 
01056         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "about_dialog")), "close",
01057                                          G_CALLBACK(a_propos_close), main_window);
01058 
01059         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "about_dialog")), "response",
01060                                          G_CALLBACK(a_propos_response), main_window);
01061 
01062         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "about_dialog")), "delete-event",
01063                                          G_CALLBACK(a_propos_delete), main_window);
01064 
01065         /* main notebook */
01066         g_signal_connect(G_OBJECT(heraia_get_widget(main_window->xmls->main, "file_notebook")),"switch-page",
01067                                          G_CALLBACK(file_notebook_tab_changed), main_window);
01068 
01069 
01070         /* main window killed or destroyed */
01071         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "main_window")), "delete-event",
01072                                           G_CALLBACK (delete_main_window_event), main_window);
01073 
01074         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "main_window")), "response",
01075                                           G_CALLBACK (delete_main_window_event), main_window);
01076 
01077         g_signal_connect (G_OBJECT (heraia_get_widget(main_window->xmls->main, "main_window")), "close",
01078                                           G_CALLBACK (on_quit_activate), main_window);
01079 }
01080 
01081 /** @fn int load_heraia_ui(heraia_window_t *main_window)
01082  *  Loads, if possible, the glade xml file and then connects the
01083  *  signals and inits the following windows :
01084  *  - log window
01085  *  - data_interpretor window
01086  *  - list data types
01087  * @param main_window : main structure
01088  * @return TRUE if load_heraia_glade suceeded, FALSE otherwise
01089  * @todo add more return values to init functions to detect any error while
01090  *       initializing the ui
01091  */
01092 int load_heraia_ui(heraia_window_t *main_window)
01093 {
01094         gboolean success = FALSE;
01095 
01096         /* load the XML interfaces (main & treatment) */
01097         success = load_heraia_glade_xml(main_window);
01098 
01099         if (success == TRUE)
01100         {
01101                 /* Heraia UI signals */
01102                 if (main_window->debug == TRUE)
01103                 {
01104                         fprintf(stdout, "Connecting heraia_ui signals     ");
01105                 }
01106 
01107                 heraia_ui_connect_signals(main_window);
01108 
01109                 if (main_window->debug == TRUE)
01110                 {
01111                         fprintf(stdout, " [Done]\n");
01112                 }
01113 
01114                 /* The Log window */
01115                 if (main_window->debug == TRUE)
01116                 {
01117                         fprintf(stdout, "log window init interface        ");
01118                 }
01119 
01120                 log_window_init_interface(main_window);
01121 
01122                 if (main_window->debug == TRUE)
01123                 {
01124                         fprintf(stdout, " [Done]\n");
01125                 }
01126 
01127                 /* Preferences window */
01128                 if (main_window->debug == TRUE)
01129                 {
01130                         fprintf(stdout, "preferences window init interface");
01131                 }
01132 
01133                 main_pref_window_init_interface(main_window);
01134 
01135                 if (main_window->debug == TRUE)
01136                 {
01137                         fprintf(stdout, " [Done]\n");
01138                 }
01139 
01140 
01141                 /* The data interpretor window */
01142                 if (main_window->debug == TRUE)
01143                 {
01144                         fprintf(stdout, "data interpretor init interface  ");
01145                 }
01146 
01147                 data_interpretor_init_interface(main_window);
01148 
01149                 if (main_window->debug == TRUE)
01150                 {
01151                         fprintf(stdout, " [Done]\n");
01152                 }
01153 
01154 
01155                 /* The list data types window */
01156                 if (main_window->debug == TRUE)
01157                 {
01158                         fprintf(stdout, "list data types init interface   ");
01159                 }
01160 
01161                 list_data_types_init_interface(main_window);
01162 
01163                 if (main_window->debug == TRUE)
01164                 {
01165                         fprintf(stdout, " [Done]\n");
01166                 }
01167 
01168 
01169                 /* The data type window (create or edit one type) */
01170                 if (main_window->debug == TRUE)
01171                 {
01172                         fprintf(stdout, "data type init interface         ");
01173                 }
01174 
01175                 data_type_init_interface(main_window);
01176 
01177                 if (main_window->debug == TRUE)
01178                 {
01179                         fprintf(stdout, " [Done]\n");
01180                 }
01181 
01182                 fprintf(stdout, "Loading heraia preference file   ");
01183 
01184                 if (load_preference_file(main_window) != TRUE)
01185                 {
01186                         fprintf(stdout, " [FAILED]\n");
01187                 }
01188                 else /* Setting up preferences */
01189                 {
01190                         fprintf(stdout, " [Done]\n");
01191                         fprintf(stdout, "Setting up preferences           ");
01192                         load_preferences(main_window);
01193                         fprintf(stdout, " [Done]\n");
01194                 }
01195         }
01196         return success;
01197 }
01198 
01199 
01200 /**
01201  * @fn void add_text_to_textview(GtkTextView *textview, const char *format, ...)
01202  *  adds a text to a textview
01203  * @param textview : the textview where to add text
01204  * @param format : printf style format
01205  * @param ... : a va_list arguments to fit format (as with printf)
01206  */
01207 void add_text_to_textview(GtkTextView *textview, const char *format, ...)
01208 {
01209         va_list args;
01210         GtkTextBuffer *tb = NULL;
01211         GtkTextIter iEnd;
01212         gchar *display = NULL;
01213         GError *err = NULL;
01214 
01215         va_start(args, format);
01216         display = g_locale_to_utf8(g_strdup_vprintf(format, args), -1, NULL, NULL, &err);
01217         va_end(args);
01218 
01219         tb = GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview)));
01220         gtk_text_buffer_get_end_iter(tb, &iEnd);
01221         gtk_text_buffer_insert(tb, &iEnd, display, -1);
01222         g_free(display);
01223 }
01224 
01225 
01226 /**
01227  * @fn kill_text_from_textview(GtkTextView *textview)
01228  *  Kills the text from a textview
01229  * @param textview : the textview to kill the text from
01230  */
01231 void kill_text_from_textview(GtkTextView *textview)
01232 {
01233         GtkTextBuffer *tb = NULL;
01234         GtkTextIter iStart;
01235         GtkTextIter iEnd;
01236 
01237         tb = GTK_TEXT_BUFFER(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview)));
01238         gtk_text_buffer_get_start_iter(tb, &iStart);
01239         gtk_text_buffer_get_end_iter(tb, &iEnd);
01240         gtk_text_buffer_delete (tb, &iStart, &iEnd);
01241 }
01242 
01243 
01244 /**
01245  * @fn GtkWidget *gtk_radio_button_get_active(GSList *group)
01246  *  Try to find the active radio button widget in a group
01247  *  This does not take into account inconsistant states
01248  *  returns the first active radio button otherwise NULL
01249  * @param group : A group of GtkRadioButtons
01250  * @return returns the active widget if any (NULL if none)
01251  */
01252 GtkWidget *gtk_radio_button_get_active(GSList *group)
01253 {
01254         GSList *tmp_slist = group;
01255 
01256         while (tmp_slist)
01257         {
01258                 if (GTK_TOGGLE_BUTTON (tmp_slist->data)->active)
01259                 {
01260                         return GTK_WIDGET (tmp_slist->data);
01261                 }
01262                 tmp_slist = tmp_slist->next;
01263         }
01264 
01265         return NULL;
01266 }
01267 
01268 
01269 /**
01270  * @fn GtkWidget *gtk_radio_button_get_active_from_widget(GtkRadioButton *radio_group_member)
01271  * gets the active radio button from a radio group
01272  * @param radio_group_member : widget to get radio group from
01273  * @returns the active GtkRadioButton within the group from
01274  *          radio_group_member
01275  **/
01276 GtkWidget *gtk_radio_button_get_active_from_widget(GtkRadioButton *radio_group_member)
01277 {
01278         if (radio_group_member)
01279         {
01280                 return gtk_radio_button_get_active(radio_group_member->group);
01281         }
01282         else
01283         {
01284                 return NULL;
01285         }
01286 }
01287 
01288 
01289 /**
01290  * @fn gboolean is_cmi_checked(GtkWidget *check_menu_item)
01291  *  Tells whether a GtkCheckMenuItem is Checked or not
01292  * @param check_menu_item : a GtkCheckMenuItem to verify
01293  * @return returns TRUE if the Check Manu Item is checked, FALSE otherwise
01294  */
01295 gboolean is_cmi_checked(GtkWidget *check_menu_item)
01296 {
01297         return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(check_menu_item));
01298 }
01299 
01300 
01301 /**
01302  * @fn gboolean is_toggle_button_activated(GladeXML *main_xml, gchar *check_button)
01303  *  returns the state of a named check button contained
01304  *  in the Glade XML description
01305  * @param main_xml : a GladeXML definition
01306  * @param check_button : the name of an existing check_button within the glade
01307  *        definition
01308  * @return TRUE if the button is activated / toggled , FALSE otherwise
01309  */
01310 gboolean is_toggle_button_activated(GladeXML *main_xml, gchar *check_button)
01311 {
01312         gboolean activated = FALSE;
01313 
01314         if (main_xml != NULL)
01315         {
01316                 activated = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(heraia_get_widget(main_xml, check_button)));
01317         }
01318 
01319         return activated;
01320 }
01321 
01322 
01323 /**
01324  * @fn GtkWidget *heraia_get_widget(GladeXML *xml, gchar *widget_name)
01325  *  This is a wrapper to the glade xml get widget. It is intended
01326  *  to simplify the developpers lives if they have to choose or
01327  *  propose other means to do the same thing than libglade (say,
01328  *  for example, GtkBuilder :)
01329  * @param xml : A glade XML definition
01330  * @param widget_name : an existing widget name in the glade definition
01331  * @return returns the widget itself if it exists in the definition file (NULL
01332  *         otherwise)
01333  */
01334 GtkWidget *heraia_get_widget(GladeXML *xml, gchar *widget_name)
01335 {
01336    /**
01337         * For debug purposes only (very verbose as this function is the main used)
01338         * fprintf(stdout, "Getting Widget named %s\n", widget_name);
01339         */
01340 
01341         if (xml != NULL && widget_name != NULL)
01342         {
01343                 return glade_xml_get_widget(xml, widget_name);
01344         }
01345         else
01346         {
01347                 return NULL;
01348         }
01349 }
01350 
01351 
01352 /**
01353  * @fn void destroy_a_single_widget(GtkWidget *widget)
01354  *  Destroys a single widget if it exists
01355  * @param widget : the widget to destroy
01356  */
01357 void destroy_a_single_widget(GtkWidget *widget)
01358 {
01359         if (widget != NULL)
01360         {
01361                 gtk_widget_destroy(widget);
01362         }
01363 }
01364 
01365 /**
01366  * @fn void close_heraia(heraia_window_t *main_window)
01367  * Before closing heraia we need to do few things
01368  * @param main_window : main_struct
01369  */
01370 static void close_heraia(heraia_window_t *main_window)
01371 {
01372         /* recording window's position */
01373         record_all_dialog_box_positions(main_window);
01374 
01375         /* . Saving preferences */
01376         save_preferences(main_window);
01377 }
01378 
01379 /**
01380  * @fn void init_one_cmi_window_state(GtkWidget *dialog_box, GtkWidget *cmi, window_prop_t *dialog_prop)
01381  * init one cmi window based state
01382  * @param dialog_box : the window or dialog box we want to init its state
01383  * @param cmi : corresponding check menu item
01384  * @param dialog_prop : corresponding window properties (should be initialized and not NULL)
01385  */
01386 static void init_one_cmi_window_state(GtkWidget *dialog_box, GtkWidget *cmi, window_prop_t *dialog_prop)
01387 {
01388         gboolean activated = FALSE;
01389 
01390         if (dialog_box != NULL && cmi != NULL && dialog_prop != NULL)
01391         {
01392                 activated = dialog_prop->displayed;
01393                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(cmi), activated);
01394                 if (activated == TRUE)
01395                 {
01396                         gtk_window_move(GTK_WINDOW(dialog_box), dialog_prop->x, dialog_prop->y);
01397                         gtk_window_resize(GTK_WINDOW(dialog_box), dialog_prop->width, dialog_prop->height);
01398                         gtk_widget_show_all(dialog_box);
01399                 }
01400         }
01401 }
01402 
01403 
01404 /**
01405  * @fn init_window_states(heraia_window_t *main_window)
01406  *  Inits all windows states (positions, displayed, and so on...)
01407  * @param main_window : main structure
01408  */
01409 void init_window_states(heraia_window_t *main_window)
01410 {
01411         GtkWidget *cmi = NULL;
01412         GtkWidget *dialog_box = NULL;
01413 
01414         if (main_window != NULL && main_window->xmls != NULL  && main_window->xmls->main != NULL)
01415         {
01416                 if (main_window->win_prop)
01417                 {
01418                         /* Main window (always the first one) */
01419                         dialog_box = heraia_get_widget(main_window->xmls->main, "main_window");
01420                         if (main_window->win_prop->main_dialog->displayed == TRUE)
01421                         {
01422                                 gtk_window_move(GTK_WINDOW(dialog_box), main_window->win_prop->main_dialog->x, main_window->win_prop->main_dialog->y);
01423                                 gtk_window_resize(GTK_WINDOW(dialog_box), main_window->win_prop->main_dialog->width, main_window->win_prop->main_dialog->height);
01424                                 gtk_widget_show(dialog_box);
01425                         }
01426 
01427                         /* Log Window Interface */
01428                         cmi = heraia_get_widget(main_window->xmls->main, "mw_cmi_show_logw");
01429                         dialog_box = heraia_get_widget(main_window->xmls->main, "log_window");
01430                         init_one_cmi_window_state(dialog_box, cmi, main_window->win_prop->log_box);
01431 
01432                         /* Data Interpretor Interface */
01433                         cmi = heraia_get_widget(main_window->xmls->main, "DIMenu");
01434                         /* Emit the specific signal to activate the check_menu_item */
01435                         if (main_window->win_prop->data_interpretor->displayed == TRUE)
01436                         {
01437                                 main_window->win_prop->data_interpretor->displayed = FALSE; /* dirty trick */
01438                                 g_signal_emit_by_name(heraia_get_widget(main_window->xmls->main, "DIMenu"), "activate");
01439                         }
01440 
01441                         /* List Data type Interface */
01442                         cmi = heraia_get_widget(main_window->xmls->main, "ldt_menu");
01443                         dialog_box = heraia_get_widget(main_window->xmls->main, "list_data_types_window");
01444                         init_one_cmi_window_state(dialog_box, cmi, main_window->win_prop->ldt);
01445 
01446                         /* Plugin List Interface */
01447                         cmi = heraia_get_widget(main_window->xmls->main, "mw_cmi_plugin_list");
01448                         dialog_box = heraia_get_widget(main_window->xmls->main, "plugin_list_window");
01449                         init_one_cmi_window_state(dialog_box, cmi, main_window->win_prop->plugin_list);
01450 
01451                         /* Preferences window */
01452                         dialog_box = heraia_get_widget(main_window->xmls->main, "main_preferences_window");
01453                         if (main_window->win_prop->main_pref_window->displayed == TRUE)
01454                         {
01455                                 /* main_window->win_prop->main_pref_window->displayed = FALSE; dirty trick */
01456                                 gtk_window_move(GTK_WINDOW(dialog_box), main_window->win_prop->main_pref_window->x, main_window->win_prop->main_pref_window->y);
01457                                 gtk_window_resize(GTK_WINDOW(dialog_box), main_window->win_prop->main_pref_window->width, main_window->win_prop->main_pref_window->height);
01458                                 gtk_widget_show_all(dialog_box);
01459                         }
01460 
01461                         /* About Box */
01462                         dialog_box = heraia_get_widget(main_window->xmls->main, "about_dialog");
01463                         if (main_window->win_prop->about_box->displayed == TRUE)
01464                         {
01465                                 /* main_window->win_prop->main_pref_window->displayed = FALSE; dirty trick */
01466                                 gtk_window_move(GTK_WINDOW(dialog_box), main_window->win_prop->about_box->x, main_window->win_prop->about_box->y);
01467                                 gtk_window_resize(GTK_WINDOW(dialog_box), main_window->win_prop->about_box->width, main_window->win_prop->about_box->height);
01468                                 set_a_propos_properties(dialog_box);
01469                                 gtk_widget_show_all(dialog_box);
01470                         }
01471                 }
01472         }
01473 }
01474 
01475 /**
01476  *  Adds a new tab to the main window in file's notebook
01477  * @param main_window : main structure
01478  * @param doc : the new document that will be related to the tab
01479  */
01480 void add_new_tab_in_main_window(heraia_window_t *main_window, doc_t *doc)
01481 {
01482         GtkWidget *vbox = NULL;       /**< used for vbox creation          */
01483         GtkNotebook *notebook = NULL; /**< file_notebook from heraia.glade */
01484         GtkWidget *tab_label = NULL;  /**< tab's label                     */
01485         gint tab_num = -1;            /**< new tab's index                 */
01486 
01487         notebook = GTK_NOTEBOOK(heraia_get_widget(main_window->xmls->main, "file_notebook"));
01488         vbox = gtk_vbox_new(FALSE, 2);
01489         gtk_box_pack_start(GTK_BOX(vbox), doc->hex_widget, TRUE, TRUE, 3);
01490 
01491         tab_label = gtk_label_new(NULL);
01492 
01493         gtk_widget_show_all(vbox);
01494         tab_num = gtk_notebook_append_page(notebook, vbox, tab_label);
01495 
01496         gtk_notebook_set_current_page(notebook, tab_num);
01497         main_window->current_doc = doc;
01498 
01499         /* This may not be necessary here has it is done later
01500                 set_notebook_tab_name(main_window);
01501         */
01502 }

Generated on Tue May 19 20:01:37 2009 for Heraia by  doxygen 1.5.8