home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / m / multimod / !MultiMod / c / wimpif < prev   
Text File  |  1992-11-16  |  8KB  |  281 lines

  1. /* wimpif.c contains the wimp stuff for the program */
  2.  
  3. #include "all.h"
  4.  
  5. #define DISPMENU "Large icons,Small icons,Full info|No sort,Sort on name,Sort on size,Reverse size"
  6.  
  7. absolute code = { CODE_INITIALISE } ;
  8.  
  9. /********************************* CONSTANTS ********************************/
  10.  
  11. /* Info box field for the version string */
  12. #define Example_info_field    4
  13.  
  14. /******************************** GLOBAL DATA *******************************/
  15.  
  16. /* Application version */
  17. char *example_Version_String = "1.00 (1 June 1992)";
  18.  
  19. /* The top of the menu tree */
  20. menu bar_menu,disp_menu;
  21.  
  22. /* Handle for the example window */
  23. file_info data;  /* data[x]->(w|cm|m[y].(offset|size|display) */
  24. usage     using;
  25.  
  26. /***************************** WINDOW FUNCTIONS *****************************/
  27.  
  28. /*--- Create the window, yielding its handle. Return TRUE if ok. ---*/
  29. BOOL example_create_window(char *name, wimp_w *handle)
  30. {
  31.   template  *t = template_copy(template_find(name));
  32.  
  33.   /* Create the window, dealing with errors */
  34.   t->window.spritearea = (void *) 1;  /* Set the sprite area pointer */
  35.   return (wimpt_complain(wimp_create_wind(&(t->window), handle)) == 0);
  36. }
  37.  
  38. /*--- Individual event routines for the window ---*/
  39.  
  40. void example_redraw_window(wimp_w handle)
  41. {
  42.   int            i,more;
  43.   wimp_redrawstr r;
  44.   int            bind = lookup_window_bind(handle);
  45.   SFI            d    = data[bind];
  46.   int            e    = d->entries;
  47.   display_types  disp = d->disp;
  48.  
  49.   /* Start the redraw */
  50.   r.w = handle;
  51.   wimpt_noerr(wimp_redraw_wind(&r, &more));
  52.  
  53.   /* Do the redraw loop */
  54.   /* Use one of the array of procedures to find the correct icon drawer */
  55.  
  56.   while (more)
  57.   {
  58.     if (bind > -1)
  59.         for (i = 0; i < e; i++) multi[disp].drawer(bind,i,&r);
  60.     wimp_get_rectangle(&r, &more);
  61.   }
  62. }
  63.  
  64. void example_open_window(wimp_openstr *o)
  65. {
  66.   /* Just pass the open request on to the wimp */
  67.   wimpt_noerr(wimp_open_wind(o));
  68. }
  69.  
  70. /****************************** EVENT HANDLERS ******************************/
  71.  
  72. /*--- Event handler called on a left click on the icon. ---*/
  73. void example_iconclick(wimp_i icon)
  74. {
  75.   wimp_wstate state;
  76.   int new;
  77.  
  78.   icon = icon; /* We don't need the handle: this stops compiler warning */
  79.  
  80.   if ((new = allocate_untitled_window((char *)-1)) == -1)
  81.     werr(0,"Too many windows");
  82.     else
  83.   {
  84.     /* Get the state of the window */
  85.     if (wimpt_complain(wimp_get_wind_state(data[new]->w , &state)) == 0)
  86.     {
  87.       state.o.behind = -1;          /* Make sure window is opened in front */
  88.       wimpt_noerr(wimp_open_wind(&state.o));
  89.     }
  90.   }
  91. }
  92.  
  93. /*--- Display the program info box - called from the menu processor. ---*/
  94. void example_info_about_program(void)
  95. {
  96.   dbox  d;  /* Dialogue box handle */
  97.  
  98.   /* Create the dialogue box */
  99.   if (d = dbox_new("Info"), d != NULL)
  100.   {
  101.     /* Fill in the version number */
  102.     dbox_setfield(d, Example_info_field, example_Version_String);
  103.  
  104.     /* Show the dialogue box */
  105.     dbox_show(d);
  106.  
  107.     /* Keep it on the screen as long as needed */
  108.     dbox_fillin(d);
  109.  
  110.     /* Dispose of the dialogue box */
  111.     dbox_dispose(&d);
  112.   }
  113. }
  114.  
  115. /*--- Event handler for the menu. ---*/
  116. void example_menuproc(void *handle, char *hit)
  117. {
  118.   handle = handle; /* We don't need handle: this stops compiler warning */
  119.  
  120.   /* Find which menu item was hit and take action as appropriate */
  121.   switch (hit[0])
  122.   {
  123.     case bar_menu_info:
  124.       example_info_about_program();
  125.       break;
  126.  
  127.     case bar_menu_new:
  128.       saveas(MultiModule_FileType,"Collection",0,save_empty_mm,0,0,-1);
  129.       break;
  130.  
  131.     case bar_menu_quit:
  132.       /* Exit from the program. The wimp gets rid of the window and icon */
  133.       if (verify_quit()) exit(0);
  134.   }
  135. }
  136.  
  137. #define DBOX_UNSAVED "Really discard modified file?"
  138.  
  139. /*--- Event handler for window. ---*/
  140. void data_win_handler(wimp_eventstr *e, void *handle)
  141. {
  142.   int bind;
  143.   char *name;
  144.  
  145.   /* Deal with event */
  146.   switch (e->e)
  147.   {
  148.     case wimp_EREDRAW:
  149.       example_redraw_window(e->data.o.w);
  150.       break;
  151.  
  152.     case wimp_EOPEN:
  153.       example_open_window(&e->data.o);
  154.       break;
  155.  
  156.     case wimp_ECLOSE:  /* Pass on close request */
  157.       bind = (int) handle;
  158.       if (data[bind]->changed) if (dboxquery(DBOX_UNSAVED) != dboxquery_YES) break;
  159.       wimpt_noerr(wimp_delete_wind(e->data.o.w));
  160.       destroy_data(bind);
  161.       break;
  162.  
  163.     case wimp_ESEND:
  164.     case wimp_ESENDWANTACK:
  165.       switch (e->data.msg.hdr.action)
  166.       {
  167.       case wimp_MDATASAVE:
  168.                 switch (xferrecv_checkimport(&bind))
  169.                 {
  170.                 case MultiModule_FileType:
  171.                         if (e->data.msg.hdr.task != wimpt_task()) break;
  172.                         werr(0,"To perform an import, save then drag the file in");
  173.                 }
  174.                 break;
  175.  
  176.       case wimp_MDATALOAD:
  177.                 switch (xferrecv_checkinsert(&name))
  178.                 {
  179.                 case MultiModule_FileType:
  180.                         xferrecv_insertfileok();
  181.                         insert_file((int) handle , name);
  182.                         break;
  183.                 case Module_FileType:
  184.                         xferrecv_insertfileok();
  185.                         add_module((int) handle , name);
  186.                         break;
  187.                 default:
  188.                         werr(0,"You can only drop modules or module collections here");
  189.                 }
  190.                 break;
  191.       default: break;
  192.       }
  193.       break;
  194.  
  195.     default:   /* Ignore any other event */
  196.       break;
  197.   }
  198. }
  199.  
  200. BOOL event_processor(wimp_eventstr *e,void *handle)
  201. {
  202.         char *name;
  203.         wimp_w w;
  204.         int    bind;
  205.  
  206.         if (xferrecv_checkinsert(&name) == MultiModule_FileType)
  207.         {
  208.                 xferrecv_insertfileok();
  209.                 if ((bind = lookup_window_bind(w = e->data.msg.data.dataload.w)) < 0)
  210.                         load_file(name);
  211.                 else
  212.                         insert_file(bind,name);
  213.                 return TRUE;
  214.         }
  215.         return FALSE;
  216. }
  217.  
  218. void iconbarload(wimp_eventstr *e,void *h) { event_processor(e,h); }
  219.  
  220. /****************************** INITIALISATION ******************************/
  221.  
  222. /*--- Initialise the program, returning TRUE if it was all OK. ---*/
  223. BOOL example_initialise(void)
  224. {
  225.   /* RISC_OSlib initialisation */
  226.   wimpt_init("Multi Module");      /* Main Wimp initialisation */
  227.   flex_init();                     /* Memory management        */
  228.   res_init("MultiMod");            /* Resources */
  229.   resspr_init();                   /* Application sprites */
  230.   template_init();                 /* Templates */
  231.   dbox_init();                     /* Dialogue boxes */
  232.   data_init();                     /* Our data structure */
  233.  
  234.   /* Create the menu subtree */
  235.   if ((disp_menu = menu_new("Display",DISPMENU)) == NULL)
  236.     return FALSE; /* Menu create failed */
  237.  
  238.   /* Create the menu tree */
  239.   if (bar_menu = menu_new("Multi Module", ">Info,>New File,Quit"), bar_menu == NULL)
  240.     return FALSE; /* Menu create failed */
  241.  
  242.   /* Set up the icon on the icon bar, and declare its event handlers */
  243.   baricon("!multimod", (int)resspr_area(), example_iconclick);
  244.   if (!event_attachmenu(win_ICONBAR, bar_menu, example_menuproc, 0))
  245.     return FALSE; /* Unable to attach menu */
  246.  
  247.   dboxquery(0);
  248.  
  249.   win_add_unknown_event_processor(event_processor,0);
  250.   win_register_event_handler(win_ICONBARLOAD, iconbarload, 0);
  251.  
  252.   /* All went ok */
  253.   return TRUE;
  254. }
  255.  
  256. /******************************* MAIN PROGRAM ********************************/
  257.  
  258. /*--- Main entry point. ---*/
  259. int main(int argc,char **argv)
  260. {
  261.   int loop;
  262.  
  263.   if (example_initialise())
  264.   {
  265.     if ((loop = argc) > 1)
  266.     {
  267.         if (strcmpic((UC *)argv[1],(UC *)"-print") == 0)
  268.         {
  269.                 if (load_file(argv[1])) print_info("<Printer$Temp>",0);
  270.                 return 0;
  271.         }
  272.         while (loop > 1) load_file(argv[--loop]);
  273.     }
  274.  
  275.     /* The main event loop */
  276.     while (TRUE)
  277.       event_process();
  278.   }
  279.   return 0;
  280. }
  281.