Overview

Toolbar module is encapsulation of the system Toolbar API. The module is designed with following goals in mind:

How it works

Toolbar consist of two sets of buttons – current buttons (buttons currently visible on the toolbar) and available buttons (buttons not currently visible, but user may add them using customization dialog). If you don’t want customization feature you can entirely omit list of available buttons. When user interacts with the toolbar specific event will be fired that you can implement in the body of the toolbar event function.

Each button may have associated image and text. Images are kept in the ImageList. By default you don’t have to specify image list. In that case module will use one of the system image catalogs that contain standard application icons.

To add toolbar in your gui, you use Add function. This function returns handle to newly created toolbar which you must use in all other functions. During creation, you can specify toolbar style: is it going to be flat, customizable, with tooltips etc… You must also specify event function that will be called whenever specific event is fired:

hToolbar := Toolbar_Add(hGui, "OnToolbar", "FLAT TOOLTIPS")

After the toolbar is created you must add buttons to it. You can add buttons one by one, or as group. Each button is described using button definition – list of button characteristics separated by comma, for instance:

Toolbar_Insert(hToolbar, "open, 11, ,dropdown")
Toolbar_Insert(hToolbar, "close")
Toolbar_Insert(hToolbar, "exit, 3")

Above code will add three button to the toolbar hToolbar. First will have caption open, use 11th icon from the image list and be of type dropdown (button that also can display menu). You can omit one or more parameters in button definition as seen in second and third case.

However, you will most likely wish to handle set of button definitions as whole, so there is another syntax. You can use button definition list – list of button definitions, each one in the next line:

btns =
(LTrim
	open	,	,		,dropdown
	close	,
	-
	undo	,4	,disabled	,dropdown
	redo	,5
	---
	state	,11	,checked	,check
)
Toolbar_Insert(hToolbar, btns)

The above code is more intuitive way of creating toolbar. First two buttons (open and close) will get icons 1 and 2 (since I didn’t specify any icon number, module will automatically set the number of line as an icon. You can specify separator using – and you can even set separator size: each – adds 10px to separator, so separator – – – will be of 30px size. Then, you can see that state button is of check type and that is initially checked. Button undo is created as disabled and is of type dropdown (so you can for instance display menu with undo levels). Separators are not counted when icon numbers are automatically set, so if you order icons in image list the way they appear on the toolbar, you can totally omit manual icon specification: first button will get icon number 1, second button icon number 2 and so on…

Once you have buttons on the toolbar, you need the way to handle user events. That’s the purpose of the event function which has following prototype:

OnToolbar(hwnd, event, txt, pos, id) {
	;your code here
}

Above function must be written in order to handle toolbar events. It must have 5 parameters but you can name them whatever you like. Its parameters are, respectively, handle to the toolbar that generated event, event that is fired, position of the button that fired event, its caption and its ID (unique ID that each button has). One typical event handler would look like:

OnToolbar(hwnd, event, txt, pos, id) {
	if (event = "hot")
		return SB_SetText(txt)	;if user is hovering the button with mouse, 
					; set status bar text with the button text.
 
	;display message box about event
	MsgBox Event:  %event%`nPosition:  %pos%`nCaption:  %txt%`n`nID:%id%    
}

That pretty much covers the basics of using Toolbar module and it is everything you need to know to start using it.

Customization

You can let a user customize the toolbar in two different ways: create the toolbar with adjustable style or call Customize function. If you use the style, a user will be able to double click the empty toolbar area to show customization dialog. In the dialog user can reorder buttons or remove them from the toolbar (i.e. put them in the list of available buttons). User can also SHIFT + drag to reorder or remove the button without opening the customization dialog.

Typical scenario consist of buttons currently on the toolbar and the list of available buttons. Both types of buttons are added the same as before with one note – buttons marked with * will be added to the list of the available buttons instead to the toolbar itself:

btns =
(LTrim
	open	,	,		,dropdown
	close	,
	-
	undo	,4	,disabled	,dropdown
	redo	,5
	---
	state	,11	,checked	,check
 
       *print   ,23
       *find    ,13
       *replace ,14
)
Toolbar_Insert(hToolbar, btns)

Above, I add 5 buttons on the toolbar and I make 3 more available (marked with *). There is empty line between current and available buttons (this doesn’t influence parsing as empty lines in the button definition list are simply skipped). You can mix both type of buttons in the button definition list although I suggest to keep them separated with blank line for better visibility.

When application exits, you will most definitely want to save the current state of the toolbar in configuration file so you can start with that toolbar state on the next run. This is the purpose of the Define function. Code

btns := Toolbar_Define(hToolbar)

will save the current button state (in the form of button definition list) to the btns variable. You can then save this variable in a file and load the toolbar from it next time your application is started.

Notes and Tips