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