1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboControl 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 wxComboControl 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 wxComboControl which can use any control as pop down
27 list and wxComboBox deriving from it which implements the standard wxWidgets
28 combobox API. wxComboControl 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.
43 #if wxUSE_COMBOCONTROL
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
52 class WXDLLEXPORT wxComboPopup
;
55 // New window styles for wxComboControlBase
59 // Double-clicking a read-only combo triggers call to popup's OnComboPopup.
60 // In wxOwnerDrawnComboBox, for instance, it cycles item.
61 wxCC_SPECIAL_DCLICK
= 0x0100,
63 // Use keyboard behaviour alternate to platform default:
64 // Up an down keys will show popup instead of cycling value.
65 wxCC_ALT_KEYS
= 0x0200,
67 // Dropbutton acts like standard push button.
68 wxCC_STD_BUTTON
= 0x0400
72 // wxComboControl internal flags
75 // First those that can be passed to Customize.
76 // It is Windows style for all flags to be clear.
78 // Button is preferred outside the border (GTK style)
79 wxCC_BUTTON_OUTSIDE_BORDER
= 0x0001,
80 // Show popup on mouse up instead of mouse down (which is the Windows style)
81 wxCC_POPUP_ON_MOUSE_UP
= 0x0002,
82 // All text is not automatically selected on click
83 wxCC_NO_TEXT_AUTO_SELECT
= 0x0004,
85 // Internal use: signals creation is complete
86 wxCC_IFLAG_CREATED
= 0x0100,
87 // Internal use: really put button outside
88 wxCC_IFLAG_BUTTON_OUTSIDE
= 0x0200,
89 // Internal use: SetTextIndent has been called
90 wxCC_IFLAG_INDENT_SET
= 0x0400,
91 // Internal use: Set wxTAB_TRAVERSAL to parent when popup is dismissed
92 wxCC_IFLAG_PARENT_TAB_TRAVERSAL
= 0x0800
96 // Flags used by PreprocessMouseEvent and HandleButtonMouseEvent
99 wxCC_MF_ON_BUTTON
= 0x0001 // cursor is on dropbutton area
103 // Namespace for wxComboControl feature flags
104 struct wxComboControlFeatures
108 MovableButton
= 0x0001, // Button can be on either side of control
109 BitmapButton
= 0x0002, // Button may be replaced with bitmap
110 ButtonSpacing
= 0x0004, // Button can have spacing from the edge
112 TextIndent
= 0x0008, // SetTextIndent can be used
113 PaintControl
= 0x0010, // Combo control itself can be custom painted
114 PaintWritable
= 0x0020, // A variable-width area in front of writable
115 // combo control's textctrl can be custom
117 Borderless
= 0x0040, // wxNO_BORDER window style works
119 // There are no feature flags for...
120 // PushButtonBitmapBackground - if its in wxRendererNative, then it should be
121 // not an issue to have it automatically under the bitmap.
123 All
= MovableButton
|BitmapButton
|
124 ButtonSpacing
|TextIndent
|
125 PaintControl
|PaintWritable
|
131 class WXDLLEXPORT wxComboControlBase
: public wxControl
133 friend class wxComboPopup
;
136 wxComboControlBase() : wxControl() { Init(); }
138 bool Create(wxWindow
*parent
,
140 const wxString
& value
,
144 const wxValidator
& validator
,
145 const wxString
& name
);
147 virtual ~wxComboControlBase();
149 // show/hide popup window
150 virtual void ShowPopup();
151 virtual void HidePopup();
153 // Override for totally custom combo action
154 virtual void OnButtonClick();
156 // return true if the popup is currently shown
157 bool IsPopupShown() const { return m_isPopupShown
; }
159 // set interface class instance derived from wxComboPopup
160 // NULL popup can be used to indicate default in a derived class
161 virtual void SetPopupControl( wxComboPopup
* popup
);
163 // get interface class instance derived from wxComboPopup
164 wxComboPopup
* GetPopupControl() const { return m_popupInterface
; }
166 // get the popup window containing the popup control
167 wxWindow
*GetPopupWindow() const { return m_winPopup
; }
169 // Get the text control which is part of the combobox.
170 wxTextCtrl
*GetTextCtrl() const { return m_text
; }
172 // get the dropdown button which is part of the combobox
173 // note: its not necessarily a wxButton or wxBitmapButton
174 wxWindow
*GetButton() const { return m_btn
; }
176 // forward these methods to all subcontrols
177 virtual bool Enable(bool enable
= true);
178 virtual bool Show(bool show
= true);
179 virtual bool SetFont(const wxFont
& font
);
181 // wxTextCtrl methods - for readonly combo they should return
183 virtual wxString
GetValue() const;
184 virtual void SetValue(const wxString
& value
);
187 virtual void Paste();
188 virtual void SetInsertionPoint(long pos
);
189 virtual void SetInsertionPointEnd();
190 virtual long GetInsertionPoint() const;
191 virtual long GetLastPosition() const;
192 virtual void Replace(long from
, long to
, const wxString
& value
);
193 virtual void Remove(long from
, long to
);
194 virtual void SetSelection(long from
, long to
);
197 // This method sets the text without affecting list selection
198 // (ie. wxComboPopup::SetStringValue doesn't get called).
199 void SetText(const wxString
& value
);
202 // Popup customization methods
205 // Sets minimum width of the popup. If wider than combo control, it will extend to the left.
207 // * Value -1 indicates the default.
208 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
209 void SetPopupMinWidth( int width
)
211 m_widthMinPopup
= width
;
214 // Sets preferred maximum height of the popup.
216 // * Value -1 indicates the default.
217 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
218 void SetPopupMaxHeight( int height
)
220 m_heightPopup
= height
;
223 // Extends popup size horizontally, relative to the edges of the combo control.
225 // * Popup minimum width may override extLeft (ie. it has higher precedence).
226 // * Values 0 indicate default.
227 // * Custom popup may not take this fully into account (wxOwnerDrawnComboBox takes).
228 void SetPopupExtents( int extLeft
, int extRight
)
231 m_extRight
= extRight
;
234 // Set width, in pixels, of custom paint area in writable combo.
235 // In read-only, used to indicate area that is not covered by the
236 // focus rectangle (which may or may not be drawn, depending on the
238 void SetCustomPaintWidth( int width
);
239 int GetCustomPaintWidth() const { return m_widthCustomPaint
; }
241 // Set side of the control to which the popup will align itself.
242 // Valid values are wxLEFT, wxRIGHT and 0. The default value 0 wmeans
243 // that the side of the button will be used.
244 void SetPopupAnchor( int anchorSide
)
246 m_anchorSide
= anchorSide
;
249 // Set position of dropdown button.
250 // width: 0 > for specific custom width, negative to adjust to smaller than default
251 // height: 0 > for specific custom height, negative to adjust to smaller than default
252 // side: wxLEFT or wxRIGHT, indicates on which side the button will be placed.
253 // spacingX: empty space on sides of the button. Default is 0.
255 // There is no spacingY - the button will be centered vertically.
256 void SetButtonPosition( int width
= 0,
263 // Sets dropbutton to be drawn with custom bitmaps.
265 // bmpNormal: drawn when cursor is not on button
266 // pushButtonBg: Draw push button background below the image.
267 // NOTE! This is usually only properly supported on platforms with appropriate
268 // method in wxRendererNative.
269 // bmpPressed: drawn when button is depressed
270 // bmpHover: drawn when cursor hovers on button. This is ignored on platforms
271 // that do not generally display hover differently.
272 // bmpDisabled: drawn when combobox is disabled.
273 void SetButtonBitmaps( const wxBitmap
& bmpNormal
,
274 bool pushButtonBg
= false,
275 const wxBitmap
& bmpPressed
= wxNullBitmap
,
276 const wxBitmap
& bmpHover
= wxNullBitmap
,
277 const wxBitmap
& bmpDisabled
= wxNullBitmap
);
280 // This will set the space in pixels between left edge of the control and the
281 // text, regardless whether control is read-only (ie. no wxTextCtrl) or not.
282 // Platform-specific default can be set with value-1.
284 // * This method may do nothing on some native implementations.
285 void SetTextIndent( int indent
);
287 // Returns actual indentation in pixels.
288 wxCoord
GetTextIndent() const
294 // Utilies needed by the popups or native implementations
297 // Draws focus background (on combo control) in a way typical on platform.
298 // Unless you plan to paint your own focus indicator, you should always call this
299 // in your wxComboPopup::PaintComboControl implementation.
300 // In addition, it sets pen and text colour to what looks good and proper
301 // against the background.
302 // flags: wxRendererNative flags: wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
303 // wxCONTROL_SELECTED: list item is selected
304 // wxCONTROL_DISABLED: control/item is disabled
305 virtual void DrawFocusBackground( wxDC
& dc
, const wxRect
& rect
, int flags
);
307 // Returns true if focus indicator should be drawn in the control.
308 bool ShouldDrawFocus() const
310 const wxWindow
* curFocus
= FindFocus();
311 return ( !m_isPopupShown
&&
312 (curFocus
== this || (m_btn
&& curFocus
== m_btn
)) &&
313 (m_windowStyle
& wxCB_READONLY
) );
316 // These methods return references to appropriate dropbutton bitmaps
317 const wxBitmap
& GetBitmapNormal() const { return m_bmpNormal
; }
318 const wxBitmap
& GetBitmapPressed() const { return m_bmpPressed
; }
319 const wxBitmap
& GetBitmapHover() const { return m_bmpHover
; }
320 const wxBitmap
& GetBitmapDisabled() const { return m_bmpDisabled
; }
322 // Return internal flags
323 wxUint32
GetInternalFlags() const { return m_iFlags
; }
325 // Return true if Create has finished
326 bool IsCreated() const { return m_iFlags
& wxCC_IFLAG_CREATED
? true : false; }
328 // common code to be called on popup hide/dismiss
329 void OnPopupDismiss();
334 // Override these for customization purposes
337 // called from wxSizeEvent handler
338 virtual void OnResize() = 0;
340 // Return native text identation (for pure text, not textctrl)
341 virtual wxCoord
GetNativeTextIndent() const;
343 // Called in syscolourchanged handler and base create
344 virtual void OnThemeChange();
346 // Creates wxTextCtrl.
347 // extraStyle: Extra style parameters
348 void CreateTextCtrl( int extraStyle
, const wxValidator
& validator
);
350 // Installs standard input handler to combo (and optionally to the textctrl)
351 void InstallInputHandlers( bool alsoTextCtrl
= true );
353 // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
354 void DrawButton( wxDC
& dc
, const wxRect
& rect
, bool paintBg
= true );
356 // Call if cursor is on button area or mouse is captured for the button.
357 //bool HandleButtonMouseEvent( wxMouseEvent& event, bool isInside );
358 bool HandleButtonMouseEvent( wxMouseEvent
& event
, int flags
);
360 // Conversion to double-clicks and some basic filtering
361 // returns true if event was consumed or filtered (event type is also set to 0 in this case)
362 //bool PreprocessMouseEvent( wxMouseEvent& event, bool isOnButtonArea );
363 bool PreprocessMouseEvent( wxMouseEvent
& event
, int flags
);
366 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
367 // If you need alternate behaviour, it is recommended you manipulate and filter events to it
368 // instead of building your own handling routine (for reference, on wxEVT_LEFT_DOWN it will
369 // toggle popup and on wxEVT_LEFT_DCLICK it will do the same or run the popup's dclick method,
370 // if defined - you should pass events of other types of it for common processing).
371 void HandleNormalMouseEvent( wxMouseEvent
& event
);
373 // Creates popup window, calls interface->Create(), etc
376 // override the base class virtuals involved in geometry calculations
377 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
378 virtual wxSize
DoGetBestSize() const;
380 // ensures there is atleast the default popup
381 void EnsurePopupControl();
383 // Recalculates button and textctrl areas. Called when size or button setup change.
384 // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
386 void CalculateAreas( int btnWidth
= 0 );
388 // Standard textctrl positioning routine. Just give it platform-dependant
389 // textctrl coordinate adjustment.
390 void PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
);
393 void OnSizeEvent( wxSizeEvent
& event
);
394 void OnFocusEvent(wxFocusEvent
& event
);
395 void OnTextCtrlEvent(wxCommandEvent
& event
);
396 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
398 // Set customization flags (directs how wxComboControlBase helpers behave)
399 void Customize( wxUint32 flags
) { m_iFlags
|= flags
; }
401 // Dispatches size event and refreshes
402 void RecalcAndRefresh();
405 virtual void DoSetToolTip( wxToolTip
*tip
);
408 // Used by OnPaints of derived classes
409 wxBitmap
& GetBufferBitmap(const wxSize
& sz
) const;
411 // This is used when m_text is hidden (readonly).
412 wxString m_valueString
;
414 // the text control and button we show all the time
418 // wxPopupWindow or similar containing the window managed by the interface.
419 wxWindow
* m_winPopup
;
421 // the popup control/panel
425 wxComboPopup
* m_popupInterface
;
427 // this is for this control itself
428 wxEvtHandler
* m_extraEvtHandler
;
431 wxEvtHandler
* m_textEvtHandler
;
433 // this is for the top level window
434 wxEvtHandler
* m_toplevEvtHandler
;
436 // this is for the control in popup
437 wxEvtHandler
* m_popupExtraHandler
;
439 // needed for "instant" double-click handling
440 wxLongLong m_timeLastMouseUp
;
442 // used to prevent immediate re-popupping incase closed popup
443 // by clicking on the combo control (needed because of inconsistent
444 // transient implementation across platforms).
445 wxLongLong m_timeCanAcceptClick
;
447 // how much popup should expand to the left/right of the control
451 // minimum popup width
452 wxCoord m_widthMinPopup
;
454 // preferred popup height
455 wxCoord m_heightPopup
;
457 // how much of writable combo is custom-paint by callback?
458 // also used to indicate area that is not covered by "blue"
459 // selection indicator.
460 wxCoord m_widthCustomPaint
;
462 // absolute text indentation, in pixels
465 // side on which the popup is aligned
468 // Width of the "fake" border
469 wxCoord m_widthCustomBorder
;
471 // The button and textctrl click/paint areas
475 // current button state (uses renderer flags)
484 // last default button width
487 // custom dropbutton bitmaps
488 wxBitmap m_bmpNormal
;
489 wxBitmap m_bmpPressed
;
491 wxBitmap m_bmpDisabled
;
493 // area used by the button
496 // platform-dependant customization and other flags
499 // draw blank button background under bitmap?
500 bool m_blankButtonBg
;
502 // is the popup window currenty shown?
505 // Set to 1 on mouse down, 0 on mouse up. Used to eliminate down-less mouse ups.
511 DECLARE_EVENT_TABLE()
513 DECLARE_ABSTRACT_CLASS(wxComboControlBase
)
517 // ----------------------------------------------------------------------------
518 // wxComboPopup is the interface which must be implemented by a control to be
519 // used as a popup by wxComboControl
520 // ----------------------------------------------------------------------------
523 // wxComboPopup internal flags
526 wxCP_IFLAG_CREATED
= 0x0001 // Set by wxComboControlBase after Create is called
530 class WXDLLEXPORT wxComboPopup
532 friend class wxComboControlBase
;
536 m_combo
= (wxComboControlBase
*) NULL
;
540 // This is called immediately after construction finishes. m_combo member
541 // variable has been initialized before the call.
542 // NOTE: It is not in constructor so the derived class doesn't need to redefine
543 // a default constructor of its own.
544 virtual void Init() { };
546 virtual ~wxComboPopup();
548 // Create the popup child control.
549 // Return true for success.
550 virtual bool Create(wxWindow
* parent
) = 0;
552 // We must have an associated control which is subclassed by the combobox.
553 virtual wxWindow
*GetControl() = 0;
555 // Called immediately after the popup is shown
556 virtual void OnPopup();
558 // Called when popup is dismissed
559 virtual void OnDismiss();
561 // Called just prior to displaying popup.
562 // Default implementation does nothing.
563 virtual void SetStringValue( const wxString
& value
);
565 // Gets displayed string representation of the value.
566 virtual wxString
GetStringValue() const = 0;
568 // This is called to custom paint in the combo control itself (ie. not the popup).
569 // Default implementation draws value as string.
570 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
572 // Receives key events from the parent wxComboControl.
573 // Events not handled should be skipped, as usual.
574 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
576 // Implement if you need to support special action when user
577 // double-clicks on the parent wxComboControl.
578 virtual void OnComboDoubleClick();
580 // Return final size of popup. Called on every popup, just prior to OnShow.
581 // minWidth = preferred minimum width for window
582 // prefHeight = preferred height. Only applies if > 0,
583 // maxHeight = max height for window, as limited by screen size
584 // and should only be rounded down, if necessary.
585 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
587 // Return true if you want delay call to Create until the popup is shown
588 // for the first time. It is more efficient, but note that it is often
589 // more convenient to have the control created immediately.
590 // Default returns false.
591 virtual bool LazyCreate();
600 // Returns true if Create has been called.
601 bool IsCreated() const
603 return (m_iFlags
& wxCP_IFLAG_CREATED
) ? true : false;
606 // Default PaintComboControl behaviour
607 static void DefaultPaintComboControl( wxComboControlBase
* combo
,
609 const wxRect
& rect
);
612 wxComboControlBase
* m_combo
;
616 // Called in wxComboControlBase::SetPopupControl
617 void InitBase(wxComboControlBase
*combo
)
624 // ----------------------------------------------------------------------------
625 // include the platform-dependent header defining the real class
626 // ----------------------------------------------------------------------------
628 #if defined(__WXUNIVERSAL__)
629 // No native universal (but it must still be first in the list)
630 #elif defined(__WXMSW__)
631 #include "wx/msw/combo.h"
634 // Any ports may need generic as an alternative
635 #include "wx/generic/combo.h"
637 #endif // wxUSE_COMBOCONTROL
640 // _WX_COMBOCONTROL_H_BASE_