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