]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/combo.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxComboCtrl and wxComboPopup
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 In order to use a custom popup with wxComboCtrl, an interface class must be
13 derived from wxComboPopup.
15 For more information on how to use it, see @ref comboctrl_custompopup.
26 Default constructor. It is recommended that internal variables are
27 prepared in Init() instead (because m_combo is not valid in
33 The derived class must implement this to create the popup control.
35 @return @true if the call succeeded, @false otherwise.
37 virtual bool Create(wxWindow
* parent
) = 0;
40 Utility function that hides the popup.
45 The derived class may implement this to return adjusted size for the
46 popup control, according to the variables given.
49 Preferred minimum width.
51 Preferred height. May be -1 to indicate no preference.
53 Max height for window, as limited by screen size.
55 @remarks This function is called each time popup is about to be shown.
57 virtual wxSize
GetAdjustedSize(int minWidth
, int prefHeight
, int maxHeight
);
60 Returns pointer to the associated parent wxComboCtrl.
62 wxComboCtrl
* GetComboCtrl() const;
65 The derived class must implement this to return pointer to the
66 associated control created in Create().
68 virtual wxWindow
* GetControl() = 0;
71 The derived class must implement this to return string representation
74 virtual wxString
GetStringValue() const = 0;
77 The derived class must implement this to initialize its internal
78 variables. This method is called immediately after construction
79 finishes. m_combo member variable has been initialized before the call.
84 Utility method that returns @true if Create has been called.
86 Useful in conjunction with LazyCreate().
88 bool IsCreated() const;
91 The derived class may implement this to return @true if it wants to
92 delay call to Create() until the popup is shown for the first time. It
93 is more efficient, but on the other hand it is often more convenient to
94 have the control created immediately.
96 @remarks Base implementation returns @false.
98 virtual bool LazyCreate();
101 The derived class may implement this to do something when the parent
102 wxComboCtrl gets double-clicked.
104 virtual void OnComboDoubleClick();
107 The derived class may implement this to receive key events from the
110 Events not handled should be skipped, as usual.
112 virtual void OnComboKeyEvent(wxKeyEvent
& event
);
115 The derived class may implement this to do special processing when
118 virtual void OnDismiss();
121 The derived class may implement this to do special processing when
124 virtual void OnPopup();
127 The derived class may implement this to paint the parent wxComboCtrl.
129 Default implementation draws value as string.
131 virtual void PaintComboControl(wxDC
& dc
, const wxRect
& rect
);
134 The derived class must implement this to receive string value changes
137 virtual void SetStringValue(const wxString
& value
);
141 Parent wxComboCtrl. This member variable is prepared automatically
142 before Init() is called.
144 wxComboCtrl
* m_combo
;
150 Features enabled for wxComboCtrl.
152 @see wxComboCtrl::GetFeatures()
154 struct wxComboCtrlFeatures
158 MovableButton
= 0x0001, ///< Button can be on either side of control.
159 BitmapButton
= 0x0002, ///< Button may be replaced with bitmap.
160 ButtonSpacing
= 0x0004, ///< Button can have spacing from the edge
162 TextIndent
= 0x0008, ///< wxComboCtrl::SetMargins() can be used.
163 PaintControl
= 0x0010, ///< Combo control itself can be custom painted.
164 PaintWritable
= 0x0020, ///< A variable-width area in front of writable
165 ///< combo control's textctrl can be custom
167 Borderless
= 0x0040, ///< wxNO_BORDER window style works.
169 All
= MovableButton
| BitmapButton
| ButtonSpacing
|
170 TextIndent
| PaintControl
| PaintWritable
|
171 Borderless
///< All features.
179 A combo control is a generic combobox that allows totally custom popup. In
180 addition it has other customization features. For instance, position and
181 size of the dropdown button can be changed.
183 @section comboctrl_custompopup Setting Custom Popup for wxComboCtrl
185 wxComboCtrl needs to be told somehow which control to use and this is done
186 by SetPopupControl(). However, we need something more than just a wxControl
187 in this method as, for example, we need to call
188 SetStringValue("initial text value") and wxControl doesn't have such
189 method. So we also need a wxComboPopup which is an interface which must be
190 implemented by a control to be usable as a popup.
192 We couldn't derive wxComboPopup from wxControl as this would make it
193 impossible to have a class deriving from a wxWidgets control and from it,
194 so instead it is just a mix-in.
196 Here's a minimal sample of wxListView popup:
199 #include <wx/combo.h>
200 #include <wx/listctrl.h>
202 class wxListViewComboPopup : public wxListView, public wxComboPopup
205 // Initialize member variables
211 // Create popup control
212 virtual bool Create(wxWindow* parent)
214 return wxListView::Create(parent,1,wxPoint(0,0),wxDefaultSize);
217 // Return pointer to the created control
218 virtual wxWindow *GetControl() { return this; }
220 // Translate string into a list selection
221 virtual void SetStringValue(const wxString& s)
223 int n = wxListView::FindItem(-1,s);
224 if ( n >= 0 && n < wxListView::GetItemCount() )
225 wxListView::Select(n);
228 // Get list selection as a string
229 virtual wxString GetStringValue() const
232 return wxListView::GetItemText(m_value);
233 return wxEmptyString;
236 // Do mouse hot-tracking (which is typical in list popups)
237 void OnMouseMove(wxMouseEvent& event)
239 // TODO: Move selection to cursor
242 // On mouse left up, set the value and close the popup
243 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
245 m_value = wxListView::GetFirstSelected();
247 // TODO: Send event as well
254 int m_value; // current item index
257 DECLARE_EVENT_TABLE()
260 BEGIN_EVENT_TABLE(wxListViewComboPopup, wxListView)
261 EVT_MOTION(wxListViewComboPopup::OnMouseMove)
262 EVT_LEFT_UP(wxListViewComboPopup::OnMouseClick)
266 Here's how you would create and populate it in a dialog constructor:
269 wxComboCtrl* comboCtrl = new wxComboCtrl(this, wxID_ANY, wxEmptyString);
271 wxListViewComboPopup* popupCtrl = new wxListViewComboPopup();
273 // It is important to call SetPopupControl() as soon as possible
274 comboCtrl->SetPopupControl(popupCtrl);
276 // Populate using wxListView methods
277 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "First Item");
278 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "Second Item");
279 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "Third Item");
283 @style{wxCB_READONLY}
284 Text will not be editable.
286 Sorts the entries in the list alphabetically.
287 @style{wxTE_PROCESS_ENTER}
288 The control will generate the event wxEVT_COMMAND_TEXT_ENTER
289 (otherwise pressing Enter key is either processed internally by the
290 control or used for navigation between dialog controls). Windows
292 @style{wxCC_SPECIAL_DCLICK}
293 Double-clicking triggers a call to popup's OnComboDoubleClick.
294 Actual behaviour is defined by a derived class. For instance,
295 wxOwnerDrawnComboBox will cycle an item. This style only applies if
296 wxCB_READONLY is used as well.
297 @style{wxCC_STD_BUTTON}
298 Drop button will behave more like a standard push button.
301 @beginEventEmissionTable{wxCommandEvent}
302 @event{EVT_TEXT(id, func)}
303 Process a wxEVT_COMMAND_TEXT_UPDATED event, when the text changes.
304 @event{EVT_TEXT_ENTER(id, func)}
305 Process a wxEVT_COMMAND_TEXT_ENTER event, when RETURN is pressed in
307 @event{EVT_COMBOBOX_DROPDOWN(id, func)}
308 Process a wxEVT_COMMAND_COMBOBOX_DROPDOWN event, which is generated
309 when the popup window is shown (drops down).
310 @event{EVT_COMBOBOX_CLOSEUP(id, func)}
311 Process a wxEVT_COMMAND_COMBOBOX_CLOSEUP event, which is generated
312 when the popup window of the combo control disappears (closes up).
313 You should avoid adding or deleting items in this event.
318 @appearance{comboctrl.png}
320 @see wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup,
323 class wxComboCtrl
: public wxControl
332 Constructor, creating and showing a combo control.
335 Parent window. Must not be @NULL.
337 Window identifier. The value wxID_ANY indicates a default value.
339 Initial selection string. An empty string indicates no selection.
342 If ::wxDefaultPosition is specified then a default position is chosen.
345 If ::wxDefaultSize is specified then the window is sized appropriately.
347 Window style. See wxComboCtrl.
353 @see Create(), wxValidator
355 wxComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
,
356 const wxString
& value
= wxEmptyString
,
357 const wxPoint
& pos
= wxDefaultPosition
,
358 const wxSize
& size
= wxDefaultSize
,
360 const wxValidator
& validator
= wxDefaultValidator
,
361 const wxString
& name
= wxComboBoxNameStr
);
364 Destructor, destroying the combo control.
366 virtual ~wxComboCtrl();
369 Copies the selected text to the clipboard.
374 Creates the combo control for two-step construction. Derived classes
375 should call or replace this function. See wxComboCtrl() for further
378 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
,
379 const wxString
& value
= wxEmptyString
,
380 const wxPoint
& pos
= wxDefaultPosition
,
381 const wxSize
& size
= wxDefaultSize
,
383 const wxValidator
& validator
= wxDefaultValidator
,
384 const wxString
& name
= wxComboBoxNameStr
);
387 Copies the selected text to the clipboard and removes the selection.
392 Enables or disables popup animation, if any, depending on the value of
395 void EnablePopupAnimation(bool enable
= true);
398 Returns disabled button bitmap that has been set with
401 @return A reference to the disabled state bitmap.
403 const wxBitmap
& GetBitmapDisabled() const;
406 Returns button mouse hover bitmap that has been set with
409 @return A reference to the mouse hover state bitmap.
411 const wxBitmap
& GetBitmapHover() const;
414 Returns default button bitmap that has been set with
417 @return A reference to the normal state bitmap.
419 const wxBitmap
& GetBitmapNormal() const;
422 Returns depressed button bitmap that has been set with
425 @return A reference to the depressed state bitmap.
427 const wxBitmap
& GetBitmapPressed() const;
430 Returns current size of the dropdown button.
432 wxSize
GetButtonSize();
435 Returns custom painted area in control.
437 @see SetCustomPaintWidth().
439 int GetCustomPaintWidth() const;
442 Returns features supported by wxComboCtrl. If needed feature is
443 missing, you need to instead use wxGenericComboCtrl, which however may
444 lack a native look and feel (but otherwise sports identical API).
446 @return Value returned is a combination of the flags defined in
449 static int GetFeatures();
452 Returns the current hint string.
454 See SetHint() for more information about hints.
458 virtual wxString
GetHint() const;
461 Returns the insertion point for the combo control's text field.
463 @note Under Windows, this function always returns 0 if the combo
464 control doesn't have the focus.
466 virtual long GetInsertionPoint() const;
469 Returns the last position in the combo control text field.
471 virtual long GetLastPosition() const;
474 Returns the margins used by the control. The @c x field of the returned
475 point is the horizontal margin and the @c y field is the vertical one.
477 @remarks If given margin cannot be accurately determined, its value
484 wxPoint
GetMargins() const;
487 Returns current popup interface that has been set with
490 wxComboPopup
* GetPopupControl();
493 Returns popup window containing the popup control.
495 wxWindow
* GetPopupWindow() const;
498 Get the text control which is part of the combo control.
500 wxTextCtrl
* GetTextCtrl() const;
503 Returns actual indentation in pixels.
505 @deprecated Use GetMargins() instead.
507 wxCoord
GetTextIndent() const;
510 Returns area covered by the text field (includes everything except
511 borders and the dropdown button).
513 const wxRect
& GetTextRect() const;
516 Returns text representation of the current value. For writable combo
517 control it always returns the value in the text field.
519 virtual wxString
GetValue() const;
522 Dismisses the popup window.
525 Set this to @true in order to generate
526 wxEVT_COMMAND_COMBOBOX_CLOSEUP event.
528 virtual void HidePopup(bool generateEvent
=false);
531 Returns @true if the popup is currently shown
533 bool IsPopupShown() const;
536 Returns @true if the popup window is in the given state. Possible
540 @row2col{wxComboCtrl::Hidden, Popup window is hidden.}
541 @row2col{wxComboCtrl::Animating, Popup window is being shown, but the
542 popup animation has not yet finished.}
543 @row2col{wxComboCtrl::Visible, Popup window is fully visible.}
546 bool IsPopupWindowState(int state
) const;
549 Implement in a derived class to define what happens on dropdown button
550 click. Default action is to show the popup.
552 @note If you implement this to do something else than show the popup,
553 you must then also implement DoSetPopupControl() to always return
556 virtual void OnButtonClick();
559 Pastes text from the clipboard to the text field.
561 virtual void Paste();
564 Removes the text between the two positions in the combo control text
572 virtual void Remove(long from
, long to
);
575 Replaces the text between two positions with the given text, in the
576 combo control text field.
585 virtual void Replace(long from
, long to
, const wxString
& text
);
588 Sets custom dropdown button graphics.
591 Default button image.
593 If @true, blank push button background is painted below the image.
595 Depressed button image.
597 Button image when mouse hovers above it. This should be ignored on
598 platforms and themes that do not generally draw different kind of
599 button on mouse hover.
601 Disabled button image.
603 void SetButtonBitmaps(const wxBitmap
& bmpNormal
,
604 bool pushButtonBg
= false,
605 const wxBitmap
& bmpPressed
= wxNullBitmap
,
606 const wxBitmap
& bmpHover
= wxNullBitmap
,
607 const wxBitmap
& bmpDisabled
= wxNullBitmap
);
610 Sets size and position of dropdown button.
613 Button width. Value = 0 specifies default.
615 Button height. Value = 0 specifies default.
617 Indicates which side the button will be placed. Value can be wxLEFT
620 Horizontal spacing around the button. Default is 0.
622 void SetButtonPosition(int width
= -1, int height
= -1,
623 int side
= wxRIGHT
, int spacingX
= 0);
626 Set width, in pixels, of custom painted area in control without
627 @c wxCB_READONLY style. In read-only wxOwnerDrawnComboBox, this is used
628 to indicate area that is not covered by the focus rectangle.
630 void SetCustomPaintWidth(int width
);
633 Sets a hint shown in an empty unfocused combo control.
635 Notice that hints are known as <em>cue banners</em> under MSW or
636 <em>placeholder strings</em> under OS X.
638 @see wxTextEntry::SetHint()
642 virtual void SetHint(const wxString
& hint
);
645 Sets the insertion point in the text field.
648 The new insertion point.
650 virtual void SetInsertionPoint(long pos
);
653 Sets the insertion point at the end of the combo control text field.
655 virtual void SetInsertionPointEnd();
659 Attempts to set the control margins. When margins are given as wxPoint,
660 x indicates the left and y the top margin. Use -1 to indicate that
661 an existing value should be used.
664 @true if setting of all requested margins was successful.
668 bool SetMargins(const wxPoint
& pt
);
669 bool SetMargins(wxCoord left
, wxCoord top
= -1);
673 Set side of the control to which the popup will align itself. Valid
674 values are @c wxLEFT, @c wxRIGHT and 0. The default value 0 means that
675 the most appropriate side is used (which, currently, is always
678 void SetPopupAnchor(int anchorSide
);
681 Set popup interface class derived from wxComboPopup. This method should
682 be called as soon as possible after the control has been created,
683 unless OnButtonClick() has been overridden.
685 void SetPopupControl(wxComboPopup
* popup
);
688 Extends popup size horizontally, relative to the edges of the combo
692 How many pixel to extend beyond the left edge of the control.
695 How many pixel to extend beyond the right edge of the control.
698 @remarks Popup minimum width may override arguments. It is up to the
699 popup to fully take this into account.
701 void SetPopupExtents(int extLeft
, int extRight
);
704 Sets preferred maximum height of the popup.
706 @remarks Value -1 indicates the default.
708 void SetPopupMaxHeight(int height
);
711 Sets minimum width of the popup. If wider than combo control, it will
714 @remarks Value -1 indicates the default. Also, popup implementation may
715 choose to ignore this.
717 void SetPopupMinWidth(int width
);
720 Selects the text between the two positions, in the combo control text
728 virtual void SetSelection(long from
, long to
);
731 Sets the text for the text field without affecting the popup. Thus,
732 unlike SetValue(), it works equally well with combo control using
733 @c wxCB_READONLY style.
735 void SetText(const wxString
& value
);
738 This will set the space in pixels between left edge of the control and
739 the text, regardless whether control is read-only or not. Value -1 can
740 be given to indicate platform default.
742 @deprecated Use SetMargins() instead.
744 void SetTextIndent(int indent
);
747 Sets the text for the combo control text field.
749 @note For a combo control with @c wxCB_READONLY style the string must
750 be accepted by the popup (for instance, exist in the dropdown
751 list), otherwise the call to SetValue() is ignored.
753 virtual void SetValue(const wxString
& value
);
756 Same as SetValue(), but also sends wxCommandEvent of type
757 wxEVT_COMMAND_TEXT_UPDATED if @a withEvent is @true.
759 void SetValueWithEvent(const wxString
& value
, bool withEvent
= true);
764 virtual void ShowPopup();
767 Undoes the last edit in the text field. Windows only.
772 Enable or disable usage of an alternative popup window, which
773 guarantees ability to focus the popup control, and allows common native
774 controls to function normally. This alternative popup window is usually
775 a wxDialog, and as such, when it is shown, its parent top-level window
776 will appear as if the focus has been lost from it.
778 void UseAltPopupWindow(bool enable
= true);
783 This member function is not normally called in application code.
784 Instead, it can be implemented in a derived class to create a custom
787 The parameters are the same as those for DoShowPopup().
789 @return @true if animation finishes before the function returns,
790 @false otherwise. In the latter case you need to manually call
791 DoShowPopup() after the animation ends.
793 virtual bool AnimateShow(const wxRect
& rect
, int flags
);
796 This member function is not normally called in application code.
797 Instead, it can be implemented in a derived class to return default
798 wxComboPopup, incase @a popup is @NULL.
800 @note If you have implemented OnButtonClick() to do something else than
801 show the popup, then DoSetPopupControl() must always set @a popup
804 virtual void DoSetPopupControl(wxComboPopup
* popup
);
807 This member function is not normally called in application code.
808 Instead, it must be called in a derived class to make sure popup is
809 properly shown after a popup animation has finished (but only if
810 AnimateShow() did not finish the animation within its function scope).
813 Position to show the popup window at, in screen coordinates.
815 Combination of any of the following:
817 @row2col{wxComboCtrl::ShowAbove,
818 Popup is shown above the control instead of below.}
819 @row2col{wxComboCtrl::CanDeferShow,
820 Showing the popup can be deferred to happen sometime after
821 ShowPopup() has finished. In this case, AnimateShow() must
825 virtual void DoShowPopup(const wxRect
& rect
, int flags
);