home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / kwclass.zip / KVALSET.CPP < prev    next >
Text File  |  1994-10-14  |  9KB  |  305 lines

  1. #define INCL_WIN
  2. #include <os2.h>
  3. #include <pmstddlg.h>
  4. #include <irect.hpp>
  5. #include <iexcept.hpp>
  6. #include <ireslib.hpp>
  7. #include <ipoint.hpp>
  8. #include <istring.hpp>
  9. #include <iexcept.hpp>
  10. #include <ievtdata.hpp>
  11. #include <iapp.hpp>
  12. #include "kvalset.hpp"
  13.  
  14. // style flags
  15. const KValueSet::Style KValueSet::bitmapItems(VS_BITMAP);
  16. const KValueSet::Style KValueSet::iconItems(VS_ICON);
  17. const KValueSet::Style KValueSet::textItems(VS_TEXT);
  18. const KValueSet::Style KValueSet::rgbItems(VS_RGB);
  19. const KValueSet::Style KValueSet::colorIndexItems(VS_COLORINDEX);
  20. const KValueSet::Style KValueSet::controlBorder(VS_BORDER);
  21. const KValueSet::Style KValueSet::itemBorder(VS_ITEMBORDER);
  22. const KValueSet::Style KValueSet::rightToLeft(VS_RIGHTTOLEFT);
  23. const KValueSet::Style KValueSet::scaleGraphics(VS_SCALEBITMAPS);
  24. const KValueSet::Style KValueSet::ownerDraw(VS_OWNERDRAW);
  25. const KValueSet::Style KValueSet::classDefaultStyle(KValueSet::textItems | IWindow::visible);
  26.  
  27. KValueSet::Style KValueSet::defaultStyle ( )
  28. {
  29.   return classDefaultStyle;
  30. }
  31.  
  32. void  KValueSet::setDefaultStyle( const Style& style )
  33. {
  34.    // not implemented yet
  35. }
  36.  
  37. KValueSet::KValueSet ( unsigned long        windowId,
  38.                        IWindow*             pwndParent,
  39.                        IWindow*             pwndOwner,
  40.                        const IRectangle&    initial,
  41.                        const IPair&         dimension,
  42.                        const    Style&      style)
  43.   : usRowCount(dimension.coord1())
  44.   , usColumnCount(dimension.coord2())
  45. {
  46.  
  47.    // create a value set control data structure to pass dimensions
  48.    VSCDATA vscData;
  49.    vscData.cbSize = sizeof(vscData);
  50.    vscData.usRowCount = dimension.coord1();
  51.    vscData.usColumnCount = dimension.coord2();
  52.  
  53.    // create the control using a call to IWindow::create
  54.    IWindowHandle whValueSet =
  55.      create(windowId, 
  56.             0,
  57.             style.asUnsignedLong(),
  58.             WC_VALUESET, 
  59.             pwndParent->handle(),
  60.             (pwndOwner==0) ? IWindowHandle(0) : pwndOwner->handle(),
  61.             initial, 
  62.             &vscData, 
  63.             0);
  64.    if (whValueSet == 0)
  65.    {
  66.       // indicates an error so throw an exception
  67.       ITHROWGUIERROR("IWindow::create");
  68.    }
  69.    
  70.    // tell IWindow to start handling events for the window
  71.    startHandlingEventsFor(whValueSet);
  72. }
  73.  
  74. KValueSet::KValueSet ( unsigned long        windowId,
  75.                        IWindow*             pwndParent )
  76. {
  77.    setAutoDestroyWindow(false);
  78.    startHandlingEventsFor(windowId, pwndParent);
  79. }
  80.  
  81. KValueSet::KValueSet ( const IWindowHandle& whValueSet)
  82. {
  83.    setAutoDestroyWindow(false);
  84.    startHandlingEventsFor(whValueSet);
  85. }
  86.  
  87. KValueSet::~KValueSet ( )
  88. {
  89. }
  90.  
  91. KValueSet& KValueSet::setItem ( const IPair &location, 
  92.                                 const IResourceId& id )
  93. {
  94.  
  95.   IASSERTPARM(location > IPair(0,0) && location <= IPair(usRowCount,usColumnCount));
  96.  
  97.   try
  98.   {
  99.     IBitmapHandle hdl = id.resourceLibrary().loadBitmap(id);
  100.     setItem(location,hdl);
  101.   } 
  102.   catch(...)
  103.   {
  104.     try
  105.     {
  106.       IPointerHandle hdl = id.resourceLibrary().loadIcon(id);
  107.       setItem(location,hdl);
  108.     }
  109.     catch(...)
  110.     {
  111.       IString str = id.resourceLibrary().loadString(id);
  112.       setItem(location,str);
  113.     }
  114.   }
  115.   return *this;
  116. }
  117.  
  118. KValueSet& KValueSet::setItem ( const IPair &item, 
  119.                                 const IBitmapHandle&  hBitmap )
  120. {
  121.   IASSERTPARM(item > IPair(0,0) & item <= IPair(usRowCount, usColumnCount));
  122.  
  123.   setItemType(item, bitmap);
  124.  
  125.   IEventResult result =
  126.            handle().sendEvent(VM_SETITEM,
  127.                      IEventData(item.coord1(), item.coord2()),
  128.                      IEventData(hBitmap));
  129.   if (!result.asUnsignedLong())
  130.     ITHROWGUIERROR("VM_SETITEM");
  131.  
  132.   return *this;
  133. }
  134.  
  135. KValueSet& KValueSet::setItem ( const IPair &item, 
  136.                                 const IPointerHandle& hIcon )
  137. {
  138.   IASSERTPARM(item > IPair(0,0) & item <= IPair(usRowCount, usColumnCount));
  139.  
  140.   setItemType(item, icon);
  141.  
  142.   IEventResult result =
  143.            handle().sendEvent(VM_SETITEM,
  144.                               IEventData(item.coord1(), item.coord2()),
  145.                               IEventData(hIcon));
  146.   if (!result.asUnsignedLong())
  147.     ITHROWGUIERROR("VM_SETITEM");
  148.   return *this;
  149. }
  150.  
  151. KValueSet& KValueSet::setItem ( const IPair &item, 
  152.                                 const IColor&         color )
  153. {
  154.   IASSERTPARM(item > IPair(0,0) & item <= IPair(usRowCount, usColumnCount));
  155.  
  156.   setItemType(item, rgb);
  157.  
  158.   IEventResult result =
  159.            handle().sendEvent(VM_SETITEM,
  160.                               IEventData(item.coord1(), item.coord2()),
  161.                               IEventData((unsigned long)color.asRGBLong()));
  162.   if (!result.asUnsignedLong())
  163.     ITHROWGUIERROR("VM_SETITEM");
  164.   return *this;
  165. }
  166.  
  167. KValueSet& KValueSet::setItem ( const IPair &item, 
  168.                                 IColor::Color index )
  169. {
  170.   IASSERTPARM(item > IPair(0,0) & item <= IPair(usRowCount, usColumnCount));
  171.  
  172.   setItemType(item, colorIndex);
  173.  
  174.   IEventResult result =
  175.            handle().sendEvent(VM_SETITEM,
  176.                               IEventData(item.coord1(), item.coord2()),
  177.                               IEventData(IColor(index).value()));
  178.   if (!result.asUnsignedLong())
  179.     ITHROWGUIERROR("VM_SETITEM");
  180.   return *this;
  181. }
  182.  
  183. KValueSet& KValueSet::setItem ( const IPair &item, 
  184.                                 const char*           str )
  185. {
  186.   IASSERTPARM(item > IPair(0,0) & item <= IPair(usRowCount, usColumnCount));
  187.  
  188.   setItemType(item, text);
  189.  
  190.   IEventResult result =
  191.            handle().sendEvent(VM_SETITEM,
  192.                               IEventData(item.coord1(), item.coord2()),
  193.                               IEventData((char *)str));
  194.   if (!result.asUnsignedLong())
  195.     ITHROWGUIERROR("VM_SETITEM");
  196.  
  197.   return *this;
  198. }
  199.  
  200. KValueSet& KValueSet::setItemBmp ( const IPair &item, const unsigned long bmpId )
  201. {
  202.   IASSERTPARM(item > IPair(0,0) & item <= IPair(usRowCount, usColumnCount));
  203.  
  204.   HPS hpsPresentationSpace = WinGetPS (handle());
  205.  
  206.   HBITMAP hBmp = GpiLoadBitmap (hpsPresentationSpace, NULLHANDLE, bmpId, 0L, 0L);
  207.  
  208.   setItemType(item, bitmap);
  209.  
  210.   if (WinSendMsg (handle(), VM_SETITEM, MPFROM2SHORT(item.coord1(), item.coord2()), MPFROMLONG(hBmp)) == FALSE)
  211.     ITHROWGUIERROR("VM_SETITEM");
  212.  
  213.   return *this;
  214. }
  215.  
  216. KValueSet& KValueSet::setItemIco ( const IPair &item, const unsigned long icoId )
  217. {
  218.   IASSERTPARM(item > IPair(0,0) & item <= IPair(usRowCount, usColumnCount));
  219.  
  220.   HPOINTER hIcon = WinLoadPointer (HWND_DESKTOP, NULLHANDLE, icoId);
  221.  
  222.   setItemType(item, icon);
  223.  
  224.   if (WinSendMsg (handle(), VM_SETITEM, MPFROM2SHORT(item.coord1(), item.coord2()), MPFROMLONG(hIcon)) == FALSE)
  225.     ITHROWGUIERROR("VM_SETITEM");
  226.  
  227.   return *this;
  228. }
  229.  
  230. KValueSet& KValueSet::setItemType ( const IPair &item, 
  231.                                     ItemType itemType )
  232. {
  233.   unsigned short usType;
  234.  
  235.   switch(itemType)
  236.   {
  237.     case text:
  238.        usType = VIA_TEXT;
  239.        break;
  240.  
  241.     case icon:
  242.        usType = VIA_ICON;
  243.        break;
  244.  
  245.     case bitmap:
  246.        usType = VIA_BITMAP;
  247.        break;
  248.  
  249.     case rgb:
  250.        usType = VIA_RGB;
  251.        break;
  252.  
  253.     case colorIndex:
  254.        usType = VIA_COLORINDEX;
  255.        break;
  256.   }
  257.  
  258.   IEventResult result = 
  259.            handle().sendEvent(VM_SETITEMATTR,
  260.                               IEventData(item.coord1(), item.coord2()),
  261.                               IEventData(usType, true));
  262.   if (!result.asUnsignedLong())
  263.     ITHROWGUIERROR("VM_SETITEMATTR");
  264.   return *this;
  265. }
  266.  
  267. KValueSet::Index KValueSet::itemToIndex(const IPair &item)
  268. {
  269.   IEventData data(item.coord1(), item.coord2());
  270.   return data;
  271. }
  272.  
  273. IPair KValueSet::indexToItem(KValueSet::Index index)
  274. {
  275.   IEventData data(index);
  276.   return(IPair(data.highNumber(), data.lowNumber()));
  277. }
  278.  
  279. IPair KValueSet::selectedItem()
  280. {
  281.   IEventResult result = handle().sendEvent(VM_QUERYSELECTEDITEM);
  282.   return(IPair(result.highNumber(), result.lowNumber()));
  283. }
  284.  
  285. KValueSet::Index KValueSet::selectedIndex()
  286. {
  287.   return itemToIndex(selectedItem());
  288. }
  289.  
  290. KValueSet& KValueSet::selectItem(const IPair &item)
  291. {
  292.   IEventResult result = handle().sendEvent(VM_SELECTITEM, 
  293.                             IEventParameter1(item.coord1(), item.coord2()));
  294.   if (result == 0)
  295.       ITHROWGUIERROR("KValueSet::selectItem");
  296.  
  297.   return *this;
  298. }
  299.  
  300. KValueSet& KValueSet::selectIndex(KValueSet::Index index)
  301. {
  302.   selectItem(indexToItem(index));
  303.   return *this;
  304. }
  305.