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