home *** CD-ROM | disk | FTP | other *** search
- // **************************************************************************
- // Copyright 1996 David Allison
- //
- // VV VV IIIIII SSSSS TTTTTT AA
- // VV VV II SS TT AA AA
- // VV VV II SSSS TT AA AA
- // VV VV II SS TT AAAAAAAA
- // VV IIIIII SSSS TT AA AA
- //
- // MULTI-THREADED C++ WIMP CLASS LIBRARY
- // for RISC OS
- // **************************************************************************
- //
- // P U B L I C D O M A I N L I C E N C E
- // -------------------------------------------
- //
- // This library is copyright. You may not sell the library for
- // profit, but you may sell products which use it providing
- // those products are presented as executable code and are not
- // libraries themselves. The library is supplied without any
- // warranty and the copyright owner cannot be held responsible for
- // damage resulting from failure of any part of this library.
- //
- // See the User Manual for details of the licence.
- //
- // *************************************************************************
-
- //
- // Example of a SaveBox
- //
- // This example program primarily shows how to use the DataSave class to
- // save data into a file. It also uses the FontObject class to display
- // some text in a window
- //
-
- #include "SaveEx.h"
- #include <kernel.h>
- #include <swis.h>
- #include <string.h>
-
- //
- // define the main program variable
- //
-
- SaveEx *save_ex ;
-
- // ---------------------------------------------------------------------
- // MainWindow class constructor - called when the main window is created
- // ---------------------------------------------------------------------
-
-
- MainWindow::MainWindow (Task *task)
- : Window (task, "main", "mainmenu") // template = "main", menu = "mainmenu"
- {
- font_handle = task->open_font ("Homerton.Medium", 12, 12) ; // open the font
- text = new FontObject (this, "font", font_handle, 100, -100, 500) ; // create font object
- text->set_font (font_handle) ;
- text->add_text ("This is an object containing text. The object can be moved ") ;
- text->add_text ("around the screen by dragging. The text is displayed in an outline font") ;
- text->finish() ; // display the object
- saver = new Saver (task, this) ;
- }
-
- //
- // MainWindow destructor
- //
-
- MainWindow::~MainWindow()
- {
- delete saver ;
- }
-
- //
- // MainWindow menu handler. This is called when there is a menu click on the
- // menu supplied as default for the class. The function parameter is an
- // array of MenuItem structures.
- //
-
- void MainWindow::menu(MenuItem items[])
- {
- switch (items[0]) // uses conversion operator in MenuItem
- {
- case SAVE:
- { // MUST be in a block as we are creating new variables
- SaveBox save (task, "xfer_send", saver->path(), "File", 0xfff, saver) ; // the save box
- save.set_title ("Save info") ; // set the title on the box
- save.show() ; // show the box
- task->sleep (&save) ; // sleep until save terminates
- break ;
- }
- }
- }
-
- //
- // This is called by the saver in order to save whatever the window wants saved
- //
-
- void MainWindow::save (FILE *fp)
- {
- fprintf (fp,"window: 0x%x\n",this) ;
- fprintf (fp," x0: %d\n",x0) ;
- fprintf (fp," y0: %d\n",y0) ;
- fprintf (fp," x1: %d\n",x1) ;
- fprintf (fp," y1: %d\n",y1) ;
- fprintf (fp," scx: %d\n",scx) ;
- fprintf (fp," scy: %d\n",scy) ;
- }
-
- // ----------------------------------------------------------------------
- // This is the Saver class which is used by the SaveBox to save something
- // ----------------------------------------------------------------------
-
-
- Saver::Saver (Task *task,MainWindow *window)
- : DataSave (task)
- {
- this->window = window ;
- previous_path[0] = '\0' ; // no path yet
- }
-
- Saver::~Saver()
- {
- }
-
- //
- // this function is called by the Channel in order to signal the receipt of a message
- //
-
- void Saver::receive (int action, int task, int my_ref, int your_ref, int data_length, void *data)
- {
- switch ((Task::events)action) // switch on message type
- {
- case Task::Message_DataSaveAck: // save to this file
- DataSave::saveackdata *s = data ;
- save (s->pathname) ;
- dataload (task, your_ref, my_ref, data_length, data) ; // send back dataload message
- break ;
-
- case Task::Message_DataLoadAck: // file has been saved
- break ;
- }
- }
-
- //
- // called by the SaveBox to save to a window and icon. The leaf is passed
- //
-
-
- void Saver::save (int window, int icon, int x, int y, char *leaf)
- {
- datasave (window, icon, x, y, 1024, 0xfff, leaf) ; // start datasave protocol
- }
-
- //
- // called by either the savebox (if the user has typed in a path), or the datasave
- // protocol (receive function). At this point we know the full path name
- // for the file, so just save it
- //
-
- void Saver::save (char *path)
- {
- FILE *fp = fopen (path, "w") ;
- if (fp == NULL)
- throw ("Cant open file") ;
- strcpy (previous_path, path) ; // copy into previous path
- window->save (fp) ;
- fclose (fp) ;
- }
-
-
- // ************************************************************************
- // The main task class itself
- // ************************************************************************
-
-
- SaveEx::SaveEx()
- : Task ("Save Example", "SaveEx", "!SaveEx", "iconbar")
- {
- mainwin = new MainWindow (this) ; // create the main window
- }
-
- //
- // icon bar click on our icon
- //
-
- void SaveEx::click (int x, int y, int button, int icon)
- {
- mainwin->do_open() ; // just open the main window
- }
-
- //
- // menu hit on the iconbar menu
- //
-
-
- void SaveEx::menu(MenuItem items[])
- {
- switch (items[0])
- {
- case INFO:
- { // need these braces
- ProgramInfo proginfo (this, 4, "1.0 (13 Dec 1995)") ;
- proginfo.show() ;
- sleep (&proginfo) ;
- break ;
- }
- case QUIT:
- exit() ; // exit task (Task::exit())
- break ;
- }
- }
-
-
- main()
- {
- #ifdef __EASY_C
- try
- #endif
- {
- save_ex = new SaveEx() ; // create the task
- save_ex->run() ; // run it
- }
- #ifdef __EASY_C
- catch (_kernel_oserror *e)
- {
- _kernel_swi_regs r ;
- r.r[0] = (int)e ;
- r.r[1] = 0 ;
- r.r[2] = (int)"SaveEx" ;
- _kernel_swi (Wimp_ReportError, &r, &r) ;
- }
-
- catch (char *s)
- {
- _kernel_oserror err ;
- _kernel_swi_regs r ;
- err.errnum = 0 ;
- strcpy (err.errmess, s) ;
- r.r[0] = (int)&err ;
- r.r[1] = 0 ;
- r.r[2] = (int)"SaveEx" ;
- _kernel_swi (Wimp_ReportError, &r, &r) ;
- }
- #endif
- }
-