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