The App class represents the whole GUI application itself. It is the container within which all GUI code runs. It is used to:
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 rescue.
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().
Creates a new Application, but does not run it; to start the and enter its event loop, call main_loop on the instance.
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.
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();
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.
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).
String get_app_name()
Returns the application name.
Widgets sets this to a reasonable default before calling App#on_init, but the application can reset it at will.
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.
Boolean get_exit_on_frame_delete()
Returns true if the application will exit when the top-level window is deleted, false otherwise.
AppConsole get_instance()
Returns the one and only global application object.
Usually TheApp is usead instead.
Window get_top_window()
Returns a pointer to the top window.
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.
Boolean get_use_best_visual()
Returns true if the application will use the best visual on systems that support different visuals, false otherwise.
String get_vendor_name()
Returns the application’s vendor name.
Boolean is_active()
Returns if the application is active, i.e. if one of its windows is currently in the foreground. If this function returns and you need to attract users attention to the application, you may use TopLevelWindow#request_user_attention to do it.
Boolean is_main_loop_running()
Returns if the main event loop is currently running, i.e. if the application is inside on_run.
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.
Integer main_loop()
Calling this method starts up the application, calling its on_init
method.
Returns 0 under X, and the wParam of the WM_QUIT message under Windows.
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.
nil if just ASSERT or FAIL was usedBoolean on_exception_in_main_loop()
This function is called if an unhandled exception occurs inside the main
application event loop. It can return to ignore the exception and to
continue running the loop or to exit the loop and terminate the
program. In the latter case it can also use C++ throw keyword to
rethrow the current exception.
The default behaviour of this function is the latter in all ports except under Windows where a dialog is shown to the user which allows him to choose between the different options. You may override this function in your class to do something more appropriate.
Finally note that if the exception is rethrown from here, it can be caught in on_unhandled_exception.
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.
on_fatal_exception()
This function may be called if something fatal happens: an unhandled exception under Win32 or a a fatal signal under Unix, for example. However, this will not happen by default: you have to explicitly call HandleFatalExceptions to enable this.
Generally speaking, this function should only show a message to the user and return. You may attempt to save unsaved data but this is not guaranteed to work and, in fact, probably won’t.
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 to continue processing, to exit the application immediately.
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 (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.
on_unhandled_exception()
This function is called when an unhandled C++ exception occurs inside on_run()() (the exceptions which occur during the program startup and shutdown might not be caught at all). Note that the exception type is lost by now, so if you want to really handle the exception you should override on_run()() and put a try/catch clause around the call to the base class version there.
Boolean process_message(MSG msg)
Windows-only function for processing a message. This function is called from the main message loop, checking for windows that may wish to process it. The function returns true if the message was processed, false otherwise. If you use Widgets with another class library with its own message loop, you should make sure that this function is called to allow Widgets to receive messages. For example, to allow co-existence with the Microsoft Foundation Classes, override the PreTranslateMessage function:
// Provide Widgets message loop compatibility
BOOL CTheApp::PreTranslateMessage(MSG msg)
{
if (TheApp && TheApp->ProcessMessage((WXMSW *)msg))
return true;
else
return CWinApp::PreTranslateMessage(msg);
}
Boolean *pending()
Returns true if unprocessed events are in the window system event queue.
Boolean send_idle_events(Window win, IdleEvent event)
Sends idle events to a window and its children.
Please note that this function is internal to Widgets and shouldn’t be used by user code.
These functions poll the top-level windows, and their children, for idle event processing. If true is returned, more OnIdle processing is requested by one or more window.
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.
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.
set_exit_on_frame_delete(Boolean flag)
Allows the programmer to specify whether the application will exit when the top-level frame is deleted.
set_instance(AppConsole app)
Allows external code to modify global TheApp, but you should really
know what you’re doing if you call it.
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.
App#get_top_window, App#on_init
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.
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.
handle_event(EvtHandler handler, EventFunction func,
Event event)
This function simply invokes the given method func of the specified event handler handler with the event as parameter. It exists solely to allow to catch the C++ exceptions which could be thrown by all event handlers in the application in one place: if you want to do this, override this function in your App-derived class and add try/catch clause(s) to it.
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 Thu May 01 00:50:32 +0100 2008]