Heraia  0.1.8
goto_dialog.c
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3  goto_dialog.c
4  goto_dialog.c - everything to manage the dialog that lets one go everywhere
5  in the file
6 
7  (C) Copyright 2010 - 2011 Olivier Delhomme
8  e-mail : heraia@delhomme.org
9  URL : http://heraia.tuxfamily.org
10 
11  This program is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 2, or (at your option)
14  any later version.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 */
25 /**
26  * @file goto_dialog.c
27  * File for the goto dialog window that lets one go everywhere in the file.
28  */
29 #include <libheraia.h>
30 
31 static gboolean delete_goto_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer data);
32 static void destroy_goto_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer data);
33 static void goto_dialog_canceled(GtkWidget *widget, gpointer data);
34 static void goto_dialog_connect_signal(heraia_struct_t *main_struct);
35 
36 
37 /**
38  * Go to..., edit menu
39  * @param widget : the widget that issued the signal
40  * @param data : user data MUST be heraia_struct_t *main_struct main structure
41  */
42 void on_goto_activate(GtkWidget *widget, gpointer data)
43 {
44  heraia_struct_t *main_struct = (heraia_struct_t *) data;
45  GtkWidget *dialog = NULL; /**< dialog window itself */
46 
47  if (main_struct != NULL && main_struct->current_doc != NULL)
48  {
49  dialog = heraia_get_widget(main_struct->xmls->main, "goto_dialog");
50  show_hide_widget(dialog, TRUE, main_struct->win_prop->goto_window);
51  }
52 }
53 
54 
55 /**
56  * Inits all the things in the goto dialog window (signal and such)
57  * @param main_struct : heraia's main structure
58  */
60 {
61  if (main_struct != NULL && main_struct->xmls != NULL && main_struct->xmls->main != NULL)
62  {
63  goto_dialog_connect_signal(main_struct);
64  }
65 }
66 
67 
68 /**
69  * Call back function for the goto dialog window destruction
70  * @param widget : calling widget (may be NULL as we don't use this here)
71  * @param event : event associated (may be NULL as we don't use this here)
72  * @param data : MUST be heraia_struct_t *main_struct main structure
73  */
74 static gboolean delete_goto_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer data)
75 {
76  goto_dialog_canceled(widget, data);
77 
78  return TRUE;
79 }
80 
81 
82 /**
83  * Call back function for the goto dialog window destruction
84  * @param widget : calling widget (may be NULL as we don't use this here)
85  * @param event : event associated (may be NULL as we don't use this here)
86  * @param data : user data - not used (may be NULL)
87  */
88 static void destroy_goto_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer data)
89 {
90  goto_dialog_canceled(widget, data);
91 }
92 
93 
94 /**
95  * Closing the window effectively
96  * @param widget : calling widget
97  * @param data : MUST be heraia_struct_t *main_struct main structure and not NULL
98  */
99 static void goto_dialog_canceled(GtkWidget *widget, gpointer data)
100 {
101  heraia_struct_t *main_struct = (heraia_struct_t *) data;
102  GtkWidget *dialog = NULL; /**< dialog window itself */
103 
104  if (main_struct != NULL)
105  {
106  dialog = heraia_get_widget(main_struct->xmls->main, "goto_dialog");
107  show_hide_widget(dialog, FALSE, main_struct->win_prop->goto_window);
108  }
109 }
110 
111 
112 /**
113  * Ok button has been clicked we want to go to the byte number from the entry
114  * @param widget : calling widget
115  * @param data : MUST be heraia_struct_t *main_struct main structure and not NULL
116  */
117 static void goto_dialog_ok(GtkWidget *widget, gpointer data)
118 {
119  heraia_struct_t *main_struct = (heraia_struct_t *) data;
120  GtkWidget *radio_button = NULL; /**< a radio button from the group */
121  GtkWidget *radio_active = NULL; /**< the radio button which is active within this group */
122  const gchar *widget_name = NULL;
123  const gchar *entry_text = NULL;
124  guint64 offset = 0;
125  gboolean convert_ok = FALSE;
126 
127 
128  radio_button = heraia_get_widget(main_struct->xmls->main, "goto_from_beginning");
129 
130  if (radio_button != NULL && main_struct->current_doc != NULL)
131  {
132  entry_text = gtk_entry_get_text(GTK_ENTRY(heraia_get_widget(main_struct->xmls->main, "goto_entry")));
133 
134  if (entry_text[0] == '0' && entry_text[1] == 'x')
135  {
136  convert_ok = (sscanf(entry_text, "%llx", (long long unsigned int *) &offset) == 1);
137  }
138  else
139  {
140  convert_ok = (sscanf(entry_text, "%llu", (long long unsigned int *) &offset) == 1);
141  }
142 
143  if (convert_ok == TRUE)
144  {
145 
146  radio_active = gtk_radio_button_get_active_from_widget(GTK_RADIO_BUTTON(radio_button));
147 
148  if (radio_active != NULL)
149  {
150  widget_name = gtk_buildable_get_name(GTK_BUILDABLE(radio_active));
151  }
152 
153  /* guessing which radio is active (by name) */
154  if (widget_name != NULL)
155  {
156  if (g_ascii_strcasecmp(widget_name, "goto_from_beginning") == 0)
157  {
158  ghex_set_cursor_position(main_struct->current_doc->hex_widget, offset - 1);
159  }
160  else if (g_ascii_strcasecmp(widget_name, "goto_from_here_fwd") == 0)
161  {
162  offset += ghex_get_cursor_position(main_struct->current_doc->hex_widget);
163  ghex_set_cursor_position(main_struct->current_doc->hex_widget, offset);
164  }
165  else if (g_ascii_strcasecmp(widget_name, "goto_from_here_rwd") == 0)
166  {
167  offset = ghex_get_cursor_position(main_struct->current_doc->hex_widget) - offset ;
168  ghex_set_cursor_position(main_struct->current_doc->hex_widget, offset);
169  }
170  else
171  {
172  offset = ghex_file_size(GTK_HEX(main_struct->current_doc->hex_widget)) - offset;
173  ghex_set_cursor_position(main_struct->current_doc->hex_widget, offset);
174  }
175  }
176  }
177  }
178 }
179 
180 
181 
182 /**
183  * Signal connections for the goto dialog window
184  * @param main_struct : heraia's main structure
185  */
187 {
188  /* Cancel button */
189  g_signal_connect(G_OBJECT(heraia_get_widget(main_struct->xmls->main, "gtd_cancel_bt")), "clicked",
190  G_CALLBACK(goto_dialog_canceled), main_struct);
191 
192  /* Ok button */
193  g_signal_connect(G_OBJECT(heraia_get_widget(main_struct->xmls->main, "gtd_ok_bt")), "clicked",
194  G_CALLBACK(goto_dialog_ok), main_struct);
195 
196 
197  /* When goto dialog's window is killed or destroyed */
198  g_signal_connect(G_OBJECT(heraia_get_widget(main_struct->xmls->main, "goto_dialog")), "delete_event",
199  G_CALLBACK(delete_goto_dialog_event), main_struct);
200 
201  g_signal_connect(G_OBJECT(heraia_get_widget(main_struct->xmls->main, "goto_dialog")), "destroy",
202  G_CALLBACK(destroy_goto_dialog_event), main_struct);
203 }
This is the main structure.
Definition: libheraia.h:332
static void goto_dialog_ok(GtkWidget *widget, gpointer data)
Ok button has been clicked we want to go to the byte number from the entry.
Definition: goto_dialog.c:117
void show_hide_widget(GtkWidget *widget, gboolean show, window_prop_t *win_prop)
To help plugins to deal with widgets, shows or hide a specific widget.
Definition: heraia_ui.c:2613
all_window_prop_t * win_prop
Keeps window properties.
Definition: libheraia.h:342
guint64 ghex_file_size(Heraia_Hex *gh)
Returns the file size of an opened Heraia_Hex document.
static void destroy_goto_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer data)
Call back function for the goto dialog window destruction.
Definition: goto_dialog.c:88
xml_t * xmls
All the xmls used in the program, loaded at running time.
Definition: libheraia.h:337
static void goto_dialog_canceled(GtkWidget *widget, gpointer data)
Closing the window effectively.
Definition: goto_dialog.c:99
static void goto_dialog_connect_signal(heraia_struct_t *main_struct)
Signal connections for the goto dialog window.
Definition: goto_dialog.c:186
void goto_dialog_init_interface(heraia_struct_t *main_struct)
Inits all the things in the goto dialog window (signal and such)
Definition: goto_dialog.c:59
GtkWidget * hex_widget
hexwidget corresponding to the document
Definition: libheraia.h:296
GtkBuilder * main
the main interface xml description
Definition: libheraia.h:222
guint64 ghex_get_cursor_position(GtkWidget *hex_widget)
Retrieves the cursor's position from the current hexwidget.
This file contains all the definitions and includes all other .h files.
void ghex_set_cursor_position(GtkWidget *hex_widget, guint64 position)
Sets the cursor at the defined position in the hexwidget.
void on_goto_activate(GtkWidget *widget, gpointer data)
Go to..., edit menu.
Definition: goto_dialog.c:42
static gboolean delete_goto_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer data)
Call back function for the goto dialog window destruction.
Definition: goto_dialog.c:74
doc_t * current_doc
This is a pointer to the current edited document.
Definition: libheraia.h:335
GtkWidget * heraia_get_widget(GtkBuilder *xml, gchar *widget_name)
This is a wrapper to the GtkBuilder xml get widget.
Definition: heraia_ui.c:2184
window_prop_t * goto_window
goto dialog window
Definition: libheraia.h:268
GtkWidget * gtk_radio_button_get_active_from_widget(GtkRadioButton *radio_button)
gets the active radio button from a radio group
Definition: heraia_ui.c:2098