home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0948.lha / Snoopy / Ini / Examples / config.c < prev    next >
C/C++ Source or Header  |  1993-12-20  |  10KB  |  317 lines

  1. /*
  2. **
  3. ** This is a small example using ini.library. It allows you to support
  4. ** user-configurable (gadtools) pulldown menus.
  5. **
  6. */
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <intuition/intuition.h>
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/graphics_protos.h>
  16. #include <clib/intuition_protos.h>
  17. #include <clib/gadtools_protos.h>
  18.  
  19. #include "libraries/ini.h"
  20. #include "clib/ini_protos.h"
  21. #include "pragmas/ini_pragmas.h"
  22.  
  23. // small macro to loop through all entrys of a logical section
  24. #define ini_Loop( HEADER, INFO ) for( INFO = (INILINEINFO *) (HEADER->node.ln_Succ); INFO->node.ln_Succ && INFO->flags != INIFLAG_HEADER; INFO = (INILINEINFO *) (INFO->node.ln_Succ) )
  25.  
  26. // built-in-commands descriptor
  27. typedef struct BuiltInCommand
  28.     {
  29.         STRPTR name;
  30.         LONG id;
  31.         VOID (*function)(VOID);
  32.     } COMMAND;
  33.  
  34. // example function
  35. void about(void)
  36. {
  37.     printf("ini.library example for making menus configurable\n" );
  38. }
  39.  
  40. // example commands
  41. COMMAND BuiltIns[] =   
  42.     {
  43.         { "about", 1, about },
  44.         { "quit", -1, 0 },
  45.         { NULL, 0, NULL }
  46.     };
  47.  
  48. // some global stuff
  49. struct NewMenu *MenuDefinition = NULL;
  50. struct Library *GadToolsBase = NULL;
  51. struct GfxBase *GfxBase = NULL;
  52. struct IniBase *IniBase = NULL;
  53. struct IntuitionBase *IntuitionBase = NULL;
  54. struct Screen *screen = NULL;
  55. struct Window *window = NULL;
  56. struct Menu *menu = NULL;
  57.  
  58. ULONG sizeOfMenus;
  59.  
  60. // add title entry
  61. void config_SetMenuTitle( struct NewMenu *nm, int offset, STRPTR string )
  62. {
  63.     nm[offset].nm_Type = NM_TITLE;
  64.     nm[offset].nm_Label = strdup( string );
  65.     nm[offset].nm_CommKey = NULL;
  66.     nm[offset].nm_Flags = 0;
  67.     nm[offset].nm_MutualExclude = 0L;
  68.     nm[offset].nm_UserData = 0L;
  69. } // config_AddMenuTitle()
  70.  
  71. // add item entry
  72. void config_SetMenuItem( struct NewMenu *nm, int offset, STRPTR string, STRPTR hotkey, ULONG userID )
  73. {
  74.     nm[offset].nm_Type = NM_ITEM;
  75.     nm[offset].nm_Label = strdup( string );
  76.     nm[offset].nm_CommKey = hotkey ? strdup( hotkey ) : NULL;
  77.     nm[offset].nm_Flags = 0;
  78.     nm[offset].nm_MutualExclude = 0L;
  79.     nm[offset].nm_UserData = (void *)userID;
  80. } // config_SetMenuItem()
  81.  
  82. // add barlabel entry
  83. void config_SetBarLabel( struct NewMenu *nm, int offset )
  84. {
  85.     nm[offset].nm_Type = NM_ITEM;
  86.     nm[offset].nm_Label = NM_BARLABEL;
  87.     nm[offset].nm_CommKey = NULL;
  88.     nm[offset].nm_Flags = 0;
  89.     nm[offset].nm_MutualExclude = 0L;
  90.     nm[offset].nm_UserData = 0L;
  91. } // config_SetBarLabel()
  92.  
  93. // add end-entry
  94. void config_SetMenuEnd( struct NewMenu *nm, int offset )
  95. {
  96.     nm[offset].nm_Type = NM_END;
  97.     nm[offset].nm_Label = NULL;
  98.     nm[offset].nm_CommKey = NULL;
  99.     nm[offset].nm_Flags = 0;
  100.     nm[offset].nm_MutualExclude = 0L;
  101.     nm[offset].nm_UserData = 0L;
  102. } // config_SetMenuEnd()
  103.  
  104. // analyse item-entry syntax
  105. BOOL syntax_MenuItem( STRPTR contents, STRPTR *string, STRPTR *hotkey, ULONG *id )
  106. {
  107.     char *cp; int i;
  108.  
  109.     *string = contents;
  110.     if( ( cp = strchr( contents, ',' ) ) == NULL )
  111.     {
  112.         printf("Syntax error: missing string in \"%s\"\n", contents );
  113.         return FALSE;    
  114.     }
  115.     *cp = 0;
  116.  
  117.     *hotkey = ++cp;
  118.     if( ( cp = strchr( *hotkey, ',' ) ) == NULL )
  119.     {
  120.         printf("Syntax error: missing hotkey in \"%s\"\n", contents );
  121.         return FALSE;            
  122.     }
  123.     *(cp++) = 0;
  124.  
  125.     for( i=0; BuiltIns[i].name; i++ )
  126.     {
  127.         if( !stricmp( BuiltIns[i].name, cp ) )
  128.         {
  129.             *id = BuiltIns[i].id;
  130.             return TRUE;
  131.         }
  132.     }
  133.     printf( "Syntax error: invalid function in \"%s\"\n", contents );
  134.     return FALSE;
  135. } // syntax_MenuItem()
  136.  
  137. // build menu
  138. BOOL config_BuildMenus( char *iniName )
  139. {
  140.     INIPARSER parser;
  141.     INILINEINFO *header,*info;
  142.     int nrOfMenus = 20,i;
  143.     
  144.     sizeOfMenus = nrOfMenus * sizeof( struct NewMenu );
  145.  
  146.     // Allocate memory for NewMenus
  147.     if( !( MenuDefinition = (struct NewMenu *) AllocMem( sizeOfMenus, MEMF_CLEAR ) ) )
  148.         return FALSE;
  149.  
  150.     // try to load ini-file
  151.     if( ini_New( iniName, &parser ) != INIERROR_NONE )
  152.         return FALSE;
  153.  
  154.     // find header
  155.     if( ( header = ini_GetHeader( &parser, "menus" ) ) == NULL )
  156.     {
  157.         // use default menu or (in our case) just fail
  158.         printf("Sorry, header not found\n" );
  159.         return FALSE;
  160.     }
  161.     else
  162.     {
  163.         i = 0;
  164.  
  165.         // loop through section
  166.         ini_Loop( header, info )        
  167.         {
  168.             if( info->flags == INIFLAG_VARIABLE )
  169.             {
  170.                 if( !stricmp( info->variable, "title" ) ) 
  171.                 {
  172.                     config_SetMenuTitle( MenuDefinition,i++, info->contents );
  173.                 }
  174.                 else if( !stricmp( info->variable, "item" ) )
  175.                 {
  176.                     char *string,*hotkey;
  177.                     ULONG id;
  178.                     
  179.                     if( syntax_MenuItem( info->contents, &string, &hotkey, &id ) )
  180.                         config_SetMenuItem( MenuDefinition, i++, string,hotkey,id );
  181.                 }
  182.                 else if( !stricmp( info->variable, "label" ) )
  183.                 {
  184.                     config_SetBarLabel( MenuDefinition, i++ );
  185.                 }
  186.             }
  187.         }
  188.         config_SetMenuEnd( MenuDefinition, i++ );
  189.     }
  190.     return TRUE;
  191. } // BuildMenus()
  192.  
  193. void CloseLibrarys( void )
  194. {
  195.     if( IntuitionBase )
  196.         CloseLibrary( (struct Library *)IntuitionBase );
  197.  
  198.     if( GfxBase )
  199.         CloseLibrary( (struct Library *)GfxBase );
  200.  
  201.     if( GadToolsBase )
  202.         CloseLibrary( (struct Library *)GadToolsBase );
  203.      
  204.     if( IniBase )
  205.         CloseLibrary( (struct Library *)IniBase );
  206.      
  207. } // CloseLibrarys()
  208.  
  209. BOOL OpenLibrarys( void )
  210. {
  211.     if( (GadToolsBase = OpenLibrary("gadtools.library", 36L) ) != NULL )
  212.     {
  213.         if( (GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L) ) != NULL )
  214.         {
  215.             if( (IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 36L) ) != NULL )
  216.             {
  217.                 if( (IniBase = (struct IniBase *) OpenLibrary("ini.library", 0L) ) != NULL )
  218.                 {
  219.                     return TRUE;
  220.                 }
  221.             }
  222.         }
  223.     }
  224.     CloseLibrarys();
  225.     return FALSE;
  226. } // OpenLibrarys()
  227.  
  228. int main( void )
  229. {
  230.     struct IntuiMessage *imsg;
  231.     APTR vi;
  232.  
  233.     if( OpenLibrarys() )
  234.     {
  235.         if( !config_BuildMenus( "config.ini" ) )
  236.         {
  237.             printf("SORRY, NO MENUS\n");
  238.             return FALSE;
  239.         }
  240.  
  241.         if( ( screen = LockPubScreen( NULL ) ) != NULL )
  242.         {
  243.             if( ( menu = CreateMenus( MenuDefinition,
  244.                                         GTMN_FrontPen, 0, TAG_DONE ) ) != NULL )
  245.             {
  246.                 vi = GetVisualInfo( screen, TAG_DONE );
  247.                 
  248.                 LayoutMenus( menu, vi, TAG_DONE );
  249.     
  250.                 if( ( window = OpenWindowTags( NULL,
  251.                                                WA_Width, 500, 
  252.                                                WA_Height, 100,
  253.                                                WA_Activate, TRUE,
  254.                                                WA_DragBar, TRUE,
  255.                                                WA_CloseGadget, TRUE,
  256.                                                WA_DepthGadget, TRUE,
  257.                                                WA_SmartRefresh, TRUE,
  258.                                                WA_IDCMP, IDCMP_CLOSEWINDOW|IDCMP_MENUPICK,
  259.                                                WA_Title, "INI-CONFIG",
  260.                                                TAG_DONE ) ) != NULL )
  261.                 {
  262.                     BOOL terminated = FALSE;
  263.                     
  264.                     SetMenuStrip( window, menu );
  265.                     
  266.                     while( !terminated )
  267.                     {
  268.                         Wait( 1<<window->UserPort->mp_SigBit );
  269.                     
  270.                         while( !terminated &&
  271.                                    ( imsg = (struct IntuiMessage *)GetMsg( window->UserPort ) ) )
  272.                         {
  273.                             ULONG imsgClass = imsg->Class;
  274.                             UWORD imsgCode = imsg->Code;
  275.                             ReplyMsg( (struct Message *)imsg );
  276.                                 
  277.                             if( imsgClass == CLOSEWINDOW ) terminated = TRUE;
  278.                             else if( imsgClass == IDCMP_MENUPICK )
  279.                             {
  280.                                 int i;
  281.                                 LONG id;
  282.                                 struct MenuItem *item;
  283.                                 
  284.                                 item = ItemAddress( menu, imsgCode );
  285.                                 id = (LONG)GTMENUITEM_USERDATA( item );
  286.  
  287.                                 if( id == -1 )
  288.                                     terminated = TRUE;
  289.                                 else
  290.                                 {
  291.                                     for( i=0; BuiltIns[i].name; i++ )
  292.                                     {
  293.                                         if( BuiltIns[i].id == id )
  294.                                         {
  295.                                             printf("Found function %ld\n", id );
  296.                                             if( BuiltIns[i].function )
  297.                                                 (*(BuiltIns[i].function))();
  298.                                         }
  299.                                     }
  300.                                 }
  301.                             }
  302.                         }
  303.                     }
  304.                     ClearMenuStrip( window );
  305.                     CloseWindow( window );
  306.                 }
  307.                 FreeMenus( menu );
  308.                 FreeVisualInfo( vi );            
  309.             }
  310.             UnlockPubScreen( NULL, screen );
  311.         }
  312.         CloseLibrarys();
  313.     }
  314.     return 0;
  315. }
  316.  
  317.