home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 18 / amigaformatcd18.iso / mui / mui_developer / c / examples / layout.c < prev    next >
C/C++ Source or Header  |  1997-03-10  |  5KB  |  219 lines

  1. #include "demo.h"
  2. #include <time.h>
  3.  
  4. #define ID_REWARD 1
  5.  
  6.  
  7. /*
  8. ** Custom layout function.
  9. ** Perform several actions according to the messages lm_Type
  10. ** field. Note that you must return MUILM_UNKNOWN if you do
  11. ** not implement a specific lm_Type.
  12. */
  13.  
  14. SAVEDS ULONG __asm LayoutFunc(REG(a0) struct Hook *h,REG(a2) Object *obj,REG(a1) struct MUI_LayoutMsg *lm)
  15. {
  16.     switch (lm->lm_Type)
  17.     {
  18.         case MUILM_MINMAX:
  19.         {
  20.             /*
  21.             ** MinMax calculation function. When this is called,
  22.             ** the children of your group have already been asked
  23.             ** about their min/max dimension so you can use their
  24.             ** dimensions to calculate yours.
  25.             **
  26.             ** In this example, we make our minimum size twice as
  27.             ** big as the biggest child in our group.
  28.             */
  29.  
  30.             Object *cstate = (Object *)lm->lm_Children->mlh_Head;
  31.             Object *child;
  32.  
  33.             WORD maxminwidth  = 0;
  34.             WORD maxminheight = 0;
  35.  
  36.             /* find out biggest widths & heights of our children */
  37.  
  38.             while (child = NextObject(&cstate))
  39.             {
  40.                 if (maxminwidth <MUI_MAXMAX && _minwidth (child) > maxminwidth ) maxminwidth  = _minwidth (child);
  41.                 if (maxminheight<MUI_MAXMAX && _minheight(child) > maxminheight) maxminheight = _minheight(child);
  42.             }
  43.  
  44.             /* set the result fields in the message */
  45.  
  46.             lm->lm_MinMax.MinWidth  = 2*maxminwidth;
  47.             lm->lm_MinMax.MinHeight = 2*maxminheight;
  48.             lm->lm_MinMax.DefWidth  = 4*maxminwidth;
  49.             lm->lm_MinMax.DefHeight = 4*maxminheight;
  50.             lm->lm_MinMax.MaxWidth  = MUI_MAXMAX;
  51.             lm->lm_MinMax.MaxHeight = MUI_MAXMAX;
  52.  
  53.             return(0);
  54.         }
  55.  
  56.         case MUILM_LAYOUT:
  57.         {
  58.             /*
  59.             ** Layout function. Here, we have to call MUI_Layout() for each
  60.             ** our children. MUI wants us to place them in a rectangle
  61.             ** defined by (0,0,lm->lm_Layout.Width-1,lm->lm_Layout.Height-1)
  62.             ** You are free to put the children anywhere in this rectangle.
  63.             **
  64.             ** If you are a virtual group, you may also extend
  65.             ** the given dimensions and place your children anywhere. Be sure
  66.             ** to return the dimensions you need in lm->lm_Layout.Width and
  67.             ** lm->lm_Layout.Height in this case.
  68.             **
  69.             ** Return TRUE if everything went ok, FALSE on error.
  70.             ** Note: Errors during layout are not easy to handle for MUI.
  71.             **       Better avoid them!
  72.             */
  73.  
  74.             Object *cstate = (Object *)lm->lm_Children->mlh_Head;
  75.             Object *child;
  76.  
  77.             while (child = NextObject(&cstate))
  78.             {
  79.                 LONG mw = _minwidth (child);
  80.                 LONG mh = _minheight(child);
  81.                 LONG l = rand() % (lm->lm_Layout.Width  - mw);
  82.                 LONG t = rand() % (lm->lm_Layout.Height - mh);
  83.  
  84.                 if (!MUI_Layout(child,l,t,mw,mh,0))
  85.                     return(FALSE);
  86.             }
  87.  
  88.             return(TRUE);
  89.         }
  90.     }
  91.     return(MUILM_UNKNOWN);
  92. }
  93.  
  94.  
  95. SAVEDS ULONG __asm PressFunc(REG(a0) struct Hook *h,REG(a2) Object *app,REG(a1) ULONG *num)
  96. {
  97.     static int lastnum = -1;
  98.  
  99.     lastnum++;
  100.  
  101.     if (lastnum!=*num)
  102.     {
  103.         DisplayBeep(0);
  104.         lastnum = -1;
  105.     }
  106.     else if (lastnum==7)
  107.     {
  108.         DoMethod(app,MUIM_Application_ReturnID,ID_REWARD);
  109.         lastnum = -1;
  110.     }
  111.  
  112.     return(0);
  113. }
  114.  
  115.  
  116. int main(int argc,char *argv[])
  117. {
  118.     APTR app,window;
  119.     APTR b[8],yeah;
  120.     static struct Hook LayoutHook = { {0,0}, LayoutFunc, NULL, NULL };
  121.     static struct Hook PressHook  = { {0,0}, PressFunc , NULL, NULL };
  122.     ULONG signals;
  123.     BOOL running = TRUE;
  124.     int i;
  125.  
  126.     init();
  127.  
  128.     srand(time(0));
  129.  
  130.     app = ApplicationObject,
  131.         MUIA_Application_Title      , "Layout",
  132.         MUIA_Application_Version    , "$VER: Layout 19.5 (12.02.97)",
  133.         MUIA_Application_Copyright  , "©1993, Stefan Stuntz",
  134.         MUIA_Application_Author     , "Stefan Stuntz",
  135.         MUIA_Application_Description, "Demonstrate custom layout hooks.",
  136.         MUIA_Application_Base       , "Layout",
  137.  
  138.         SubWindow, window = WindowObject,
  139.             MUIA_Window_Title, "Custom Layout",
  140.             MUIA_Window_ID   , MAKE_ID('C','L','S','3'),
  141.             WindowContents, VGroup,
  142.  
  143.                 Child, TextObject,
  144.                     TextFrame,
  145.                     MUIA_Background, MUII_TextBack,
  146.                     MUIA_Text_Contents, "\33cDemonstration of a custom layout hook.\nSince it's usually no good idea to have overlapping\nobjects, your hooks should be more sophisticated.",
  147.                     End,
  148.  
  149.                 Child, VGroup,
  150.                     GroupFrame,
  151.                     MUIA_Group_LayoutHook, &LayoutHook,
  152.                     Child, b[0] = SimpleButton("Click"),
  153.                     Child, b[1] = SimpleButton("me"),
  154.                     Child, b[2] = SimpleButton("in"),
  155.                     Child, b[3] = SimpleButton("correct"),
  156.                     Child, b[4] = SimpleButton("sequence"),
  157.                     Child, b[5] = SimpleButton("to"),
  158.                     Child, b[6] = SimpleButton("be"),
  159.                     Child, b[7] = SimpleButton("rewarded!"),
  160.  
  161.                     /* Child, ScrollbarObject, MUIA_Group_Horiz, TRUE, End, */
  162.  
  163.                     Child, yeah = SimpleButton("Yeah!\nYou did it!\nClick to quit!"),
  164.                     End,
  165.  
  166.                 End,
  167.  
  168.             End,
  169.         End;
  170.  
  171.     if (!app)
  172.         fail(app,"Failed to create Application.");
  173.  
  174.     DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  175.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  176.  
  177.     DoMethod(yeah,MUIM_Notify,MUIA_Pressed,FALSE,
  178.         app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  179.  
  180.     for (i=0;i<8;i++)
  181.         DoMethod(b[i],MUIM_Notify,MUIA_Pressed,FALSE,
  182.             app,3,MUIM_CallHook,&PressHook,i);
  183.  
  184.     set(yeah,MUIA_ShowMe,FALSE);
  185.  
  186.  
  187. /*
  188. ** Input loop...
  189. */
  190.  
  191.     set(window,MUIA_Window_Open,TRUE);
  192.  
  193.     while (running)
  194.     {
  195.         switch (DoMethod(app,MUIM_Application_Input,&signals))
  196.         {
  197.             case MUIV_Application_ReturnID_Quit:
  198.                 running = FALSE;
  199.                 break;
  200.  
  201.             case ID_REWARD:
  202.                 set(yeah,MUIA_ShowMe,TRUE);
  203.                 break;
  204.         }
  205.  
  206.         if (running && signals) Wait(signals);
  207.     }
  208.  
  209.     set(window,MUIA_Window_Open,FALSE);
  210.  
  211.  
  212. /*
  213. ** Shut down...
  214. */
  215.  
  216.     MUI_DisposeObject(app);     /* dispose all objects. */
  217.     fail(NULL,NULL);            /* exit, app is already disposed. */
  218. }
  219.