Course list http://www.c-jump.com/bcc/
In this exercise we add a menu to the Scratch Pad window:
We discuss FLTK classes and callback functions that support user interaction with the menus.
Make a copy of Lab 16 subfolder
c255labs\labs\c255_lab16_scratch
or download and unzip c255_lab16_scratch.zip
Rename the new folder as c255_lab20_menu.
Open solution file in Visual Studio and finish the usual steps to rename the project.
Start FLUID program and open project
c255labs\labs\c255_lab20_menu\fluid_project\CFluidWindow.fl
Select Window win_app object in the tree view.
To add a menu bar, a menu, and the menu items, use FLUID menus
New/Menus/Menu_Bar... New/Menus/Submenu... -- name your submenu "File" New/Menus/MenuIem... -- name your submenu item "Save" New/Menus/MenuIem... -- name your submenu item "Load" New/Menus/MenuIem... -- name your submenu item "Quit"
(You can also click on the window designer, then right-click and choose same options from the context menu.)
If menu bar appears in the window as a small rectangle, drag it to the top of the window and resize to fit accross the top.
When a submenu is added to the menu bar, its properties window is displayed. Switch to the properties/GUI tab and change the label text.
Switch to the Properties/C++ tab and name the menu items as
menu_save menu_load menu_quit
Note that a submenu is added by selecting the menu bar, and the menu items are added when the correspecting submenu object is selected.
Alternatively, you can duplicate the existing menu item in the project tree by selecting it and then hitting CTRL+C, CTRL+V to copy and paste the new item. Double-click the item to display and update its properties.
Save FLUID project and generate C++ code by
File/Write Code...
Open solution
c255labs\labs\c255_lab20_menu\c255_lab20.sln
in Visual Studio.
Download the new versions of
Compile and test the program.
Use WinMerge to compare the old and the new versions of CMainWindow.h. Notice new callback functions for "Load" and "Save". There are two new #includes in support of the standard file chooser dialog:
#include <string> #include <FL/Fl_Native_File_Chooser.H>
There is also a new global function declaration at the end of the file:
std::string select_file( std::string prompt );
Compare the old and the new versions of CMainWindow.cpp:
CMainWindow::show() attaches callback functions to the menu items. Notice that menu_quit gets the same callback as the window's "X" button.
"Load" and "Save" callbacks aren't fully implemented: both functions call select_file() and print the user choice on the console screen.
Function select_file() configures and displays the Fl_Native_File_Chooser dialog. If the user cancels the dialog, it returns an empty string. Otherwise it returns the full path to the file picked by the user.
This is a self-learning lab. There are no files to submit. Make sure to complete all required steps to build and test the project on your own home computer.