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