1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: assertdlg_gtk.c
3 // Purpose: GtkAssertDialog
4 // Author: Francesco Montorsi
6 // Copyright: (c) 2006 Francesco Montorsi
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////// */
11 #define XCheckIfEvent XCHECKIFEVENT
14 #include "wx/platform.h"
15 #include "wx/gtk/assertdlg_gtk.h"
19 #endif /* __cplusplus */
27 #if GTK_CHECK_VERSION(2,4,0)
28 #include <gtk/gtkexpander.h>
32 /* ----------------------------------------------------------------------------
34 ---------------------------------------------------------------------------- */
37 NB: when changing order of the columns also update the gtk_list_store_new() call
38 in gtk_assert_dialog_create_backtrace_list_model() function
40 #define STACKFRAME_LEVEL_COLIDX 0
41 #define FUNCTION_NAME_COLIDX 1
42 #define SOURCE_FILE_COLIDX 2
43 #define LINE_NUMBER_COLIDX 3
44 #define FUNCTION_ARGS_COLIDX 4
49 /* ----------------------------------------------------------------------------
50 GtkAssertDialog helpers
51 ---------------------------------------------------------------------------- */
53 GtkWidget
*gtk_assert_dialog_add_button_to (GtkBox
*box
, const gchar
*label
,
54 const gchar
*stock
, gint response_id
)
56 /* create the button */
57 GtkWidget
*button
= gtk_button_new_with_mnemonic (label
);
58 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
60 #if GTK_CHECK_VERSION(2,6,0)
61 if (!gtk_check_version (2, 6, 0))
63 /* add a stock icon inside it */
64 GtkWidget
*image
= gtk_image_new_from_stock (stock
, GTK_ICON_SIZE_BUTTON
);
65 gtk_button_set_image (GTK_BUTTON (button
), image
);
69 /* add to the given (container) widget */
71 gtk_box_pack_end (box
, button
, FALSE
, TRUE
, 8);
76 GtkWidget
*gtk_assert_dialog_add_button (GtkAssertDialog
*dlg
, const gchar
*label
,
77 const gchar
*stock
, gint response_id
)
79 /* create the button */
80 GtkWidget
*button
= gtk_assert_dialog_add_button_to (NULL
, label
, stock
, response_id
);
82 /* add the button to the dialog's action area */
83 gtk_dialog_add_action_widget (GTK_DIALOG (dlg
), button
, response_id
);
88 void gtk_assert_dialog_append_text_column (GtkWidget
*treeview
, const gchar
*name
, int index
)
90 GtkCellRenderer
*renderer
;
91 GtkTreeViewColumn
*column
;
93 renderer
= gtk_cell_renderer_text_new ();
94 column
= gtk_tree_view_column_new_with_attributes (name
, renderer
,
96 gtk_tree_view_insert_column (GTK_TREE_VIEW (treeview
), column
, index
);
97 gtk_tree_view_column_set_resizable (column
, TRUE
);
98 gtk_tree_view_column_set_reorderable (column
, TRUE
);
101 GtkWidget
*gtk_assert_dialog_create_backtrace_list_model ()
106 /* create list store */
107 store
= gtk_list_store_new (5,
108 G_TYPE_UINT
, /* stack frame number */
109 G_TYPE_STRING
, /* function name */
110 G_TYPE_STRING
, /* source file name */
111 G_TYPE_STRING
, /* line number */
112 G_TYPE_STRING
); /* function arguments */
114 /* create the tree view */
115 treeview
= gtk_tree_view_new_with_model (GTK_TREE_MODEL(store
));
116 g_object_unref (store
);
117 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview
), TRUE
);
120 gtk_assert_dialog_append_text_column(treeview
, "#", STACKFRAME_LEVEL_COLIDX
);
121 gtk_assert_dialog_append_text_column(treeview
, "Function name", FUNCTION_NAME_COLIDX
);
122 gtk_assert_dialog_append_text_column(treeview
, "Function args", FUNCTION_ARGS_COLIDX
);
123 gtk_assert_dialog_append_text_column(treeview
, "Source file", SOURCE_FILE_COLIDX
);
124 gtk_assert_dialog_append_text_column(treeview
, "Line #", LINE_NUMBER_COLIDX
);
129 void gtk_assert_dialog_process_backtrace (GtkAssertDialog
*dlg
)
131 /* set busy cursor */
132 GdkWindow
*parent
= GTK_WIDGET(dlg
)->window
;
133 GdkCursor
*cur
= gdk_cursor_new (GDK_WATCH
);
134 gdk_window_set_cursor (parent
, cur
);
137 (*dlg
->callback
)(dlg
->userdata
);
139 /* toggle busy cursor */
140 gdk_window_set_cursor (parent
, NULL
);
141 gdk_cursor_unref (cur
);
146 /* ----------------------------------------------------------------------------
147 GtkAssertDialog signal handlers
148 ---------------------------------------------------------------------------- */
150 /* GtkFileChooserDialog and GtkExpander are only available in GTK+ >= 2.4 */
151 #if GTK_CHECK_VERSION(2,4,0)
153 void gtk_assert_dialog_expander_callback (GtkWidget
*widget
, GtkAssertDialog
*dlg
)
155 /* status is not yet updated so we need to invert it to get the new one */
156 gboolean expanded
= !gtk_expander_get_expanded (GTK_EXPANDER(dlg
->expander
));
157 gtk_window_set_resizable (GTK_WINDOW (dlg
), expanded
);
159 if (dlg
->callback
== NULL
) /* was the backtrace already processed? */
162 gtk_assert_dialog_process_backtrace (dlg
);
164 /* mark the work as done (so that next activate we won't call the callback again) */
165 dlg
->callback
= NULL
;
168 void gtk_assert_dialog_save_backtrace_callback (GtkWidget
*widget
, GtkAssertDialog
*dlg
)
172 dialog
= gtk_file_chooser_dialog_new ("Save assert info to file", GTK_WINDOW(dlg
),
173 GTK_FILE_CHOOSER_ACTION_SAVE
,
174 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
175 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
,
178 if (gtk_dialog_run (GTK_DIALOG (dialog
)) == GTK_RESPONSE_ACCEPT
)
180 char *filename
, *msg
, *backtrace
;
183 filename
= gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog
));
186 msg
= gtk_assert_dialog_get_message (dlg
);
187 backtrace
= gtk_assert_dialog_get_backtrace (dlg
);
189 /* open the file and write all info inside it */
190 fp
= fopen (filename
, "w");
193 fprintf (fp
, "ASSERT INFO:\n%s\n\nBACKTRACE:\n%s", msg
, backtrace
);
203 gtk_widget_destroy (dialog
);
205 #endif /* GTK+ 2.4+ */
207 void gtk_assert_dialog_copy_callback (GtkWidget
*widget
, GtkAssertDialog
*dlg
)
209 char *msg
, *backtrace
;
210 GtkClipboard
*clipboard
;
213 msg
= gtk_assert_dialog_get_message (dlg
);
214 backtrace
= gtk_assert_dialog_get_backtrace (dlg
);
216 /* combine both in a single string */
217 str
= g_string_new("");
218 g_string_printf (str
, "ASSERT INFO:\n%s\n\nBACKTRACE:\n%s\n\n", msg
, backtrace
);
220 /* copy everything in default clipboard */
221 clipboard
= gtk_clipboard_get (GDK_SELECTION_CLIPBOARD
);
222 gtk_clipboard_set_text (clipboard
, str
->str
, str
->len
);
224 /* copy everything in primary clipboard too */
225 clipboard
= gtk_clipboard_get (GDK_SELECTION_PRIMARY
);
226 gtk_clipboard_set_text (clipboard
, str
->str
, str
->len
);
230 g_string_free (str
, TRUE
);
233 void gtk_assert_dialog_continue_callback (GtkWidget
*widget
, GtkAssertDialog
*dlg
)
236 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(dlg
->shownexttime
)) ?
237 GTK_ASSERT_DIALOG_CONTINUE
: GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
;
239 gtk_dialog_response (GTK_DIALOG(dlg
), response
);
243 /* ----------------------------------------------------------------------------
244 GtkAssertDialogClass implementation
245 ---------------------------------------------------------------------------- */
247 static void gtk_assert_dialog_init (GtkAssertDialog
*self
);
248 static void gtk_assert_dialog_class_init (GtkAssertDialogClass
*klass
);
251 GtkType
gtk_assert_dialog_get_type (void)
253 static GtkType assert_dialog_type
= 0;
255 if (!assert_dialog_type
)
257 const GTypeInfo assert_dialog_info
=
259 sizeof (GtkAssertDialogClass
),
260 NULL
, /* base_init */
261 NULL
, /* base_finalize */
262 (GClassInitFunc
) gtk_assert_dialog_class_init
,
263 NULL
, /* class_finalize */
264 NULL
, /* class_data */
265 sizeof (GtkAssertDialog
),
266 16, /* n_preallocs */
267 (GInstanceInitFunc
) gtk_assert_dialog_init
,
270 assert_dialog_type
= g_type_register_static (GTK_TYPE_DIALOG
, "GtkAssertDialog", &assert_dialog_info
, (GTypeFlags
)0);
273 return assert_dialog_type
;
276 void gtk_assert_dialog_class_init(GtkAssertDialogClass
*klass
)
278 /* no special initializations required */
281 void gtk_assert_dialog_init(GtkAssertDialog
*dlg
)
283 GtkWidget
*vbox
, *hbox
, *image
, *continuebtn
;
285 /* start the main vbox */
286 gtk_widget_push_composite_child ();
287 vbox
= gtk_vbox_new (FALSE
, 8);
288 gtk_container_set_border_width (GTK_CONTAINER(vbox
), 8);
289 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg
)->vbox
), vbox
, TRUE
, TRUE
, 5);
292 /* add the icon+message hbox */
293 hbox
= gtk_hbox_new (FALSE
, 0);
294 gtk_box_pack_start (GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
297 image
= gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR
, GTK_ICON_SIZE_DIALOG
);
298 gtk_box_pack_start (GTK_BOX(hbox
), image
, FALSE
, FALSE
, 12);
301 GtkWidget
*vbox2
, *info
;
304 vbox2
= gtk_vbox_new (FALSE
, 0);
305 gtk_box_pack_start (GTK_BOX (hbox
), vbox2
, TRUE
, TRUE
, 0);
306 info
= gtk_label_new ("An assertion failed!");
307 gtk_box_pack_start (GTK_BOX(vbox2
), info
, TRUE
, TRUE
, 8);
310 dlg
->message
= gtk_label_new (NULL
);
311 gtk_label_set_selectable (GTK_LABEL (dlg
->message
), TRUE
);
312 gtk_label_set_line_wrap (GTK_LABEL (dlg
->message
), TRUE
);
313 gtk_label_set_justify (GTK_LABEL (dlg
->message
), GTK_JUSTIFY_LEFT
);
314 gtk_widget_set_size_request (GTK_WIDGET(dlg
->message
), 450, -1);
316 gtk_box_pack_end (GTK_BOX(vbox2
), GTK_WIDGET(dlg
->message
), TRUE
, TRUE
, 8);
319 /* add the expander */
320 #if GTK_CHECK_VERSION(2,4,0)
321 if (!gtk_check_version (2, 4, 0))
323 dlg
->expander
= gtk_expander_new_with_mnemonic ("Back_trace:");
324 gtk_box_pack_start (GTK_BOX(vbox
), dlg
->expander
, TRUE
, TRUE
, 0);
325 g_signal_connect (GTK_EXPANDER(dlg
->expander
), "activate",
326 G_CALLBACK(gtk_assert_dialog_expander_callback
), dlg
);
331 /* if GtkExpander is unavailable, then use a static frame instead */
332 dlg
->expander
= gtk_frame_new ("Back_trace:");
333 gtk_box_pack_start (GTK_BOX(vbox
), dlg
->expander
, TRUE
, TRUE
, 0);
337 GtkWidget
*hbox
, *vbox
, *button
, *sw
;
339 /* create expander's vbox */
340 vbox
= gtk_vbox_new (FALSE
, 0);
341 gtk_container_add (GTK_CONTAINER (dlg
->expander
), vbox
);
343 /* add a scrollable window under the expander */
344 sw
= gtk_scrolled_window_new (NULL
, NULL
);
345 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw
), GTK_SHADOW_ETCHED_IN
);
346 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw
), GTK_POLICY_AUTOMATIC
,
347 GTK_POLICY_AUTOMATIC
);
348 gtk_box_pack_start (GTK_BOX(vbox
), sw
, TRUE
, TRUE
, 8);
350 /* add the treeview to the scrollable window */
351 dlg
->treeview
= gtk_assert_dialog_create_backtrace_list_model ();
352 gtk_widget_set_size_request (GTK_WIDGET(dlg
->treeview
), -1, 180);
353 gtk_container_add (GTK_CONTAINER (sw
), dlg
->treeview
);
355 /* create button's hbox */
356 hbox
= gtk_hbutton_box_new ();
357 gtk_box_pack_end (GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
358 gtk_button_box_set_layout (GTK_BUTTON_BOX(hbox
), GTK_BUTTONBOX_END
);
360 /* add the buttons */
361 #if GTK_CHECK_VERSION(2,4,0)
362 if (!gtk_check_version (2, 4, 0))
364 /* add this button only if GTK supports GtkFileChooserDialog */
365 button
= gtk_assert_dialog_add_button_to (GTK_BOX(hbox
), "Save to _file",
366 GTK_STOCK_SAVE
, GTK_RESPONSE_NONE
);
367 g_signal_connect (button
, "clicked",
368 G_CALLBACK(gtk_assert_dialog_save_backtrace_callback
), dlg
);
372 button
= gtk_assert_dialog_add_button_to (GTK_BOX(hbox
), "Copy to clip_board",
373 GTK_STOCK_COPY
, GTK_RESPONSE_NONE
);
374 g_signal_connect (button
, "clicked", G_CALLBACK(gtk_assert_dialog_copy_callback
), dlg
);
377 /* add the checkbutton */
378 dlg
->shownexttime
= gtk_check_button_new_with_mnemonic("Show this _dialog the next time");
379 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(dlg
->shownexttime
), TRUE
);
380 gtk_box_pack_end (GTK_BOX(GTK_DIALOG(dlg
)->action_area
), dlg
->shownexttime
, FALSE
, TRUE
, 8);
382 /* add the stop button */
383 gtk_assert_dialog_add_button (dlg
, "_Stop", GTK_STOCK_QUIT
, GTK_ASSERT_DIALOG_STOP
);
385 /* add the continue button */
386 continuebtn
= gtk_assert_dialog_add_button (dlg
, "_Continue", GTK_STOCK_YES
, GTK_ASSERT_DIALOG_CONTINUE
);
387 gtk_dialog_set_default_response (GTK_DIALOG (dlg
), GTK_ASSERT_DIALOG_CONTINUE
);
388 g_signal_connect (continuebtn
, "clicked", G_CALLBACK(gtk_assert_dialog_continue_callback
), dlg
);
390 /* complete creation */
391 dlg
->callback
= NULL
;
392 dlg
->userdata
= NULL
;
394 /* the resizeable property of this window is modified by the expander:
395 when it's collapsed, the window must be non-resizeable! */
396 gtk_window_set_resizable (GTK_WINDOW (dlg
), FALSE
);
397 gtk_widget_pop_composite_child ();
398 gtk_widget_show_all (GTK_WIDGET(dlg
));
403 /* ----------------------------------------------------------------------------
404 GtkAssertDialog public API
405 ---------------------------------------------------------------------------- */
407 gchar
*gtk_assert_dialog_get_message (GtkAssertDialog
*dlg
)
410 1) returned string must g_free()d !
411 2) Pango markup is automatically stripped off by GTK
413 return g_strdup (gtk_label_get_text (GTK_LABEL(dlg
->message
)));
416 gchar
*gtk_assert_dialog_get_backtrace (GtkAssertDialog
*dlg
)
418 gchar
*function
, *arguments
, *sourcefile
, *linenum
;
425 g_return_val_if_fail (GTK_IS_ASSERT_DIALOG (dlg
), NULL
);
426 model
= gtk_tree_view_get_model (GTK_TREE_VIEW(dlg
->treeview
));
427 string
= g_string_new("");
429 /* iterate over the list */
430 if (!gtk_tree_model_get_iter_first (model
, &iter
))
435 /* append this stack frame's info to the string */
436 gtk_tree_model_get (model
, &iter
,
437 STACKFRAME_LEVEL_COLIDX
, &count
,
438 FUNCTION_NAME_COLIDX
, &function
,
439 FUNCTION_ARGS_COLIDX
, &arguments
,
440 SOURCE_FILE_COLIDX
, &sourcefile
,
441 LINE_NUMBER_COLIDX
, &linenum
,
444 g_string_append_printf (string
, "[%d] %s(%s)",
445 count
, function
, arguments
);
446 if (sourcefile
[0] != '\0')
447 g_string_append_printf (string
, " %s", sourcefile
);
448 if (linenum
[0] != '\0')
449 g_string_append_printf (string
, ":%s", linenum
);
450 g_string_append (string
, "\n");
457 } while (gtk_tree_model_iter_next (model
, &iter
));
459 /* returned string must g_free()d */
460 return g_string_free (string
, FALSE
);
463 void gtk_assert_dialog_set_message(GtkAssertDialog
*dlg
, const gchar
*msg
)
465 /* prepend and append the <b> tag
466 NOTE: g_markup_printf_escaped() is not used because it's available
467 only for glib >= 2.4 */
468 gchar
*escaped_msg
= g_markup_escape_text (msg
, -1);
469 gchar
*decorated_msg
= g_strdup_printf ("<b>%s</b>", escaped_msg
);
471 g_return_if_fail (GTK_IS_ASSERT_DIALOG (dlg
));
472 gtk_label_set_markup (GTK_LABEL(dlg
->message
), decorated_msg
);
474 g_free (decorated_msg
);
475 g_free (escaped_msg
);
478 void gtk_assert_dialog_set_backtrace_callback(GtkAssertDialog
*assertdlg
,
479 GtkAssertDialogStackFrameCallback callback
,
482 assertdlg
->callback
= callback
;
483 assertdlg
->userdata
= userdata
;
485 if (gtk_check_version (2, 4, 0))
487 /* we need to immediately process the stack trace as we're not using
488 an expander since GTK does not support it */
489 gtk_assert_dialog_process_backtrace (assertdlg
);
493 void gtk_assert_dialog_append_stack_frame(GtkAssertDialog
*dlg
,
494 const gchar
*function
,
495 const gchar
*arguments
,
496 const gchar
*sourcefile
,
504 g_return_if_fail (GTK_IS_ASSERT_DIALOG (dlg
));
505 model
= gtk_tree_view_get_model (GTK_TREE_VIEW(dlg
->treeview
));
507 /* how many items are in the list up to now ? */
508 count
= gtk_tree_model_iter_n_children (model
, NULL
);
510 linenum
= g_string_new("");
511 if ( line_number
!= 0 )
512 g_string_printf (linenum
, "%d", line_number
);
514 /* add data to the list store */
515 gtk_list_store_append (GTK_LIST_STORE(model
), &iter
);
516 gtk_list_store_set (GTK_LIST_STORE(model
), &iter
,
517 STACKFRAME_LEVEL_COLIDX
, count
+1, /* start from 1 and not from 0 */
518 FUNCTION_NAME_COLIDX
, function
,
519 FUNCTION_ARGS_COLIDX
, arguments
,
520 SOURCE_FILE_COLIDX
, sourcefile
,
521 LINE_NUMBER_COLIDX
, linenum
->str
,
524 g_string_free (linenum
, TRUE
);
527 GtkWidget
*gtk_assert_dialog_new(void)
529 GtkAssertDialog
*dialog
= g_object_new (GTK_TYPE_ASSERT_DIALOG
, NULL
);
531 return GTK_WIDGET (dialog
);
536 #endif /* __cplusplus */