home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / cmnctrls / slidctrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  8.5 KB  |  318 lines

  1. // SlidCtrl.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ctrldemo.h"
  6. #include "SlidCtrl.h"
  7. #include "sliddlg.h"
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CSliderCtrlPage property page
  16.  
  17. IMPLEMENT_DYNCREATE(CSliderCtrlPage, CPropertyPage)
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #endif
  22.  
  23. CSliderCtrlPage::CSliderCtrlPage() : CPropertyPage(CSliderCtrlPage::IDD)
  24. {
  25.     //{{AFX_DATA_INIT(CSliderCtrlPage)
  26.     m_bAutoticks = TRUE;
  27.     m_bEnablesel = FALSE;
  28.     m_bFixed = FALSE;
  29.     m_uiLine = 10;
  30.     m_bNothumb = FALSE;
  31.     m_bNoticks = FALSE;
  32.     m_iOrientation = 0;
  33.     m_uiPage = 50;
  34.     m_iTickpos = 0;
  35.     m_uiRangeFrom = 0;
  36.     m_uiRangeTo = 100;
  37.     m_uiSelRangeFrom = 20;
  38.     m_uiSelRangeTo = 80;
  39.     m_uiTickFreq = 20;
  40.     //}}AFX_DATA_INIT
  41. }
  42.  
  43. CSliderCtrlPage::~CSliderCtrlPage()
  44. {
  45. }
  46.  
  47. void CSliderCtrlPage::DoDataExchange(CDataExchange* pDX)
  48. {
  49.     CPropertyPage::DoDataExchange(pDX);
  50.     //{{AFX_DATA_MAP(CSliderCtrlPage)
  51.     DDX_Control(pDX, IDC_NOTIFICATIONS, m_Notifications);
  52.     DDX_Text(pDX, IDC_SLIDER_LINE, m_uiLine);
  53.     DDV_MinMaxUInt(pDX, m_uiLine, 0, 65535);
  54.     DDX_Text(pDX, IDC_SLIDER_PAGE, m_uiPage);
  55.     DDV_MinMaxUInt(pDX, m_uiPage, 0, 65535);
  56.     DDX_Text(pDX, IDC_SLIDER_RANGEFROM, m_uiRangeFrom);
  57.     DDV_MinMaxUInt(pDX, m_uiRangeFrom, 0, 65535);
  58.     DDX_Text(pDX, IDC_SLIDER_RANGETO, m_uiRangeTo);
  59.     DDV_MinMaxUInt(pDX, m_uiRangeTo, 0, 65535);
  60.     DDX_Text(pDX, IDC_SLIDER_SELRANGEFROM, m_uiSelRangeFrom);
  61.     DDV_MinMaxUInt(pDX, m_uiSelRangeFrom, 0, 65535);
  62.     DDX_Text(pDX, IDC_SLIDER_SELRANGETO, m_uiSelRangeTo);
  63.     DDV_MinMaxUInt(pDX, m_uiSelRangeTo, 0, 65535);
  64.     DDX_Text(pDX, IDC_SLIDER_TICKFREQ, m_uiTickFreq);
  65.     DDV_MinMaxUInt(pDX, m_uiTickFreq, 0, 65535);
  66.     //}}AFX_DATA_MAP
  67. }
  68.  
  69. BOOL CSliderCtrlPage::OnInitDialog()
  70. {
  71.     CPropertyPage::OnInitDialog();
  72.  
  73.     // Initially create slider control in horizontal position
  74.     CWnd* pWnd =
  75.         GetDlgItem( IDC_SLIDER_HORZPOS );
  76.     CRect rect;
  77.     pWnd->GetWindowRect( &rect );
  78.     ScreenToClient( &rect );
  79.  
  80.     // Initialise controls
  81.     m_Slider.Create( WS_VISIBLE|WS_CHILD|TBS_HORZ|TBS_BOTH|TBS_AUTOTICKS|WS_TABSTOP,
  82.                      rect, this, IDC_SLIDER );
  83.     m_Slider.SetTicFreq( m_uiTickFreq );    // Send TBM_SETTICFREQ
  84.     m_Slider.SetLineSize( m_uiLine );        // Send TBM_SETLINESIZE
  85.     m_Slider.SetPageSize( m_uiPage );        // Send TBM_SETPAGESIZE
  86.     m_Slider.SetRange( m_uiRangeFrom, m_uiRangeTo, TRUE );
  87.                                             // Send TBM_SETRANGE
  88.     return TRUE;
  89. }
  90.  
  91. void CSliderCtrlPage::ChangeCtrlStyle( long lStyle, BOOL bSetBit)
  92. {
  93.     long    lStyleOld;
  94.  
  95.     lStyleOld = GetWindowLong( m_Slider.GetSafeHwnd(), GWL_STYLE );
  96.     if ( bSetBit )
  97.         lStyleOld |= lStyle;
  98.     else
  99.         lStyleOld &= ~lStyle;
  100.     SetWindowLong( m_Slider.GetSafeHwnd(), GWL_STYLE, lStyleOld );
  101.     m_Slider.SetWindowPos( NULL, 0, 0, 0, 0, SWP_DRAWFRAME|SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER );
  102. }
  103.  
  104. BEGIN_MESSAGE_MAP(CSliderCtrlPage, CPropertyPage)
  105.     //{{AFX_MSG_MAP(CSliderCtrlPage)
  106.     ON_EN_KILLFOCUS(IDC_SLIDER_TICKFREQ, OnTickfreq)
  107.     ON_EN_KILLFOCUS(IDC_SLIDER_LINE, OnLine)
  108.     ON_EN_KILLFOCUS(IDC_SLIDER_PAGE, OnPage)
  109.     ON_EN_KILLFOCUS(IDC_SLIDER_RANGEFROM, OnRangefrom)
  110.     ON_EN_KILLFOCUS(IDC_SLIDER_RANGETO, OnRangeto)
  111.     ON_EN_KILLFOCUS(IDC_SLIDER_SELRANGEFROM, OnSelrangefrom)
  112.     ON_EN_KILLFOCUS(IDC_SLIDER_SELRANGETO, OnSelrangeto)
  113.     ON_BN_CLICKED(IDC_MORE, OnMore)
  114.     //    ON_WM_HSCROLL()
  115.     //}}AFX_MSG_MAP
  116. END_MESSAGE_MAP()
  117.  
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CSliderCtrlPage message handlers
  120.  
  121. void CSliderCtrlPage::OnTickfreq() 
  122. {
  123.     UpdateData();
  124.     if ( m_bAutoticks )
  125.         // Set tick marks at given frequency (1 = tick at every increment)
  126.         m_Slider.SetTicFreq( m_uiTickFreq );    // Send TBM_SETTICFREQ - need TBS_AUTOTICKS
  127.     m_Slider.InvalidateRect(NULL,FALSE);
  128. }
  129.  
  130. void CSliderCtrlPage::OnLine() 
  131. {
  132.     UpdateData();
  133.  
  134.     // Set line size (determines effect of arrow keys)
  135.     if (m_uiLine > (m_uiRangeTo-m_uiRangeFrom))
  136.     {
  137.         m_uiLine = m_uiRangeTo-m_uiRangeFrom;
  138.         UpdateData(FALSE);
  139.     }
  140.  
  141.     m_Slider.SetLineSize( m_uiLine );        // Send TBM_SETLINESIZE
  142.     m_Slider.InvalidateRect(NULL,FALSE);
  143. }
  144.  
  145. void CSliderCtrlPage::OnPage() 
  146. {
  147.     UpdateData();
  148.  
  149.     // Set page size (determines effect of PageUp/PageDown keys)
  150.     if (m_uiPage > (m_uiRangeTo-m_uiRangeFrom))
  151.     {
  152.         m_uiPage = m_uiRangeTo-m_uiRangeFrom;
  153.         UpdateData(FALSE);
  154.     }
  155.     m_Slider.SetPageSize( m_uiPage );        // Send TBM_SETPAGESIZE
  156.     m_Slider.InvalidateRect(NULL,FALSE);
  157. }
  158.  
  159. void CSliderCtrlPage::OnRangefrom() 
  160. {
  161.     resetSliderRange();
  162. }
  163.  
  164. void CSliderCtrlPage::OnRangeto() 
  165. {
  166.     resetSliderRange();
  167. }
  168.  
  169. void CSliderCtrlPage::OnSelrangefrom() 
  170. {
  171.     resetSliderRange();
  172. }
  173.  
  174. void CSliderCtrlPage::OnSelrangeto() 
  175. {
  176.     resetSliderRange();
  177. }
  178.  
  179. void CSliderCtrlPage::OnMore() 
  180. {
  181.     CSliderCtrlDlg slidCtrlDlg;
  182.     slidCtrlDlg.m_bAutoticks = m_bAutoticks;
  183.     slidCtrlDlg.m_bEnablesel = m_bEnablesel;
  184.     slidCtrlDlg.m_bFixed = m_bFixed;
  185.     slidCtrlDlg.m_bNothumb = m_bNothumb;
  186.     slidCtrlDlg.m_bNoticks = m_bNoticks;
  187.     slidCtrlDlg.m_iOrientation = m_iOrientation;
  188.     slidCtrlDlg.m_iTickpos = m_iTickpos;
  189.  
  190.     if(slidCtrlDlg.DoModal() == IDOK)
  191.     {
  192.         m_bAutoticks = slidCtrlDlg.m_bAutoticks;
  193.         m_bEnablesel = slidCtrlDlg.m_bEnablesel;
  194.         m_bFixed = slidCtrlDlg.m_bFixed;
  195.         m_bNothumb = slidCtrlDlg.m_bNothumb;
  196.         m_bNoticks = slidCtrlDlg.m_bNoticks;
  197.         m_iOrientation = slidCtrlDlg.m_iOrientation;
  198.         m_iTickpos = slidCtrlDlg.m_iTickpos;
  199.         SetSliderCtrlStyle();
  200.     }
  201. }
  202.  
  203.  
  204. void CSliderCtrlPage::SetSliderCtrlStyle()
  205. {    
  206.     long lStyle = 0;
  207.     long lMask = TBS_AUTOTICKS | TBS_ENABLESELRANGE | TBS_FIXEDLENGTH |
  208.                  TBS_NOTHUMB | TBS_NOTICKS | TBS_VERT | TBS_HORZ |
  209.                  TBS_BOTH | TBS_TOP | TBS_BOTTOM;
  210.  
  211.     // Set boolean styles
  212.     if(m_bAutoticks)
  213.         lStyle |= TBS_AUTOTICKS;
  214.     if(m_bEnablesel)
  215.         lStyle |= TBS_ENABLESELRANGE;
  216.     if(m_bFixed)
  217.         lStyle |= TBS_FIXEDLENGTH;
  218.     if(m_bNothumb)
  219.         lStyle |= TBS_NOTHUMB;
  220.     if(m_bNoticks)
  221.         lStyle |= TBS_NOTICKS;
  222.     if(m_iOrientation) 
  223.         lStyle |= TBS_VERT;
  224.     else 
  225.         lStyle |= TBS_HORZ;
  226.     switch ( m_iTickpos )
  227.     {
  228.     case 0:
  229.         lStyle |= TBS_BOTH;
  230.         break;
  231.     case 1:
  232.         lStyle |= TBS_TOP;
  233.         break;
  234.     case 2:
  235.         lStyle |= TBS_BOTTOM;
  236.         break;
  237.     default:
  238.         break;
  239.     }
  240.  
  241.     long lNewStyle = GetWindowLong( m_Slider.GetSafeHwnd(), GWL_STYLE );
  242.     lNewStyle &= ~lMask; 
  243.     lNewStyle = lNewStyle | lStyle;
  244.     SetWindowLong( m_Slider.GetSafeHwnd(), GWL_STYLE, lNewStyle );
  245.     m_Slider.SetWindowPos( NULL, 0, 0, 0, 0, SWP_DRAWFRAME|SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER );
  246.  
  247.     if ( m_bAutoticks ) {
  248.         // Set tick marks with given frequency (1 = tick at every increment)
  249.         m_Slider.SetTicFreq( m_uiTickFreq );    // Send TBM_SETTICFREQ - need TBS_AUTOTICKS
  250.         m_Slider.InvalidateRect(NULL,FALSE);
  251.     }
  252.     else
  253.         // Clear tick marks
  254.         m_Slider.ClearTics( TRUE );
  255.     if ( m_bEnablesel ) {
  256.         // Set selection range
  257.         m_Slider.SetSelection( m_uiSelRangeFrom, m_uiSelRangeTo );    // Send TBM_SETSEL
  258.         m_Slider.InvalidateRect(NULL,FALSE);    
  259.     }
  260.     else
  261.         // Clear selection range
  262.         m_Slider.ClearSel( TRUE );
  263.  
  264.     // Enable/disable edit controls
  265.     GetDlgItem( IDC_SLIDER_TICKFREQ )->EnableWindow( m_bAutoticks );
  266.     GetDlgItem( IDC_SLIDER_SELRANGEFROM )->EnableWindow( m_bEnablesel );
  267.     GetDlgItem( IDC_SLIDER_SELRANGETO )->EnableWindow( m_bEnablesel );
  268.     GetDlgItem( IDC_SELRANGE_STATIC )->EnableWindow( m_bEnablesel );
  269.  
  270.     // Obtain horz/vert place marker (static control)
  271.     CWnd* pWnd =
  272.         GetDlgItem( (0==m_iOrientation)?IDC_SLIDER_HORZPOS:IDC_SLIDER_VERTPOS );
  273.     CRect rect;
  274.     pWnd->GetWindowRect( &rect );
  275.     ScreenToClient( &rect );
  276.     // Move control to horz/vert position
  277.     m_Slider.SetWindowPos( NULL,
  278.                            rect.left, rect.top,
  279.                            rect.Width(), rect.Height(),
  280.                            SWP_NOZORDER|SWP_SHOWWINDOW );
  281.     Invalidate();
  282. }
  283.  
  284. void CSliderCtrlPage::resetSliderRange()
  285. {
  286.     int nChanged = 0;
  287.     UpdateData();
  288.     
  289.     if (m_uiRangeFrom > m_uiRangeTo) 
  290.     {
  291.         SWAP(m_uiRangeFrom, m_uiRangeTo);
  292.         nChanged++;
  293.     }
  294.     if (m_uiSelRangeFrom > m_uiSelRangeTo) 
  295.     {
  296.         SWAP(m_uiSelRangeFrom, m_uiSelRangeTo);
  297.         nChanged++;
  298.     }
  299.     if (m_uiSelRangeFrom < m_uiRangeFrom)
  300.     {
  301.         m_uiSelRangeFrom = m_uiRangeFrom;
  302.         nChanged++;
  303.     }
  304.     if (m_uiSelRangeTo > m_uiRangeTo)
  305.     {
  306.         m_uiSelRangeTo = m_uiRangeTo;
  307.         nChanged++;
  308.     }
  309.     if (nChanged > 0)
  310.         UpdateData(FALSE);
  311.  
  312.     m_Slider.SetRangeMin( m_uiRangeFrom, TRUE );                // Send TBM_SETRANGE
  313.     m_Slider.SetRangeMax( m_uiRangeTo, TRUE );                    // Send TBM_SETRANGE
  314.     m_Slider.SetSelection( m_uiSelRangeFrom, m_uiSelRangeTo );    // Send TBM_SETSEL
  315.     m_Slider.SetSelection( m_uiSelRangeFrom, m_uiSelRangeTo );    // Send TBM_SETSEL
  316.     m_Slider.InvalidateRect(NULL,FALSE);
  317. }
  318.