]> git.saurik.com Git - wxWidgets.git/blob - src/msw/combo.cpp
Use SetSizeHints() for wxTLW.
[wxWidgets.git] / src / msw / combo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/combo.cpp
3 // Purpose: wxMSW 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 #ifndef WX_PRECOMP
29 #include "wx/log.h"
30 #include "wx/combobox.h"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33 #include "wx/dialog.h"
34 #include "wx/stopwatch.h"
35 #endif
36
37 #include "wx/dcbuffer.h"
38 #include "wx/combo.h"
39
40 #include "wx/msw/registry.h"
41 #include "wx/msw/uxtheme.h"
42
43 // Change to #if 1 to include tmschema.h for easier testing of theme
44 // parameters.
45 #if 0
46 #include <tmschema.h>
47 #else
48 //----------------------------------
49 #define EP_EDITTEXT 1
50 #define ETS_NORMAL 1
51 #define ETS_HOT 2
52 #define ETS_SELECTED 3
53 #define ETS_DISABLED 4
54 #define ETS_FOCUSED 5
55 #define ETS_READONLY 6
56 #define ETS_ASSIST 7
57 #define TMT_FILLCOLOR 3802
58 #define TMT_TEXTCOLOR 3803
59 #define TMT_BORDERCOLOR 3801
60 #define TMT_EDGEFILLCOLOR 3808
61 //----------------------------------
62 #endif
63
64
65 #define NATIVE_TEXT_INDENT_XP 4
66 #define NATIVE_TEXT_INDENT_CLASSIC 2
67
68 #define TEXTCTRLXADJUST_XP 1
69 #define TEXTCTRLYADJUST_XP 3
70 #define TEXTCTRLXADJUST_CLASSIC 1
71 #define TEXTCTRLYADJUST_CLASSIC 2
72
73 #define COMBOBOX_ANIMATION_RESOLUTION 10
74
75 #define COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
76
77 #define wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM (1<<26)
78
79
80 // ============================================================================
81 // implementation
82 // ============================================================================
83
84
85 BEGIN_EVENT_TABLE(wxComboCtrl, wxComboCtrlBase)
86 EVT_PAINT(wxComboCtrl::OnPaintEvent)
87 EVT_MOUSE_EVENTS(wxComboCtrl::OnMouseEvent)
88 EVT_TIMER(wxID_ANY, wxComboCtrl::OnTimerEvent)
89 END_EVENT_TABLE()
90
91
92 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl, wxComboCtrlBase)
93
94 void wxComboCtrl::Init()
95 {
96 }
97
98 bool wxComboCtrl::Create(wxWindow *parent,
99 wxWindowID id,
100 const wxString& value,
101 const wxPoint& pos,
102 const wxSize& size,
103 long style,
104 const wxValidator& validator,
105 const wxString& name)
106 {
107
108 // Set border
109 long border = style & wxBORDER_MASK;
110
111 wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
112
113 if ( !border )
114 {
115 // For XP, have 1-width custom border, for older version use sunken
116 if ( theme )
117 {
118 border = wxBORDER_NONE;
119 m_widthCustomBorder = 1;
120 }
121 else
122 border = wxBORDER_SUNKEN;
123
124 style = (style & ~(wxBORDER_MASK)) | border;
125 }
126
127 // create main window
128 if ( !wxComboCtrlBase::Create(parent,
129 id,
130 value,
131 pos,
132 size,
133 style | wxFULL_REPAINT_ON_RESIZE,
134 wxDefaultValidator,
135 name) )
136 return false;
137
138 if ( style & wxCC_STD_BUTTON )
139 m_iFlags |= wxCC_POPUP_ON_MOUSE_UP;
140
141 // Create textctrl, if necessary
142 CreateTextCtrl( wxNO_BORDER, validator );
143
144 // Add keyboard input handlers for main control and textctrl
145 InstallInputHandlers();
146
147 // Prepare background for double-buffering
148 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
149
150 // SetBestSize should be called last
151 SetBestSize(size);
152
153 return true;
154 }
155
156 wxComboCtrl::~wxComboCtrl()
157 {
158 }
159
160 void wxComboCtrl::OnThemeChange()
161 {
162 wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
163 if ( theme )
164 {
165 wxUxThemeHandle hTheme(this, L"COMBOBOX");
166
167 COLORREF col;
168 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_FILLCOLOR,&col);
169 SetBackgroundColour(wxRGBToColour(col));
170 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&col);
171 SetForegroundColour(wxRGBToColour(col));
172 }
173 else
174 {
175 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
176 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
177 }
178 }
179
180 void wxComboCtrl::OnResize()
181 {
182 //
183 // Recalculates button and textctrl areas
184
185 int textCtrlXAdjust;
186 int textCtrlYAdjust;
187
188 if ( wxUxThemeEngine::GetIfActive() )
189 {
190 textCtrlXAdjust = TEXTCTRLXADJUST_XP;
191 textCtrlYAdjust = TEXTCTRLYADJUST_XP;
192 }
193 else
194 {
195 textCtrlXAdjust = TEXTCTRLXADJUST_CLASSIC;
196 textCtrlYAdjust = TEXTCTRLYADJUST_CLASSIC;
197 }
198
199 // Technically Classic Windows style combo has more narrow button,
200 // but the native renderer doesn't paint it well like that.
201 int btnWidth = 17;
202 CalculateAreas(btnWidth);
203
204 // Position textctrl using standard routine
205 PositionTextCtrl(textCtrlXAdjust,textCtrlYAdjust);
206 }
207
208 // Draws non-XP GUI dotted line around the focus area
209 static void wxMSWDrawFocusRect( wxDC& dc, const wxRect& rect )
210 {
211 #if !defined(__WXWINCE__)
212 /*
213 RECT mswRect;
214 mswRect.left = rect.x;
215 mswRect.top = rect.y;
216 mswRect.right = rect.x + rect.width;
217 mswRect.bottom = rect.y + rect.height;
218 HDC hdc = (HDC) dc.GetHDC();
219 SetMapMode(hdc,MM_TEXT); // Just in case...
220 DrawFocusRect(hdc,&mswRect);
221 */
222 // FIXME: Use DrawFocusRect code above (currently it draws solid line
223 // for caption focus but works ok for other stuff).
224 // Also, this code below may not work in future wx versions, since
225 // it employs wxCAP_BUTT hack to have line of width 1.
226 dc.SetLogicalFunction(wxINVERT);
227
228 wxPen pen(*wxBLACK,1,wxDOT);
229 pen.SetCap(wxCAP_BUTT);
230 dc.SetPen(pen);
231 dc.SetBrush(*wxTRANSPARENT_BRUSH);
232
233 dc.DrawRectangle(rect);
234
235 dc.SetLogicalFunction(wxCOPY);
236 #else
237 dc.SetLogicalFunction(wxINVERT);
238
239 dc.SetPen(wxPen(*wxBLACK,1,wxDOT));
240 dc.SetBrush(*wxTRANSPARENT_BRUSH);
241
242 dc.DrawRectangle(rect);
243
244 dc.SetLogicalFunction(wxCOPY);
245 #endif
246 }
247
248 // draw focus background on area in a way typical on platform
249 void wxComboCtrl::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const
250 {
251 wxUxThemeEngine* theme = (wxUxThemeEngine*) NULL;
252
253 // Constructor only calls GetHWND() const, so it should be safe
254 // to cast "this" to const.
255 wxUxThemeHandle hTheme(this, L"COMBOBOX");
256 //COLORREF cref;
257
258 wxSize sz = GetClientSize();
259 bool isEnabled;
260 bool isFocused; // also selected
261
262 // For smaller size control (and for disabled background) use less spacing
263 int focusSpacingX;
264 int focusSpacingY;
265
266 if ( !(flags & wxCONTROL_ISSUBMENU) )
267 {
268 // Drawing control
269 isEnabled = IsEnabled();
270 isFocused = ShouldDrawFocus();
271
272 // Windows-style: for smaller size control (and for disabled background) use less spacing
273 if ( hTheme )
274 {
275 // WinXP Theme
276 focusSpacingX = isEnabled ? 2 : 1;
277 focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1;
278 }
279 else
280 {
281 // Classic Theme
282 if ( isEnabled )
283 {
284 focusSpacingX = 1;
285 focusSpacingY = 1;
286 }
287 else
288 {
289 focusSpacingX = 0;
290 focusSpacingY = 0;
291 }
292 }
293 }
294 else
295 {
296 // Drawing a list item
297 isEnabled = true; // they are never disabled
298 isFocused = flags & wxCONTROL_SELECTED ? true : false;
299
300 focusSpacingX = 0;
301 focusSpacingY = 0;
302 }
303
304 // Set the background sub-rectangle for selection, disabled etc
305 wxRect selRect(rect);
306 selRect.y += focusSpacingY;
307 selRect.height -= (focusSpacingY*2);
308
309 int wcp = 0;
310
311 if ( !(flags & wxCONTROL_ISSUBMENU) )
312 wcp += m_widthCustomPaint;
313
314 selRect.x += wcp + focusSpacingX;
315 selRect.width -= wcp + (focusSpacingX*2);
316
317 if ( hTheme )
318 theme = wxUxThemeEngine::GetIfActive();
319
320 wxColour bgCol;
321 bool drawDottedEdge = false;
322
323 if ( isEnabled )
324 {
325 // If popup is hidden and this control is focused,
326 // then draw the focus-indicator (selbgcolor background etc.).
327 if ( isFocused )
328 {
329 #if 0
330 // TODO: Proper theme color getting (JMS: I don't know which parts/colors to use,
331 // those below don't work)
332 if ( hTheme )
333 {
334 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_SELECTED,TMT_TEXTCOLOR,&cref);
335 dc.SetTextForeground( wxRGBToColour(cref) );
336 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_SELECTED,TMT_FILLCOLOR,&cref);
337 bgCol = wxRGBToColour(cref);
338 }
339 else
340 #endif
341 {
342 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
343 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
344 if ( m_windowStyle & wxCB_READONLY )
345 drawDottedEdge = true;
346 }
347 }
348 else
349 {
350 /*if ( hTheme )
351 {
352 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
353 dc.SetTextForeground( wxRGBToColour(cref) );
354 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_FILLCOLOR,&cref);
355 bgCol = wxRGBToColour(cref);
356 }
357 else
358 {*/
359 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
360 bgCol = GetBackgroundColour();
361 //}
362 }
363 }
364 else
365 {
366 /*if ( hTheme )
367 {
368 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_DISABLED,TMT_TEXTCOLOR,&cref);
369 dc.SetTextForeground( wxRGBToColour(cref) );
370 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_DISABLED,TMT_EDGEFILLCOLOR,&cref);
371 bgCol = wxRGBToColour(cref);
372 }
373 else
374 {*/
375 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT) );
376 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
377 //}
378 }
379
380 dc.SetBrush(bgCol);
381 dc.SetPen(bgCol);
382 dc.DrawRectangle(selRect);
383 if ( drawDottedEdge )
384 wxMSWDrawFocusRect(dc,selRect);
385
386 // Don't clip exactly to the selection rectangle so we can draw
387 // to the non-selected area in front of it.
388 wxRect clipRect(rect.x,rect.y,
389 (selRect.x+selRect.width)-rect.x-1,rect.height);
390 dc.SetClippingRegion(clipRect);
391 }
392
393 void wxComboCtrl::OnPaintEvent( wxPaintEvent& WXUNUSED(event) )
394 {
395 // TODO: Convert drawing in this function to Windows API Code
396
397 wxSize sz = GetClientSize();
398 wxAutoBufferedPaintDC dc(this);
399
400 const wxRect& rectb = m_btnArea;
401 wxRect rect = m_tcArea;
402 bool isEnabled = IsEnabled();
403 wxColour bgCol = GetBackgroundColour();
404 wxColour fgCol;
405
406 wxUxThemeEngine* theme = NULL;
407 wxUxThemeHandle hTheme(this, L"COMBOBOX");
408 int etsState;
409
410 // area around both controls
411 wxRect rect2(0,0,sz.x,sz.y);
412 if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE )
413 {
414 rect2 = m_tcArea;
415 rect2.Inflate(1);
416 }
417
418 // Use theme to draw border on XP
419 if ( hTheme )
420 {
421 theme = wxUxThemeEngine::GetIfActive();
422 COLORREF cref;
423
424 // Select correct border colour
425 if ( !isEnabled )
426 etsState = ETS_DISABLED;
427 else
428 etsState = ETS_NORMAL;
429
430 if ( m_widthCustomBorder )
431 {
432 theme->GetThemeColor(hTheme,EP_EDITTEXT,etsState,TMT_BORDERCOLOR,&cref);
433
434 // Set border colour
435 dc.SetPen( wxRGBToColour(cref) );
436
437 dc.SetBrush( *wxTRANSPARENT_BRUSH );
438 dc.DrawRectangle(rect2);
439 }
440
441 theme->GetThemeColor(hTheme,EP_EDITTEXT,etsState,TMT_TEXTCOLOR,&cref);
442 fgCol = wxRGBToColour(cref);
443 }
444 else
445 {
446 // draw regular background
447 fgCol = GetForegroundColour();
448 }
449
450 rect2.Deflate(m_widthCustomBorder);
451
452 dc.SetBrush(bgCol);
453 dc.SetPen(bgCol);
454
455 // clear main background
456 dc.DrawRectangle(rect);
457
458 // Button background with theme?
459 bool drawButBg = true;
460 if ( hTheme && m_blankButtonBg )
461 {
462 RECT r;
463 wxCopyRectToRECT(rectb, r);
464
465 // Draw parent background if needed (since button looks like its out of
466 // the combo, this is preferred).
467 theme->DrawThemeParentBackground(GetHwndOf(this),
468 GetHdcOf(dc),
469 &r);
470
471 drawButBg = false;
472 }
473
474 // Standard button rendering
475 DrawButton(dc,rectb,drawButBg);
476
477 // paint required portion on the control
478 if ( (!m_text || m_widthCustomPaint) )
479 {
480 wxASSERT( m_widthCustomPaint >= 0 );
481
482 // this is intentionally here to allow drawed rectangle's
483 // right edge to be hidden
484 if ( m_text )
485 rect.width = m_widthCustomPaint;
486
487 dc.SetFont( GetFont() );
488
489 dc.SetClippingRegion(rect);
490 if ( m_popupInterface )
491 m_popupInterface->PaintComboControl(dc,rect);
492 else
493 wxComboPopup::DefaultPaintComboControl(this,dc,rect);
494 }
495 }
496
497 void wxComboCtrl::OnMouseEvent( wxMouseEvent& event )
498 {
499 int mx = event.m_x;
500 bool isOnButtonArea = m_btnArea.Contains(mx,event.m_y);
501 int handlerFlags = isOnButtonArea ? wxCC_MF_ON_BUTTON : 0;
502
503 if ( PreprocessMouseEvent(event,isOnButtonArea) )
504 return;
505
506 if ( (m_windowStyle & (wxCC_SPECIAL_DCLICK|wxCB_READONLY)) == wxCB_READONLY )
507 {
508 // if no textctrl and no special double-click, then the entire control acts
509 // as a button
510 handlerFlags |= wxCC_MF_ON_BUTTON;
511 if ( HandleButtonMouseEvent(event,handlerFlags) )
512 return;
513 }
514 else
515 {
516 if ( isOnButtonArea || HasCapture() ||
517 (m_widthCustomPaint && mx < (m_tcArea.x+m_widthCustomPaint)) )
518 {
519 handlerFlags |= wxCC_MF_ON_CLICK_AREA;
520
521 if ( HandleButtonMouseEvent(event,handlerFlags) )
522 return;
523 }
524 else if ( m_btnState )
525 {
526 // otherwise need to clear the hover status
527 m_btnState = 0;
528 RefreshRect(m_btnArea);
529 }
530 }
531
532 //
533 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
534 // See header file for further information on this method.
535 HandleNormalMouseEvent(event);
536
537 }
538
539 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
540 static wxUint32 GetUserPreferencesMask()
541 {
542 static wxUint32 userPreferencesMask = 0;
543 static bool valueSet = false;
544
545 if ( valueSet )
546 return userPreferencesMask;
547
548 wxRegKey key(wxRegKey::HKCU, wxT("Control Panel\\Desktop"));
549 if( key.Open(wxRegKey::Read) )
550 {
551 wxMemoryBuffer buf;
552 if ( key.QueryValue(wxT("UserPreferencesMask"), buf) )
553 {
554 if ( buf.GetDataLen() >= 4 )
555 {
556 wxByte* p = (wxByte*) buf.GetData();
557 userPreferencesMask = p[3] + (p[2]<<8) + (p[1]<<16) + (p[0]<<24);
558 }
559 }
560 }
561
562 valueSet = true;
563
564 return userPreferencesMask;
565 }
566 #endif
567
568 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
569 void wxComboCtrl::OnTimerEvent( wxTimerEvent& WXUNUSED(event) )
570 {
571 bool stopTimer = false;
572
573 wxWindow* popup = GetPopupControl()->GetControl();
574
575 // Popup was hidden before it was fully shown?
576 if ( IsPopupWindowState(Hidden) )
577 {
578 stopTimer = true;
579 }
580 else
581 {
582 wxLongLong t = ::wxGetLocalTimeMillis();
583 const wxRect& rect = m_animRect;
584 wxWindow* win = GetPopupWindow();
585
586 int pos = (int) (t-m_animStart).GetLo();
587 if ( pos < COMBOBOX_ANIMATION_DURATION )
588 {
589 int height = rect.height;
590 //int h0 = rect.height;
591 int h = (((pos*256)/COMBOBOX_ANIMATION_DURATION)*height)/256;
592 int y = (height - h);
593 if ( y < 0 )
594 y = 0;
595
596 if ( m_animFlags & ShowAbove )
597 {
598 win->SetSize( rect.x, rect.y + height - h, rect.width, h );
599 }
600 else
601 {
602 popup->Move( 0, -y );
603 win->SetSize( rect.x, rect.y, rect.width, h );
604 }
605 }
606 else
607 {
608 stopTimer = true;
609 }
610 }
611
612 if ( stopTimer )
613 {
614 popup->Move( 0, 0 );
615 m_animTimer.Stop();
616 DoShowPopup( m_animRect, m_animFlags );
617 }
618 }
619 #endif
620
621 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
622 bool wxComboCtrl::AnimateShow( const wxRect& rect, int flags )
623 {
624 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM )
625 {
626 m_animStart = ::wxGetLocalTimeMillis();
627 m_animRect = rect;
628 m_animFlags = flags;
629
630 wxWindow* win = GetPopupWindow();
631 win->SetSize( rect.x, rect.y, rect.width, 0 );
632 win->Show();
633
634 m_animTimer.SetOwner( this, wxID_ANY );
635 m_animTimer.Start( COMBOBOX_ANIMATION_RESOLUTION, wxTIMER_CONTINUOUS );
636
637 OnTimerEvent(*((wxTimerEvent*)NULL)); // Event is never used, so we can give NULL
638
639 return false;
640 }
641
642 return true;
643 }
644 #endif
645
646 wxCoord wxComboCtrl::GetNativeTextIndent() const
647 {
648 if ( wxUxThemeEngine::GetIfActive() )
649 return NATIVE_TEXT_INDENT_XP;
650 return NATIVE_TEXT_INDENT_CLASSIC;
651 }
652
653 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent& event) const
654 {
655 const bool isPopupShown = IsPopupShown();
656
657 switch ( event.GetKeyCode() )
658 {
659 case WXK_F4:
660 // F4 toggles the popup in the native comboboxes, so emulate them
661 if ( !event.AltDown() )
662 return true;
663 break;
664
665 case WXK_ESCAPE:
666 if ( isPopupShown )
667 return true;
668 break;
669
670 case WXK_DOWN:
671 case WXK_UP:
672 // On XP or with writable combo in Classic, arrows don't open the
673 // popup but Alt-arrow does
674 if ( event.AltDown() ||
675 ( !isPopupShown &&
676 HasFlag(wxCB_READONLY) &&
677 !wxUxThemeEngine::GetIfActive()
678 ) )
679 {
680 return true;
681 }
682 break;
683 }
684
685 return false;
686 }
687
688 #endif // wxUSE_COMBOCTRL