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