wxRuby Documentation Home

Wx::App

The App class represents the whole GUI application itself. It is the container within which all GUI code runs. It is used to:

Running an app

No GUI code can run before an App starts – all Windows, Frames, Brushes, Pens, Colours, and Icons must be created after the app has been started, otherwise wxRuby will raise an exception. Therefore, a Wx::App is responsible for setting up the initial state of the application’s windows. There are two ways to do this.

The first, simpler way is just to use the Wx::App.run class method. Pass this method a block specifying how to set up the initial windows in the app; this code will run, and the App will then wait for the user to interact with the application:

Wx::App.run do 
  f = Wx::Frame.new(nil, :title => 'basic app')
  f.show
end

The second way is appropriate for more complex applications. In this case, define a subclass of Wx::App, and describe what it should do on start up (display frames) within an on_init method. Then create an instance of this class, and call its main_loop method.

class MyApp < Wx::App
  def on_init
    f = Wx::Frame.new(nil, :title => 'basic app')
    f.show
  end
end
app = MyApp.new
app.main_loop

This method is more suitable for complex apps because it allows you to define global custom methods and behaviours in the App class, and to override the default handling of events or exceptions using filter_event or on_run.

Exception handling

To handle ruby exceptions within wxRuby applications, override on_init to trap those occurring at init time, or on_run to trap exceptions in the main loop. See on_run for an example of exception handling.

Accessing the application instance

Any wxRuby program will have a single instance of Wx::App, which can be accessed once the app has started by the constant Wx::THE_APP, or by the module method Wx::get_app().

Derived from

EvtHandler

Object

See also

App overview

Methods

App.new

Creates a new Application, but does not run it; to start the and enter its event loop, call main_loop on the instance.

App.run

Wx::App.run { ... }

Creates a new App and starts its main loop, using the code in the passed block to set up the initial state of the Windows.

App#dispatch

dispatch()

Dispatches the next event in the windowing system event queue.

This can be used for programming event loops, e.g.

while (app.Pending())
  Dispatch();

See also

App#pending

App#exit_main_loop

exit_main_loop()

Call this to explicitly exit the main message (event) loop. You should normally exit the main loop (and the application) by deleting the top window.

App#filter_event

Integer filter_event(Event event)

This function is called before processing any event and allows the application to preempt the processing of some events. If this method returns $-1$ the event is processed normally, otherwise either true or false should be returned and the event processing stops immediately considering that the event had been already processed (for the former return value) or that it is not going to be processed at all (for the latter one).

App#get_app_name

String get_app_name()

Returns the application name.

Remarks

Widgets sets this to a reasonable default before calling App#on_init, but the application can reset it at will.

App#get_class_name

String get_class_name()

Gets the class name of the application. The class name may be used in a platform specific manner to refer to the application.

See also

App#set_class_name

App#get_exit_on_frame_delete

Boolean get_exit_on_frame_delete()

Returns true if the application will exit when the top-level window is deleted, false otherwise.

See also

App#set_exit_on_frame_delete,

App shutdown overview

App#get_top_window

Window get_top_window()

Returns a pointer to the top window.

Remarks

If the top window hasn’t been set using App#set_top_window, this function will find the first top-level window (frame or dialog) and return that.

See also

set_top_window

App#get_use_best_visual

Boolean get_use_best_visual()

Returns true if the application will use the best visual on systems that support different visuals, false otherwise.

See also

set_use_best_visual

App#get_vendor_name

String get_vendor_name()

Returns the application’s vendor name.

App#is_active

Boolean is_active()

Returns true if the application is active, i.e. if one of its windows is currently in the foreground. If this function returns false and you need to attract users attention to the application, you may use TopLevelWindow#request_user_attention to do it.

App#is_main_loop_running

Boolean is_main_loop_running()

Returns true if the main event loop is currently running, i.e. if the application is inside on_run or main_loop.

This can be useful to test whether the events can be dispatched. For example, if this function returns , non-blocking sockets cannot be used because the events from them would never be processed.

App#main_loop

Integer main_loop()

Calling this method starts up the application, calling its on_init method.

Return value

Returns 0 under X, and the wParam of the WM_QUIT message under Windows.

App#on_assert_failure

on_assert_failure(Char file,  Integer line,  Char cond, 
          Char msg)

This method is called when an assert failure occurs, i.e. a condition specified in the WxWidgets C++ library evaluates to false. It is only called if WxWidgets was compiled in debug mode (when __WXDEBUG__ is defined); note that standard releases of WxRuby are compiled without this option.

Parameters

App#on_exit

Integer on_exit()

Override this member function for any processing which needs to be done as the application is about to exit. OnExit is called after destroying all application windows and controls, but before Widgets cleanup. Note that it is not called at all if on_init failed.

The return value of this function is currently ignored, return the same value as returned by the base class method if you override it.

App#on_init

Boolean on_init()

This must be provided by the application, and will usually create the application’s main window, optionally calling App#set_top_window. You may use on_exit to clean up anything initialized here, provided that the function returns .

Notice that if you want to to use the command line processing provided by Widgets you have to call the base class version in the derived class OnInit().

Return true to continue processing, false to exit the application immediately.

App#on_run

Integer on_run()

This virtual function is where the execution of a program written in Widgets starts. The default implementation just enters the main loop and starts handling the events until it terminates, either because exit_main_loop has been explicitly called or because the last frame has been deleted and get_exit_on_frame_delete flag is true (this is the default).

The return value of this function becomes the exit code of the program, so it should return 0 in case of successful termination.

In wxRuby this is the place for a general exception handler that will catch exception raised in any event handler. Here is an example that will simply print the exception to STDOUT and continue the event loop:

def on_run
  super
rescue Exception => e
  puts e.message
  retry
end

App#pending

Boolean pending()

Returns true if unprocessed events are in the window system event queue.

See also

App#dispatch

App#set_app_name

set_app_name(String name)

Sets the name of the application. The name may be used in dialogs (for example by the document/view framework). A default name is set by Widgets.

See also

App#get_app_name

App#set_class_name

set_class_name(String name)

Sets the class name of the application. This may be used in a platform specific manner to refer to the application.

See also

App#get_class_name

App#set_exit_on_frame_delete

set_exit_on_frame_delete(Boolean flag)

Allows the programmer to specify whether the application will exit when the top-level frame is deleted.

Parameters

See also

App#get_exit_on_frame_delete,

App shutdown overview

App#set_top_window

set_top_window(Window window)

Sets the `top’ window. You can call this from within App#on_init to let Widgets know which is the main window. You don’t have to set the top window; it is only a convenience so that (for example) certain dialogs without parents can use a specific window as the top window. If no top window is specified by the application, Widgets just uses the first frame or dialog in its top-level window list, when it needs to use the top window.

Parameters

See also

App#get_top_window, App#on_init

App#set_vendor_name

set_vendor_name(String name)

Sets the name of application’s vendor. The name will be used in registry access. A default name is set by Widgets.

See also

App#get_vendor_name

App#set_use_best_visual

set_use_best_visual(Boolean flag)

Allows the programmer to specify whether the application will use the best visual on systems that support several visual on the same display. This is typically the case under Solaris and IRIX, where the default visual is only 8-bit whereas certain applications are supposed to run in TrueColour mode.

Note that this function has to be called in the constructor of the App instance and won’t have any effect when called later on.

This function currently only has effect under GTK.

Parameters

App#yield

Boolean yield(Boolean onlyIfNeeded = false)

Yields control to pending messages in the windowing system. This can be useful, for example, when a time-consuming process writes to a text window. Without an occasional yield, the text window will not be updated properly, and on systems with cooperative multitasking, such as Windows 3.1 other processes will not respond.

Caution should be exercised, however, since yielding may allow the user to perform actions which are not compatible with the current task. Disabling menu items or whole menus during processing can avoid unwanted reentrance of code: see ::SafeYield for a better function.

Note that Yield() will not flush the message logs. This is intentional as calling Yield() is usually done to quickly update the screen and popping up a message box dialog may be undesirable. If you do wish to flush the log messages immediately (otherwise it will be done during the next idle loop iteration), call Log#flush_active.

Calling Yield() recursively is normally an error and an assert failure is raised in debug build if such situation is detected. However if the onlyIfNeeded parameter is true, the method will just silently return false instead.

[This page automatically generated from the Textile source at Wed Sep 09 02:21:00 +0100 2009]