AppTimerCB() is a Timeout callback which receives, as the second parameter, the STATUSBAR’s id. It increments the object’s value by 10 units, sets the text string to the percentage amount of task completion, and sets the timeout callback again. When the ‘end of task’ value is reached (100), the callback is no longer set and the push button is made sensitive to allow the user to close the window. The push button’s id is found as user data in the status bar Object. The callback is set via the MAddTimeout() function specifying a delay of 500 msec and the STATUSBAR object id as user data. The MGUIMain() function simply creates a SHELL with a STATUSBAR and a PUSHBUTTON. The push button id is stored as user data in the statusbar object and it is initially set unsensitive. Once the AppTimerCB() callback is set the first time, the program evolves simulating a background work in progress for 5 seconds.
/* C version */ /* * example 10 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mgui.h" void ExitCB(MOBJECT obj, void *a, void *b) { MShellDestroy((MOBJECT)b); MGUITerm(); exit(0); } void AppTimerCB(TIMEOUT_ID id, void *data) { static int perc; char str[128]; MOBJECT sbar = (MOBJECT )data; perc += 10; sprintf(str, "%d %%", perc); MObjectSetText(sbar, str); MStatusBarSetPos(sbar, perc); if (perc < 100) MAddTimeout(500L, AppTimerCB, data); else MObjectSetSensitive((MOBJECT)MObjectGetUserData(sbar)); } void MGUIMain(int argc, char **argv) { MOBJECT shell, *pb, *statusbar; MTColor sliderColor; shell = MCreateShell("Sample 10", SF_NO_CLOSE); MObjectSetBackgroundImageFile(shell, "tile5.bmp", BI_TILED); sliderColor = MAllocColor(80, 140, 120); statusbar = MCreateStatusBar(shell, "0 %", 100, HELV_MEDIUM); MObjectSetTransparency(statusbar, 255); MStatusBarSetSliderColor(statusbar, sliderColor); MFreeColor(sliderColor); pb = MCreatePButton(shell, "Quit", TIMES_LARGE); MPButtonSetCallback(pb, ExitCB, shell); MObjectSetUnsensitive(pb); MObjectSetUserData(statusbar, pb); MAddTimeout(500L, AppTimerCB, statusbar); MShellRealize(shell); MMainLoop(); }
// C++ version #include <stdio.h> #include "mguipp.h" class CaAppl : public CmAppl { public: void start(int, char **); }; CaAppl appInstance; class CaMainWindow : public CmShell { int perc; CmStatusBar *sbar; CmPushButton *quit_button; CmTimeout *tid; public: CaMainWindow(const char *title, int flags); void appTimerCB(void); void exitCB(void); }; void CaMainWindow::exitCB(void) { delete this; CmAppl::end(0); } void CaAppl::start(int argc, char **argv) { CaMainWindow *win = new CaMainWindow("Example 10", SF_NO_CLOSE); win->realize(); mainLoop(); } void CaMainWindow::appTimerCB(void) { char str[128]; perc += 10; sprintf(str, "%d %%", perc); sbar->setText(str); sbar->setPos(perc); if (perc >= 100) { delete tid; quit_button->setSensitive(); } } CaMainWindow::CaMainWindow(const char *title, int flags) : CmShell(title, flags) { CmSharedColor sliderColor(80, 140, 120); setBackgroundImageFile("tile5.bmp", BI_TILED); sbar = new CmStatusBar(this, "0 %", 100, HELV_MEDIUM); sbar->setTransparency(255); sbar->setSliderColor(&sliderColor); quit_button = new CmPushButton(this, "Quit", TIMES_MEDIUM); quit_button->setCallback(this, (VOID_CB)exitCB); quit_button->setUnsensitive(); perc = 0; tid = new CmTimeout(this, (VOID_CB)appTimerCB, (CARD32)500, True); }