Applications

A Minimal Application

Note

Describe what a minimal application consists of. Create a standard application and attach a single frame window. Allow the application to close.

The simplest application in Vision consists of an instance of EV_APPLICATION and a single window, typically an instance of EV_FRAME. The application object initializes the window and starts the event processing loop. Example 1 shows a minimal application class.

Example 1. Minimal Application

class APPLICATION
	
inherit 

	EV_APPLICATION                                            (1)
	
creation

	start

feature -- Initialization

	start is
			-- Create the application, set up `main_window'
			-- and then launch the application.
		do
			default_create                                          (2)
			create main_window
			main_window.show                                        (3)
			launch                                                  (4)
		end
					
feature {NONE} -- Implementation
			
	main_window: MAIN_WINDOW
			-- Main window.

end -- class APPLICATION
	
(1)
An application class must inherit from EV_APPLICATION to provide the initialization routines and event processing functionality.
(2)
The default_create feature must be called to ensure that the widget is initialized correctly. We do this first in the start routine but we could have called it later on in the body. We rely only on the class invariant being satisfied which default_create ensures.
(3)
We then create the main window instance and force it to be shown on the screen by calling its show routine. It is typical to show the main window of an application during the initialization process, however We could have shown the window at a later stage, perhaps as a result of an external event.
(4)
Finally, the event processing loop is started by calling launch from EV_APPLICATION. This causes control to be passed to the underlying windowing system to allow it to process user events for the application.

The corresponding MAIN_WINDOW class is shown in Example 2. The window initializes itself with a ...

Example 2. Minimal Application Window

class MAIN_WINDOW

inherit

	EV_TITLED_WINDOW
		redefine
			initialize
		end
				
feature -- Initialisation

	initialize is
			-- Initialize window.
		local
			environment: EV_ENVIRONMENT
		do
			Precursor {EV_TITLED_WINDOW}
			set_title ("Minimal Application")
			
			-- allow close using X button
			create environment
			close_request_actions.extend (agent (environment.application).destroy)
		end
	
end -- class MAIN_WINDOW
      

The widget created by this application is a single framed window with the title Minimal Application. The window contains no other widgets and is painted grey. You may notice that the size of the window differs depending on your computer platform. The source code did not explicitly set the initial size of the window and because of this, the underlying windowing system has used its own default size. On Windows, the window is sized so that the title bar and border are the only visible elements. On GTK, the central panel of the window is, in this case, sized to 200 by 200 pixels. This highlights that fact that a number of underlying windowing system differences and anomolies can surface even when using a library such as Vision 2. Differences such as this can be easily overcome. In this case, all we need to do is to give the window an initial size in the initialize routine using the set_size routine. Simple as these problems are to fix, you still need to test an application on all target platforms to discover them. The GTK version of this window is shown in Figure 1.

Figure 1. Minimal Application Window on GTK