Ugly hack to make the Yes button grab the default
[moonshot-ui.git] / src / moonshot-warning-dialog.vala
index 8ec583a..72233a4 100644 (file)
 */
 using Gtk;
 
+static const string GROUP_NAME="WarningDialogs";
+
 // MessageDialog doesn't allow subclassing, so we merely wrap the
 // constructor for it the dialog, and then run it, returning the result.
 class WarningDialog 
 {
+    private static MoonshotLogger _logger = null;
+    private static MoonshotLogger logger()
+        {
+            if (_logger == null) {
+                _logger = get_logger("WarningDialog");
+            }
+            return _logger;
+        }
+
     public static bool confirm(Window parent, string message, string dialog_name)
     {
+
+        if (get_bool_setting(GROUP_NAME, dialog_name, false))
+        {
+            logger().trace(@"confirm: Settings group $GROUP_NAME has 'true' for key $dialog_name; skipping dialog and returning true.");
+            return true;
+        }
+
         Gdk.Color white = make_color(65535, 65535, 65535);
 
         MessageDialog dialog = new Gtk.MessageDialog(parent,
@@ -46,14 +64,25 @@ class WarningDialog
                                                      "");
 
         var content_area = dialog.get_content_area();
-        CheckButton remember_checkbutton;
+        CheckButton remember_checkbutton = null;
 
         if (dialog_name != null && dialog_name != "")
         {
             remember_checkbutton = new CheckButton.with_label(_("Do not show this message again"));
-            remember_checkbutton.set_focus_on_click(false);
-            remember_checkbutton.has_focus = false;
-            content_area.has_focus = true;
+            // remember_checkbutton.set_focus_on_click(false);
+            // remember_checkbutton.set_can_focus(false);
+            // remember_checkbutton.has_focus = false;
+            remember_checkbutton.set_receives_default(false);
+            Container action_area = (Container) dialog.get_action_area();
+
+            // This is awful, because it assumes the Yes button is first in the
+            // children (and for that matter, it assumes there are no intermediate
+            // containers.) But searching for "Yes" in the widget text would
+            // cause localization problems.
+            // TODO: Rewrite to use Dialog instead of MessageDialog?
+            var yes_button = action_area.get_children().first().data;
+            yes_button.grab_default();
+            yes_button.grab_focus();
 
 // Not sure if 0.26 is the minimum for MessageDialog.get_message_area. 0.16 sure isn't :-(
 #if VALA_0_26
@@ -79,6 +108,12 @@ class WarningDialog
         dialog.set_markup(message);
 
         var ret = dialog.run();
+
+        if (ret == Gtk.ResponseType.YES && remember_checkbutton != null && remember_checkbutton.active)
+        {
+            set_bool_setting(GROUP_NAME, dialog_name, true);
+        }
+
         dialog.destroy();
         return (ret == Gtk.ResponseType.YES);
     }