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