From dba006200f4b262f2381cfcbc2657cd0680502c3 Mon Sep 17 00:00:00 2001 From: Mattia Barbon Date: Sun, 28 Jul 2002 13:33:29 +0000 Subject: [PATCH] Implemented wxSpinButton for wxMotif. Renamed generic wxSpinCtrl::DoGetBestClientSize to DoGetBestSize ( the "Client" appears to be a relic of the past ). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16303 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/spinctlg.h | 2 +- include/wx/motif/spinbutt.h | 27 ++- src/generic/spinctlg.cpp | 7 +- src/motif/spinbutt.cpp | 342 ++++++++++++++++++++++++++++++++-- 4 files changed, 352 insertions(+), 26 deletions(-) diff --git a/include/wx/generic/spinctlg.h b/include/wx/generic/spinctlg.h index 973af21a18..c442db8e9b 100644 --- a/include/wx/generic/spinctlg.h +++ b/include/wx/generic/spinctlg.h @@ -92,7 +92,7 @@ public: protected: // override the base class virtuals involved into geometry calculations - virtual wxSize DoGetBestClientSize() const; + virtual wxSize DoGetBestSize() const; virtual void DoMoveWindow(int x, int y, int width, int height); // common part of all ctors diff --git a/include/wx/motif/spinbutt.h b/include/wx/motif/spinbutt.h index 77aa1ec12d..756b210e03 100644 --- a/include/wx/motif/spinbutt.h +++ b/include/wx/motif/spinbutt.h @@ -16,12 +16,14 @@ #pragma interface "spinbutt.h" #endif +class WXDLLEXPORT wxArrowButton; // internal + class WXDLLEXPORT wxSpinButton : public wxSpinButtonBase { DECLARE_DYNAMIC_CLASS(wxSpinButton) public: - wxSpinButton() { } + wxSpinButton() : m_up( 0 ), m_down( 0 ), m_pos( 0 ) { } wxSpinButton(wxWindow *parent, wxWindowID id = -1, @@ -29,6 +31,9 @@ public: const wxSize& size = wxDefaultSize, long style = wxSP_VERTICAL, const wxString& name = "wxSpinButton") + : m_up( 0 ), + m_down( 0 ), + m_pos( 0 ) { Create(parent, id, pos, size, style, name); } @@ -51,14 +56,24 @@ public: void SetRange(int minVal, int maxVal); // Implementation - virtual void Command(wxCommandEvent& event) { (void)ProcessCommand(event); }; + virtual void Command(wxCommandEvent& event) + { (void)ProcessCommand(event); }; virtual void ChangeFont(bool keepOriginalSize = TRUE); virtual void ChangeBackgroundColour(); virtual void ChangeForegroundColour(); - -protected: - int m_min; - int m_max; +public: + // implementation detail + void Increment( int delta ); + +private: + virtual void DoSetSize(int x, int y, int width, int height, + int sizeFlags = wxSIZE_AUTO); + virtual void DoMoveWindow(int x, int y, int width, int height); + virtual wxSize DoGetBestSize() const; + + wxArrowButton* m_up; + wxArrowButton* m_down; + int m_pos; }; #endif diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index 9c8aad397b..0da033d763 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -190,7 +190,10 @@ bool wxSpinCtrl::Create(wxWindow *parent, } DoSetSize(pos.x, pos.y, csize.x, csize.y); #else - DoSetSize(pos.x, pos.y, size.x, size.y); + wxSize best = GetBestSize(); + if ( size.x != -1 ) best.x = size.x; + if ( size.y != -1 ) best.y = size.y; + DoSetSize(pos.x, pos.y, best.x, best.y); #endif // have to disable this window to avoid interfering it with message // processing to the text and the button... but pretend it is enabled to @@ -220,7 +223,7 @@ wxSpinCtrl::~wxSpinCtrl() // geometry // ---------------------------------------------------------------------------- -wxSize wxSpinCtrl::DoGetBestClientSize() const +wxSize wxSpinCtrl::DoGetBestSize() const { wxSize sizeBtn = m_btn->GetBestSize(), sizeText = m_text->GetBestSize(); diff --git a/src/motif/spinbutt.cpp b/src/motif/spinbutt.cpp index 112d3e0631..b0f2e76d94 100644 --- a/src/motif/spinbutt.cpp +++ b/src/motif/spinbutt.cpp @@ -15,50 +15,358 @@ #include "wx/spinbutt.h" #include "wx/spinctrl.h" +#include "wx/timer.h" -// put wxSpinCtrl here too because it doesn't have a separate file for it in -// wxMotif (yet) -IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl) -IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent) +#ifdef __VMS__ +#pragma message disable nosimpint +#endif +#include +#include +#ifdef __VMS__ +#pragma message enable nosimpint +#endif -bool wxSpinButton::Create(wxWindow *parent, wxWindowID id, const wxPoint& WXUNUSED(pos), const wxSize& WXUNUSED(size), - long style, const wxString& name) +#include "wx/motif/private.h" + +// helper class +enum ArrowDirection { - SetName(name); + wxARROW_UP, + wxARROW_DOWN, + wxARROW_LEFT, + wxARROW_RIGHT +}; +class wxArrowButtonTimer; +class wxArrowButton; + +// ---------------------------------------------------------------------------- +// wxArrowButtonTimer +// ---------------------------------------------------------------------------- + +static const unsigned int TICK_BEFORE_START = 10; +static const unsigned int TICK_BEFORE_EXPONENTIAL = 40; +static const unsigned int MAX_INCREMENT = 150; +static const unsigned int TICK_INTERVAL = 113; + +class wxArrowButtonTimer : public wxTimer +{ +public: + wxArrowButtonTimer( wxArrowButton* btn, int sign ) + : m_sign( sign ), + m_button( btn ) + { Reset(); }; + + void Notify(); + void Reset() { m_ticks = 0; m_increment = 1; } +private: + unsigned int m_ticks; + unsigned int m_increment; + int m_sign; + wxArrowButton* m_button; +}; + +// ---------------------------------------------------------------------------- +// wxArrowButton +// ---------------------------------------------------------------------------- + +class wxArrowButton : public wxControl +{ + friend class wxArrowButtonTimer; +public: + wxArrowButton( int increment ) + : m_increment( increment ), + m_timer( 0 ) {} + + wxArrowButton( wxSpinButton* parent, wxWindowID id, ArrowDirection d, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, int increment = 1 ) + : wxControl(), + m_increment( increment ), + m_timer( 0 ) + { + Create( parent, id, d, pos, size ); + } + + ~wxArrowButton() + { delete m_timer; } + + bool Create( wxSpinButton* parent, wxWindowID id, ArrowDirection d, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize ); +private: + // creates a new timer object, or stops the currently running one + wxTimer* GetFreshTimer(); + wxSpinButton* GetSpinButton() { return (wxSpinButton*)GetParent(); } + static void SpinButtonCallback( Widget w, XtPointer clientData, + XtPointer WXUNUSED(ptr) ); + static void StartTimerCallback( Widget w, XtPointer clientData, + XtPointer WXUNUSED(ptr) ); + + static void StopTimerCallback( Widget w, XtPointer clientData, + XtPointer WXUNUSED(ptr) ); + + int m_increment; + wxArrowButtonTimer* m_timer; +}; + +// ---------------------------------------------------------------------------- +// wxArrowButtonTimer implementation +// ---------------------------------------------------------------------------- + +void wxArrowButtonTimer::Notify() +{ + ++m_ticks; + if( m_ticks < TICK_BEFORE_START ) return; + // increment every other tick + if( m_ticks <= TICK_BEFORE_EXPONENTIAL && m_ticks & 1 ) + return; + if( m_ticks > TICK_BEFORE_EXPONENTIAL ) + m_increment = 2 * m_increment; + if( m_increment >= MAX_INCREMENT ) m_increment = MAX_INCREMENT; + m_button->GetSpinButton()->Increment( m_sign * m_increment ); +} + +// ---------------------------------------------------------------------------- +// wxArrowButton implementation +// ---------------------------------------------------------------------------- + +wxTimer* wxArrowButton::GetFreshTimer() +{ + if( m_timer ) + { + m_timer->Stop(); + m_timer->Reset(); + } + else + m_timer = new wxArrowButtonTimer( this, m_increment ); + + return m_timer; +} + +void wxArrowButton::SpinButtonCallback( Widget w, XtPointer clientData, + XtPointer WXUNUSED(ptr) ) +{ + if( !wxGetWindowFromTable( w ) ) + // Widget has been deleted! + return; + + wxArrowButton* btn = (wxArrowButton*)clientData; + + btn->GetSpinButton()->Increment( btn->m_increment ); +} + +void wxArrowButton::StartTimerCallback( Widget w, XtPointer clientData, + XtPointer WXUNUSED(ptr) ) +{ + if( !wxGetWindowFromTable( w ) ) + // Widget has been deleted! + return; + + wxArrowButton* btn = (wxArrowButton*)clientData; + btn->GetFreshTimer()->Start( TICK_INTERVAL ); +} + +void wxArrowButton::StopTimerCallback( Widget w, XtPointer clientData, + XtPointer WXUNUSED(ptr) ) +{ + if( !wxGetWindowFromTable( w ) ) + // Widget has been deleted! + return; + + wxArrowButton* btn = (wxArrowButton*)clientData; + delete btn->m_timer; + btn->m_timer = 0; +} + +bool wxArrowButton::Create( wxSpinButton* parent, wxWindowID id, + ArrowDirection d, + const wxPoint& pos, const wxSize& size ) +{ + int arrow_dir; + + switch( d ) + { + case wxARROW_UP: + arrow_dir = XmARROW_UP; + break; + case wxARROW_DOWN: + arrow_dir = XmARROW_DOWN; + break; + case wxARROW_LEFT: + arrow_dir = XmARROW_LEFT; + break; + case wxARROW_RIGHT: + arrow_dir = XmARROW_RIGHT; + break; + } + + if( parent ) parent->AddChild( this ); + + Widget parentWidget = (Widget) parent->GetClientWidget(); + m_mainWidget = (WXWidget) XtVaCreateManagedWidget( "XmArrowButton", + xmArrowButtonWidgetClass, + parentWidget, + XmNarrowDirection, arrow_dir, + NULL ); + + XtAddCallback( (Widget) m_mainWidget, + XmNactivateCallback, (XtCallbackProc) SpinButtonCallback, + (XtPointer) this ); + XtAddCallback( (Widget) m_mainWidget, + XmNarmCallback, (XtCallbackProc) StartTimerCallback, + (XtPointer) this ); + XtAddCallback( (Widget) m_mainWidget, + XmNactivateCallback, (XtCallbackProc) StopTimerCallback, + (XtPointer) this ); + + AttachWidget( parent, m_mainWidget, (WXWidget) NULL, + pos.x, pos.y, size.x, size.y ); + + return TRUE; +} + +// ---------------------------------------------------------------------------- +// wxSpinButton +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl); +IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent); + +static void CalcSizes( wxPoint pt, wxSize sz, + wxPoint& pt1, wxSize& sz1, + wxPoint& pt2, wxSize& sz2, + bool isVertical ) +{ + typedef int wxSize::* CDPTR1; + typedef int wxPoint::* CDPTR2; + + sz1 = sz2 = sz; + pt2 = pt1 = pt; + + CDPTR1 szm = isVertical ? &wxSize::y : &wxSize::x; + CDPTR2 ptm = isVertical ? &wxPoint::y : &wxPoint::x; + int dim = sz.*szm, half = dim/2; + + sz1.*szm = half; + sz2.*szm = dim - half; + pt2.*ptm += half + 1; +} + +bool wxSpinButton::Create( wxWindow *parent, wxWindowID id, + const wxPoint& pos, const wxSize& size, + long style, const wxString& name ) +{ m_windowStyle = style; - if (parent) parent->AddChild(this); + wxSize newSize = GetBestSize(); + if( size.x != -1 ) newSize.x = size.x; + if( size.y != -1 ) newSize.y = size.y; + + if( !wxControl::Create( parent, id, pos, newSize, style ) ) + { + return FALSE; + } + + SetName(name); InitBase(); - m_windowId = (id == -1) ? NewControlId() : id; + m_windowId = ( id == -1 ) ? NewControlId() : id; - // TODO create spin button - return FALSE; + bool isVert = IsVertical(); + wxPoint pt1, pt2; + wxSize sz1, sz2; + CalcSizes( wxPoint(0,0), newSize, pt1, sz1, pt2, sz2, isVert ); + m_up = new wxArrowButton( this, -1, isVert ? wxARROW_UP : wxARROW_LEFT, + pt1, sz1, 1 ); + m_down = new wxArrowButton( this, -1, + isVert ? wxARROW_DOWN : wxARROW_RIGHT, + pt2, sz2, -1 ); + + return TRUE; } wxSpinButton::~wxSpinButton() { } +void wxSpinButton::DoMoveWindow(int x, int y, int width, int height) +{ + wxControl::DoMoveWindow( x, y, width, height ); + + wxPoint pt1, pt2; + wxSize sz1, sz2; + + CalcSizes( wxPoint(0,0), wxSize(width,height), pt1, + sz1, pt2, sz2, IsVertical() ); + m_up->SetSize( pt1.x, pt1.y, sz1.x, sz1.y ); + m_down->SetSize( pt2.x, pt2.y, sz2.x, sz2.y ); +} + +void wxSpinButton::DoSetSize(int x, int y, int width, int height, + int sizeFlags) +{ + if( sizeFlags & wxSIZE_AUTO_WIDTH && width == -1 ) + width = 30; + if( sizeFlags & wxSIZE_AUTO_HEIGHT && height == -1 ) + height = 30; + if( sizeFlags & wxSIZE_USE_EXISTING && width == -1 ) + width = GetSize().x; + if( sizeFlags & wxSIZE_USE_EXISTING && height == -1 ) + height = GetSize().y; + + wxControl::DoSetSize(x, y, width, height, 0); +} + +void wxSpinButton::Increment( int delta ) +{ + if( m_pos < m_min ) m_pos = m_min; + if( m_pos > m_max ) m_pos = m_max; + + int npos = m_pos + delta; + + if( npos < m_min ) npos = m_min; + if( npos > m_max ) npos = m_max; + if( npos == m_pos ) return; + + wxSpinEvent event( delta > 0 ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN, + m_windowId ); + event.SetPosition( npos ); + event.SetEventObject( this ); + + GetEventHandler()->ProcessEvent( event ); + + if( event.IsAllowed() ) + { + m_pos = npos; + event.SetEventType( wxEVT_SCROLL_THUMBTRACK ); + event.SetPosition( m_pos ); + + GetEventHandler()->ProcessEvent( event ); + } +} + +wxSize wxSpinButton::DoGetBestSize() const +{ + return IsVertical() ? wxSize( 24, 34 ) : wxSize( 34, 24 ); +} + // Attributes //////////////////////////////////////////////////////////////////////////// int wxSpinButton::GetValue() const { - // TODO - return 0; + return m_pos; } -void wxSpinButton::SetValue(int WXUNUSED(val)) +void wxSpinButton::SetValue(int val) { - // TODO + m_pos = val; } void wxSpinButton::SetRange(int minVal, int maxVal) { - // TODO wxSpinButtonBase::SetRange(minVal, maxVal); } @@ -69,7 +377,7 @@ void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize)) void wxSpinButton::ChangeBackgroundColour() { - // TODO + wxControl::ChangeBackgroundColour(); } void wxSpinButton::ChangeForegroundColour()