home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 2 / AUCD2.iso / program / vista.arc / c / test < prev    next >
Text File  |  1996-02-01  |  17KB  |  729 lines

  1. //
  2. // main test
  3. //
  4.  
  5. // this program is used as a test for Vista development.  It is
  6. // not a particularly good example of the use of the library
  7. // but does use much of the facilities.  Hopefully
  8. // it will suffice until I write somethine better.
  9.  
  10. // David Allison  30/3/95
  11.  
  12. #include "Vista:vista.h"
  13. #include <stdio.h>
  14. #include <kernel.h>
  15. #include <swis.h>
  16. #include <string.h>
  17.  
  18. //
  19. // a little counter to show threading
  20. //
  21.  
  22. class Counter: public Window, public Thread
  23.    {
  24.    public:
  25.       Counter(Task *) ;
  26.       ~Counter() ;
  27.       void run() ;
  28.       void close () ;
  29.    private:
  30.       int count ;
  31.       Icon *display ;
  32.       int running ;
  33.    } ;
  34.  
  35. Counter::Counter(Task *t)
  36.    : Window (t, "counter"),
  37.      Thread ("counter")
  38.    {
  39.    running = 1 ;
  40.    count = 0 ;
  41.    display = new Icon (this, 0) ;
  42.    }
  43.  
  44. Counter::~Counter()
  45.    {
  46.    }
  47.  
  48. void Counter::run()
  49.    {
  50.    do_open() ;
  51.    while (running)
  52.       {
  53.       display->print ("%d",count++) ;
  54.       sleep (100) ;
  55.       }
  56.    do_close() ;
  57.    }
  58.  
  59. void Counter::close()
  60.    {
  61.    running = 0 ;
  62.    wakeup() ;
  63.    }
  64.  
  65. //
  66. // this class saves something using the DataSave protocol
  67. //
  68.  
  69. class Saver: public DataSave
  70.    {
  71.    public:
  72.       Saver(Task *task) ;
  73.       ~Saver() ;
  74.       void receive (int action, int task, int my_ref, int your_ref, int data_length, void *data) ;   // receive a message
  75.       void save(int window, int icon, int x, int y, char *leaf) ;
  76.       void save (char *path) ;
  77.  
  78.    } ;
  79.  
  80. Saver::Saver (Task *task) : DataSave (task)
  81.    {
  82.    }
  83.  
  84. Saver::~Saver()
  85.    {
  86.    }
  87.  
  88. void Saver::receive (int action, int task, int my_ref, int your_ref, int data_length, void *data)
  89.    {
  90.    switch ((Task::events)action)
  91.       {
  92.       case Task::Message_DataSaveAck:
  93.          {            // save to this file
  94.          DataSave::saveackdata *s = (DataSave::saveackdata *)data ;
  95.          save (s->pathname) ;
  96.          dataload (task, your_ref, my_ref, data_length, data) ;
  97.          break ;
  98.          }
  99.       case Task::Message_DataLoadAck:
  100. //         delete this ;
  101.          break ;
  102.       }
  103.    }
  104.  
  105. void Saver::save (int window, int icon, int x, int y, char *leaf)
  106.    {
  107.    datasave (window, icon, x, y, 1024, 0xfff, leaf) ;
  108.    }
  109.  
  110. void Saver::save (char *path)
  111.    {
  112.    FILE *fp = fopen (path, "w") ;
  113.    if (fp == NULL)
  114.       throw ("cant open file") ;
  115.    fprintf (fp,"hello world\n") ;
  116.    fclose (fp) ;
  117.    }
  118.  
  119. class Loader ;
  120.  
  121. //
  122. // an icon grid
  123. //
  124.  
  125. class LibraryDisplay : public IconGrid
  126.    {
  127.    public:
  128.       LibraryDisplay (Task *t, char *tname, int ticon, char *menu = 0) : IconGrid (t,tname,ticon,menu) { set_flag (SORTED) ; }
  129.       void menu (MenuItem items[]) ;
  130.       void drag_icon (int mx, int my, int buttons, Icon *icon) ;
  131.       void drag (int x0, int y0, int x1, int y1, int id) ;
  132.       void drag_selection (int mx, int my, int buttons, int x0, int y0, int x1, int y1) ;
  133.    } ;
  134.  
  135. void LibraryDisplay::menu (MenuItem items[])
  136.    {
  137.    }
  138.  
  139. void LibraryDisplay::drag_icon (int mx, int my, int buttons, Icon *icon)
  140.    {
  141.    icon->drag (mx, my, buttons) ;
  142.    task->register_drag (this,1) ;
  143.    }
  144.  
  145. void LibraryDisplay::drag_selection (int mx, int my, int buttons,int x0, int y0, int x1, int y1)
  146.    {
  147.    do_drag (5, x0, y0, x1, y1) ;
  148.    task->register_drag (this,1) ;
  149.    }
  150.  
  151. void LibraryDisplay::drag (int x0, int y0, int x1, int y1, int id)
  152.    {
  153.    if (id != 1)         // not my drag?
  154.        {
  155.        IconGrid::drag (x0,y0,x1,y1,id) ;
  156.        return ;
  157.        }
  158.    int block[5] ;
  159.    _kernel_swi_regs r ;
  160.    _kernel_oserror *e ;
  161.    r.r[1] = (int)block ;
  162.    if ((e = _kernel_swi (Wimp_GetPointerInfo, &r, &r)) != NULL)
  163.       throw (e) ;
  164.    Saver *saver = new Saver(task) ;
  165.    saver->save (block[3], block[4], block[0], block[1], "newfile") ;
  166.    }
  167.  
  168. //
  169. // this class loads files into the icon grid
  170. //
  171.  
  172. class Loader: public DataSave
  173.    {
  174.    public:
  175.       Loader(Task *task, IconGrid *grid) ;
  176.       ~Loader() ;
  177.       void receive (int action, int task, int my_ref, int your_ref, int data_length, void *data) ;   // receive a message
  178.    private:
  179.       IconGrid *grid ;
  180.    } ;
  181.  
  182. Loader::Loader (Task *task, IconGrid *grid) : DataSave (task)
  183.    {
  184.    this->grid = grid ;
  185.    }
  186.  
  187. Loader::~Loader()
  188.    {
  189.    }
  190.  
  191. void Loader::receive (int action, int task, int my_ref, int your_ref, int data_length, void *data)
  192.    {
  193.    switch ((Task::events)action)
  194.       {
  195.       case Task::Message_DataSave:             // save to this file
  196.          datasaveack (your_ref, (DataSave::saveackdata *)data, "<Wimp$Scrap>") ;
  197.          break ;
  198.       case Task::Message_DataLoad:
  199.          {
  200.          DataSave::saveackdata *s = (DataSave::saveackdata *)data ;
  201.          char *leaf = strrchr (s->pathname, '.') ;
  202.          if (leaf == NULL)
  203.             leaf = s->pathname ;
  204.          else
  205.             leaf++ ;
  206.          grid->insert_icon (leaf) ;
  207.          dataloadack (task, your_ref, my_ref, data_length, data) ;
  208.          break ;
  209.          }
  210.       }
  211.    }
  212.  
  213.  
  214.  
  215.  
  216.  
  217. //
  218. // declare a class which is the main task
  219. //
  220.  
  221. class Vista : public Task
  222.    {
  223.    public:
  224.       Vista() ;
  225.       void click(int x, int y, int button, int icon) ;   // I want iconbar clicks
  226.       char *get_iconbar_menu (int icon) ;
  227.       void menu (MenuItem items[]) ;                     // iconbar menu hit
  228.       char *help (int mx, int my, int buttons, int w, int i) ;
  229.  
  230.       Window *mainwin ;               // main window
  231.       Window *win2 ;                  // another window
  232.       LibraryDisplay *lib ;           // the icon grid
  233.       Window *txt ;                   // a text window
  234.       Loader *loader ;                // icon grid file loader
  235.    } ;
  236.  
  237. //
  238. // declare the main task variable
  239. //
  240.  
  241. Vista *vista ;
  242.  
  243. //
  244. // this class is for an action button
  245. //
  246.  
  247. class Compile : public Icon
  248.    {
  249.    public:
  250.       Compile(Window *w, int icon) ;
  251.       void click(int mx, int my, int button, int icon) ;
  252.       char *help (int mx, int my, int buttons) ;
  253.    } ;
  254.  
  255. Compile::Compile(Window *w, int icon) : Icon (w,icon)
  256.    {
  257.    }
  258.  
  259. void Compile::click(int mx, int my, int button, int icon)
  260.    {
  261. //   Counter *c = new Counter (window->task) ;
  262. //   c->start() ;
  263.    window->task->show_threads() ;
  264.    Window *w = new Window (window->task, "counter") ;
  265.    Icon *ic = new Icon (w, 0) ;
  266.    w->do_open() ;
  267.    for (int i = 0 ; i < 20 ; i++)
  268.       {
  269.       ic->print ("%d",i) ;
  270.       window->task->sleep (100) ;
  271.       }
  272.    w->do_close() ;
  273.    delete ic ;
  274.    delete w ;
  275.    }
  276.  
  277. char *Compile::help (int mx, int my, int buttons)
  278.    {
  279.    return "This is the counter icon|MClick select to start a new counter" ;
  280.    }
  281. //
  282. // this class is for an action button which adds a new icon to the
  283. // scrolling list
  284. //
  285.  
  286. class Add : public Icon
  287.    {
  288.    public:
  289.       Add(Window *w, int icon, IconGrid *scroll) ;
  290.       void click(int mx, int my, int button, int icon) ;
  291.    private:
  292.       IconGrid *scroll ;
  293.       int num ;
  294.    } ;
  295.  
  296. Add::Add(Window *w, int icon, IconGrid *scroll) : Icon (w,icon)
  297.    {
  298.    this->scroll = scroll ;
  299.    num = 0 ;
  300.    }
  301.  
  302. void Add::click(int mx, int my, int button, int icon)
  303.    {
  304.    char buf[256] ;
  305.    sprintf (buf,"Icon %d",num++) ;
  306.    scroll->insert_icon (buf) ;
  307.    }
  308.  
  309. //
  310. // this class adds a new icon to the icon grid
  311. //
  312.  
  313. class AddIcon : public Icon
  314.    {
  315.    public:
  316.       AddIcon(Window *w, int icon, IconGrid *grid) ;
  317.       void click(int mx, int my, int button, int icon) ;
  318.    private:
  319.       IconGrid *grid ;
  320.       int num ;
  321.    } ;
  322.  
  323. AddIcon::AddIcon(Window *w, int icon, IconGrid *grid) : Icon (w,icon)
  324.    {
  325.    this->grid = grid ;
  326.    num = 0 ;
  327.    }
  328.  
  329. void AddIcon::click(int mx, int my, int button, int icon)
  330.    {
  331.    char buf[256] ;
  332.    sprintf (buf,"Icon %d",num++) ;
  333.    grid->insert_icon (buf) ;
  334.    grid->set_title ("%d icons",num) ;
  335.    }
  336.  
  337. //
  338. // a small sub window (a scrolling list)
  339. //
  340.  
  341.  
  342. class SubWindow: public IconGrid
  343.    {
  344.    public:
  345.       SubWindow (Window *w, int ticon) : IconGrid (w, "sub1", ticon, "submenu") {}
  346.       void menu (MenuItem items[]) ;
  347.       char *help (int mx, int my, int buttons, int icon) ;
  348.    } ;
  349.  
  350.  
  351. void SubWindow::menu (MenuItem items[])
  352.    {
  353.    }
  354.  
  355. char *SubWindow::help (int mx, int my, int buttons, int icon)
  356.    {
  357.    return "This is a scrolling window" ;
  358.    }
  359.  
  360. //
  361. // a dialogue box
  362. //
  363.  
  364.  
  365. class TestBox : public DialogueBox
  366.    {
  367.    public:
  368.       TestBox (Task *task) ;
  369.       ~TestBox() ;
  370.    private:
  371.       char string[32] ;           // string attribute
  372.       int number ;                // integer attribute
  373.       bool option ;                // option attribute
  374.       bool choices[3] ;
  375.       Icon *text, *num ;
  376.    } ;
  377.  
  378.  
  379. TestBox::TestBox (Task *task)
  380.    : DialogueBox (task, "testbox", 10, 11)
  381.    {
  382.    strcpy (string, "a big hello") ;
  383.    number = 100 ;
  384.    option = true ;
  385.    choices[0] = false ;
  386.    choices[1] = true ;
  387.    choices[2] = false ;
  388.    int x = 1234 ;
  389.    text = new Icon (this, 1) ;
  390.    text->print ("hello world") ;
  391.    num = new Icon (this, 3) ;
  392.    num->print ("%d",x) ;
  393.    create_attribute (5, string) ;
  394.    create_attribute (7, number) ;
  395.    create_attribute (8, option) ;
  396.    create_attribute (3, choices, 9, 12, 13) ;
  397.    }
  398.  
  399. TestBox::~TestBox()
  400.    {
  401.    }
  402.  
  403. //
  404. // action button to open the dialogue box
  405. //
  406.  
  407.  
  408. class BoxIcon : public Icon
  409.    {
  410.    public:
  411.       BoxIcon(Window *w, int icon, TestBox *box) ;
  412.       void click(int mx, int my, int button, int icon) ;
  413.    private:
  414.       TestBox *box ;
  415.    } ;
  416.  
  417. BoxIcon::BoxIcon(Window *w, int icon, TestBox *box) : Icon (w,icon)
  418.    {
  419.    this->box = box ;
  420.    }
  421.  
  422. void BoxIcon::click(int mx, int my, int button, int icon)
  423.    {
  424.    box->show() ;
  425.    }
  426.  
  427.  
  428. //
  429. // the main window class
  430. //
  431.  
  432.  
  433. class MainWindow : public Window
  434.    {
  435.    public:
  436.       MainWindow(Vista *task) ;
  437.       void menu(MenuItem items[]) ;
  438.       void pre_menu (Menu *menu, int x, int y, int button, int icon) ;
  439.       char *help (int, int, int, int) ;
  440.  
  441.    private:
  442.       Icon *compile ;        // compile icon
  443.       Icon *link ;           // link icon
  444.       Icon *adjuster ;       // adjuster icon
  445.       Icon *slider ;         // slider icon
  446.       Icon *add ;            // add to scroll icon
  447.       Icon *add_icon ;            // add to grid
  448.       IconGrid *scroll ;       // sub window
  449.       TestBox *box ;
  450.       Icon *boxicon ;
  451.       Window *toolbarl ;        // toolbar left
  452.       Window *toolbarr ;        // toolbar right
  453.       Window *toolbart ;        // toolbar top
  454.       Window *toolbarb ;        // toolbar bottom
  455.       Icon *popup ;
  456.    } ;
  457.  
  458. char *MainWindow::help (int mx, int my, int buttons, int icon)
  459.    {
  460.    return "This is the main window" ;
  461.    }
  462.  
  463. MainWindow::MainWindow(Vista *task) : Window (task, "main", "mainmenu")
  464.    {
  465. #ifdef __EASY_C
  466.    try        // catch exceptions
  467. #endif
  468.       {
  469.       compile = new Compile (this, 0) ;
  470.       link = new Icon (this, 1) ;
  471.       adjuster = new Adjuster (this, 4,3,5) ;
  472.       slider = new Slider (this, 9, 10, 8, 7, 100) ;
  473.       scroll = new SubWindow (this, 0) ;
  474.       add = new Add (this, 14, scroll) ;
  475.       add_icon = new AddIcon (this, 15, task->lib) ;
  476.       box = new TestBox (task) ;
  477.       boxicon = new BoxIcon (this, 16, box) ;
  478.       toolbarl = new Window (this, "anchorl", Window::A_LEFT) ;
  479.       toolbarr = new Window (this, "anchorr", Window::A_RIGHT) ;
  480.       toolbart = new Window (this, "anchort", Window::A_TOP) ;
  481.       toolbarb = new Window (this, "anchorb", Window::A_BOTTOM) ;
  482.       popup = new Popup (this, 17, 18, "popup") ;
  483.       }
  484. #ifdef __EASY_C
  485.    catch (_kernel_oserror *e)
  486.       {
  487.       _kernel_swi_regs r ;
  488.       r.r[0] = (int)e ;
  489.       r.r[1] = 0 ;
  490.       r.r[2] = (int)task->program ;
  491.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  492.       task->exit (1) ;
  493.       }
  494.  
  495.    catch (char *s)
  496.       {
  497.       _kernel_oserror err ;
  498.       _kernel_swi_regs r ;
  499.       err.errnum = 0 ;
  500.       strcpy (err.errmess, s) ;
  501.       r.r[0] = (int)&err ;
  502.       r.r[1] = 0 ;
  503.       r.r[2] = (int)task->program ;
  504.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  505.       task->exit (1) ;
  506.       }
  507. #endif
  508.  
  509.    }
  510.  
  511. //
  512. //  menu hit on main window
  513. //
  514.  
  515.  
  516. void MainWindow::menu(MenuItem items[])
  517.    {
  518. #ifdef __EASY_C
  519.    try
  520. #endif
  521.       {
  522.       switch (items[0])
  523.          {
  524.          case 1:
  525.             {
  526.             task->debug->print ("creating save box") ;
  527.             Saver saver (task) ;
  528.             SaveBox save (task, "xfer_send", "",  "File", 0xfff, &saver) ;
  529.             save.set_title ("new saver") ;
  530.             save.show() ;
  531.             task->sleep (&save) ;           // sleep until save terminates
  532.         ::print ("woke up") ;
  533.             break ;
  534.             }
  535.          case 2:
  536.             if (items[0].is_ticked())
  537.                items[0].untick() ;
  538.             else
  539.                items[0].tick() ;
  540.             break ;
  541.  
  542.          case 5:
  543.             {
  544.             char str[256] ;
  545.             items[1].read (str) ;
  546.             char *s = items[1] ;
  547.             ::print ("item 4:  %s, %s",s,str) ;
  548.             break ;
  549.             }
  550.          }
  551.       }
  552. #ifdef __EASY_C
  553.    catch (char *s)
  554.       {
  555.       _kernel_oserror err ;
  556.       _kernel_swi_regs r ;
  557.       err.errnum = 0 ;
  558.       strcpy (err.errmess, s) ;
  559.       r.r[0] = (int)&err ;
  560.       r.r[1] = 0 ;
  561.       r.r[2] = (int)"Test" ;
  562.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  563.       }
  564. #endif
  565.    }
  566.  
  567. //
  568. //  a chance to adjust the menu
  569. //
  570.  
  571.  
  572. void MainWindow::pre_menu (Menu *menu, int x, int y, int button, int icon)
  573.    {
  574.    static int i = 0 ;
  575.    menu->print ("item1", "Item '%d'", i++) ;
  576.    }
  577.  
  578. //
  579. // construct the task
  580. //
  581.  
  582.  
  583. Vista::Vista () : Task ("Test task", "Test", "!Test", "iconbar")
  584. //Vista::Vista () : Task ("Test task", "Test")
  585.    {
  586.    char buffer [1024] ;
  587.    lib = new LibraryDisplay (this, "grid", 0, 0) ;
  588.    loader = new Loader (this, lib) ;         // a new loader
  589.    mainwin = new MainWindow (this) ;         // construct the main window
  590.    win2 = new MainWindow (this) ;
  591.  
  592. // make the text window
  593.  
  594.    txt = new Window (this,"txtwin") ;
  595. // a text object
  596. #if 1
  597.    TextObject *text = new TextObject (txt, "text1", "a piece of\ntext\nat the top of a\nwindow", 100,-100) ;
  598. // another one
  599. #endif
  600. #if 0
  601.    TextObject *text2 = new TextObject (txt, "text2", "another\npiece\nof\ntext", 2000,-2000) ;
  602. #endif
  603. // a line object
  604.    LineObject *line1 = new LineObject (txt, "line1", 100, -1000, 1000, -100, 0, 10) ;
  605. #if 1
  606.  
  607.    int font_handle = open_font ("Homerton.Medium.Oblique", 16, 16) ;
  608.    int bold_handle = open_font ("Homerton.Bold", 20, 20) ;
  609.    FontObject *font = new FontObject (txt, "font1", font_handle, 100, -1000, 900) ;
  610.    font->set_indent (300) ;
  611.    font->set_font (font_handle) ;
  612.    font->add_text ("The ") ;
  613.    font->set_colour (255,255,255,255,0,0) ;
  614.    font->begin_underline() ;
  615.    font->add_text ("quick") ;
  616.    font->end_underline() ;
  617.    font->set_font (bold_handle) ;
  618.    font->set_colour (255,255,255,84,43,14) ;
  619.    font->add_text (" Brown") ;
  620.    font->set_colour (255,255,255,255,0,0) ;
  621.    font->set_font (font_handle) ;
  622.    font->set_colour (255,255,255,255,128,0) ;
  623.    font->add_text (" fox jumps over the lazy dog.  ") ;
  624.    font->begin_underline() ;
  625.    font->add_text ("Now is the time for all good men to ") ;
  626.    font->set_colour (255,255,255,0,0,0) ;
  627.    font->set_font (bold_handle) ;
  628.    font->add_text ("come") ;
  629.    font->set_font (font_handle) ;
  630.    font->add_text (" to the aid of the party...") ;
  631.    font->add_text ("And again, the quick brown fox jumps over the lazy dog.") ;
  632.    font->finish() ;
  633. #endif
  634.    SpriteObject *sprite = new SpriteObject (txt, "sprite1", "plane", 1500, -1500, 1) ;
  635.    SpriteObject *sprite2 = new SpriteObject (txt, "sprite2", "!test", 600, -600, 1) ;
  636.    DrawObject *draw = new DrawObject (txt,"draw", "<Test$Dir>.Draw", 300, -800) ;
  637.    SpriteObject *sprite3 = new SpriteObject (txt, "sprite3", "!test1", 300, -800, 1) ;
  638.    set_mask (1 << ENULL) ;
  639.    }
  640.  
  641. //
  642. // iconbar click
  643. //
  644.  
  645. void Vista::click (int x, int y, int button, int icon)
  646.     {
  647.     lib->do_open() ;
  648.     txt->do_open() ;
  649.     if (button & 1)                       // right button?
  650.        mainwin->do_open() ;
  651.     else
  652.        win2->do_open() ;
  653.     }
  654.  
  655. char *Vista::get_iconbar_menu (int icon)
  656.    {
  657.    return "iconbar" ;
  658.    }
  659.  
  660. //
  661. // menu hit on the icon bar icon
  662. //
  663.  
  664. void Vista::menu(MenuItem items[])
  665.    {
  666.    switch (items[0])
  667.       {
  668.       case 0:
  669.          {
  670.          DialogueBox proginfo (this, "ProgInfo") ;
  671.          proginfo.show() ;
  672.          sleep (&proginfo) ;
  673.          break ;
  674.          }
  675.       case 1:
  676.          exit() ;
  677.          break ;
  678.       default:
  679.          {
  680.          char str[64] ;
  681.          sprintf (str,"unknown item %d\n",(int)items[0]) ;
  682.       throw (str) ;
  683.          }
  684.       }
  685.    }
  686.  
  687. char *Vista::help (int mx, int my, int buttons, int w, int i)
  688.    {
  689. //   debug->print ("Help") ;
  690.    return "This is Vista" ;
  691.    }
  692.  
  693. //
  694. // main program: simply construct the task and run it
  695. //
  696.  
  697. main()
  698.    {
  699. #ifdef __EASY_C
  700.    try
  701. #endif
  702.       {
  703.       vista = new Vista() ;
  704.       vista->run() ;
  705.       }
  706. #ifdef __EASY_C
  707.    catch (_kernel_oserror *e)
  708.       {
  709.       _kernel_swi_regs r ;
  710.       r.r[0] = (int)e ;
  711.       r.r[1] = 0 ;
  712.       r.r[2] = (int)"Test" ;
  713.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  714.       }
  715.  
  716.    catch (char *s)
  717.       {
  718.       _kernel_oserror err ;
  719.       _kernel_swi_regs r ;
  720.       err.errnum = 0 ;
  721.       strcpy (err.errmess, s) ;
  722.       r.r[0] = (int)&err ;
  723.       r.r[1] = 0 ;
  724.       r.r[2] = (int)"Test" ;
  725.       _kernel_swi (Wimp_ReportError, &r, &r) ;
  726.       }
  727. #endif
  728.    }
  729.