Course list http://www.c-jump.com/bcc/
In this lab we create a window with a button to animate a progress bar widget:
Part I of the lab adds a timer event, which allows the application to receive notifications at a specific time interval. Part II of the lab the progress bar animation driven by the timer notifications.
The prototype for this project is Lab 4, an empty window:
c255labs\labs\c255_lab04
Make a copy of Lab 4 subfolder, or download and unzip c255_lab04.zip
Rename the new folder as c255_lab30_timer.
Open solution file in Visual Studio and finish the usual steps to rename the project.
Start FLUID program and open project
c255labs\labs\c255_lab30_timer\fluid_project\CFluidWindow.fl
Select Window win_app object in FLUID tree view. Use menu
New/Buttons/Button...
Name the new button btn_animate in the properties/C++ dialog.
Use menu
New/Other/Progress...
to add the Fl_Progress bar widget and name it bar_progress in the properties/C++ dialog.
Save FLUID project and generate C++ code by
File/Write Code...
Open solution
c255labs\labs\c255_lab30_timer\c255_lab30.sln
in Visual Studio.
Download the new versions of
Compile and test the program.
Open CMainWindow.h in the text editor and examine the code:
New constant TICK_TIMEOUT specifies the time interval at which the program wants to update the progress bar during the animation. It is a double value specifying the interval in seconds. For example, 0.05 indicates 50 milliseconds.
Another constant, DEFAULT_PROGRESS_COUNT indicates progress bar increments between 0% and 100%. For example, an increment of 10 creates progress values
0%, 10%, 20%, 30%, ..., 100%
CMainWindow::callback_btn_animate() is the callback function attached to the button.
CMainWindow::callback_animate() is the callback function invoked by the system timer.
Open CMainWindow.cpp in the text editor and examine the code:
The static member variable CMainWindow::TICK_TIMEOUT must be initialized in the implementation file. Notice the absence of the static keyword in the initialization code. Strangely, using it here is a syntax error, because it conflicts with other C++ rules for the static keyword in the implementation file.
Constructor CMainWindow::CMainWindow() initializes the m_progress_count to zero.
CMainWindow::show() attaches the callback function to the button widget.
When the user clicks "Animate", the CMainWindow::callback_btn_animate():
sets the progress count to the default value, and
calls CMainWindow::click_btn_animate().
CMainWindow::click_btn_animate() calls Fl::add_timeout(), asking FLTK to invoke CMainWindow::callback_animate() after TICK_TIMEOUT seconds.
CMainWindow::callback_animate() is called by the timer - this is a one-time timer event. To repeat the event, the function calls the Fl::repeat_timeout(). The "payload" here is the call to CMainWindow::animate(), which does the actual animation.
CMainWindow::animate() decrements the m_progress_count. If the progress count becomes zero, the function returns false, and the animation sequence terminates. Otherwise animation sequence continues: the function returns true, the Fl::repeat_timeout() is called again, and the animation sequence continues.
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.