[wxruby-users] wxRuby dialog separate from main Ruby script execution

David Muir lists at ruby-forum.com
Fri Jan 16 07:19:39 EST 2009


Hi guys,

I had some success yesterday so thought I should complete this thread by 
showing how I got it working.

The LogWindowDialog class is not included because it includes a lot of 
details that are irrelevant for the topic.  In that class I create a 
Log4r outputter and formatter to pass LogEvent objects straight to the 
dialog which are then serialised and output into a TextCtrl widget.

Seems to work fairly well with the few examples I've tried.  If the Ruby 
script kicks off an external app the UI will be unresponsive.

Code below.  Hope it helps others.
Cheers,
Dave

#
# == Description
# Generic log window dialog that integrates with our Pipeline::Log
# objects for displaying log messages to users.
#
# === Example Usage
#   # To auto-popup the display of the log dialog:
#   Pipeline::GUI::LogWindow::show_dialog( true ) do
#     # Do something that will log messages, dialog only displayed on
#     # errors.
#   end
#
#   # To force the display of the log dialog:
#   Pipeline::GUI::LogWindow::show_dialog( ) do
#      # Do something that will log messages
#   end
#
class LogWindow < Wx::App

  #---------------------------------------------------------------------
  # Class Methods
  #---------------------------------------------------------------------
  def initialize( log, auto_popup, title = 'Log', &block )
    super( )
    @log = log
    @auto_popup = auto_popup
    @title = title
    @proc = Proc.new( ) do
      yield
    end
  end

  #
  # Use this method to explicitly display a log dialog box.  Only
  # log messages generated after this call will be displayed.
  #
  def LogWindow::show_dialog( auto_popup = false, log = 
LogSystem::instance().rootlog, title = 'Log' )
    begin
      dlg = LogWindow.new( log, auto_popup, title ) do
        yield if ( block_given? )
      end
      dlg.main_loop( )
    rescue Exception => ex
      throw ex unless ( 'exit' == ex.message )
    end
  end

  #---------------------------------------------------------------------
  # Instance Methods
  #---------------------------------------------------------------------
  def on_init( )
    Wx::Timer::every( 25 ) do
      Thread::pass( )
    end
    dlg = LogWindowDialog::new( @log, @title, @auto_popup )
    dlg.show( ) unless ( @auto_popup )

    script_thread = Thread.new do
      @proc.call( )
      exit( ) if ( @auto_popup and ( not dlg.is_shown ) )
    end
    script_thread.join( )
  end
end

#
# == Description
# Log window dialog.
#
class LogWindowDialog < Wx::Dialog
  ...
end
-- 
Posted via http://www.ruby-forum.com/.


More information about the wxruby-users mailing list