Ugly hack to make the Yes button grab the default
[moonshot-ui.git] / src / moonshot-warning-dialog.vala
1 /*
2  * Copyright (c) 2011-2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31 */
32 using Gtk;
33
34 static const string GROUP_NAME="WarningDialogs";
35
36 // MessageDialog doesn't allow subclassing, so we merely wrap the
37 // constructor for it the dialog, and then run it, returning the result.
38 class WarningDialog 
39 {
40     private static MoonshotLogger _logger = null;
41     private static MoonshotLogger logger()
42         {
43             if (_logger == null) {
44                 _logger = get_logger("WarningDialog");
45             }
46             return _logger;
47         }
48
49     public static bool confirm(Window parent, string message, string dialog_name)
50     {
51
52         if (get_bool_setting(GROUP_NAME, dialog_name, false))
53         {
54             logger().trace(@"confirm: Settings group $GROUP_NAME has 'true' for key $dialog_name; skipping dialog and returning true.");
55             return true;
56         }
57
58         Gdk.Color white = make_color(65535, 65535, 65535);
59
60         MessageDialog dialog = new Gtk.MessageDialog(parent,
61                                                      Gtk.DialogFlags.DESTROY_WITH_PARENT,
62                                                      Gtk.MessageType.WARNING,
63                                                      Gtk.ButtonsType.YES_NO,
64                                                      "");
65
66         var content_area = dialog.get_content_area();
67         CheckButton remember_checkbutton = null;
68
69         if (dialog_name != null && dialog_name != "")
70         {
71             remember_checkbutton = new CheckButton.with_label(_("Do not show this message again"));
72             // remember_checkbutton.set_focus_on_click(false);
73             // remember_checkbutton.set_can_focus(false);
74             // remember_checkbutton.has_focus = false;
75             remember_checkbutton.set_receives_default(false);
76             Container action_area = (Container) dialog.get_action_area();
77
78             // This is awful, because it assumes the Yes button is first in the
79             // children (and for that matter, it assumes there are no intermediate
80             // containers.) But searching for "Yes" in the widget text would
81             // cause localization problems.
82             // TODO: Rewrite to use Dialog instead of MessageDialog?
83             var yes_button = action_area.get_children().first().data;
84             yes_button.grab_default();
85             yes_button.grab_focus();
86
87 // Not sure if 0.26 is the minimum for MessageDialog.get_message_area. 0.16 sure isn't :-(
88 #if VALA_0_26
89             var message_area = dialog.get_message_area();
90             ((Box)message_area).pack_start(remember_checkbutton, false, false, 12);
91 #else
92             HBox hbox = new HBox(false, 0);
93             hbox.pack_start(new HBox(false, 0), true, true, 20);
94             hbox.pack_start(remember_checkbutton, false, false, 12);
95             ((Box)content_area).pack_start(hbox, true, true, 12);
96 #endif
97         }
98
99         // dialog.set_modal(true);
100         dialog.set_title(_("Warning"));
101         dialog.modify_bg(StateType.NORMAL, white);
102
103         // ((Box) content_area).set_spacing(12);
104         content_area.modify_bg(StateType.NORMAL, white);
105
106         content_area.show_all();
107
108         dialog.set_markup(message);
109
110         var ret = dialog.run();
111
112         if (ret == Gtk.ResponseType.YES && remember_checkbutton != null && remember_checkbutton.active)
113         {
114             set_bool_setting(GROUP_NAME, dialog_name, true);
115         }
116
117         dialog.destroy();
118         return (ret == Gtk.ResponseType.YES);
119     }
120 }