1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboCtrl declaration
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_COMBOCONTROL_H_BASE_
13 #define _WX_COMBOCONTROL_H_BASE_
17 A few words about all the classes defined in this file are probably in
18 order: why do we need extra wxComboCtrl and wxComboPopup classes?
20 This is because a traditional combobox is a combination of a text control
21 (with a button allowing to open the pop down list) with a listbox and
22 wxComboBox class is exactly such control, however we want to also have other
23 combinations - in fact, we want to allow anything at all to be used as pop
24 down list, not just a wxListBox.
26 So we define a base wxComboCtrl which can use any control as pop down
27 list and wxComboBox deriving from it which implements the standard wxWidgets
28 combobox API. wxComboCtrl needs to be told somehow which control to use
29 and this is done by SetPopupControl(). However, we need something more than
30 just a wxControl in this method as, for example, we need to call
31 SetSelection("initial text value") and wxControl doesn't have such method.
32 So we also need a wxComboPopup which is just a very simple interface which
33 must be implemented by a control to be usable as a popup.
35 We couldn't derive wxComboPopup from wxControl as this would make it
36 impossible to have a class deriving from both wxListBx and from it, so
37 instead it is just a mix-in.
45 #include "wx/control.h"
46 #include "wx/renderer.h" // this is needed for wxCONTROL_XXX flags
47 #include "wx/bitmap.h" // wxBitmap used by-value
49 class WXDLLIMPEXP_CORE wxTextCtrl
;
50 class WXDLLEXPORT wxComboPopup
;
53 // New window styles for wxComboCtrlBase
57 // Double-clicking a read-only combo triggers call to popup's OnComboPopup.
58 // In wxOwnerDrawnComboBox, for instance, it cycles item.
59 wxCC_SPECIAL_DCLICK
= 0x0100,
61 // Use keyboard behaviour alternate to platform default:
62 // Up an down keys will show popup instead of cycling value.
63 wxCC_ALT_KEYS
= 0x0200,
65 // Dropbutton acts like standard push button.
66 wxCC_STD_BUTTON
= 0x0400
70 // wxComboCtrl internal flags
73 // First those that can be passed to Customize.
74 // It is Windows style for all flags to be clear.
76 // Button is preferred outside the border (GTK style)
77 wxCC_BUTTON_OUTSIDE_BORDER
= 0x0001,
78 // Show popup on mouse up instead of mouse down (which is the Windows style)
79 wxCC_POPUP_ON_MOUSE_UP
= 0x0002,
80 // All text is not automatically selected on click
81 wxCC_NO_TEXT_AUTO_SELECT
= 0x0004,
83 // Internal use: signals creation is complete
84 wxCC_IFLAG_CREATED
= 0x0100,
85 // Internal use: really put button outside
86 wxCC_IFLAG_BUTTON_OUTSIDE
= 0x0200,
87 // Internal use: SetTextIndent has been called
88 wxCC_IFLAG_INDENT_SET
= 0x0400,
89 // Internal use: Set wxTAB_TRAVERSAL to parent when popup is dismissed
90 wxCC_IFLAG_PARENT_TAB_TRAVERSAL
= 0x0800
94 // Flags used by PreprocessMouseEvent and HandleButtonMouseEvent
97 wxCC_MF_ON_BUTTON
= 0x0001 // cursor is on dropbutton area
101 // Namespace for wxComboCtrl feature flags
102 struct wxComboCtrlFeatures
106 MovableButton
= 0x0001, // Button can be on either side of control
107 BitmapButton
= 0x0002, // Button may be replaced with bitmap
108 ButtonSpacing
= 0x0004, // Button can have spacing from the edge
110 TextIndent
= 0x0008, // SetTextIndent can be used
111 PaintControl
= 0x0010, // Combo control itself can be custom painted
112 PaintWritable
= 0x0020, // A variable-width area in front of writable
113 // combo control's textctrl can be custom
115 Borderless
= 0x0040, // wxNO_BORDER window style works
117 // There are no feature flags for...
118 // PushButtonBitmapBackground - if its in wxRendererNative, then it should be
119 // not an issue to have it automatically under the bitmap.
121 All
= MovableButton
|BitmapButton
|
122 ButtonSpacing
|TextIndent
|
123 PaintControl
|PaintWritable
|
129 class WXDLLEXPORT wxComboCtrlBase
: public wxControl
131 friend class wxComboPopup
;
134 wxComboCtrlBase() : wxControl() { Init(); }
136 bool Create(wxWindow
*parent
,
138 const wxString
& value
,
142 const wxValidator
& validator
,
143 const wxString
& name
);
145 virtual ~wxComboCtrlBase();
147 // show/hide popup window
148 virtual void ShowPopup();
149 virtual void HidePopup();
151 // Override for totally custom combo action
152 virtual void OnButtonClick();
154 // return true if the popup is currently shown
155 bool IsPopupShown() const { return m_isPopupShown
; }
157 // set interface class instance derived from wxComboPopup
158 // NULL popup can be used to indicate default in a derived class
159 void SetPopupControl( wxComboPopup
* popup
)
161 DoSetPopupControl(popup
);
164 // get interface class instance derived from wxComboPopup
165 wxComboPopup
* GetPopupControl()
167 EnsurePopupControl();
168 return m_popupInterface
;
171 // get the popup window containing the popup control
172 wxWindow
*GetPopupWindow() const { return m_winPopup
; }
174 // Get the text control which is part of the combobox.
175 wxTextCtrl
*GetTextCtrl() const { return m_text
; }
177 // get the dropdown button which is part of the combobox
178 // note: its not necessarily a wxButton or wxBitmapButton
179 wxWindow
*GetButton() const { return m_btn
; }
181 // forward these methods to all subcontrols
182 virtual bool Enable(bool enable
= true);
183 virtual bool Show(bool show
= true);
184 virtual bool SetFont(const wxFont
& font
);
186 // wxTextCtrl methods - for readonly combo they should return
188 virtual wxString
GetValue() const;
189 virtual void SetValue(const wxString
& value
);
192 virtual void Paste();
193 virtual void SetInsertionPoint(long pos
);
194 virtual void SetInsertionPointEnd();
195 virtual long GetInsertionPoint() const;
196 virtual long GetLastPosition() const;
197 virtual void Replace(long from
, long to
, const wxString
& value
);
198 virtual void Remove(long from
, long to
);
199 virtual void SetSelection(long from
, long to
);
202 // This method sets the text without affecting list selection
203 // (ie. wxComboPopup::SetStringValue doesn't get called).
204 void SetText(const wxString
& value
);
207 // Popup customization methods
210 // Sets minimum width of the popup. If wider than combo control, it will extend to the left.
212 // * Value -1 indicates the default.
213 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
214 void SetPopupMinWidth( int width
)
216 m_widthMinPopup
= width
;
219 // Sets preferred maximum height of the popup.
221 // * Value -1 indicates the default.
222 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
223 void SetPopupMaxHeight( int height
)
225 m_heightPopup
= height
;
228 // Extends popup size horizontally, relative to the edges of the combo control.
230 // * Popup minimum width may override extLeft (ie. it has higher precedence).
231 // * Values 0 indicate default.
232 // * Custom popup may not take this fully into account (wxOwnerDrawnComboBox takes).
233 void SetPopupExtents( int extLeft
, int extRight
)
236 m_extRight
= extRight
;
239 // Set width, in pixels, of custom paint area in writable combo.
240 // In read-only, used to indicate area that is not covered by the
241 // focus rectangle (which may or may not be drawn, depending on the
243 void SetCustomPaintWidth( int width
);
244 int GetCustomPaintWidth() const { return m_widthCustomPaint
; }
246 // Set side of the control to which the popup will align itself.
247 // Valid values are wxLEFT, wxRIGHT and 0. The default value 0 wmeans
248 // that the side of the button will be used.
249 void SetPopupAnchor( int anchorSide
)
251 m_anchorSide
= anchorSide
;
254 // Set position of dropdown button.
255 // width: 0 > for specific custom width, negative to adjust to smaller than default
256 // height: 0 > for specific custom height, negative to adjust to smaller than default
257 // side: wxLEFT or wxRIGHT, indicates on which side the button will be placed.
258 // spacingX: empty space on sides of the button. Default is 0.
260 // There is no spacingY - the button will be centered vertically.
261 void SetButtonPosition( int width
= 0,
268 // Sets dropbutton to be drawn with custom bitmaps.
270 // bmpNormal: drawn when cursor is not on button
271 // pushButtonBg: Draw push button background below the image.
272 // NOTE! This is usually only properly supported on platforms with appropriate
273 // method in wxRendererNative.
274 // bmpPressed: drawn when button is depressed
275 // bmpHover: drawn when cursor hovers on button. This is ignored on platforms
276 // that do not generally display hover differently.
277 // bmpDisabled: drawn when combobox is disabled.
278 void SetButtonBitmaps( const wxBitmap
& bmpNormal
,
279 bool pushButtonBg
= false,
280 const wxBitmap
& bmpPressed
= wxNullBitmap
,
281 const wxBitmap
& bmpHover
= wxNullBitmap
,
282 const wxBitmap
& bmpDisabled
= wxNullBitmap
);
285 // This will set the space in pixels between left edge of the control and the
286 // text, regardless whether control is read-only (ie. no wxTextCtrl) or not.
287 // Platform-specific default can be set with value-1.
289 // * This method may do nothing on some native implementations.
290 void SetTextIndent( int indent
);
292 // Returns actual indentation in pixels.
293 wxCoord
GetTextIndent() const
299 // Utilies needed by the popups or native implementations
302 // Draws focus background (on combo control) in a way typical on platform.
303 // Unless you plan to paint your own focus indicator, you should always call this
304 // in your wxComboPopup::PaintComboControl implementation.
305 // In addition, it sets pen and text colour to what looks good and proper
306 // against the background.
307 // flags: wxRendererNative flags: wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
308 // wxCONTROL_SELECTED: list item is selected
309 // wxCONTROL_DISABLED: control/item is disabled
310 virtual void DrawFocusBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const;
312 // Returns true if focus indicator should be drawn in the control.
313 bool ShouldDrawFocus() const
315 const wxWindow
* curFocus
= FindFocus();
316 return ( !m_isPopupShown
&&
317 (curFocus
== this || (m_btn
&& curFocus
== m_btn
)) &&
318 (m_windowStyle
& wxCB_READONLY
) );
321 // These methods return references to appropriate dropbutton bitmaps
322 const wxBitmap
& GetBitmapNormal() const { return m_bmpNormal
; }
323 const wxBitmap
& GetBitmapPressed() const { return m_bmpPressed
; }
324 const wxBitmap
& GetBitmapHover() const { return m_bmpHover
; }
325 const wxBitmap
& GetBitmapDisabled() const { return m_bmpDisabled
; }
327 // Return internal flags
328 wxUint32
GetInternalFlags() const { return m_iFlags
; }
330 // Return true if Create has finished
331 bool IsCreated() const { return m_iFlags
& wxCC_IFLAG_CREATED
? true : false; }
333 // common code to be called on popup hide/dismiss
334 void OnPopupDismiss();
339 // Override these for customization purposes
342 // called from wxSizeEvent handler
343 virtual void OnResize() = 0;
345 // Return native text identation (for pure text, not textctrl)
346 virtual wxCoord
GetNativeTextIndent() const;
348 // Called in syscolourchanged handler and base create
349 virtual void OnThemeChange();
351 // Creates wxTextCtrl.
352 // extraStyle: Extra style parameters
353 void CreateTextCtrl( int extraStyle
, const wxValidator
& validator
);
355 // Installs standard input handler to combo (and optionally to the textctrl)
356 void InstallInputHandlers( bool alsoTextCtrl
= true );
358 // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
359 void DrawButton( wxDC
& dc
, const wxRect
& rect
, bool paintBg
= true );
361 // Call if cursor is on button area or mouse is captured for the button.
362 //bool HandleButtonMouseEvent( wxMouseEvent& event, bool isInside );
363 bool HandleButtonMouseEvent( wxMouseEvent
& event
, int flags
);
365 // Conversion to double-clicks and some basic filtering
366 // returns true if event was consumed or filtered (event type is also set to 0 in this case)
367 //bool PreprocessMouseEvent( wxMouseEvent& event, bool isOnButtonArea );
368 bool PreprocessMouseEvent( wxMouseEvent
& event
, int flags
);
371 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
372 // If you need alternate behaviour, it is recommended you manipulate and filter events to it
373 // instead of building your own handling routine (for reference, on wxEVT_LEFT_DOWN it will
374 // toggle popup and on wxEVT_LEFT_DCLICK it will do the same or run the popup's dclick method,
375 // if defined - you should pass events of other types of it for common processing).
376 void HandleNormalMouseEvent( wxMouseEvent
& event
);
378 // Creates popup window, calls interface->Create(), etc
381 // Destroy popup window and all related constructs
384 // override the base class virtuals involved in geometry calculations
385 virtual wxSize
DoGetBestSize() const;
387 // NULL popup can be used to indicate default in a derived class
388 virtual void DoSetPopupControl(wxComboPopup
* popup
);
390 // ensures there is atleast the default popup
391 void EnsurePopupControl();
393 // Recalculates button and textctrl areas. Called when size or button setup change.
394 // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
396 void CalculateAreas( int btnWidth
= 0 );
398 // Standard textctrl positioning routine. Just give it platform-dependant
399 // textctrl coordinate adjustment.
400 void PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
);
403 void OnSizeEvent( wxSizeEvent
& event
);
404 void OnFocusEvent(wxFocusEvent
& event
);
405 void OnTextCtrlEvent(wxCommandEvent
& event
);
406 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
408 // Set customization flags (directs how wxComboCtrlBase helpers behave)
409 void Customize( wxUint32 flags
) { m_iFlags
|= flags
; }
411 // Dispatches size event and refreshes
412 void RecalcAndRefresh();
415 virtual void DoSetToolTip( wxToolTip
*tip
);
418 // Used by OnPaints of derived classes
419 wxBitmap
& GetBufferBitmap(const wxSize
& sz
) const;
421 // This is used when m_text is hidden (readonly).
422 wxString m_valueString
;
424 // the text control and button we show all the time
428 // wxPopupWindow or similar containing the window managed by the interface.
429 wxWindow
* m_winPopup
;
431 // the popup control/panel
435 wxComboPopup
* m_popupInterface
;
437 // this is for this control itself
438 wxEvtHandler
* m_extraEvtHandler
;
441 wxEvtHandler
* m_textEvtHandler
;
443 // this is for the top level window
444 wxEvtHandler
* m_toplevEvtHandler
;
446 // this is for the control in popup
447 wxEvtHandler
* m_popupExtraHandler
;
449 // needed for "instant" double-click handling
450 wxLongLong m_timeLastMouseUp
;
452 // used to prevent immediate re-popupping incase closed popup
453 // by clicking on the combo control (needed because of inconsistent
454 // transient implementation across platforms).
455 wxLongLong m_timeCanAcceptClick
;
457 // how much popup should expand to the left/right of the control
461 // minimum popup width
462 wxCoord m_widthMinPopup
;
464 // preferred popup height
465 wxCoord m_heightPopup
;
467 // how much of writable combo is custom-paint by callback?
468 // also used to indicate area that is not covered by "blue"
469 // selection indicator.
470 wxCoord m_widthCustomPaint
;
472 // absolute text indentation, in pixels
475 // side on which the popup is aligned
478 // Width of the "fake" border
479 wxCoord m_widthCustomBorder
;
481 // The button and textctrl click/paint areas
485 // current button state (uses renderer flags)
494 // last default button width
497 // custom dropbutton bitmaps
498 wxBitmap m_bmpNormal
;
499 wxBitmap m_bmpPressed
;
501 wxBitmap m_bmpDisabled
;
503 // area used by the button
506 // platform-dependant customization and other flags
509 // draw blank button background under bitmap?
510 bool m_blankButtonBg
;
512 // is the popup window currenty shown?
515 // Set to 1 on mouse down, 0 on mouse up. Used to eliminate down-less mouse ups.
521 DECLARE_EVENT_TABLE()
523 DECLARE_ABSTRACT_CLASS(wxComboCtrlBase
)
527 // ----------------------------------------------------------------------------
528 // wxComboPopup is the interface which must be implemented by a control to be
529 // used as a popup by wxComboCtrl
530 // ----------------------------------------------------------------------------
533 // wxComboPopup internal flags
536 wxCP_IFLAG_CREATED
= 0x0001 // Set by wxComboCtrlBase after Create is called
540 class WXDLLEXPORT wxComboPopup
542 friend class wxComboCtrlBase
;
546 m_combo
= (wxComboCtrlBase
*) NULL
;
550 // This is called immediately after construction finishes. m_combo member
551 // variable has been initialized before the call.
552 // NOTE: It is not in constructor so the derived class doesn't need to redefine
553 // a default constructor of its own.
554 virtual void Init() { };
556 virtual ~wxComboPopup();
558 // Create the popup child control.
559 // Return true for success.
560 virtual bool Create(wxWindow
* parent
) = 0;
562 // We must have an associated control which is subclassed by the combobox.
563 virtual wxWindow
*GetControl() = 0;
565 // Called immediately after the popup is shown
566 virtual void OnPopup();
568 // Called when popup is dismissed
569 virtual void OnDismiss();
571 // Called just prior to displaying popup.
572 // Default implementation does nothing.
573 virtual void SetStringValue( const wxString
& value
);
575 // Gets displayed string representation of the value.
576 virtual wxString
GetStringValue() const = 0;
578 // This is called to custom paint in the combo control itself (ie. not the popup).
579 // Default implementation draws value as string.
580 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
582 // Receives key events from the parent wxComboCtrl.
583 // Events not handled should be skipped, as usual.
584 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
586 // Implement if you need to support special action when user
587 // double-clicks on the parent wxComboCtrl.
588 virtual void OnComboDoubleClick();
590 // Return final size of popup. Called on every popup, just prior to OnShow.
591 // minWidth = preferred minimum width for window
592 // prefHeight = preferred height. Only applies if > 0,
593 // maxHeight = max height for window, as limited by screen size
594 // and should only be rounded down, if necessary.
595 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
597 // Return true if you want delay call to Create until the popup is shown
598 // for the first time. It is more efficient, but note that it is often
599 // more convenient to have the control created immediately.
600 // Default returns false.
601 virtual bool LazyCreate();
610 // Returns true if Create has been called.
611 bool IsCreated() const
613 return (m_iFlags
& wxCP_IFLAG_CREATED
) ? true : false;
616 // Default PaintComboControl behaviour
617 static void DefaultPaintComboControl( wxComboCtrlBase
* combo
,
619 const wxRect
& rect
);
622 wxComboCtrlBase
* m_combo
;
626 // Called in wxComboCtrlBase::SetPopupControl
627 void InitBase(wxComboCtrlBase
*combo
)
634 // ----------------------------------------------------------------------------
635 // include the platform-dependent header defining the real class
636 // ----------------------------------------------------------------------------
638 #if defined(__WXUNIVERSAL__)
639 // No native universal (but it must still be first in the list)
640 #elif defined(__WXMSW__)
641 #include "wx/msw/combo.h"
644 // Any ports may need generic as an alternative
645 #include "wx/generic/combo.h"
647 #endif // wxUSE_COMBOCTRL
650 // _WX_COMBOCONTROL_H_BASE_