home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / v / vista / Examples / !SaveEx / c / SaveEx
Encoding:
Text File  |  1996-02-01  |  7.1 KB  |  246 lines

  1. // **************************************************************************
  2. //                     Copyright 1996 David Allison
  3. //
  4. //             VV    VV    IIIIII     SSSSS     TTTTTT       AA
  5. //             VV    VV      II      SS           TT       AA  AA
  6. //             VV    VV      II        SSSS       TT      AA    AA
  7. //              VV  VV       II           SS      TT      AAAAAAAA
  8. //                VV       IIIIII     SSSS        TT      AA    AA
  9. //
  10. //                    MULTI-THREADED C++ WIMP CLASS LIBRARY
  11. //                                for RISC OS
  12. // **************************************************************************
  13. //
  14. //             P U B L I C    D O M A I N    L I C E N C E
  15. //             -------------------------------------------
  16. //
  17. //     This library is copyright. You may not sell the library for
  18. //     profit, but you may sell products which use it providing
  19. //     those products are presented as executable code and are not
  20. //     libraries themselves.  The library is supplied without any
  21. //     warranty and the copyright owner cannot be held responsible for
  22. //     damage resulting from failure of any part of this library.
  23. //
  24. //          See the User Manual for details of the licence.
  25. //
  26. // *************************************************************************
  27.  
  28. //
  29. // Example of a SaveBox
  30. //
  31. // This example program primarily shows how to use the DataSave class to
  32. // save data into a file.  It also uses the FontObject class to display
  33. // some text in a window
  34. //
  35.  
  36. #include "SaveEx.h"
  37. #include <kernel.h>
  38. #include <swis.h>
  39. #include <string.h>
  40.  
  41. //
  42. // define the main program variable
  43. //
  44.  
  45. SaveEx *save_ex ;
  46.  
  47. // ---------------------------------------------------------------------
  48. // MainWindow class constructor - called when the main window is created
  49. // ---------------------------------------------------------------------
  50.  
  51.  
  52. MainWindow::MainWindow (Task *task)
  53.    : Window (task, "main", "mainmenu")          // template = "main", menu = "mainmenu"
  54.    {
  55.    font_handle = task->open_font ("Homerton.Medium", 12, 12) ;           // open the font
  56.    text = new FontObject (this, "font", font_handle, 100, -100, 500) ;   // create font object
  57.    text->set_font (font_handle) ;
  58.    text->add_text ("This is an object containing text.  The object can be moved ") ;
  59.    text->add_text ("around the screen by dragging.  The text is displayed in an outline font") ;
  60.    text->finish() ;                                                      // display the object
  61.    saver = new Saver (task, this) ;
  62.    }
  63.  
  64. //
  65. // MainWindow destructor
  66. //
  67.  
  68. MainWindow::~MainWindow()
  69.    {
  70.    delete saver ;
  71.    }
  72.  
  73. //
  74. // MainWindow menu handler.  This is called when there is a menu click on the
  75. // menu supplied as default for the class.  The function parameter is an
  76. // array of MenuItem structures.
  77. //
  78.  
  79. void MainWindow::menu(MenuItem items[])
  80.    {
  81.    switch (items[0])              // uses conversion operator in MenuItem
  82.       {
  83.       case SAVE:
  84.          {                         // MUST be in a block as we are creating new variables
  85.          SaveBox save (task, "xfer_send", saver->path(), "File", 0xfff, saver) ;  // the save box
  86.          save.set_title ("Save info") ;                          // set the title on the box
  87.          save.show() ;                                           // show the box
  88.          task->sleep (&save) ;                                   // sleep until save terminates
  89.          break ;
  90.          }
  91.       }
  92.    }
  93.  
  94. //
  95. // This is called by the saver in order to save whatever the window wants saved
  96. //
  97.  
  98. void MainWindow::save (FILE *fp)
  99.    {
  100.    fprintf (fp,"window: 0x%x\n",this) ;
  101.    fprintf (fp,"   x0: %d\n",x0) ;
  102.    fprintf (fp,"   y0: %d\n",y0) ;
  103.    fprintf (fp,"   x1: %d\n",x1) ;
  104.    fprintf (fp,"   y1: %d\n",y1) ;
  105.    fprintf (fp,"   scx: %d\n",scx) ;
  106.    fprintf (fp,"   scy: %d\n",scy) ;
  107.    }
  108.  
  109. // ----------------------------------------------------------------------
  110. // This is the Saver class which is used by the SaveBox to save something
  111. // ----------------------------------------------------------------------
  112.  
  113.  
  114. Saver::Saver (Task *task,MainWindow *window)
  115.    : DataSave (task)
  116.    {
  117.    this->window = window ;
  118.    previous_path[0] = '\0' ;        // no path yet
  119.    }
  120.  
  121. Saver::~Saver()
  122.    {
  123.    }
  124.  
  125. //
  126. // this function is called by the Channel in order to signal the receipt of a message
  127. //
  128.  
  129. void Saver::receive (int action, int task, int my_ref, int your_ref, int data_length, void *data)
  130.    {
  131.    switch ((Task::events)action)                  // switch on message type
  132.       {
  133.       case Task::Message_DataSaveAck:             // save to this file
  134.          DataSave::saveackdata *s = data ;
  135.          save (s->pathname) ;
  136.          dataload (task, your_ref, my_ref, data_length, data) ;    // send back dataload message
  137.          break ;
  138.  
  139.       case Task::Message_DataLoadAck:                     // file has been saved
  140.          break ;
  141.       }
  142.    }
  143.  
  144. //
  145. //  called by the SaveBox to save to a window and icon.  The leaf is passed
  146. //
  147.  
  148.  
  149. void Saver::save (int window, int icon, int x, int y, char *leaf)
  150.    {
  151.    datasave (window, icon, x, y, 1024, 0xfff, leaf) ;            // start datasave protocol
  152.    }
  153.  
  154. //
  155. // called by either the savebox (if the user has typed in a path), or the datasave
  156. // protocol (receive function).  At this point we know the full path name
  157. // for the file, so just save it
  158. //
  159.  
  160. void Saver::save (char *path)
  161.    {
  162.    FILE *fp = fopen (path, "w") ;
  163.    if (fp == NULL)
  164.       throw ("Cant open file") ;
  165.    strcpy (previous_path, path) ;              // copy into previous path
  166.    window->save (fp) ;
  167.    fclose (fp) ;
  168.    }
  169.  
  170.  
  171. // ************************************************************************
  172. // The main task class itself
  173. // ************************************************************************
  174.  
  175.  
  176. SaveEx::SaveEx()
  177.    : Task ("Save Example", "SaveEx", "!SaveEx", "iconbar")
  178.    {
  179.    mainwin = new MainWindow (this) ;            // create the main window
  180.    }
  181.  
  182. //
  183. // icon bar click on our icon
  184. //
  185.  
  186. void SaveEx::click (int x, int y, int button, int icon)
  187.     {
  188.     mainwin->do_open() ;           // just open the main window
  189.     }
  190.  
  191. //
  192. // menu hit on the iconbar menu
  193. //
  194.  
  195.  
  196. void SaveEx::menu(MenuItem items[])
  197.    {
  198.    switch (items[0])
  199.       {
  200.       case INFO:
  201.          {                          // need these braces
  202.          ProgramInfo proginfo (this, 4, "1.0 (13 Dec 1995)") ;
  203.          proginfo.show() ;
  204.          sleep (&proginfo) ;
  205.          break ;
  206.          }
  207.       case QUIT:
  208.          exit() ;             // exit task (Task::exit())
  209.          break ;
  210.       }
  211.    }
  212.  
  213.  
  214. main()
  215.    {
  216. #ifdef __EASY_C
  217.    try
  218. #endif
  219.       {
  220.       save_ex = new SaveEx() ;           // create the task
  221.       save_ex->run() ;                   // run it
  222.       }
  223. #ifdef __EASY_C
  224.    catch (_kernel_oserror *e)
  225.       {
  226.       _kernel_swi_regs r ;
  227.       r.r[0] = (int)e ;
  228.       r.r[1] = 0 ;
  229.       r.r[2] = (int)"SaveEx" ;
  230.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  231.       }
  232.  
  233.    catch (char *s)
  234.       {
  235.       _kernel_oserror err ;
  236.       _kernel_swi_regs r ;
  237.       err.errnum = 0 ;
  238.       strcpy (err.errmess, s) ;
  239.       r.r[0] = (int)&err ;
  240.       r.r[1] = 0 ;
  241.       r.r[2] = (int)"SaveEx" ;
  242.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  243.       }
  244. #endif
  245.    }
  246.