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 virtual void DoSetToolTip( wxToolTip
*tip
);
184 // wxTextCtrl methods - for readonly combo they should return
186 virtual wxString
GetValue() const;
187 virtual void SetValue(const wxString
& value
);
190 virtual void Paste();
191 virtual void SetInsertionPoint(long pos
);
192 virtual void SetInsertionPointEnd();
193 virtual long GetInsertionPoint() const;
194 virtual long GetLastPosition() const;
195 virtual void Replace(long from
, long to
, const wxString
& value
);
196 virtual void Remove(long from
, long to
);
197 virtual void SetSelection(long from
, long to
);
200 // This method sets the text without affecting list selection
201 // (ie. wxComboPopup::SetStringValue doesn't get called).
202 void SetText(const wxString
& value
);
205 // Popup customization methods
208 // Sets minimum width of the popup. If wider than combo control, it will extend to the left.
210 // * Value -1 indicates the default.
211 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
212 void SetPopupMinWidth( int width
)
214 m_widthMinPopup
= width
;
217 // Sets preferred maximum height of the popup.
219 // * Value -1 indicates the default.
220 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
221 void SetPopupMaxHeight( int height
)
223 m_heightPopup
= height
;
226 // Extends popup size horizontally, relative to the edges of the combo control.
228 // * Popup minimum width may override extLeft (ie. it has higher precedence).
229 // * Values 0 indicate default.
230 // * Custom popup may not take this fully into account (wxOwnerDrawnComboBox takes).
231 void SetPopupExtents( int extLeft
, int extRight
)
234 m_extRight
= extRight
;
237 // Set width, in pixels, of custom paint area in writable combo.
238 // In read-only, used to indicate area that is not covered by the
239 // focus rectangle (which may or may not be drawn, depending on the
241 void SetCustomPaintWidth( int width
);
242 int GetCustomPaintWidth() const { return m_widthCustomPaint
; }
244 // Set side of the control to which the popup will align itself.
245 // Valid values are wxLEFT, wxRIGHT and 0. The default value 0 wmeans
246 // that the side of the button will be used.
247 void SetPopupAnchor( int anchorSide
)
249 m_anchorSide
= anchorSide
;
252 // Set position of dropdown button.
253 // width: 0 > for specific custom width, negative to adjust to smaller than default
254 // height: 0 > for specific custom height, negative to adjust to smaller than default
255 // side: wxLEFT or wxRIGHT, indicates on which side the button will be placed.
256 // spacingX: empty space on sides of the button. Default is 0.
258 // There is no spacingY - the button will be centered vertically.
259 void SetButtonPosition( int width
= 0,
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 // Draws focus background (on combo control) in a way typical on platform.
301 // Unless you plan to paint your own focus indicator, you should always call this
302 // in your wxComboPopup::PaintComboControl implementation.
303 // In addition, it sets pen and text colour to what looks good and proper
304 // against the background.
305 // flags: wxRendererNative flags: wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
306 // wxCONTROL_SELECTED: list item is selected
307 // wxCONTROL_DISABLED: control/item is disabled
308 virtual void DrawFocusBackground( wxDC
& dc
, const wxRect
& rect
, int flags
);
310 // Returns true if focus indicator should be drawn in the control.
311 bool ShouldDrawFocus() const
313 const wxWindow
* curFocus
= FindFocus();
314 return ( !m_isPopupShown
&&
315 (curFocus
== this || (m_btn
&& curFocus
== m_btn
)) &&
316 (m_windowStyle
& wxCB_READONLY
) );
319 // These methods return references to appropriate dropbutton bitmaps
320 const wxBitmap
& GetBitmapNormal() const { return m_bmpNormal
; }
321 const wxBitmap
& GetBitmapPressed() const { return m_bmpPressed
; }
322 const wxBitmap
& GetBitmapHover() const { return m_bmpHover
; }
323 const wxBitmap
& GetBitmapDisabled() const { return m_bmpDisabled
; }
325 // Return internal flags
326 wxUint32
GetInternalFlags() const { return m_iFlags
; }
328 // Return true if Create has finished
329 bool IsCreated() const { return m_iFlags
& wxCC_IFLAG_CREATED
? true : false; }
331 // common code to be called on popup hide/dismiss
332 void OnPopupDismiss();
337 // Override these for customization purposes
340 // called from wxSizeEvent handler
341 virtual void OnResize() = 0;
343 // Return native text identation (for pure text, not textctrl)
344 virtual wxCoord
GetNativeTextIndent() const;
346 // Called in syscolourchanged handler and base create
347 virtual void OnThemeChange();
349 // Creates wxTextCtrl.
350 // extraStyle: Extra style parameters
351 void CreateTextCtrl( int extraStyle
, const wxValidator
& validator
);
353 // Installs standard input handler to combo (and optionally to the textctrl)
354 void InstallInputHandlers( bool alsoTextCtrl
= true );
356 // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
357 void DrawButton( wxDC
& dc
, const wxRect
& rect
, bool paintBg
= true );
359 // Call if cursor is on button area or mouse is captured for the button.
360 //bool HandleButtonMouseEvent( wxMouseEvent& event, bool isInside );
361 bool HandleButtonMouseEvent( wxMouseEvent
& event
, int flags
);
363 // Conversion to double-clicks and some basic filtering
364 // returns true if event was consumed or filtered (event type is also set to 0 in this case)
365 //bool PreprocessMouseEvent( wxMouseEvent& event, bool isOnButtonArea );
366 bool PreprocessMouseEvent( wxMouseEvent
& event
, int flags
);
369 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
370 // If you need alternate behaviour, it is recommended you manipulate and filter events to it
371 // instead of building your own handling routine (for reference, on wxEVT_LEFT_DOWN it will
372 // toggle popup and on wxEVT_LEFT_DCLICK it will do the same or run the popup's dclick method,
373 // if defined - you should pass events of other types of it for common processing).
374 void HandleNormalMouseEvent( wxMouseEvent
& event
);
376 // Creates popup window, calls interface->Create(), etc
379 // override the base class virtuals involved in geometry calculations
380 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
381 virtual wxSize
DoGetBestSize() const;
383 // ensures there is atleast the default popup
384 void EnsurePopupControl();
386 // Recalculates button and textctrl areas. Called when size or button setup change.
387 // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
389 void CalculateAreas( int btnWidth
= 0 );
391 // Standard textctrl positioning routine. Just give it platform-dependant
392 // textctrl coordinate adjustment.
393 void PositionTextCtrl( int textCtrlXAdjust
, int textCtrlYAdjust
);
396 void OnSizeEvent( wxSizeEvent
& event
);
397 void OnFocusEvent(wxFocusEvent
& event
);
398 void OnTextCtrlEvent(wxCommandEvent
& event
);
399 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
401 // Set customization flags (directs how wxComboControlBase helpers behave)
402 void Customize( wxUint32 flags
) { m_iFlags
|= flags
; }
404 // Dispatches size event and refreshes
405 void RecalcAndRefresh();
407 // Used by OnPaints of derived classes
408 wxBitmap
& GetBufferBitmap(const wxSize
& sz
) const;
410 // This is used when m_text is hidden (readonly).
411 wxString m_valueString
;
413 // the text control and button we show all the time
417 // wxPopupWindow or similar containing the window managed by the interface.
418 wxWindow
* m_winPopup
;
420 // the popup control/panel
424 wxComboPopup
* m_popupInterface
;
426 // this is for this control itself
427 wxEvtHandler
* m_extraEvtHandler
;
430 wxEvtHandler
* m_textEvtHandler
;
432 // this is for the top level window
433 wxEvtHandler
* m_toplevEvtHandler
;
435 // this is for the control in popup
436 wxEvtHandler
* m_popupExtraHandler
;
438 // needed for "instant" double-click handling
439 wxLongLong m_timeLastMouseUp
;
441 // used to prevent immediate re-popupping incase closed popup
442 // by clicking on the combo control (needed because of inconsistent
443 // transient implementation across platforms).
444 wxLongLong m_timeCanAcceptClick
;
446 // how much popup should expand to the left/right of the control
450 // minimum popup width
451 wxCoord m_widthMinPopup
;
453 // preferred popup height
454 wxCoord m_heightPopup
;
456 // how much of writable combo is custom-paint by callback?
457 // also used to indicate area that is not covered by "blue"
458 // selection indicator.
459 wxCoord m_widthCustomPaint
;
461 // absolute text indentation, in pixels
464 // side on which the popup is aligned
467 // Width of the "fake" border
468 wxCoord m_widthCustomBorder
;
470 // The button and textctrl click/paint areas
474 // current button state (uses renderer flags)
483 // last default button width
486 // custom dropbutton bitmaps
487 wxBitmap m_bmpNormal
;
488 wxBitmap m_bmpPressed
;
490 wxBitmap m_bmpDisabled
;
492 // area used by the button
495 // platform-dependant customization and other flags
498 // draw blank button background under bitmap?
499 bool m_blankButtonBg
;
501 // is the popup window currenty shown?
504 // Set to 1 on mouse down, 0 on mouse up. Used to eliminate down-less mouse ups.
510 DECLARE_EVENT_TABLE()
512 DECLARE_ABSTRACT_CLASS(wxComboControlBase
)
516 // ----------------------------------------------------------------------------
517 // wxComboPopup is the interface which must be implemented by a control to be
518 // used as a popup by wxComboControl
519 // ----------------------------------------------------------------------------
522 // wxComboPopup internal flags
525 wxCP_IFLAG_CREATED
= 0x0001 // Set by wxComboControlBase after Create is called
529 class WXDLLEXPORT wxComboPopup
531 friend class wxComboControlBase
;
535 m_combo
= (wxComboControlBase
*) NULL
;
539 // This is called immediately after construction finishes. m_combo member
540 // variable has been initialized before the call.
541 // NOTE: It is not in constructor so the derived class doesn't need to redefine
542 // a default constructor of its own.
543 virtual void Init() { };
545 virtual ~wxComboPopup();
547 // Create the popup child control.
548 // Return true for success.
549 virtual bool Create(wxWindow
* parent
) = 0;
551 // We must have an associated control which is subclassed by the combobox.
552 virtual wxWindow
*GetControl() = 0;
554 // Called immediately after the popup is shown
555 virtual void OnPopup();
557 // Called when popup is dismissed
558 virtual void OnDismiss();
560 // Called just prior to displaying popup.
561 // Default implementation does nothing.
562 virtual void SetStringValue( const wxString
& value
);
564 // Gets displayed string representation of the value.
565 virtual wxString
GetStringValue() const = 0;
567 // This is called to custom paint in the combo control itself (ie. not the popup).
568 // Default implementation draws value as string.
569 virtual void PaintComboControl( wxDC
& dc
, const wxRect
& rect
);
571 // Receives key events from the parent wxComboControl.
572 // Events not handled should be skipped, as usual.
573 virtual void OnComboKeyEvent( wxKeyEvent
& event
);
575 // Implement if you need to support special action when user
576 // double-clicks on the parent wxComboControl.
577 virtual void OnComboDoubleClick();
579 // Return final size of popup. Called on every popup, just prior to OnShow.
580 // minWidth = preferred minimum width for window
581 // prefHeight = preferred height. Only applies if > 0,
582 // maxHeight = max height for window, as limited by screen size
583 // and should only be rounded down, if necessary.
584 virtual wxSize
GetAdjustedSize( int minWidth
, int prefHeight
, int maxHeight
);
586 // Return true if you want delay call to Create until the popup is shown
587 // for the first time. It is more efficient, but note that it is often
588 // more convenient to have the control created immediately.
589 // Default returns false.
590 virtual bool LazyCreate();
599 // Returns true if Create has been called.
600 bool IsCreated() const
602 return (m_iFlags
& wxCP_IFLAG_CREATED
) ? true : false;
605 // Default PaintComboControl behaviour
606 static void DefaultPaintComboControl( wxComboControlBase
* combo
,
608 const wxRect
& rect
);
611 wxComboControlBase
* m_combo
;
615 // Called in wxComboControlBase::SetPopupControl
616 void InitBase(wxComboControlBase
*combo
)
623 // ----------------------------------------------------------------------------
624 // include the platform-dependent header defining the real class
625 // ----------------------------------------------------------------------------
627 #if defined(__WXUNIVERSAL__)
628 // No native universal (but it must still be first in the list)
629 #elif defined(__WXMSW__)
630 #include "wx/msw/combo.h"
633 // Any ports may need generic as an alternative
634 #include "wx/generic/combo.h"
636 #endif // wxUSE_COMBOCONTROL
639 // _WX_COMBOCONTROL_H_BASE_