1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/combog.cpp
3 // Purpose: Generic wxComboCtrl
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
7 // Copyright: (c) 2005 Jaakko Salli
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
31 #include "wx/combobox.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/textctrl.h"
37 #include "wx/dcbuffer.h"
39 // ----------------------------------------------------------------------------
40 // Some constant adjustments to make the generic more bearable
42 #if defined(__WXUNIVERSAL__)
44 // position adjustment for wxTextCtrl, to achieve zero left margin
45 // meaningless if LEFT_MARGIN_CAN_BE_SET set to 1 in combocmn.cpp
46 #define TEXTCTRLXADJUST 0
48 #define TEXTXADJUST 0 // how much is read-only text's x adjusted
49 #define DEFAULT_DROPBUTTON_WIDTH 19
51 #elif defined(__WXMSW__)
53 // position adjustment for wxTextCtrl, to achieve zero left margin
54 // meaningless if LEFT_MARGIN_CAN_BE_SET set to 1 in combocmn.cpp
55 #define TEXTCTRLXADJUST 2
57 #define TEXTXADJUST 0 // how much is read-only text's x adjusted
58 #define DEFAULT_DROPBUTTON_WIDTH 17
60 #elif defined(__WXGTK__)
62 // position adjustment for wxTextCtrl, to achieve zero left margin
63 // meaningless if LEFT_MARGIN_CAN_BE_SET set to 1 in combocmn.cpp
64 #define TEXTCTRLXADJUST -1
66 #define TEXTXADJUST 1 // how much is read-only text's x adjusted
67 #define DEFAULT_DROPBUTTON_WIDTH 23
69 #elif defined(__WXMAC__)
71 // position adjustment for wxTextCtrl, to achieve zero left margin
72 // meaningless if LEFT_MARGIN_CAN_BE_SET set to 1 in combocmn.cpp
73 #define TEXTCTRLXADJUST 0
75 #define TEXTXADJUST 0 // how much is read-only text's x adjusted
76 #define DEFAULT_DROPBUTTON_WIDTH 22
80 // position adjustment for wxTextCtrl, to achieve zero left margin
81 // meaningless if LEFT_MARGIN_CAN_BE_SET set to 1 in combocmn.cpp
82 #define TEXTCTRLXADJUST 0
84 #define TEXTXADJUST 0 // how much is read-only text's x adjusted
85 #define DEFAULT_DROPBUTTON_WIDTH 19
90 // ============================================================================
92 // ============================================================================
94 // Only implement if no native or it wasn't fully featured
95 #ifndef wxCOMBOCONTROL_FULLY_FEATURED
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 BEGIN_EVENT_TABLE(wxGenericComboCtrl
, wxComboCtrlBase
)
103 EVT_PAINT(wxGenericComboCtrl::OnPaintEvent
)
104 EVT_MOUSE_EVENTS(wxGenericComboCtrl::OnMouseEvent
)
108 IMPLEMENT_DYNAMIC_CLASS(wxGenericComboCtrl
, wxComboCtrlBase
)
110 void wxGenericComboCtrl::Init()
114 bool wxGenericComboCtrl::Create(wxWindow
*parent
,
116 const wxString
& value
,
120 const wxValidator
& validator
,
121 const wxString
& name
)
124 // Note that technically we only support 'default' border and wxNO_BORDER.
125 long border
= style
& wxBORDER_MASK
;
126 int tcBorder
= wxNO_BORDER
;
128 #if defined(__WXUNIVERSAL__)
130 border
= wxBORDER_SIMPLE
;
131 #elif defined(__WXMSW__)
133 // For XP, have 1-width custom border, for older version use sunken
134 /*if ( wxUxThemeEngine::GetIfActive() )
136 border = wxBORDER_NONE;
137 m_widthCustomBorder = 1;
140 border
= wxBORDER_SUNKEN
;
144 // Generic version is optimized for wxGTK
147 #define UNRELIABLE_TEXTCTRL_BORDER
151 if ( style
& wxCB_READONLY
)
153 m_widthCustomBorder
= 1;
157 m_widthCustomBorder
= 0;
163 // Have textctrl instead use the border given.
167 // Because we are going to have button outside the border,
168 // let's use wxBORDER_NONE for the whole control.
169 border
= wxBORDER_NONE
;
171 Customize( wxCC_BUTTON_OUTSIDE_BORDER
|
172 wxCC_NO_TEXT_AUTO_SELECT
|
173 wxCC_BUTTON_STAYS_DOWN
);
177 style
= (style
& ~(wxBORDER_MASK
)) | border
;
178 if ( style
& wxCC_STD_BUTTON
)
179 m_iFlags
|= wxCC_POPUP_ON_MOUSE_UP
;
181 // create main window
182 if ( !wxComboCtrlBase::Create(parent
,
187 style
| wxFULL_REPAINT_ON_RESIZE
,
192 // Create textctrl, if necessary
193 CreateTextCtrl( tcBorder
);
195 // Add keyboard input handlers for main control and textctrl
196 InstallInputHandlers();
198 // Set background style for double-buffering, when needed
199 // (cannot use when system draws background automatically)
200 if ( !HasTransparentBackground() )
201 SetBackgroundStyle( wxBG_STYLE_PAINT
);
203 // SetInitialSize should be called last
204 SetInitialSize(size
);
209 wxGenericComboCtrl::~wxGenericComboCtrl()
213 void wxGenericComboCtrl::OnResize()
216 // Recalculates button and textctrl areas
217 CalculateAreas(DEFAULT_DROPBUTTON_WIDTH
);
220 // Move separate button control, if any, to correct position
223 wxSize sz
= GetClientSize();
224 m_btn
->SetSize( m_btnArea
.x
+ m_btnSpacingX
,
225 (sz
.y
-m_btnSize
.y
)/2,
231 // Move textctrl, if any, accordingly
232 PositionTextCtrl( TEXTCTRLXADJUST
);
235 void wxGenericComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
237 // Determine wxDC to use based on need to double-buffer or
238 // use system-generated transparent background portions
240 if ( HasTransparentBackground() )
241 dcPtr
= new wxPaintDC(this);
243 dcPtr
= new wxAutoBufferedPaintDC(this);
246 wxSize sz
= GetClientSize();
247 const wxRect
& butRect
= m_btnArea
;
248 wxRect tcRect
= m_tcArea
;
249 wxRect
fullRect(0, 0, sz
.x
, sz
.y
);
251 // artificial simple border
252 if ( m_widthCustomBorder
)
254 int customBorder
= m_widthCustomBorder
;
258 wxPen
pen1( wxColour(133,133,133),
262 wxPen
pen1( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
),
268 // area around both controls
269 wxRect
rect2(fullRect
);
270 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
273 if ( customBorder
== 1 )
283 rect2
.x
-= customBorder
;
284 rect2
.y
-= customBorder
;
286 rect2
.width
+= 1 + customBorder
;
287 rect2
.height
+= 1 + customBorder
;
291 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
292 dc
.DrawRectangle(rect2
);
295 // Clear the main background if the system doesn't do it by itself
296 if ( !HasTransparentBackground() &&
297 (tcRect
.x
> 0 || tcRect
.y
> 0) )
299 wxColour winCol
= GetParent()->GetBackgroundColour();
303 dc
.DrawRectangle(fullRect
);
308 // Standard button rendering
309 DrawButton(dc
, butRect
);
312 // paint required portion on the control
313 if ( !m_text
|| m_widthCustomPaint
)
315 wxASSERT( m_widthCustomPaint
>= 0 );
317 // Clear the text-control area background
318 wxColour tcCol
= GetBackgroundColour();
321 dc
.DrawRectangle(tcRect
);
323 // this is intentionally here to allow drawed rectangle's
324 // right edge to be hidden
326 tcRect
.width
= m_widthCustomPaint
;
328 dc
.SetFont( GetFont() );
330 dc
.SetClippingRegion(tcRect
);
331 if ( m_popupInterface
)
332 m_popupInterface
->PaintComboControl(dc
, tcRect
);
334 wxComboPopup::DefaultPaintComboControl(this, dc
, tcRect
);
340 void wxGenericComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
343 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
344 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
346 if ( PreprocessMouseEvent(event
,handlerFlags
) )
349 const bool ctrlIsButton
= wxPlatformIs(wxOS_WINDOWS
);
352 (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
354 // if no textctrl and no special double-click, then the entire control acts
356 handlerFlags
|= wxCC_MF_ON_BUTTON
;
357 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
362 if ( isOnButtonArea
|| HasCapture() ||
363 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
365 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
367 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
370 else if ( m_btnState
)
372 // otherwise need to clear the hover status
374 RefreshRect(m_btnArea
);
379 // This will handle left_down and left_dclick events outside button in a Windows/GTK-like manner.
380 // See header file for further information on this method.
381 HandleNormalMouseEvent(event
);
385 void wxGenericComboCtrl::SetCustomPaintWidth( int width
)
387 #ifdef UNRELIABLE_TEXTCTRL_BORDER
389 // If starting/stopping to show an image in front
390 // of a writable text-field, then re-create textctrl
391 // with different kind of border (because we can't
392 // assume that textctrl fully supports wxNO_BORDER).
394 wxTextCtrl
* tc
= GetTextCtrl();
396 if ( tc
&& (m_iFlags
& wxCC_BUTTON_OUTSIDE_BORDER
) )
398 int borderType
= tc
->GetWindowStyle() & wxBORDER_MASK
;
399 int tcCreateStyle
= -1;
403 // Re-create textctrl with no border
404 if ( borderType
!= wxNO_BORDER
)
406 m_widthCustomBorder
= 1;
407 tcCreateStyle
= wxNO_BORDER
;
410 else if ( width
== 0 )
412 // Re-create textctrl with normal border
413 if ( borderType
== wxNO_BORDER
)
415 m_widthCustomBorder
= 0;
420 // Common textctrl re-creation code
421 if ( tcCreateStyle
!= -1 )
423 tc
->RemoveEventHandler(m_textEvtHandler
);
424 delete m_textEvtHandler
;
426 CreateTextCtrl( tcCreateStyle
);
428 InstallInputHandlers();
431 #endif // UNRELIABLE_TEXTCTRL_BORDER
433 wxComboCtrlBase::SetCustomPaintWidth( width
);
436 bool wxGenericComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
438 int keycode
= event
.GetKeyCode();
439 bool isPopupShown
= IsPopupShown();
441 // This code is AFAIK appropriate for wxGTK.
445 if ( keycode
== WXK_ESCAPE
||
446 ( keycode
== WXK_UP
&& event
.AltDown() ) )
451 if ( (keycode
== WXK_DOWN
&& event
.AltDown()) ||
452 (keycode
== WXK_F4
) )
459 #ifdef __WXUNIVERSAL__
461 bool wxGenericComboCtrl::PerformAction(const wxControlAction
& action
,
463 const wxString
& strArg
)
465 bool processed
= false;
466 if ( action
== wxACTION_COMBOBOX_POPUP
)
468 if ( !IsPopupShown() )
475 else if ( action
== wxACTION_COMBOBOX_DISMISS
)
477 if ( IsPopupShown() )
488 return wxControl::PerformAction(action
, numArg
, strArg
);
494 #endif // __WXUNIVERSAL__
496 // If native wxComboCtrl was not defined, then prepare a simple
497 // front-end so that wxRTTI works as expected.
498 #ifndef _WX_COMBOCONTROL_H_
499 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl
, wxGenericComboCtrl
)
502 #endif // !wxCOMBOCONTROL_FULLY_FEATURED
504 #endif // wxUSE_COMBOCTRL