touque.ca > Education Commons > Turing

GUI Programming

Vocabulary

interface:
point at which two systems interact
human-computer interface:
commonly called “interface“ or “user interface“
command-line interface:
an interface which requires the user to type precise commands in order to command the computer; examples: DOS, Unix, Linux
graphical user interface (GUI):
an interface which requires the user to activate widgets in order to command the computer
widget:
an element of a GUI; examples: menus, buttons, check boxes, text fields

Widgets

Turing identifies widgets by integer ID number. All of the subprograms which create a widget (example: GUI.CreateButton) return an integer. This number is the ID of the widget that has been created.

Most widgets are associated with a programmer-defined subprogram which is called into action whenever the widget is activated. Until a widget is activated, no action occurs. The procedure called when a widget is activated is called the widget’s action procedure.

Event processing

After all of the widgets have been created, the program must repeatedly call GUI.ProcessEvent until the function returns true. GUI.ProcessEvent returns true when GUI.Quit has been called, otherwise it returns false.

   % Listen for widget activation until GUI.Quit is called.
   loop
       exit when GUI.ProcessEvent
   end loop

When a user signals “done” (for example, by clicking a “Quit” or “Exit” button), the program should call the GUI.Quit procedure. This will cause the GUI.ProcessEvent loop to exit. The program can then display final results (if appropriate), close files, release fonts, and display a closing message.

Interface design

To change the background colour of a window, use the GUI.SetBackgroundColour procedure, not colourback. GUI.SetBackgroundColour “redraws the window in the background colour and then redraws all the widgets. It also notifies the widgets about the new background colour so that when the widget is erased or moved, the location of the widget is filled with the new background colour instead of white” (Turing Reference Manual: The GUI Module).

By default, newly created widgets are both visible and enabled. At any time, widgets can be hidden (GUI.Hide) or made visible (GUI.Show), disabled (GUI.Disable) or enabled (GUI.Enable).

When widgets are no longer needed, they should be disposed of (GUI.Dispose).

Background & structure

The programs we wrote when first studying programming are called console programs. These usually present text, prompt the user, accept the user’s response, display a result, and repeat as appropriate. Console programs, sometimes called command-line programs, “drive the user.”

In contrast, GUI programs are “driven by the user”: they present an interface composed of one or more widgets and wait for an event like the activation of a widget. When a widget is activated, its action procedure is called, and then the program waits for another event.

GUI programs are written all in procedures and functions; the main program consists entirely (or almost entirely) of procedure and function calls. This means that the declaration section of a GUI program is quite large and the main program quite small.

Procedures & functions

Since procedures perform a specific task, their names start with lowercase verbs. For example: findBiggestValue. Since functions return a value, their names (which also start in lowercase) describe the value returned. For example: biggestValue.

Procedures and functions must be declared before they can be used. Their declarations appear, in alphabetical order, after the alphabetical declaration of constants and the alphabetical declaration of variables.

Sometimes a procedure or function which is declared early may itself call a procedure or function which is declared late. For example, if there are three procedures called doTaskA, doTaskB, and doTaskC, and if doTaskA calls doTaskC, then when the compiler reaches the declaration of doTaskA, it will complain that it knows nothing about a procedure called doTaskC, because doTaskC has not yet been declared. In contrast, if doTaskC calls doTaskB, there’s no problem, because by the time the compiler gets to the declaration of doTaskC, doTaskB has already been declared.

To get around this potential problem, every procedure or function is declared in two parts: its forward and its body. For example:

forward procedure doTaskA
forward procedure doTaskB
forward procedure doTaskC

body procedure doTaskA
   % some code
   doTaskC
   % some more code
end procedure doTaskA

body procedure doTaskB
   % some code
end procedure doTaskB

body procedure doTaskC
   % some code
   doTaskB
   % some more code
end procedure doTaskC

For every forward declared in alphabetical order, there must be a corresponding body declared in alphabetical order.

touque.ca > Education Commons > Turing

[This page last updated 2020-12-23 at 12h13 Toronto local time.]