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