1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/combog.cpp
3 // Purpose: Generic wxComboCtrl
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
32 #include "wx/combobox.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
37 #include "wx/dcbuffer.h"
39 // ----------------------------------------------------------------------------
40 // Some constant adjustments to make the generic more bearable
42 #if defined(__WXUNIVERSAL__)
44 #define TEXTCTRLXADJUST 0 // position adjustment for wxTextCtrl, with zero indent
45 #define TEXTCTRLYADJUST 0
46 #define TEXTXADJUST 0 // how much is read-only text's x adjusted
47 #define DEFAULT_DROPBUTTON_WIDTH 19
49 #elif defined(__WXMSW__)
51 #define TEXTCTRLXADJUST 2 // position adjustment for wxTextCtrl, with zero indent
52 #define TEXTCTRLYADJUST 3
53 #define TEXTXADJUST 0 // how much is read-only text's x adjusted
54 #define DEFAULT_DROPBUTTON_WIDTH 17
56 #elif defined(__WXGTK__)
58 #define TEXTCTRLXADJUST -1 // position adjustment for wxTextCtrl, with zero indent
59 #define TEXTCTRLYADJUST 0
60 #define TEXTXADJUST 1 // how much is read-only text's x adjusted
61 #define DEFAULT_DROPBUTTON_WIDTH 23
63 #elif defined(__WXMAC__)
65 #define TEXTCTRLXADJUST 0 // position adjustment for wxTextCtrl, with zero indent
66 #define TEXTCTRLYADJUST 0
67 #define TEXTXADJUST 0 // how much is read-only text's x adjusted
68 #define DEFAULT_DROPBUTTON_WIDTH 22
72 #define TEXTCTRLXADJUST 0 // position adjustment for wxTextCtrl, with zero indent
73 #define TEXTCTRLYADJUST 0
74 #define TEXTXADJUST 0 // how much is read-only text's x adjusted
75 #define DEFAULT_DROPBUTTON_WIDTH 19
80 // ============================================================================
82 // ============================================================================
84 // Only implement if no native or it wasn't fully featured
85 #ifndef wxCOMBOCONTROL_FULLY_FEATURED
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 BEGIN_EVENT_TABLE(wxGenericComboCtrl
, wxComboCtrlBase
)
93 EVT_PAINT(wxGenericComboCtrl::OnPaintEvent
)
94 EVT_MOUSE_EVENTS(wxGenericComboCtrl::OnMouseEvent
)
98 IMPLEMENT_DYNAMIC_CLASS(wxGenericComboCtrl
, wxComboCtrlBase
)
100 void wxGenericComboCtrl::Init()
104 bool wxGenericComboCtrl::Create(wxWindow
*parent
,
106 const wxString
& value
,
110 const wxValidator
& validator
,
111 const wxString
& name
)
114 // Note that technically we only support 'default' border and wxNO_BORDER.
115 long border
= style
& wxBORDER_MASK
;
116 int tcBorder
= wxNO_BORDER
;
118 #if defined(__WXUNIVERSAL__)
120 border
= wxBORDER_SIMPLE
;
121 #elif defined(__WXMSW__)
123 // For XP, have 1-width custom border, for older version use sunken
124 /*if ( wxUxThemeEngine::GetIfActive() )
126 border = wxBORDER_NONE;
127 m_widthCustomBorder = 1;
130 border
= wxBORDER_SUNKEN
;
134 // Generic version is optimized for wxGTK
137 #define UNRELIABLE_TEXTCTRL_BORDER
141 if ( style
& wxCB_READONLY
)
143 m_widthCustomBorder
= 1;
147 m_widthCustomBorder
= 0;
153 // Have textctrl instead use the border given.
157 // Because we are going to have button outside the border,
158 // let's use wxBORDER_NONE for the whole control.
159 border
= wxBORDER_NONE
;
161 Customize( wxCC_BUTTON_OUTSIDE_BORDER
|
162 wxCC_NO_TEXT_AUTO_SELECT
|
163 wxCC_BUTTON_STAYS_DOWN
);
167 style
= (style
& ~(wxBORDER_MASK
)) | border
;
168 if ( style
& wxCC_STD_BUTTON
)
169 m_iFlags
|= wxCC_POPUP_ON_MOUSE_UP
;
171 // create main window
172 if ( !wxComboCtrlBase::Create(parent
,
177 style
| wxFULL_REPAINT_ON_RESIZE
,
182 // Create textctrl, if necessary
183 CreateTextCtrl( tcBorder
, validator
);
185 // Add keyboard input handlers for main control and textctrl
186 InstallInputHandlers();
189 SetBackgroundStyle( wxBG_STYLE_CUSTOM
); // for double-buffering
191 // SetInitialSize should be called last
192 SetInitialSize(size
);
197 wxGenericComboCtrl::~wxGenericComboCtrl()
201 void wxGenericComboCtrl::OnResize()
204 // Recalculates button and textctrl areas
205 CalculateAreas(DEFAULT_DROPBUTTON_WIDTH
);
208 // Move separate button control, if any, to correct position
211 wxSize sz
= GetClientSize();
212 m_btn
->SetSize( m_btnArea
.x
+ m_btnSpacingX
,
213 (sz
.y
-m_btnSize
.y
)/2,
219 // Move textctrl, if any, accordingly
220 PositionTextCtrl( TEXTCTRLXADJUST
, TEXTCTRLYADJUST
);
223 void wxGenericComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
225 wxSize sz
= GetClientSize();
226 wxAutoBufferedPaintDC
dc(this);
228 const wxRect
& rectb
= m_btnArea
;
229 wxRect rect
= m_tcArea
;
231 // artificial simple border
232 if ( m_widthCustomBorder
)
234 int customBorder
= m_widthCustomBorder
;
237 wxPen
pen1( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
),
242 // area around both controls
243 wxRect
rect2(0,0,sz
.x
,sz
.y
);
244 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
247 if ( customBorder
== 1 )
257 rect2
.x
-= customBorder
;
258 rect2
.y
-= customBorder
;
260 rect2
.width
+= 1 + customBorder
;
261 rect2
.height
+= 1 + customBorder
;
265 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
266 dc
.DrawRectangle(rect2
);
269 #ifndef __WXMAC__ // see note in OnThemeChange
270 wxColour winCol
= GetBackgroundColour();
272 wxColour winCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
277 //wxLogDebug(wxT("hei: %i tcy: %i tchei: %i"),GetClientSize().y,m_tcArea.y,m_tcArea.height);
278 //wxLogDebug(wxT("btnx: %i tcx: %i tcwid: %i"),m_btnArea.x,m_tcArea.x,m_tcArea.width);
280 // clear main background
281 dc
.DrawRectangle(rect
);
285 // Standard button rendering
286 DrawButton(dc
,rectb
);
289 // paint required portion on the control
290 if ( (!m_text
|| m_widthCustomPaint
) )
292 wxASSERT( m_widthCustomPaint
>= 0 );
294 // this is intentionally here to allow drawed rectangle's
295 // right edge to be hidden
297 rect
.width
= m_widthCustomPaint
;
299 dc
.SetFont( GetFont() );
301 dc
.SetClippingRegion(rect
);
302 if ( m_popupInterface
)
303 m_popupInterface
->PaintComboControl(dc
,rect
);
305 wxComboPopup::DefaultPaintComboControl(this,dc
,rect
);
309 void wxGenericComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
312 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
313 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
315 if ( PreprocessMouseEvent(event
,handlerFlags
) )
318 const bool ctrlIsButton
= wxPlatformIs(wxOS_WINDOWS
);
321 (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
323 // if no textctrl and no special double-click, then the entire control acts
325 handlerFlags
|= wxCC_MF_ON_BUTTON
;
326 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
331 if ( isOnButtonArea
|| HasCapture() ||
332 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
334 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
336 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
339 else if ( m_btnState
)
341 // otherwise need to clear the hover status
343 RefreshRect(m_btnArea
);
348 // This will handle left_down and left_dclick events outside button in a Windows/GTK-like manner.
349 // See header file for further information on this method.
350 HandleNormalMouseEvent(event
);
354 void wxGenericComboCtrl::SetCustomPaintWidth( int width
)
356 #ifdef UNRELIABLE_TEXTCTRL_BORDER
358 // If starting/stopping to show an image in front
359 // of a writable text-field, then re-create textctrl
360 // with different kind of border (because we can't
361 // assume that textctrl fully supports wxNO_BORDER).
363 wxTextCtrl
* tc
= GetTextCtrl();
365 if ( tc
&& (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) )
367 int borderType
= tc
->GetWindowStyle() & wxBORDER_MASK
;
368 int tcCreateStyle
= -1;
372 // Re-create textctrl with no border
373 if ( borderType
!= wxNO_BORDER
)
375 m_widthCustomBorder
= 1;
376 tcCreateStyle
= wxNO_BORDER
;
379 else if ( width
== 0 )
381 // Re-create textctrl with normal border
382 if ( borderType
== wxNO_BORDER
)
384 m_widthCustomBorder
= 0;
389 // Common textctrl re-creation code
390 if ( tcCreateStyle
!= -1 )
392 tc
->RemoveEventHandler(m_textEvtHandler
);
393 delete m_textEvtHandler
;
396 wxValidator
* pValidator
= tc
->GetValidator();
399 pValidator
= (wxValidator
*) pValidator
->Clone();
400 CreateTextCtrl( tcCreateStyle
, *pValidator
);
406 CreateTextCtrl( tcCreateStyle
, wxDefaultValidator
);
409 InstallInputHandlers();
412 #endif // UNRELIABLE_TEXTCTRL_BORDER
414 wxComboCtrlBase::SetCustomPaintWidth( width
);
417 bool wxGenericComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
419 int keycode
= event
.GetKeyCode();
420 bool isPopupShown
= IsPopupShown();
422 // This code is AFAIK appropriate for wxGTK.
426 if ( keycode
== WXK_ESCAPE
||
427 ( keycode
== WXK_UP
&& event
.AltDown() ) )
432 if ( keycode
== WXK_DOWN
&& event
.AltDown() )
439 #ifdef __WXUNIVERSAL__
441 bool wxGenericComboCtrl::PerformAction(const wxControlAction
& action
,
443 const wxString
& strArg
)
445 bool processed
= false;
446 if ( action
== wxACTION_COMBOBOX_POPUP
)
448 if ( !IsPopupShown() )
455 else if ( action
== wxACTION_COMBOBOX_DISMISS
)
457 if ( IsPopupShown() )
468 return wxControl::PerformAction(action
, numArg
, strArg
);
474 #endif // __WXUNIVERSAL__
476 // If native wxComboCtrl was not defined, then prepare a simple
477 // front-end so that wxRTTI works as expected.
478 #ifndef _WX_COMBOCONTROL_H_
479 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl
, wxGenericComboCtrl
)
482 #endif // !wxCOMBOCONTROL_FULLY_FEATURED
484 #endif // wxUSE_COMBOCTRL