home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / boopsi / popupmenuclass / example.c < prev    next >
C/C++ Source or Header  |  1995-10-09  |  5KB  |  197 lines

  1. /** ** *** MakeRev Header *** **
  2. **
  3. **  ** Example - PopUpMenuClass example. **
  4. **
  5. **  Copyright © 1993 Markus Aalto
  6. **
  7. **  Creation date: 18-Dec-93
  8. **
  9. **  ------------------------------------------------------------------
  10. **  $Filename: Example.c $
  11. **  $Revision: 1.1 $
  12. **  $Date: 28-Dec-93 $
  13. **
  14. **  $Author: Markus Aalto $
  15. **  $Comment: Very simple example just to show use of PopUpMenuClass. $
  16. **
  17. */
  18.  
  19.  
  20. #include <BoopsiObjects/PopUpMenuClass.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24.  
  25. void DoAll( void );
  26. void HandleIDCMP( struct Gadget *gad );
  27.  
  28. #define PUMG_GADGET_ID  1
  29.  
  30. long __oslibversion = 37;
  31.  
  32. Class *PopUpMenuClass;
  33. struct Screen *scr;
  34. struct Window *win;
  35. struct DrawInfo *dri;
  36.  
  37. UBYTE *names[] = {
  38.     "PopUpMenuClass",
  39.     "Copyright",
  40.     "(c) 1993",
  41.     "Markus Aalto",
  42.     "",
  43.     "Freely",
  44.     "Distributable",
  45.     "Software",
  46.     "",
  47.     "For more info",
  48.     "Write to:",
  49.     "s37732v@vipunen.hut.fi",
  50.     NULL
  51. };
  52.  
  53. int main( int argc, char **argv )
  54. {
  55.     UWORD Height;
  56.  
  57.     if( PopUpMenuClass = CreatePopUpMenuClass() ) {
  58.         if( scr = LockPubScreen( NULL ) ) {
  59.             if( dri = GetScreenDrawInfo( scr ) ) {
  60.                 Height = 11 + scr->WBorTop + 2*dri->dri_Font->tf_YSize
  61.                          + INTERHEIGHT;
  62.  
  63.                 win = OpenWindowTags(NULL,
  64.                     WA_Left,            0,
  65.                     WA_Top,             scr->BarHeight,
  66.                     WA_Height,          Height,
  67.                     WA_Width,           scr->WBorRight + scr->WBorLeft + PUMG_MinWidth + 100,
  68.                     WA_IDCMP,           IDCMP_CLOSEWINDOW|IDCMP_GADGETUP|IDCMP_REFRESHWINDOW|IDCMP_VANILLAKEY,
  69.                     WA_SizeGadget,      TRUE,
  70.                     WA_CloseGadget,     TRUE,
  71.                     WA_DragBar,         TRUE,
  72.                     WA_Title,           "Example for PopUpMenuClass",
  73.                     WA_PubScreen,       scr,
  74.                     WA_MinWidth,        scr->WBorRight + scr->WBorLeft + PUMG_MinWidth,
  75.                     WA_MinHeight,       Height,
  76.                     WA_MaxWidth,        ~0,
  77.                     WA_MaxHeight,       ~0,
  78.                     WA_SizeBBottom,     TRUE,
  79.                     WA_DepthGadget,     TRUE,
  80.                     WA_Activate,        TRUE,
  81.                     WA_SimpleRefresh,   TRUE,
  82.                     TAG_END );
  83.  
  84.                 if( win ) {
  85.                     DoAll();
  86.  
  87.                     CloseWindow( win );
  88.                 }
  89.  
  90.                 FreeScreenDrawInfo( scr, dri );
  91.             }
  92.         }
  93.  
  94.         DisposePopUpMenuClass( PopUpMenuClass );
  95.     }
  96. }
  97.  
  98. void DoAll()
  99. {
  100.     struct Node *node;
  101.     struct List labels;
  102.     UBYTE **strings = names;
  103.     BOOL OK = TRUE;
  104.     struct Gadget *gad;
  105.     ULONG Height;
  106.  
  107.     NewList( &labels );
  108.  
  109.     while( *strings ) {
  110.         node = (struct Node *)AllocVec( sizeof(struct Node), MEMF_ANY|MEMF_CLEAR );
  111.         if( node ) {
  112.             node->ln_Name = *strings;
  113.             AddTail( &labels, node );
  114.         }
  115.         else {
  116.             OK = FALSE;
  117.             break;
  118.         }
  119.  
  120.         strings++;
  121.     }
  122.  
  123.     if( OK ) {
  124.         Height = max(dri->dri_Font->tf_YSize + INTERHEIGHT, PUMG_MinHeight );
  125.  
  126.         gad = (struct Gadget *)NewObject(PopUpMenuClass, NULL,
  127.             GA_Left,            win->BorderLeft,
  128.             GA_RelBottom,       1 - win->BorderBottom - Height,
  129.             GA_Height,          Height,
  130.             GA_RelWidth,        -win->BorderLeft - win->BorderRight,
  131.             GA_RelVerify,       TRUE,
  132.             GA_ID,              PUMG_GADGET_ID,
  133.             PUMG_Labels,        &labels,
  134.             PUMG_TextFont,      dri->dri_Font,
  135.             PUMG_NewLook,       TRUE,
  136.             TAG_END);
  137.  
  138.         if( gad ) {
  139.             AddGadget( win, gad, -1);
  140.             RefreshGList( gad, win, NULL, 1 );
  141.  
  142.             HandleIDCMP( gad );
  143.  
  144.             RemoveGadget( win, gad );
  145.         }
  146.  
  147.         DisposeObject( (Object *)gad );
  148.     }
  149.  
  150.     while( node = RemHead( &labels ) ) {
  151.         FreeVec( (void *)node );
  152.     }
  153. }
  154.  
  155. void HandleIDCMP( struct Gadget *gad )
  156. {
  157.     ULONG   signalmask = (1 << win->UserPort->mp_SigBit) |\
  158.                          SIGBREAKF_CTRL_C,
  159.             signal;
  160.     BOOL    END = FALSE;
  161.     struct  IntuiMessage *imsg;
  162.     struct  Gadget *gadget;
  163.  
  164.     while( !END ) {
  165.         signal = Wait( signalmask );
  166.         if( signal & (1lu << win->UserPort->mp_SigBit) ) {
  167.             while( imsg = (struct IntuiMessage *)GetMsg(win->UserPort) ) {
  168.                 switch( imsg->Class )
  169.                 {
  170.                 case IDCMP_REFRESHWINDOW:
  171.                     BeginRefresh( win );
  172.                     EndRefresh(win, TRUE );
  173.                     break;
  174.                 case IDCMP_CLOSEWINDOW:
  175.                     END = TRUE;
  176.                     break;
  177.                 case IDCMP_VANILLAKEY:  /* Any keypress will do. */
  178.                     ActivateGadget( gad, win, NULL );
  179.                     break;
  180.                 case IDCMP_GADGETUP:
  181.                     gadget = (struct Gadget *)imsg->IAddress;
  182.  
  183.                     if( gadget->GadgetID == PUMG_GADGET_ID ) {
  184.                         printf("New Active Item is %d\n", imsg->Code );
  185.                     }
  186.                     break;
  187.                 }
  188.  
  189.                 ReplyMsg( (struct Message *)imsg );
  190.             }
  191.         }
  192.  
  193.         if( signal & SIGBREAKF_CTRL_C ) {
  194.             END = TRUE;
  195.         }
  196.     }
  197. }