home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2240.zip / wxWindows-2.4.0 / src / os2 / scrolbar.cpp < prev    next >
C/C++ Source or Header  |  2002-12-27  |  10KB  |  311 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name:        scrolbar.cpp
  3. // Purpose:     wxScrollBar
  4. // Author:      David Webster
  5. // Modified by:
  6. // Created:     10/15/99
  7. // RCS-ID:      $Id: SCROLBAR.CPP,v 1.7.6.2 2002/12/27 14:49:35 JS Exp $
  8. // Copyright:   (c) David Webster
  9. // Licence:     wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11.  
  12. // For compilers that support precompilation, includes "wx.h".
  13. #include "wx/wxprec.h"
  14.  
  15. #ifndef WX_PRECOMP
  16. #include "wx/defs.h"
  17. #include "wx/utils.h"
  18. #endif
  19.  
  20. #include "wx/scrolbar.h"
  21. #include "wx/os2/private.h"
  22.  
  23. IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
  24.  
  25. // Scrollbar
  26. bool wxScrollBar::Create (
  27.   wxWindow*                         pParent
  28. , wxWindowID                        vId
  29. , const wxPoint&                    rPos
  30. , const wxSize&                     rSize
  31. , long                              lStyle
  32. , const wxValidator&                rValidator
  33. , const wxString&                   rsName
  34. )
  35. {
  36.     int                             nX = rPos.x;
  37.     int                             nY = rPos.y;
  38.     int                             nWidth = rSize.x;
  39.     int                             nHeight = rSize.y;
  40.  
  41.     if (!pParent)
  42.         return FALSE;
  43.     pParent->AddChild(this);
  44.     SetName(rsName);
  45. #if wxUSE_VALIDATORS
  46.     SetValidator(rValidator);
  47. #endif
  48.     SetBackgroundColour(pParent->GetBackgroundColour()) ;
  49.     SetForegroundColour(pParent->GetForegroundColour()) ;
  50.  
  51.     if (vId == -1L)
  52.         m_windowId = (int)NewControlId();
  53.     else
  54.         m_windowId = vId;
  55.  
  56.     if (nWidth == -1)
  57.     {
  58.         if (lStyle & wxHORIZONTAL)
  59.             nWidth = 140;
  60.         else
  61.             nWidth = 14;
  62.     }
  63.     if (nHeight == -1)
  64.     {
  65.         if (lStyle & wxVERTICAL)
  66.             nHeight = 140;
  67.         else
  68.             nHeight = 14;
  69.     }
  70.  
  71.     DWORD                           dwStyle = WS_VISIBLE;
  72.  
  73.     if (GetWindowStyleFlag() & wxCLIP_SIBLINGS)
  74.         dwStyle |= WS_CLIPSIBLINGS;
  75.  
  76.     DWORD                           dwDirection = (lStyle & wxHORIZONTAL) ? SBS_HORZ: SBS_VERT;
  77.  
  78.     HWND                            hScrollBar = ::WinCreateWindow( (HWND)GetHwndOf(pParent)
  79.                                                                    ,WC_SCROLLBAR
  80.                                                                    ,(PSZ)NULL
  81.                                                                    ,dwDirection | dwStyle
  82.                                                                    ,0, 0, 0, 0
  83.                                                                    ,(HWND)GetHwndOf(pParent)
  84.                                                                    ,HWND_TOP
  85.                                                                    ,(HMENU)m_windowId
  86.                                                                    ,NULL
  87.                                                                    ,NULL
  88.                                                                   );
  89.  
  90.     m_nPageSize = 1;
  91.     m_nViewSize = 1;
  92.     m_nObjectSize = 1;
  93.     ::WinSendMsg( hScrollBar
  94.                  ,SBM_SETSCROLLBAR
  95.                  ,(MPARAM)0
  96.                  ,MPFROM2SHORT(0,1)
  97.                 );
  98.     ::WinShowWindow( hScrollBar
  99.                     ,TRUE
  100.                    );
  101.     SetFont(*wxSMALL_FONT);
  102.  
  103.     m_hWnd = hScrollBar;
  104.  
  105.     //
  106.     // Subclass again for purposes of dialog editing mode
  107.     //
  108.     SubclassWin((WXHWND)hScrollBar);
  109.     SetSize( nX
  110.             ,nY
  111.             ,nWidth
  112.             ,nHeight
  113.            );
  114.     return TRUE;
  115. } // end of wxScrollBar::Create
  116.  
  117. wxScrollBar::~wxScrollBar()
  118. {
  119. }
  120.  
  121. bool wxScrollBar::OS2OnScroll (
  122.   int                               nOrientation
  123. , WXWORD                            wParam
  124. , WXWORD                            wPos
  125. , WXHWND                            hControl
  126. )
  127. {
  128.     int                             nPosition;
  129.     int                             nMaxPos;
  130.     int                             nTrackPos = wPos;
  131.     int                             nMinPos;
  132.     int                             nScrollInc;
  133.     wxEventType                     vScrollEvent = wxEVT_NULL;
  134.  
  135.     MRESULT                         vRange;
  136.  
  137.     //
  138.     // When we're dragging the scrollbar we can't use pos parameter because it
  139.     // is limited to 16 bits
  140.     //
  141.     if (wParam == SB_SLIDERPOSITION || wParam == SB_SLIDERTRACK)
  142.     {
  143.         SBCDATA                     vScrollInfo;
  144.  
  145.         vScrollInfo.sHilite = SB_SLIDERTRACK;
  146.  
  147.         ::WinSendMsg((HWND)GetHwnd(), WM_QUERYWINDOWPARAMS, (PVOID)&vScrollInfo, NULL);
  148.  
  149.         nTrackPos = vScrollInfo.posThumb;
  150.         nPosition = vScrollInfo.posFirst;
  151.         nMaxPos   = vScrollInfo.posLast;
  152.     }
  153.     else
  154.     {
  155.         nPosition = (int)(MRESULT)::WinSendMsg((HWND)GetHwnd(), SBM_QUERYPOS, (MPARAM)NULL, (MPARAM)NULL);
  156.         vRange = ::WinSendMsg((HWND)GetHwnd(), SBM_QUERYRANGE, (MPARAM)NULL, (MPARAM)NULL);
  157.         nMinPos = SHORT1FROMMR(vRange);
  158.         nMaxPos = SHORT2FROMMR(vRange);
  159.     }
  160.     //
  161.     // A page size greater than one has the effect of reducing the effective
  162.     // range, therefore the range has already been boosted artificially - so
  163.     // reduce it again.
  164.     //
  165.     if (m_nPageSize > 1)
  166.         nMaxPos -= (m_nPageSize - 1);
  167.     switch (wParam)
  168.     {
  169.         case SB_LINEUP:
  170.             nScrollInc   = -1;
  171.             vScrollEvent = wxEVT_SCROLL_LINEUP;
  172.             break;
  173.  
  174.         case SB_LINEDOWN:
  175.             nScrollInc   = 1;
  176.             vScrollEvent = wxEVT_SCROLL_LINEDOWN;
  177.             break;
  178.  
  179.         case SB_PAGEUP:
  180.             nScrollInc   = -GetPageSize();
  181.             vScrollEvent = wxEVT_SCROLL_PAGEUP;
  182.             break;
  183.  
  184.         case SB_PAGEDOWN:
  185.             nScrollInc   = GetPageSize();
  186.             vScrollEvent = wxEVT_SCROLL_PAGEDOWN;
  187.             break;
  188.  
  189.         case SB_SLIDERTRACK:
  190.             nScrollInc   = nTrackPos - nPosition;
  191.             vScrollEvent = wxEVT_SCROLL_THUMBTRACK;
  192.             break;
  193.  
  194.         case SB_ENDSCROLL:
  195.             nScrollInc   = 0;
  196.             vScrollEvent = wxEVT_SCROLL_ENDSCROLL;
  197.             break;
  198.  
  199.         default:
  200.             nScrollInc = 0;
  201.     }
  202.     if (nScrollInc)
  203.     {
  204.         nPosition += nScrollInc;
  205.  
  206.         if (nPosition < 0)
  207.             nPosition = 0;
  208.         if (nPosition > nMaxPos)
  209.             nPosition = nMaxPos;
  210.         SetThumbPosition(nPosition);
  211.     }
  212.     else if ( vScrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
  213.               vScrollEvent != wxEVT_SCROLL_ENDSCROLL
  214.             )
  215.     {
  216.         //
  217.         // Don't process the event if there is no displacement,
  218.         // unless this is a thumb release or end scroll event.
  219.         //
  220.         return FALSE;
  221.     }
  222.  
  223.     wxScrollEvent                   vEvent( vScrollEvent
  224.                                            ,m_windowId
  225.                                           );
  226.  
  227.     vEvent.SetPosition(nPosition);
  228.     vEvent.SetEventObject(this);
  229.     return GetEventHandler()->ProcessEvent(vEvent);
  230. } // end of wxScrollBar::OS2OnScroll
  231.  
  232. void wxScrollBar::SetThumbPosition (
  233.   int                               nViewStart
  234. )
  235. {
  236.     SBCDATA                        vInfo;
  237.  
  238.     memset(&vInfo, '\0', sizeof(SBCDATA));
  239.     vInfo.cb       = sizeof(SBCDATA);
  240.     vInfo.posThumb = nViewStart;
  241.  
  242.     ::WinSendMsg((HWND)GetHwnd(), WM_SETWINDOWPARAMS, (MPARAM)&vInfo, (MPARAM)NULL);
  243.     ::WinSendMsg((HWND)GetHwnd(), SBM_SETPOS, (MPARAM)nViewStart, (MPARAM)NULL);
  244. } // end of wxScrollBar::SetThumbPosition
  245.  
  246. int wxScrollBar::GetThumbPosition() const
  247. {
  248.     return((int)(MRESULT)::WinSendMsg((HWND)GetHwnd(), SBM_QUERYPOS, (MPARAM)NULL, (MPARAM)NULL));
  249. } // end of wxScrollBar::GetThumbPosition
  250.  
  251. void wxScrollBar::SetScrollbar (
  252.   int                               nPosition
  253. , int                               nThumbSize
  254. , int                               nRange
  255. , int                               nPageSize
  256. , bool                              bRefresh
  257. )
  258. {
  259.     SBCDATA                         vInfo;
  260.     //
  261.     // The lRange (number of scroll steps) is the
  262.     // object length minus the page size.
  263.     //
  264.     int                             nRange1 = wxMax((m_nObjectSize - m_nPageSize), 0);
  265.  
  266.     m_nViewSize   = nPageSize;
  267.     m_nPageSize   = nThumbSize;
  268.     m_nObjectSize = nRange;
  269.  
  270.  
  271.     //
  272.     // Try to adjust the lRange to cope with page size > 1
  273.     // (see comment for SetPageLength)
  274.     //
  275.     if (m_nPageSize > 1 )
  276.     {
  277.         nRange1 += (m_nPageSize - 1);
  278.     }
  279.     vInfo.cb = sizeof(SBCDATA);
  280.     vInfo.cVisible = m_nPageSize;
  281.     vInfo.posFirst = 0;
  282.     vInfo.posLast  = nRange1;
  283.     vInfo.posThumb = nPosition;
  284.  
  285.     ::WinSendMsg((HWND)GetHwnd(), WM_SETWINDOWPARAMS, (MPARAM)&vInfo, (MPARAM)NULL);
  286. } // end of wxScrollBar::SetScrollbar
  287.  
  288. WXHBRUSH wxScrollBar::OnCtlColor (
  289.   WXHDC                             hDC
  290. , WXHWND                            hWnd
  291. , WXUINT                            uCtlColor
  292. , WXUINT                            uMessage
  293. , WXWPARAM                          wParam
  294. , WXLPARAM                          lParam
  295. )
  296. {
  297.     //
  298.     // Does nothing under OS/2
  299.     //
  300.     return 0;
  301. } // end of wxScrollBar::OnCtlColor
  302.  
  303. void wxScrollBar::Command (
  304.   wxCommandEvent&                   rEvent
  305. )
  306. {
  307.     SetThumbPosition(rEvent.m_commandInt);
  308.     ProcessCommand(rEvent);
  309. } // end of wxScrollBar::Command
  310.  
  311.