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 // Dropbutton acts like standard push button.
62 wxCC_STD_BUTTON
= 0x0200
66 // wxComboCtrl internal flags
69 // First those that can be passed to Customize.
70 // It is Windows style for all flags to be clear.
72 // Button is preferred outside the border (GTK style)
73 wxCC_BUTTON_OUTSIDE_BORDER
= 0x0001,
74 // Show popup on mouse up instead of mouse down (which is the Windows style)
75 wxCC_POPUP_ON_MOUSE_UP
= 0x0002,
76 // All text is not automatically selected on click
77 wxCC_NO_TEXT_AUTO_SELECT
= 0x0004,
79 // Internal use: signals creation is complete
80 wxCC_IFLAG_CREATED
= 0x0100,
81 // Internal use: really put button outside
82 wxCC_IFLAG_BUTTON_OUTSIDE
= 0x0200,
83 // Internal use: SetTextIndent has been called
84 wxCC_IFLAG_INDENT_SET
= 0x0400,
85 // Internal use: Set wxTAB_TRAVERSAL to parent when popup is dismissed
86 wxCC_IFLAG_PARENT_TAB_TRAVERSAL
= 0x0800
90 // Flags used by PreprocessMouseEvent and HandleButtonMouseEvent
93 wxCC_MF_ON_BUTTON
= 0x0001 // cursor is on dropbutton area
97 // Namespace for wxComboCtrl feature flags
98 struct wxComboCtrlFeatures
102 MovableButton
= 0x0001, // Button can be on either side of control
103 BitmapButton
= 0x0002, // Button may be replaced with bitmap
104 ButtonSpacing
= 0x0004, // Button can have spacing from the edge
106 TextIndent
= 0x0008, // SetTextIndent can be used
107 PaintControl
= 0x0010, // Combo control itself can be custom painted
108 PaintWritable
= 0x0020, // A variable-width area in front of writable
109 // combo control's textctrl can be custom
111 Borderless
= 0x0040, // wxNO_BORDER window style works
113 // There are no feature flags for...
114 // PushButtonBitmapBackground - if its in wxRendererNative, then it should be
115 // not an issue to have it automatically under the bitmap.
117 All
= MovableButton
|BitmapButton
|
118 ButtonSpacing
|TextIndent
|
119 PaintControl
|PaintWritable
|
125 class WXDLLEXPORT wxComboCtrlBase
: public wxControl
127 friend class wxComboPopup
;
130 wxComboCtrlBase() : wxControl() { Init(); }
132 bool Create(wxWindow
*parent
,
134 const wxString
& value
,
138 const wxValidator
& validator
,
139 const wxString
& name
);
141 virtual ~wxComboCtrlBase();
143 // show/hide popup window
144 virtual void ShowPopup();
145 virtual void HidePopup();
147 // Override for totally custom combo action
148 virtual void OnButtonClick();
150 // return true if the popup is currently shown
151 bool IsPopupShown() const { return m_isPopupShown
; }
153 // set interface class instance derived from wxComboPopup
154 // NULL popup can be used to indicate default in a derived class
155 void SetPopupControl( wxComboPopup
* popup
)
157 DoSetPopupControl(popup
);
160 // get interface class instance derived from wxComboPopup
161 wxComboPopup
* GetPopupControl()
163 EnsurePopupControl();
164 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: button width. <= 0 for default.
252 // height: button height. <= 0 for 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
= -1,
262 // Returns current size of the dropdown button.
263 wxSize
GetButtonSize();
266 // Sets dropbutton to be drawn with custom bitmaps.
268 // bmpNormal: drawn when cursor is not on button
269 // pushButtonBg: Draw push button background below the image.
270 // NOTE! This is usually only properly supported on platforms with appropriate
271 // method in wxRendererNative.
272 // bmpPressed: drawn when button is depressed
273 // bmpHover: drawn when cursor hovers on button. This is ignored on platforms
274 // that do not generally display hover differently.
275 // bmpDisabled: drawn when combobox is disabled.
276 void SetButtonBitmaps( const wxBitmap
& bmpNormal
,
277 bool pushButtonBg
= false,
278 const wxBitmap
& bmpPressed
= wxNullBitmap
,
279 const wxBitmap
& bmpHover
= wxNullBitmap
,
280 const wxBitmap
& bmpDisabled
= wxNullBitmap
);
283 // This will set the space in pixels between left edge of the control and the
284 // text, regardless whether control is read-only (ie. no wxTextCtrl) or not.
285 // Platform-specific default can be set with value-1.
287 // * This method may do nothing on some native implementations.
288 void SetTextIndent( int indent
);
290 // Returns actual indentation in pixels.
291 wxCoord
GetTextIndent() const
297 // Utilies needed by the popups or native implementations
300 // Returns true if given key combination should toggle the popup.
301 // NB: This is a separate from other keyboard handling because:
302 // 1) Replaceability.
303 // 2) Centralized code (otherwise it'd be split up between
304 // wxComboCtrl key handler and wxVListBoxComboPopup's
306 virtual bool IsKeyPopupToggle(const wxKeyEvent
& event
) const = 0;
308 // Prepare background of combo control or an item in a dropdown list
309 // in a way typical on platform. This includes painting the focus/disabled
310 // background and setting the clipping region.
311 // Unless you plan to paint your own focus indicator, you should always call this
312 // in your wxComboPopup::PaintComboControl implementation.
313 // In addition, it sets pen and text colour to what looks good and proper
314 // against the background.
315 // flags: wxRendererNative flags: wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
316 // wxCONTROL_SELECTED: list item is selected
317 // wxCONTROL_DISABLED: control/item is disabled
318 virtual void PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const;
320 // Returns true if focus indicator should be drawn in the control.
321 bool ShouldDrawFocus() const
323 const wxWindow
* curFocus
= FindFocus();
324 return ( !m_isPopupShown
&&
325 (curFocus
== this || (m_btn
&& curFocus
== m_btn
)) &&
326 (m_windowStyle
& wxCB_READONLY
) );
329 // These methods return references to appropriate dropbutton bitmaps
330 const wxBitmap
& GetBitmapNormal() const { return m_bmpNormal
; }
331 const wxBitmap
& GetBitmapPressed() const { return m_bmpPressed
; }
332 const wxBitmap
& GetBitmapHover() const { return m_bmpHover
; }
333 const wxBitmap
& GetBitmapDisabled() const { return m_bmpDisabled
; }
335 // Return internal flags
336 wxUint32
GetInternalFlags() const { return m_iFlags
; }
338 // Return true if Create has finished
339 bool IsCreated() const { return m_iFlags
& wxCC_IFLAG_CREATED
? true : false; }
341 // common code to be called on popup hide/dismiss
342 void OnPopupDismiss();
347 // Override these for customization purposes
350 // called from wxSizeEvent handler
351 virtual void OnResize() = 0;
353 // Return native text identation (for pure text, not textctrl)
354 virtual wxCoord
GetNativeTextIndent() const;
356 // Called in syscolourchanged handler and base create
357 virtual void OnThemeChange();
359 // Creates wxTextCtrl.
360 // extraStyle: Extra style parameters
361 void CreateTextCtrl( int extraStyle
, const wxValidator
& validator
);
363 // Installs standard input handler to combo (and optionally to the textctrl)
364 void InstallInputHandlers();
366 // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
367 void DrawButton( wxDC
& dc
, const wxRect
& rect
, bool paintBg
= true );
369 // Call if cursor is on button area or mouse is captured for the button.
370 //bool HandleButtonMouseEvent( wxMouseEvent& event, bool isInside );
371 bool HandleButtonMouseEvent( wxMouseEvent
& event
, int flags
);
373 // Conversion to double-clicks and some basic filtering
374 // returns true if event was consumed or filtered (event type is also set to 0 in this case)
375 //bool PreprocessMouseEvent( wxMouseEvent& event, bool isOnButtonArea );
376 bool PreprocessMouseEvent( wxMouseEvent
& event
, int flags
);
379 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
380 // If you need alternate behaviour, it is recommended you manipulate and filter events to it
381 // instead of building your own handling routine (for reference, on wxEVT_LEFT_DOWN it will
382 // toggle popup and on wxEVT_LEFT_DCLICK it will do the same or run the popup's dclick method,
383 // if defined - you should pass events of other types of it for common processing).
384 void HandleNormalMouseEvent( wxMouseEvent
& event
);
386 // Creates popup window, calls interface->Create(), etc
389 // Destroy popup window and all related constructs
392 // override the base class virtuals involved in geometry calculations
393 virtual wxSize
DoGetBestSize() const;
395 // NULL popup can be used to indicate default in a derived class
396 virtual void DoSetPopupControl(wxComboPopup
* popup
);
398 // ensures there is atleast the default popup
399 void EnsurePopupControl();
401 // Recalculates button and textctrl areas. Called when size or button setup change.
402 // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
404 void CalculateAreas( int btnWidth
= 0 );
406 // Standard textctrl positioning routine. Just give it platform-dependant
407 // textctrl coordinate adjustment.
408 void PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
);
411 void OnSizeEvent( wxSizeEvent
& event
);
412 void OnFocusEvent(wxFocusEvent
& event
);
413 void OnTextCtrlEvent(wxCommandEvent
& event
);
414 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
415 void OnKeyEvent(wxKeyEvent
& event
);
417 // Set customization flags (directs how wxComboCtrlBase helpers behave)
418 void Customize( wxUint32 flags
) { m_iFlags
|= flags
; }
420 // Dispatches size event and refreshes
421 void RecalcAndRefresh();
424 virtual void DoSetToolTip( wxToolTip
*tip
);
427 // Used by OnPaints of derived classes
428 wxBitmap
& GetBufferBitmap(const wxSize
& sz
) const;
430 // This is used when m_text is hidden (readonly).
431 wxString m_valueString
;
433 // the text control and button we show all the time
437 // wxPopupWindow or similar containing the window managed by the interface.
438 wxWindow
* m_winPopup
;
440 // the popup control/panel
444 wxComboPopup
* m_popupInterface
;
446 // this is input etc. handler for the text control
447 wxEvtHandler
* m_textEvtHandler
;
449 // this is for the top level window
450 wxEvtHandler
* m_toplevEvtHandler
;
452 // this is for the control in popup
453 wxEvtHandler
* m_popupExtraHandler
;
455 // needed for "instant" double-click handling
456 wxLongLong m_timeLastMouseUp
;
458 // used to prevent immediate re-popupping incase closed popup
459 // by clicking on the combo control (needed because of inconsistent
460 // transient implementation across platforms).
461 wxLongLong m_timeCanAcceptClick
;
463 // how much popup should expand to the left/right of the control
467 // minimum popup width
468 wxCoord m_widthMinPopup
;
470 // preferred popup height
471 wxCoord m_heightPopup
;
473 // how much of writable combo is custom-paint by callback?
474 // also used to indicate area that is not covered by "blue"
475 // selection indicator.
476 wxCoord m_widthCustomPaint
;
478 // absolute text indentation, in pixels
481 // side on which the popup is aligned
484 // Width of the "fake" border
485 wxCoord m_widthCustomBorder
;
487 // The button and textctrl click/paint areas
491 // current button state (uses renderer flags)
500 // last default button width
503 // custom dropbutton bitmaps
504 wxBitmap m_bmpNormal
;
505 wxBitmap m_bmpPressed
;
507 wxBitmap m_bmpDisabled
;
509 // area used by the button
512 // platform-dependant customization and other flags
515 // draw blank button background under bitmap?
516 bool m_blankButtonBg
;
518 // is the popup window currenty shown?
521 // Set to 1 on mouse down, 0 on mouse up. Used to eliminate down-less mouse ups.
527 DECLARE_EVENT_TABLE()
529 DECLARE_ABSTRACT_CLASS(wxComboCtrlBase
)
533 // ----------------------------------------------------------------------------
534 // wxComboPopup is the interface which must be implemented by a control to be
535 // used as a popup by wxComboCtrl
536 // ----------------------------------------------------------------------------
539 // wxComboPopup internal flags
542 wxCP_IFLAG_CREATED
= 0x0001 // Set by wxComboCtrlBase after Create is called
546 class WXDLLEXPORT wxComboPopup
548 friend class wxComboCtrlBase
;
552 m_combo
= (wxComboCtrlBase
*) NULL
;
556 // This is called immediately after construction finishes. m_combo member
557 // variable has been initialized before the call.
558 // NOTE: It is not in constructor so the derived class doesn't need to redefine
559 // a default constructor of its own.
560 virtual void Init() { };
562 virtual ~wxComboPopup();
564 // Create the popup child control.
565 // Return true for success.
566 virtual bool Create(wxWindow
* parent
) = 0;
568 // We must have an associated control which is subclassed by the combobox.
569 virtual wxWindow
*GetControl() = 0;
571 // Called immediately after the popup is shown
572 virtual void OnPopup();
574 // Called when popup is dismissed
575 virtual void OnDismiss();
577 // Called just prior to displaying popup.
578 // Default implementation does nothing.
579 virtual void SetStringValue( const wxString
& value
);
581 // Gets displayed string representation of the value.
582 virtual wxString
GetStringValue() const = 0;
584 // This is called to custom paint in the combo control itself (ie. not the popup).
585 // Default implementation draws value as string.
586 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
588 // Receives key events from the parent wxComboCtrl.
589 // Events not handled should be skipped, as usual.
590 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
592 // Implement if you need to support special action when user
593 // double-clicks on the parent wxComboCtrl.
594 virtual void OnComboDoubleClick();
596 // Return final size of popup. Called on every popup, just prior to OnShow.
597 // minWidth = preferred minimum width for window
598 // prefHeight = preferred height. Only applies if > 0,
599 // maxHeight = max height for window, as limited by screen size
600 // and should only be rounded down, if necessary.
601 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
603 // Return true if you want delay call to Create until the popup is shown
604 // for the first time. It is more efficient, but note that it is often
605 // more convenient to have the control created immediately.
606 // Default returns false.
607 virtual bool LazyCreate();
616 // Returns true if Create has been called.
617 bool IsCreated() const
619 return (m_iFlags
& wxCP_IFLAG_CREATED
) ? true : false;
622 // Default PaintComboControl behaviour
623 static void DefaultPaintComboControl( wxComboCtrlBase
* combo
,
625 const wxRect
& rect
);
628 wxComboCtrlBase
* m_combo
;
632 // Called in wxComboCtrlBase::SetPopupControl
633 void InitBase(wxComboCtrlBase
*combo
)
640 // ----------------------------------------------------------------------------
641 // include the platform-dependent header defining the real class
642 // ----------------------------------------------------------------------------
644 #if defined(__WXUNIVERSAL__)
645 // No native universal (but it must still be first in the list)
646 #elif defined(__WXMSW__)
647 #include "wx/msw/combo.h"
650 // Any ports may need generic as an alternative
651 #include "wx/generic/combo.h"
653 #endif // wxUSE_COMBOCTRL
656 // _WX_COMBOCONTROL_H_BASE_