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