]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/combocmn.cpp | |
3 | // Purpose: wxComboCtrlBase | |
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/combobox.h" | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/log.h" | |
32 | #include "wx/dcclient.h" | |
33 | #include "wx/settings.h" | |
34 | #include "wx/dialog.h" | |
35 | #include "wx/timer.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/tooltip.h" | |
39 | ||
40 | #include "wx/combo.h" | |
41 | ||
42 | ||
43 | ||
44 | // constants | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | #define DEFAULT_DROPBUTTON_WIDTH 19 | |
48 | ||
49 | #define BMP_BUTTON_MARGIN 4 | |
50 | ||
51 | #define DEFAULT_POPUP_HEIGHT 400 | |
52 | ||
53 | #define DEFAULT_TEXT_INDENT 3 | |
54 | ||
55 | #define COMBO_MARGIN 2 // spacing right of wxTextCtrl | |
56 | ||
57 | ||
58 | #if defined(__WXMSW__) | |
59 | ||
60 | #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
61 | #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common | |
62 | // native controls work on it like normal. | |
63 | #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window. | |
64 | #define TEXTCTRL_TEXT_CENTERED 0 // 1 if text in textctrl is vertically centered | |
65 | ||
66 | //#undef wxUSE_POPUPWIN | |
67 | //#define wxUSE_POPUPWIN 0 | |
68 | ||
69 | #elif defined(__WXGTK__) | |
70 | ||
71 | // NB: It is not recommended to use wxDialog as popup on wxGTK, because of | |
72 | // this bug: If wxDialog is hidden, its position becomes corrupt | |
73 | // between hide and next show, but without internal coordinates being | |
74 | // reflected (or something like that - atleast commenting out ->Hide() | |
75 | // seemed to eliminate the position change). | |
76 | ||
77 | #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
78 | #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common | |
79 | // native controls work on it like normal. | |
80 | #define POPUPWIN_IS_PERFECT 1 // Same, but for non-transient popup window. | |
81 | #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered | |
82 | ||
83 | #elif defined(__WXMAC__) | |
84 | ||
85 | #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
86 | #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common | |
87 | // native controls work on it like normal. | |
88 | #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window. | |
89 | #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered | |
90 | ||
91 | #else | |
92 | ||
93 | #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
94 | #define TRANSIENT_POPUPWIN_IS_PERFECT 0 // wxPopupTransientWindow works, its child can have focus, and common | |
95 | // native controls work on it like normal. | |
96 | #define POPUPWIN_IS_PERFECT 0 // Same, but for non-transient popup window. | |
97 | #define TEXTCTRL_TEXT_CENTERED 1 // 1 if text in textctrl is vertically centered | |
98 | ||
99 | #undef DEFAULT_DROPBUTTON_WIDTH | |
100 | #define DEFAULT_DROPBUTTON_WIDTH 22 | |
101 | ||
102 | #endif | |
103 | ||
104 | ||
105 | // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless | |
106 | // what the wxUSE_POPUPWIN says. | |
107 | // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2) | |
108 | #if (!defined(__WXMSW__) && !defined(__WXGTK__)) || defined(__WXWINCE__) | |
109 | #undef wxUSE_POPUPWIN | |
110 | #define wxUSE_POPUPWIN 0 | |
111 | #endif | |
112 | ||
113 | ||
114 | #if wxUSE_POPUPWIN | |
115 | #include "wx/popupwin.h" | |
116 | #else | |
117 | #undef USE_TRANSIENT_POPUP | |
118 | #define USE_TRANSIENT_POPUP 0 | |
119 | #endif | |
120 | ||
121 | ||
122 | // Define different types of popup windows | |
123 | enum | |
124 | { | |
125 | POPUPWIN_NONE = 0, | |
126 | POPUPWIN_WXPOPUPTRANSIENTWINDOW = 1, | |
127 | POPUPWIN_WXPOPUPWINDOW = 2, | |
128 | POPUPWIN_WXDIALOG = 3 | |
129 | }; | |
130 | ||
131 | ||
132 | #if USE_TRANSIENT_POPUP | |
133 | // wxPopupTransientWindow is implemented | |
134 | ||
135 | #define wxComboPopupWindowBase wxPopupTransientWindow | |
136 | #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPTRANSIENTWINDOW | |
137 | #define USES_WXPOPUPTRANSIENTWINDOW 1 | |
138 | ||
139 | #if TRANSIENT_POPUPWIN_IS_PERFECT | |
140 | // | |
141 | #elif POPUPWIN_IS_PERFECT | |
142 | #define wxComboPopupWindowBase2 wxPopupWindow | |
143 | #define SECONDARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW | |
144 | #define USES_WXPOPUPWINDOW 1 | |
145 | #else | |
146 | #define wxComboPopupWindowBase2 wxDialog | |
147 | #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG | |
148 | #define USES_WXDIALOG 1 | |
149 | #endif | |
150 | ||
151 | #elif wxUSE_POPUPWIN | |
152 | // wxPopupWindow (but not wxPopupTransientWindow) is properly implemented | |
153 | ||
154 | #define wxComboPopupWindowBase wxPopupWindow | |
155 | #define PRIMARY_POPUP_TYPE POPUPWIN_WXPOPUPWINDOW | |
156 | #define USES_WXPOPUPWINDOW 1 | |
157 | ||
158 | #if !POPUPWIN_IS_PERFECT | |
159 | #define wxComboPopupWindowBase2 wxDialog | |
160 | #define SECONDARY_POPUP_TYPE POPUPWIN_WXDIALOG | |
161 | #define USES_WXDIALOG 1 | |
162 | #endif | |
163 | ||
164 | #else | |
165 | // wxPopupWindow is not implemented | |
166 | ||
167 | #define wxComboPopupWindowBase wxDialog | |
168 | #define PRIMARY_POPUP_TYPE POPUPWIN_WXDIALOG | |
169 | #define USES_WXDIALOG 1 | |
170 | ||
171 | #endif | |
172 | ||
173 | ||
174 | #ifndef USES_WXPOPUPTRANSIENTWINDOW | |
175 | #define USES_WXPOPUPTRANSIENTWINDOW 0 | |
176 | #endif | |
177 | ||
178 | #ifndef USES_WXPOPUPWINDOW | |
179 | #define USES_WXPOPUPWINDOW 0 | |
180 | #endif | |
181 | ||
182 | #ifndef USES_WXDIALOG | |
183 | #define USES_WXDIALOG 0 | |
184 | #endif | |
185 | ||
186 | ||
187 | #if USES_WXPOPUPWINDOW | |
188 | #define INSTALL_TOPLEV_HANDLER 1 | |
189 | #else | |
190 | #define INSTALL_TOPLEV_HANDLER 0 | |
191 | #endif | |
192 | ||
193 | ||
194 | // | |
195 | // ** TODO ** | |
196 | // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow) | |
197 | // | |
198 | ||
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // wxComboFrameEventHandler takes care of hiding the popup when events happen | |
202 | // in its top level parent. | |
203 | // ---------------------------------------------------------------------------- | |
204 | ||
205 | #if INSTALL_TOPLEV_HANDLER | |
206 | ||
207 | // | |
208 | // This will no longer be necessary after wxTransientPopupWindow | |
209 | // works well on all platforms. | |
210 | // | |
211 | ||
212 | class wxComboFrameEventHandler : public wxEvtHandler | |
213 | { | |
214 | public: | |
215 | wxComboFrameEventHandler( wxComboCtrlBase* pCb ); | |
216 | virtual ~wxComboFrameEventHandler(); | |
217 | ||
218 | void OnPopup(); | |
219 | ||
220 | void OnIdle( wxIdleEvent& event ); | |
221 | void OnMouseEvent( wxMouseEvent& event ); | |
222 | void OnActivate( wxActivateEvent& event ); | |
223 | void OnResize( wxSizeEvent& event ); | |
224 | void OnMove( wxMoveEvent& event ); | |
225 | void OnMenuEvent( wxMenuEvent& event ); | |
226 | void OnClose( wxCloseEvent& event ); | |
227 | ||
228 | protected: | |
229 | wxWindow* m_focusStart; | |
230 | wxComboCtrlBase* m_combo; | |
231 | ||
232 | private: | |
233 | DECLARE_EVENT_TABLE() | |
234 | }; | |
235 | ||
236 | BEGIN_EVENT_TABLE(wxComboFrameEventHandler, wxEvtHandler) | |
237 | EVT_IDLE(wxComboFrameEventHandler::OnIdle) | |
238 | EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent) | |
239 | EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent) | |
240 | EVT_SIZE(wxComboFrameEventHandler::OnResize) | |
241 | EVT_MOVE(wxComboFrameEventHandler::OnMove) | |
242 | EVT_MENU_HIGHLIGHT(wxID_ANY,wxComboFrameEventHandler::OnMenuEvent) | |
243 | EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent) | |
244 | EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate) | |
245 | EVT_CLOSE(wxComboFrameEventHandler::OnClose) | |
246 | END_EVENT_TABLE() | |
247 | ||
248 | wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase* combo ) | |
249 | : wxEvtHandler() | |
250 | { | |
251 | m_combo = combo; | |
252 | } | |
253 | ||
254 | wxComboFrameEventHandler::~wxComboFrameEventHandler() | |
255 | { | |
256 | } | |
257 | ||
258 | void wxComboFrameEventHandler::OnPopup() | |
259 | { | |
260 | m_focusStart = ::wxWindow::FindFocus(); | |
261 | } | |
262 | ||
263 | void wxComboFrameEventHandler::OnIdle( wxIdleEvent& event ) | |
264 | { | |
265 | wxWindow* winFocused = ::wxWindow::FindFocus(); | |
266 | ||
267 | wxWindow* popup = m_combo->GetPopupControl()->GetControl(); | |
268 | wxWindow* winpopup = m_combo->GetPopupWindow(); | |
269 | ||
270 | if ( | |
271 | winFocused != m_focusStart && | |
272 | winFocused != popup && | |
273 | winFocused->GetParent() != popup && | |
274 | winFocused != winpopup && | |
275 | winFocused->GetParent() != winpopup && | |
276 | winFocused != m_combo && | |
277 | winFocused != m_combo->GetButton() // GTK (atleast) requires this | |
278 | ) | |
279 | { | |
280 | m_combo->HidePopup(); | |
281 | } | |
282 | ||
283 | event.Skip(); | |
284 | } | |
285 | ||
286 | void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent& event ) | |
287 | { | |
288 | m_combo->HidePopup(); | |
289 | event.Skip(); | |
290 | } | |
291 | ||
292 | void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent& event ) | |
293 | { | |
294 | m_combo->HidePopup(); | |
295 | event.Skip(); | |
296 | } | |
297 | ||
298 | void wxComboFrameEventHandler::OnClose( wxCloseEvent& event ) | |
299 | { | |
300 | m_combo->HidePopup(); | |
301 | event.Skip(); | |
302 | } | |
303 | ||
304 | void wxComboFrameEventHandler::OnActivate( wxActivateEvent& event ) | |
305 | { | |
306 | m_combo->HidePopup(); | |
307 | event.Skip(); | |
308 | } | |
309 | ||
310 | void wxComboFrameEventHandler::OnResize( wxSizeEvent& event ) | |
311 | { | |
312 | m_combo->HidePopup(); | |
313 | event.Skip(); | |
314 | } | |
315 | ||
316 | void wxComboFrameEventHandler::OnMove( wxMoveEvent& event ) | |
317 | { | |
318 | m_combo->HidePopup(); | |
319 | event.Skip(); | |
320 | } | |
321 | ||
322 | #endif // INSTALL_TOPLEV_HANDLER | |
323 | ||
324 | // ---------------------------------------------------------------------------- | |
325 | // wxComboPopupWindow is, in essence, wxPopupWindow customized for | |
326 | // wxComboCtrl. | |
327 | // ---------------------------------------------------------------------------- | |
328 | ||
329 | class wxComboPopupWindow : public wxComboPopupWindowBase | |
330 | { | |
331 | public: | |
332 | ||
333 | wxComboPopupWindow( wxComboCtrlBase *parent, | |
334 | int style ) | |
335 | #if USES_WXPOPUPWINDOW || USES_WXPOPUPTRANSIENTWINDOW | |
336 | : wxComboPopupWindowBase(parent,style) | |
337 | #else | |
338 | : wxComboPopupWindowBase(parent, | |
339 | wxID_ANY, | |
340 | wxEmptyString, | |
341 | wxPoint(-21,-21), | |
342 | wxSize(20,20), | |
343 | style) | |
344 | #endif | |
345 | { | |
346 | m_inShow = 0; | |
347 | } | |
348 | ||
349 | #if USES_WXPOPUPTRANSIENTWINDOW | |
350 | virtual bool Show( bool show ); | |
351 | virtual bool ProcessLeftDown(wxMouseEvent& event); | |
352 | virtual void OnDismiss(); | |
353 | #endif | |
354 | ||
355 | private: | |
356 | wxByte m_inShow; | |
357 | }; | |
358 | ||
359 | ||
360 | #if USES_WXPOPUPTRANSIENTWINDOW | |
361 | bool wxComboPopupWindow::Show( bool show ) | |
362 | { | |
363 | // Guard against recursion | |
364 | if ( m_inShow ) | |
365 | return wxComboPopupWindowBase::Show(show); | |
366 | ||
367 | m_inShow++; | |
368 | ||
369 | wxASSERT( IsKindOf(CLASSINFO(wxPopupTransientWindow)) ); | |
370 | ||
371 | wxPopupTransientWindow* ptw = (wxPopupTransientWindow*) this; | |
372 | wxComboCtrlBase* combo = (wxComboCtrlBase*) GetParent(); | |
373 | ||
374 | if ( show != ptw->IsShown() ) | |
375 | { | |
376 | if ( show ) | |
377 | ptw->Popup(combo->GetPopupControl()->GetControl()); | |
378 | else | |
379 | ptw->Dismiss(); | |
380 | } | |
381 | ||
382 | m_inShow--; | |
383 | ||
384 | return true; | |
385 | } | |
386 | ||
387 | bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent& event) | |
388 | { | |
389 | return wxPopupTransientWindow::ProcessLeftDown(event); | |
390 | } | |
391 | ||
392 | // First thing that happens when a transient popup closes is that this method gets called. | |
393 | void wxComboPopupWindow::OnDismiss() | |
394 | { | |
395 | wxComboCtrlBase* combo = (wxComboCtrlBase*) GetParent(); | |
396 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxComboCtrlBase)), | |
397 | wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") ); | |
398 | ||
399 | combo->OnPopupDismiss(); | |
400 | } | |
401 | #endif // USES_WXPOPUPTRANSIENTWINDOW | |
402 | ||
403 | ||
404 | // ---------------------------------------------------------------------------- | |
405 | // wxComboPopupWindowEvtHandler does bulk of the custom event handling | |
406 | // of a popup window. It is separate so we can have different types | |
407 | // of popup windows. | |
408 | // ---------------------------------------------------------------------------- | |
409 | ||
410 | class wxComboPopupWindowEvtHandler : public wxEvtHandler | |
411 | { | |
412 | public: | |
413 | ||
414 | wxComboPopupWindowEvtHandler( wxComboCtrlBase *parent ) | |
415 | { | |
416 | m_combo = parent; | |
417 | } | |
418 | ||
419 | void OnSizeEvent( wxSizeEvent& event ); | |
420 | void OnKeyEvent(wxKeyEvent& event); | |
421 | #if USES_WXDIALOG | |
422 | void OnActivate( wxActivateEvent& event ); | |
423 | #endif | |
424 | ||
425 | private: | |
426 | wxComboCtrlBase* m_combo; | |
427 | ||
428 | DECLARE_EVENT_TABLE() | |
429 | }; | |
430 | ||
431 | ||
432 | BEGIN_EVENT_TABLE(wxComboPopupWindowEvtHandler, wxEvtHandler) | |
433 | EVT_KEY_DOWN(wxComboPopupWindowEvtHandler::OnKeyEvent) | |
434 | EVT_KEY_UP(wxComboPopupWindowEvtHandler::OnKeyEvent) | |
435 | #if USES_WXDIALOG | |
436 | EVT_ACTIVATE(wxComboPopupWindowEvtHandler::OnActivate) | |
437 | #endif | |
438 | EVT_SIZE(wxComboPopupWindowEvtHandler::OnSizeEvent) | |
439 | END_EVENT_TABLE() | |
440 | ||
441 | ||
442 | void wxComboPopupWindowEvtHandler::OnSizeEvent( wxSizeEvent& WXUNUSED(event) ) | |
443 | { | |
444 | // Block the event so that the popup control does not get auto-resized. | |
445 | } | |
446 | ||
447 | void wxComboPopupWindowEvtHandler::OnKeyEvent( wxKeyEvent& event ) | |
448 | { | |
449 | // Relay keyboard event to the main child controls | |
450 | wxWindowList children = m_combo->GetPopupWindow()->GetChildren(); | |
451 | wxWindowList::iterator node = children.begin(); | |
452 | wxWindow* child = (wxWindow*)*node; | |
453 | child->AddPendingEvent(event); | |
454 | } | |
455 | ||
456 | #if USES_WXDIALOG | |
457 | void wxComboPopupWindowEvtHandler::OnActivate( wxActivateEvent& event ) | |
458 | { | |
459 | if ( !event.GetActive() ) | |
460 | { | |
461 | // Tell combo control that we are dismissed. | |
462 | m_combo->HidePopup(); | |
463 | ||
464 | event.Skip(); | |
465 | } | |
466 | } | |
467 | #endif | |
468 | ||
469 | ||
470 | // ---------------------------------------------------------------------------- | |
471 | // wxComboPopup | |
472 | // | |
473 | // ---------------------------------------------------------------------------- | |
474 | ||
475 | wxComboPopup::~wxComboPopup() | |
476 | { | |
477 | } | |
478 | ||
479 | void wxComboPopup::OnPopup() | |
480 | { | |
481 | } | |
482 | ||
483 | void wxComboPopup::OnDismiss() | |
484 | { | |
485 | } | |
486 | ||
487 | wxSize wxComboPopup::GetAdjustedSize( int minWidth, | |
488 | int prefHeight, | |
489 | int WXUNUSED(maxHeight) ) | |
490 | { | |
491 | return wxSize(minWidth,prefHeight); | |
492 | } | |
493 | ||
494 | void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase* combo, | |
495 | wxDC& dc, const wxRect& rect ) | |
496 | { | |
497 | if ( combo->GetWindowStyle() & wxCB_READONLY ) // ie. no textctrl | |
498 | { | |
499 | combo->PrepareBackground(dc,rect,0); | |
500 | ||
501 | dc.DrawText( combo->GetValue(), | |
502 | rect.x + combo->GetTextIndent(), | |
503 | (rect.height-dc.GetCharHeight())/2 + rect.y ); | |
504 | } | |
505 | } | |
506 | ||
507 | void wxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect ) | |
508 | { | |
509 | DefaultPaintComboControl(m_combo,dc,rect); | |
510 | } | |
511 | ||
512 | void wxComboPopup::OnComboKeyEvent( wxKeyEvent& event ) | |
513 | { | |
514 | event.Skip(); | |
515 | } | |
516 | ||
517 | void wxComboPopup::OnComboDoubleClick() | |
518 | { | |
519 | } | |
520 | ||
521 | void wxComboPopup::SetStringValue( const wxString& WXUNUSED(value) ) | |
522 | { | |
523 | } | |
524 | ||
525 | bool wxComboPopup::LazyCreate() | |
526 | { | |
527 | return false; | |
528 | } | |
529 | ||
530 | void wxComboPopup::Dismiss() | |
531 | { | |
532 | m_combo->HidePopup(); | |
533 | } | |
534 | ||
535 | // ---------------------------------------------------------------------------- | |
536 | // input handling | |
537 | // ---------------------------------------------------------------------------- | |
538 | ||
539 | // | |
540 | // This is pushed to the event handler queue of the child textctrl. | |
541 | // | |
542 | class wxComboBoxExtraInputHandler : public wxEvtHandler | |
543 | { | |
544 | public: | |
545 | ||
546 | wxComboBoxExtraInputHandler( wxComboCtrlBase* combo ) | |
547 | : wxEvtHandler() | |
548 | { | |
549 | m_combo = combo; | |
550 | } | |
551 | virtual ~wxComboBoxExtraInputHandler() { } | |
552 | void OnKey(wxKeyEvent& event); | |
553 | void OnFocus(wxFocusEvent& event); | |
554 | ||
555 | protected: | |
556 | wxComboCtrlBase* m_combo; | |
557 | ||
558 | private: | |
559 | DECLARE_EVENT_TABLE() | |
560 | }; | |
561 | ||
562 | ||
563 | BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler, wxEvtHandler) | |
564 | EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey) | |
565 | EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus) | |
566 | END_EVENT_TABLE() | |
567 | ||
568 | ||
569 | void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent& event) | |
570 | { | |
571 | // Let the wxComboCtrl event handler have a go first. | |
572 | wxComboCtrlBase* combo = m_combo; | |
573 | wxObject* prevObj = event.GetEventObject(); | |
574 | ||
575 | event.SetId(combo->GetId()); | |
576 | event.SetEventObject(combo); | |
577 | combo->GetEventHandler()->ProcessEvent(event); | |
578 | ||
579 | event.SetId(((wxWindow*)prevObj)->GetId()); | |
580 | event.SetEventObject(prevObj); | |
581 | } | |
582 | ||
583 | void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent& event) | |
584 | { | |
585 | // FIXME: This code does run when control is clicked, | |
586 | // yet on Windows it doesn't select all the text. | |
587 | if ( !(m_combo->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT) ) | |
588 | { | |
589 | if ( m_combo->GetTextCtrl() ) | |
590 | m_combo->GetTextCtrl()->SelectAll(); | |
591 | else | |
592 | m_combo->SetSelection(-1,-1); | |
593 | } | |
594 | ||
595 | // Send focus indication to parent. | |
596 | // NB: This is needed for cases where the textctrl gets focus | |
597 | // instead of its parent. While this may trigger multiple | |
598 | // wxEVT_SET_FOCUSes (since m_text->SetFocus is called | |
599 | // from combo's focus event handler), they should be quite | |
600 | // harmless. | |
601 | wxFocusEvent evt2(wxEVT_SET_FOCUS,m_combo->GetId()); | |
602 | evt2.SetEventObject(m_combo); | |
603 | m_combo->GetEventHandler()->ProcessEvent(evt2); | |
604 | ||
605 | event.Skip(); | |
606 | } | |
607 | ||
608 | ||
609 | // | |
610 | // This is pushed to the event handler queue of the control in popup. | |
611 | // | |
612 | ||
613 | class wxComboPopupExtraEventHandler : public wxEvtHandler | |
614 | { | |
615 | public: | |
616 | ||
617 | wxComboPopupExtraEventHandler( wxComboCtrlBase* combo ) | |
618 | : wxEvtHandler() | |
619 | { | |
620 | m_combo = combo; | |
621 | m_beenInside = false; | |
622 | } | |
623 | virtual ~wxComboPopupExtraEventHandler() { } | |
624 | ||
625 | void OnMouseEvent( wxMouseEvent& event ); | |
626 | ||
627 | // Called from wxComboCtrlBase::OnPopupDismiss | |
628 | void OnPopupDismiss() | |
629 | { | |
630 | m_beenInside = false; | |
631 | } | |
632 | ||
633 | protected: | |
634 | wxComboCtrlBase* m_combo; | |
635 | ||
636 | bool m_beenInside; | |
637 | ||
638 | private: | |
639 | DECLARE_EVENT_TABLE() | |
640 | }; | |
641 | ||
642 | ||
643 | BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler, wxEvtHandler) | |
644 | EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent) | |
645 | END_EVENT_TABLE() | |
646 | ||
647 | ||
648 | void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent& event ) | |
649 | { | |
650 | wxPoint pt = event.GetPosition(); | |
651 | wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize(); | |
652 | int evtType = event.GetEventType(); | |
653 | bool isInside = pt.x >= 0 && pt.y >= 0 && pt.x < sz.x && pt.y < sz.y; | |
654 | ||
655 | if ( evtType == wxEVT_MOTION || | |
656 | evtType == wxEVT_LEFT_DOWN || | |
657 | evtType == wxEVT_RIGHT_DOWN ) | |
658 | { | |
659 | // Block motion and click events outside the popup | |
660 | if ( !isInside || !m_combo->IsPopupShown() ) | |
661 | { | |
662 | event.Skip(false); | |
663 | return; | |
664 | } | |
665 | } | |
666 | else if ( evtType == wxEVT_LEFT_UP ) | |
667 | { | |
668 | if ( !m_combo->IsPopupShown() ) | |
669 | { | |
670 | event.Skip(false); | |
671 | return; | |
672 | } | |
673 | ||
674 | if ( !m_beenInside ) | |
675 | { | |
676 | if ( isInside ) | |
677 | { | |
678 | m_beenInside = true; | |
679 | } | |
680 | else | |
681 | { | |
682 | // | |
683 | // Some mouse events to popup that happen outside it, before cursor | |
684 | // has been inside the popu, need to be ignored by it but relayed to | |
685 | // the dropbutton. | |
686 | // | |
687 | wxWindow* btn = m_combo->GetButton(); | |
688 | if ( btn ) | |
689 | btn->GetEventHandler()->AddPendingEvent(event); | |
690 | else | |
691 | m_combo->GetEventHandler()->AddPendingEvent(event); | |
692 | ||
693 | return; | |
694 | } | |
695 | ||
696 | event.Skip(); | |
697 | } | |
698 | } | |
699 | ||
700 | event.Skip(); | |
701 | } | |
702 | ||
703 | // ---------------------------------------------------------------------------- | |
704 | // wxComboCtrlBase | |
705 | // ---------------------------------------------------------------------------- | |
706 | ||
707 | ||
708 | BEGIN_EVENT_TABLE(wxComboCtrlBase, wxControl) | |
709 | EVT_TEXT(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent) | |
710 | EVT_SIZE(wxComboCtrlBase::OnSizeEvent) | |
711 | EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent) | |
712 | EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent) | |
713 | //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent) | |
714 | EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent) | |
715 | EVT_TEXT_ENTER(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent) | |
716 | EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged) | |
717 | END_EVENT_TABLE() | |
718 | ||
719 | ||
720 | IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase, wxControl) | |
721 | ||
722 | void wxComboCtrlBase::Init() | |
723 | { | |
724 | m_winPopup = (wxWindow *)NULL; | |
725 | m_popup = (wxWindow *)NULL; | |
726 | m_popupWinState = Hidden; | |
727 | m_btn = (wxWindow*) NULL; | |
728 | m_text = (wxTextCtrl*) NULL; | |
729 | m_popupInterface = (wxComboPopup*) NULL; | |
730 | ||
731 | m_popupExtraHandler = (wxEvtHandler*) NULL; | |
732 | m_textEvtHandler = (wxEvtHandler*) NULL; | |
733 | ||
734 | #if INSTALL_TOPLEV_HANDLER | |
735 | m_toplevEvtHandler = (wxEvtHandler*) NULL; | |
736 | #endif | |
737 | ||
738 | m_mainCtrlWnd = this; | |
739 | ||
740 | m_heightPopup = -1; | |
741 | m_widthMinPopup = -1; | |
742 | m_anchorSide = 0; | |
743 | m_widthCustomPaint = 0; | |
744 | m_widthCustomBorder = 0; | |
745 | ||
746 | m_btnState = 0; | |
747 | m_btnWidDefault = 0; | |
748 | m_blankButtonBg = false; | |
749 | m_ignoreEvtText = 0; | |
750 | m_popupWinType = POPUPWIN_NONE; | |
751 | m_btnWid = m_btnHei = -1; | |
752 | m_btnSide = wxRIGHT; | |
753 | m_btnSpacingX = 0; | |
754 | ||
755 | m_extLeft = 0; | |
756 | m_extRight = 0; | |
757 | m_absIndent = -1; | |
758 | m_iFlags = 0; | |
759 | m_timeCanAcceptClick = 0; | |
760 | } | |
761 | ||
762 | bool wxComboCtrlBase::Create(wxWindow *parent, | |
763 | wxWindowID id, | |
764 | const wxString& value, | |
765 | const wxPoint& pos, | |
766 | const wxSize& size, | |
767 | long style, | |
768 | const wxValidator& validator, | |
769 | const wxString& name) | |
770 | { | |
771 | if ( !wxControl::Create(parent, | |
772 | id, | |
773 | pos, | |
774 | size, | |
775 | style | wxWANTS_CHARS, | |
776 | validator, | |
777 | name) ) | |
778 | return false; | |
779 | ||
780 | m_valueString = value; | |
781 | ||
782 | // Get colours | |
783 | OnThemeChange(); | |
784 | m_absIndent = GetNativeTextIndent(); | |
785 | ||
786 | m_iFlags |= wxCC_IFLAG_CREATED; | |
787 | ||
788 | // If x and y indicate valid size, wxSizeEvent won't be | |
789 | // emitted automatically, so we need to add artifical one. | |
790 | if ( size.x > 0 && size.y > 0 ) | |
791 | { | |
792 | wxSizeEvent evt(size,GetId()); | |
793 | GetEventHandler()->AddPendingEvent(evt); | |
794 | } | |
795 | ||
796 | return true; | |
797 | } | |
798 | ||
799 | void wxComboCtrlBase::InstallInputHandlers() | |
800 | { | |
801 | if ( m_text ) | |
802 | { | |
803 | m_textEvtHandler = new wxComboBoxExtraInputHandler(this); | |
804 | m_text->PushEventHandler(m_textEvtHandler); | |
805 | } | |
806 | } | |
807 | ||
808 | void | |
809 | wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator) | |
810 | { | |
811 | if ( !(m_windowStyle & wxCB_READONLY) ) | |
812 | { | |
813 | if ( m_text ) | |
814 | m_text->Destroy(); | |
815 | ||
816 | // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is | |
817 | // not used by the wxPropertyGrid and therefore the tab is processed by | |
818 | // looking at ancestors to see if they have wxTAB_TRAVERSAL. The | |
819 | // navigation event is then sent to the wrong window. | |
820 | style |= wxTE_PROCESS_TAB; | |
821 | ||
822 | if ( HasFlag(wxTE_PROCESS_ENTER) ) | |
823 | style |= wxTE_PROCESS_ENTER; | |
824 | ||
825 | // Ignore EVT_TEXT generated by the constructor (but only | |
826 | // if the event redirector already exists) | |
827 | // NB: This must be " = 1" instead of "++"; | |
828 | if ( m_textEvtHandler ) | |
829 | m_ignoreEvtText = 1; | |
830 | else | |
831 | m_ignoreEvtText = 0; | |
832 | ||
833 | m_text = new wxTextCtrl(this, wxID_ANY, m_valueString, | |
834 | wxDefaultPosition, wxSize(10,-1), | |
835 | style, validator); | |
836 | } | |
837 | } | |
838 | ||
839 | void wxComboCtrlBase::OnThemeChange() | |
840 | { | |
841 | SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
842 | } | |
843 | ||
844 | wxComboCtrlBase::~wxComboCtrlBase() | |
845 | { | |
846 | if ( HasCapture() ) | |
847 | ReleaseMouse(); | |
848 | ||
849 | #if INSTALL_TOPLEV_HANDLER | |
850 | delete ((wxComboFrameEventHandler*)m_toplevEvtHandler); | |
851 | m_toplevEvtHandler = (wxEvtHandler*) NULL; | |
852 | #endif | |
853 | ||
854 | DestroyPopup(); | |
855 | ||
856 | if ( m_text ) | |
857 | m_text->RemoveEventHandler(m_textEvtHandler); | |
858 | ||
859 | delete m_textEvtHandler; | |
860 | } | |
861 | ||
862 | ||
863 | // ---------------------------------------------------------------------------- | |
864 | // geometry stuff | |
865 | // ---------------------------------------------------------------------------- | |
866 | ||
867 | // Recalculates button and textctrl areas | |
868 | void wxComboCtrlBase::CalculateAreas( int btnWidth ) | |
869 | { | |
870 | wxSize sz = GetClientSize(); | |
871 | int customBorder = m_widthCustomBorder; | |
872 | int btnBorder; // border for button only | |
873 | ||
874 | // check if button should really be outside the border: we'll do it it if | |
875 | // its platform default or bitmap+pushbutton background is used, but not if | |
876 | // there is vertical size adjustment or horizontal spacing. | |
877 | if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) || | |
878 | (m_bmpNormal.Ok() && m_blankButtonBg) ) && | |
879 | m_btnSpacingX == 0 && | |
880 | m_btnHei <= 0 ) | |
881 | { | |
882 | m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE; | |
883 | btnBorder = 0; | |
884 | } | |
885 | else | |
886 | { | |
887 | m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE); | |
888 | btnBorder = customBorder; | |
889 | } | |
890 | ||
891 | // Defaul indentation | |
892 | if ( m_absIndent < 0 ) | |
893 | m_absIndent = GetNativeTextIndent(); | |
894 | ||
895 | int butWidth = btnWidth; | |
896 | ||
897 | if ( butWidth <= 0 ) | |
898 | butWidth = m_btnWidDefault; | |
899 | else | |
900 | m_btnWidDefault = butWidth; | |
901 | ||
902 | if ( butWidth <= 0 ) | |
903 | return; | |
904 | ||
905 | int butHeight = sz.y - btnBorder*2; | |
906 | ||
907 | // Adjust button width | |
908 | if ( m_btnWid > 0 ) | |
909 | butWidth = m_btnWid; | |
910 | else | |
911 | { | |
912 | // Adjust button width to match aspect ratio | |
913 | // (but only if control is smaller than best size). | |
914 | int bestHeight = GetBestSize().y; | |
915 | int height = GetSize().y; | |
916 | ||
917 | if ( height < bestHeight ) | |
918 | { | |
919 | // Make very small buttons square, as it makes | |
920 | // them accommodate arrow image better and still | |
921 | // looks decent. | |
922 | if ( height > 18 ) | |
923 | butWidth = (height*butWidth)/bestHeight; | |
924 | else | |
925 | butWidth = butHeight; | |
926 | } | |
927 | } | |
928 | ||
929 | // Adjust button height | |
930 | if ( m_btnHei > 0 ) | |
931 | butHeight = m_btnHei; | |
932 | ||
933 | // Use size of normal bitmap if... | |
934 | // It is larger | |
935 | // OR | |
936 | // button width is set to default and blank button bg is not drawn | |
937 | if ( m_bmpNormal.Ok() ) | |
938 | { | |
939 | int bmpReqWidth = m_bmpNormal.GetWidth(); | |
940 | int bmpReqHeight = m_bmpNormal.GetHeight(); | |
941 | ||
942 | // If drawing blank button background, we need to add some margin. | |
943 | if ( m_blankButtonBg ) | |
944 | { | |
945 | bmpReqWidth += BMP_BUTTON_MARGIN*2; | |
946 | bmpReqHeight += BMP_BUTTON_MARGIN*2; | |
947 | } | |
948 | ||
949 | if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) ) | |
950 | butWidth = bmpReqWidth; | |
951 | if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) ) | |
952 | butHeight = bmpReqHeight; | |
953 | ||
954 | // Need to fix height? | |
955 | if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 ) | |
956 | { | |
957 | int newY = butHeight+(customBorder*2); | |
958 | SetClientSize(wxDefaultCoord,newY); | |
959 | sz.y = newY; | |
960 | } | |
961 | } | |
962 | ||
963 | int butAreaWid = butWidth + (m_btnSpacingX*2); | |
964 | ||
965 | m_btnSize.x = butWidth; | |
966 | m_btnSize.y = butHeight; | |
967 | ||
968 | m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder ); | |
969 | m_btnArea.y = btnBorder; | |
970 | m_btnArea.width = butAreaWid; | |
971 | m_btnArea.height = sz.y - (btnBorder*2); | |
972 | ||
973 | m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder; | |
974 | m_tcArea.y = customBorder; | |
975 | m_tcArea.width = sz.x - butAreaWid - (customBorder*2); | |
976 | m_tcArea.height = sz.y - (customBorder*2); | |
977 | ||
978 | /* | |
979 | if ( m_text ) | |
980 | { | |
981 | ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) + | |
982 | wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height)); | |
983 | } | |
984 | */ | |
985 | } | |
986 | ||
987 | void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust ) | |
988 | { | |
989 | if ( !m_text ) | |
990 | return; | |
991 | ||
992 | wxSize sz = GetClientSize(); | |
993 | int customBorder = m_widthCustomBorder; | |
994 | ||
995 | #if !TEXTCTRL_TEXT_CENTERED | |
996 | if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER ) | |
997 | { | |
998 | // Centre textctrl | |
999 | int tcSizeY = m_text->GetBestSize().y; | |
1000 | int diff = sz.y - tcSizeY; | |
1001 | int y = textCtrlYAdjust + (diff/2); | |
1002 | ||
1003 | if ( y < customBorder ) | |
1004 | y = customBorder; | |
1005 | ||
1006 | m_text->SetSize( m_tcArea.x + m_widthCustomPaint + m_absIndent + textCtrlXAdjust, | |
1007 | y, | |
1008 | m_tcArea.width - COMBO_MARGIN - | |
1009 | (textCtrlXAdjust + m_widthCustomPaint + m_absIndent), | |
1010 | -1 ); | |
1011 | ||
1012 | // Make sure textctrl doesn't exceed the bottom custom border | |
1013 | wxSize tsz = m_text->GetSize(); | |
1014 | diff = (y + tsz.y) - (sz.y - customBorder); | |
1015 | if ( diff >= 0 ) | |
1016 | { | |
1017 | tsz.y = tsz.y - diff - 1; | |
1018 | m_text->SetSize(tsz); | |
1019 | } | |
1020 | } | |
1021 | else | |
1022 | #else | |
1023 | wxUnusedVar(textCtrlXAdjust); | |
1024 | wxUnusedVar(textCtrlYAdjust); | |
1025 | #endif | |
1026 | { | |
1027 | // If it has border, have textctrl will the entire text field. | |
1028 | m_text->SetSize( m_tcArea.x + m_widthCustomPaint, | |
1029 | customBorder, | |
1030 | sz.x - m_btnArea.width - m_widthCustomPaint - customBorder, | |
1031 | sz.y-(customBorder*2) ); | |
1032 | } | |
1033 | } | |
1034 | ||
1035 | wxSize wxComboCtrlBase::DoGetBestSize() const | |
1036 | { | |
1037 | wxSize sizeText(150,0); | |
1038 | ||
1039 | if ( m_text ) | |
1040 | sizeText = m_text->GetBestSize(); | |
1041 | ||
1042 | // TODO: Better method to calculate close-to-native control height. | |
1043 | ||
1044 | int fhei; | |
1045 | if ( m_font.Ok() ) | |
1046 | fhei = (m_font.GetPointSize()*2) + 5; | |
1047 | else if ( wxNORMAL_FONT->Ok() ) | |
1048 | fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5; | |
1049 | else | |
1050 | fhei = sizeText.y + 4; | |
1051 | ||
1052 | // Need to force height to accomodate bitmap? | |
1053 | int btnSizeY = m_btnSize.y; | |
1054 | if ( m_bmpNormal.Ok() && fhei < btnSizeY ) | |
1055 | fhei = btnSizeY; | |
1056 | ||
1057 | // Control height doesn't depend on border | |
1058 | /* | |
1059 | // Add border | |
1060 | int border = m_windowStyle & wxBORDER_MASK; | |
1061 | if ( border == wxSIMPLE_BORDER ) | |
1062 | fhei += 2; | |
1063 | else if ( border == wxNO_BORDER ) | |
1064 | fhei += (m_widthCustomBorder*2); | |
1065 | else | |
1066 | // Sunken etc. | |
1067 | fhei += 4; | |
1068 | */ | |
1069 | ||
1070 | // Final adjustments | |
1071 | #ifdef __WXGTK__ | |
1072 | fhei += 1; | |
1073 | #endif | |
1074 | ||
1075 | #ifdef __WXMAC__ | |
1076 | // these are the numbers from the HIG: | |
1077 | switch ( m_windowVariant ) | |
1078 | { | |
1079 | case wxWINDOW_VARIANT_NORMAL: | |
1080 | default : | |
1081 | fhei = 22; | |
1082 | break; | |
1083 | case wxWINDOW_VARIANT_SMALL: | |
1084 | fhei = 19; | |
1085 | break; | |
1086 | case wxWINDOW_VARIANT_MINI: | |
1087 | fhei = 15; | |
1088 | break; | |
1089 | } | |
1090 | #endif | |
1091 | ||
1092 | wxSize ret(sizeText.x + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH, | |
1093 | fhei); | |
1094 | ||
1095 | CacheBestSize(ret); | |
1096 | return ret; | |
1097 | } | |
1098 | ||
1099 | void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event ) | |
1100 | { | |
1101 | if ( !IsCreated() ) | |
1102 | return; | |
1103 | ||
1104 | // defined by actual wxComboCtrls | |
1105 | OnResize(); | |
1106 | ||
1107 | event.Skip(); | |
1108 | } | |
1109 | ||
1110 | // ---------------------------------------------------------------------------- | |
1111 | // standard operations | |
1112 | // ---------------------------------------------------------------------------- | |
1113 | ||
1114 | bool wxComboCtrlBase::Enable(bool enable) | |
1115 | { | |
1116 | if ( !wxControl::Enable(enable) ) | |
1117 | return false; | |
1118 | ||
1119 | if ( m_btn ) | |
1120 | m_btn->Enable(enable); | |
1121 | if ( m_text ) | |
1122 | m_text->Enable(enable); | |
1123 | ||
1124 | return true; | |
1125 | } | |
1126 | ||
1127 | bool wxComboCtrlBase::Show(bool show) | |
1128 | { | |
1129 | if ( !wxControl::Show(show) ) | |
1130 | return false; | |
1131 | ||
1132 | if (m_btn) | |
1133 | m_btn->Show(show); | |
1134 | ||
1135 | if (m_text) | |
1136 | m_text->Show(show); | |
1137 | ||
1138 | return true; | |
1139 | } | |
1140 | ||
1141 | bool wxComboCtrlBase::SetFont ( const wxFont& font ) | |
1142 | { | |
1143 | if ( !wxControl::SetFont(font) ) | |
1144 | return false; | |
1145 | ||
1146 | if (m_text) | |
1147 | m_text->SetFont(font); | |
1148 | ||
1149 | return true; | |
1150 | } | |
1151 | ||
1152 | #if wxUSE_TOOLTIPS | |
1153 | void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip) | |
1154 | { | |
1155 | wxControl::DoSetToolTip(tooltip); | |
1156 | ||
1157 | // Set tool tip for button and text box | |
1158 | if ( tooltip ) | |
1159 | { | |
1160 | const wxString &tip = tooltip->GetTip(); | |
1161 | if ( m_text ) m_text->SetToolTip(tip); | |
1162 | if ( m_btn ) m_btn->SetToolTip(tip); | |
1163 | } | |
1164 | else | |
1165 | { | |
1166 | if ( m_text ) m_text->SetToolTip( (wxToolTip*) NULL ); | |
1167 | if ( m_btn ) m_btn->SetToolTip( (wxToolTip*) NULL ); | |
1168 | } | |
1169 | } | |
1170 | #endif // wxUSE_TOOLTIPS | |
1171 | ||
1172 | #if wxUSE_VALIDATORS | |
1173 | void wxComboCtrlBase::SetValidator(const wxValidator& validator) | |
1174 | { | |
1175 | wxTextCtrl* textCtrl = GetTextCtrl(); | |
1176 | ||
1177 | if ( textCtrl ) | |
1178 | textCtrl->SetValidator( validator ); | |
1179 | } | |
1180 | ||
1181 | wxValidator* wxComboCtrlBase::GetValidator() | |
1182 | { | |
1183 | wxTextCtrl* textCtrl = GetTextCtrl(); | |
1184 | ||
1185 | if ( textCtrl ) | |
1186 | return textCtrl->GetValidator(); | |
1187 | ||
1188 | return wxControl::GetValidator(); | |
1189 | } | |
1190 | #endif // wxUSE_VALIDATORS | |
1191 | ||
1192 | // ---------------------------------------------------------------------------- | |
1193 | // painting | |
1194 | // ---------------------------------------------------------------------------- | |
1195 | ||
1196 | #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__) | |
1197 | // prepare combo box background on area in a way typical on platform | |
1198 | void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const | |
1199 | { | |
1200 | wxSize sz = GetClientSize(); | |
1201 | bool isEnabled; | |
1202 | bool isFocused; // also selected | |
1203 | ||
1204 | // For smaller size control (and for disabled background) use less spacing | |
1205 | int focusSpacingX; | |
1206 | int focusSpacingY; | |
1207 | ||
1208 | if ( !(flags & wxCONTROL_ISSUBMENU) ) | |
1209 | { | |
1210 | // Drawing control | |
1211 | isEnabled = IsEnabled(); | |
1212 | isFocused = ShouldDrawFocus(); | |
1213 | ||
1214 | // Windows-style: for smaller size control (and for disabled background) use less spacing | |
1215 | focusSpacingX = isEnabled ? 2 : 1; | |
1216 | focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1; | |
1217 | } | |
1218 | else | |
1219 | { | |
1220 | // Drawing a list item | |
1221 | isEnabled = true; // they are never disabled | |
1222 | isFocused = flags & wxCONTROL_SELECTED ? true : false; | |
1223 | ||
1224 | focusSpacingX = 0; | |
1225 | focusSpacingY = 0; | |
1226 | } | |
1227 | ||
1228 | // Set the background sub-rectangle for selection, disabled etc | |
1229 | wxRect selRect(rect); | |
1230 | selRect.y += focusSpacingY; | |
1231 | selRect.height -= (focusSpacingY*2); | |
1232 | ||
1233 | int wcp = 0; | |
1234 | ||
1235 | if ( !(flags & wxCONTROL_ISSUBMENU) ) | |
1236 | wcp += m_widthCustomPaint; | |
1237 | ||
1238 | selRect.x += wcp + focusSpacingX; | |
1239 | selRect.width -= wcp + (focusSpacingX*2); | |
1240 | ||
1241 | wxColour bgCol; | |
1242 | ||
1243 | if ( isEnabled ) | |
1244 | { | |
1245 | // If popup is hidden and this control is focused, | |
1246 | // then draw the focus-indicator (selbgcolor background etc.). | |
1247 | if ( isFocused ) | |
1248 | { | |
1249 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) ); | |
1250 | bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); | |
1251 | } | |
1252 | else | |
1253 | { | |
1254 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) ); | |
1255 | bgCol = GetBackgroundColour(); | |
1256 | } | |
1257 | } | |
1258 | else | |
1259 | { | |
1260 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT) ); | |
1261 | bgCol = GetBackgroundColour(); | |
1262 | } | |
1263 | ||
1264 | dc.SetBrush( bgCol ); | |
1265 | dc.SetPen( bgCol ); | |
1266 | dc.DrawRectangle( selRect ); | |
1267 | ||
1268 | // Don't clip exactly to the selection rectangle so we can draw | |
1269 | // to the non-selected area in front of it. | |
1270 | wxRect clipRect(rect.x,rect.y, | |
1271 | (selRect.x+selRect.width)-rect.x,rect.height); | |
1272 | dc.SetClippingRegion(clipRect); | |
1273 | } | |
1274 | #else | |
1275 | // Save the library size a bit for platforms that re-implement this. | |
1276 | void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const | |
1277 | { | |
1278 | } | |
1279 | #endif | |
1280 | ||
1281 | void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, int paintBg ) | |
1282 | { | |
1283 | int drawState = m_btnState; | |
1284 | ||
1285 | #ifdef __WXGTK__ | |
1286 | if ( GetPopupWindowState() >= Animating ) | |
1287 | drawState |= wxCONTROL_PRESSED; | |
1288 | #endif | |
1289 | ||
1290 | wxRect drawRect(rect.x+m_btnSpacingX, | |
1291 | rect.y+((rect.height-m_btnSize.y)/2), | |
1292 | m_btnSize.x, | |
1293 | m_btnSize.y); | |
1294 | ||
1295 | // Make sure area is not larger than the control | |
1296 | if ( drawRect.y < rect.y ) | |
1297 | drawRect.y = rect.y; | |
1298 | if ( drawRect.height > rect.height ) | |
1299 | drawRect.height = rect.height; | |
1300 | ||
1301 | bool enabled = IsEnabled(); | |
1302 | ||
1303 | if ( !enabled ) | |
1304 | drawState |= wxCONTROL_DISABLED; | |
1305 | ||
1306 | if ( !m_bmpNormal.Ok() ) | |
1307 | { | |
1308 | // Need to clear button background even if m_btn is present | |
1309 | if ( paintBg ) | |
1310 | { | |
1311 | wxColour bgCol; | |
1312 | ||
1313 | if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE ) | |
1314 | bgCol = GetParent()->GetBackgroundColour(); | |
1315 | else | |
1316 | bgCol = GetBackgroundColour(); | |
1317 | ||
1318 | dc.SetBrush(bgCol); | |
1319 | dc.SetPen(bgCol); | |
1320 | dc.DrawRectangle(rect); | |
1321 | } | |
1322 | ||
1323 | // Draw standard button | |
1324 | wxRendererNative::Get().DrawComboBoxDropButton(this, | |
1325 | dc, | |
1326 | drawRect, | |
1327 | drawState); | |
1328 | } | |
1329 | else | |
1330 | { | |
1331 | // Draw bitmap | |
1332 | ||
1333 | wxBitmap* pBmp; | |
1334 | ||
1335 | if ( !enabled ) | |
1336 | pBmp = &m_bmpDisabled; | |
1337 | else if ( m_btnState & wxCONTROL_PRESSED ) | |
1338 | pBmp = &m_bmpPressed; | |
1339 | else if ( m_btnState & wxCONTROL_CURRENT ) | |
1340 | pBmp = &m_bmpHover; | |
1341 | else | |
1342 | pBmp = &m_bmpNormal; | |
1343 | ||
1344 | if ( m_blankButtonBg ) | |
1345 | { | |
1346 | // If using blank button background, we need to clear its background | |
1347 | // with button face colour instead of colour for rest of the control. | |
1348 | if ( paintBg ) | |
1349 | { | |
1350 | wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
1351 | //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
1352 | dc.SetPen(bgCol); | |
1353 | dc.SetBrush(bgCol); | |
1354 | dc.DrawRectangle(rect); | |
1355 | } | |
1356 | ||
1357 | wxRendererNative::Get().DrawPushButton(this, | |
1358 | dc, | |
1359 | drawRect, | |
1360 | drawState); | |
1361 | ||
1362 | } | |
1363 | else | |
1364 | ||
1365 | { | |
1366 | // Need to clear button background even if m_btn is present | |
1367 | // (assume non-button background was cleared just before this call so brushes are good) | |
1368 | if ( paintBg ) | |
1369 | dc.DrawRectangle(rect); | |
1370 | } | |
1371 | ||
1372 | // Draw bitmap centered in drawRect | |
1373 | dc.DrawBitmap(*pBmp, | |
1374 | drawRect.x + (drawRect.width-pBmp->GetWidth())/2, | |
1375 | drawRect.y + (drawRect.height-pBmp->GetHeight())/2, | |
1376 | true); | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | void wxComboCtrlBase::RecalcAndRefresh() | |
1381 | { | |
1382 | if ( IsCreated() ) | |
1383 | { | |
1384 | wxSizeEvent evt(GetSize(),GetId()); | |
1385 | GetEventHandler()->ProcessEvent(evt); | |
1386 | Refresh(); | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | // ---------------------------------------------------------------------------- | |
1391 | // miscellaneous event handlers | |
1392 | // ---------------------------------------------------------------------------- | |
1393 | ||
1394 | void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event) | |
1395 | { | |
1396 | if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED ) | |
1397 | { | |
1398 | if ( m_ignoreEvtText > 0 ) | |
1399 | { | |
1400 | m_ignoreEvtText--; | |
1401 | return; | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | // Change event id, object and string before relaying it forward | |
1406 | event.SetId(GetId()); | |
1407 | wxString s = event.GetString(); | |
1408 | event.SetEventObject(this); | |
1409 | event.SetString(s); | |
1410 | event.Skip(); | |
1411 | } | |
1412 | ||
1413 | // call if cursor is on button area or mouse is captured for the button | |
1414 | bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event, | |
1415 | int flags ) | |
1416 | { | |
1417 | int type = event.GetEventType(); | |
1418 | ||
1419 | if ( type == wxEVT_MOTION ) | |
1420 | { | |
1421 | if ( flags & wxCC_MF_ON_BUTTON ) | |
1422 | { | |
1423 | if ( !(m_btnState & wxCONTROL_CURRENT) ) | |
1424 | { | |
1425 | // Mouse hover begins | |
1426 | m_btnState |= wxCONTROL_CURRENT; | |
1427 | if ( HasCapture() ) // Retain pressed state. | |
1428 | m_btnState |= wxCONTROL_PRESSED; | |
1429 | Refresh(); | |
1430 | } | |
1431 | } | |
1432 | else if ( (m_btnState & wxCONTROL_CURRENT) ) | |
1433 | { | |
1434 | // Mouse hover ends | |
1435 | m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED); | |
1436 | Refresh(); | |
1437 | } | |
1438 | } | |
1439 | else if ( type == wxEVT_LEFT_DOWN || type == wxEVT_LEFT_DCLICK ) | |
1440 | { | |
1441 | if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) ) | |
1442 | { | |
1443 | m_btnState |= wxCONTROL_PRESSED; | |
1444 | Refresh(); | |
1445 | ||
1446 | if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) ) | |
1447 | OnButtonClick(); | |
1448 | else | |
1449 | // If showing popup now, do not capture mouse or there will be interference | |
1450 | CaptureMouse(); | |
1451 | } | |
1452 | } | |
1453 | else if ( type == wxEVT_LEFT_UP ) | |
1454 | { | |
1455 | ||
1456 | // Only accept event if mouse was left-press was previously accepted | |
1457 | if ( HasCapture() ) | |
1458 | ReleaseMouse(); | |
1459 | ||
1460 | if ( m_btnState & wxCONTROL_PRESSED ) | |
1461 | { | |
1462 | // If mouse was inside, fire the click event. | |
1463 | if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP ) | |
1464 | { | |
1465 | if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) ) | |
1466 | OnButtonClick(); | |
1467 | } | |
1468 | ||
1469 | m_btnState &= ~(wxCONTROL_PRESSED); | |
1470 | Refresh(); | |
1471 | } | |
1472 | } | |
1473 | else if ( type == wxEVT_LEAVE_WINDOW ) | |
1474 | { | |
1475 | if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) ) | |
1476 | { | |
1477 | m_btnState &= ~(wxCONTROL_CURRENT); | |
1478 | ||
1479 | // Mouse hover ends | |
1480 | if ( IsPopupWindowState(Hidden) ) | |
1481 | { | |
1482 | m_btnState &= ~(wxCONTROL_PRESSED); | |
1483 | Refresh(); | |
1484 | } | |
1485 | } | |
1486 | } | |
1487 | else | |
1488 | return false; | |
1489 | ||
1490 | return true; | |
1491 | } | |
1492 | ||
1493 | // returns true if event was consumed or filtered | |
1494 | bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event, | |
1495 | int WXUNUSED(flags) ) | |
1496 | { | |
1497 | wxLongLong t = ::wxGetLocalTimeMillis(); | |
1498 | int evtType = event.GetEventType(); | |
1499 | ||
1500 | #if USES_WXPOPUPWINDOW || USES_WXDIALOG | |
1501 | if ( m_popupWinType != POPUPWIN_WXPOPUPTRANSIENTWINDOW ) | |
1502 | { | |
1503 | if ( IsPopupWindowState(Visible) && | |
1504 | ( evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_RIGHT_DOWN ) ) | |
1505 | { | |
1506 | HidePopup(); | |
1507 | return true; | |
1508 | } | |
1509 | } | |
1510 | #endif | |
1511 | ||
1512 | // Filter out clicks on button immediately after popup dismiss (Windows like behaviour) | |
1513 | if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick ) | |
1514 | { | |
1515 | event.SetEventType(0); | |
1516 | return true; | |
1517 | } | |
1518 | ||
1519 | return false; | |
1520 | } | |
1521 | ||
1522 | void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event ) | |
1523 | { | |
1524 | int evtType = event.GetEventType(); | |
1525 | ||
1526 | if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) && | |
1527 | (m_windowStyle & wxCB_READONLY) ) | |
1528 | { | |
1529 | if ( GetPopupWindowState() >= Animating ) | |
1530 | { | |
1531 | #if USES_WXPOPUPWINDOW | |
1532 | // Click here always hides the popup. | |
1533 | if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW ) | |
1534 | HidePopup(); | |
1535 | #endif | |
1536 | } | |
1537 | else | |
1538 | { | |
1539 | if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) ) | |
1540 | { | |
1541 | // In read-only mode, clicking the text is the | |
1542 | // same as clicking the button. | |
1543 | OnButtonClick(); | |
1544 | } | |
1545 | else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK ) | |
1546 | { | |
1547 | //if ( m_popupInterface->CycleValue() ) | |
1548 | // Refresh(); | |
1549 | if ( m_popupInterface ) | |
1550 | m_popupInterface->OnComboDoubleClick(); | |
1551 | } | |
1552 | } | |
1553 | } | |
1554 | else | |
1555 | if ( IsPopupShown() ) | |
1556 | { | |
1557 | // relay (some) mouse events to the popup | |
1558 | if ( evtType == wxEVT_MOUSEWHEEL ) | |
1559 | m_popup->AddPendingEvent(event); | |
1560 | } | |
1561 | else if ( evtType ) | |
1562 | event.Skip(); | |
1563 | } | |
1564 | ||
1565 | void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event) | |
1566 | { | |
1567 | if ( IsPopupShown() ) | |
1568 | { | |
1569 | // pass it to the popped up control | |
1570 | GetPopupControl()->GetControl()->AddPendingEvent(event); | |
1571 | } | |
1572 | else // no popup | |
1573 | { | |
1574 | int keycode = event.GetKeyCode(); | |
1575 | ||
1576 | if ( keycode == WXK_TAB ) | |
1577 | { | |
1578 | wxNavigationKeyEvent evt; | |
1579 | ||
1580 | wxWindow* mainCtrl = GetMainWindowOfCompositeControl(); | |
1581 | ||
1582 | evt.SetFlags(wxNavigationKeyEvent::FromTab| | |
1583 | (!event.ShiftDown() ? wxNavigationKeyEvent::IsForward | |
1584 | : wxNavigationKeyEvent::IsBackward)); | |
1585 | evt.SetEventObject(mainCtrl); | |
1586 | evt.SetCurrentFocus(mainCtrl); | |
1587 | mainCtrl->GetParent()->GetEventHandler()->AddPendingEvent(evt); | |
1588 | return; | |
1589 | } | |
1590 | ||
1591 | if ( IsKeyPopupToggle(event) ) | |
1592 | { | |
1593 | OnButtonClick(); | |
1594 | return; | |
1595 | } | |
1596 | ||
1597 | int comboStyle = GetWindowStyle(); | |
1598 | wxComboPopup* popupInterface = GetPopupControl(); | |
1599 | ||
1600 | if ( !popupInterface ) | |
1601 | { | |
1602 | event.Skip(); | |
1603 | return; | |
1604 | } | |
1605 | ||
1606 | if ( (comboStyle & wxCB_READONLY) || | |
1607 | (keycode != WXK_RIGHT && keycode != WXK_LEFT) ) | |
1608 | { | |
1609 | popupInterface->OnComboKeyEvent(event); | |
1610 | } | |
1611 | else | |
1612 | event.Skip(); | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event ) | |
1617 | { | |
1618 | if ( event.GetEventType() == wxEVT_SET_FOCUS ) | |
1619 | { | |
1620 | #ifndef __WXMAC__ | |
1621 | wxWindow* tc = GetTextCtrl(); | |
1622 | if ( tc && tc != DoFindFocus() ) | |
1623 | tc->SetFocus(); | |
1624 | #endif | |
1625 | } | |
1626 | ||
1627 | Refresh(); | |
1628 | } | |
1629 | ||
1630 | void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) | |
1631 | { | |
1632 | OnThemeChange(); | |
1633 | // indentation may also have changed | |
1634 | if ( !(m_iFlags & wxCC_IFLAG_INDENT_SET) ) | |
1635 | m_absIndent = GetNativeTextIndent(); | |
1636 | RecalcAndRefresh(); | |
1637 | } | |
1638 | ||
1639 | // ---------------------------------------------------------------------------- | |
1640 | // popup handling | |
1641 | // ---------------------------------------------------------------------------- | |
1642 | ||
1643 | // Create popup window and the child control | |
1644 | void wxComboCtrlBase::CreatePopup() | |
1645 | { | |
1646 | wxComboPopup* popupInterface = m_popupInterface; | |
1647 | wxWindow* popup; | |
1648 | ||
1649 | if ( !m_winPopup ) | |
1650 | { | |
1651 | #ifdef wxComboPopupWindowBase2 | |
1652 | if ( m_iFlags & wxCC_IFLAG_USE_ALT_POPUP ) | |
1653 | { | |
1654 | #if !USES_WXDIALOG | |
1655 | m_winPopup = new wxComboPopupWindowBase2( this, wxNO_BORDER ); | |
1656 | #else | |
1657 | m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY, wxEmptyString, | |
1658 | wxPoint(-21,-21), wxSize(20, 20), | |
1659 | wxNO_BORDER ); | |
1660 | #endif | |
1661 | m_popupWinType = SECONDARY_POPUP_TYPE; | |
1662 | } | |
1663 | else | |
1664 | #endif | |
1665 | { | |
1666 | m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER ); | |
1667 | m_popupWinType = PRIMARY_POPUP_TYPE; | |
1668 | } | |
1669 | m_popupWinEvtHandler = new wxComboPopupWindowEvtHandler(this); | |
1670 | m_winPopup->PushEventHandler(m_popupWinEvtHandler); | |
1671 | } | |
1672 | ||
1673 | popupInterface->Create(m_winPopup); | |
1674 | m_popup = popup = popupInterface->GetControl(); | |
1675 | ||
1676 | m_popupExtraHandler = new wxComboPopupExtraEventHandler(this); | |
1677 | popup->PushEventHandler( m_popupExtraHandler ); | |
1678 | ||
1679 | // This may be helpful on some platforms | |
1680 | // (eg. it bypasses a wxGTK popupwindow bug where | |
1681 | // window is not initially hidden when it should be) | |
1682 | m_winPopup->Hide(); | |
1683 | ||
1684 | popupInterface->m_iFlags |= wxCP_IFLAG_CREATED; | |
1685 | } | |
1686 | ||
1687 | // Destroy popup window and the child control | |
1688 | void wxComboCtrlBase::DestroyPopup() | |
1689 | { | |
1690 | HidePopup(); | |
1691 | ||
1692 | if ( m_popup ) | |
1693 | m_popup->RemoveEventHandler(m_popupExtraHandler); | |
1694 | ||
1695 | delete m_popupExtraHandler; | |
1696 | ||
1697 | delete m_popupInterface; | |
1698 | ||
1699 | if ( m_winPopup ) | |
1700 | { | |
1701 | m_winPopup->RemoveEventHandler(m_popupWinEvtHandler); | |
1702 | delete m_popupWinEvtHandler; | |
1703 | m_popupWinEvtHandler = NULL; | |
1704 | m_winPopup->Destroy(); | |
1705 | } | |
1706 | ||
1707 | m_popupExtraHandler = (wxEvtHandler*) NULL; | |
1708 | m_popupInterface = (wxComboPopup*) NULL; | |
1709 | m_winPopup = (wxWindow*) NULL; | |
1710 | m_popup = (wxWindow*) NULL; | |
1711 | } | |
1712 | ||
1713 | void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface) | |
1714 | { | |
1715 | wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") ); | |
1716 | ||
1717 | DestroyPopup(); | |
1718 | ||
1719 | iface->InitBase(this); | |
1720 | iface->Init(); | |
1721 | ||
1722 | m_popupInterface = iface; | |
1723 | ||
1724 | if ( !iface->LazyCreate() ) | |
1725 | { | |
1726 | CreatePopup(); | |
1727 | } | |
1728 | else | |
1729 | { | |
1730 | m_popup = (wxWindow*) NULL; | |
1731 | } | |
1732 | ||
1733 | // This must be done after creation | |
1734 | if ( m_valueString.length() ) | |
1735 | { | |
1736 | iface->SetStringValue(m_valueString); | |
1737 | //Refresh(); | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | // Ensures there is atleast the default popup | |
1742 | void wxComboCtrlBase::EnsurePopupControl() | |
1743 | { | |
1744 | if ( !m_popupInterface ) | |
1745 | SetPopupControl(NULL); | |
1746 | } | |
1747 | ||
1748 | void wxComboCtrlBase::OnButtonClick() | |
1749 | { | |
1750 | // Derived classes can override this method for totally custom | |
1751 | // popup action | |
1752 | ShowPopup(); | |
1753 | } | |
1754 | ||
1755 | void wxComboCtrlBase::ShowPopup() | |
1756 | { | |
1757 | EnsurePopupControl(); | |
1758 | wxCHECK_RET( !IsPopupWindowState(Visible), wxT("popup window already shown") ); | |
1759 | ||
1760 | if ( IsPopupWindowState(Animating) ) | |
1761 | return; | |
1762 | ||
1763 | SetFocus(); | |
1764 | ||
1765 | // Space above and below | |
1766 | int screenHeight; | |
1767 | wxPoint scrPos; | |
1768 | int spaceAbove; | |
1769 | int spaceBelow; | |
1770 | int maxHeightPopup; | |
1771 | wxSize ctrlSz = GetSize(); | |
1772 | ||
1773 | screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ); | |
1774 | scrPos = GetParent()->ClientToScreen(GetPosition()); | |
1775 | ||
1776 | spaceAbove = scrPos.y; | |
1777 | spaceBelow = screenHeight - spaceAbove - ctrlSz.y; | |
1778 | ||
1779 | maxHeightPopup = spaceBelow; | |
1780 | if ( spaceAbove > spaceBelow ) | |
1781 | maxHeightPopup = spaceAbove; | |
1782 | ||
1783 | // Width | |
1784 | int widthPopup = ctrlSz.x + m_extLeft + m_extRight; | |
1785 | ||
1786 | if ( widthPopup < m_widthMinPopup ) | |
1787 | widthPopup = m_widthMinPopup; | |
1788 | ||
1789 | wxWindow* winPopup = m_winPopup; | |
1790 | wxWindow* popup; | |
1791 | ||
1792 | // Need to disable tab traversal of parent | |
1793 | // | |
1794 | // NB: This is to fix a bug in wxMSW. In theory it could also be fixed | |
1795 | // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage | |
1796 | // that if transient popup is open, then tab traversal is to be ignored. | |
1797 | // However, I think this code would still be needed for cases where | |
1798 | // transient popup doesn't work yet (wxWinCE?). | |
1799 | wxWindow* parent = GetParent(); | |
1800 | int parentFlags = parent->GetWindowStyle(); | |
1801 | if ( parentFlags & wxTAB_TRAVERSAL ) | |
1802 | { | |
1803 | parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) ); | |
1804 | m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL; | |
1805 | } | |
1806 | ||
1807 | if ( !winPopup ) | |
1808 | { | |
1809 | CreatePopup(); | |
1810 | winPopup = m_winPopup; | |
1811 | popup = m_popup; | |
1812 | } | |
1813 | else | |
1814 | { | |
1815 | popup = m_popup; | |
1816 | } | |
1817 | ||
1818 | winPopup->Enable(); | |
1819 | ||
1820 | wxASSERT( !m_popup || m_popup == popup ); // Consistency check. | |
1821 | ||
1822 | wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup, | |
1823 | m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup, | |
1824 | maxHeightPopup); | |
1825 | ||
1826 | popup->SetSize(adjustedSize); | |
1827 | popup->Move(0,0); | |
1828 | m_popupInterface->OnPopup(); | |
1829 | ||
1830 | // | |
1831 | // Reposition and resize popup window | |
1832 | // | |
1833 | ||
1834 | wxSize szp = popup->GetSize(); | |
1835 | ||
1836 | int popupX; | |
1837 | int popupY = scrPos.y + ctrlSz.y; | |
1838 | ||
1839 | // Default anchor is wxLEFT | |
1840 | int anchorSide = m_anchorSide; | |
1841 | if ( !anchorSide ) | |
1842 | anchorSide = wxLEFT; | |
1843 | ||
1844 | int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x; | |
1845 | int leftX = scrPos.x - m_extLeft; | |
1846 | int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ); | |
1847 | ||
1848 | // If there is not enough horizontal space, anchor on the other side. | |
1849 | // If there is no space even then, place the popup at x 0. | |
1850 | if ( anchorSide == wxRIGHT ) | |
1851 | { | |
1852 | if ( rightX < 0 ) | |
1853 | { | |
1854 | if ( (leftX+szp.x) < screenWidth ) | |
1855 | anchorSide = wxLEFT; | |
1856 | else | |
1857 | anchorSide = 0; | |
1858 | } | |
1859 | } | |
1860 | else | |
1861 | { | |
1862 | if ( (leftX+szp.x) >= screenWidth ) | |
1863 | { | |
1864 | if ( rightX >= 0 ) | |
1865 | anchorSide = wxRIGHT; | |
1866 | else | |
1867 | anchorSide = 0; | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | // Select x coordinate according to the anchor side | |
1872 | if ( anchorSide == wxRIGHT ) | |
1873 | popupX = rightX; | |
1874 | else if ( anchorSide == wxLEFT ) | |
1875 | popupX = leftX; | |
1876 | else | |
1877 | popupX = 0; | |
1878 | ||
1879 | int showFlags = CanDeferShow; | |
1880 | ||
1881 | if ( spaceBelow < szp.y ) | |
1882 | { | |
1883 | popupY = scrPos.y - szp.y; | |
1884 | showFlags |= ShowAbove; | |
1885 | } | |
1886 | ||
1887 | #if INSTALL_TOPLEV_HANDLER | |
1888 | // Put top level window event handler into place | |
1889 | if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW ) | |
1890 | { | |
1891 | if ( !m_toplevEvtHandler ) | |
1892 | m_toplevEvtHandler = new wxComboFrameEventHandler(this); | |
1893 | ||
1894 | wxWindow* toplev = ::wxGetTopLevelParent( this ); | |
1895 | wxASSERT( toplev ); | |
1896 | ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup(); | |
1897 | toplev->PushEventHandler( m_toplevEvtHandler ); | |
1898 | } | |
1899 | #endif | |
1900 | ||
1901 | // Set string selection (must be this way instead of SetStringSelection) | |
1902 | if ( m_text ) | |
1903 | { | |
1904 | if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) ) | |
1905 | m_text->SelectAll(); | |
1906 | ||
1907 | m_popupInterface->SetStringValue( m_text->GetValue() ); | |
1908 | } | |
1909 | else | |
1910 | { | |
1911 | // This is neede since focus/selection indication may change when popup is shown | |
1912 | Refresh(); | |
1913 | } | |
1914 | ||
1915 | // This must be after SetStringValue | |
1916 | m_popupWinState = Animating; | |
1917 | ||
1918 | wxRect popupWinRect( popupX, popupY, szp.x, szp.y ); | |
1919 | ||
1920 | m_popup = popup; | |
1921 | if ( (m_iFlags & wxCC_IFLAG_DISABLE_POPUP_ANIM) || | |
1922 | AnimateShow( popupWinRect, showFlags ) ) | |
1923 | { | |
1924 | DoShowPopup( popupWinRect, showFlags ); | |
1925 | } | |
1926 | } | |
1927 | ||
1928 | bool wxComboCtrlBase::AnimateShow( const wxRect& WXUNUSED(rect), int WXUNUSED(flags) ) | |
1929 | { | |
1930 | return true; | |
1931 | } | |
1932 | ||
1933 | void wxComboCtrlBase::DoShowPopup( const wxRect& rect, int WXUNUSED(flags) ) | |
1934 | { | |
1935 | wxWindow* winPopup = m_winPopup; | |
1936 | ||
1937 | if ( IsPopupWindowState(Animating) ) | |
1938 | { | |
1939 | // Make sure the popup window is shown in the right position. | |
1940 | // Should not matter even if animation already did this. | |
1941 | ||
1942 | // Some platforms (GTK) may like SetSize and Move to be separate | |
1943 | // (though the bug was probably fixed). | |
1944 | winPopup->SetSize( rect ); | |
1945 | ||
1946 | winPopup->Show(); | |
1947 | ||
1948 | m_popupWinState = Visible; | |
1949 | } | |
1950 | else if ( IsPopupWindowState(Hidden) ) | |
1951 | { | |
1952 | // Animation was aborted | |
1953 | ||
1954 | wxASSERT( !winPopup->IsShown() ); | |
1955 | ||
1956 | m_popupWinState = Hidden; | |
1957 | } | |
1958 | } | |
1959 | ||
1960 | void wxComboCtrlBase::OnPopupDismiss() | |
1961 | { | |
1962 | // Just in case, avoid double dismiss | |
1963 | if ( IsPopupWindowState(Hidden) ) | |
1964 | return; | |
1965 | ||
1966 | // This must be set before focus - otherwise there will be recursive | |
1967 | // OnPopupDismisses. | |
1968 | m_popupWinState = Hidden; | |
1969 | ||
1970 | //SetFocus(); | |
1971 | m_winPopup->Disable(); | |
1972 | ||
1973 | // Inform popup control itself | |
1974 | m_popupInterface->OnDismiss(); | |
1975 | ||
1976 | if ( m_popupExtraHandler ) | |
1977 | ((wxComboPopupExtraEventHandler*)m_popupExtraHandler)->OnPopupDismiss(); | |
1978 | ||
1979 | #if INSTALL_TOPLEV_HANDLER | |
1980 | // Remove top level window event handler | |
1981 | if ( m_toplevEvtHandler ) | |
1982 | { | |
1983 | wxWindow* toplev = ::wxGetTopLevelParent( this ); | |
1984 | if ( toplev ) | |
1985 | toplev->RemoveEventHandler( m_toplevEvtHandler ); | |
1986 | } | |
1987 | #endif | |
1988 | ||
1989 | m_timeCanAcceptClick = ::wxGetLocalTimeMillis(); | |
1990 | ||
1991 | if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW ) | |
1992 | m_timeCanAcceptClick += 150; | |
1993 | ||
1994 | // If cursor not on dropdown button, then clear its state | |
1995 | // (technically not required by all ports, but do it for all just in case) | |
1996 | if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) ) | |
1997 | m_btnState = 0; | |
1998 | ||
1999 | // Return parent's tab traversal flag. | |
2000 | // See ShowPopup for notes. | |
2001 | if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL ) | |
2002 | { | |
2003 | wxWindow* parent = GetParent(); | |
2004 | parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL ); | |
2005 | m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL); | |
2006 | } | |
2007 | ||
2008 | // refresh control (necessary even if m_text) | |
2009 | Refresh(); | |
2010 | ||
2011 | SetFocus(); | |
2012 | } | |
2013 | ||
2014 | void wxComboCtrlBase::HidePopup() | |
2015 | { | |
2016 | // Should be able to call this without popup interface | |
2017 | if ( IsPopupWindowState(Hidden) ) | |
2018 | return; | |
2019 | ||
2020 | // transfer value and show it in textctrl, if any | |
2021 | if ( !IsPopupWindowState(Animating) ) | |
2022 | SetValue( m_popupInterface->GetStringValue() ); | |
2023 | ||
2024 | m_winPopup->Hide(); | |
2025 | ||
2026 | OnPopupDismiss(); | |
2027 | } | |
2028 | ||
2029 | // ---------------------------------------------------------------------------- | |
2030 | // customization methods | |
2031 | // ---------------------------------------------------------------------------- | |
2032 | ||
2033 | void wxComboCtrlBase::SetButtonPosition( int width, int height, | |
2034 | int side, int spacingX ) | |
2035 | { | |
2036 | m_btnWid = width; | |
2037 | m_btnHei = height; | |
2038 | m_btnSide = side; | |
2039 | m_btnSpacingX = spacingX; | |
2040 | ||
2041 | RecalcAndRefresh(); | |
2042 | } | |
2043 | ||
2044 | wxSize wxComboCtrlBase::GetButtonSize() | |
2045 | { | |
2046 | if ( m_btnSize.x > 0 ) | |
2047 | return m_btnSize; | |
2048 | ||
2049 | wxSize retSize(m_btnWid,m_btnHei); | |
2050 | ||
2051 | // Need to call CalculateAreas now if button size is | |
2052 | // is not explicitly specified. | |
2053 | if ( retSize.x <= 0 || retSize.y <= 0) | |
2054 | { | |
2055 | OnResize(); | |
2056 | ||
2057 | retSize = m_btnSize; | |
2058 | } | |
2059 | ||
2060 | return retSize; | |
2061 | } | |
2062 | ||
2063 | void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal, | |
2064 | bool blankButtonBg, | |
2065 | const wxBitmap& bmpPressed, | |
2066 | const wxBitmap& bmpHover, | |
2067 | const wxBitmap& bmpDisabled ) | |
2068 | { | |
2069 | m_bmpNormal = bmpNormal; | |
2070 | m_blankButtonBg = blankButtonBg; | |
2071 | ||
2072 | if ( bmpPressed.Ok() ) | |
2073 | m_bmpPressed = bmpPressed; | |
2074 | else | |
2075 | m_bmpPressed = bmpNormal; | |
2076 | ||
2077 | if ( bmpHover.Ok() ) | |
2078 | m_bmpHover = bmpHover; | |
2079 | else | |
2080 | m_bmpHover = bmpNormal; | |
2081 | ||
2082 | if ( bmpDisabled.Ok() ) | |
2083 | m_bmpDisabled = bmpDisabled; | |
2084 | else | |
2085 | m_bmpDisabled = bmpNormal; | |
2086 | ||
2087 | RecalcAndRefresh(); | |
2088 | } | |
2089 | ||
2090 | void wxComboCtrlBase::SetCustomPaintWidth( int width ) | |
2091 | { | |
2092 | if ( m_text ) | |
2093 | { | |
2094 | // move textctrl accordingly | |
2095 | wxRect r = m_text->GetRect(); | |
2096 | int inc = width - m_widthCustomPaint; | |
2097 | r.x += inc; | |
2098 | r.width -= inc; | |
2099 | m_text->SetSize( r ); | |
2100 | } | |
2101 | ||
2102 | m_widthCustomPaint = width; | |
2103 | ||
2104 | RecalcAndRefresh(); | |
2105 | } | |
2106 | ||
2107 | void wxComboCtrlBase::SetTextIndent( int indent ) | |
2108 | { | |
2109 | if ( indent < 0 ) | |
2110 | { | |
2111 | m_absIndent = GetNativeTextIndent(); | |
2112 | m_iFlags &= ~(wxCC_IFLAG_INDENT_SET); | |
2113 | } | |
2114 | else | |
2115 | { | |
2116 | m_absIndent = indent; | |
2117 | m_iFlags |= wxCC_IFLAG_INDENT_SET; | |
2118 | } | |
2119 | ||
2120 | RecalcAndRefresh(); | |
2121 | } | |
2122 | ||
2123 | wxCoord wxComboCtrlBase::GetNativeTextIndent() const | |
2124 | { | |
2125 | return DEFAULT_TEXT_INDENT; | |
2126 | } | |
2127 | ||
2128 | // ---------------------------------------------------------------------------- | |
2129 | // methods forwarded to wxTextCtrl | |
2130 | // ---------------------------------------------------------------------------- | |
2131 | ||
2132 | wxString wxComboCtrlBase::GetValue() const | |
2133 | { | |
2134 | if ( m_text ) | |
2135 | return m_text->GetValue(); | |
2136 | return m_valueString; | |
2137 | } | |
2138 | ||
2139 | void wxComboCtrlBase::SetValueWithEvent(const wxString& value, bool withEvent) | |
2140 | { | |
2141 | if ( m_text ) | |
2142 | { | |
2143 | if ( !withEvent ) | |
2144 | m_ignoreEvtText++; | |
2145 | ||
2146 | m_text->SetValue(value); | |
2147 | if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) ) | |
2148 | m_text->SelectAll(); | |
2149 | } | |
2150 | ||
2151 | m_valueString = value; | |
2152 | ||
2153 | Refresh(); | |
2154 | ||
2155 | // Since wxComboPopup may want to paint the combo as well, we need | |
2156 | // to set the string value here (as well as sometimes in ShowPopup). | |
2157 | if ( m_valueString != value && m_popupInterface ) | |
2158 | { | |
2159 | m_popupInterface->SetStringValue(value); | |
2160 | } | |
2161 | } | |
2162 | ||
2163 | void wxComboCtrlBase::SetValue(const wxString& value) | |
2164 | { | |
2165 | SetValueWithEvent(value, false); | |
2166 | } | |
2167 | ||
2168 | // In this SetValue variant wxComboPopup::SetStringValue is not called | |
2169 | void wxComboCtrlBase::SetText(const wxString& value) | |
2170 | { | |
2171 | // Unlike in SetValue(), this must be called here or | |
2172 | // the behaviour will no be consistent in readonlys. | |
2173 | EnsurePopupControl(); | |
2174 | ||
2175 | m_valueString = value; | |
2176 | ||
2177 | if ( m_text ) | |
2178 | { | |
2179 | m_ignoreEvtText++; | |
2180 | m_text->SetValue( value ); | |
2181 | } | |
2182 | ||
2183 | Refresh(); | |
2184 | } | |
2185 | ||
2186 | void wxComboCtrlBase::Copy() | |
2187 | { | |
2188 | if ( m_text ) | |
2189 | m_text->Copy(); | |
2190 | } | |
2191 | ||
2192 | void wxComboCtrlBase::Cut() | |
2193 | { | |
2194 | if ( m_text ) | |
2195 | m_text->Cut(); | |
2196 | } | |
2197 | ||
2198 | void wxComboCtrlBase::Paste() | |
2199 | { | |
2200 | if ( m_text ) | |
2201 | m_text->Paste(); | |
2202 | } | |
2203 | ||
2204 | void wxComboCtrlBase::SetInsertionPoint(long pos) | |
2205 | { | |
2206 | if ( m_text ) | |
2207 | m_text->SetInsertionPoint(pos); | |
2208 | } | |
2209 | ||
2210 | void wxComboCtrlBase::SetInsertionPointEnd() | |
2211 | { | |
2212 | if ( m_text ) | |
2213 | m_text->SetInsertionPointEnd(); | |
2214 | } | |
2215 | ||
2216 | long wxComboCtrlBase::GetInsertionPoint() const | |
2217 | { | |
2218 | if ( m_text ) | |
2219 | return m_text->GetInsertionPoint(); | |
2220 | ||
2221 | return 0; | |
2222 | } | |
2223 | ||
2224 | long wxComboCtrlBase::GetLastPosition() const | |
2225 | { | |
2226 | if ( m_text ) | |
2227 | return m_text->GetLastPosition(); | |
2228 | ||
2229 | return 0; | |
2230 | } | |
2231 | ||
2232 | void wxComboCtrlBase::Replace(long from, long to, const wxString& value) | |
2233 | { | |
2234 | if ( m_text ) | |
2235 | m_text->Replace(from, to, value); | |
2236 | } | |
2237 | ||
2238 | void wxComboCtrlBase::Remove(long from, long to) | |
2239 | { | |
2240 | if ( m_text ) | |
2241 | m_text->Remove(from, to); | |
2242 | } | |
2243 | ||
2244 | void wxComboCtrlBase::SetSelection(long from, long to) | |
2245 | { | |
2246 | if ( m_text ) | |
2247 | m_text->SetSelection(from, to); | |
2248 | } | |
2249 | ||
2250 | void wxComboCtrlBase::Undo() | |
2251 | { | |
2252 | if ( m_text ) | |
2253 | m_text->Undo(); | |
2254 | } | |
2255 | ||
2256 | #endif // wxUSE_COMBOCTRL |