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
This commit is contained in:
Mattia Barbon 2002-07-28 13:33:29 +00:00
parent f6b66bc29d
commit dba006200f
4 changed files with 352 additions and 26 deletions

View File

@ -92,7 +92,7 @@ public:
protected: protected:
// override the base class virtuals involved into geometry calculations // 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); virtual void DoMoveWindow(int x, int y, int width, int height);
// common part of all ctors // common part of all ctors

View File

@ -16,12 +16,14 @@
#pragma interface "spinbutt.h" #pragma interface "spinbutt.h"
#endif #endif
class WXDLLEXPORT wxArrowButton; // internal
class WXDLLEXPORT wxSpinButton : public wxSpinButtonBase class WXDLLEXPORT wxSpinButton : public wxSpinButtonBase
{ {
DECLARE_DYNAMIC_CLASS(wxSpinButton) DECLARE_DYNAMIC_CLASS(wxSpinButton)
public: public:
wxSpinButton() { } wxSpinButton() : m_up( 0 ), m_down( 0 ), m_pos( 0 ) { }
wxSpinButton(wxWindow *parent, wxSpinButton(wxWindow *parent,
wxWindowID id = -1, wxWindowID id = -1,
@ -29,6 +31,9 @@ public:
const wxSize& size = wxDefaultSize, const wxSize& size = wxDefaultSize,
long style = wxSP_VERTICAL, long style = wxSP_VERTICAL,
const wxString& name = "wxSpinButton") const wxString& name = "wxSpinButton")
: m_up( 0 ),
m_down( 0 ),
m_pos( 0 )
{ {
Create(parent, id, pos, size, style, name); Create(parent, id, pos, size, style, name);
} }
@ -51,14 +56,24 @@ public:
void SetRange(int minVal, int maxVal); void SetRange(int minVal, int maxVal);
// Implementation // 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 ChangeFont(bool keepOriginalSize = TRUE);
virtual void ChangeBackgroundColour(); virtual void ChangeBackgroundColour();
virtual void ChangeForegroundColour(); virtual void ChangeForegroundColour();
public:
protected: // implementation detail
int m_min; void Increment( int delta );
int m_max;
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 #endif

View File

@ -190,7 +190,10 @@ bool wxSpinCtrl::Create(wxWindow *parent,
} }
DoSetSize(pos.x, pos.y, csize.x, csize.y); DoSetSize(pos.x, pos.y, csize.x, csize.y);
#else #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 #endif
// have to disable this window to avoid interfering it with message // have to disable this window to avoid interfering it with message
// processing to the text and the button... but pretend it is enabled to // processing to the text and the button... but pretend it is enabled to
@ -220,7 +223,7 @@ wxSpinCtrl::~wxSpinCtrl()
// geometry // geometry
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxSize wxSpinCtrl::DoGetBestClientSize() const wxSize wxSpinCtrl::DoGetBestSize() const
{ {
wxSize sizeBtn = m_btn->GetBestSize(), wxSize sizeBtn = m_btn->GetBestSize(),
sizeText = m_text->GetBestSize(); sizeText = m_text->GetBestSize();

View File

@ -15,50 +15,358 @@
#include "wx/spinbutt.h" #include "wx/spinbutt.h"
#include "wx/spinctrl.h" #include "wx/spinctrl.h"
#include "wx/timer.h"
// put wxSpinCtrl here too because it doesn't have a separate file for it in #ifdef __VMS__
// wxMotif (yet) #pragma message disable nosimpint
IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl) #endif
IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent) #include <Xm/ArrowBG.h>
#include <Xm/ArrowB.h>
#ifdef __VMS__
#pragma message enable nosimpint
#endif
bool wxSpinButton::Create(wxWindow *parent, wxWindowID id, const wxPoint& WXUNUSED(pos), const wxSize& WXUNUSED(size), #include "wx/motif/private.h"
long style, const wxString& name)
// 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; 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(); InitBase();
m_windowId = (id == -1) ? NewControlId() : id; m_windowId = ( id == -1 ) ? NewControlId() : id;
// TODO create spin button bool isVert = IsVertical();
return FALSE; 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() 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 // Attributes
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
int wxSpinButton::GetValue() const int wxSpinButton::GetValue() const
{ {
// TODO return m_pos;
return 0;
} }
void wxSpinButton::SetValue(int WXUNUSED(val)) void wxSpinButton::SetValue(int val)
{ {
// TODO m_pos = val;
} }
void wxSpinButton::SetRange(int minVal, int maxVal) void wxSpinButton::SetRange(int minVal, int maxVal)
{ {
// TODO
wxSpinButtonBase::SetRange(minVal, maxVal); wxSpinButtonBase::SetRange(minVal, maxVal);
} }
@ -69,7 +377,7 @@ void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
void wxSpinButton::ChangeBackgroundColour() void wxSpinButton::ChangeBackgroundColour()
{ {
// TODO wxControl::ChangeBackgroundColour();
} }
void wxSpinButton::ChangeForegroundColour() void wxSpinButton::ChangeForegroundColour()