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