home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 2 / AUCD2.iso / program / vista.arc / c / tools < prev    next >
Text File  |  1996-02-01  |  7KB  |  275 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. // c.tools
  30. //
  31.  
  32.  
  33. #include "Vista:tools.h"
  34. #include "Vista:window.h"
  35. #include <kernel.h>
  36. #include <stdlib.h>
  37. #include <swis.h>
  38. #include <string.h>
  39. #include <stdio.h>
  40. #include <stdarg.h>
  41. #include "Vista:task.h"
  42.  
  43.  
  44. //
  45. // adjuster arrow tool
  46. //
  47.  
  48.  
  49. Adjuster::Adjuster (Window *w, int upicon, int downicon, int valueicon, int write_init, int defvalue)
  50.    :IconSet (4), Icon (w, valueicon)
  51.    {
  52.    add_icon (upicon) ;
  53.    add_icon (downicon) ;
  54.    add_icon (valueicon) ;
  55.    value = defvalue ;
  56.    if (write_init)
  57.       write (defvalue) ;
  58.    }
  59.  
  60. Adjuster::~Adjuster()
  61.    {
  62.    }
  63.  
  64. void Adjuster::write (int v)
  65.    {
  66.    value = v ;
  67.    print ("%d",v) ;
  68.    }
  69.  
  70. int Adjuster::compare (int icon)
  71.    {
  72.    return IconSet::compare(icon) ;
  73.    }
  74.  
  75.  
  76. void Adjuster::click (int mx, int mt, int button, int icon)
  77.    {
  78.    int up = 0, down = 0 ;
  79.    if (button & 4)   // select?
  80.       {
  81.       up = 1 ;
  82.       down = -1 ;
  83.       }
  84.    else if (button & 1)  // adjust?
  85.       {
  86.       up = -1 ;
  87.       down = 1 ;
  88.       }
  89.    if (icon == icons[UP])
  90.       write (value+up) ;
  91.    else if (icon == icons[DOWN])
  92.       write (value+down) ;
  93.    }
  94.  
  95.  
  96. Meter::Meter (Window *w, int backgroundicon, int valueicon, int maxvalue)
  97.    : Icon (w,valueicon)
  98.    {
  99.    _kernel_swi_regs r ;
  100.    _kernel_oserror *e ;
  101.    int block[10] ;
  102.    block[0] = w->handle ;
  103.    block[1] = backgroundicon ;
  104.    r.r[1] = (int)block ;
  105.    if ((e = _kernel_swi (Wimp_GetIconState, &r, &r)) != NULL)
  106.       throw (e) ;
  107.    range = block[4] - block[2] ;       // x1 - x0
  108.    background_icon = backgroundicon ;
  109.    max_value = maxvalue ;
  110.    reset() ;
  111.    }
  112.  
  113. //
  114. // set the meter according to a value
  115. //
  116.  
  117. void Meter::write (int v)
  118.    {
  119.    int minx, maxx ;                    // min and max x coords for redraw
  120.    if (v > max_value)
  121.       v = max_value ;
  122.    if (v <= 0)
  123.       percent = 0 ;
  124.    else
  125.       percent = (v * 100) / max_value  ;
  126.    int val = (range * percent) / 100 ;
  127. //   dprintf ("max = %d, v = %d, percent = %d%%, range = %d, val = %d",max_value,v,percent,range,val) ;
  128.    _kernel_swi_regs r ;
  129.    _kernel_oserror *e ;
  130.    int block[10] ;
  131.    block[0] = window->handle ;
  132.    block[1] = handle ;              // value icon
  133.    r.r[1] = (int)block ;
  134.    if ((e = _kernel_swi (Wimp_GetIconState, &r, &r)) != NULL)
  135.       throw (e) ;
  136. // calculate the change in coords
  137.    if ((block[2] + val) > xsize)         // increasing in size?
  138.       {
  139.       minx = xsize ;
  140.       maxx = block[2] + val ;
  141.       }
  142.    else
  143.       {
  144.       minx = block[2] + val ;
  145.       maxx = xsize ;
  146.       }
  147.    block[4] = block[2] + val ;
  148.    xsize = block[4] ;
  149.    int delblock[2] ;
  150.    delblock[0] = window->handle ;
  151.    delblock[1] = handle ;
  152.    r.r[1] = (int)delblock ;
  153.    if ((e = _kernel_swi (Wimp_DeleteIcon, &r, &r)) != NULL)
  154.       throw (e) ;
  155.    r.r[1] = (int)&block[1] ;
  156.    r.r[0] = handle ;
  157.    block[1] = window->handle ;   // set window handle
  158.    if ((e = _kernel_swi (Wimp_CreateIcon, &r, &r)) != NULL)
  159.       throw (e) ;
  160.  
  161. //
  162. // now redraw just the bit that's changed
  163. //
  164.  
  165.    r.r[0] = window->handle ;
  166.    r.r[1] = minx ;
  167.    r.r[2] = block[3] ;
  168.    r.r[3] = maxx ;
  169.    r.r[4] = block[5] ;
  170.    if ((e = _kernel_swi (Wimp_ForceRedraw, &r, &r)) != NULL)
  171.       throw (e) ;
  172.    }
  173.  
  174. void Meter::reset()
  175.    {
  176.    write(0) ;
  177.    }
  178.  
  179. Meter::~Meter()
  180.    {
  181.    }
  182.  
  183. Slider::Slider (Window *w, int upicon, int downicon, int valueicon,
  184.              int backgroundicon, int maxvalue, int defvalue)
  185.    : Icon (w, valueicon),
  186.      Adjuster (w, upicon, downicon, valueicon, 0, defvalue),
  187.      Meter (w, backgroundicon, valueicon, maxvalue)
  188.    {
  189.    add_icon (backgroundicon) ;
  190.    }
  191.  
  192.  
  193. void Slider::write (int v)
  194.    {
  195.    if (v < 0)
  196.       v = 0 ;
  197.    if (v > max_value)
  198.       v = max_value ;
  199.    value = v ;
  200.    Meter::write (v) ;
  201.    }
  202.  
  203. void Slider::click (int mx, int my, int button, int icon)
  204.    {
  205.    if (icon != background_icon && icon != handle)
  206.       Adjuster::click (mx, my, button, icon) ;
  207.    else
  208.       {
  209.       _kernel_swi_regs r ;
  210.       _kernel_oserror *e ;
  211.       int block[10] ;
  212.       int x, p, v ;
  213.       for (;;)
  214.          {
  215.          block[0] = window->handle ;
  216.          block[1] = handle ;              // value icon
  217.          r.r[1] = (int)block ;
  218.          if ((e = _kernel_swi (Wimp_GetIconState, &r, &r)) != NULL)
  219.             throw (e) ;
  220.          x = mx + (window->scx - window->x0) ;
  221.          p = ((x - block[2]) * 100) / range ;       // calc percentage
  222.          v = (p * 100) / max_value ;
  223.          write (v) ;
  224.          r.r[1] = (int)block ;
  225.          if ((e = _kernel_swi (Wimp_GetPointerInfo, &r, &r)) != NULL)
  226.             throw (e) ;
  227.          if (block[2] == 0)
  228.             break ;
  229.          mx = block[0] ;
  230.          }
  231.       }
  232.    }
  233.  
  234.  
  235. Slider::~Slider()
  236.   {
  237.   }
  238.  
  239. void Slider::read (int &v)
  240.    {
  241.    v = percent ;
  242.    }
  243.  
  244.  
  245. Popup::Popup (Window *w, int displayicon, int menuicon, char *menu)
  246.    :IconSet (2), Icon (w, displayicon,0,menu)
  247.    {
  248.    add_icon (displayicon) ;
  249.    add_icon (menuicon) ;
  250.    }
  251.  
  252. Popup::~Popup()
  253.    {
  254.    }
  255.  
  256. int Popup::compare (int icon)
  257.    {
  258.    return IconSet::compare(icon) ;
  259.    }
  260.  
  261. void Popup::menu (MenuItem items[])
  262.    {
  263.    char buf[256] ;
  264.    items[0].read (buf) ;
  265.    print (buf) ;
  266.    }
  267.  
  268. void Popup::click (int mx, int my, int button, int icon)
  269.    {
  270.    Menu *m = display_menu (mx, my, button, icon) ;
  271.    if (m != NULL)
  272.       window->task->set_menu (m, this) ;
  273.    }
  274.  
  275.