Filter Library Camera Interface Physics

ControlNumSpinCtrl.h

00001 /*
00002  * ControlNumSpinCtrl.h - a spin control for real numbers.
00003  *
00004  * This file is part of the Camera Filter Library.
00005  * Computer Aided Measurement Environment for Realtime Atomic imaging (Camera)
00006  *
00007  * Copyright (C) 2004-2005, Leiden Probe Microscopy.
00008  * Copyright (C) 2004-2005, Universiteit Leiden.
00009  *
00010  * This spin control is based on:
00011  * CNumSpinCtrl - a simple numeric spin control, By Damir Valiulin
00012  * See http://www.codeproject.com/miscctrl/numspinctrl.asp
00013  *
00014  * Authors: Martin J. Moene (original)
00015  *
00016  * $Id: ControlNumSpinCtrl.h 202 2005-05-12 11:00:48Z moene $
00017  */
00018 
00019 #ifndef CFL_CONTROLNUMSPINCTRL_H
00020 #define CFL_CONTROLNUMSPINCTRL_H
00021 
00022 #if _MSC_VER >= 1000
00023 #pragma once
00024 #endif // _MSC_VER >= 1000
00025 
00026 /**
00027  * \addtogroup cfl_controlnumspincontrol Numeric Spin Control
00028  * \brief numeric spin control.
00029  *
00030  * This section contains a numeric spin control class:
00031  * - CNumSpinCtrl
00032  *
00033  * Usually the spin control is combined with a \ref cfl_controlnumedit to display
00034  * the numeric value. Special about CNumSpinCtrl is that it allows you to spin real
00035  * numbers.
00036  *
00037  * Here is how you may use a spin control of type CNumSpinCtrl in a dialog.
00038  * \code
00039  * // mydialog.h - header
00040  *
00041  * class MyDialog
00042  * {
00043  * public:
00044  *    MyDialog( CWnd* pParent = NULL );
00045  *
00046  * protected:
00047  *    virtual void DoDataExchange( CDataExchange* pDX );
00048  *
00049  *    //{{AFX_MSG(MyDialog)
00050  *    virtual BOOL OnInitDialog();
00051  *    afx_msg void OnChangeFactor();
00052  *    afx_msg void OnDeltaposFactor( NMHDR* pNMHDR, LRESULT* pResult );
00053  *    //}}AFX_MSG
00054  *    DECLARE_MESSAGE_MAP()
00055  *
00056  * private:
00057  *    CIntegerEdit c_txtFactor;
00058  *    CNumSpinCtrl c_spnFactor;
00059  *    bool         initialized;
00060  * };
00061  * \endcode
00062  *
00063  * Please note the member variable \c initialized. Under some circumestances
00064  * messages start coming, while not all controls are fully initialized and
00065  * messages should not be handled yet. This happens for example when you
00066  * auto-buddy an edit control with a spin control.
00067  * (See also \ref newcomer_avoidgetdlgitem "[GETDLGITEM]".)
00068  *
00069  * \code
00070  * // mydialog.cpp - implementation
00071  *
00072  * MyDialog::MyDialog( CWnd* pParent ) :
00073  *    c_txtFactor( true   ),  // signed
00074  *    initialized( false  )   // don't handle messages yet; see OnInitDialog()
00075  * {
00076  *    //{{AFX_DATA_INIT(MyDialog)
00077  *    //}}AFX_DATA_INIT
00078  * }
00079  *
00080  * void MyDialog::DoDataExchange( CDataExchange* pDX )
00081  * {
00082  *    CDialog::DoDataExchange(pDX);
00083  *    //{{AFX_DATA_MAP(MyDialog)
00084  *    DDX_Control(pDX, IDC_MYDIALOG_TEXTFACTOR, c_txtFactor );
00085  *    DDX_Control(pDX, IDC_MYDIALOG_SPINFACTOR, c_spnFactor );
00086  *    //}}AFX_DATA_MAP
00087  * }
00088  *
00089  * BEGIN_MESSAGE_MAP( MyDialog )
00090  *    //{{AFX_MSG_MAP(MyDialog)
00091  *    ON_EN_CHANGE( IDC_MYDIALOG_TEXTFACTOR, OnChangeFactor )
00092  *    ON_NOTIFY   ( UDN_DELTAPOS, IDC_MYDIALOG_SPINFACTOR, OnDeltaposFactor )
00093  *    //}}AFX_MSG_MAP
00094  * END_MESSAGE_MAP()
00095  *
00096  * BOOL MyDialog::OnInitDialog()
00097  * {
00098  *    CFilterDlg::OnInitDialog();
00099  *
00100  *    c_spnFactor.SetRangeAndDelta( -1, 1, 0.01 );
00101  *    c_spnFactor = 0.1;
00102  *
00103  *    UpdateData( FALSE ); // transfer data from variables to controls
00104  *
00105  *    initialized = true;  // dialog initialized
00106  *
00107  *    return TRUE;         // we set the focus
00108  * }
00109  *
00110  * // optional handler for c_txtFactor:
00111  *
00112  * void CFilterDlg_Clad::OnChangeFactor()
00113  * {
00114  *     // ensure dialog has been initialized:
00115  *
00116  *    if ( !initialized )
00117  *       return ;
00118  *
00119  *    //  Transfer data from controls to filter variables:
00120  *
00121  *    Q_ASSERT( 0 != UpdateData() );
00122  *
00123  *    //
00124  *    // put your event handling code here, if any.
00125  *    //
00126  * }
00127  *
00128  * // optional handler for c_spnFactor:
00129  *
00130  * void MyDialog::OnDeltaposFactor( NMHDR* pNMHDR, LRESULT* pResult )
00131  * {
00132  *    // ensure dialog has been initialized:
00133  *
00134  *    if ( !initialized )
00135  *       return ;
00136  *
00137  *    NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
00138  *
00139  *    //
00140  *    // put your event handling code here, if any.
00141  *    //
00142  *
00143  *    *pResult = 0;        // result
00144  * }
00145  * \endcode
00146  *
00147  * Note that the entries for c_txtFactor and c_spnFactor in DoDataExchange() are
00148  * required for the typical behaviour of these controls.
00149  * Handlers OnChangeFactor and OnDeltaposFactor() are only needed if you want to
00150  * do something special when the edit control changes or when the spin button is
00151  * pressed.
00152  *
00153  * @{
00154  */
00155 
00156 DECLARE_CLASS( CNumSpinCtrl );          // declare various types for class
00157 
00158 /**
00159  * a spin control for real numbers.
00160  *
00161  * CNumSpinCtrl is a spin control for real numbers that can be combined with a
00162  * \ref cfl_controlnumedit.
00163  *
00164  * Class CNumSpinCtrl enables you to:
00165  * - get and set the range to spin and the delta, e.g. SetRangeAndDelta(),
00166  * - set the format string to represent the number in a buddy control, SetFormat()
00167  * - get and set a new position, e.g. GetPos(), operator double(), operator=()
00168  * - increase or decrease the position with delta, operator++(), operator--()
00169  *
00170  * Make sure that the control is included in method DoDataExchange() of the
00171  * dialog classes where you use the spin control.
00172  *
00173  * Class CNumSpinCtrl is a slightly modified version of the class CNumSpinCtrl by
00174  * Damir Valiulin \ref valiulin_numspinctrl "[NUMSPINCTRL]".
00175  */
00176 
00177 class CNumSpinCtrl : public CSpinButtonCtrl
00178 {
00179    typedef CNumSpinCtrl self;           ///< this type
00180 
00181    DECLARE_DYNAMIC( CNumSpinCtrl )
00182 
00183 public:
00184    typedef double real_type;            ///< real number type
00185    typedef long   integer_type;         ///< integer number type
00186 
00187    ///
00188    /// \name Construction
00189    /// @{
00190 
00191    CNumSpinCtrl();
00192    virtual ~CNumSpinCtrl();
00193 
00194    /// @}
00195    /// \name Operators
00196    /// @{
00197 
00198    operator     double       ();
00199    operator     int          ();
00200    operator     long         ();
00201    operator     unsigned long();
00202 
00203    self&        operator=    ( double        value );
00204    self&        operator=    ( int           value );
00205    self&        operator=    ( long          value );
00206    self&        operator=    ( unsigned long value );
00207 
00208    self&        operator++   ()    ;
00209    void         operator++   (int) ;
00210    self&        operator--   ()    ;
00211    void         operator--   (int) ;
00212 
00213    /// @}
00214    /// \name Predicates
00215    /// @{
00216 
00217    /// @}
00218    /// \name Accessors
00219    /// @{
00220 
00221    void         GetRangeAndDelta( real_type& lower, real_type& upper, real_type& delta );
00222 
00223    double       GetPos( );
00224    integer_type GetPosInt();
00225 
00226    /// @}
00227    /// \name Mutators
00228    /// @{
00229 
00230    void         SetRangeAndDelta( real_type lower, real_type upper, real_type delta );
00231 
00232    void         SetPos( double        value );
00233    void         SetPos( int           value );
00234    void         SetPos( long          value );
00235    void         SetPos( unsigned long value );
00236 
00237    void         SetFormat( LPCTSTR lpszFormatString = NULL );
00238 
00239    void         Select();
00240 
00241    /// @}
00242 
00243    // ClassWizard generated virtual function overrides
00244    //{{AFX_VIRTUAL(CNumSpinCtrl)
00245 protected:
00246    virtual void PreSubclassWindow();
00247    //}}AFX_VIRTUAL
00248 
00249 protected:
00250    void         InitSpinCtrl();
00251    void         SetIntPos (real_type pos);
00252    void         SetValueForBuddy (real_type val);
00253 
00254 protected:
00255    //{{AFX_MSG(CNumSpinCtrl)
00256    afx_msg BOOL OnDeltapos( NMHDR* pNMHDR, LRESULT* pResult );
00257    afx_msg int  OnCreate  ( LPCREATESTRUCT lpCreateStruct   );
00258    //}}AFX_MSG
00259 
00260    DECLARE_MESSAGE_MAP()
00261 
00262 protected:
00263    real_type    m_MinVal;               ///< minimum value
00264    real_type    m_MaxVal;               ///< maximum value
00265    real_type    m_Delta;                ///< stepping value
00266    UINT         m_IntRange;             ///< minimum value
00267    CString      m_csFormat;             ///< printf format string
00268 };
00269 
00270 //{{AFX_INSERT_LOCATION}}
00271 // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
00272 
00273 /// @} cfl_controlnumspincontrol
00274 
00275 /**
00276  * convert to double.
00277  */
00278 
00279 inline CNumSpinCtrl::operator double()
00280 {
00281    return GetPos();
00282 }
00283 
00284 /**
00285  * convert to int.
00286  */
00287 
00288 inline CNumSpinCtrl::operator int()
00289 {
00290    return GetPosInt();
00291 }
00292 
00293 /**
00294  * convert to long.
00295  */
00296 
00297 inline CNumSpinCtrl::operator long()
00298 {
00299    return GetPosInt();
00300 }
00301 
00302 /**
00303  * convert to unsigned long.
00304  */
00305 
00306 inline CNumSpinCtrl::operator unsigned long()
00307 {
00308    return GetPosInt();
00309 }
00310 
00311 /**
00312  * assign double.
00313  */
00314 
00315 inline CNumSpinCtrl::self& CNumSpinCtrl::operator=( double value )
00316 {
00317    SetPos( value );
00318    return *this;
00319 }
00320 
00321 /**
00322  * assign int.
00323  */
00324 
00325 inline CNumSpinCtrl::self& CNumSpinCtrl::operator=( int value )
00326 {
00327    SetPos( value );
00328    return *this;
00329 }
00330 
00331 /**
00332  * assign long.
00333  */
00334 
00335 inline CNumSpinCtrl::self& CNumSpinCtrl::operator=( long value )
00336 {
00337    SetPos( value );
00338    return *this;
00339 }
00340 
00341 /**
00342  * assign unsigned long.
00343  */
00344 
00345 inline CNumSpinCtrl::self& CNumSpinCtrl::operator=( unsigned long value )
00346 {
00347    SetPos( value );
00348    return *this;
00349 }
00350 
00351 /**
00352  * pre-increment with delta.
00353  */
00354 
00355 inline CNumSpinCtrl::self& CNumSpinCtrl::operator++()
00356 {
00357    SetPos( GetPos() + m_Delta );
00358    return *this;
00359 }
00360 
00361 /**
00362  * post-increment with delta (returns void).
00363  */
00364 
00365 inline void CNumSpinCtrl::operator++(int)
00366 {
00367    this->operator++();
00368 }
00369 
00370 /**
00371  * pre-decrement with delta.
00372  */
00373 
00374 inline CNumSpinCtrl::self& CNumSpinCtrl::operator--()
00375 {
00376    SetPos( GetPos() - m_Delta );
00377    return *this;
00378 }
00379 
00380 /**
00381  * post-decrement with delta (returns void).
00382  */
00383 
00384 inline void CNumSpinCtrl::operator--(int)
00385 {
00386    this->operator--();
00387 }
00388 
00389 /**
00390  * the current position as integer.
00391  */
00392 
00393 inline CNumSpinCtrl::integer_type CNumSpinCtrl::GetPosInt()
00394 {
00395    return static_cast<integer_type>( GetPos() );
00396 }
00397 
00398 /**
00399  * set the new positon.
00400  */
00401 
00402 inline void CNumSpinCtrl::SetPos( int value )
00403 {
00404    SetPos( static_cast<real_type>( value ) );
00405 }
00406 
00407 /**
00408  * set the new positon.
00409  */
00410 
00411 inline void CNumSpinCtrl::SetPos( long value )
00412 {
00413    SetPos( static_cast<real_type>( value ) );
00414 }
00415 
00416 /**
00417  * set the new positon.
00418  */
00419 
00420 inline void CNumSpinCtrl::SetPos( unsigned long value )
00421 {
00422    SetPos( static_cast<real_type>( value ) );
00423 }
00424 
00425 #endif // CFL_CONTROLNUMSPINCTRL_H
00426 
00427 /*
00428  * end of file
00429  */

Camera Filter Library documentation © 2004-2007 by Leiden Probe Microscopy