2 * Declarations of GTK+-specific UI utility routines
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #ifndef __GUI_UTILS_H__
26 #define __GUI_UTILS_H__
28 /** @defgroup windows_group Windows
30 * There are the following toplevel windows:
32 * - @ref main_window_group
33 * - Statistic Windows (several different statistic windows)
35 * See: @ref howto_window_page for details.
39 /** @page howto_window_page How to develop a window / dialog
41 * Windows and dialogs are related to each other. Dialogs are special kind of windows, but they behave
42 * slightly different. A dialog sticks on its parent window; A normal window will be much more independent
43 * from its parent window. Dialogs should be used to ask or tell the user something, while windows should
44 * show data which is independent of the main window.
45 * Dialogs are created by calling dlg_window_new() which in turn will call window_new().
46 * After that, dialogs can be developed the same way as windows; all window related functions in gui_utils.h
47 * can be used for both.
49 * @section window_create Create a window
51 * A typical window / dialog will be created by the following calls:
53 * - window_new() will create a new window with default position and size,
54 * use dlg_window_new() if you need a dialog (transient to the main window)
55 * - gtk_window_set_default_size() will set the default size of the window. Only
56 * needed, if the initial size is not appropriate, e.g. when a scrolled_window_new() is used.
57 * - g_signal_connect(my_win, "destroy", my_destroy_cb, NULL) will create a callback if some cleanup
58 * needs to be done after the window is destroyed, e.g. free up memory, or set the window pointer
59 * of a singleton window (only one instance allowed, e.g. about dialog) back to zero
60 * - create and fill in the content and button widgets
61 * - gtk_widget_show_all() shows all the widgets in the window
62 * - window_present() will present the window on screen and
63 * (if available) set previously saved position and size
65 * @section window_events Events
67 * The following events are usually interesting:
69 * - "delete_event": the window manager's "X" (e.g. upper right edge) of the window
70 * was clicked; the default handler will call gtk_widget_destroy()
71 * - "destroy": everything is already gone; only cleanup of left over resources
72 * can/should be done now
74 * @section window_hints Hints
76 * If you want to save size and position, be sure to call window_destroy() instead of only
77 * gtk_widget_destroy(), so you will probably have to g_signal_connect() to the "delete_event"!
79 * Don't use gtk_widget_set_size_request() to set the size of a window;
80 * use gtk_window_set_default_size() for that purpose!
82 * Be sure to call window_present() / window_destroy() appropriately, if you
83 * want to have size and position of the window handled by ui_util.
88 * Utilities for Windows and other user interface functions. See: @ref howto_window_page for details.
89 * @ingroup dialog_group
90 * @ingroup windows_group
93 /** @name Window Functions
94 * @todo Move these window functions to a new file win_utils.h?
97 /** Create a new window with the Wireshark icon.
98 * If you want to create a dialog, use dlg_window_new() instead.
100 * @param type window type, typical GTK_WINDOW_TOPLEVEL
101 * @param title the title for the new window
102 * @return the newly created window
104 extern GtkWidget *window_new(GtkWindowType type, const gchar *title);
106 /** Same as window_new(), but will keep its geometry values (size, position, ...).
107 * Be sure to use window_present() and window_destroy() appropriately!
109 * @param type window type, typical GTK_WINDOW_TOPLEVEL
110 * @param title the title for the new window
111 * @param geom_name the name to distinguish this window; will also be used for the recent file (don't use special chars)
112 * @return the newly created window
114 extern GtkWidget *window_new_with_geom(GtkWindowType type, const gchar *title, const gchar *geom_name);
116 /** Create a new splash window, with no icon or title bar.
118 * @return the newly created window
120 extern GtkWidget *splash_window_new(void);
122 /** Present the created window on the top of the screen. This will put the window on top and
123 * (if available) set previously saved position and size.
125 * @param win the window from window_new()
127 extern void window_present(GtkWidget *win);
129 /** callback function for window_set_cancel_button() */
130 typedef void (*window_cancel_button_fct) (GtkWidget *w, gpointer data);
132 /** Register the default cancel button "Cancel"/"Close"/"Ok" of this window.
133 * This will set the callback function for this button, grab this button as the default one and
134 * set the "ESC" key handler to call the callback function if key is pressed.
136 * @param win the window from window_new()
137 * @param bt the default button of this window
138 * @param cb callback function to be called, when this button is pressed
140 extern void window_set_cancel_button(GtkWidget *win, GtkWidget *bt, window_cancel_button_fct cb);
142 /** Remember the current window position / size and then destroy the window.
143 * It's important to call this instead of gtk_widget_destroy() when using window_new_with_geom().
145 * @param win the window from window_new()
147 extern void window_destroy(GtkWidget *win);
149 /** Default callback handler for cancel button "clicked" signal.
150 * Use this for window_set_cancel_button(), if no user specific functionality required,
151 * will simply call window_destroy()
153 extern void window_cancel_button_cb(GtkWidget *w _U_, gpointer data);
155 /** Default callback handler if the window manager's X of the window was clicked (delete_event).
156 * Use this for g_signal_connect(), if no user specific functionality required,
157 * will simply call window_destroy()
159 extern gboolean window_delete_event_cb(GtkWidget *win, GdkEvent *event _U_, gpointer user_data _U_);
161 /** geometry values for use in window_get_geometry() and window_set_geometry() */
162 typedef struct window_geometry_s {
163 gchar *key; /**< current key in hashtable (internally used only) */
164 gboolean set_pos; /**< set the x and y position values */
165 gint x; /**< the windows x position */
166 gint y; /**< the windows y position */
167 gboolean set_size; /**< set the width and height values */
168 gint width; /**< the windows width */
169 gint height; /**< the windows height */
171 gboolean set_maximized; /**< set the maximized state (GTK2 only) */
172 gboolean maximized; /**< the windows maximized state (GTK2 only) */
175 /** Get the geometry of a window.
177 * @param win the window from window_new()
178 * @param geom the current geometry values of the window; the set_xy values will not be used
179 * @todo if main uses the window_new_with_geom() to save size and such, make this function static
181 extern void window_get_geometry(GtkWidget *win, window_geometry_t *geom);
182 /** Set the geometry of a window.
184 * @param win the window from window_new()
185 * @param geom the new geometry values of the window
186 * @todo if main uses the window_new_with_geom() to save size and such, make this function static
188 extern void window_set_geometry(GtkWidget *win, window_geometry_t *geom);
190 /** Write all geometry values of all windows to the recent file.
191 * Will call write_recent_geom() for every existing window type.
193 * @param rf recent file handle from caller
195 extern void window_geom_recent_write_all(gpointer rf);
197 /** Read in a single geometry key value pair from the recent file.
199 * @param name the geom_name of the window
200 * @param key the subkey of this pair (e.g. "x")
201 * @param value the new value (e.g. "123")
203 extern void window_geom_recent_read_pair(const char *name, const char *key, const char *value);
205 /** Raise a top-level window and de-iconify it.
206 * This routine is used if the user has done something to
207 * ask that a window of a certain type be popped up when there can be only
208 * one such window and such a window has already been popped up - we
209 * pop up the existing one rather than creating a new one.
211 * @param win the window from window_new() to be reactivated
213 extern void reactivate_window(GtkWidget *win);
217 /** Create a GtkScrolledWindow, set its scrollbar placement appropriately,
220 * @param hadjustment horizontal adjustment
221 * @param vadjustment vertical adjustment
222 * @return the new scrolled window
224 extern GtkWidget *scrolled_window_new(GtkAdjustment *hadjustment,
225 GtkAdjustment *vadjustment);
227 /** Set the scrollbar placement of all scrolled windows based on user
229 extern void set_scrollbar_placement_all(void);
231 /** Create a GtkTreeView, give it the right styles, and remember it.
233 * @param model The model (the data) of this tree view.
235 extern GtkWidget *tree_view_new(GtkTreeModel *model);
237 /** Move the currently-selected item in a list store up or down one position.
239 * @param tree GtkTreeView using a GtkListStore.
240 * @param move_up TRUE to move the selected item up or FALSE to move it down.
241 * @return TRUE if successful, FALSE otherwise.
243 extern gboolean tree_view_list_store_move_selection(GtkTreeView *tree, gboolean move_up);
245 /** Find the selected row in a list store.
247 * @param tree GtkTreeView using a GtkListStore.
248 * @return The selected row number or -1 if no row is selected.
250 extern gint tree_view_list_store_get_selected_row(GtkTreeView *tree);
252 /** Create a simple list widget.
254 * @param cols number of columns
255 * @param titles the titles of all columns
256 * @return the new simple list widget
258 extern GtkWidget *simple_list_new(gint cols, const gchar **titles);
259 /** Append a row to the simple list.
261 * @param list the list from simple_list_new()
262 * @param ... row and title, finished by -1 (e.g.: 0, "first", 1, "second", -1).
264 extern void simple_list_append(GtkWidget *list, ...);
266 /*** Make a column look like a url
268 * @param list the list from simple_list_new()
269 * @param col the column to make the values lookk like urls
271 extern void simple_list_url_col(GtkWidget *list, gint col);
273 /*** Make a cell underline to look like links
275 * @param cell the cell renderer that will show the text as a link
278 extern void render_as_url(GtkCellRenderer *cell);
280 /** Set the styles of all Trees based upon user preferences. */
281 extern void set_tree_styles_all(void);
283 /** Convert an xpm picture into a GtkWidget showing it.
284 * Beware: Wireshark's main window must already be visible!
286 * @param xpm the character array containing the picture
287 * @return a newly created GtkWidget showing the picture
289 extern GtkWidget *xpm_to_widget(const char ** xpm);
291 /** Convert an xpm picture into a GtkWidget showing it.
292 * Beware: the given parent window must already be visible!
294 * @param parent the parent window of to widget to be generated
295 * @param xpm the character array containing the picture
296 * @return a newly created GtkWidget showing the picture
298 /*extern GtkWidget *xpm_to_widget_from_parent(GtkWidget *parent, const char ** xpm);*/
300 /** Convert an pixbuf data to a GtkWidget
302 * @param pb_data Inline pixbuf data. This should be created with "gdk-pixbuf-csource --raw"
304 extern GtkWidget *pixbuf_to_widget(const char * pb_data);
306 /** Copy a GString to the clipboard.
308 * @param str GString that is to be copied to the clipboard.
310 extern void copy_to_clipboard(GString *str);
312 /** Copy an array of bytes to the clipboard.
313 * Copies as mime-type application/octet_stream in GTK 2.
315 * @param data_p Pointer to data to be copied.
316 * @param len Number of bytes in the data to be copied.
318 extern void copy_binary_to_clipboard(const guint8* data_p, int len);
320 /** Create a new window title that includes user-defined preference string.
322 * @param caption string you want included in title (appended to user-defined string)
323 * @return a newly created title string including user-defined preference (if specified)
325 extern gchar *create_user_window_title(const gchar *caption);
327 /** Construct the main window's title with the current main_window_name optionally appended
328 * with the user-specified title and/or wireshark version.
329 * Display the result in the main window's title bar and in its icon title
331 extern void update_main_window_title(void);
333 /** Renders a float with two decimals precission, called from gtk_tree_view_column_set_cell_data_func().
334 * the user data must be the colum number.
335 * Present floats with two decimals
337 * @param column A GtkTreeColumn
338 * @param renderer The GtkCellRenderer that is being rendered by tree_column
339 * @param model The GtkTreeModel being rendered
340 * @param iter A GtkTreeIter of the current row rendered
341 * @param user_data must be the colum number to fetch the data from
343 void float_data_func (GtkTreeViewColumn *column, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data);
345 /** Renders a unsinged integer as a hexadecimal value, called from gtk_tree_view_column_set_cell_data_func()
346 * The user data must be the colum number.
347 * Present value as hexadecimal.
348 * @param column A GtkTreeColumn
349 * @param renderer The GtkCellRenderer that is being rendered by tree_column
350 * @param model The GtkTreeModel being rendered
351 * @param iter A GtkTreeIter of the current row rendered
352 * @param user_data must be the colum number to fetch the data from
354 void present_as_hex_func (GtkTreeViewColumn *column, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data);
356 /** Renders an unsigned 64 bits integer with space as thousand separator, called from gtk_tree_view_column_set_cell_data_func()
357 * The user data must be the colum number.
358 * Present value as hexadecimal.
359 * @param column A GtkTreeColumn
360 * @param renderer The GtkCellRenderer that is being rendered by tree_column
361 * @param model The GtkTreeModel being rendered
362 * @param iter A GtkTreeIter of the current row rendered
363 * @param user_data must be the colum number to fetch the data from
365 void u64_data_func (GtkTreeViewColumn *column, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data);
367 /** This function can be called from gtk_tree_view_column_set_cell_data_func()
368 * the user data must be the colum number.
369 * Present value as hexadecimal.
370 * @param column A GtkTreeColumn
371 * @param renderer The GtkCellRenderer that is being rendered by tree_column
372 * @param model The GtkTreeModel being rendered
373 * @param iter A GtkTreeIter of the current row rendered
374 * @param user_data must be the colum number to fetch the data from
376 void str_ptr_data_func(GtkTreeViewColumn *column, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data);
378 /** This function can be called from gtk_tree_sortable_set_sort_func()
379 * the user data must be the colum number.
380 * Used together with str_ptr_data_func to sort the corresponding column.
381 * @param model The GtkTreeModel the comparison is within
382 * @param a A GtkTreeIter in model
383 * @param b Another GtkTreeIter in model
384 * @param user_data must be the colum number to fetch the data from
387 gint str_ptr_sort_func(GtkTreeModel *model,
392 /** Switch a GtkTReeView to fixed columns (speed optimization)
393 * @param view A GtkTreeView
395 void switch_to_fixed_col(GtkTreeView *view);
397 /** Return the size in pixels of a string displayed with the GtkWidget's font.
398 * @param view A GtkWidget
399 * @param str UTF8 string
401 gint get_default_col_size(GtkWidget *view, const gchar *str);
404 /** --------------------------------------------------
405 * ws_combo_box_text_and_pointer convenience functions
406 * (Code adapted from GtkComboBox.c)
410 * ws_combo_box_new_text_and_pointer_full:
412 * Convenience function which constructs a new "text and pointer" combo box, which
413 * is a #GtkComboBox just displaying strings and storing a pointer associated with
414 * each combo_box entry; The pointer can be retrieved when an entry is selected.
415 * Also: optionally returns the cell renderer for the combo box.
416 * If you use this function to create a text_and_pointer combo_box,
417 * you should only manipulate its data source with the
418 * following convenience functions:
419 * ws_combo_box_append_text_and_pointer()
420 * ws_combo_box_append_text_and_pointer_full()
422 * @param cell_p pointer to return the 'GtkCellRenderer *' for the combo box (or NULL).
423 * @return A pointer to a new text_and_pointer combo_box.
425 GtkWidget *ws_combo_box_new_text_and_pointer_full(GtkCellRenderer **cell_p);
428 * ws_combo_box_new_text_and_pointer:
430 * Convenience function which constructs a new "text and pointer" combo box, which
431 * is a #GtkComboBox just displaying strings and storing a pointer associated with
432 * each combo_box entry; The pointer can be retrieved when an entry is selected.
433 * If you use this function to create a text_and_pointer combo_box,
434 * you should only manipulate its data source with the
435 * following convenience functions:
436 * ws_combo_box_append_text_and_pointer()
437 * ws_combo_box_append_text_and_pointer_full()
439 * @return A pointer to a new text_and_pointer combo_box.
441 GtkWidget *ws_combo_box_new_text_and_pointer(void);
444 * ws_combo_box_clear_text_and_pointer:
445 * @param combo_box A #GtkComboBox constructed using ws_combo_box_new_text_and_pointer()
447 * Clears all the text_and_pointer entries in the text_and_pointer combo_box.
448 * Note: A "changed" signal will be emitted after the clear if there was
449 * an active (selected) entry before the clear.
450 * You should use this function only with combo boxes constructed with
451 * ws_combo_box_new_text_and_pointer().
453 void ws_combo_box_clear_text_and_pointer(GtkComboBox *combo_box);
456 * ws_combo_box_append_text_and_pointer_full:
457 * @param combo_box A #GtkComboBox constructed using ws_combo_box_new_text_and_pointer()
458 * @param parent_iter Parent row for apending; NULL if appending to tree top-level;
459 * @param text A string to be displayed as an entry in the dropdown list of the combo_box
460 * @param ptr A pointer to be associated with this entry of the combo_box
461 * @param sensitive TRUE/FALSE to set sensitivity of the entry
462 * @return A GtkTreeIter pointing to the appended GtkVomboBox entry.
464 * Appends text and ptr to the list of strings and pointers stored in combo_box.
465 * The text and ptr can be appended to any existing level of the tree_store.
466 * The sensitivity of the row will be set as requested.
467 * Note that you can only use this function with combo boxes constructed with
468 * ws_combo_box_new_text_and_pointer().
471 ws_combo_box_append_text_and_pointer_full(GtkComboBox *combo_box,
472 GtkTreeIter *parent_iter,
475 const gboolean sensitive);
478 * ws_combo_box_append_text_and_pointer:
479 * @param combo_box A #GtkComboBox constructed using ws_combo_box_new_text_and_pointer()
480 * @param text A string to be displayed as an entry in the dropdown list of the combo_box
481 * @param ptr A pointer to be associated with this entry of the combo_box
482 * @return A GtkTreeIter pointing to the appended GtkComboBox entry.
484 * Appends text and ptr to the list of strings and pointers stored in combo_box. Note that
485 * you can only use this function with combo boxes constructed with
486 * ws_combo_box_new_text_and_pointer().
489 ws_combo_box_append_text_and_pointer(GtkComboBox *combo_box,
494 * ws_combo_box_get_active_pointer:
495 * @param combo_box A #GtkComboBox constructed using ws_combo_box_new_text_and_pointer()
496 * @param ptr A pointer to a location in which to store the pointer associated with the active entry
497 * @return TRUE if an entry is selected (i.e: an active entry exists); FALSE otherwise
499 * You can only use this function with combo boxes constructed with
500 * ws_combo_box_new_text_and_pointer().
502 gboolean ws_combo_box_get_active_pointer(GtkComboBox *combo_box, gpointer *ptr);
505 * ws_combo_box_get_active:
506 * @param combo_box A #GtkComboBox constructed using ws_combo_box_new_text_and_pointer()
507 * @return Index of the active entry; -1 if no entry is selected;
508 * Note: If the active item is not an immediate child of root of the tree then
509 * the index returned is that of the top-level for the acftive entry.
511 gint ws_combo_box_get_active(GtkComboBox *combo_box);
514 * ws_combo_box_set_active:
515 * @param combo_box A #GtkComboBox constructed using ws_combo_box_new_text_and_pointer()
516 * @param idx Index of the entry which is to be set as active (ie: selected).
517 * Index refers to the immediate children of the tree.
519 void ws_combo_box_set_active(GtkComboBox *combo_box, gint idx);
522 * ws_combo_box_set_active_iter:
523 * @param combo_box A #GtkComboBox constructed using ws_combo_box_new_text_and_pointer()
524 * @param iter of the entry which is to be set as active (ie: selected).
527 ws_combo_box_set_active_iter(GtkComboBox *combo_box, GtkTreeIter *iter);
529 #if GTK_CHECK_VERSION(2,22,0)
530 #if !GTK_CHECK_VERSION(3,0,0)
531 GdkPixbuf *gdk_pixbuf_get_from_surface (cairo_surface_t *surface,
538 #endif /* __GUI_UTIL__H__ */