home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / desklib / examples / DeskLib / Examples / !Pane2test / c / Pane2test next >
Encoding:
Text File  |  1995-05-12  |  11.0 KB  |  379 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *        File: Pane2test.c                                             *
  4.  *                                                                      *
  5.  *     Purpose: Core of program.                                        *
  6.  *              Initialises and loads resources.                        *
  7.  *              Handles IconBar icon and menu.                          *
  8.  *                                                                      *
  9.  ************************************************************************/
  10.  
  11. #include "DeskLib:WimpSWIs.h"          /* Low-level WIMP commands         */
  12. #include "DeskLib:Window.h"            /* Window handling automation      */
  13. #include "DeskLib:Core.h"              /* usefull core functions          */
  14. #include "DeskLib:Dialog.h"            /* High-level dialogue windows     */
  15. #include "DeskLib:Error.h"             /* Error despatcher                */
  16. #include "DeskLib:Event.h"             /* Event despatcher                */
  17. #include "DeskLib:EventMsg.h"          /* Wimp Message event dispatcher   */
  18. #include "DeskLib:File.h"              /* Low level file handling         */
  19. #include "DeskLib:GFX.h"               /* Graphics routines (GFX_Wait)    */
  20. #include "DeskLib:Handler.h"           /* Default/example event handlers  */
  21. #include "DeskLib:Hourglass.h"         /* Hourglass module interfaces     */
  22. #include "DeskLib:Icon.h"              /* Icon handling automation        */
  23. #include "DeskLib:Menu.h"              /* Menu create & show support      */
  24. #include "DeskLib:Msgs.h"              /* Message translation code        */
  25. #include "DeskLib:Resource.h"          /* Handles finding resource files  */
  26. #include "DeskLib:Screen.h"            /* Getting screen size info, etc   */
  27. #include "DeskLib:Sound.h"             /* Sound System control            */
  28. #include "DeskLib:Template.h"          /* Template loading and caching    */
  29. #include "DeskLib:Slider.h"            /* "Easy" slider handling          */
  30. #include "DeskLib:Kbd.h"               /* Read hotkeys                    */
  31. #include "DeskLib:Time.h"              /* "TIME"                          */
  32. #include "DeskLib:KernelSWIs.h"        /* OS_Byte                         */
  33. #include "DeskLib:Dialog2.h"           /* windows on menus made easy      */
  34.  
  35. #include "PaneTests.h"
  36.  
  37. #include "kernel.h"
  38. #include "MySwis.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. static BOOL quit = FALSE;              /* Set to TRUE to quit the app.    */
  44.  
  45. extern sprite_area usericonsprites = NULL;
  46.  
  47. static menu_ptr mainmenu = NULL;
  48.  
  49. /* Defines for the positions at which items appear on the menus. */
  50. #define mainmenu_INFO           0
  51. #define mainmenu_QUIT           5
  52.  
  53. #define idle_TIME               40
  54.  
  55. /* Wimp_PollIdle delay time */ 
  56. extern unsigned int idletime = idle_TIME;
  57.  
  58. /* startup command for the Desktop Boot file */
  59. static char startup_command[256];
  60.  
  61. #define program_NAME "Pane2test"
  62.  
  63. void setup_menus(void);
  64. void setup_templates(void);
  65. void build_startupcommand(void);
  66. static BOOL save_desktop(event_pollblock *event, void *reference);
  67.  
  68. /******************************************************************************/
  69.  
  70. static BOOL CloseWindow(event_pollblock *event, void *reference)
  71. /*  Handler attached to all closewindow events.
  72.  * fall through causes BEEP when window is closed which indicates that I have
  73.  * not installed a close window hander for that window
  74.  */
  75. {
  76.   Sound_SysBeep();
  77.  
  78.   if(event->type == event_CLOSE){
  79.  
  80.      Sound_SysBeep();
  81.      Window_Delete(event->data.words[0]);
  82.      
  83.   } 
  84.   return(TRUE);
  85. }
  86.  
  87. /******************************************************************************/
  88.  
  89. static BOOL Menuwarn_proginfo(event_pollblock *event, void *reference)
  90. {
  91.  /*
  92.   * called to display "about this program" window
  93.   */
  94.  
  95.   if((menu_currentopen != mainmenu) ||
  96.      (event->data.message.data.menuwarn.selection[0] != mainmenu_INFO))
  97.      return(FALSE);
  98.  
  99.   Dialog2_OpenDialogMenuLeaf( event, (dialog2_block *)reference);
  100.  
  101.   return(TRUE);
  102. }
  103.  
  104. /******************************************************************************/
  105.  
  106. static void Popup_proginfo( dialog2_block *dialog2)
  107. {
  108.  /*
  109.   * called when "about this program" window is displayed to set the correct
  110.   * version text
  111.   */
  112.   char version[50];
  113.  
  114.   Msgs_Lookup("info.version", version, 50);
  115.   Icon_SetText(dialog2->window, 4, version);
  116. }
  117.  
  118. /******************************************************************************
  119.  *                              Click handlers                                *
  120.  ******************************************************************************/
  121.  
  122. static BOOL Click_baricon(event_pollblock *event, void *reference)
  123. {
  124.  /*
  125.   * called when mouse click on iconbar icon is made.
  126.   * opens "Create new image" with the select button.
  127.   * opens iconbar menu with the menu button.
  128.   * opens debug window with the adjust button
  129.   */
  130.  
  131.   if(event->data.mouse.button.data.menu)
  132.      Menu_Show(mainmenu, event->data.mouse.pos.x,-1);
  133.  
  134.   return(TRUE);
  135. }
  136.  
  137. /******************************************************************************
  138.  *                           menu choice handlers                             *
  139.  ******************************************************************************/
  140.  
  141. static BOOL MenuChoice_mainmenu(event_pollblock *event, void *reference)
  142. {
  143.  /*
  144.   * called when menu selection of the iconbar menu is made
  145.   */
  146.   mouse_block ptr;
  147.  
  148.   if (menu_currentopen != mainmenu)        /* Only respond to the mainmenu  */
  149.     return(FALSE);
  150.  
  151.   switch(event->data.selection[0])
  152.   {
  153.  
  154.     case mainmenu_QUIT:
  155.       quit = TRUE;
  156.       break;
  157.  
  158.     default:
  159.       if( (event->data.selection[0] > mainmenu_INFO) &&
  160.           (event->data.selection[0] < mainmenu_QUIT) )
  161.           PaneTests_MenuSelection(event->data.selection[0]);
  162.       break;
  163.  
  164.   }
  165.  
  166.   Wimp_GetPointerInfo(&ptr);               /* If ADJUST was used to select, */
  167.   if (ptr.button.data.adjust)              /* then we keep the menu open.   */
  168.     Menu_ShowLast();
  169.  
  170.   return(TRUE);
  171. }
  172.  
  173. /******************************************************************************
  174.  *                                    main()                                  *
  175.  ******************************************************************************/
  176.  
  177. int main(int argc, char *argv[])
  178. {
  179.   char appname[64];
  180.  
  181.   Resource_Initialise(program_NAME);   /* resources in <program_NAME$Dir> */
  182.  
  183.  /*
  184.   * create startup command for Desktop Boot file
  185.   */
  186.   build_startupcommand();
  187.  
  188.  /*
  189.   * load messages file and look up application name
  190.   */
  191.   Msgs_LoadFile("messages");
  192.   Msgs_Lookup("app.name", appname, 64);
  193.  
  194.  /*
  195.   * initialise event and messages systems
  196.   */
  197.   Event_Initialise(appname);
  198.   EventMsg_Initialise();
  199.  
  200.  /*
  201.   * initialise shifting heap memory manager
  202.   */
  203. /* don't need it yet .....
  204.   flex_init();
  205.   _kernel_register_slotextend(flex_budge);
  206. */
  207.  
  208.  /*
  209.   * store current screen description and install event hander to keep it updated
  210.   */
  211.   Screen_CacheModeInfo();
  212.   EventMsg_Claim(message_MODECHANGE, event_ANY, Handler_ModeChange, NULL);
  213.  
  214.  /*
  215.   * install default event handlers
  216.   */
  217.   Event_Claim(event_REDRAW, event_ANY, event_ANY, Handler_NullRedraw, NULL);
  218.   Event_Claim(event_OPEN, event_ANY, event_ANY, Handler_OpenWindow, NULL);
  219.  
  220.  /*
  221.   * install savedesktop file message handler to insert startup command into
  222.   * Desktop Boot file
  223.   */
  224.   EventMsg_Claim(message_SAVEDESK, event_ANY, save_desktop, NULL);
  225.  
  226.  /*
  227.   * load window definitions
  228.   */
  229.   setup_templates();
  230.  
  231.  /*
  232.   * event handler to close windows and beep if I have messed the code up
  233.   */
  234.   Event_Claim(event_CLOSE, event_ANY, event_ANY, CloseWindow, NULL); 
  235.  
  236.  /*
  237.   * build menus
  238.   */
  239.   setup_menus();
  240.  
  241.  /*
  242.   * initialise the other files
  243.   */
  244.   PaneTests_Init();
  245.  
  246.   /*  Main event handling loop.
  247.    */
  248.   while (!quit){
  249.  
  250.     Wimp_Poll(event_mask, &event_lastevent);
  251.     /* Wimp_PollIdle(event_mask, &event_lastevent, (Time_Monotonic() + idletime)); */
  252.  
  253.     Event_Process(&event_lastevent);
  254.   }
  255.  
  256.   Template_ClearAll();
  257.  
  258.   return(0);
  259. }
  260.  
  261. /******************************************************************************
  262.  *                              setup routines                                *
  263.  ******************************************************************************/
  264.  
  265. void setup_menus(void)
  266. {
  267.  /*
  268.   * build iconbar menu
  269.   */
  270.   char menudescription[260];
  271.   char menutitle[15];
  272.   dialog2_block *proginfo;
  273.  
  274.   Msgs_Lookup("title.main", menutitle, 15);
  275.   Msgs_Lookup("menu.main", menudescription, 260);
  276.   mainmenu  = Menu_New(menutitle, menudescription);
  277.   proginfo = Dialog2_CreateDialogBlock( "ProgInfo", 42, 42, Popup_proginfo, NULL, NULL);
  278.   Menu_Warn(mainmenu, mainmenu_INFO, TRUE, Menuwarn_proginfo, proginfo);
  279.   
  280.   Event_Claim(event_MENU,  event_ANY, event_ANY, MenuChoice_mainmenu, NULL);
  281.  
  282. }
  283.  
  284. /******************************************************************************/
  285.  
  286. void setup_templates(void)
  287. {
  288.  /*
  289.   * load window and icon definition files
  290.   * install icon on icon bar
  291.   */
  292.   int threeD;
  293.   char spritefile[50];
  294.   icon_handle baricon;
  295.  
  296.   Template_Initialise();
  297.   /* Template_UseOutlineFonts(); */
  298.  
  299.   OS_Byte(osbyte_READCMOSRAM, 140, 0, NULL, &threeD);
  300.  
  301.   if((threeD & 1) == 1)
  302.      Template_LoadFile("Template3D");
  303.   else
  304.      Template_LoadFile("Templates");
  305.  
  306.   strncpy(spritefile, resource_pathname, 39);
  307.   if(screen_eig.y == screen_eig.x)
  308.      strcat(spritefile, "Sprites22");
  309.   else
  310.      strcat(spritefile, "Sprites");
  311.   usericonsprites = Sprite_LoadFile(spritefile);
  312.   
  313.   baricon = Icon_BarIconUser(program_NAME,iconbar_RIGHT,(unsigned int *) usericonsprites);
  314.  
  315.   Template_UseSpriteArea(usericonsprites);
  316.  
  317.   Event_Claim(event_CLICK, -2, baricon, Click_baricon, NULL);
  318. }
  319.  
  320. /******************************************************************************
  321.  *                              misc routines                                 *
  322.  ******************************************************************************/
  323.  
  324. /******************************************************************************/
  325.  
  326. void build_startupcommand(void)
  327. {
  328.  /*
  329.   * build startup command for DesktopBoot file from resource pathname
  330.   */
  331.   char resourcepath[40];
  332.   _kernel_swi_regs r;
  333.  
  334.   strcpy(resourcepath, "/");
  335.   strncat(resourcepath, resource_pathname, 38);
  336.  
  337.  /*
  338.   * remove "." from end of path
  339.   */
  340.   resourcepath[strlen(resourcepath) - 1] = 0;
  341.  
  342.   r.r[0] = (int) resourcepath;
  343.   r.r[1] = (int) startup_command;
  344.   r.r[2] = 256;
  345.   _kernel_swi( SWI_XOS_Bit | SWI_OS_GSTrans , &r, &r );
  346.  
  347. }
  348.  
  349. /******************************************************************************/
  350.  
  351. static BOOL save_desktop(event_pollblock *event, void *reference)
  352. {
  353.  /*
  354.   * save startup command to DesktopBoot file
  355.   */
  356.   file_handle channel;
  357.   BOOL error;
  358.   message_block message;
  359.  
  360.   channel = (file_handle) event->data.message.data.words[0];
  361.  
  362.   error = Error_Check(File_WriteBytes(channel, startup_command, strlen(startup_command)));
  363.  
  364.   if(!error){
  365.      if(Error_Check(File_WriteByte(channel, '\n')))
  366.         error = TRUE;
  367.   }
  368.  
  369.   if(error){ /* acknowledge message */
  370.  
  371.      message.header.yourref = message.header.myref;
  372.      Wimp_SendMessage(event_USERMESSAGEACK, &message, message.header.sender, NULL);
  373.      
  374.   } 
  375.  
  376.   return(TRUE);
  377. }
  378.  
  379.