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