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