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