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 DialogueBox
- //
-
- #include "myapps.h"
- #include <kernel.h>
- #include <swis.h>
- #include <string.h>
- #include <math.h>
-
- //
- // define the main program variable
- //
-
- MyApps *myapps ;
-
-
- AppObject::AppObject (Task *task, char *n)
- {
- int len = strlen (n) + 1 ;
- name = new char[len] ;
- strcpy (name, n) ;
- sprite = NULL ;
- this->task = task ;
- }
-
- AppObject::AppObject (Task *task, char *n, char *s)
- {
- int len = strlen (n) + 1 ;
- name = new char[len] ;
- strcpy (name, n) ;
- len = strlen (s) + 1 ;
- sprite = new char[len] ;
- strcpy (sprite, s) ;
- this->task = task ;
- }
-
- AppObject::~AppObject()
- {
- delete name ;
- delete sprite ;
- }
-
-
- void AppObject::rename (char *newname)
- {
- int len = strlen (newname) ;
- delete name ;
- name = new char[len] ;
- strcpy (name, newname) ;
- }
-
-
- PathAppObject::PathAppObject (Task *task, char *n, char *path)
- : AppObject (task, n)
- {
- strcpy (this->path, path) ;
- }
-
-
- PathAppObject::~PathAppObject()
- {
- }
-
-
-
-
- ApplicationAppObject::ApplicationAppObject (Task *task, char *n, char *path)
- : PathAppObject (task, n, path)
- {
- sprite = new char [strlen (n) + 2] ;
- sprintf (sprite,"%s",n) ;
- }
-
- ApplicationAppObject::~ApplicationAppObject()
- {
- }
-
- void ApplicationAppObject::run()
- {
- task->spawn ("run %s",path) ;
- }
-
-
- DirAppObject::DirAppObject (Task *task, char *n, char *path)
- : PathAppObject (task, n, path)
- {
- sprite = "directory" ;
- }
-
-
- DirAppObject::~DirAppObject()
- {
- }
-
-
- void DirAppObject::run()
- {
- task->spawn ("filer_opendir %s",path) ;
- }
-
-
- FileAppObject::FileAppObject (Task *task, char *n, char *path, int type)
- : PathAppObject (task, n, path)
- {
- sprite = new char [12] ;
- if (type == -1)
- sprintf (sprite,"file_xxx") ;
- else
- sprintf (sprite,"file_%x",type & 0xfff) ;
- }
-
-
- FileAppObject::~FileAppObject()
- {
- }
-
-
- void FileAppObject::run()
- {
- task->spawn ("run %s",path) ;
- }
-
-
- GridAppObject::GridAppObject (Task *task, char *n, AppGrid *grid)
- : AppObject (task, n)
- {
- this->grid = grid ;
- sprite = "directory" ;
- }
-
-
- GridAppObject::~GridAppObject()
- {
- }
-
- void GridAppObject::run()
- {
- grid->do_open() ;
- }
-
- void GridAppObject::rename (char *newname)
- {
- AppObject::rename (newname) ;
- grid->set_title (newname) ;
- }
-
- // -----------------------------------------------------------------
- // The AppGrid class
- // -----------------------------------------------------------------
-
- AppGrid::AppGrid (MyApps *myapps, bool ismain)
- : IconGrid (myapps,"grid",0,"main")
- {
- set_flag (SORTED) ;
- this->myapps = myapps ;
- if (!ismain)
- myapps->add_grid (this) ;
- this->ismain = ismain ;
- }
-
- AppGrid::~AppGrid()
- {
- if (!ismain)
- myapps->remove_grid(this) ;
- }
-
- void AppGrid::menu (MenuItem items[])
- {
- char name[256] ;
- switch (items[0])
- {
- case APP:
- if (num_selected ==0 && current_object == NULL)
- return ;
- switch (items[1])
- {
- case RENAME:
- items[2].read (name) ;
- current_object->rename (name) ;
- current_icon->write (name) ;
- break ;
- case REMOVE:
- if (num_selected <= 1)
- delete current_icon ;
- else
- {
- Icon *nexti ;
- clear_flag (SORTED) ;
- for (Icon *i = icons ; i != NULL ; i = nexti)
- {
- nexti = i->next ;
- if (i->is_selected())
- delete i ;
- }
- set_flag (SORTED) ;
- sort() ;
- }
- break ;
- }
- break ;
- case NEW:
- items[1].read (name) ;
- AppGrid *grid = new AppGrid (myapps) ;
- grid->set_title (name) ;
- add_object (grid,name) ;
- break ;
- case SELECT_ALL:
- select_all() ;
- break ;
- case CLEAR_SEL:
- clear_selection() ;
- break ;
- case SET_NAME:
- items[1].read (name) ;
- set_title (name) ;
- break ;
- }
- }
-
-
- void AppGrid::pre_menu(Menu *m, int x, int y, int button, int icon)
- {
- MenuItem *item = m->item ("app") ;
- Menu *app_menu = task->find_menu("app") ;
- if (app_menu == NULL)
- throw ("No app menu") ;
- MenuItem *rename_item ;
- MenuItem *new_item ;
- rename_item = app_menu->item("rename") ;
- new_item = m->item ("new") ;
- if (ismain)
- new_item->unfade() ;
- else
- new_item->fade() ;
- num_selected = 0 ;
- Icon *selected_icon ;
- for (Icon *i = icons ; i != NULL ; i = i->next)
- if (i->is_selected())
- {
- num_selected++ ;
- selected_icon = i ;
- }
- if (num_selected > 1)
- {
- rename_item->fade() ;
- item->print ("Selection") ;
- item->unfade() ;
- return ;
- }
- rename_item->unfade() ;
- if (num_selected == 1)
- {
- current_icon = selected_icon ;
- current_object = current_icon->user_ref ;
- item->unfade() ;
- item->print ("Object '%s'",current_object->name) ;
- return ;
- }
- current_icon = Window::find_icon (icon) ;
- if (current_icon == NULL)
- {
- current_object = NULL ;
- item->print ("Object ''") ;
- item->fade() ;
- return ;
- }
- current_object = current_icon->user_ref ;
- item->unfade() ;
- item->print ("Object '%s'",current_object->name) ;
- }
-
- void AppGrid::double_click (int mx, int my, int buttons, Icon *icon)
- {
- AppObject *object = icon->user_ref ;
- object->run() ;
- }
-
- void AppGrid::add_object (char *path)
- {
- _kernel_swi_regs r ;
- _kernel_oserror *e ;
- r.r[0] = 23 ;
- r.r[1] = (int)path ;
- if ((e = _kernel_swi (OS_File, &r, &r)) != NULL)
- throw (e) ;
- if (r.r[0] == 0)
- throw ("File not found") ;
- char *leaf = strrchr (path,'.') ;
- leaf++ ;
- AppObject *object ;
- switch (r.r[6])
- {
- case -1:
- default:
- object = new FileAppObject (task, leaf, path, r.r[6]) ;
- break ;
- case 0x1000:
- object = new DirAppObject (task, leaf, path) ;
- break ;
- case 0x2000:
- object = new ApplicationAppObject (task, leaf, path) ;
- break ;
- }
- Icon *ic = insert_icon (leaf, object) ;
- ic->change_sprite (object->sprite) ;
- }
-
- void AppGrid::add_object (AppGrid *grid, char *name)
- {
- AppObject *object = new GridAppObject (task, name, grid) ;
- insert_icon (name, object) ;
- }
-
-
- Loader::Loader (MyApps *myapps) : DataSave (myapps)
- {
- this->myapps = myapps ;
- }
-
- Loader::~Loader()
- {
- }
-
-
-
- void Loader::receive (int action, int task, int my_ref, int your_ref, int data_length, void *data)
- {
- switch ((Task::events)action)
- {
- case Task::Message_DataSave: // save to this file
- datasaveack (your_ref, data, "<Wimp$Scrap>") ;
- break ;
- case Task::Message_DataLoad:
- DataSave::saveackdata *s = data ;
- AppGrid *grid = myapps->find_grid (s->window) ;
- if (grid != NULL)
- grid->add_object (s->pathname) ;
- dataloadack (task, your_ref, my_ref, data_length, data) ;
- break ;
- }
- }
-
-
-
- // ************************************************************************
- // The main task class itself
- // ************************************************************************
-
-
- MyApps::MyApps()
- : Task ("Favourite Apps", "MyApps", "!MyApps", "iconbar", 0, -2) // on left of icon bar
- {
- grids = NULL ;
- last_grid = NULL ;
- loader = new Loader (this) ;
- num_grids = 0 ;
- main_grid = new AppGrid (this, true) ;
- main_grid->set_title ("Favourite Applications") ;
- }
-
- MyApps::~MyApps()
- {
- AppGrid *grid, *next ;
- for (grid = grids ; grid != NULL ; grid = next)
- {
- next = grid->next ;
- delete grid ;
- }
- delete loader ;
- delete main_grid ;
- }
-
- void MyApps::click(int x, int y, int buttons, int icon)
- {
- main_grid->do_open() ;
- }
-
-
- //
- // menu hit on the iconbar menu
- //
-
-
- void MyApps::menu(MenuItem items[])
- {
- switch (items[0])
- {
- case INFO:
- { // need these braces
- ProgramInfo proginfo (this, 4, "1.0 (11 Jan 1996)") ;
- proginfo.show() ;
- sleep (&proginfo) ;
- break ;
- }
- case QUIT:
- exit() ; // exit task (Task::exit())
- break ;
- }
- }
-
-
- AppGrid *MyApps::find_grid (int handle)
- {
- AppGrid *grid ;
- if (handle == main_grid->handle)
- return main_grid ;
- for (grid = grids ; grid != NULL ; grid = grid->next)
- if (grid->handle == handle)
- return grid ;
- return NULL ;
- }
-
- void MyApps::add_grid(AppGrid *g)
- {
- g->next = NULL ;
- if (grids == NULL)
- grids = last_grid = g ;
- else
- {
- last_grid->next = g ;
- g->prev = last_grid ;
- last_grid = g ;
- }
- num_grids++ ;
- }
-
- void MyApps::remove_grid(AppGrid *g)
- {
- if (g->prev == NULL)
- grids = g->next ;
- else
- g->prev->next = g->next ;
- if (g->next == NULL)
- last_grid = g->prev ;
- else
- g->next->prev = g->prev ;
- num_grids-- ;
- }
-
- main()
- {
- #ifdef __EASY_C
- try
- #endif
- {
- myapps = new MyApps() ; // create the task
- myapps->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)"MyApps" ;
- _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)"MyApps" ;
- _kernel_swi (Wimp_ReportError, &r, &r) ;
- }
- #endif
- }
-