1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/assertdlg_gtk.cpp
3 // Purpose: GtkAssertDialog
4 // Author: Francesco Montorsi
6 // Copyright: (c) 2006 Francesco Montorsi
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////// */
10 #include "wx/wxprec.h"
15 #include "wx/gtk/assertdlg_gtk.h"
16 #include "wx/gtk/private/gtk2-compat.h"
20 /* ----------------------------------------------------------------------------
22 ---------------------------------------------------------------------------- */
25 NB: when changing order of the columns also update the gtk_list_store_new() call
26 in gtk_assert_dialog_create_backtrace_list_model() function
28 #define STACKFRAME_LEVEL_COLIDX 0
29 #define FUNCTION_PROTOTYPE_COLIDX 1
30 #define SOURCE_FILE_COLIDX 2
31 #define LINE_NUMBER_COLIDX 3
33 /* ----------------------------------------------------------------------------
34 GtkAssertDialog helpers
35 ---------------------------------------------------------------------------- */
38 GtkWidget
*gtk_assert_dialog_add_button_to (GtkBox
*box
, const gchar
*label
,
41 /* create the button */
42 GtkWidget
*button
= gtk_button_new_with_mnemonic (label
);
43 gtk_widget_set_can_default(button
, true);
45 /* add a stock icon inside it */
46 GtkWidget
*image
= gtk_image_new_from_stock (stock
, GTK_ICON_SIZE_BUTTON
);
47 gtk_button_set_image (GTK_BUTTON (button
), image
);
49 /* add to the given (container) widget */
51 gtk_box_pack_end (box
, button
, FALSE
, TRUE
, 8);
57 GtkWidget
*gtk_assert_dialog_add_button (GtkAssertDialog
*dlg
, const gchar
*label
,
58 const gchar
*stock
, gint response_id
)
60 /* create the button */
61 GtkWidget
* button
= gtk_assert_dialog_add_button_to(NULL
, label
, stock
);
63 /* add the button to the dialog's action area */
64 gtk_dialog_add_action_widget (GTK_DIALOG (dlg
), button
, response_id
);
72 void gtk_assert_dialog_append_text_column (GtkWidget
*treeview
, const gchar
*name
, int index
)
74 GtkCellRenderer
*renderer
;
75 GtkTreeViewColumn
*column
;
77 renderer
= gtk_cell_renderer_text_new ();
78 column
= gtk_tree_view_column_new_with_attributes (name
, renderer
,
80 gtk_tree_view_insert_column (GTK_TREE_VIEW (treeview
), column
, index
);
81 gtk_tree_view_column_set_resizable (column
, TRUE
);
82 gtk_tree_view_column_set_reorderable (column
, TRUE
);
86 GtkWidget
*gtk_assert_dialog_create_backtrace_list_model ()
91 /* create list store */
92 store
= gtk_list_store_new (4,
93 G_TYPE_UINT
, /* stack frame number */
94 G_TYPE_STRING
, /* function prototype */
95 G_TYPE_STRING
, /* source file name */
96 G_TYPE_STRING
); /* line number */
98 /* create the tree view */
99 treeview
= gtk_tree_view_new_with_model (GTK_TREE_MODEL(store
));
100 g_object_unref (store
);
101 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview
), TRUE
);
104 gtk_assert_dialog_append_text_column(treeview
, "#", STACKFRAME_LEVEL_COLIDX
);
105 gtk_assert_dialog_append_text_column(treeview
, "Function Prototype", FUNCTION_PROTOTYPE_COLIDX
);
106 gtk_assert_dialog_append_text_column(treeview
, "Source file", SOURCE_FILE_COLIDX
);
107 gtk_assert_dialog_append_text_column(treeview
, "Line #", LINE_NUMBER_COLIDX
);
113 void gtk_assert_dialog_process_backtrace (GtkAssertDialog
*dlg
)
115 /* set busy cursor */
116 GdkWindow
*parent
= gtk_widget_get_window(GTK_WIDGET(dlg
));
117 GdkCursor
*cur
= gdk_cursor_new (GDK_WATCH
);
118 gdk_window_set_cursor (parent
, cur
);
121 (*dlg
->callback
)(dlg
->userdata
);
123 /* toggle busy cursor */
124 gdk_window_set_cursor (parent
, NULL
);
128 gdk_cursor_unref (cur
);
133 /* ----------------------------------------------------------------------------
134 GtkAssertDialog signal handlers
135 ---------------------------------------------------------------------------- */
137 static void gtk_assert_dialog_expander_callback(GtkWidget
*, GtkAssertDialog
* dlg
)
139 /* status is not yet updated so we need to invert it to get the new one */
140 gboolean expanded
= !gtk_expander_get_expanded (GTK_EXPANDER(dlg
->expander
));
141 gtk_window_set_resizable (GTK_WINDOW (dlg
), expanded
);
143 if (dlg
->callback
== NULL
) /* was the backtrace already processed? */
146 gtk_assert_dialog_process_backtrace (dlg
);
148 /* mark the work as done (so that next activate we won't call the callback again) */
149 dlg
->callback
= NULL
;
152 static void gtk_assert_dialog_save_backtrace_callback(GtkWidget
*, GtkAssertDialog
* dlg
)
156 dialog
= gtk_file_chooser_dialog_new ("Save assert info to file", GTK_WINDOW(dlg
),
157 GTK_FILE_CHOOSER_ACTION_SAVE
,
158 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
159 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
,
162 if (gtk_dialog_run (GTK_DIALOG (dialog
)) == GTK_RESPONSE_ACCEPT
)
164 char *filename
, *msg
, *backtrace
;
167 filename
= gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog
));
170 msg
= gtk_assert_dialog_get_message (dlg
);
171 backtrace
= gtk_assert_dialog_get_backtrace (dlg
);
173 /* open the file and write all info inside it */
174 fp
= fopen (filename
, "w");
177 fprintf (fp
, "ASSERT INFO:\n%s\n\nBACKTRACE:\n%s", msg
, backtrace
);
187 gtk_widget_destroy (dialog
);
190 static void gtk_assert_dialog_copy_callback(GtkWidget
*, GtkAssertDialog
* dlg
)
192 char *msg
, *backtrace
;
193 GtkClipboard
*clipboard
;
196 msg
= gtk_assert_dialog_get_message (dlg
);
197 backtrace
= gtk_assert_dialog_get_backtrace (dlg
);
199 /* combine both in a single string */
200 str
= g_string_new("");
201 g_string_printf (str
, "ASSERT INFO:\n%s\n\nBACKTRACE:\n%s\n\n", msg
, backtrace
);
203 /* copy everything in default clipboard */
204 clipboard
= gtk_clipboard_get (GDK_SELECTION_CLIPBOARD
);
205 gtk_clipboard_set_text (clipboard
, str
->str
, str
->len
);
207 /* copy everything in primary clipboard too */
208 clipboard
= gtk_clipboard_get (GDK_SELECTION_PRIMARY
);
209 gtk_clipboard_set_text (clipboard
, str
->str
, str
->len
);
213 g_string_free (str
, TRUE
);
217 #endif // wxUSE_STACKWALKER
220 static void gtk_assert_dialog_continue_callback(GtkWidget
*, GtkAssertDialog
* dlg
)
223 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(dlg
->shownexttime
)) ?
224 GTK_ASSERT_DIALOG_CONTINUE
: GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
;
226 gtk_dialog_response (GTK_DIALOG(dlg
), response
);
230 /* ----------------------------------------------------------------------------
231 GtkAssertDialogClass implementation
232 ---------------------------------------------------------------------------- */
234 static void gtk_assert_dialog_init (GtkAssertDialog
*self
);
235 static void gtk_assert_dialog_class_init (GtkAssertDialogClass
*klass
);
237 GType
gtk_assert_dialog_get_type()
239 static GType assert_dialog_type
;
241 if (!assert_dialog_type
)
243 const GTypeInfo assert_dialog_info
=
245 sizeof (GtkAssertDialogClass
),
246 NULL
, /* base_init */
247 NULL
, /* base_finalize */
248 (GClassInitFunc
) gtk_assert_dialog_class_init
,
249 NULL
, /* class_finalize */
250 NULL
, /* class_data */
251 sizeof (GtkAssertDialog
),
252 16, /* n_preallocs */
253 (GInstanceInitFunc
) gtk_assert_dialog_init
,
256 assert_dialog_type
= g_type_register_static (GTK_TYPE_DIALOG
, "GtkAssertDialog", &assert_dialog_info
, (GTypeFlags
)0);
259 return assert_dialog_type
;
262 static void gtk_assert_dialog_class_init(GtkAssertDialogClass
*)
264 /* no special initializations required */
267 static void gtk_assert_dialog_init(GtkAssertDialog
* dlg
)
269 GtkWidget
*continuebtn
;
272 GtkWidget
*vbox
, *hbox
, *image
;
274 /* start the main vbox */
275 gtk_widget_push_composite_child ();
276 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 8);
277 gtk_container_set_border_width (GTK_CONTAINER(vbox
), 8);
278 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg
))), vbox
, true, true, 5);
281 /* add the icon+message hbox */
282 hbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
283 gtk_box_pack_start (GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
286 image
= gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR
, GTK_ICON_SIZE_DIALOG
);
287 gtk_box_pack_start (GTK_BOX(hbox
), image
, FALSE
, FALSE
, 12);
290 GtkWidget
*vbox2
, *info
;
293 vbox2
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
294 gtk_box_pack_start (GTK_BOX (hbox
), vbox2
, TRUE
, TRUE
, 0);
295 info
= gtk_label_new ("An assertion failed!");
296 gtk_box_pack_start (GTK_BOX(vbox2
), info
, TRUE
, TRUE
, 8);
299 dlg
->message
= gtk_label_new (NULL
);
300 gtk_label_set_selectable (GTK_LABEL (dlg
->message
), TRUE
);
301 gtk_label_set_line_wrap (GTK_LABEL (dlg
->message
), TRUE
);
302 gtk_label_set_justify (GTK_LABEL (dlg
->message
), GTK_JUSTIFY_LEFT
);
303 gtk_widget_set_size_request (GTK_WIDGET(dlg
->message
), 450, -1);
305 gtk_box_pack_end (GTK_BOX(vbox2
), GTK_WIDGET(dlg
->message
), TRUE
, TRUE
, 8);
308 #if wxUSE_STACKWALKER
309 /* add the expander */
310 dlg
->expander
= gtk_expander_new_with_mnemonic ("Back_trace:");
311 gtk_box_pack_start (GTK_BOX(vbox
), dlg
->expander
, TRUE
, TRUE
, 0);
312 g_signal_connect (dlg
->expander
, "activate",
313 G_CALLBACK(gtk_assert_dialog_expander_callback
), dlg
);
314 #endif // wxUSE_STACKWALKER
316 #if wxUSE_STACKWALKER
318 GtkWidget
*hbox
, *vbox
, *button
, *sw
;
320 /* create expander's vbox */
321 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
322 gtk_container_add (GTK_CONTAINER (dlg
->expander
), vbox
);
324 /* add a scrollable window under the expander */
325 sw
= gtk_scrolled_window_new (NULL
, NULL
);
326 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw
), GTK_SHADOW_ETCHED_IN
);
327 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw
), GTK_POLICY_AUTOMATIC
,
328 GTK_POLICY_AUTOMATIC
);
329 gtk_box_pack_start (GTK_BOX(vbox
), sw
, TRUE
, TRUE
, 8);
331 /* add the treeview to the scrollable window */
332 dlg
->treeview
= gtk_assert_dialog_create_backtrace_list_model ();
333 gtk_widget_set_size_request (GTK_WIDGET(dlg
->treeview
), -1, 180);
334 gtk_container_add (GTK_CONTAINER (sw
), dlg
->treeview
);
336 /* create button's hbox */
337 hbox
= gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL
);
338 gtk_box_pack_end (GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
339 gtk_button_box_set_layout (GTK_BUTTON_BOX(hbox
), GTK_BUTTONBOX_END
);
341 /* add the buttons */
342 button
= gtk_assert_dialog_add_button_to (GTK_BOX(hbox
), "Save to _file",
344 g_signal_connect (button
, "clicked",
345 G_CALLBACK(gtk_assert_dialog_save_backtrace_callback
), dlg
);
347 button
= gtk_assert_dialog_add_button_to (GTK_BOX(hbox
), "Copy to clip_board",
349 g_signal_connect (button
, "clicked", G_CALLBACK(gtk_assert_dialog_copy_callback
), dlg
);
351 #endif // wxUSE_STACKWALKER
353 /* add the checkbutton */
354 dlg
->shownexttime
= gtk_check_button_new_with_mnemonic("Show this _dialog the next time");
355 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(dlg
->shownexttime
), TRUE
);
356 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dlg
))), dlg
->shownexttime
, false, true, 8);
358 /* add the stop button */
359 gtk_assert_dialog_add_button (dlg
, "_Stop", GTK_STOCK_QUIT
, GTK_ASSERT_DIALOG_STOP
);
361 /* add the continue button */
362 continuebtn
= gtk_assert_dialog_add_button (dlg
, "_Continue", GTK_STOCK_YES
, GTK_ASSERT_DIALOG_CONTINUE
);
363 gtk_dialog_set_default_response (GTK_DIALOG (dlg
), GTK_ASSERT_DIALOG_CONTINUE
);
364 g_signal_connect (continuebtn
, "clicked", G_CALLBACK(gtk_assert_dialog_continue_callback
), dlg
);
366 /* complete creation */
367 dlg
->callback
= NULL
;
368 dlg
->userdata
= NULL
;
370 /* the resizable property of this window is modified by the expander:
371 when it's collapsed, the window must be non-resizable! */
372 gtk_window_set_resizable (GTK_WINDOW (dlg
), FALSE
);
373 gtk_widget_pop_composite_child ();
374 gtk_widget_show_all (GTK_WIDGET(dlg
));
377 /* ----------------------------------------------------------------------------
378 GtkAssertDialog public API
379 ---------------------------------------------------------------------------- */
381 gchar
*gtk_assert_dialog_get_message (GtkAssertDialog
*dlg
)
384 1) returned string must g_free()d !
385 2) Pango markup is automatically stripped off by GTK
387 return g_strdup (gtk_label_get_text (GTK_LABEL(dlg
->message
)));
390 #if wxUSE_STACKWALKER
392 gchar
*gtk_assert_dialog_get_backtrace (GtkAssertDialog
*dlg
)
394 gchar
*function
, *sourcefile
, *linenum
;
401 g_return_val_if_fail (GTK_IS_ASSERT_DIALOG (dlg
), NULL
);
402 model
= gtk_tree_view_get_model (GTK_TREE_VIEW(dlg
->treeview
));
403 string
= g_string_new("");
405 /* iterate over the list */
406 if (!gtk_tree_model_get_iter_first (model
, &iter
))
411 /* append this stack frame's info to the string */
412 gtk_tree_model_get(model
, &iter
,
413 STACKFRAME_LEVEL_COLIDX
, &count
,
414 FUNCTION_PROTOTYPE_COLIDX
, &function
,
415 SOURCE_FILE_COLIDX
, &sourcefile
,
416 LINE_NUMBER_COLIDX
, &linenum
,
419 g_string_append_printf(string
, "[%u] %s",
421 if (sourcefile
[0] != '\0')
422 g_string_append_printf (string
, " %s", sourcefile
);
423 if (linenum
[0] != '\0')
424 g_string_append_printf (string
, ":%s", linenum
);
425 g_string_append (string
, "\n");
431 } while (gtk_tree_model_iter_next (model
, &iter
));
433 /* returned string must g_free()d */
434 return g_string_free (string
, FALSE
);
437 void gtk_assert_dialog_set_message(GtkAssertDialog
*dlg
, const gchar
*msg
)
439 /* prepend and append the <b> tag
440 NOTE: g_markup_printf_escaped() is not used because it's available
441 only for glib >= 2.4 */
442 gchar
*escaped_msg
= g_markup_escape_text (msg
, -1);
443 gchar
*decorated_msg
= g_strdup_printf ("<b>%s</b>", escaped_msg
);
445 g_return_if_fail (GTK_IS_ASSERT_DIALOG (dlg
));
446 gtk_label_set_markup (GTK_LABEL(dlg
->message
), decorated_msg
);
448 g_free (decorated_msg
);
449 g_free (escaped_msg
);
452 void gtk_assert_dialog_set_backtrace_callback(GtkAssertDialog
*assertdlg
,
453 GtkAssertDialogStackFrameCallback callback
,
456 assertdlg
->callback
= callback
;
457 assertdlg
->userdata
= userdata
;
460 void gtk_assert_dialog_append_stack_frame(GtkAssertDialog
*dlg
,
461 const gchar
*function
,
462 const gchar
*sourcefile
,
470 g_return_if_fail (GTK_IS_ASSERT_DIALOG (dlg
));
471 model
= gtk_tree_view_get_model (GTK_TREE_VIEW(dlg
->treeview
));
473 /* how many items are in the list up to now ? */
474 count
= gtk_tree_model_iter_n_children (model
, NULL
);
476 linenum
= g_string_new("");
477 if ( line_number
!= 0 )
478 g_string_printf (linenum
, "%u", line_number
);
480 /* add data to the list store */
481 gtk_list_store_append (GTK_LIST_STORE(model
), &iter
);
482 gtk_list_store_set (GTK_LIST_STORE(model
), &iter
,
483 STACKFRAME_LEVEL_COLIDX
, count
+1, /* start from 1 and not from 0 */
484 FUNCTION_PROTOTYPE_COLIDX
, function
,
485 SOURCE_FILE_COLIDX
, sourcefile
,
486 LINE_NUMBER_COLIDX
, linenum
->str
,
489 g_string_free (linenum
, TRUE
);
492 #endif // wxUSE_STACKWALKER
494 GtkWidget
*gtk_assert_dialog_new(void)
496 void* dialog
= g_object_new(GTK_TYPE_ASSERT_DIALOG
, NULL
);
498 return GTK_WIDGET (dialog
);
501 #endif // wxDEBUG_LEVEL