home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / v / vista / Examples / !MyApps / c / myapps
Encoding:
Text File  |  1996-02-01  |  10.9 KB  |  500 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 DialogueBox
  30. //
  31.  
  32. #include "myapps.h"
  33. #include <kernel.h>
  34. #include <swis.h>
  35. #include <string.h>
  36. #include <math.h>
  37.  
  38. //
  39. // define the main program variable
  40. //
  41.  
  42. MyApps *myapps ;
  43.  
  44.  
  45. AppObject::AppObject (Task *task, char *n)
  46.    {
  47.    int len = strlen (n) + 1 ;
  48.    name = new char[len] ;
  49.    strcpy (name, n) ;
  50.    sprite = NULL ;
  51.    this->task = task ;
  52.    }
  53.  
  54. AppObject::AppObject (Task *task, char *n, char *s)
  55.    {
  56.    int len = strlen (n) + 1 ;
  57.    name = new char[len] ;
  58.    strcpy (name, n) ;
  59.    len = strlen (s) + 1 ;
  60.    sprite = new char[len] ;
  61.    strcpy (sprite, s) ;
  62.    this->task = task ;
  63.    }
  64.  
  65. AppObject::~AppObject()
  66.    {
  67.    delete name ;
  68.    delete sprite ;
  69.    }
  70.  
  71.  
  72. void AppObject::rename (char *newname)
  73.    {
  74.    int len = strlen (newname) ;
  75.    delete name ;
  76.    name = new char[len] ;
  77.    strcpy (name, newname) ;
  78.    }
  79.  
  80.  
  81. PathAppObject::PathAppObject (Task *task, char *n, char *path)
  82.    : AppObject (task, n)
  83.    {
  84.    strcpy (this->path, path) ;
  85.    }
  86.  
  87.  
  88. PathAppObject::~PathAppObject()
  89.    {
  90.    }
  91.  
  92.  
  93.  
  94.  
  95. ApplicationAppObject::ApplicationAppObject (Task *task, char *n, char *path)
  96.    : PathAppObject (task, n, path)
  97.    {
  98.    sprite = new char [strlen (n) + 2] ;
  99.    sprintf (sprite,"%s",n) ;
  100.    }
  101.  
  102. ApplicationAppObject::~ApplicationAppObject()
  103.    {
  104.    }
  105.  
  106. void ApplicationAppObject::run()
  107.    {
  108.    task->spawn ("run %s",path) ;
  109.    }
  110.  
  111.  
  112. DirAppObject::DirAppObject (Task *task, char *n, char *path)
  113.    : PathAppObject (task, n, path)
  114.    {
  115.    sprite = "directory" ;
  116.    }
  117.  
  118.  
  119. DirAppObject::~DirAppObject()
  120.    {
  121.    }
  122.  
  123.  
  124. void DirAppObject::run()
  125.    {
  126.    task->spawn ("filer_opendir %s",path) ;
  127.    }
  128.  
  129.  
  130. FileAppObject::FileAppObject (Task *task, char *n, char *path, int type)
  131.    : PathAppObject (task, n, path)
  132.    {
  133.    sprite = new char [12] ;
  134.    if (type == -1)
  135.       sprintf (sprite,"file_xxx") ;
  136.    else
  137.       sprintf (sprite,"file_%x",type & 0xfff) ;
  138.    }
  139.  
  140.  
  141. FileAppObject::~FileAppObject()
  142.    {
  143.    }
  144.  
  145.  
  146. void FileAppObject::run()
  147.    {
  148.    task->spawn ("run %s",path) ;
  149.    }
  150.  
  151.  
  152. GridAppObject::GridAppObject (Task *task, char *n, AppGrid *grid)
  153.    : AppObject (task, n)
  154.    {
  155.    this->grid = grid ;
  156.    sprite = "directory" ;
  157.    }
  158.  
  159.  
  160. GridAppObject::~GridAppObject()
  161.    {
  162.    }
  163.  
  164. void GridAppObject::run()
  165.    {
  166.    grid->do_open() ;
  167.    }
  168.  
  169. void GridAppObject::rename (char *newname)
  170.    {
  171.    AppObject::rename (newname) ;
  172.    grid->set_title (newname) ;
  173.    }
  174.  
  175. // -----------------------------------------------------------------
  176. // The AppGrid class
  177. // -----------------------------------------------------------------
  178.  
  179. AppGrid::AppGrid (MyApps *myapps, bool ismain)
  180.    : IconGrid (myapps,"grid",0,"main")
  181.    {
  182.    set_flag (SORTED) ;
  183.    this->myapps = myapps ;
  184.    if (!ismain)
  185.       myapps->add_grid (this) ;
  186.    this->ismain = ismain ;
  187.    }
  188.  
  189. AppGrid::~AppGrid()
  190.    {
  191.    if (!ismain)
  192.       myapps->remove_grid(this) ;
  193.    }
  194.  
  195. void AppGrid::menu (MenuItem items[])
  196.    {
  197.    char name[256] ;
  198.    switch (items[0])
  199.       {
  200.       case APP:
  201.          if (num_selected ==0 && current_object == NULL)
  202.             return ;
  203.          switch (items[1])
  204.             {
  205.             case RENAME:
  206.                items[2].read (name) ;
  207.            current_object->rename (name) ;
  208.            current_icon->write (name) ;
  209.            break ;
  210.             case REMOVE:
  211.                if (num_selected <= 1)
  212.                   delete current_icon ;
  213.                else
  214.                   {
  215.                   Icon *nexti ;
  216.                   clear_flag (SORTED) ;
  217.                   for (Icon *i = icons ; i != NULL ; i = nexti)
  218.                      {
  219.                      nexti = i->next ;
  220.                      if (i->is_selected())
  221.                 delete i ;
  222.                      }
  223.                   set_flag (SORTED) ;
  224.                   sort() ;
  225.                   }
  226.                break ;
  227.             }
  228.          break ;
  229.       case NEW:
  230.          items[1].read (name) ;
  231.      AppGrid *grid = new AppGrid (myapps) ;
  232.      grid->set_title (name) ;
  233.      add_object (grid,name) ;
  234.      break ;
  235.       case SELECT_ALL:
  236.          select_all() ;
  237.          break ;
  238.       case CLEAR_SEL:
  239.          clear_selection() ;
  240.          break ;
  241.       case SET_NAME:
  242.          items[1].read (name) ;
  243.          set_title (name) ;
  244.          break ;
  245.       }
  246.    }
  247.  
  248.  
  249. void AppGrid::pre_menu(Menu *m, int x, int y, int button, int icon)
  250.    {
  251.    MenuItem *item = m->item ("app") ;
  252.    Menu *app_menu = task->find_menu("app") ;
  253.    if (app_menu == NULL)
  254.       throw ("No app menu") ;
  255.    MenuItem *rename_item ;
  256.    MenuItem *new_item ;
  257.    rename_item = app_menu->item("rename") ;
  258.    new_item = m->item ("new") ;
  259.    if (ismain)
  260.       new_item->unfade() ;
  261.    else
  262.       new_item->fade() ;
  263.    num_selected = 0 ;
  264.    Icon *selected_icon ;
  265.    for (Icon *i = icons ; i != NULL ; i = i->next)
  266.       if (i->is_selected())
  267.          {
  268.          num_selected++ ;
  269.          selected_icon = i ;
  270.          }
  271.    if (num_selected > 1)
  272.       {
  273.       rename_item->fade() ;
  274.       item->print ("Selection") ;
  275.       item->unfade() ;
  276.       return ;
  277.       }
  278.    rename_item->unfade() ;
  279.    if (num_selected == 1)
  280.       {
  281.       current_icon = selected_icon ;
  282.       current_object = current_icon->user_ref ;
  283.       item->unfade() ;
  284.       item->print ("Object '%s'",current_object->name) ;
  285.       return ;
  286.       }
  287.    current_icon = Window::find_icon (icon) ;
  288.    if (current_icon == NULL)
  289.       {
  290.       current_object = NULL ;
  291.       item->print ("Object ''") ;
  292.       item->fade() ;
  293.       return ;
  294.       }
  295.    current_object = current_icon->user_ref ;
  296.    item->unfade() ;
  297.    item->print ("Object '%s'",current_object->name) ;
  298.    }
  299.  
  300. void AppGrid::double_click (int mx, int my, int buttons, Icon *icon)
  301.    {
  302.    AppObject *object = icon->user_ref ;
  303.    object->run() ;
  304.    }
  305.  
  306. void AppGrid::add_object (char *path)
  307.    {
  308.    _kernel_swi_regs r ;
  309.    _kernel_oserror *e ;
  310.    r.r[0] = 23 ;
  311.    r.r[1] = (int)path ;
  312.    if ((e = _kernel_swi (OS_File, &r, &r)) != NULL)
  313.       throw (e) ;
  314.    if (r.r[0] == 0)
  315.       throw ("File not found") ;
  316.    char *leaf = strrchr (path,'.') ;
  317.    leaf++ ;
  318.    AppObject *object ;
  319.    switch (r.r[6])
  320.       {
  321.       case -1:
  322.       default:
  323.          object = new FileAppObject (task, leaf, path, r.r[6]) ;
  324.          break ;
  325.       case 0x1000:
  326.          object = new DirAppObject (task, leaf, path) ;
  327.          break ;
  328.       case 0x2000:
  329.          object = new ApplicationAppObject (task, leaf, path) ;
  330.          break ;
  331.       }
  332.    Icon *ic = insert_icon (leaf, object) ;
  333.    ic->change_sprite (object->sprite) ;
  334.    }
  335.  
  336. void AppGrid::add_object (AppGrid *grid, char *name)
  337.    {
  338.    AppObject *object = new GridAppObject (task, name, grid) ;
  339.    insert_icon (name, object) ;
  340.    }
  341.  
  342.  
  343. Loader::Loader (MyApps *myapps) : DataSave (myapps)
  344.    {
  345.    this->myapps = myapps ;
  346.    }
  347.  
  348. Loader::~Loader()
  349.    {
  350.    }
  351.  
  352.  
  353.  
  354. void Loader::receive (int action, int task, int my_ref, int your_ref, int data_length, void *data)
  355.    {
  356.    switch ((Task::events)action)
  357.       {
  358.       case Task::Message_DataSave:             // save to this file
  359.          datasaveack (your_ref, data, "<Wimp$Scrap>") ;
  360.          break ;
  361.       case Task::Message_DataLoad:
  362.          DataSave::saveackdata *s = data ;
  363.          AppGrid *grid = myapps->find_grid (s->window) ;
  364.          if (grid != NULL)
  365.             grid->add_object (s->pathname) ;
  366.          dataloadack (task, your_ref, my_ref, data_length, data) ;
  367.          break ;
  368.       }
  369.    }
  370.  
  371.  
  372.  
  373. // ************************************************************************
  374. // The main task class itself
  375. // ************************************************************************
  376.  
  377.  
  378. MyApps::MyApps()
  379.    : Task ("Favourite Apps", "MyApps", "!MyApps", "iconbar", 0, -2)  // on left of icon bar
  380.    {
  381.    grids = NULL ;
  382.    last_grid = NULL ;
  383.    loader = new Loader (this) ;
  384.    num_grids = 0 ;
  385.    main_grid = new AppGrid (this, true) ;
  386.    main_grid->set_title ("Favourite Applications") ;
  387.    }
  388.  
  389. MyApps::~MyApps()
  390.    {
  391.    AppGrid *grid, *next ;
  392.    for (grid = grids ; grid != NULL ; grid = next)
  393.       {
  394.       next = grid->next ;
  395.       delete grid ;
  396.       }
  397.    delete loader ;
  398.    delete main_grid ;
  399.    }
  400.  
  401. void MyApps::click(int x, int y, int buttons, int icon)
  402.    {
  403.    main_grid->do_open() ;
  404.    }
  405.  
  406.  
  407. //
  408. // menu hit on the iconbar menu
  409. //
  410.  
  411.  
  412. void MyApps::menu(MenuItem items[])
  413.    {
  414.    switch (items[0])
  415.       {
  416.       case INFO:
  417.          {                          // need these braces
  418.          ProgramInfo proginfo (this, 4, "1.0 (11 Jan 1996)") ;
  419.          proginfo.show() ;
  420.          sleep (&proginfo) ;
  421.          break ;
  422.          }
  423.       case QUIT:
  424.          exit() ;             // exit task (Task::exit())
  425.          break ;
  426.       }
  427.    }
  428.  
  429.  
  430. AppGrid *MyApps::find_grid (int handle)
  431.    {
  432.    AppGrid *grid ;
  433.    if (handle == main_grid->handle)
  434.       return main_grid ;
  435.    for (grid = grids ; grid != NULL ; grid = grid->next)
  436.       if (grid->handle == handle)
  437.          return grid ;
  438.    return NULL ;
  439.    }
  440.  
  441. void MyApps::add_grid(AppGrid *g)
  442.    {
  443.    g->next = NULL ;
  444.    if (grids == NULL)
  445.       grids = last_grid = g ;
  446.    else
  447.       {
  448.       last_grid->next = g ;
  449.       g->prev = last_grid ;
  450.       last_grid = g ;
  451.       }
  452.    num_grids++ ;
  453.    }
  454.  
  455. void MyApps::remove_grid(AppGrid *g)
  456.    {
  457.    if (g->prev == NULL)
  458.       grids = g->next ;
  459.    else
  460.       g->prev->next = g->next ;
  461.    if (g->next == NULL)
  462.       last_grid = g->prev ;
  463.    else
  464.       g->next->prev = g->prev ;
  465.    num_grids-- ;
  466.    }
  467.  
  468. main()
  469.    {
  470. #ifdef __EASY_C
  471.    try
  472. #endif
  473.       {
  474.       myapps = new MyApps() ;           // create the task
  475.       myapps->run() ;                   // run it
  476.       }
  477. #ifdef __EASY_C
  478.    catch (_kernel_oserror *e)
  479.       {
  480.       _kernel_swi_regs r ;
  481.       r.r[0] = (int)e ;
  482.       r.r[1] = 0 ;
  483.       r.r[2] = (int)"MyApps" ;
  484.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  485.       }
  486.  
  487.    catch (char *s)
  488.       {
  489.       _kernel_oserror err ;
  490.       _kernel_swi_regs r ;
  491.       err.errnum = 0 ;
  492.       strcpy (err.errmess, s) ;
  493.       r.r[0] = (int)&err ;
  494.       r.r[1] = 0 ;
  495.       r.r[2] = (int)"MyApps" ;
  496.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  497.       }
  498. #endif
  499.    }
  500.