handle save of modifications when opening a new file
authorgc <gc>
Mon, 23 May 2005 19:56:47 +0000 (19:56 +0000)
committergc <gc>
Mon, 23 May 2005 19:56:47 +0000 (19:56 +0000)
bin/booh

index 9707fa3488777d25248251fc6dbec641736eacf0..d64cc446f15149550e2ecc2e4183d38eadb7e24e 100755 (executable)
--- a/bin/booh
+++ b/bin/booh
@@ -996,15 +996,27 @@ def save_current_file
     end
 end
 
-def try_quit
+#- ret: true => ok  false => cancel
+def ask_save_modifications(msg1, msg2, *options)
+    ret = true
+    options = options.size > 0 ? options[0] : {}
     if $modified
-        dialog = Gtk::Dialog.new(utf8(_("Save before quitting?")),
-                                 $main_window,
-                                 Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
-                                 [Gtk::Stock::CLOSE, Gtk::Dialog::RESPONSE_NO],
-                                 [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
-                                 [Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_YES])
-        dialog.vbox.add(Gtk::Label.new(utf8(_("Do you want to save your changes before quitting?"))))
+        if options[:disallow_cancel]
+            dialog = Gtk::Dialog.new(msg1,
+                                     $main_window,
+                                     Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
+                                     [options[:no] || Gtk::Stock::CLOSE, Gtk::Dialog::RESPONSE_NO],
+                                     [options[:yes] || Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_YES])
+        else
+            dialog = Gtk::Dialog.new(msg1,
+                                     $main_window,
+                                     Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
+                                     [options[:cancel] || Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
+                                     [options[:no] || Gtk::Stock::CLOSE, Gtk::Dialog::RESPONSE_NO],
+                                     [options[:yes] || Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_YES])
+        end
+        dialog.default_response = Gtk::Dialog::RESPONSE_YES
+        dialog.vbox.add(Gtk::Label.new(msg2))
         dialog.window_position = Gtk::Window::POS_CENTER
         dialog.show_all
         
@@ -1012,13 +1024,18 @@ def try_quit
             dialog.destroy
             if response == Gtk::Dialog::RESPONSE_YES
                 save_current_file
-                Gtk.main_quit
-            elsif response == Gtk::Dialog::RESPONSE_NO
-                Gtk.main_quit
+            elsif response == Gtk::Dialog::RESPONSE_CANCEL
+                ret = false
             end
         }
+    end
+    return ret
+end
 
-    else
+def try_quit(*options)
+    if ask_save_modifications(utf8(_("Save before quitting?")),
+                              utf8(_("Do you want to save your changes before quitting?")),
+                              *options)
         Gtk.main_quit
     end
 end
@@ -1673,6 +1690,11 @@ def open_file(filename)
 end
 
 def open_file_popup
+    if !ask_save_modifications(utf8(_("Save this album?")),
+                               utf8(_("Do you want to save the changes to this album?")),
+                               { :yes => Gtk::Stock::YES, :no => Gtk::Stock::NO })
+        return
+    end
     fc = Gtk::FileChooserDialog.new(utf8(_("Open file")),
                                     nil,
                                     Gtk::FileChooser::ACTION_OPEN,
@@ -2251,11 +2273,15 @@ Copyright (c) 2005 Guillaume Cottenceau") % $VERSION), { :centered => true, :pos
             $config['last-opens'].reverse.each { |e|
                 lastopens.attach(item = Gtk::ImageMenuItem.new(e, false), 0, 1, j, j + 1)
                 item.signal_connect('activate') {
-                    push_mousecursor_wait
-                    msg = open_file(from_utf8(e))
-                    pop_mousecursor
-                    if msg
-                        show_popup($main_window, msg)
+                    if ask_save_modifications(utf8(_("Save this album?")),
+                                              utf8(_("Do you want to save the changes to this album?")),
+                                              { :yes => Gtk::Stock::YES, :no => Gtk::Stock::NO })
+                        push_mousecursor_wait
+                        msg = open_file(from_utf8(e))
+                        pop_mousecursor
+                        if msg
+                            show_popup($main_window, msg)
+                        end
                     end
                 }
                 j += 1
@@ -2455,7 +2481,7 @@ def create_main_window
     $main_window = Gtk::Window.new
     $main_window.add(main_vbox)
     $main_window.signal_connect('delete-event') {
-        try_quit
+        try_quit({ :disallow_cancel => true })
     }
 
     #- read/save size and position of window