Search code examples
rubywindowmodal-dialogfxruby

FXRuby intercept window close


I've created a 'confirm exit' dialog box to prompt the user when exiting. I've successfully connected it to an 'exit' menu command, but I also want to connect it to the window-close (X) button. How can I do this? I've had some experience with Java Swing, and to accomplish this task you had to add a window listener to the frame that would call this prompt. Is there something similar I must do here?


Solution

  • Do it like this:

    require 'fox16'
    include Fox
    
    class MyApp < FXMainWindow
      def initialize(app)
        @app = app
        super(app, "Test", :height => 150, :width => 350, :opts=> DECOR_ALL)
        self.connect(SEL_CLOSE, method(:on_close))
      end
    
      def create
        super
        show(PLACEMENT_SCREEN)
      end
    
      def on_close(sender, sel, event)
        q = FXMessageBox.question(@app, MBOX_YES_NO, "Sure?", "You sure?")
        if q == MBOX_CLICKED_YES
          getApp().exit(0)
        end
      end
    end
    
    FXApp.new do |app|
      MyApp.new(app)
      app.create
      app.run
    end