correct access for virtuals
[wxWidgets.git] / include / wx / combo.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/combo.h
3 // Purpose: wxComboControl declaration
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: Apr-30-2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_COMBOCONTROL_H_BASE_
13 #define _WX_COMBOCONTROL_H_BASE_
14
15
16 /*
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?
19
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.
25
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.
34
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.
38 */
39
40
41 #include "wx/defs.h"
42
43 #if wxUSE_COMBOCONTROL
44
45
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
51
52 class WXDLLEXPORT wxComboPopup;
53
54 //
55 // New window styles for wxComboControlBase
56 //
57 enum
58 {
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,
62
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,
66
67 // Dropbutton acts like standard push button.
68 wxCC_STD_BUTTON = 0x0400
69 };
70
71
72 // wxComboControl internal flags
73 enum
74 {
75 // First those that can be passed to Customize.
76 // It is Windows style for all flags to be clear.
77
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,
84
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
93 };
94
95
96 // Flags used by PreprocessMouseEvent and HandleButtonMouseEvent
97 enum
98 {
99 wxCC_MF_ON_BUTTON = 0x0001 // cursor is on dropbutton area
100 };
101
102
103 // Namespace for wxComboControl feature flags
104 struct wxComboControlFeatures
105 {
106 enum
107 {
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
111 // of the control
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
116 // painted
117 Borderless = 0x0040, // wxNO_BORDER window style works
118
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.
122
123 All = MovableButton|BitmapButton|
124 ButtonSpacing|TextIndent|
125 PaintControl|PaintWritable|
126 Borderless
127 };
128 };
129
130
131 class WXDLLEXPORT wxComboControlBase : public wxControl
132 {
133 friend class wxComboPopup;
134 public:
135 // ctors and such
136 wxComboControlBase() : wxControl() { Init(); }
137
138 bool Create(wxWindow *parent,
139 wxWindowID id,
140 const wxString& value,
141 const wxPoint& pos,
142 const wxSize& size,
143 long style,
144 const wxValidator& validator,
145 const wxString& name);
146
147 virtual ~wxComboControlBase();
148
149 // show/hide popup window
150 virtual void ShowPopup();
151 virtual void HidePopup();
152
153 // Override for totally custom combo action
154 virtual void OnButtonClick();
155
156 // return true if the popup is currently shown
157 bool IsPopupShown() const { return m_isPopupShown; }
158
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 );
162
163 // get interface class instance derived from wxComboPopup
164 wxComboPopup* GetPopupControl() const { return m_popupInterface; }
165
166 // get the popup window containing the popup control
167 wxWindow *GetPopupWindow() const { return m_winPopup; }
168
169 // Get the text control which is part of the combobox.
170 wxTextCtrl *GetTextCtrl() const { return m_text; }
171
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; }
175
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);
180
181 // wxTextCtrl methods - for readonly combo they should return
182 // without errors.
183 virtual wxString GetValue() const;
184 virtual void SetValue(const wxString& value);
185 virtual void Copy();
186 virtual void Cut();
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);
195 virtual void Undo();
196
197 // This method sets the text without affecting list selection
198 // (ie. wxComboPopup::SetStringValue doesn't get called).
199 void SetText(const wxString& value);
200
201 //
202 // Popup customization methods
203 //
204
205 // Sets minimum width of the popup. If wider than combo control, it will extend to the left.
206 // Remarks:
207 // * Value -1 indicates the default.
208 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
209 void SetPopupMinWidth( int width )
210 {
211 m_widthMinPopup = width;
212 }
213
214 // Sets preferred maximum height of the popup.
215 // Remarks:
216 // * Value -1 indicates the default.
217 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
218 void SetPopupMaxHeight( int height )
219 {
220 m_heightPopup = height;
221 }
222
223 // Extends popup size horizontally, relative to the edges of the combo control.
224 // Remarks:
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 )
229 {
230 m_extLeft = extLeft;
231 m_extRight = extRight;
232 }
233
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
237 // popup type).
238 void SetCustomPaintWidth( int width );
239 int GetCustomPaintWidth() const { return m_widthCustomPaint; }
240
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 )
245 {
246 m_anchorSide = anchorSide;
247 }
248
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.
254 // Remarks:
255 // There is no spacingY - the button will be centered vertically.
256 void SetButtonPosition( int width = 0,
257 int height = 0,
258 int side = wxRIGHT,
259 int spacingX = 0 );
260
261
262 //
263 // Sets dropbutton to be drawn with custom bitmaps.
264 //
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 );
278
279 //
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.
283 // Remarks
284 // * This method may do nothing on some native implementations.
285 void SetTextIndent( int indent );
286
287 // Returns actual indentation in pixels.
288 wxCoord GetTextIndent() const
289 {
290 return m_absIndent;
291 }
292
293 //
294 // Utilies needed by the popups or native implementations
295 //
296
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 );
306
307 // Returns true if focus indicator should be drawn in the control.
308 bool ShouldDrawFocus() const
309 {
310 const wxWindow* curFocus = FindFocus();
311 return ( !m_isPopupShown &&
312 (curFocus == this || (m_btn && curFocus == m_btn)) &&
313 (m_windowStyle & wxCB_READONLY) );
314 }
315
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; }
321
322 // Return internal flags
323 wxUint32 GetInternalFlags() const { return m_iFlags; }
324
325 // Return true if Create has finished
326 bool IsCreated() const { return m_iFlags & wxCC_IFLAG_CREATED ? true : false; }
327
328 // common code to be called on popup hide/dismiss
329 void OnPopupDismiss();
330
331 protected:
332
333 //
334 // Override these for customization purposes
335 //
336
337 // called from wxSizeEvent handler
338 virtual void OnResize() = 0;
339
340 // Return native text identation (for pure text, not textctrl)
341 virtual wxCoord GetNativeTextIndent() const;
342
343 // Called in syscolourchanged handler and base create
344 virtual void OnThemeChange();
345
346 // Creates wxTextCtrl.
347 // extraStyle: Extra style parameters
348 void CreateTextCtrl( int extraStyle, const wxValidator& validator );
349
350 // Installs standard input handler to combo (and optionally to the textctrl)
351 void InstallInputHandlers( bool alsoTextCtrl = true );
352
353 // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
354 void DrawButton( wxDC& dc, const wxRect& rect, bool paintBg = true );
355
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 );
359
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 );
364
365 //
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 );
372
373 // Creates popup window, calls interface->Create(), etc
374 void CreatePopup();
375
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;
379
380 // ensures there is atleast the default popup
381 void EnsurePopupControl();
382
383 // Recalculates button and textctrl areas. Called when size or button setup change.
384 // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
385 // just recalculate.
386 void CalculateAreas( int btnWidth = 0 );
387
388 // Standard textctrl positioning routine. Just give it platform-dependant
389 // textctrl coordinate adjustment.
390 void PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust );
391
392 // event handlers
393 void OnSizeEvent( wxSizeEvent& event );
394 void OnFocusEvent(wxFocusEvent& event);
395 void OnTextCtrlEvent(wxCommandEvent& event);
396 void OnSysColourChanged(wxSysColourChangedEvent& event);
397
398 // Set customization flags (directs how wxComboControlBase helpers behave)
399 void Customize( wxUint32 flags ) { m_iFlags |= flags; }
400
401 // Dispatches size event and refreshes
402 void RecalcAndRefresh();
403
404 #if wxUSE_TOOLTIPS
405 virtual void DoSetToolTip( wxToolTip *tip );
406 #endif
407
408 // Used by OnPaints of derived classes
409 wxBitmap& GetBufferBitmap(const wxSize& sz) const;
410
411 // This is used when m_text is hidden (readonly).
412 wxString m_valueString;
413
414 // the text control and button we show all the time
415 wxTextCtrl* m_text;
416 wxWindow* m_btn;
417
418 // wxPopupWindow or similar containing the window managed by the interface.
419 wxWindow* m_winPopup;
420
421 // the popup control/panel
422 wxWindow* m_popup;
423
424 // popup interface
425 wxComboPopup* m_popupInterface;
426
427 // this is for this control itself
428 wxEvtHandler* m_extraEvtHandler;
429
430 // this is for text
431 wxEvtHandler* m_textEvtHandler;
432
433 // this is for the top level window
434 wxEvtHandler* m_toplevEvtHandler;
435
436 // this is for the control in popup
437 wxEvtHandler* m_popupExtraHandler;
438
439 // needed for "instant" double-click handling
440 wxLongLong m_timeLastMouseUp;
441
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;
446
447 // how much popup should expand to the left/right of the control
448 wxCoord m_extLeft;
449 wxCoord m_extRight;
450
451 // minimum popup width
452 wxCoord m_widthMinPopup;
453
454 // preferred popup height
455 wxCoord m_heightPopup;
456
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;
461
462 // absolute text indentation, in pixels
463 wxCoord m_absIndent;
464
465 // side on which the popup is aligned
466 int m_anchorSide;
467
468 // Width of the "fake" border
469 wxCoord m_widthCustomBorder;
470
471 // The button and textctrl click/paint areas
472 wxRect m_tcArea;
473 wxRect m_btnArea;
474
475 // current button state (uses renderer flags)
476 int m_btnState;
477
478 // button position
479 int m_btnWid;
480 int m_btnHei;
481 int m_btnSide;
482 int m_btnSpacingX;
483
484 // last default button width
485 int m_btnWidDefault;
486
487 // custom dropbutton bitmaps
488 wxBitmap m_bmpNormal;
489 wxBitmap m_bmpPressed;
490 wxBitmap m_bmpHover;
491 wxBitmap m_bmpDisabled;
492
493 // area used by the button
494 wxSize m_btnSize;
495
496 // platform-dependant customization and other flags
497 wxUint32 m_iFlags;
498
499 // draw blank button background under bitmap?
500 bool m_blankButtonBg;
501
502 // is the popup window currenty shown?
503 bool m_isPopupShown;
504
505 // Set to 1 on mouse down, 0 on mouse up. Used to eliminate down-less mouse ups.
506 bool m_downReceived;
507
508 private:
509 void Init();
510
511 DECLARE_EVENT_TABLE()
512
513 DECLARE_ABSTRACT_CLASS(wxComboControlBase)
514 };
515
516
517 // ----------------------------------------------------------------------------
518 // wxComboPopup is the interface which must be implemented by a control to be
519 // used as a popup by wxComboControl
520 // ----------------------------------------------------------------------------
521
522
523 // wxComboPopup internal flags
524 enum
525 {
526 wxCP_IFLAG_CREATED = 0x0001 // Set by wxComboControlBase after Create is called
527 };
528
529
530 class WXDLLEXPORT wxComboPopup
531 {
532 friend class wxComboControlBase;
533 public:
534 wxComboPopup()
535 {
536 m_combo = (wxComboControlBase*) NULL;
537 m_iFlags = 0;
538 }
539
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() { };
545
546 virtual ~wxComboPopup();
547
548 // Create the popup child control.
549 // Return true for success.
550 virtual bool Create(wxWindow* parent) = 0;
551
552 // We must have an associated control which is subclassed by the combobox.
553 virtual wxWindow *GetControl() = 0;
554
555 // Called immediately after the popup is shown
556 virtual void OnPopup();
557
558 // Called when popup is dismissed
559 virtual void OnDismiss();
560
561 // Called just prior to displaying popup.
562 // Default implementation does nothing.
563 virtual void SetStringValue( const wxString& value );
564
565 // Gets displayed string representation of the value.
566 virtual wxString GetStringValue() const = 0;
567
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 );
571
572 // Receives key events from the parent wxComboControl.
573 // Events not handled should be skipped, as usual.
574 virtual void OnComboKeyEvent( wxKeyEvent& event );
575
576 // Implement if you need to support special action when user
577 // double-clicks on the parent wxComboControl.
578 virtual void OnComboDoubleClick();
579
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 );
586
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();
592
593 //
594 // Utilies
595 //
596
597 // Hides the popup
598 void Dismiss();
599
600 // Returns true if Create has been called.
601 bool IsCreated() const
602 {
603 return (m_iFlags & wxCP_IFLAG_CREATED) ? true : false;
604 }
605
606 // Default PaintComboControl behaviour
607 static void DefaultPaintComboControl( wxComboControlBase* combo,
608 wxDC& dc,
609 const wxRect& rect );
610
611 protected:
612 wxComboControlBase* m_combo;
613 wxUint32 m_iFlags;
614
615 private:
616 // Called in wxComboControlBase::SetPopupControl
617 void InitBase(wxComboControlBase *combo)
618 {
619 m_combo = combo;
620 }
621 };
622
623
624 // ----------------------------------------------------------------------------
625 // include the platform-dependent header defining the real class
626 // ----------------------------------------------------------------------------
627
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"
632 #endif
633
634 // Any ports may need generic as an alternative
635 #include "wx/generic/combo.h"
636
637 #endif // wxUSE_COMBOCONTROL
638
639 #endif
640 // _WX_COMBOCONTROL_H_BASE_