spinctrl.cpp
来自「gspiceui电子CAD仿真程序.用于电路参数模拟仿真」· C++ 代码 · 共 456 行
CPP
456 行
//*****************************************************************************// SpinCtrl.cpp *// -------------- *// Started : 20/03/2004 *// Last Update : 01/07/2005 *// Copyright : (C) 2004 by MSWaters *// Email : M.Waters@bom.gov.au *//*****************************************************************************//*****************************************************************************// *// This program is free software; you can redistribute it and/or modify *// it under the terms of the GNU General Public License as published by *// the Free Software Foundation; either version 2 of the License, or *// (at your option) any later version. *// *//*****************************************************************************#include "SpinCtrl.hpp"//*****************************************************************************// Implement an event table.BEGIN_EVENT_TABLE( SpinCtrl, wxPanel ) EVT_SPIN_UP ( ID_SPINBUTT, SpinCtrl::OnSbnScroll ) EVT_SPIN_DOWN( ID_SPINBUTT, SpinCtrl::OnSbnScroll )END_EVENT_TABLE( )//*****************************************************************************// Constructor.//// Argument List:// eType - The type of the number to be displayedSpinCtrl::SpinCtrl( eVarType eVType ) : wxPanel( ){ if( ! bSetVType( eVType ) ) // Set the variable type to display m_eVType = eVAR_FLT; // Set the default variable type to display m_fInitV = 0.0; // Set the default initial value m_fMinV = -1000.0; // Set the default minimum value m_fMaxV = 1000.0; // Set the default maximum value m_fMinIncSz = 10.0; // Set the default minimum increment size m_fMaxIncSz = 10.0; // Set the default maximum increment size}//*****************************************************************************// Destructor.SpinCtrl::~SpinCtrl( ){}//*****************************************************************************// Create an instance of this object.//// Argument List:// poWin - The parent window// oWinID - The window identifier// iWidth - The width the spin control in pixels//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bCreate( wxWindow * poWin, wxWindowID oWinID, int iWidth ){ wxSize oSize=wxDefaultSize; if( bIsCreated( ) ) return( TRUE ); // Create the display objects if( ! Create( poWin, oWinID ) ) return( FALSE ); if( iWidth > 0 ) oSize.SetWidth( iWidth ); m_oTxtValue.Create( this, ID_TEXTCTRL, wxT(""), wxDefaultPosition, oSize, wxTE_LEFT ); m_oSbnValue.Create( this, ID_SPINBUTT, wxDefaultPosition, wxDefaultSize, wxSP_VERTICAL | wxSP_ARROW_KEYS ); // Set the display object attributes m_oSbnValue.SetRange( -0x8000, 0x7FFF ); bSetParms( m_fInitV, m_fMinV, m_fMaxV, m_fMinIncSz, m_fMaxIncSz ); bSetValue( m_fInitV ); // Layout manager stuff wxBoxSizer * poSizer = new wxBoxSizer( wxHORIZONTAL ); wxSizerFlags oFlags; oFlags.Expand( ); oFlags.Proportion( 1 ); poSizer->Add( &m_oTxtValue, oFlags ); oFlags.Proportion( 0 ); poSizer->Add( &m_oSbnValue, oFlags ); SetSizer( poSizer ); // Set minimum size and initial size as calculated by the sizer poSizer->SetSizeHints( this ); return( TRUE );}//*****************************************************************************// Clear the object attributes.//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bClear( void ){ return( bSetValue( m_fInitV ) );}//*****************************************************************************// Set the variable type to displayed by the spin control.//// Argument List:// eVType - The variable type specifier//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bSetVType( eVarType eVType ){ float f1; // Argument validity checks if( m_eVType == eVType ) return( TRUE ); if( eVType<eVAR_FST || eVType>eVAR_LST ) return( FALSE ); m_eVType = eVType; // Set the new variable type // Display the new variable type if( bIsCreated( ) ) { f1 = fGetValue( ); if( ! bSetValue( f1 ) ) return( FALSE ); } return( TRUE );}//*****************************************************************************// Set the current value of the spin control.//// Argument List:// fValue - The float value//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bSetValue( float fValue ){ wxString osValue, os1; int i1; if( ! bIsCreated( ) ) return( FALSE ); if( fValue<m_fMinV || fValue>m_fMaxV ) return( FALSE ); m_oTxtValue.Clear( ); switch( m_eVType ) { case eVAR_INT: if( fValue >= 0.0 ) i1 = (int) ( fValue + 0.5 ); else i1 = (int) ( fValue - 0.5 ); osValue << i1; break; case eVAR_FLT: if( fabs( fValue ) < 1000.0 ) os1 = wxT("%#.2f"); else os1 = wxT("%#.1f"); osValue.Printf( os1.c_str( ), fValue ); break; default: return( FALSE ); } m_oTxtValue.SetValue( osValue ); return( TRUE );}//*****************************************************************************// Set the current value of the spin control.//// Argument List:// fValue - The string value//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bSetValue( const wxString & rosValue ){ double fValue; if( ! ConvertType::bStrToFlt( rosValue, &fValue ) ) return( FALSE ); if( ! bSetValue( (float) fValue ) ) return( FALSE ); return( TRUE );}//*****************************************************************************// Set the initial value of the spin control.//// Argument List:// fInitV - The spin control initial value//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bSetInitV( float fInitV ){ if( fInitV<m_fMinV || fInitV>m_fMaxV ) return( FALSE ); m_fInitV = fInitV; return( TRUE );}//*****************************************************************************// Set the increment sizes of the spin control.//// This spin control can be incremented using two different approaches,// constant or variable step sizes. A constant step size means that the spin// control is incremented by the same amount throughout it's range. A variable// step size means that the spin control is incremented by an amount dependent// on it's current value.//// Argument List:// fMinIncSz - The minimum spin control increment size// fMaxIncSz - The maximum spin control increment size//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bSetIncSz( float fMinIncSz, float fMaxIncSz ){ // Constant or variable incrementing? if( fMaxIncSz <= 0.0 ) fMaxIncSz = fMinIncSz; // Do some validity checks on the arguments if( fMinIncSz > fMaxIncSz ) return( FALSE ); // Check that the new increment sizes fit within the current range if( fMaxIncSz > (m_fMaxV - m_fMinV) ) return( FALSE ); m_fMinIncSz = fMinIncSz; // Set increment sizes m_fMaxIncSz = fMaxIncSz; return( TRUE );}//*****************************************************************************// Set the spincontrol range.//// Argument List:// fMinV - The spin control minimum value// fMaxV - The spin control maximum value//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bSetRange( float fMinV, float fMaxV ){ return( bSetParms( m_fInitV, fMinV, fMaxV ) );}//*****************************************************************************// Set all the spin control parameters.//// Argument List:// fInitV - The spin control initial value// fMinV - The spin control minimum value// fMaxV - The spin control maximum value// fMinIncSz - The spin control minimum increment size// fMaxIncSz - The spin control maximum increment size//// Return Values:// TRUE - Success// FALSE - Failurebool SpinCtrl::bSetParms( float fInitV, float fMinV, float fMaxV, float fMinIncSz, float fMaxIncSz ){ wxString os1; double f1; // Check argument validity if( fMinV > fMaxV ) return( FALSE ); if( fMinIncSz > 0.0 ) f1 = fMinIncSz; else f1 = m_fMinIncSz; if( f1 > (fMaxV-fMinV) ) return( FALSE ); if( fInitV<fMinV || fInitV>fMaxV ) return( FALSE ); // Set new parameter values m_fInitV = fInitV; m_fMinV = fMinV; m_fMaxV = fMaxV; // Update the text control if necessary if( bIsCreated( ) ) { os1 = m_oTxtValue.GetValue( ); ConvertType::bStrToFlt( os1, &f1 ); if( f1 < m_fMinV ) bSetValue( m_fMinV ); else if( f1 > m_fMaxV ) bSetValue( m_fMaxV ); } if( fMinIncSz > 0.0 ) if( ! bSetIncSz( fMinIncSz, fMaxIncSz ) ) return( FALSE ); return( TRUE );}//*****************************************************************************// Get the current spin control value as an integer.//// Return Values:// Success - The integer value// Failure - 0int SpinCtrl::iGetValue( void ){ float f1; int i1; if( ! bIsCreated( ) ) return( 0 ); f1 = fGetValue( ); if( f1 >= 0.0 ) i1 = (int) ( f1 + 0.5 ); else i1 = (int) ( f1 - 0.5 ); return( i1 );}//*****************************************************************************// Get the current spin control value as a float.//// Return Values:// Success - The float value// Failure - 0.0float SpinCtrl::fGetValue( void ){ wxString os1; double f1; if( ! bIsCreated( ) ) return( 0.0 ); os1 = m_oTxtValue.GetValue( ); f1 = 0.0; ConvertType::bStrToFlt( os1 , &f1 ); return( f1 );}//*****************************************************************************// Get the current spin control value as a string.//// Return Values:// Success - The string value// Failure - An empty stringconst wxString & SpinCtrl::rosGetValue( void ){ static wxString osValue; double f1; osValue.Empty( ); if( ! bIsCreated( ) ) return( osValue ); osValue = m_oTxtValue.GetValue( ); if( ! osValue.ToDouble( &f1 ) ) osValue.Empty( ); return( osValue );}//*****************************************************************************//// Event Handlers////*****************************************************************************// Spin button scroll event handler.//// Argument List:// roEvtSpn - An object holding information about the eventvoid SpinCtrl::OnSbnScroll( wxSpinEvent & roEvtSpn ){ wxString os1; double df1; float fIncSz, f2; long li1; int i2; // Get the current text control value os1 = m_oTxtValue.GetValue( ); df1 = 0.0; if( ! ConvertType::bStrToFlt( os1, &df1 ) ) return; // Determine the increment size to use if( ! ConvertType::bParseFlt( df1, &f2, &i2 ) ) return; if( roEvtSpn.GetEventType( )==wxEVT_SCROLL_LINEDOWN && f2== 1.0 ) i2 -= 1; if( roEvtSpn.GetEventType( )==wxEVT_SCROLL_LINEUP && f2==-1.0 ) i2 -= 1; fIncSz = (float) pow( 10.0, (double) i2 ); if( fIncSz < m_fMinIncSz ) fIncSz = m_fMinIncSz; if( fIncSz > m_fMaxIncSz ) fIncSz = m_fMaxIncSz; // Determine if the text control has been altered by the user li1 = (long) ( df1 / fIncSz ); f2 = (float) ( df1 - ( (float) li1 ) * fIncSz ); if( f2<0.01 && f2>-0.01 ) f2 = 0.0; // Determine the event type and act accordingly if( roEvtSpn.GetEventType( ) == wxEVT_SCROLL_LINEUP ) { // Increment the value if( df1 < (double) m_fMaxV ) { df1 += (double) fIncSz; if( f2 != 0.0 ) { if( f2 > 0.0 ) df1 -= (double) f2; else df1 -= (double) ( f2 + fIncSz ); } if( df1 > (double) m_fMaxV ) df1 = (double) m_fMaxV; } } else if( roEvtSpn.GetEventType( ) == wxEVT_SCROLL_LINEDOWN ) { // Decrement the value if( df1 > (double) m_fMinV ) { df1 -= (double) fIncSz; if( f2 != 0.0 ) { if( f2 > 0.0 ) df1 -= (double) ( f2 - fIncSz ); else df1 -= (double) f2; } if( df1 < (double) m_fMinV ) df1 = (double) m_fMinV; } } else return; bSetValue( (float) df1 ); // Set the text control value roEvtSpn.Skip( ); // Continue to search for event handlers}//*****************************************************************************
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?