home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sompar.zip / LISTBOX.CPP < prev    next >
C/C++ Source or Header  |  1994-01-10  |  9KB  |  259 lines

  1. //
  2. // Project: Digitalk Parts like Toolbar and SOMObjects Demo
  3. // File:    Listbox.hpp
  4. // Author:  Stewart Hyde
  5. // Created: Dec   18, 1993
  6. // Updated: Jan    7, 1993
  7. //
  8. // Description:
  9. //
  10.  
  11. #include <stdio.h>
  12. #include <ireslib.hpp>
  13. #include <iframe.hpp>
  14. #include <istring.hpp>
  15. #include <icolor.hpp>
  16. #include "listbox.hpp"
  17. #include "somparts.h"
  18.  
  19. // ------------------------------------------------------------------------
  20. //  Class:                 PartsListbox
  21. //  Function:            PartsListBox (constructor)
  22. //                
  23. //  Description:        This is the constructor for Parts Actions list box
  24. //       
  25. //  Input:                IWindow *parentWin     - parent window
  26. //                            IWindow *ownerWin      - owner window
  27. //                            SomAction *somAction    - pointer to SomActions class
  28. //
  29. //  Output:                N/A
  30. //
  31. //  Notes:
  32. //
  33. // ------------------------------------------------------------------------
  34.  
  35. PartsListbox::PartsListbox(IWindow *parentWin,
  36.                                     IWindow *ownerWin,
  37.                                     SomAction *somAction)
  38.   : ICanvas(WND_PARTS_LISTBOX_WINDOW, 
  39.             parentWin,
  40.             ownerWin,
  41.             IRectangle(),
  42.             IWindow::noStyle),
  43.   lst101(101, this, this, 
  44.                    mapDialogRect(IRectangle(0, 0, 147, 108)),
  45.                    IWindow::visible  | IControl::tabStop),
  46.   edt102(102, this, this, 
  47.                    mapDialogRect(IRectangle(8, 119, 42, 127)),
  48.                    IEntryField::defaultStyle() | IEntryField::readOnly |
  49.                          IControl::tabStop),
  50.   grp103(103, this, this, 
  51.                    mapDialogRect(IRectangle(0, 111, 50, 139)),
  52.                    IGroupBox::defaultStyle()),
  53.   edt104(104, this, this, 
  54.                    mapDialogRect(IRectangle(59, 119, 138, 127)),
  55.                    IEntryField::defaultStyle() | IEntryField::readOnly |
  56.                         IControl::tabStop),
  57.   grp105(105, this, this, 
  58.                    mapDialogRect(IRectangle(53, 111, 148, 139)),
  59.                    IGroupBox::defaultStyle())
  60. {
  61.  
  62.     // ---------------------------------------------------------------------
  63.       // Initialize variables 
  64.     // ---------------------------------------------------------------------
  65.     
  66.     LastStatus = -1;                      // LastStatus is not set 
  67.     ActionCount = 0;                    // No Actions yet
  68.  
  69.     // ---------------------------------------------------------------------
  70.       // Save pointer to SomActions class
  71.     // ---------------------------------------------------------------------
  72.  
  73.     somAct= somAction;
  74.  
  75.     // ---------------------------------------------------------------------
  76.     // translate the dialog rectange to size approviate for system font
  77.     // ---------------------------------------------------------------------
  78.  
  79.       IRectangle sizeRect(148, 147);
  80.       sizeTo(mapDialogRect(sizeRect).size());
  81.  
  82.     // ---------------------------------------------------------------------
  83.       // setup all controls in dialog
  84.     // ---------------------------------------------------------------------
  85.  
  86.       setupControls();
  87.  
  88.     // ---------------------------------------------------------------------
  89.       // start handling timer events
  90.     // ---------------------------------------------------------------------
  91.  
  92.       ATimeHandler::handleEventsFor(this);  //Set self as time handler            v6
  93. }                             
  94.  
  95. // ------------------------------------------------------------------------
  96. //  Class:                PartsListBox
  97. //  Function:            mapDialogRect
  98. //                
  99. //  Description:        translates dialog units to pels
  100. //       
  101. //  Input:                IRectangle &oldRect
  102. //
  103. //  Output:                IRectangle
  104. //
  105. //  Notes:
  106. //
  107. // ------------------------------------------------------------------------
  108.  
  109. IRectangle PartsListbox::mapDialogRect(const IRectangle &oldRect)
  110. {
  111.       IFont font("System Proportional");
  112.  
  113.     // ---------------------------------------------------------------------
  114.     // create a new rect that for each X dialog unit is equal to average 
  115.     // character width / 4 and for each Y dialog unit is equal to maximum
  116.     // character height / 8
  117.     // ---------------------------------------------------------------------
  118.  
  119.       IRectangle newRect(oldRect.left() * font.avgCharWidth() / 4,
  120.                         oldRect.bottom() * font.maxCharHeight() / 8,
  121.                          oldRect.right() * font.avgCharWidth() / 4,
  122.                          oldRect.top() * font.maxCharHeight() / 8);
  123.  
  124.     // ---------------------------------------------------------------------
  125.     // return the new translated rectange to caller
  126.     // ---------------------------------------------------------------------
  127.  
  128.       return(newRect);
  129. }
  130.  
  131.  
  132. // ------------------------------------------------------------------------
  133. //  Class:                PartsListbox
  134. //  Function:            setupControls
  135. //                
  136. //  Description:        initializes each control and sets approvate values
  137. //       
  138. //  Input:                N/A
  139. //
  140. //  Output:                N/A
  141. //
  142. //  Notes:
  143. //
  144. // ------------------------------------------------------------------------
  145.  
  146. void PartsListbox::setupControls()
  147. {
  148.       IFont font("System Proportional");
  149.  
  150.     //----------------------------------------------------------------------
  151.       // change the position of this dialog
  152.     //----------------------------------------------------------------------
  153.  
  154.       IPoint origin(175 * font.avgCharWidth() / 4,
  155.                 56 * font.maxCharHeight() / 8);
  156.       IPoint extent(329 * font.avgCharWidth() / 4,
  157.                 205 * font.maxCharHeight() / 8);
  158.       moveSizeTo(IRectangle(origin, extent));
  159.       grp103.setText(STR_COUNT);
  160.       grp105.setText(STR_ACTIONS);
  161. }
  162.  
  163.  
  164. // ------------------------------------------------------------------------
  165. //  Class:                PartsListbox
  166. //  Function:            tick
  167. //                
  168. //  Description:        timer handler for listbox
  169. //       
  170. //  Input:                IEvent& evt - event 
  171. //
  172. //  Output:                
  173. //
  174. //  Notes:
  175. //
  176. // ------------------------------------------------------------------------
  177.  
  178.  
  179. Boolean PartsListbox::tick(IEvent& evt) 
  180. {        
  181.     long NewStatus = STR_LIST_WAITING;
  182.     Boolean FirstTime = false;
  183.     char szLine[60];
  184.     
  185.     // ---------------------------------------------------------------------
  186.     // On each Tick check our update list to see if any items are on the
  187.     // Update list, if so check to see if if update or new item    
  188.     // ---------------------------------------------------------------------
  189.         
  190.     if (somAct->Next())
  191.     {
  192.         if (somAct->getUpdate())
  193.             NewStatus = STR_LIST_UPDATE;
  194.         else
  195.             NewStatus = STR_LIST_ADD;
  196.     }                      
  197.  
  198.     // ---------------------------------------------------------------------
  199.     // If not the same as last status, upstate status listbox
  200.     // ---------------------------------------------------------------------
  201.  
  202.     if (NewStatus != LastStatus)
  203.     {
  204.         if (LastStatus == -1)
  205.             FirstTime = true;
  206.  
  207.         LastStatus = NewStatus;
  208.         edt104.setText(NewStatus);
  209.     }
  210.  
  211.     // ---------------------------------------------------------------------
  212.     // If nothing in list and still waiting, then return 
  213.     // ---------------------------------------------------------------------
  214.  
  215.     if ((NewStatus == STR_LIST_WAITING) && (!FirstTime))
  216.         return true;
  217.  
  218.     // ---------------------------------------------------------------------
  219.     // If this is a new item, then update count item
  220.     // ---------------------------------------------------------------------
  221.  
  222.     if ((NewStatus == STR_LIST_ADD) || (FirstTime))
  223.     {    
  224.         char szBuffer[8];
  225.  
  226.         // ------------------------------------------------------------------
  227.         //  if adding new item, then update count
  228.         // ------------------------------------------------------------------
  229.  
  230.         if (NewStatus == STR_LIST_ADD)
  231.             ActionCount++;
  232.  
  233.         // ------------------------------------------------------------------
  234.         //  Update edit control    
  235.         // -----------------------------------------------------------------
  236.  
  237.         sprintf(szBuffer,"%d",ActionCount);
  238.         edt102.setText(szBuffer);
  239.  
  240.         // ------------------------------------------------------------------
  241.         //  if still waiting then return, we are done
  242.         // ------------------------------------------------------------------
  243.  
  244.         if (NewStatus == STR_LIST_WAITING)                
  245.             return true;
  246.     }    
  247.  
  248.  
  249.     // ---------------------------------------------------------------------
  250.     //  Update the list box, by adding item as first item
  251.     // ---------------------------------------------------------------------
  252.  
  253.     sprintf(szLine,"Set #%d, Fun #%d, Count %d",somAct->getSet()+1,
  254.                         somAct->getAction()+1,somAct->getCount());
  255.     lst101.addAsFirst(szLine);
  256.  
  257.       return true;                       
  258. } /* end PartsListBox :: Tick() */                           
  259.