|By Brad Wery||
|October 24, 2005 02:30 PM EDT||
There's a reason why software developers like Microsoft update the look of their applications every two years. It shows the consumer that something has changed and therefore they need to upgrade, at least that's my opinion.
If this is true, I can only presume that presentation plays a vital role in the sale of commercial applications. If you're not writing commercial applications, then it's just a cool thing to do.
Unfortunately, most developers are usually confined to creating applications with the look provided by the development tool they're using. Fortunately for us developers, some people don't like this kind of confinement.
We all know who Aart Onkenhout is: the creator of CoolMenu. For those who are unaware, CoolMenu is a utility that allows us to update the look of the menus in our applications by adding color and bitmaps. It also provides style options: XP and Office 2003, for example.
Personally I like the Office 2003 style. What I don't like is the contrast between the new menu and the old toolbar.
During my last personal programming challenge I needed to display some dropdowns on my application's toolbar. Of course, in order to achieve this, you would have to create your own. I now had the opportunity to upgrade my application's toolbar to match my new Office 2003 menu.
Before I started developing, I had to accept the fact that it would not behave exactly like the toolbars do in other applications. My goal was to make it as similar as possible.
The sample I created contains more code and explanation than can fit in one article. So, instead of constructing a toolbar with step-by-step instructions, go to zip.html and download the source code I've provided. I'll proceed by giving a guided tour of the objects that I created, explaining as best I can what is happening.
When looking at the source you will find three main objects:
- A toolbar manager (n_cst_toolbarmanager)
- A toolbar container (w_toolbar)
- A toolbar (d_toolbar...)
Toolbar Manager - n_cst_toolbarmanager
This is where the magic happens. Your application can have many toolbars. In order to have the numerous toolbars work well together, they need to be managed. Below is a list of functions and an explanation as to how they manage and manipulate the open toolbars:
- of_GetToolbarHeight: This function returns the height of the toolbar area. The toolbar area is the space between the menu and the MDI client.
- of_GetToolbar: Used to obtain a reference to a specific toolbar.
- of_ArrangeToolbars: This function is the bulk of the object. After a toolbar has been moved, this function is called to reorganize the toolbar area. If toolbars overlap, then they are repositioned. If bands become empty (i.e., you make the toolbar float), they're removed and all items lower are moved up.
- of_GetToolbarPos: The name of this function is a bit misleading. It actually returns the index position of the requested toolbar in the toolbar array (iw_toolbar). This function is only used internally.
- of_GetBandWidth: Returns the total width of the toolbars in a band. Used to determine where free space starts on the right side of the last toolbar in a band.
- of_OpenToolbar: This function will open a toolbar and position it in the specified band.
- of_ResizeMDIClient: Used to size the MDI frames' client area as required. This function will be called from the frames' resize event.
- of_SetEnabled: Toolbar items can be made enabled or disabled. Use this function to do so, passing true for enabled and false for disabled.
- of_ToolbarExistsBelow: Used to determine if a toolbar was moved over the top of another toolbar. This function is used for internal purposes only.
- of_RemoveBand: This function is called to remove any bands that are no longer occupied by a toolbar.
- of_AddBand: Used to create new bands in the toolbar area.
- of_GetBandCount: Returns the number of bands in the toolbar area.
Toolbar Container - w_toolbar
w_toolbar contains a lot of generic code. Most of the code is used to control the toolbar's visual effects and behaviors. After running the sample application you'll notice several things:
- Toolbar items are highlighted when you move the mouse over them.
- Toolbar items change color when pressed.
- You can reposition the toolbars within the toolbar area.
- Dragging the toolbar out of the toolbar area makes it a floating toolbar.
- Floating toolbars have a title.
Making a toolbar item highlight when the mouse pointer is positioned over it doesn't take much. Code in the mousemove event of dw_1 makes a bitmap visible or invisible. Each item has this bitmap positioned behind it. The bitmap is created dynamically for each toolbar item (objects prefixed with tbi_).
Repositioning the toolbar is achieved by sending a move message to w_toolbar when the handle is clicked. The move message will process until the left-mouse button is released. Consequently the toolbar can be dropped anywhere so it's important to reposition it so that it displays properly and orderly. This is done by calling of_ArrangeToolbars in the toolbar manager.
If you drag the toolbar out of the toolbar area, it floats. Once a toolbar floats, a title is displayed letting you know its name\window title. You can determine if it's floating by calling of_IsDocked.
When a toolbar item is pressed, a user event on dw_1 called ue_buttonclicked is triggered. This event has one argument passed to it: the name of the toolbar item clicked. In this event you would most likely code a case statement triggering menu items as necessary. If you have data entry fields on the toolbar, use the DataWindows itemchanged event to determine the next course of action.
Toolbar - d_toolbar...
The toolbar itself is nothing more than a dataobject containing many images. These images are used to represent the toolbar items and other graphical components of the toolbar. The nice thing about using a dataobject is that you can make use of its data entry and retrieval elements. It would be easy to retrieve dropdown lists or allow a user to enter information into free form or edit masked fields.