Make room for the focus ring to be visible on Mac.
[wxWidgets.git] / src / generic / combog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/combog.cpp
3 // Purpose: Generic wxComboCtrl
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: Apr-30-2006
7 // RCS-ID: $Id$
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_COMBOCTRL
27
28 #include "wx/combo.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32 #include "wx/combobox.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #endif
36
37 #include "wx/dcbuffer.h"
38
39 // ----------------------------------------------------------------------------
40 // Some constant adjustments to make the generic more bearable
41
42 #if defined(__WXUNIVERSAL__)
43
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
48
49 #elif defined(__WXMSW__)
50
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
55
56 #elif defined(__WXGTK__)
57
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
62
63 #elif defined(__WXMAC__)
64
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
69
70 #else
71
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
76
77 #endif
78
79
80 // ============================================================================
81 // implementation
82 // ============================================================================
83
84 // Only implement if no native or it wasn't fully featured
85 #ifndef wxCOMBOCONTROL_FULLY_FEATURED
86
87
88 // ----------------------------------------------------------------------------
89 // wxGenericComboCtrl
90 // ----------------------------------------------------------------------------
91
92 BEGIN_EVENT_TABLE(wxGenericComboCtrl, wxComboCtrlBase)
93 EVT_PAINT(wxGenericComboCtrl::OnPaintEvent)
94 EVT_MOUSE_EVENTS(wxGenericComboCtrl::OnMouseEvent)
95 END_EVENT_TABLE()
96
97
98 IMPLEMENT_DYNAMIC_CLASS(wxGenericComboCtrl, wxComboCtrlBase)
99
100 void wxGenericComboCtrl::Init()
101 {
102 }
103
104 bool wxGenericComboCtrl::Create(wxWindow *parent,
105 wxWindowID id,
106 const wxString& value,
107 const wxPoint& pos,
108 const wxSize& size,
109 long style,
110 const wxValidator& validator,
111 const wxString& name)
112 {
113 //
114 // Note that technically we only support 'default' border and wxNO_BORDER.
115 long border = style & wxBORDER_MASK;
116 int tcBorder = wxNO_BORDER;
117
118 #if defined(__WXUNIVERSAL__)
119 if ( !border )
120 border = wxBORDER_SIMPLE;
121 #elif defined(__WXMSW__)
122 if ( !border )
123 // For XP, have 1-width custom border, for older version use sunken
124 /*if ( wxUxThemeEngine::GetIfActive() )
125 {
126 border = wxBORDER_NONE;
127 m_widthCustomBorder = 1;
128 }
129 else*/
130 border = wxBORDER_SUNKEN;
131 #else
132
133 //
134 // Generic version is optimized for wxGTK
135 //
136
137 #define UNRELIABLE_TEXTCTRL_BORDER
138
139 if ( !border )
140 {
141 if ( style & wxCB_READONLY )
142 {
143 m_widthCustomBorder = 1;
144 }
145 else
146 {
147 m_widthCustomBorder = 0;
148 tcBorder = 0;
149 }
150 }
151 else
152 {
153 // Have textctrl instead use the border given.
154 tcBorder = border;
155 }
156
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;
160
161 Customize( wxCC_BUTTON_OUTSIDE_BORDER |
162 wxCC_NO_TEXT_AUTO_SELECT );
163
164 #endif
165
166 style = (style & ~(wxBORDER_MASK)) | border;
167 if ( style & wxCC_STD_BUTTON )
168 m_iFlags |= wxCC_POPUP_ON_MOUSE_UP;
169
170 // create main window
171 if ( !wxComboCtrlBase::Create(parent,
172 id,
173 value,
174 pos,
175 size,
176 style | wxFULL_REPAINT_ON_RESIZE,
177 wxDefaultValidator,
178 name) )
179 return false;
180
181 // Create textctrl, if necessary
182 CreateTextCtrl( tcBorder, validator );
183
184 // Add keyboard input handlers for main control and textctrl
185 InstallInputHandlers();
186
187 // Set background
188 SetBackgroundStyle( wxBG_STYLE_CUSTOM ); // for double-buffering
189
190 // SetInitialSize should be called last
191 SetInitialSize(size);
192
193 return true;
194 }
195
196 wxGenericComboCtrl::~wxGenericComboCtrl()
197 {
198 }
199
200 void wxGenericComboCtrl::OnResize()
201 {
202
203 // Recalculates button and textctrl areas
204 CalculateAreas(DEFAULT_DROPBUTTON_WIDTH);
205
206 #if 0
207 // Move separate button control, if any, to correct position
208 if ( m_btn )
209 {
210 wxSize sz = GetClientSize();
211 m_btn->SetSize( m_btnArea.x + m_btnSpacingX,
212 (sz.y-m_btnSize.y)/2,
213 m_btnSize.x,
214 m_btnSize.y );
215 }
216 #endif
217
218 // Move textctrl, if any, accordingly
219 PositionTextCtrl( TEXTCTRLXADJUST, TEXTCTRLYADJUST );
220 }
221
222 void wxGenericComboCtrl::OnPaintEvent( wxPaintEvent& WXUNUSED(event) )
223 {
224 wxSize sz = GetClientSize();
225 wxAutoBufferedPaintDC dc(this);
226
227 const wxRect& rectb = m_btnArea;
228 wxRect rect = m_tcArea;
229
230 // artificial simple border
231 if ( m_widthCustomBorder )
232 {
233 int customBorder = m_widthCustomBorder;
234
235 // Set border colour
236 wxPen pen1( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT),
237 customBorder,
238 wxSOLID );
239 dc.SetPen( pen1 );
240
241 // area around both controls
242 wxRect rect2(0,0,sz.x,sz.y);
243 if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE )
244 {
245 rect2 = m_tcArea;
246 if ( customBorder == 1 )
247 {
248 rect2.Inflate(1);
249 }
250 else
251 {
252 #ifdef __WXGTK__
253 rect2.x -= 1;
254 rect2.y -= 1;
255 #else
256 rect2.x -= customBorder;
257 rect2.y -= customBorder;
258 #endif
259 rect2.width += 1 + customBorder;
260 rect2.height += 1 + customBorder;
261 }
262 }
263
264 dc.SetBrush( *wxTRANSPARENT_BRUSH );
265 dc.DrawRectangle(rect2);
266 }
267
268 #ifndef __WXMAC__ // see note in OnThemeChange
269 wxColour winCol = GetBackgroundColour();
270 #else
271 wxColour winCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
272 #endif
273 dc.SetBrush(winCol);
274 dc.SetPen(winCol);
275
276 //wxLogDebug(wxT("hei: %i tcy: %i tchei: %i"),GetClientSize().y,m_tcArea.y,m_tcArea.height);
277 //wxLogDebug(wxT("btnx: %i tcx: %i tcwid: %i"),m_btnArea.x,m_tcArea.x,m_tcArea.width);
278
279 // clear main background
280 dc.DrawRectangle(rect);
281
282 if ( !m_btn )
283 {
284 // Standard button rendering
285 DrawButton(dc,rectb);
286 }
287
288 // paint required portion on the control
289 if ( (!m_text || m_widthCustomPaint) )
290 {
291 wxASSERT( m_widthCustomPaint >= 0 );
292
293 // this is intentionally here to allow drawed rectangle's
294 // right edge to be hidden
295 if ( m_text )
296 rect.width = m_widthCustomPaint;
297
298 dc.SetFont( GetFont() );
299
300 dc.SetClippingRegion(rect);
301 if ( m_popupInterface )
302 m_popupInterface->PaintComboControl(dc,rect);
303 else
304 wxComboPopup::DefaultPaintComboControl(this,dc,rect);
305 }
306 }
307
308 void wxGenericComboCtrl::OnMouseEvent( wxMouseEvent& event )
309 {
310 int mx = event.m_x;
311 bool isOnButtonArea = m_btnArea.Contains(mx,event.m_y);
312 int handlerFlags = isOnButtonArea ? wxCC_MF_ON_BUTTON : 0;
313
314 if ( PreprocessMouseEvent(event,handlerFlags) )
315 return;
316
317 const bool ctrlIsButton = wxPlatformIs(wxOS_WINDOWS);
318
319 if ( ctrlIsButton &&
320 (m_windowStyle & (wxCC_SPECIAL_DCLICK|wxCB_READONLY)) == wxCB_READONLY )
321 {
322 // if no textctrl and no special double-click, then the entire control acts
323 // as a button
324 handlerFlags |= wxCC_MF_ON_BUTTON;
325 if ( HandleButtonMouseEvent(event,handlerFlags) )
326 return;
327 }
328 else
329 {
330 if ( isOnButtonArea || HasCapture() ||
331 (m_widthCustomPaint && mx < (m_tcArea.x+m_widthCustomPaint)) )
332 {
333 handlerFlags |= wxCC_MF_ON_CLICK_AREA;
334
335 if ( HandleButtonMouseEvent(event,handlerFlags) )
336 return;
337 }
338 else if ( m_btnState )
339 {
340 // otherwise need to clear the hover status
341 m_btnState = 0;
342 RefreshRect(m_btnArea);
343 }
344 }
345
346 //
347 // This will handle left_down and left_dclick events outside button in a Windows/GTK-like manner.
348 // See header file for further information on this method.
349 HandleNormalMouseEvent(event);
350
351 }
352
353 void wxGenericComboCtrl::SetCustomPaintWidth( int width )
354 {
355 #ifdef UNRELIABLE_TEXTCTRL_BORDER
356 //
357 // If starting/stopping to show an image in front
358 // of a writable text-field, then re-create textctrl
359 // with different kind of border (because we can't
360 // assume that textctrl fully supports wxNO_BORDER).
361 //
362 wxTextCtrl* tc = GetTextCtrl();
363
364 if ( tc && (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) )
365 {
366 int borderType = tc->GetWindowStyle() & wxBORDER_MASK;
367 int tcCreateStyle = -1;
368
369 if ( width > 0 )
370 {
371 // Re-create textctrl with no border
372 if ( borderType != wxNO_BORDER )
373 {
374 m_widthCustomBorder = 1;
375 tcCreateStyle = wxNO_BORDER;
376 }
377 }
378 else if ( width == 0 )
379 {
380 // Re-create textctrl with normal border
381 if ( borderType == wxNO_BORDER )
382 {
383 m_widthCustomBorder = 0;
384 tcCreateStyle = 0;
385 }
386 }
387
388 // Common textctrl re-creation code
389 if ( tcCreateStyle != -1 )
390 {
391 tc->RemoveEventHandler(m_textEvtHandler);
392 delete m_textEvtHandler;
393
394 wxValidator* pValidator = tc->GetValidator();
395 if ( pValidator )
396 {
397 pValidator = (wxValidator*) pValidator->Clone();
398 CreateTextCtrl( tcCreateStyle, *pValidator );
399 delete pValidator;
400 }
401 else
402 {
403 CreateTextCtrl( tcCreateStyle, wxDefaultValidator );
404 }
405
406 InstallInputHandlers();
407 }
408 }
409 #endif // UNRELIABLE_TEXTCTRL_BORDER
410
411 wxComboCtrlBase::SetCustomPaintWidth( width );
412 }
413
414 bool wxGenericComboCtrl::IsKeyPopupToggle(const wxKeyEvent& event) const
415 {
416 int keycode = event.GetKeyCode();
417 bool isPopupShown = IsPopupShown();
418
419 // This code is AFAIK appropriate for wxGTK.
420
421 if ( isPopupShown )
422 {
423 if ( keycode == WXK_ESCAPE ||
424 ( keycode == WXK_UP && event.AltDown() ) )
425 return true;
426 }
427 else
428 {
429 if ( keycode == WXK_DOWN && event.AltDown() )
430 return true;
431 }
432
433 return false;
434 }
435
436 #ifdef __WXUNIVERSAL__
437
438 bool wxGenericComboCtrl::PerformAction(const wxControlAction& action,
439 long numArg,
440 const wxString& strArg)
441 {
442 bool processed = false;
443 if ( action == wxACTION_COMBOBOX_POPUP )
444 {
445 if ( !IsPopupShown() )
446 {
447 ShowPopup();
448
449 processed = true;
450 }
451 }
452 else if ( action == wxACTION_COMBOBOX_DISMISS )
453 {
454 if ( IsPopupShown() )
455 {
456 HidePopup();
457
458 processed = true;
459 }
460 }
461
462 if ( !processed )
463 {
464 // pass along
465 return wxControl::PerformAction(action, numArg, strArg);
466 }
467
468 return true;
469 }
470
471 #endif // __WXUNIVERSAL__
472
473 // If native wxComboCtrl was not defined, then prepare a simple
474 // front-end so that wxRTTI works as expected.
475 #ifndef _WX_COMBOCONTROL_H_
476 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl, wxGenericComboCtrl)
477 #endif
478
479 #endif // !wxCOMBOCONTROL_FULLY_FEATURED
480
481 #endif // wxUSE_COMBOCTRL