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