Foundation of Control Programming
There are predefined controls in a relatively complex GUI system, and they are the main elements for the human-machine interaction. This chapter will illustrate what is the control and control class, and briefly introduce the predefined control classes in MiniGUI.
Control and Control Class
The concept of controls (or widgets) is well known. The control can be interpreted as the child window in the main window. The behaviors of these child windows, as well as that of the main window, both can accept the exterior input such as the keyboard and the mouse, and can output in their own regions - but all the actions are restricted within the main window. MiniGUI also supports child windows, and can create nested child windows in the child window. We refer to all the child windows in MiniGUI as controls.
In Window or X Window, the system will predefine some control classes. When you use a certain control class to create controls, all the controls belonging to this control class will have the same behaviors and appearances. With this technique, the consistent human-machine interaction interface can be ensured, and the program can construct the graphics user interface as building toy bricks. MiniGUI uses the concept of control and control class, and can conveniently overload the existed control to make it have some special effects. For example, when an edit box which only allows digit to input in, you can realize it by overloading the existed EDIT control class, instead of developing a new control class.
If you have the experience of developing a Windows application, you should remember that before creating a new window, you must assure that the corresponding window class of this new window has existed in the system. In Windows operating system, each window created by the program corresponds to a certain window class. This concept is similar to the relationship between the class and the object in the object-oriented programming. Referring to the terms of object oriented, each window in Windows is an instance of a certain window class. Similar concepts are present in the X Window programming; for example, each widget created is actually an instance of a certain widget class.
Thus, if a program needs to create a window, it should first be sure to select the correct window class, because each window class determines the appearances and behaviors of the corresponding window instances. Herein the presentation means the appearance of the window, such as the width of the window border, whether or not the caption bar exists, etc. The behavior means the response of the window to the user input. Each GUI system will predefine some window classes. Examples of the common window classes include the button, list box, scroll bar, edit box, etc. If the window to be created by the program is very special, you can first register your own window class, and then create an instance of this window class. Thus the code reusability is improved substantially.
In MiniGUI, the main window is generally considered as a special window. Since the reusability of the main window code is usually very low, it will cause additional unnecessary memory space if we register a window class for each main window in a usual way. So we do not provide window class support in the main window, but all the child windows, i.e. controls, in the main window support the concept of the window class (control class). MiniGUI provides the predefined control classes commonly used, including button (including radio button, check box), static control, list box, progress bar, track bar, edit box, etc. A program can customize its own control class, register the control class and then create the corresponding control instances. Table 1 lists the predefined control classes and the corresponding class names in MiniGUI.
Table 1 Control class and corresponding class name predefined in MiniGUI
Control Class
Class Name
C Macro for Control Name
Note
Multi-Line Edit Box
"mledit"
CTRL_MLEDIT
(same with TRL_TEXTEDIT)
Creating Control Instance by Using Predefined Control Class
In MiniGUI, by calling the CreateWindow function (the CreateWindow is in fact a macro of the CreateWindowEx function) can create an instance of a certain control class. The control class can be either the predefined MiniGUI control classes in Table 1, or a user-defined control class. Following are the prototypes of several functions related to CreateWindow function (minigui/window.h):
The CreateWindow function creates a child window, i.e. a control. It specifies the control class (class_name), the control caption (caption), the control style (style), the control identifier (id), and the initial position and size of the control (x, y, w, h). This function also specifies the parent window (parent) of the child window. The parameter add_data is used to pass a special data to the control, and the data structures pointed to by this pointer are different for different control classes.
The affection of the CreateWindowEx function is same as CreateWindow function; however, the CreateWindowEx can be used to specify the extended style (dwExStyle) of the control.
CreateWindowEx2 function is a new added function in MiniGUI 3.0. It is based on CreateWindowEx and has two new arguments: werdr_name and we_attrs, they specify the name and the property of renderer for control.
The DestroyWindow function is used to destroy the control or the child window created by above two functions.
The program in List 1 creates several types of controls by using the predefined control classes: static control, button and single-line edit box. Among them hStaticWnd1 is the static control created in the main window hWnd; hButton1, hButton2, hEdit1, and hStaticWnd2 are several controls created in hStaicWnd1, and existing as the child controls of hStaticWnd1; While hEdit2 is the child control of hStaicWnd2, and is the grandchild control of hStaticWnd1.
List 1 Creating controls using the predefined control classes
Topics Involved in Control Programming
In control programming, besides creating and destroying of the control, following subjects are usually involved:
The control has its own window style definition. You need to specify the needed style when creating the control. Different styles will result in different appearance and behavior.
Getting or setting the status and contents and so on of the control. In general, sending some general or special messages to the control can complete this. In addition, the general functions for windows are usually suitable for the controls, such as
ShowWindow,MoveWindow,EnableWindow,SetWindowFont, etc.Informing the parent window by notification messages when a certain event happens in the control. Notification messages are generally sent by
MSG_COMMANDmessages.WParamparameter of this message consists of the window identifier and the notification code, andlParamincludes the handle of the control sending the notification messages. For example, when users change the contents of an edit box, the edit box will sendEN_CHANGEnotification message to the parent window. If the window procedure of the parent window needs to know this change, the notification message should be handled in the window procedure of the parent window as follows:
MiniGUI V1.2.6 introduces
SetNotificationCallbackfunction for the notification message handling of control. This function can set a callback function of the notification message for a control. When the control has a notification message, it will call the callback function, instead of sending the notification message to the parent window. New applications should use this function as far as possible to handle notification messages of a control to get a good program structure. All the sample programs for this guide use this interface to handle notification messages of a control.
The function in List 2 creates a simple dialog box by using the predefined control classes. When the user inputs data in unit of millimeter (mm) into the edit box, the system will display the corresponding data in unit of inch in the static control below the edit box, and return the data input by the user to the function calling the dialog box when the user select “OK” button.
List 2 Realizing a simple input dialog box using the predefined controls
The running effect of the program in List 2 is shown in Figure 1. Please refer to the input.c file of the sample program package for this guide to get the complete source code of the program.
Figure 1 A simple input dialog box
In the Part IV, we will introduce the predefined controls of MiniGUI. We will describe all the predefined controls in three aspects: the purpose and usage of the control, the styles, messages, and notification codes. We will also give the sample code of the controls.
Operations Specific to Control
MiniGUI provides some operation functions specific to control, as show in Table 2.
Table 2 Operation functions specific to control
Function
Purpose
Comment
GetNotificationCallback
Gets the notification callback procedure of a control
Appear in MiniGUI version 1.2.6
SetNotificationCallback
Sets the notification callback procedure of a control
^
NotifyParentEx
Sends a notification message to the parent
<< Foundation of Dialog Box Programming | Table of Contents | Advanced Programming of Control >>
Last updated