fixes and missing files from the first wxComboControl patch (update of patch 1479938)
[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 #if wxUSE_TOOLTIPS
181 virtual void DoSetToolTip( wxToolTip *tip );
182 #endif
183
184 // wxTextCtrl methods - for readonly combo they should return
185 // without errors.
186 virtual wxString GetValue() const;
187 virtual void SetValue(const wxString& value);
188 virtual void Copy();
189 virtual void Cut();
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);
198 virtual void Undo();
199
200 // This method sets the text without affecting list selection
201 // (ie. wxComboPopup::SetStringValue doesn't get called).
202 void SetText(const wxString& value);
203
204 //
205 // Popup customization methods
206 //
207
208 // Sets minimum width of the popup. If wider than combo control, it will extend to the left.
209 // Remarks:
210 // * Value -1 indicates the default.
211 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
212 void SetPopupMinWidth( int width )
213 {
214 m_widthMinPopup = width;
215 }
216
217 // Sets preferred maximum height of the popup.
218 // Remarks:
219 // * Value -1 indicates the default.
220 // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
221 void SetPopupMaxHeight( int height )
222 {
223 m_heightPopup = height;
224 }
225
226 // Extends popup size horizontally, relative to the edges of the combo control.
227 // Remarks:
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 )
232 {
233 m_extLeft = extLeft;
234 m_extRight = extRight;
235 }
236
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
240 // popup type).
241 void SetCustomPaintWidth( int width );
242 int GetCustomPaintWidth() const { return m_widthCustomPaint; }
243
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 )
248 {
249 m_anchorSide = anchorSide;
250 }
251
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.
257 // Remarks:
258 // There is no spacingY - the button will be centered vertically.
259 void SetButtonPosition( int width = 0,
260 int height = 0,
261 int side = wxRIGHT,
262 int spacingX = 0 );
263
264
265 //
266 // Sets dropbutton to be drawn with custom bitmaps.
267 //
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 );
281
282 //
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.
286 // Remarks
287 // * This method may do nothing on some native implementations.
288 void SetTextIndent( int indent );
289
290 // Returns actual indentation in pixels.
291 wxCoord GetTextIndent() const
292 {
293 return m_absIndent;
294 }
295
296 //
297 // Utilies needed by the popups or native implementations
298 //
299
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 );
309
310 // Returns true if focus indicator should be drawn in the control.
311 bool ShouldDrawFocus() const
312 {
313 const wxWindow* curFocus = FindFocus();
314 return ( !m_isPopupShown &&
315 (curFocus == this || (m_btn && curFocus == m_btn)) &&
316 (m_windowStyle & wxCB_READONLY) );
317 }
318
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; }
324
325 // Return internal flags
326 wxUint32 GetInternalFlags() const { return m_iFlags; }
327
328 // Return true if Create has finished
329 bool IsCreated() const { return m_iFlags & wxCC_IFLAG_CREATED ? true : false; }
330
331 // common code to be called on popup hide/dismiss
332 void OnPopupDismiss();
333
334 protected:
335
336 //
337 // Override these for customization purposes
338 //
339
340 // called from wxSizeEvent handler
341 virtual void OnResize() = 0;
342
343 // Return native text identation (for pure text, not textctrl)
344 virtual wxCoord GetNativeTextIndent() const;
345
346 // Called in syscolourchanged handler and base create
347 virtual void OnThemeChange();
348
349 // Creates wxTextCtrl.
350 // extraStyle: Extra style parameters
351 void CreateTextCtrl( int extraStyle, const wxValidator& validator );
352
353 // Installs standard input handler to combo (and optionally to the textctrl)
354 void InstallInputHandlers( bool alsoTextCtrl = true );
355
356 // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
357 void DrawButton( wxDC& dc, const wxRect& rect, bool paintBg = true );
358
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 );
362
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 );
367
368 //
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 );
375
376 // Creates popup window, calls interface->Create(), etc
377 void CreatePopup();
378
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;
382
383 // ensures there is atleast the default popup
384 void EnsurePopupControl();
385
386 // Recalculates button and textctrl areas. Called when size or button setup change.
387 // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
388 // just recalculate.
389 void CalculateAreas( int btnWidth = 0 );
390
391 // Standard textctrl positioning routine. Just give it platform-dependant
392 // textctrl coordinate adjustment.
393 void PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust );
394
395 // event handlers
396 void OnSizeEvent( wxSizeEvent& event );
397 void OnFocusEvent(wxFocusEvent& event);
398 void OnTextCtrlEvent(wxCommandEvent& event);
399 void OnSysColourChanged(wxSysColourChangedEvent& event);
400
401 // Set customization flags (directs how wxComboControlBase helpers behave)
402 void Customize( wxUint32 flags ) { m_iFlags |= flags; }
403
404 // Dispatches size event and refreshes
405 void RecalcAndRefresh();
406
407 // Used by OnPaints of derived classes
408 wxBitmap& GetBufferBitmap(const wxSize& sz) const;
409
410 // This is used when m_text is hidden (readonly).
411 wxString m_valueString;
412
413 // the text control and button we show all the time
414 wxTextCtrl* m_text;
415 wxWindow* m_btn;
416
417 // wxPopupWindow or similar containing the window managed by the interface.
418 wxWindow* m_winPopup;
419
420 // the popup control/panel
421 wxWindow* m_popup;
422
423 // popup interface
424 wxComboPopup* m_popupInterface;
425
426 // this is for this control itself
427 wxEvtHandler* m_extraEvtHandler;
428
429 // this is for text
430 wxEvtHandler* m_textEvtHandler;
431
432 // this is for the top level window
433 wxEvtHandler* m_toplevEvtHandler;
434
435 // this is for the control in popup
436 wxEvtHandler* m_popupExtraHandler;
437
438 // needed for "instant" double-click handling
439 wxLongLong m_timeLastMouseUp;
440
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;
445
446 // how much popup should expand to the left/right of the control
447 wxCoord m_extLeft;
448 wxCoord m_extRight;
449
450 // minimum popup width
451 wxCoord m_widthMinPopup;
452
453 // preferred popup height
454 wxCoord m_heightPopup;
455
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;
460
461 // absolute text indentation, in pixels
462 wxCoord m_absIndent;
463
464 // side on which the popup is aligned
465 int m_anchorSide;
466
467 // Width of the "fake" border
468 wxCoord m_widthCustomBorder;
469
470 // The button and textctrl click/paint areas
471 wxRect m_tcArea;
472 wxRect m_btnArea;
473
474 // current button state (uses renderer flags)
475 int m_btnState;
476
477 // button position
478 int m_btnWid;
479 int m_btnHei;
480 int m_btnSide;
481 int m_btnSpacingX;
482
483 // last default button width
484 int m_btnWidDefault;
485
486 // custom dropbutton bitmaps
487 wxBitmap m_bmpNormal;
488 wxBitmap m_bmpPressed;
489 wxBitmap m_bmpHover;
490 wxBitmap m_bmpDisabled;
491
492 // area used by the button
493 wxSize m_btnSize;
494
495 // platform-dependant customization and other flags
496 wxUint32 m_iFlags;
497
498 // draw blank button background under bitmap?
499 bool m_blankButtonBg;
500
501 // is the popup window currenty shown?
502 bool m_isPopupShown;
503
504 // Set to 1 on mouse down, 0 on mouse up. Used to eliminate down-less mouse ups.
505 bool m_downReceived;
506
507 private:
508 void Init();
509
510 DECLARE_EVENT_TABLE()
511
512 DECLARE_ABSTRACT_CLASS(wxComboControlBase)
513 };
514
515
516 // ----------------------------------------------------------------------------
517 // wxComboPopup is the interface which must be implemented by a control to be
518 // used as a popup by wxComboControl
519 // ----------------------------------------------------------------------------
520
521
522 // wxComboPopup internal flags
523 enum
524 {
525 wxCP_IFLAG_CREATED = 0x0001 // Set by wxComboControlBase after Create is called
526 };
527
528
529 class WXDLLEXPORT wxComboPopup
530 {
531 friend class wxComboControlBase;
532 public:
533 wxComboPopup()
534 {
535 m_combo = (wxComboControlBase*) NULL;
536 m_iFlags = 0;
537 }
538
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() { };
544
545 virtual ~wxComboPopup();
546
547 // Create the popup child control.
548 // Return true for success.
549 virtual bool Create(wxWindow* parent) = 0;
550
551 // We must have an associated control which is subclassed by the combobox.
552 virtual wxWindow *GetControl() = 0;
553
554 // Called immediately after the popup is shown
555 virtual void OnPopup();
556
557 // Called when popup is dismissed
558 virtual void OnDismiss();
559
560 // Called just prior to displaying popup.
561 // Default implementation does nothing.
562 virtual void SetStringValue( const wxString& value );
563
564 // Gets displayed string representation of the value.
565 virtual wxString GetStringValue() const = 0;
566
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 );
570
571 // Receives key events from the parent wxComboControl.
572 // Events not handled should be skipped, as usual.
573 virtual void OnComboKeyEvent( wxKeyEvent& event );
574
575 // Implement if you need to support special action when user
576 // double-clicks on the parent wxComboControl.
577 virtual void OnComboDoubleClick();
578
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 );
585
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();
591
592 //
593 // Utilies
594 //
595
596 // Hides the popup
597 void Dismiss();
598
599 // Returns true if Create has been called.
600 bool IsCreated() const
601 {
602 return (m_iFlags & wxCP_IFLAG_CREATED) ? true : false;
603 }
604
605 // Default PaintComboControl behaviour
606 static void DefaultPaintComboControl( wxComboControlBase* combo,
607 wxDC& dc,
608 const wxRect& rect );
609
610 protected:
611 wxComboControlBase* m_combo;
612 wxUint32 m_iFlags;
613
614 private:
615 // Called in wxComboControlBase::SetPopupControl
616 void InitBase(wxComboControlBase *combo)
617 {
618 m_combo = combo;
619 }
620 };
621
622
623 // ----------------------------------------------------------------------------
624 // include the platform-dependent header defining the real class
625 // ----------------------------------------------------------------------------
626
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"
631 #endif
632
633 // Any ports may need generic as an alternative
634 #include "wx/generic/combo.h"
635
636 #endif // wxUSE_COMBOCONTROL
637
638 #endif
639 // _WX_COMBOCONTROL_H_BASE_