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.
46 #include "wx/textctrl.h"
47 #include "wx/button.h"
48 #include "wx/combobox.h"
49 #include "wx/renderer.h" // this is needed for wxCONTROL_XXX flags
50 #include "wx/bitmap.h" // wxBitmap used by-value
53 class WXDLLEXPORT wxComboPopup
;
56 // New window styles for wxComboCtrlBase
60 // Double-clicking a read-only combo triggers call to popup's OnComboPopup.
61 // In wxOwnerDrawnComboBox, for instance, it cycles item.
62 wxCC_SPECIAL_DCLICK
= 0x0100,
64 // Use keyboard behaviour alternate to platform default:
65 // Up an down keys will show popup instead of cycling value.
66 wxCC_ALT_KEYS
= 0x0200,
68 // Dropbutton acts like standard push button.
69 wxCC_STD_BUTTON
= 0x0400
73 // wxComboCtrl internal flags
76 // First those that can be passed to Customize.
77 // It is Windows style for all flags to be clear.
79 // Button is preferred outside the border (GTK style)
80 wxCC_BUTTON_OUTSIDE_BORDER
= 0x0001,
81 // Show popup on mouse up instead of mouse down (which is the Windows style)
82 wxCC_POPUP_ON_MOUSE_UP
= 0x0002,
83 // All text is not automatically selected on click
84 wxCC_NO_TEXT_AUTO_SELECT
= 0x0004,
86 // Internal use: signals creation is complete
87 wxCC_IFLAG_CREATED
= 0x0100,
88 // Internal use: really put button outside
89 wxCC_IFLAG_BUTTON_OUTSIDE
= 0x0200,
90 // Internal use: SetTextIndent has been called
91 wxCC_IFLAG_INDENT_SET
= 0x0400,
92 // Internal use: Set wxTAB_TRAVERSAL to parent when popup is dismissed
93 wxCC_IFLAG_PARENT_TAB_TRAVERSAL
= 0x0800
97 // Flags used by PreprocessMouseEvent and HandleButtonMouseEvent
100 wxCC_MF_ON_BUTTON
= 0x0001 // cursor is on dropbutton area
104 // Namespace for wxComboCtrl feature flags
105 struct wxComboCtrlFeatures
109 MovableButton
= 0x0001, // Button can be on either side of control
110 BitmapButton
= 0x0002, // Button may be replaced with bitmap
111 ButtonSpacing
= 0x0004, // Button can have spacing from the edge
113 TextIndent
= 0x0008, // SetTextIndent can be used
114 PaintControl
= 0x0010, // Combo control itself can be custom painted
115 PaintWritable
= 0x0020, // A variable-width area in front of writable
116 // combo control's textctrl can be custom
118 Borderless
= 0x0040, // wxNO_BORDER window style works
120 // There are no feature flags for...
121 // PushButtonBitmapBackground - if its in wxRendererNative, then it should be
122 // not an issue to have it automatically under the bitmap.
124 All
= MovableButton
|BitmapButton
|
125 ButtonSpacing
|TextIndent
|
126 PaintControl
|PaintWritable
|
132 class WXDLLEXPORT wxComboCtrlBase
: public wxControl
134 friend class wxComboPopup
;
137 wxComboCtrlBase() : wxControl() { Init(); }
139 bool Create(wxWindow
*parent
,
141 const wxString
& value
,
145 const wxValidator
& validator
,
146 const wxString
& name
);
148 virtual ~wxComboCtrlBase();
150 // show/hide popup window
151 virtual void ShowPopup();
152 virtual void HidePopup();
154 // Override for totally custom combo action
155 virtual void OnButtonClick();
157 // return true if the popup is currently shown
158 bool IsPopupShown() const { return m_isPopupShown
; }
160 // set interface class instance derived from wxComboPopup
161 // NULL popup can be used to indicate default in a derived class
162 virtual void SetPopupControl( wxComboPopup
* popup
);
164 // get interface class instance derived from wxComboPopup
165 wxComboPopup
* GetPopupControl() const { return m_popupInterface
; }
167 // get the popup window containing the popup control
168 wxWindow
*GetPopupWindow() const { return m_winPopup
; }
170 // Get the text control which is part of the combobox.
171 wxTextCtrl
*GetTextCtrl() const { return m_text
; }
173 // get the dropdown button which is part of the combobox
174 // note: its not necessarily a wxButton or wxBitmapButton
175 wxWindow
*GetButton() const { return m_btn
; }
177 // forward these methods to all subcontrols
178 virtual bool Enable(bool enable
= true);
179 virtual bool Show(bool show
= true);
180 virtual bool SetFont(const wxFont
& font
);
182 // wxTextCtrl methods - for readonly combo they should return
184 virtual wxString
GetValue() const;
185 virtual void SetValue(const wxString
& value
);
188 virtual void Paste();
189 virtual void SetInsertionPoint(long pos
);
190 virtual void SetInsertionPointEnd();
191 virtual long GetInsertionPoint() const;
192 virtual long GetLastPosition() const;
193 virtual void Replace(long from
, long to
, const wxString
& value
);
194 virtual void Remove(long from
, long to
);
195 virtual void SetSelection(long from
, long to
);
198 // This method sets the text without affecting list selection
199 // (ie. wxComboPopup::SetStringValue doesn't get called).
200 void SetText(const wxString
& value
);
203 // Popup customization methods
206 // Sets minimum width of the popup. If wider than combo control, it will extend to the left.
208 // * Value -1 indicates the default.
209 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
210 void SetPopupMinWidth( int width
)
212 m_widthMinPopup
= width
;
215 // Sets preferred maximum height of the popup.
217 // * Value -1 indicates the default.
218 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
219 void SetPopupMaxHeight( int height
)
221 m_heightPopup
= height
;
224 // Extends popup size horizontally, relative to the edges of the combo control.
226 // * Popup minimum width may override extLeft (ie. it has higher precedence).
227 // * Values 0 indicate default.
228 // * Custom popup may not take this fully into account (wxOwnerDrawnComboBox takes).
229 void SetPopupExtents( int extLeft
, int extRight
)
232 m_extRight
= extRight
;
235 // Set width, in pixels, of custom paint area in writable combo.
236 // In read-only, used to indicate area that is not covered by the
237 // focus rectangle (which may or may not be drawn, depending on the
239 void SetCustomPaintWidth( int width
);
240 int GetCustomPaintWidth() const { return m_widthCustomPaint
; }
242 // Set side of the control to which the popup will align itself.
243 // Valid values are wxLEFT, wxRIGHT and 0. The default value 0 wmeans
244 // that the side of the button will be used.
245 void SetPopupAnchor( int anchorSide
)
247 m_anchorSide
= anchorSide
;
250 // Set position of dropdown button.
251 // width: 0 > for specific custom width, negative to adjust to smaller than default
252 // height: 0 > for specific custom height, negative to adjust to smaller than default
253 // side: wxLEFT or wxRIGHT, indicates on which side the button will be placed.
254 // spacingX: empty space on sides of the button. Default is 0.
256 // There is no spacingY - the button will be centered vertically.
257 void SetButtonPosition( int width
= 0,
264 // Sets dropbutton to be drawn with custom bitmaps.
266 // bmpNormal: drawn when cursor is not on button
267 // pushButtonBg: Draw push button background below the image.
268 // NOTE! This is usually only properly supported on platforms with appropriate
269 // method in wxRendererNative.
270 // bmpPressed: drawn when button is depressed
271 // bmpHover: drawn when cursor hovers on button. This is ignored on platforms
272 // that do not generally display hover differently.
273 // bmpDisabled: drawn when combobox is disabled.
274 void SetButtonBitmaps( const wxBitmap
& bmpNormal
,
275 bool pushButtonBg
= false,
276 const wxBitmap
& bmpPressed
= wxNullBitmap
,
277 const wxBitmap
& bmpHover
= wxNullBitmap
,
278 const wxBitmap
& bmpDisabled
= wxNullBitmap
);
281 // This will set the space in pixels between left edge of the control and the
282 // text, regardless whether control is read-only (ie. no wxTextCtrl) or not.
283 // Platform-specific default can be set with value-1.
285 // * This method may do nothing on some native implementations.
286 void SetTextIndent( int indent
);
288 // Returns actual indentation in pixels.
289 wxCoord
GetTextIndent() const
295 // Utilies needed by the popups or native implementations
298 // Draws focus background (on combo control) in a way typical on platform.
299 // Unless you plan to paint your own focus indicator, you should always call this
300 // in your wxComboPopup::PaintComboControl implementation.
301 // In addition, it sets pen and text colour to what looks good and proper
302 // against the background.
303 // flags: wxRendererNative flags: wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
304 // wxCONTROL_SELECTED: list item is selected
305 // wxCONTROL_DISABLED: control/item is disabled
306 virtual void DrawFocusBackground( wxDC
& dc
, const wxRect
& rect
, int flags
);
308 // Returns true if focus indicator should be drawn in the control.
309 bool ShouldDrawFocus() const
311 const wxWindow
* curFocus
= FindFocus();
312 return ( !m_isPopupShown
&&
313 (curFocus
== this || (m_btn
&& curFocus
== m_btn
)) &&
314 (m_windowStyle
& wxCB_READONLY
) );
317 // These methods return references to appropriate dropbutton bitmaps
318 const wxBitmap
& GetBitmapNormal() const { return m_bmpNormal
; }
319 const wxBitmap
& GetBitmapPressed() const { return m_bmpPressed
; }
320 const wxBitmap
& GetBitmapHover() const { return m_bmpHover
; }
321 const wxBitmap
& GetBitmapDisabled() const { return m_bmpDisabled
; }
323 // Return internal flags
324 wxUint32
GetInternalFlags() const { return m_iFlags
; }
326 // Return true if Create has finished
327 bool IsCreated() const { return m_iFlags
& wxCC_IFLAG_CREATED ?
true : false; }
329 // common code to be called on popup hide/dismiss
330 void OnPopupDismiss();
335 // Override these for customization purposes
338 // called from wxSizeEvent handler
339 virtual void OnResize() = 0;
341 // Return native text identation (for pure text, not textctrl)
342 virtual wxCoord
GetNativeTextIndent() const;
344 // Called in syscolourchanged handler and base create
345 virtual void OnThemeChange();
347 // Creates wxTextCtrl.
348 // extraStyle: Extra style parameters
349 void CreateTextCtrl( int extraStyle
, const wxValidator
& validator
);
351 // Installs standard input handler to combo (and optionally to the textctrl)
352 void InstallInputHandlers( bool alsoTextCtrl
= true );
354 // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
355 void DrawButton( wxDC
& dc
, const wxRect
& rect
, bool paintBg
= true );
357 // Call if cursor is on button area or mouse is captured for the button.
358 //bool HandleButtonMouseEvent( wxMouseEvent& event, bool isInside );
359 bool HandleButtonMouseEvent( wxMouseEvent
& event
, int flags
);
361 // Conversion to double-clicks and some basic filtering
362 // returns true if event was consumed or filtered (event type is also set to 0 in this case)
363 //bool PreprocessMouseEvent( wxMouseEvent& event, bool isOnButtonArea );
364 bool PreprocessMouseEvent( wxMouseEvent
& event
, int flags
);
367 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
368 // If you need alternate behaviour, it is recommended you manipulate and filter events to it
369 // instead of building your own handling routine (for reference, on wxEVT_LEFT_DOWN it will
370 // toggle popup and on wxEVT_LEFT_DCLICK it will do the same or run the popup's dclick method,
371 // if defined - you should pass events of other types of it for common processing).
372 void HandleNormalMouseEvent( wxMouseEvent
& event
);
374 // Creates popup window, calls interface->Create(), etc
377 // override the base class virtuals involved in geometry calculations
378 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
379 virtual wxSize
DoGetBestSize() const;
381 // ensures there is atleast the default popup
382 void EnsurePopupControl();
384 // Recalculates button and textctrl areas. Called when size or button setup change.
385 // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
387 void CalculateAreas( int btnWidth
= 0 );
389 // Standard textctrl positioning routine. Just give it platform-dependant
390 // textctrl coordinate adjustment.
391 void PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
);
394 void OnSizeEvent( wxSizeEvent
& event
);
395 void OnFocusEvent(wxFocusEvent
& event
);
396 void OnTextCtrlEvent(wxCommandEvent
& event
);
397 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
399 // Set customization flags (directs how wxComboCtrlBase helpers behave)
400 void Customize( wxUint32 flags
) { m_iFlags
|= flags
; }
402 // Dispatches size event and refreshes
403 void RecalcAndRefresh();
406 virtual void DoSetToolTip( wxToolTip
*tip
);
409 // Used by OnPaints of derived classes
410 wxBitmap
& GetBufferBitmap(const wxSize
& sz
) const;
412 // This is used when m_text is hidden (readonly).
413 wxString m_valueString
;
415 // the text control and button we show all the time
419 // wxPopupWindow or similar containing the window managed by the interface.
420 wxWindow
* m_winPopup
;
422 // the popup control/panel
426 wxComboPopup
* m_popupInterface
;
428 // this is for this control itself
429 wxEvtHandler
* m_extraEvtHandler
;
432 wxEvtHandler
* m_textEvtHandler
;
434 // this is for the top level window
435 wxEvtHandler
* m_toplevEvtHandler
;
437 // this is for the control in popup
438 wxEvtHandler
* m_popupExtraHandler
;
440 // needed for "instant" double-click handling
441 wxLongLong m_timeLastMouseUp
;
443 // used to prevent immediate re-popupping incase closed popup
444 // by clicking on the combo control (needed because of inconsistent
445 // transient implementation across platforms).
446 wxLongLong m_timeCanAcceptClick
;
448 // how much popup should expand to the left/right of the control
452 // minimum popup width
453 wxCoord m_widthMinPopup
;
455 // preferred popup height
456 wxCoord m_heightPopup
;
458 // how much of writable combo is custom-paint by callback?
459 // also used to indicate area that is not covered by "blue"
460 // selection indicator.
461 wxCoord m_widthCustomPaint
;
463 // absolute text indentation, in pixels
466 // side on which the popup is aligned
469 // Width of the "fake" border
470 wxCoord m_widthCustomBorder
;
472 // The button and textctrl click/paint areas
476 // current button state (uses renderer flags)
485 // last default button width
488 // custom dropbutton bitmaps
489 wxBitmap m_bmpNormal
;
490 wxBitmap m_bmpPressed
;
492 wxBitmap m_bmpDisabled
;
494 // area used by the button
497 // platform-dependant customization and other flags
500 // draw blank button background under bitmap?
501 bool m_blankButtonBg
;
503 // is the popup window currenty shown?
506 // Set to 1 on mouse down, 0 on mouse up. Used to eliminate down-less mouse ups.
512 DECLARE_EVENT_TABLE()
514 DECLARE_ABSTRACT_CLASS(wxComboCtrlBase
)
518 // ----------------------------------------------------------------------------
519 // wxComboPopup is the interface which must be implemented by a control to be
520 // used as a popup by wxComboCtrl
521 // ----------------------------------------------------------------------------
524 // wxComboPopup internal flags
527 wxCP_IFLAG_CREATED
= 0x0001 // Set by wxComboCtrlBase after Create is called
531 class WXDLLEXPORT wxComboPopup
533 friend class wxComboCtrlBase
;
537 m_combo
= (wxComboCtrlBase
*) NULL
;
541 // This is called immediately after construction finishes. m_combo member
542 // variable has been initialized before the call.
543 // NOTE: It is not in constructor so the derived class doesn't need to redefine
544 // a default constructor of its own.
545 virtual void Init() { };
547 virtual ~wxComboPopup();
549 // Create the popup child control.
550 // Return true for success.
551 virtual bool Create(wxWindow
* parent
) = 0;
553 // We must have an associated control which is subclassed by the combobox.
554 virtual wxWindow
*GetControl() = 0;
556 // Called immediately after the popup is shown
557 virtual void OnPopup();
559 // Called when popup is dismissed
560 virtual void OnDismiss();
562 // Called just prior to displaying popup.
563 // Default implementation does nothing.
564 virtual void SetStringValue( const wxString
& value
);
566 // Gets displayed string representation of the value.
567 virtual wxString
GetStringValue() const = 0;
569 // This is called to custom paint in the combo control itself (ie. not the popup).
570 // Default implementation draws value as string.
571 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
573 // Receives key events from the parent wxComboCtrl.
574 // Events not handled should be skipped, as usual.
575 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
577 // Implement if you need to support special action when user
578 // double-clicks on the parent wxComboCtrl.
579 virtual void OnComboDoubleClick();
581 // Return final size of popup. Called on every popup, just prior to OnShow.
582 // minWidth = preferred minimum width for window
583 // prefHeight = preferred height. Only applies if > 0,
584 // maxHeight = max height for window, as limited by screen size
585 // and should only be rounded down, if necessary.
586 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
588 // Return true if you want delay call to Create until the popup is shown
589 // for the first time. It is more efficient, but note that it is often
590 // more convenient to have the control created immediately.
591 // Default returns false.
592 virtual bool LazyCreate();
601 // Returns true if Create has been called.
602 bool IsCreated() const
604 return (m_iFlags
& wxCP_IFLAG_CREATED
) ?
true : false;
607 // Default PaintComboControl behaviour
608 static void DefaultPaintComboControl( wxComboCtrlBase
* combo
,
610 const wxRect
& rect
);
613 wxComboCtrlBase
* m_combo
;
617 // Called in wxComboCtrlBase::SetPopupControl
618 void InitBase(wxComboCtrlBase
*combo
)
625 // ----------------------------------------------------------------------------
626 // include the platform-dependent header defining the real class
627 // ----------------------------------------------------------------------------
629 #if defined(__WXUNIVERSAL__)
630 // No native universal (but it must still be first in the list)
631 #elif defined(__WXMSW__)
632 #include "wx/msw/combo.h"
635 // Any ports may need generic as an alternative
636 #include "wx/generic/combo.h"
638 #endif // wxUSE_COMBOCTRL
641 // _WX_COMBOCONTROL_H_BASE_