home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / desklib / Examples / !TestApp / c / TestApp < prev   
Encoding:
Text File  |  1994-04-24  |  11.6 KB  |  334 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    TestApp.c
  12.     Author:  Copyright © 1992, 1993 Jason Williams
  13.     Version: 1.10 (12 Jul 1993)
  14.     Purpose: Demonstrate  (and test ;-)  some of the main DeskLib application
  15.              base routines. Note that 80% of this file is comment(!) and that
  16.              most of the calls used are overkill for such a small application
  17. */
  18.  
  19.  
  20. #include "DeskLib:WimpSWIs.h"          /* Low-level WIMP commands         */
  21. #include "DeskLib:Window.h"            /* Window handling automation      */
  22.  
  23. #include "DeskLib:Dialog.h"            /* High-level dialogue windows     */
  24. #include "DeskLib:Error.h"             /* Error despatcher                */
  25. #include "DeskLib:Event.h"             /* Event despatcher                */
  26. #include "DeskLib:EventMsg.h"          /* Wimp Message event dispatcher   */
  27. #include "DeskLib:File.h"              /* Low level file handling         */
  28. #include "DeskLib:GFX.h"               /* Graphics routines (GFX_Wait)    */
  29. #include "DeskLib:Handler.h"           /* Default/example event handlers  */
  30. #include "DeskLib:Hourglass.h"         /* Hourglass module interfaces     */
  31. #include "DeskLib:Icon.h"              /* Icon handling automation        */
  32. #include "DeskLib:Menu.h"              /* Menu create & show support      */
  33. #include "DeskLib:Msgs.h"              /* Message translation code        */
  34. #include "DeskLib:Resource.h"          /* Handles finding resource files  */
  35. #include "DeskLib:Screen.h"            /* Getting screen size info, etc   */
  36. #include "DeskLib:Sound.h"             /* Sound System control            */
  37. #include "DeskLib:Template.h"          /* Template loading and caching    */
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. static BOOL     quit      = FALSE;     /* Set to TRUE to quit the app.    */
  43. static menu_ptr mainmenu  = NULL;      /* Pointer to our main menu        */
  44. static menu_ptr item2menu = NULL;      /* Pointer to our main menu        */
  45.  
  46.  
  47. /* Defines for the positions at which items appear on our menus.
  48.  * Note that menu items always start at number 0, so item number 2 is
  49.  * actually the THIRD item in the menu.
  50.  */
  51. #define mainmenu_CHANGETITLE 0
  52. #define mainmenu_ITEM1       1
  53. #define mainmenu_ITEM2       2
  54. #define mainmenu_QUIT        3
  55.  
  56.  
  57. static BOOL AskUser(char *questiontag)
  58. /*  Tests/demonstrates use of Dialog_ calls to show a query window containing
  59.  *  the given bit of text - the given string is a Msgs_ message tag
  60.  */
  61. {
  62.   char   msg[200];
  63.   dialog dlog;
  64.   BOOL   clickedOK;
  65.   
  66.   dlog = Dialog_Create("query", 0);                 /* Create the window    */
  67.  
  68.   Msgs_Lookup(questiontag, msg, 196);               /* Fill in the message  */
  69.   Icon_SetText(Dialog_WindowHandle(dlog), 2, msg);
  70.  
  71.   Dialog_Show(dlog);                                /* Pop up the dialog    */
  72.   Sound_SysBeep();                                  /* And beep 4 attention */
  73.  
  74.   while (Dialog_WaitForClick(dlog) > 1 || Dialog_Persist(dlog))
  75.     /* Busy wait loop */ ;                          /* Wait for OK/Cancel   */
  76.  
  77.   /*  Check: Did they click OK?
  78.    *  The other possibilities are: they didn't click any icon, but just closed
  79.    *  the window (in which case LastClicked will be dialog_NOCHOICE), or
  80.    *  they clicked the cancel icon (icon number 1)
  81.    *  We must remember this before calling Dialog_Destroy, as the dialog
  82.    *  structure will be lost upon a destroy.
  83.    */
  84.   clickedOK = (Dialog_LastClicked(dlog) == 0);
  85.  
  86.   Event_Poll();  /*  Ensure WIMP gets a chance to deselect the icon before we
  87.                   *  kill the window (this is a bug in the RISC OS 3 Wimp)
  88.                   */
  89.  
  90.   Dialog_Destroy(dlog);              /* Remove the dialog, releasing memory */
  91.  
  92.   return(clickedOK);
  93. }
  94.  
  95.  
  96.  
  97. static window_handle menuwindow = NULL;
  98.  
  99. static BOOL ButtonClick(event_pollblock *event, void *reference)
  100. /*  Handler attached to all button click events.
  101.  *  If no other handler specifically handles the click, and it was a menu
  102.  *  click, we will pop up our menu in an appropriate position.
  103.  */
  104. {
  105.   int ypos;
  106.  
  107.   if (event->data.mouse.button.data.menu)             /* is a MENU click */
  108.   {
  109.     if (event->data.mouse.window >= 0)
  110.     {
  111.       ypos = event->data.mouse.pos.y;                /* Over a window    */
  112.       menuwindow = event->data.mouse.window;         /* (remember which) */
  113.       Menu_SetFlags(mainmenu, mainmenu_CHANGETITLE, FALSE, FALSE);
  114.                                                      /* Unshade item 0   */
  115.     }
  116.     else
  117.     {
  118.       ypos = -1;                                     /* Over the iconbar */
  119.       menuwindow = NULL;
  120.       Menu_SetFlags(mainmenu, mainmenu_CHANGETITLE, FALSE, TRUE);
  121.                                                      /* Shade item 0     */
  122.     }
  123.  
  124.     Menu_Show(mainmenu, event->data.mouse.pos.x, ypos);
  125.   }
  126.  
  127.   return(TRUE);
  128. }
  129.  
  130.  
  131.  
  132. static BOOL MenuChoice(event_pollblock *event, void *reference)
  133. /*  Handler to take care of the user's menu choices.
  134.  *  For now, we simply beep at practically everything except the 'Quit' option
  135.  */
  136. {
  137.   mouse_block ptr;
  138.  
  139.   if (menu_currentopen != mainmenu)        /* Only respond to the mainmenu  */
  140.     return(FALSE);
  141.  
  142.   switch(event->data.selection[0])
  143.   {
  144.     case mainmenu_CHANGETITLE:             /* Change the window's titlebar  */
  145.       Sound_SysBeep();                     /* Beep to confirm the choice.   */
  146.       if (menuwindow != NULL)
  147.         Window_SetTitle(menuwindow, "Whee! A New title!");
  148.       break;
  149.  
  150.     case mainmenu_ITEM2:
  151.       switch(event->data.selection[1])
  152.       {
  153.         case 0:
  154.           Menu_SetFlags(item2menu, 0, 1, 0);  /* Tick item 0   */
  155.           Menu_SetFlags(item2menu, 1, 0, 0);  /* Untick item 1 */
  156.           break;
  157.  
  158.         case 1:
  159.           Menu_SetFlags(item2menu, 0, 0, 0);  /* Untick item 0 */
  160.           Menu_SetFlags(item2menu, 1, 1, 0);  /* Tick item 1   */
  161.           break;
  162.  
  163.         case -1:                      /* Instead of choosing a submenu item, */
  164.           AskUser("query.click");     /* user clicked main item directly     */
  165.           break;
  166.       }
  167.       break;
  168.  
  169.     case mainmenu_QUIT:
  170.       quit = TRUE;
  171.       break;
  172.  
  173.     default:
  174.       Sound_SysBeep();                     /* Beep to confirm the choice.   */
  175.       break;
  176.   }
  177.  
  178.   Wimp_GetPointerInfo(&ptr);               /* If ADJUST was used to select, */
  179.   if (ptr.button.data.adjust)              /* then we keep the menu open.   */
  180.     Menu_ShowLast();
  181.  
  182.   return(TRUE);
  183. }
  184.  
  185.  
  186.  
  187. int main()
  188. {
  189.   window_handle window1, window2, window3;
  190.   icon_handle   baricon;
  191.   char          appname[64];
  192.   char          menudescription[260];
  193.  
  194.  
  195.   /*  Tell Resource (and thereby Template, Msgs, etc) where our resource
  196.    *  directory is: "<Tester$Dir>"
  197.    */
  198.   Resource_Initialise("Tester");   /* resources in <Tester$Dir> */
  199.  
  200.  
  201.   /*  Load and cache the messages. Find out the application name.
  202.    */
  203.   Msgs_LoadFile("messages");
  204.   Msgs_Lookup("app.name:Tester", appname, 64);
  205.  
  206.  
  207.   /*  Initialise the Wimp and Event Manager.
  208.    *  The task name shown on the task display and error reports is set from
  209.    *  the string "appname" fetched previously from the messages file.
  210.    */
  211.   Event_Initialise(appname);
  212.   EventMsg_Initialise();
  213.  
  214.  
  215.   /*  Read and remember the current screen mode information
  216.    *  (width and height, eig-factors, delta-x and delta-y, etc.)
  217.    *  This is needed by the Menu code to get menu widths correct.
  218.    */
  219.   Screen_CacheModeInfo();
  220.  
  221.  
  222.   /*  Add a handler function to a "screen mode changing" Wimp message, so
  223.    *  that the information provided by the "Screen" functions is always
  224.    *  correct.
  225.    *  NOTE that the new Handler_ModeChange should also ensure that outline
  226.    *  fonts will work if we change between lo-res and hi-res screen modes.
  227.    */
  228.   EventMsg_Claim(message_MODECHANGE, event_ANY, Handler_ModeChange, NULL);
  229.  
  230.  
  231.   /*  Plonk an icon onto the iconbar
  232.    */
  233.   baricon = Icon_BarIcon("!testapp", iconbar_RIGHT);
  234.  
  235.  
  236.   /*  Place the Handler_ module skeleton default handlers on all 
  237.    *  Redraw and Open-window request events (Application-wide defaults)
  238.    */
  239.   Event_Claim(event_REDRAW, event_ANY, event_ANY, Handler_NullRedraw, NULL);
  240.   Event_Claim(event_OPEN, event_ANY, event_ANY, Handler_OpenWindow, NULL);
  241.  
  242.  
  243.   /*  Load in and cache our window templates from the template file
  244.    *  "<Tester$Dir>.Templates" (Templates utilise the "Resource Directory"),
  245.    *  with support for using outline fonts in the icons if the file specifies
  246.    *  outline fonts.
  247.    */
  248.   Template_Initialise();
  249.   Template_UseOutlineFonts();
  250.   Template_LoadFile("Templates");
  251.  
  252.   /*  Create and open our main windows from the template "mainwindow".
  253.    *  Open the same window three times (3 copies):
  254.    *  1.  on-screen wherever it was positioned in the template manager,
  255.    *  2.  Centered on screen (screen-mode independent)
  256.    *  3.  Under the pointer (over where the program was double-clicked -note
  257.    *      that the window won't appear until after the hourglass test below
  258.    *      so it may not look like it was opened under the pointer!)
  259.    */
  260.   window1 = Window_CreateAndShow("mainwindow", 0, open_WHEREVER);
  261.   window2 = Window_CreateAndShow("mainwindow", 0, open_CENTERED);
  262.   window3 = Window_CreateAndShow("mainwindow", 0, open_UNDERPOINTER);
  263.  
  264.  
  265.   /*  Claim window-close events
  266.    *  Use the Window_Delete() handler rather than the Wimp_CloseWindow() one
  267.    *  so that the window is closed and deleted and all memory and event 
  268.    *  claims are released whenever the user hits the close icon.
  269.    */
  270.   Event_Claim(event_CLOSE, event_ANY, event_ANY, Handler_DeleteWindow, NULL);
  271.  
  272.  
  273.   /*  Add the 3-d "OK" button click handler so that the OK button (icon #2)
  274.    *  in the 3-d window indents for 1/3rd of a second whenever it is clicked
  275.    */
  276.   Event_Claim(event_CLICK, window2, 2, Handler_ClickOK, NULL);
  277.  
  278.  
  279.   /*  Add the Window_HelpHandler message handler so that help requests
  280.    *  for the 3 main windows and the iconbar icon are automatically handled
  281.    */
  282.   Window_AutoHelp(event_ANY, event_ANY);
  283.  
  284.  
  285.   /*  Menu example/test
  286.    *  1. Create two menu structures from messages in the Msgs file.
  287.    *  2. Attach the 'item2menu' as a submenu of the main menu
  288.    *  3. Extend the submenu by adding another two items to it
  289.    *  4. Add a handler to pop up our menu if we get a menu click over
  290.    *     our icon/windows, and another handler to process menu choices.
  291.    */
  292.   Msgs_Lookup("menu.main", menudescription, 260);
  293.   mainmenu  = Menu_New("TestApp", menudescription);
  294.  
  295.   Msgs_Lookup("menu.item2", menudescription, 260);
  296.   item2menu = Menu_New("SubMenu", menudescription);
  297.  
  298.   item2menu = Menu_Extend(item2menu, "~Extension 1,~Extension 2");
  299.  
  300.   Menu_AddSubMenu(mainmenu, 2, item2menu);
  301.  
  302.   Event_Claim(event_CLICK, event_ANY, event_ANY, ButtonClick, NULL);
  303.   Event_Claim(event_MENU,  event_ANY, event_ANY, MenuChoice,  NULL);
  304.  
  305.  
  306.   /*  Test the SWI call interface, and the Hourglass macros by whizzing
  307.    *  through a 0..100 percent count. Also uses GFX_Wait to tie it
  308.    *  to the vsync, so it will take 1 second to go from 0% to 100%
  309.    */
  310.  
  311.   {
  312.     int loop;
  313.     Hourglass_Start(1);
  314.     for (loop = 0; loop < 50; loop++)
  315.     {
  316.       Hourglass_Percentage(loop * 2);
  317.       GFX_Wait();
  318.     }
  319.     Hourglass_Off();
  320.   }
  321.  
  322.  
  323.   /*  Main event handling loop. Let Event_ do all the work for us!
  324.    */
  325.   while (!quit)
  326.     Event_Poll();
  327.  
  328.   Template_ClearAll();
  329.  
  330.   /*  Finally, we just exit. The atexit() handlers will clean up after us
  331.    */
  332.   return(0);
  333. }
  334.