home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / a / backthrow / !backthrow_c_main < prev    next >
Encoding:
Text File  |  1993-02-07  |  8.7 KB  |  299 lines

  1. /* main.c - wimp interfacing routines for backthrow
  2.  * using desklib */
  3. /* GTK 06-Feb-1993 */
  4.  
  5. /* standard ANSI C includes.
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. /* desklib includes
  12.  */
  13. #include "Event.h"       /* Event despatcher                             */
  14. #include "EventMsg.h"    /* Wimp Message event dispatcher                */
  15. #include "Error.h"       /* Error despatcher                             */
  16. #include "Resource.h"    /* Handles finding resource files in home dir.  */
  17. #include "Template.h"    /* Template loading and cacheing                */
  18. #include "Window.h"      /* Window handling automation                   */
  19. #include "Icon.h"        /* Icon handling automation                     */
  20. #include "Hourglass.h"   /* Hourglass module interfaces                  */
  21. #include "GFX.h"         /* Graphics routines (GFX_Wait)                 */
  22. #include "Msgs.h"        /* Message translation code                     */
  23. #include "Handler.h"     /* Default event handlers                       */
  24. #include "menus.h"       /* Menu utilities                               */
  25. #include "WimpSwis.h"
  26.  
  27. /* backthrow includes
  28.  */
  29. #include "backthrow.h"
  30.  
  31. /* Misc defines.
  32.  */
  33. #define BUFFERSIZE      4096
  34. #define APPNAME_MAX     64
  35. #define VERSION_MAX     10
  36. #define NUM_FILES       32
  37.  
  38. /* defines for the icons in the main window
  39.  */
  40. #define OK_ICN            0
  41. #define    CANCEL_ICN      1
  42. #define TEXT_ICN        3
  43.  
  44. /* define for the version writable icon in the program info dbox.
  45.  */
  46. #define DBOX_VSN_ICN    4
  47.  
  48. /* defines for the menu entries.
  49.  */
  50. #define MNU_INFO        0
  51. #define MNU_QUIT        1
  52.  
  53. /* Well... I'll think up something to make this one local to main...
  54.  * until then it has to be global because the iconbar mouse click
  55.  * handler needs to access it.
  56.  */
  57. menu            mnu;
  58.  
  59. /* Handle icon bar clicks: open window if select is clicked, or
  60.  * open menu if the menu button is clicked.
  61.  */
  62. BOOL barclickproc(event_pollblock *event, void *reference)
  63. {
  64.     if (event->data.mouse.button.data.select)
  65.         Window_Show(*(window_handle*)reference, open_WHEREVER);
  66.     if (event->data.mouse.button.data.menu)
  67.         Menus_Show(mnu, event->data.mouse.pos, TRUE);
  68.  
  69.     return TRUE;
  70. }
  71.  
  72. /* Close window if cancel clicked, just wrapped into a separate procedure
  73.  * because it has to be an event handler.
  74.  */
  75. BOOL cancelproc(event_pollblock *event, void *reference)
  76. {
  77.     Window_Hide(event->data.mouse.window);
  78.     return TRUE;
  79. }
  80.  
  81. /* add filename of dragged files to our list.
  82.  */
  83. BOOL dragproc(event_pollblock *event, void *reference)
  84. {
  85.     char buf[BUFFERSIZE];
  86.     static BOOL first=TRUE;
  87.     window_handle mywindow;
  88.  
  89.     mywindow = *(window_handle*)reference;
  90.  
  91.     /* if the file was dragged to the window, then insert it
  92.      */
  93.     if (event->data.message.data.dataload.window == mywindow)
  94.     {
  95.         Icon_GetText(mywindow, TEXT_ICN, buf);
  96.         if(first || strlen(buf) == 0)
  97.         {
  98.             Icon_SetText(mywindow, TEXT_ICN,
  99.                 event->data.message.data.dataload.filename);
  100.             first = FALSE;
  101.         }
  102.         else
  103.             Icon_printf(mywindow, TEXT_ICN, "%s,%s",buf,
  104.                 event->data.message.data.dataload.filename);
  105.     }
  106.     /* otherwise replace the contents of the icon with the filename,
  107.      * and open the main window.
  108.      */
  109.     else
  110.     {
  111.         Icon_SetText(mywindow, TEXT_ICN,
  112.             event->data.message.data.dataload.filename);
  113.         if (first)
  114.             first = FALSE;
  115.         Window_Show(*(window_handle*)reference, open_WHEREVER);
  116.     }    
  117.  
  118.     return TRUE;
  119. }
  120.  
  121. /* This one is called when the OK button is clicked. It decomposes the
  122.  * string in the icon into a string array, then calls backthrow.
  123.  */
  124. BOOL okproc(event_pollblock *event, void *reference)
  125. {
  126.     char *buf, *end, *sav;
  127.     char **pass;
  128.     int num;
  129.     BOOL retval;
  130.  
  131.     /* init a buffer for the contents of the writable icon, and pointers
  132.      * and the array for backthrow().
  133.      */
  134.     buf = malloc(BUFFERSIZE);
  135.     sav = buf;
  136.     num = 0;
  137.     pass = (char **) calloc(NUM_FILES, sizeof(int *));
  138.  
  139.     /* read the icons contents, and init the end pointer for the
  140.      * resulting string.
  141.      */
  142.     Icon_GetText(event->data.mouse.window, TEXT_ICN, buf);
  143.     end = buf + strlen(buf);
  144.  
  145.     /* this comparison does not work properly... it should inhibit
  146.      * the start of the backthrower with a zero filename argument.
  147.      */
  148.     if (end > buf)
  149.     {
  150.         /* init first array argument.
  151.          */
  152.         pass[num++] = buf++;
  153.         /* replace every ',' in the string with a '\0', then set the next
  154.          * array element to point to the next char after the ',' unless
  155.          * there is no next char. Increment element counter as appropriate.
  156.          */
  157.         while ((num < NUM_FILES) && (buf < end))
  158.             if (*buf == ',')
  159.             {
  160.                 *buf++ = NULL;
  161.                 if (buf < end)
  162.                     pass[num++] = buf;
  163.             }
  164.             else
  165.                 buf++;
  166.     }
  167.  
  168.     /* call backthrow, and store its return value for the caller.
  169.      */
  170.     retval = backthrow(num, pass);
  171.  
  172.     /* clean up and close the main window after processing.
  173.      */
  174.     free(sav);
  175.     Window_Hide(event->data.mouse.window);
  176.  
  177.     /* return retval frombackthrow to the caller.
  178.      */
  179.     return retval;
  180. }
  181.  
  182. /* handle menu selections
  183.  */
  184. void mnuproc(void *reference, int *hit)
  185. {
  186.     switch(*hit)
  187.     {
  188.         case MNU_QUIT:    Event_CloseDown();
  189.                         break;
  190.         case MNU_INFO: ; /* Nothing happens! */
  191.     }
  192. }
  193.  
  194. /* This is the Menuselection handler procedure for the iconbar menu.
  195.  * Doesn't really do a lot... just calls the mnuproc
  196.  */
  197. BOOL mnuhandlerproc(event_pollblock *event, void *reference)
  198. {
  199.     mnuproc(NULL, event->data.selection);
  200.  
  201.     return TRUE;
  202. }
  203.  
  204. /* the programs main: just do some initialisations, then go into an
  205.  * infinite event_poll() loop.
  206.  */
  207. int main()
  208. {
  209.     window_handle window;
  210.     window_handle proginfo;
  211.     icon_handle   baricon;
  212.     char          appname[APPNAME_MAX];
  213.     char      version[VERSION_MAX];
  214.  
  215.     /* Tell Resource (and thereby Template, Msgs, etc) where our resource
  216.      * directory is: "<Backthrow$Dir>"
  217.      */
  218.     Resource_Initialise("Backthrow");   /* resources in <Tester$Dir> */
  219.  
  220.     /* Load and cache the messages. Find out the application name
  221.      * and version number
  222.      */
  223.     Msgs_LoadFile("messages");
  224.     Msgs_Lookup("app.name:Backthrow", appname, APPNAME_MAX);
  225.     Msgs_Lookup("app.version:unset", version, VERSION_MAX);
  226.  
  227.     /* Initialise the Wimp and Event Manager.
  228.      */
  229.     Event_Initialise(appname);
  230.  
  231.     /* Put an icon onto the iconbar
  232.      */
  233.     baricon = Icon_BarIcon("!Backthrow", iconbar_RIGHT);
  234.  
  235.     /* Place the Handler_ module skeleton default handlers on all 
  236.      * Redraw and Open-window request events (Application-wide defaults)
  237.      */
  238.     Event_Claim(event_REDRAW, event_ANY, event_ANY, Handler_NullRedraw, NULL);
  239.     Event_Claim(event_OPEN, event_ANY, event_ANY, Handler_OpenWindow, NULL);
  240.  
  241.     /* Load in and cache our window templates from the file
  242.      * "<Backthrow$Dir>.Templates" (Templates utilise the "Resource Directory")
  243.      */
  244.     Template_Initialise();
  245.     Template_LoadFile("Templates");
  246.  
  247.     /* Create and open our main windows from the template "mainwindow".
  248.      * Centered on screen (screen-mode independent)
  249.      */
  250.     window = Window_Create("mainwindow", 0);
  251.     proginfo = Window_Create("proginfo",20);
  252.     Icon_SetText(proginfo, DBOX_VSN_ICN, version);
  253.  
  254.     /* Claim events:
  255.      * window close
  256.      */
  257.     Event_Claim(event_CLOSE, window, event_ANY, Handler_CloseWindow, NULL);
  258.     /* Click to the OK icon
  259.      */
  260.     Event_Claim(event_CLICK, window, OK_ICN, okproc, NULL);
  261.     /* Click to the cancel icon
  262.      */
  263.     Event_Claim(event_CLICK, window, CANCEL_ICN, cancelproc, NULL);
  264.     /* Click to the iconbar icon (also does the iconbar menu handling)
  265.      */
  266.     Event_Claim(event_CLICK, window_ICONBAR, baricon, barclickproc, &window);
  267.     /* Iconbar menu selection event handler
  268.      */
  269.     Event_Claim(event_MENU, event_ANY, baricon, mnuhandlerproc, NULL);
  270.  
  271.     /* Add the Window_HelpHandler message handler so that help requests
  272.      * are handeled automatically.
  273.      */
  274.     Window_AutoHelp(event_ANY, event_ANY);
  275.  
  276.     /* Initialize the eventmsg system, and claim the dataload message
  277.      * (for handling the filedrags to the main window)
  278.      */
  279.     EventMsg_Initialise();
  280.     EventMsg_Claim(message_DATALOAD, event_ANY, dragproc, &window);
  281.  
  282.     /* Setup application menu
  283.      */
  284.     mnu = Menus_New("Backthrow", "Info  ,Quit");
  285.     Menus_AddDialogBox(mnu, 1, proginfo);
  286.  
  287.     /* attach menu to the main window, so that it is handeled automatically.
  288.      * the menu that is opened on the iconbar is handeled separately...
  289.      * (mainly because I did not find out to handle it automatically and
  290.      * correctly!)
  291.      */
  292.     Menus_AttachMenu(mnu, window, event_ANY, mnuproc, NULL);
  293.  
  294.     /* Main event handling loop. Let Event_* do all the work for us!
  295.      */
  296.     while (TRUE)
  297.         Event_Poll();
  298. }
  299.