Sample5: RadioButton Widget.

The radio-button is used to make an exclusive selection in a button group. The group is formed simply by creating all buttons as children of the same parent. When a radio-button is selected, the one, if any, that was previously selected, is automatically deselected. Both Objects invoke a callback to notify the application about their new selection status. The object data supplied by the callback in the second argument is a boolean value that specifies the Object selection status.
Radio-buttons are always created deselected, so, to make the initial selection in the Object group, you must use the function call MRButtonActivate().
This Object is transparent by default, so it assumes its parent background and foreground color. However, it's possible to change them via the provided color setting function calls (like MObjectSetColor()).

/* C version */ #include <stdio.h> #include <stdlib.h> #include "mgui.h" void ExitCB(MOBJECT obj, void *a, void *b) {   MShellDestroy((MOBJECT)b);   MGUITerm();   exit(0); } MOBJECT label; void RadioButtonCB(MOBJECT rb, MVALUE status, void *ii) {    char str[64];    if (status)    {       sprintf(str, "Selected button %d", (int)ii);       MObjectSetText(label, str);    } } void MGUIMain(int argc, char **argv) {    int ii;    char str[16];    MOBJECT shell, rform, pb, rb;    shell = MCreateShell("Example 5", 0);    rform = MCreateRowForm(shell);    for (ii=0; ii < 4; ii++)    {       sprintf(str, "Radio %d", ii);       rb = MCreateRButton(rform, str, HELV_MEDIUM);       MRButtonSetCallback(rb, RadioButtonCB, (void *)ii);    }    label = MCreateLabel(rform, "", DEFAULT_FONT);    MRButtonActivate(rb);    pb = MCreatePButton(shell, "Quit", TIMES_MEDIUM);    MPButtonSetCallback(pb, ExitCB, shell);    MShellRealize(shell);    MMainLoop(); }
The callback RadioButtonCB(), used for all four buttons, sets the text in LABEL 'label'  to show  which is the currently selected button. The Object index is supplied to  the callback via the application data 'ii', set with the callback. Only  when the Object status evolves from 0 to 1 the callback changes the  LABEL text. Notice that the initial selection made via the MRButtonActivate() call  is realized after the LABEL 'label' creation. This is necessary because  the LABEL is referenced in the callback code, so 'label' must be a valid  Object identifier.
In the C++ version below you can note that working with MGUI in C++ you don't have the user-data facility in callbacks. One callback per object is needed to get different actions by different objects.

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

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

CaAppl appInstance;
class CaMainWindow : public CmShell {
        CmLabel *label;
public:
   CaMainWindow(const char *title, int flags);
   void radioButton1CB(int);
   void radioButton2CB(int);
   void radioButton3CB(int);
   void radioButton4CB(int);
   void changeLabelText(int);
   void exitCB(void);
};

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

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

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

   rform = new CmRowForm(this);
   rb= new CmRadioButton(rform, "Radio 1", HELV_MEDIUM);
   rb->setCallback(this, (INT_CB)radioButton1CB);
   rb= new CmRadioButton(rform, "Radio 2", HELV_MEDIUM);
   rb->setCallback(this, (INT_CB)radioButton2CB);
   rb= new CmRadioButton(rform, "Radio 3", HELV_MEDIUM);
   rb->setCallback(this, (INT_CB)radioButton3CB);
   rb= new CmRadioButton(rform, "Radio 4", HELV_MEDIUM);
   rb->setCallback(this, (INT_CB)radioButton4CB);
   label = new CmLabel(rform, "", DEFAULT_FONT);

   rb->arm();

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

void CaMainWindow::changeLabelText(int btn)
{
   char str[64];
   sprintf(str, "Selected button %d", btn);
   label->setText(str);
}

void CaMainWindow::radioButton1CB(int status)
{
   if (status)
       changeLabelText(1);
}

void CaMainWindow::radioButton2CB(int status)
{
   if (status)
       changeLabelText(2);
}

void CaMainWindow::radioButton3CB(int status)
{
   if (status)
       changeLabelText(3);
}

void CaMainWindow::radioButton4CB(int status)
{
   if (status)
       changeLabelText(4);
}






Screenshot

Back