prepare to convert file to C++
[wxWidgets.git] / src / gtk / assertdlg_gtk.cpp
1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 /* ----------------------------------------------------------------------------
28 Constants
29 ---------------------------------------------------------------------------- */
30
31 /*
32 NB: when changing order of the columns also update the gtk_list_store_new() call
33 in gtk_assert_dialog_create_backtrace_list_model() function
34 */
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 GtkWidget *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 return button;
81 }
82
83 void gtk_assert_dialog_append_text_column (GtkWidget *treeview, const gchar *name, int index)
84 {
85 GtkCellRenderer *renderer;
86 GtkTreeViewColumn *column;
87
88 renderer = gtk_cell_renderer_text_new ();
89 column = gtk_tree_view_column_new_with_attributes (name, renderer,
90 "text", index, NULL);
91 gtk_tree_view_insert_column (GTK_TREE_VIEW (treeview), column, index);
92 gtk_tree_view_column_set_resizable (column, TRUE);
93 gtk_tree_view_column_set_reorderable (column, TRUE);
94 }
95
96 GtkWidget *gtk_assert_dialog_create_backtrace_list_model ()
97 {
98 GtkListStore *store;
99 GtkWidget *treeview;
100
101 /* create list store */
102 store = gtk_list_store_new (5,
103 G_TYPE_UINT, /* stack frame number */
104 G_TYPE_STRING, /* function name */
105 G_TYPE_STRING, /* source file name */
106 G_TYPE_STRING, /* line number */
107 G_TYPE_STRING); /* function arguments */
108
109 /* create the tree view */
110 treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL(store));
111 g_object_unref (store);
112 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
113
114 /* append columns */
115 gtk_assert_dialog_append_text_column(treeview, "#", STACKFRAME_LEVEL_COLIDX);
116 gtk_assert_dialog_append_text_column(treeview, "Function name", FUNCTION_NAME_COLIDX);
117 gtk_assert_dialog_append_text_column(treeview, "Function args", FUNCTION_ARGS_COLIDX);
118 gtk_assert_dialog_append_text_column(treeview, "Source file", SOURCE_FILE_COLIDX);
119 gtk_assert_dialog_append_text_column(treeview, "Line #", LINE_NUMBER_COLIDX);
120
121 return treeview;
122 }
123
124 void gtk_assert_dialog_process_backtrace (GtkAssertDialog *dlg)
125 {
126 /* set busy cursor */
127 GdkWindow *parent = GTK_WIDGET(dlg)->window;
128 GdkCursor *cur = gdk_cursor_new (GDK_WATCH);
129 gdk_window_set_cursor (parent, cur);
130 gdk_flush ();
131
132 (*dlg->callback)(dlg->userdata);
133
134 /* toggle busy cursor */
135 gdk_window_set_cursor (parent, NULL);
136 gdk_cursor_unref (cur);
137 }
138
139
140
141 /* ----------------------------------------------------------------------------
142 GtkAssertDialog signal handlers
143 ---------------------------------------------------------------------------- */
144
145 void gtk_assert_dialog_expander_callback (GtkWidget *widget, GtkAssertDialog *dlg)
146 {
147 /* status is not yet updated so we need to invert it to get the new one */
148 gboolean expanded = !gtk_expander_get_expanded (GTK_EXPANDER(dlg->expander));
149 gtk_window_set_resizable (GTK_WINDOW (dlg), expanded);
150
151 if (dlg->callback == NULL) /* was the backtrace already processed? */
152 return;
153
154 gtk_assert_dialog_process_backtrace (dlg);
155
156 /* mark the work as done (so that next activate we won't call the callback again) */
157 dlg->callback = NULL;
158 }
159
160 void gtk_assert_dialog_save_backtrace_callback (GtkWidget *widget, GtkAssertDialog *dlg)
161 {
162 GtkWidget *dialog;
163
164 dialog = gtk_file_chooser_dialog_new ("Save assert info to file", GTK_WINDOW(dlg),
165 GTK_FILE_CHOOSER_ACTION_SAVE,
166 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
167 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
168 NULL);
169
170 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
171 {
172 char *filename, *msg, *backtrace;
173 FILE *fp;
174
175 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
176 if ( filename )
177 {
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)
184 {
185 fprintf (fp, "ASSERT INFO:\n%s\n\nBACKTRACE:\n%s", msg, backtrace);
186 fclose (fp);
187 }
188
189 g_free (filename);
190 g_free (msg);
191 g_free (backtrace);
192 }
193 }
194
195 gtk_widget_destroy (dialog);
196 }
197
198 void gtk_assert_dialog_copy_callback (GtkWidget *widget, GtkAssertDialog *dlg)
199 {
200 char *msg, *backtrace;
201 GtkClipboard *clipboard;
202 GString *str;
203
204 msg = gtk_assert_dialog_get_message (dlg);
205 backtrace = gtk_assert_dialog_get_backtrace (dlg);
206
207 /* combine both in a single string */
208 str = g_string_new("");
209 g_string_printf (str, "ASSERT INFO:\n%s\n\nBACKTRACE:\n%s\n\n", msg, backtrace);
210
211 /* copy everything in default clipboard */
212 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
213 gtk_clipboard_set_text (clipboard, str->str, str->len);
214
215 /* copy everything in primary clipboard too */
216 clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
217 gtk_clipboard_set_text (clipboard, str->str, str->len);
218
219 g_free (msg);
220 g_free (backtrace);
221 g_string_free (str, TRUE);
222 }
223
224 void gtk_assert_dialog_continue_callback (GtkWidget *widget, GtkAssertDialog *dlg)
225 {
226 gint response =
227 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(dlg->shownexttime)) ?
228 GTK_ASSERT_DIALOG_CONTINUE : GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING;
229
230 gtk_dialog_response (GTK_DIALOG(dlg), response);
231 }
232
233
234 /* ----------------------------------------------------------------------------
235 GtkAssertDialogClass implementation
236 ---------------------------------------------------------------------------- */
237
238 static void gtk_assert_dialog_init (GtkAssertDialog *self);
239 static void gtk_assert_dialog_class_init (GtkAssertDialogClass *klass);
240
241
242 GtkType gtk_assert_dialog_get_type (void)
243 {
244 static GtkType assert_dialog_type = 0;
245
246 if (!assert_dialog_type)
247 {
248 const GTypeInfo assert_dialog_info =
249 {
250 sizeof (GtkAssertDialogClass),
251 NULL, /* base_init */
252 NULL, /* base_finalize */
253 (GClassInitFunc) gtk_assert_dialog_class_init,
254 NULL, /* class_finalize */
255 NULL, /* class_data */
256 sizeof (GtkAssertDialog),
257 16, /* n_preallocs */
258 (GInstanceInitFunc) gtk_assert_dialog_init,
259 NULL
260 };
261 assert_dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "GtkAssertDialog", &assert_dialog_info, (GTypeFlags)0);
262 }
263
264 return assert_dialog_type;
265 }
266
267 void gtk_assert_dialog_class_init(GtkAssertDialogClass *klass)
268 {
269 /* no special initializations required */
270 }
271
272 void gtk_assert_dialog_init(GtkAssertDialog *dlg)
273 {
274 GtkWidget *continuebtn;
275
276 {
277 GtkWidget *vbox, *hbox, *image;
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 dlg->expander = gtk_expander_new_with_mnemonic ("Back_trace:");
315 gtk_box_pack_start (GTK_BOX(vbox), dlg->expander, TRUE, TRUE, 0);
316 g_signal_connect (GTK_EXPANDER(dlg->expander), "activate",
317 G_CALLBACK(gtk_assert_dialog_expander_callback), dlg);
318 }
319
320 {
321 GtkWidget *hbox, *vbox, *button, *sw;
322
323 /* create expander's vbox */
324 vbox = gtk_vbox_new (FALSE, 0);
325 gtk_container_add (GTK_CONTAINER (dlg->expander), vbox);
326
327 /* add a scrollable window under the expander */
328 sw = gtk_scrolled_window_new (NULL, NULL);
329 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_ETCHED_IN);
330 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_AUTOMATIC,
331 GTK_POLICY_AUTOMATIC);
332 gtk_box_pack_start (GTK_BOX(vbox), sw, TRUE, TRUE, 8);
333
334 /* add the treeview to the scrollable window */
335 dlg->treeview = gtk_assert_dialog_create_backtrace_list_model ();
336 gtk_widget_set_size_request (GTK_WIDGET(dlg->treeview), -1, 180);
337 gtk_container_add (GTK_CONTAINER (sw), dlg->treeview);
338
339 /* create button's hbox */
340 hbox = gtk_hbutton_box_new ();
341 gtk_box_pack_end (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
342 gtk_button_box_set_layout (GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
343
344 /* add the buttons */
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 button = gtk_assert_dialog_add_button_to (GTK_BOX(hbox), "Copy to clip_board",
351 GTK_STOCK_COPY, GTK_RESPONSE_NONE);
352 g_signal_connect (button, "clicked", G_CALLBACK(gtk_assert_dialog_copy_callback), dlg);
353 }
354
355 /* add the checkbutton */
356 dlg->shownexttime = gtk_check_button_new_with_mnemonic("Show this _dialog the next time");
357 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(dlg->shownexttime), TRUE);
358 gtk_box_pack_end (GTK_BOX(GTK_DIALOG(dlg)->action_area), dlg->shownexttime, FALSE, TRUE, 8);
359
360 /* add the stop button */
361 gtk_assert_dialog_add_button (dlg, "_Stop", GTK_STOCK_QUIT, GTK_ASSERT_DIALOG_STOP);
362
363 /* add the continue button */
364 continuebtn = gtk_assert_dialog_add_button (dlg, "_Continue", GTK_STOCK_YES, GTK_ASSERT_DIALOG_CONTINUE);
365 gtk_dialog_set_default_response (GTK_DIALOG (dlg), GTK_ASSERT_DIALOG_CONTINUE);
366 g_signal_connect (continuebtn, "clicked", G_CALLBACK(gtk_assert_dialog_continue_callback), dlg);
367
368 /* complete creation */
369 dlg->callback = NULL;
370 dlg->userdata = NULL;
371
372 /* the resizeable property of this window is modified by the expander:
373 when it's collapsed, the window must be non-resizeable! */
374 gtk_window_set_resizable (GTK_WINDOW (dlg), FALSE);
375 gtk_widget_pop_composite_child ();
376 gtk_widget_show_all (GTK_WIDGET(dlg));
377 }
378
379
380
381 /* ----------------------------------------------------------------------------
382 GtkAssertDialog public API
383 ---------------------------------------------------------------------------- */
384
385 gchar *gtk_assert_dialog_get_message (GtkAssertDialog *dlg)
386 {
387 /* NOTES:
388 1) returned string must g_free()d !
389 2) Pango markup is automatically stripped off by GTK
390 */
391 return g_strdup (gtk_label_get_text (GTK_LABEL(dlg->message)));
392 }
393
394 gchar *gtk_assert_dialog_get_backtrace (GtkAssertDialog *dlg)
395 {
396 gchar *function, *arguments, *sourcefile, *linenum;
397 guint count;
398
399 GtkTreeModel *model;
400 GtkTreeIter iter;
401 GString *string;
402
403 g_return_val_if_fail (GTK_IS_ASSERT_DIALOG (dlg), NULL);
404 model = gtk_tree_view_get_model (GTK_TREE_VIEW(dlg->treeview));
405 string = g_string_new("");
406
407 /* iterate over the list */
408 if (!gtk_tree_model_get_iter_first (model, &iter))
409 return NULL;
410
411 do
412 {
413 /* append this stack frame's info to the string */
414 gtk_tree_model_get (model, &iter,
415 STACKFRAME_LEVEL_COLIDX, &count,
416 FUNCTION_NAME_COLIDX, &function,
417 FUNCTION_ARGS_COLIDX, &arguments,
418 SOURCE_FILE_COLIDX, &sourcefile,
419 LINE_NUMBER_COLIDX, &linenum,
420 -1);
421
422 g_string_append_printf (string, "[%u] %s(%s)",
423 count, function, arguments);
424 if (sourcefile[0] != '\0')
425 g_string_append_printf (string, " %s", sourcefile);
426 if (linenum[0] != '\0')
427 g_string_append_printf (string, ":%s", linenum);
428 g_string_append (string, "\n");
429
430 g_free (function);
431 g_free (arguments);
432 g_free (sourcefile);
433 g_free (linenum);
434
435 } while (gtk_tree_model_iter_next (model, &iter));
436
437 /* returned string must g_free()d */
438 return g_string_free (string, FALSE);
439 }
440
441 void gtk_assert_dialog_set_message(GtkAssertDialog *dlg, const gchar *msg)
442 {
443 /* prepend and append the <b> tag
444 NOTE: g_markup_printf_escaped() is not used because it's available
445 only for glib >= 2.4 */
446 gchar *escaped_msg = g_markup_escape_text (msg, -1);
447 gchar *decorated_msg = g_strdup_printf ("<b>%s</b>", escaped_msg);
448
449 g_return_if_fail (GTK_IS_ASSERT_DIALOG (dlg));
450 gtk_label_set_markup (GTK_LABEL(dlg->message), decorated_msg);
451
452 g_free (decorated_msg);
453 g_free (escaped_msg);
454 }
455
456 void gtk_assert_dialog_set_backtrace_callback(GtkAssertDialog *assertdlg,
457 GtkAssertDialogStackFrameCallback callback,
458 void *userdata)
459 {
460 assertdlg->callback = callback;
461 assertdlg->userdata = userdata;
462 }
463
464 void gtk_assert_dialog_append_stack_frame(GtkAssertDialog *dlg,
465 const gchar *function,
466 const gchar *arguments,
467 const gchar *sourcefile,
468 guint line_number)
469 {
470 GtkTreeModel *model;
471 GtkTreeIter iter;
472 GString *linenum;
473 gint count;
474
475 g_return_if_fail (GTK_IS_ASSERT_DIALOG (dlg));
476 model = gtk_tree_view_get_model (GTK_TREE_VIEW(dlg->treeview));
477
478 /* how many items are in the list up to now ? */
479 count = gtk_tree_model_iter_n_children (model, NULL);
480
481 linenum = g_string_new("");
482 if ( line_number != 0 )
483 g_string_printf (linenum, "%u", line_number);
484
485 /* add data to the list store */
486 gtk_list_store_append (GTK_LIST_STORE(model), &iter);
487 gtk_list_store_set (GTK_LIST_STORE(model), &iter,
488 STACKFRAME_LEVEL_COLIDX, count+1, /* start from 1 and not from 0 */
489 FUNCTION_NAME_COLIDX, function,
490 FUNCTION_ARGS_COLIDX, arguments,
491 SOURCE_FILE_COLIDX, sourcefile,
492 LINE_NUMBER_COLIDX, linenum->str,
493 -1);
494
495 g_string_free (linenum, TRUE);
496 }
497
498 GtkWidget *gtk_assert_dialog_new(void)
499 {
500 GtkAssertDialog *dialog = g_object_new (GTK_TYPE_ASSERT_DIALOG, NULL);
501
502 return GTK_WIDGET (dialog);
503 }
504
505 #ifdef __cplusplus
506 }
507 #endif /* __cplusplus */