Sample2: Shell, Label and Push Button.

This example shows how to create a complete dialog window without any help from utility functions. It simulates the dialog window of the previous example.

Program termination in this sample is controlled via a callback function (a callback method in C++). 


/* C version */
#include <stdio.h>
#include <stdlib.h>
#include "mgui.h"

/* Program termination callback */

void ExitCB(MOBJECT obj, void *a, void *b)
{
  char *msg = "Are you sure you want to quit?";

  if (MMessageDialog("EXIT", msg, "Sure", "Cancel", NULL) != 0)
    return;
  obj = MObjectShell(obj);
  MShellUnrealize(obj);
  MShellDestroy(obj);
  MGUITerm();
  exit(0);
}

void MGUIMain(int argc, char **argv)
{
   MOBJECT shell, pushbutton;

   shell = MCreateShell("Example 2", SF_NO_CLOSE);
   MCreateLabel(shell, "Hello, world", HELV_LARGE);
   pushbutton = MCreatePButton(shell, " Ok ", TIMES_MEDIUM);
   MPButtonSetCallback(pushbutton, ExitCB, NULL);
   MShellRealize(shell);

   MMainLoop();
}

The ExitCB() function uses the typical MGUI Object callback prototype. First argument is the calling Object identifier. The second, notifying Object data, depends on the particular callback type. The third is the application user data, supplied at the callback setting time. In this case, only the first argument is used. In fact it offers the possibility to go back to SHELL Object, ancestor of the calling push button Object, via the MObjectShell() function call. The SHELL identifier is then used to remove its window from the screen (with MUnrealizeShell()) and to destroy the SHELL itself and all its descendent by using the MShellDestroy() call.
The callback proceeds calling the termination function MGUITerm() to free all resources, and then the canonical exit() function.
Beware the call to MGUITerm() is particularly important in Windows, since it doesn't automatically free resources allocated by a terminating program.
For simplicity, in the following examples, ExitCB() will be used as the termination callback, even if its source code will not appear in the examples.
This program code creates a SHELL Object with a flag argument that forces the shell window to be open with no close button, then a LABEL and a PBUTTON as its children.
LABEL Object's task is to show multi line text within a window. By default it has no visible shadow, is transparent and displays text center aligned in the horizontal direction. Alignment can be changed via the MLabelSetAlignment() function call.
Text to be displayed and the font to be used are specified respectively as the second and third argument of the LABEL creation function.
The PBUTTON Object also shows multi line text, unlike the LABEL it has a shadow that gives it a particular 3 D look and provides a callback for the mouse button press and release user action.
In this example the push button is created with the "Ok" text, Times Large font and the termination callback ExitCB() is set. The MMainLoop() function call places the program in the application events main loop. This function never returns, so the program can terminate only via an application provided callback (like ExitCB()). 


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

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

// The class bound to the dialog window
class CaMainWindow : public CmShell {
public:
   CaMainWindow(const char *title, int flags);
   void exitCB(void);
};

// Termination callback
void CaMainWindow::exitCB(void)
{
  char *msg = "Are you sure you want to quit?";

  if (messageDialog("EXIT", msg, "Sure", "Cancel", NULL) != 0)
    return;
  delete this;
  CmAppl::end(0);
}

// Window constructor
CaMainWindow::CaMainWindow(const char *title, int flags)
: CmShell(title, flags)
{
   new CmLabel(this, "Hello, World.", HELV_LARGE);
   CmPushButton *pb = new CmPushButton(this, " Ok ", TIMES_MEDIUM);
   pb->setCallback(this, (VOID_CB)exitCB);
}

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

C++ callbacks in MGUI are usually bound to methods of the window class. MGUI Designer generates, for each  created window, a c++ class derived from CmShell that holds, as methods, all callbacks  the user activates on any GUI object in the window. This is possible because all MGUI  objects derive from CmCallback (CmMGUI).


Screenshot

Back