Compilation fix after wxCB_ flags move.
[wxWidgets.git] / include / wx / combo.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/combo.h
3 // Purpose: wxComboCtrl 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 wxComboCtrl 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 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.
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_COMBOCTRL
44
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
48 #include "wx/combobox.h"
49
50 class WXDLLIMPEXP_CORE wxTextCtrl;
51 class WXDLLEXPORT wxComboPopup;
52
53 //
54 // New window styles for wxComboCtrlBase
55 //
56 enum
57 {
58 // Double-clicking a read-only combo triggers call to popup's OnComboPopup.
59 // In wxOwnerDrawnComboBox, for instance, it cycles item.
60 wxCC_SPECIAL_DCLICK = 0x0100,
61
62 // Dropbutton acts like standard push button.
63 wxCC_STD_BUTTON = 0x0200
64 };
65
66
67 // wxComboCtrl internal flags
68 enum
69 {
70 // First those that can be passed to Customize.
71 // It is Windows style for all flags to be clear.
72
73 // Button is preferred outside the border (GTK style)
74 wxCC_BUTTON_OUTSIDE_BORDER = 0x0001,
75 // Show popup on mouse up instead of mouse down (which is the Windows style)
76 wxCC_POPUP_ON_MOUSE_UP = 0x0002,
77 // All text is not automatically selected on click
78 wxCC_NO_TEXT_AUTO_SELECT = 0x0004,
79
80 // Internal use: signals creation is complete
81 wxCC_IFLAG_CREATED = 0x0100,
82 // Internal use: really put button outside
83 wxCC_IFLAG_BUTTON_OUTSIDE = 0x0200,
84 // Internal use: SetTextIndent has been called
85 wxCC_IFLAG_INDENT_SET = 0x0400,
86 // Internal use: Set wxTAB_TRAVERSAL to parent when popup is dismissed
87 wxCC_IFLAG_PARENT_TAB_TRAVERSAL = 0x0800
88 };
89
90
91 // Flags used by PreprocessMouseEvent and HandleButtonMouseEvent
92 enum
93 {
94 wxCC_MF_ON_BUTTON = 0x0001 // cursor is on dropbutton area
95 };
96
97
98 // Namespace for wxComboCtrl feature flags
99 struct wxComboCtrlFeatures
100 {
101 enum
102 {
103 MovableButton = 0x0001, // Button can be on either side of control
104 BitmapButton = 0x0002, // Button may be replaced with bitmap
105 ButtonSpacing = 0x0004, // Button can have spacing from the edge
106 // of the control
107 TextIndent = 0x0008, // SetTextIndent can be used
108 PaintControl = 0x0010, // Combo control itself can be custom painted
109 PaintWritable = 0x0020, // A variable-width area in front of writable
110 // combo control's textctrl can be custom
111 // painted
112 Borderless = 0x0040, // wxNO_BORDER window style works
113
114 // There are no feature flags for...
115 // PushButtonBitmapBackground - if its in wxRendererNative, then it should be
116 // not an issue to have it automatically under the bitmap.
117
118 All = MovableButton|BitmapButton|
119 ButtonSpacing|TextIndent|
120 PaintControl|PaintWritable|
121 Borderless
122 };
123 };
124
125
126 class WXDLLEXPORT wxComboCtrlBase : public wxControl
127 {
128 friend class wxComboPopup;
129 public:
130 // ctors and such
131 wxComboCtrlBase() : wxControl() { Init(); }
132
133 bool Create(wxWindow *parent,
134 wxWindowID id,
135 const wxString& value,
136 const wxPoint& pos,
137 const wxSize& size,
138 long style,
139 const wxValidator& validator,
140 const wxString& name);
141
142 virtual ~wxComboCtrlBase();
143
144 // show/hide popup window
145 virtual void ShowPopup();
146 virtual void HidePopup();
147
148 // Override for totally custom combo action
149 virtual void OnButtonClick();
150
151 // return true if the popup is currently shown
152 bool IsPopupShown() const { return m_isPopupShown; }
153
154 // set interface class instance derived from wxComboPopup
155 // NULL popup can be used to indicate default in a derived class
156 void SetPopupControl( wxComboPopup* popup )
157 {
158 DoSetPopupControl(popup);
159 }
160
161 // get interface class instance derived from wxComboPopup
162 wxComboPopup* GetPopupControl()
163 {
164 EnsurePopupControl();
165 return m_popupInterface;
166 }
167
168 // get the popup window containing the popup control
169 wxWindow *GetPopupWindow() const { return m_winPopup; }
170
171 // Get the text control which is part of the combobox.
172 wxTextCtrl *GetTextCtrl() const { return m_text; }
173
174 // get the dropdown button which is part of the combobox
175 // note: its not necessarily a wxButton or wxBitmapButton
176 wxWindow *GetButton() const { return m_btn; }
177
178 // forward these methods to all subcontrols
179 virtual bool Enable(bool enable = true);
180 virtual bool Show(bool show = true);
181 virtual bool SetFont(const wxFont& font);
182
183 // wxTextCtrl methods - for readonly combo they should return
184 // without errors.
185 virtual wxString GetValue() const;
186 virtual void SetValue(const wxString& value);
187 virtual void Copy();
188 virtual void Cut();
189 virtual void Paste();
190 virtual void SetInsertionPoint(long pos);
191 virtual void SetInsertionPointEnd();
192 virtual long GetInsertionPoint() const;
193 virtual long GetLastPosition() const;
194 virtual void Replace(long from, long to, const wxString& value);
195 virtual void Remove(long from, long to);
196 virtual void SetSelection(long from, long to);
197 virtual void Undo();
198
199 // This method sets the text without affecting list selection
200 // (ie. wxComboPopup::SetStringValue doesn't get called).
201 void SetText(const wxString& value);
202
203 //
204 // Popup customization methods
205 //
206
207 // Sets minimum width of the popup. If wider than combo control, it will extend to the left.
208 // Remarks:
209 // * Value -1 indicates the default.
210 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
211 void SetPopupMinWidth( int width )
212 {
213 m_widthMinPopup = width;
214 }
215
216 // Sets preferred maximum height of the popup.
217 // Remarks:
218 // * Value -1 indicates the default.
219 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
220 void SetPopupMaxHeight( int height )
221 {
222 m_heightPopup = height;
223 }
224
225 // Extends popup size horizontally, relative to the edges of the combo control.
226 // Remarks:
227 // * Popup minimum width may override extLeft (ie. it has higher precedence).
228 // * Values 0 indicate default.
229 // * Custom popup may not take this fully into account (wxOwnerDrawnComboBox takes).
230 void SetPopupExtents( int extLeft, int extRight )
231 {
232 m_extLeft = extLeft;
233 m_extRight = extRight;
234 }
235
236 // Set width, in pixels, of custom paint area in writable combo.
237 // In read-only, used to indicate area that is not covered by the
238 // focus rectangle (which may or may not be drawn, depending on the
239 // popup type).
240 void SetCustomPaintWidth( int width );
241 int GetCustomPaintWidth() const { return m_widthCustomPaint; }
242
243 // Set side of the control to which the popup will align itself.
244 // Valid values are wxLEFT, wxRIGHT and 0. The default value 0 wmeans
245 // that the side of the button will be used.
246 void SetPopupAnchor( int anchorSide )
247 {
248 m_anchorSide = anchorSide;
249 }
250
251 // Set position of dropdown button.
252 // width: button width. <= 0 for default.
253 // height: button height. <= 0 for default.
254 // side: wxLEFT or wxRIGHT, indicates on which side the button will be placed.
255 // spacingX: empty space on sides of the button. Default is 0.
256 // Remarks:
257 // There is no spacingY - the button will be centered vertically.
258 void SetButtonPosition( int width = -1,
259 int height = -1,
260 int side = wxRIGHT,
261 int spacingX = 0 );
262
263 // Returns current size of the dropdown button.
264 wxSize GetButtonSize();
265
266 //
267 // Sets dropbutton to be drawn with custom bitmaps.
268 //
269 // bmpNormal: drawn when cursor is not on button
270 // pushButtonBg: Draw push button background below the image.
271 // NOTE! This is usually only properly supported on platforms with appropriate
272 // method in wxRendererNative.
273 // bmpPressed: drawn when button is depressed
274 // bmpHover: drawn when cursor hovers on button. This is ignored on platforms
275 // that do not generally display hover differently.
276 // bmpDisabled: drawn when combobox is disabled.
277 void SetButtonBitmaps( const wxBitmap& bmpNormal,
278 bool pushButtonBg = false,
279 const wxBitmap& bmpPressed = wxNullBitmap,
280 const wxBitmap& bmpHover = wxNullBitmap,
281 const wxBitmap& bmpDisabled = wxNullBitmap );
282
283 //
284 // This will set the space in pixels between left edge of the control and the
285 // text, regardless whether control is read-only (ie. no wxTextCtrl) or not.
286 // Platform-specific default can be set with value-1.
287 // Remarks
288 // * This method may do nothing on some native implementations.
289 void SetTextIndent( int indent );
290
291 // Returns actual indentation in pixels.
292 wxCoord GetTextIndent() const
293 {
294 return m_absIndent;
295 }
296
297 //
298 // Utilies needed by the popups or native implementations
299 //
300
301 // Draws focus background (on combo control) in a way typical on platform.
302 // Unless you plan to paint your own focus indicator, you should always call this
303 // in your wxComboPopup::PaintComboControl implementation.
304 // In addition, it sets pen and text colour to what looks good and proper
305 // against the background.
306 // flags: wxRendererNative flags: wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
307 // wxCONTROL_SELECTED: list item is selected
308 // wxCONTROL_DISABLED: control/item is disabled
309 virtual void DrawFocusBackground( wxDC& dc, const wxRect& rect, int flags ) const;
310
311 // Returns true if focus indicator should be drawn in the control.
312 bool ShouldDrawFocus() const
313 {
314 const wxWindow* curFocus = FindFocus();
315 return ( !m_isPopupShown &&
316 (curFocus == this || (m_btn && curFocus == m_btn)) &&
317 (m_windowStyle & wxCB_READONLY) );
318 }
319
320 // These methods return references to appropriate dropbutton bitmaps
321 const wxBitmap& GetBitmapNormal() const { return m_bmpNormal; }
322 const wxBitmap& GetBitmapPressed() const { return m_bmpPressed; }
323 const wxBitmap& GetBitmapHover() const { return m_bmpHover; }
324 const wxBitmap& GetBitmapDisabled() const { return m_bmpDisabled; }
325
326 // Return internal flags
327 wxUint32 GetInternalFlags() const { return m_iFlags; }
328
329 // Return true if Create has finished
330 bool IsCreated() const { return m_iFlags & wxCC_IFLAG_CREATED ? true : false; }
331
332 // common code to be called on popup hide/dismiss
333 void OnPopupDismiss();
334
335 protected:
336
337 //
338 // Override these for customization purposes
339 //
340
341 // called from wxSizeEvent handler
342 virtual void OnResize() = 0;
343
344 // Return native text identation (for pure text, not textctrl)
345 virtual wxCoord GetNativeTextIndent() const;
346
347 // Called in syscolourchanged handler and base create
348 virtual void OnThemeChange();
349
350 // Creates wxTextCtrl.
351 // extraStyle: Extra style parameters
352 void CreateTextCtrl( int extraStyle, const wxValidator& validator );
353
354 // Installs standard input handler to combo (and optionally to the textctrl)
355 void InstallInputHandlers( bool alsoTextCtrl = true );
356
357 // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
358 void DrawButton( wxDC& dc, const wxRect& rect, bool paintBg = true );
359
360 // Call if cursor is on button area or mouse is captured for the button.
361 //bool HandleButtonMouseEvent( wxMouseEvent& event, bool isInside );
362 bool HandleButtonMouseEvent( wxMouseEvent& event, int flags );
363
364 // Conversion to double-clicks and some basic filtering
365 // returns true if event was consumed or filtered (event type is also set to 0 in this case)
366 //bool PreprocessMouseEvent( wxMouseEvent& event, bool isOnButtonArea );
367 bool PreprocessMouseEvent( wxMouseEvent& event, int flags );
368
369 //
370 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
371 // If you need alternate behaviour, it is recommended you manipulate and filter events to it
372 // instead of building your own handling routine (for reference, on wxEVT_LEFT_DOWN it will
373 // toggle popup and on wxEVT_LEFT_DCLICK it will do the same or run the popup's dclick method,
374 // if defined - you should pass events of other types of it for common processing).
375 void HandleNormalMouseEvent( wxMouseEvent& event );
376
377 // Creates popup window, calls interface->Create(), etc
378 void CreatePopup();
379
380 // Destroy popup window and all related constructs
381 void DestroyPopup();
382
383 // override the base class virtuals involved in geometry calculations
384 virtual wxSize DoGetBestSize() const;
385
386 // NULL popup can be used to indicate default in a derived class
387 virtual void DoSetPopupControl(wxComboPopup* popup);
388
389 // ensures there is atleast the default popup
390 void EnsurePopupControl();
391
392 // Recalculates button and textctrl areas. Called when size or button setup change.
393 // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
394 // just recalculate.
395 void CalculateAreas( int btnWidth = 0 );
396
397 // Standard textctrl positioning routine. Just give it platform-dependant
398 // textctrl coordinate adjustment.
399 void PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust );
400
401 // event handlers
402 void OnSizeEvent( wxSizeEvent& event );
403 void OnFocusEvent(wxFocusEvent& event);
404 void OnTextCtrlEvent(wxCommandEvent& event);
405 void OnSysColourChanged(wxSysColourChangedEvent& event);
406
407 // Set customization flags (directs how wxComboCtrlBase helpers behave)
408 void Customize( wxUint32 flags ) { m_iFlags |= flags; }
409
410 // Dispatches size event and refreshes
411 void RecalcAndRefresh();
412
413 #if wxUSE_TOOLTIPS
414 virtual void DoSetToolTip( wxToolTip *tip );
415 #endif
416
417 // Used by OnPaints of derived classes
418 wxBitmap& GetBufferBitmap(const wxSize& sz) const;
419
420 // This is used when m_text is hidden (readonly).
421 wxString m_valueString;
422
423 // the text control and button we show all the time
424 wxTextCtrl* m_text;
425 wxWindow* m_btn;
426
427 // wxPopupWindow or similar containing the window managed by the interface.
428 wxWindow* m_winPopup;
429
430 // the popup control/panel
431 wxWindow* m_popup;
432
433 // popup interface
434 wxComboPopup* m_popupInterface;
435
436 // this is for this control itself
437 wxEvtHandler* m_extraEvtHandler;
438
439 // this is for text
440 wxEvtHandler* m_textEvtHandler;
441
442 // this is for the top level window
443 wxEvtHandler* m_toplevEvtHandler;
444
445 // this is for the control in popup
446 wxEvtHandler* m_popupExtraHandler;
447
448 // needed for "instant" double-click handling
449 wxLongLong m_timeLastMouseUp;
450
451 // used to prevent immediate re-popupping incase closed popup
452 // by clicking on the combo control (needed because of inconsistent
453 // transient implementation across platforms).
454 wxLongLong m_timeCanAcceptClick;
455
456 // how much popup should expand to the left/right of the control
457 wxCoord m_extLeft;
458 wxCoord m_extRight;
459
460 // minimum popup width
461 wxCoord m_widthMinPopup;
462
463 // preferred popup height
464 wxCoord m_heightPopup;
465
466 // how much of writable combo is custom-paint by callback?
467 // also used to indicate area that is not covered by "blue"
468 // selection indicator.
469 wxCoord m_widthCustomPaint;
470
471 // absolute text indentation, in pixels
472 wxCoord m_absIndent;
473
474 // side on which the popup is aligned
475 int m_anchorSide;
476
477 // Width of the "fake" border
478 wxCoord m_widthCustomBorder;
479
480 // The button and textctrl click/paint areas
481 wxRect m_tcArea;
482 wxRect m_btnArea;
483
484 // current button state (uses renderer flags)
485 int m_btnState;
486
487 // button position
488 int m_btnWid;
489 int m_btnHei;
490 int m_btnSide;
491 int m_btnSpacingX;
492
493 // last default button width
494 int m_btnWidDefault;
495
496 // custom dropbutton bitmaps
497 wxBitmap m_bmpNormal;
498 wxBitmap m_bmpPressed;
499 wxBitmap m_bmpHover;
500 wxBitmap m_bmpDisabled;
501
502 // area used by the button
503 wxSize m_btnSize;
504
505 // platform-dependant customization and other flags
506 wxUint32 m_iFlags;
507
508 // draw blank button background under bitmap?
509 bool m_blankButtonBg;
510
511 // is the popup window currenty shown?
512 bool m_isPopupShown;
513
514 // Set to 1 on mouse down, 0 on mouse up. Used to eliminate down-less mouse ups.
515 bool m_downReceived;
516
517 private:
518 void Init();
519
520 DECLARE_EVENT_TABLE()
521
522 DECLARE_ABSTRACT_CLASS(wxComboCtrlBase)
523 };
524
525
526 // ----------------------------------------------------------------------------
527 // wxComboPopup is the interface which must be implemented by a control to be
528 // used as a popup by wxComboCtrl
529 // ----------------------------------------------------------------------------
530
531
532 // wxComboPopup internal flags
533 enum
534 {
535 wxCP_IFLAG_CREATED = 0x0001 // Set by wxComboCtrlBase after Create is called
536 };
537
538
539 class WXDLLEXPORT wxComboPopup
540 {
541 friend class wxComboCtrlBase;
542 public:
543 wxComboPopup()
544 {
545 m_combo = (wxComboCtrlBase*) NULL;
546 m_iFlags = 0;
547 }
548
549 // This is called immediately after construction finishes. m_combo member
550 // variable has been initialized before the call.
551 // NOTE: It is not in constructor so the derived class doesn't need to redefine
552 // a default constructor of its own.
553 virtual void Init() { };
554
555 virtual ~wxComboPopup();
556
557 // Create the popup child control.
558 // Return true for success.
559 virtual bool Create(wxWindow* parent) = 0;
560
561 // We must have an associated control which is subclassed by the combobox.
562 virtual wxWindow *GetControl() = 0;
563
564 // Called immediately after the popup is shown
565 virtual void OnPopup();
566
567 // Called when popup is dismissed
568 virtual void OnDismiss();
569
570 // Called just prior to displaying popup.
571 // Default implementation does nothing.
572 virtual void SetStringValue( const wxString& value );
573
574 // Gets displayed string representation of the value.
575 virtual wxString GetStringValue() const = 0;
576
577 // This is called to custom paint in the combo control itself (ie. not the popup).
578 // Default implementation draws value as string.
579 virtual void PaintComboControl( wxDC& dc, const wxRect& rect );
580
581 // Receives key events from the parent wxComboCtrl.
582 // Events not handled should be skipped, as usual.
583 virtual void OnComboKeyEvent( wxKeyEvent& event );
584
585 // Implement if you need to support special action when user
586 // double-clicks on the parent wxComboCtrl.
587 virtual void OnComboDoubleClick();
588
589 // Return final size of popup. Called on every popup, just prior to OnShow.
590 // minWidth = preferred minimum width for window
591 // prefHeight = preferred height. Only applies if > 0,
592 // maxHeight = max height for window, as limited by screen size
593 // and should only be rounded down, if necessary.
594 virtual wxSize GetAdjustedSize( int minWidth, int prefHeight, int maxHeight );
595
596 // Return true if you want delay call to Create until the popup is shown
597 // for the first time. It is more efficient, but note that it is often
598 // more convenient to have the control created immediately.
599 // Default returns false.
600 virtual bool LazyCreate();
601
602 //
603 // Utilies
604 //
605
606 // Hides the popup
607 void Dismiss();
608
609 // Returns true if Create has been called.
610 bool IsCreated() const
611 {
612 return (m_iFlags & wxCP_IFLAG_CREATED) ? true : false;
613 }
614
615 // Default PaintComboControl behaviour
616 static void DefaultPaintComboControl( wxComboCtrlBase* combo,
617 wxDC& dc,
618 const wxRect& rect );
619
620 protected:
621 wxComboCtrlBase* m_combo;
622 wxUint32 m_iFlags;
623
624 private:
625 // Called in wxComboCtrlBase::SetPopupControl
626 void InitBase(wxComboCtrlBase *combo)
627 {
628 m_combo = combo;
629 }
630 };
631
632
633 // ----------------------------------------------------------------------------
634 // include the platform-dependent header defining the real class
635 // ----------------------------------------------------------------------------
636
637 #if defined(__WXUNIVERSAL__)
638 // No native universal (but it must still be first in the list)
639 #elif defined(__WXMSW__)
640 #include "wx/msw/combo.h"
641 #endif
642
643 // Any ports may need generic as an alternative
644 #include "wx/generic/combo.h"
645
646 #endif // wxUSE_COMBOCTRL
647
648 #endif
649 // _WX_COMBOCONTROL_H_BASE_