Sample6: ToggleButton Widget.

The toggle-button is an on/off button, used to operate option selections. Unlike radio-buttons, toggle-buttons are never grouped, so they have always independent selection status. The activation callback acts as for the radio-button Object.
Via MTButtonActivate() and MTButtonDeactivate() it's possible to set the button activation status.

/* C version */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mgui.h" #define N_TOGGLE_BUTTON 4 MOBJECT label; int toggle_status[N_TOGGLE_BUTTON]; void ExitCB(MOBJECT obj, void *a, void *b) {    MShellDestroy((MOBJECT)b);    MGUITerm();    exit(0); } void ToggleButtonCB(MOBJECT rb, MVALUE status, void *ii) {    char str[128];    int i;    toggle_status[(int)ii] = (int)status;    strcpy(str, "Button status: ");    for (i=0; i < N_TOGGLE_BUTTON; i++)       strcat(str, (toggle_status[i] ? "  ON" : " OFF"));    MObjectSetText(label, str); } void MGUIMain(int argc, char **argv) {    int ii;    char str[16];    MOBJECT shell, rform, pb, tb;    shell = MCreateShell("Sample 6", SF_NO_CLOSE);    rform = MCreateRowForm(shell);    for (ii=0; ii < N_TOGGLE_BUTTON; ii++)    {       sprintf(str, "Toggle %d", ii);       tb = MCreateTButton(rform, str, HELV_L_FONT);       MTButtonSetCallback(tb, ToggleButtonCB, (void *)ii);    }    label = MCreateLabel(rform, "", DEFAULT_FONT);     MTButtonActivate(tb);    pb = MCreatePButton(shell, "Quit", TIMES_L_FONT);    MPButtonSetCallback(pb, ExitCB, shell);    MShellRealize(shell);    MMainLoop(); }
By activating the last created toggle-button (MTButtonActivate(tb)),  the 'label' LABEL changes its text before the SHELL (the window) is  realized. This is useful to force the window size to a suitable width.

// C++ version
#include <stdio.h>
#include <string.h>
#include "mguipp.h"

class CaAppl : public CmAppl {
public:
   void start(int, char **);
};

CaAppl appInstance;

class CaMainWindow : public CmShell {
        CmLabel *label;
        int toggle_status[4];
public:
   CaMainWindow(const char *title, int flags);
   void toggleButton1CB(int);
   void toggleButton2CB(int);
   void toggleButton3CB(int);
   void toggleButton4CB(int);
   void changeLabelText(int id, int status);
   void exitCB(void);
};

void CaMainWindow::exitCB(void)
{
   delete this;
   CmAppl::end(0);
}

void CaAppl::start(int argc, char **argv)
{
   CaMainWindow *win = new CaMainWindow("Example 6", SF_NO_CLOSE);
   win->realize();
   mainLoop();
}

CaMainWindow::CaMainWindow(const char *title, int flags)
: CmShell(title, flags)
{
   CmRowForm *rform;
   CmToggleButton *tb;
   CmPushButton *pb;

   for (int ii=0; ii < 4; ii++)
           toggle_status[ii] = 0;
   rform = new CmRowForm(this);
   tb= new CmToggleButton(rform, "Toggle 1", HELV_MEDIUM);
   tb->setCallback(this, (INT_CB)toggleButton1CB);
   tb= new CmToggleButton(rform, "Toggle 2", HELV_MEDIUM);
   tb->setCallback(this, (INT_CB)toggleButton2CB);
   tb= new CmToggleButton(rform, "Toggle 3", HELV_MEDIUM);
   tb->setCallback(this, (INT_CB)toggleButton3CB);
   tb= new CmToggleButton(rform, "Toggle 4", HELV_MEDIUM);
   tb->setCallback(this, (INT_CB)toggleButton4CB);
   label = new CmLabel(rform, "", DEFAULT_FONT);

   tb->arm();
   pb = new CmPushButton(this, "Quit", TIMES_MEDIUM);
   pb->setCallback(this, (VOID_CB)exitCB);
}

void CaMainWindow::changeLabelText(int btn, int status)
{
   char str[64];
   int ii;
   
   toggle_status[btn] = status;
   strcpy(str, "Button status: ");
   for (ii=0; ii < 4; ii++)
      strcat(str, (toggle_status[ii] ? "  ON" : " OFF"));
   label->setText(str);
}

void CaMainWindow::toggleButton1CB(int status)
{
   changeLabelText(0, status);
}

void CaMainWindow::toggleButton2CB(int status)
{
   changeLabelText(1, status);
}

void CaMainWindow::toggleButton3CB(int status)
{
   changeLabelText(2, status);
}

void CaMainWindow::toggleButton4CB(int status)
{
   changeLabelText(3, status);
}





Screenshot

Back