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