]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/assertdlg_gtk.cpp
attempt to get the 'new focus' window parameter of a focus kill event set correctly
[wxWidgets.git] / src / gtk / assertdlg_gtk.cpp
CommitLineData
23b22b10 1/* ///////////////////////////////////////////////////////////////////////////
a481bbc3 2// Name: src/gtk/assertdlg_gtk.cpp
23b22b10
PC
3// Purpose: GtkAssertDialog
4// Author: Francesco Montorsi
5// Id: $Id$
6// Copyright: (c) 2006 Francesco Montorsi
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////// */
9
e762ef8f
VZ
10#include "wx/wxprec.h"
11
3eabbbcc
PC
12#if wxDEBUG_LEVEL
13
23b22b10 14#include <gtk/gtk.h>
a481bbc3 15#include "wx/gtk/assertdlg_gtk.h"
6c875e80 16#include "wx/gtk/private/gtk2-compat.h"
23b22b10 17
bedba0d1
VZ
18#include <stdio.h>
19
23b22b10
PC
20/* ----------------------------------------------------------------------------
21 Constants
22 ---------------------------------------------------------------------------- */
23
24/*
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
27 */
28#define STACKFRAME_LEVEL_COLIDX 0
8d241dea 29#define FUNCTION_PROTOTYPE_COLIDX 1
23b22b10
PC
30#define SOURCE_FILE_COLIDX 2
31#define LINE_NUMBER_COLIDX 3
23b22b10 32
23b22b10
PC
33/* ----------------------------------------------------------------------------
34 GtkAssertDialog helpers
35 ---------------------------------------------------------------------------- */
36
26ec6be7 37static
23b22b10 38GtkWidget *gtk_assert_dialog_add_button_to (GtkBox *box, const gchar *label,
a481bbc3 39 const gchar *stock)
23b22b10
PC
40{
41 /* create the button */
42 GtkWidget *button = gtk_button_new_with_mnemonic (label);
a481bbc3 43 gtk_widget_set_can_default(button, true);
23b22b10 44
a481bbc3
PC
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);
23b22b10
PC
48
49 /* add to the given (container) widget */
50 if (box)
51 gtk_box_pack_end (box, button, FALSE, TRUE, 8);
52
53 return button;
54}
55
26ec6be7 56static
23b22b10
PC
57GtkWidget *gtk_assert_dialog_add_button (GtkAssertDialog *dlg, const gchar *label,
58 const gchar *stock, gint response_id)
59{
60 /* create the button */
a481bbc3 61 GtkWidget* button = gtk_assert_dialog_add_button_to(NULL, label, stock);
23b22b10
PC
62
63 /* add the button to the dialog's action area */
64 gtk_dialog_add_action_widget (GTK_DIALOG (dlg), button, response_id);
65
66 return button;
67}
68
3eabbbcc
PC
69#if wxUSE_STACKWALKER
70
26ec6be7 71static
23b22b10
PC
72void gtk_assert_dialog_append_text_column (GtkWidget *treeview, const gchar *name, int index)
73{
74 GtkCellRenderer *renderer;
75 GtkTreeViewColumn *column;
76
77 renderer = gtk_cell_renderer_text_new ();
78 column = gtk_tree_view_column_new_with_attributes (name, renderer,
79 "text", index, NULL);
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);
83}
84
26ec6be7 85static
23b22b10
PC
86GtkWidget *gtk_assert_dialog_create_backtrace_list_model ()
87{
88 GtkListStore *store;
89 GtkWidget *treeview;
90
91 /* create list store */
8d241dea 92 store = gtk_list_store_new (4,
23b22b10 93 G_TYPE_UINT, /* stack frame number */
8d241dea 94 G_TYPE_STRING, /* function prototype */
23b22b10 95 G_TYPE_STRING, /* source file name */
8d241dea 96 G_TYPE_STRING); /* line number */
23b22b10
PC
97
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);
102
103 /* append columns */
104 gtk_assert_dialog_append_text_column(treeview, "#", STACKFRAME_LEVEL_COLIDX);
8d241dea 105 gtk_assert_dialog_append_text_column(treeview, "Function Prototype", FUNCTION_PROTOTYPE_COLIDX);
23b22b10
PC
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);
108
109 return treeview;
110}
111
26ec6be7 112static
23b22b10
PC
113void gtk_assert_dialog_process_backtrace (GtkAssertDialog *dlg)
114{
115 /* set busy cursor */
a481bbc3 116 GdkWindow *parent = gtk_widget_get_window(GTK_WIDGET(dlg));
23b22b10
PC
117 GdkCursor *cur = gdk_cursor_new (GDK_WATCH);
118 gdk_window_set_cursor (parent, cur);
119 gdk_flush ();
120
121 (*dlg->callback)(dlg->userdata);
122
123 /* toggle busy cursor */
124 gdk_window_set_cursor (parent, NULL);
1897abe1
PC
125#ifdef __WXGTK3__
126 g_object_unref(cur);
127#else
23b22b10 128 gdk_cursor_unref (cur);
1897abe1 129#endif
23b22b10
PC
130}
131
a481bbc3 132extern "C" {
23b22b10
PC
133/* ----------------------------------------------------------------------------
134 GtkAssertDialog signal handlers
135 ---------------------------------------------------------------------------- */
136
a481bbc3 137static void gtk_assert_dialog_expander_callback(GtkWidget*, GtkAssertDialog* dlg)
23b22b10
PC
138{
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);
142
143 if (dlg->callback == NULL) /* was the backtrace already processed? */
144 return;
145
146 gtk_assert_dialog_process_backtrace (dlg);
147
148 /* mark the work as done (so that next activate we won't call the callback again) */
149 dlg->callback = NULL;
150}
151
a481bbc3 152static void gtk_assert_dialog_save_backtrace_callback(GtkWidget*, GtkAssertDialog* dlg)
23b22b10
PC
153{
154 GtkWidget *dialog;
155
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,
160 NULL);
161
162 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
163 {
164 char *filename, *msg, *backtrace;
165 FILE *fp;
166
167 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
168 if ( filename )
169 {
170 msg = gtk_assert_dialog_get_message (dlg);
171 backtrace = gtk_assert_dialog_get_backtrace (dlg);
172
173 /* open the file and write all info inside it */
174 fp = fopen (filename, "w");
175 if (fp)
176 {
177 fprintf (fp, "ASSERT INFO:\n%s\n\nBACKTRACE:\n%s", msg, backtrace);
178 fclose (fp);
179 }
180
181 g_free (filename);
182 g_free (msg);
183 g_free (backtrace);
184 }
185 }
186
187 gtk_widget_destroy (dialog);
188}
189
a481bbc3 190static void gtk_assert_dialog_copy_callback(GtkWidget*, GtkAssertDialog* dlg)
23b22b10
PC
191{
192 char *msg, *backtrace;
193 GtkClipboard *clipboard;
194 GString *str;
195
196 msg = gtk_assert_dialog_get_message (dlg);
197 backtrace = gtk_assert_dialog_get_backtrace (dlg);
198
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);
202
203 /* copy everything in default clipboard */
204 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
205 gtk_clipboard_set_text (clipboard, str->str, str->len);
206
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);
210
211 g_free (msg);
212 g_free (backtrace);
213 g_string_free (str, TRUE);
214}
3eabbbcc 215} // extern "C"
23b22b10 216
3eabbbcc
PC
217#endif // wxUSE_STACKWALKER
218
219extern "C" {
a481bbc3 220static void gtk_assert_dialog_continue_callback(GtkWidget*, GtkAssertDialog* dlg)
23b22b10
PC
221{
222 gint response =
223 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(dlg->shownexttime)) ?
224 GTK_ASSERT_DIALOG_CONTINUE : GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING;
225
226 gtk_dialog_response (GTK_DIALOG(dlg), response);
227}
a481bbc3 228} // extern "C"
23b22b10
PC
229
230/* ----------------------------------------------------------------------------
231 GtkAssertDialogClass implementation
232 ---------------------------------------------------------------------------- */
233
faad53d1
PC
234extern "C" {
235static void gtk_assert_dialog_init(GTypeInstance* instance, void*);
236}
23b22b10 237
a481bbc3 238GType gtk_assert_dialog_get_type()
23b22b10 239{
a481bbc3 240 static GType assert_dialog_type;
23b22b10
PC
241
242 if (!assert_dialog_type)
243 {
244 const GTypeInfo assert_dialog_info =
245 {
246 sizeof (GtkAssertDialogClass),
247 NULL, /* base_init */
248 NULL, /* base_finalize */
faad53d1 249 NULL,
23b22b10
PC
250 NULL, /* class_finalize */
251 NULL, /* class_data */
252 sizeof (GtkAssertDialog),
253 16, /* n_preallocs */
faad53d1 254 gtk_assert_dialog_init,
23b22b10
PC
255 NULL
256 };
257 assert_dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "GtkAssertDialog", &assert_dialog_info, (GTypeFlags)0);
258 }
259
260 return assert_dialog_type;
261}
262
faad53d1
PC
263extern "C" {
264static void gtk_assert_dialog_init(GTypeInstance* instance, void*)
23b22b10 265{
faad53d1 266 GtkAssertDialog* dlg = GTK_ASSERT_DIALOG(instance);
23b22b10
PC
267 GtkWidget *continuebtn;
268
269 {
270 GtkWidget *vbox, *hbox, *image;
271
272 /* start the main vbox */
273 gtk_widget_push_composite_child ();
1897abe1 274 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
23b22b10 275 gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
a481bbc3 276 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), vbox, true, true, 5);
23b22b10
PC
277
278
279 /* add the icon+message hbox */
1897abe1 280 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
23b22b10
PC
281 gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
282
283 /* icon */
284 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
285 gtk_box_pack_start (GTK_BOX(hbox), image, FALSE, FALSE, 12);
286
287 {
288 GtkWidget *vbox2, *info;
289
290 /* message */
1897abe1 291 vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
23b22b10
PC
292 gtk_box_pack_start (GTK_BOX (hbox), vbox2, TRUE, TRUE, 0);
293 info = gtk_label_new ("An assertion failed!");
294 gtk_box_pack_start (GTK_BOX(vbox2), info, TRUE, TRUE, 8);
295
296 /* assert message */
297 dlg->message = gtk_label_new (NULL);
298 gtk_label_set_selectable (GTK_LABEL (dlg->message), TRUE);
299 gtk_label_set_line_wrap (GTK_LABEL (dlg->message), TRUE);
300 gtk_label_set_justify (GTK_LABEL (dlg->message), GTK_JUSTIFY_LEFT);
301 gtk_widget_set_size_request (GTK_WIDGET(dlg->message), 450, -1);
302
303 gtk_box_pack_end (GTK_BOX(vbox2), GTK_WIDGET(dlg->message), TRUE, TRUE, 8);
304 }
305
3eabbbcc 306#if wxUSE_STACKWALKER
23b22b10
PC
307 /* add the expander */
308 dlg->expander = gtk_expander_new_with_mnemonic ("Back_trace:");
309 gtk_box_pack_start (GTK_BOX(vbox), dlg->expander, TRUE, TRUE, 0);
84f623f8 310 g_signal_connect (dlg->expander, "activate",
23b22b10 311 G_CALLBACK(gtk_assert_dialog_expander_callback), dlg);
3eabbbcc 312#endif // wxUSE_STACKWALKER
23b22b10 313 }
3eabbbcc 314#if wxUSE_STACKWALKER
23b22b10
PC
315 {
316 GtkWidget *hbox, *vbox, *button, *sw;
317
318 /* create expander's vbox */
1897abe1 319 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
23b22b10
PC
320 gtk_container_add (GTK_CONTAINER (dlg->expander), vbox);
321
322 /* add a scrollable window under the expander */
323 sw = gtk_scrolled_window_new (NULL, NULL);
324 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_ETCHED_IN);
325 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_AUTOMATIC,
326 GTK_POLICY_AUTOMATIC);
327 gtk_box_pack_start (GTK_BOX(vbox), sw, TRUE, TRUE, 8);
328
329 /* add the treeview to the scrollable window */
330 dlg->treeview = gtk_assert_dialog_create_backtrace_list_model ();
331 gtk_widget_set_size_request (GTK_WIDGET(dlg->treeview), -1, 180);
332 gtk_container_add (GTK_CONTAINER (sw), dlg->treeview);
333
334 /* create button's hbox */
1897abe1 335 hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
23b22b10
PC
336 gtk_box_pack_end (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
337 gtk_button_box_set_layout (GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
338
339 /* add the buttons */
340 button = gtk_assert_dialog_add_button_to (GTK_BOX(hbox), "Save to _file",
a481bbc3 341 GTK_STOCK_SAVE);
23b22b10
PC
342 g_signal_connect (button, "clicked",
343 G_CALLBACK(gtk_assert_dialog_save_backtrace_callback), dlg);
344
345 button = gtk_assert_dialog_add_button_to (GTK_BOX(hbox), "Copy to clip_board",
a481bbc3 346 GTK_STOCK_COPY);
23b22b10
PC
347 g_signal_connect (button, "clicked", G_CALLBACK(gtk_assert_dialog_copy_callback), dlg);
348 }
3eabbbcc 349#endif // wxUSE_STACKWALKER
23b22b10
PC
350
351 /* add the checkbutton */
352 dlg->shownexttime = gtk_check_button_new_with_mnemonic("Show this _dialog the next time");
353 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(dlg->shownexttime), TRUE);
a481bbc3 354 gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dlg))), dlg->shownexttime, false, true, 8);
23b22b10
PC
355
356 /* add the stop button */
357 gtk_assert_dialog_add_button (dlg, "_Stop", GTK_STOCK_QUIT, GTK_ASSERT_DIALOG_STOP);
358
359 /* add the continue button */
360 continuebtn = gtk_assert_dialog_add_button (dlg, "_Continue", GTK_STOCK_YES, GTK_ASSERT_DIALOG_CONTINUE);
361 gtk_dialog_set_default_response (GTK_DIALOG (dlg), GTK_ASSERT_DIALOG_CONTINUE);
362 g_signal_connect (continuebtn, "clicked", G_CALLBACK(gtk_assert_dialog_continue_callback), dlg);
363
364 /* complete creation */
365 dlg->callback = NULL;
366 dlg->userdata = NULL;
367
d13b34d3
DS
368 /* the resizable property of this window is modified by the expander:
369 when it's collapsed, the window must be non-resizable! */
23b22b10
PC
370 gtk_window_set_resizable (GTK_WINDOW (dlg), FALSE);
371 gtk_widget_pop_composite_child ();
372 gtk_widget_show_all (GTK_WIDGET(dlg));
373}
faad53d1 374}
23b22b10 375
23b22b10
PC
376/* ----------------------------------------------------------------------------
377 GtkAssertDialog public API
378 ---------------------------------------------------------------------------- */
379
380gchar *gtk_assert_dialog_get_message (GtkAssertDialog *dlg)
381{
382 /* NOTES:
383 1) returned string must g_free()d !
384 2) Pango markup is automatically stripped off by GTK
385 */
386 return g_strdup (gtk_label_get_text (GTK_LABEL(dlg->message)));
387}
388
3eabbbcc
PC
389#if wxUSE_STACKWALKER
390
23b22b10
PC
391gchar *gtk_assert_dialog_get_backtrace (GtkAssertDialog *dlg)
392{
8d241dea 393 gchar *function, *sourcefile, *linenum;
23b22b10
PC
394 guint count;
395
396 GtkTreeModel *model;
397 GtkTreeIter iter;
398 GString *string;
399
400 g_return_val_if_fail (GTK_IS_ASSERT_DIALOG (dlg), NULL);
401 model = gtk_tree_view_get_model (GTK_TREE_VIEW(dlg->treeview));
402 string = g_string_new("");
403
404 /* iterate over the list */
405 if (!gtk_tree_model_get_iter_first (model, &iter))
406 return NULL;
407
408 do
409 {
410 /* append this stack frame's info to the string */
8d241dea 411 gtk_tree_model_get(model, &iter,
23b22b10 412 STACKFRAME_LEVEL_COLIDX, &count,
8d241dea 413 FUNCTION_PROTOTYPE_COLIDX, &function,
23b22b10
PC
414 SOURCE_FILE_COLIDX, &sourcefile,
415 LINE_NUMBER_COLIDX, &linenum,
416 -1);
417
8d241dea
VZ
418 g_string_append_printf(string, "[%u] %s",
419 count, function);
23b22b10
PC
420 if (sourcefile[0] != '\0')
421 g_string_append_printf (string, " %s", sourcefile);
422 if (linenum[0] != '\0')
423 g_string_append_printf (string, ":%s", linenum);
424 g_string_append (string, "\n");
425
426 g_free (function);
23b22b10
PC
427 g_free (sourcefile);
428 g_free (linenum);
429
430 } while (gtk_tree_model_iter_next (model, &iter));
431
432 /* returned string must g_free()d */
433 return g_string_free (string, FALSE);
434}
435
2a9332ae
PC
436#endif // wxUSE_STACKWALKER
437
23b22b10
PC
438void gtk_assert_dialog_set_message(GtkAssertDialog *dlg, const gchar *msg)
439{
440 /* prepend and append the <b> tag
441 NOTE: g_markup_printf_escaped() is not used because it's available
442 only for glib >= 2.4 */
443 gchar *escaped_msg = g_markup_escape_text (msg, -1);
444 gchar *decorated_msg = g_strdup_printf ("<b>%s</b>", escaped_msg);
445
446 g_return_if_fail (GTK_IS_ASSERT_DIALOG (dlg));
447 gtk_label_set_markup (GTK_LABEL(dlg->message), decorated_msg);
448
449 g_free (decorated_msg);
450 g_free (escaped_msg);
451}
452
2a9332ae
PC
453#if wxUSE_STACKWALKER
454
23b22b10
PC
455void gtk_assert_dialog_set_backtrace_callback(GtkAssertDialog *assertdlg,
456 GtkAssertDialogStackFrameCallback callback,
457 void *userdata)
458{
459 assertdlg->callback = callback;
460 assertdlg->userdata = userdata;
461}
462
463void gtk_assert_dialog_append_stack_frame(GtkAssertDialog *dlg,
464 const gchar *function,
23b22b10
PC
465 const gchar *sourcefile,
466 guint line_number)
467{
468 GtkTreeModel *model;
469 GtkTreeIter iter;
470 GString *linenum;
471 gint count;
472
473 g_return_if_fail (GTK_IS_ASSERT_DIALOG (dlg));
474 model = gtk_tree_view_get_model (GTK_TREE_VIEW(dlg->treeview));
475
476 /* how many items are in the list up to now ? */
477 count = gtk_tree_model_iter_n_children (model, NULL);
478
479 linenum = g_string_new("");
480 if ( line_number != 0 )
481 g_string_printf (linenum, "%u", line_number);
482
483 /* add data to the list store */
484 gtk_list_store_append (GTK_LIST_STORE(model), &iter);
485 gtk_list_store_set (GTK_LIST_STORE(model), &iter,
486 STACKFRAME_LEVEL_COLIDX, count+1, /* start from 1 and not from 0 */
8d241dea 487 FUNCTION_PROTOTYPE_COLIDX, function,
23b22b10
PC
488 SOURCE_FILE_COLIDX, sourcefile,
489 LINE_NUMBER_COLIDX, linenum->str,
490 -1);
491
492 g_string_free (linenum, TRUE);
493}
494
3eabbbcc
PC
495#endif // wxUSE_STACKWALKER
496
23b22b10
PC
497GtkWidget *gtk_assert_dialog_new(void)
498{
a481bbc3 499 void* dialog = g_object_new(GTK_TYPE_ASSERT_DIALOG, NULL);
23b22b10
PC
500
501 return GTK_WIDGET (dialog);
502}
3eabbbcc
PC
503
504#endif // wxDEBUG_LEVEL