]>
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 licence
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 You only need to implement this member function if you create
41 your popup class in non-standard way. The default implementation can
42 handle both multiple-inherited popup control (as seen in wxComboCtrl
43 samples) and one allocated separately in heap.
45 If you do completely re-implement this function, make sure it calls
46 Destroy() for the popup control and also deletes @a this object
47 (usually as the last thing).
49 virtual void DestroyPopup();
52 Utility function that hides the popup.
57 Implement to customize matching of value string to an item container
61 String entered, usually by user or from SetValue() call.
64 When item matches an entry, but the entry's string representation
65 is not exactly the same (case mismatch, for example), then the
66 true item string should be written back to here, if it is not
70 Default implementation always return true and does not alter
73 virtual bool FindItem(const wxString
& item
, wxString
* trueItem
=NULL
);
76 The derived class may implement this to return adjusted size for the
77 popup control, according to the variables given.
80 Preferred minimum width.
82 Preferred height. May be -1 to indicate no preference.
84 Max height for window, as limited by screen size.
86 @remarks This function is called each time popup is about to be shown.
88 virtual wxSize
GetAdjustedSize(int minWidth
, int prefHeight
, int maxHeight
);
91 Returns pointer to the associated parent wxComboCtrl.
93 wxComboCtrl
* GetComboCtrl() const;
96 The derived class must implement this to return pointer to the
97 associated control created in Create().
99 virtual wxWindow
* GetControl() = 0;
102 The derived class must implement this to return string representation
105 virtual wxString
GetStringValue() const = 0;
108 The derived class must implement this to initialize its internal
109 variables. This method is called immediately after construction
110 finishes. m_combo member variable has been initialized before the call.
115 Utility method that returns @true if Create has been called.
117 Useful in conjunction with LazyCreate().
119 bool IsCreated() const;
122 The derived class may implement this to return @true if it wants to
123 delay call to Create() until the popup is shown for the first time. It
124 is more efficient, but on the other hand it is often more convenient to
125 have the control created immediately.
127 @remarks Base implementation returns @false.
129 virtual bool LazyCreate();
132 The derived class may implement this to do something when the parent
133 wxComboCtrl gets double-clicked.
135 virtual void OnComboDoubleClick();
138 The derived class may implement this to receive key events from the
141 Events not handled should be skipped, as usual.
143 virtual void OnComboKeyEvent(wxKeyEvent
& event
);
146 The derived class may implement this to do special processing when
149 virtual void OnDismiss();
152 The derived class may implement this to do special processing when
155 virtual void OnPopup();
158 The derived class may implement this to paint the parent wxComboCtrl.
160 Default implementation draws value as string.
162 virtual void PaintComboControl(wxDC
& dc
, const wxRect
& rect
);
165 The derived class must implement this to receive string value changes
168 virtual void SetStringValue(const wxString
& value
);
172 Parent wxComboCtrl. This member variable is prepared automatically
173 before Init() is called.
175 wxComboCtrl
* m_combo
;
181 Features enabled for wxComboCtrl.
183 @see wxComboCtrl::GetFeatures()
185 struct wxComboCtrlFeatures
189 MovableButton
= 0x0001, ///< Button can be on either side of control.
190 BitmapButton
= 0x0002, ///< Button may be replaced with bitmap.
191 ButtonSpacing
= 0x0004, ///< Button can have spacing from the edge
193 TextIndent
= 0x0008, ///< wxComboCtrl::SetMargins() can be used.
194 PaintControl
= 0x0010, ///< Combo control itself can be custom painted.
195 PaintWritable
= 0x0020, ///< A variable-width area in front of writable
196 ///< combo control's textctrl can be custom
198 Borderless
= 0x0040, ///< wxNO_BORDER window style works.
200 All
= MovableButton
| BitmapButton
| ButtonSpacing
|
201 TextIndent
| PaintControl
| PaintWritable
|
202 Borderless
///< All features.
210 A combo control is a generic combobox that allows totally custom popup. In
211 addition it has other customization features. For instance, position and
212 size of the dropdown button can be changed.
214 @section comboctrl_custompopup Setting Custom Popup for wxComboCtrl
216 wxComboCtrl needs to be told somehow which control to use and this is done
217 by SetPopupControl(). However, we need something more than just a wxControl
218 in this method as, for example, we need to call
219 SetStringValue("initial text value") and wxControl doesn't have such
220 method. So we also need a wxComboPopup which is an interface which must be
221 implemented by a control to be usable as a popup.
223 We couldn't derive wxComboPopup from wxControl as this would make it
224 impossible to have a class deriving from a wxWidgets control and from it,
225 so instead it is just a mix-in.
227 Here's a minimal sample of wxListView popup:
230 #include <wx/combo.h>
231 #include <wx/listctrl.h>
233 class wxListViewComboPopup : public wxListView, public wxComboPopup
236 // Initialize member variables
242 // Create popup control
243 virtual bool Create(wxWindow* parent)
245 return wxListView::Create(parent,1,wxPoint(0,0),wxDefaultSize);
248 // Return pointer to the created control
249 virtual wxWindow *GetControl() { return this; }
251 // Translate string into a list selection
252 virtual void SetStringValue(const wxString& s)
254 int n = wxListView::FindItem(-1,s);
255 if ( n >= 0 && n < wxListView::GetItemCount() )
256 wxListView::Select(n);
259 // Get list selection as a string
260 virtual wxString GetStringValue() const
263 return wxListView::GetItemText(m_value);
264 return wxEmptyString;
267 // Do mouse hot-tracking (which is typical in list popups)
268 void OnMouseMove(wxMouseEvent& event)
270 // TODO: Move selection to cursor
273 // On mouse left up, set the value and close the popup
274 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
276 m_value = wxListView::GetFirstSelected();
278 // TODO: Send event as well
285 int m_value; // current item index
288 wxDECLARE_EVENT_TABLE();
291 wxBEGIN_EVENT_TABLE(wxListViewComboPopup, wxListView)
292 EVT_MOTION(wxListViewComboPopup::OnMouseMove)
293 EVT_LEFT_UP(wxListViewComboPopup::OnMouseClick)
297 Here's how you would create and populate it in a dialog constructor:
300 wxComboCtrl* comboCtrl = new wxComboCtrl(this, wxID_ANY, wxEmptyString);
302 wxListViewComboPopup* popupCtrl = new wxListViewComboPopup();
304 // It is important to call SetPopupControl() as soon as possible
305 comboCtrl->SetPopupControl(popupCtrl);
307 // Populate using wxListView methods
308 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "First Item");
309 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "Second Item");
310 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "Third Item");
314 @style{wxCB_READONLY}
315 Text will not be editable.
317 Sorts the entries in the list alphabetically.
318 @style{wxTE_PROCESS_ENTER}
319 The control will generate the event @c wxEVT_COMMAND_TEXT_ENTER
320 (otherwise pressing Enter key is either processed internally by the
321 control or used for navigation between dialog controls). Windows
323 @style{wxCC_SPECIAL_DCLICK}
324 Double-clicking triggers a call to popup's OnComboDoubleClick.
325 Actual behaviour is defined by a derived class. For instance,
326 wxOwnerDrawnComboBox will cycle an item. This style only applies if
327 wxCB_READONLY is used as well.
328 @style{wxCC_STD_BUTTON}
329 Drop button will behave more like a standard push button.
332 @beginEventEmissionTable{wxCommandEvent}
333 @event{EVT_TEXT(id, func)}
334 Process a @c wxEVT_COMMAND_TEXT_UPDATED event, when the text changes.
335 @event{EVT_TEXT_ENTER(id, func)}
336 Process a @c wxEVT_COMMAND_TEXT_ENTER event, when RETURN is pressed in
338 @event{EVT_COMBOBOX_DROPDOWN(id, func)}
339 Process a @c wxEVT_COMMAND_COMBOBOX_DROPDOWN event, which is generated
340 when the popup window is shown (drops down).
341 @event{EVT_COMBOBOX_CLOSEUP(id, func)}
342 Process a @c wxEVT_COMMAND_COMBOBOX_CLOSEUP event, which is generated
343 when the popup window of the combo control disappears (closes up).
344 You should avoid adding or deleting items in this event.
349 @appearance{comboctrl.png}
351 @see wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup,
354 class wxComboCtrl
: public wxControl
,
364 Constructor, creating and showing a combo control.
367 Parent window. Must not be @NULL.
369 Window identifier. The value wxID_ANY indicates a default value.
371 Initial selection string. An empty string indicates no selection.
374 If ::wxDefaultPosition is specified then a default position is chosen.
377 If ::wxDefaultSize is specified then the window is sized appropriately.
379 Window style. See wxComboCtrl.
385 @see Create(), wxValidator
387 wxComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
,
388 const wxString
& value
= wxEmptyString
,
389 const wxPoint
& pos
= wxDefaultPosition
,
390 const wxSize
& size
= wxDefaultSize
,
392 const wxValidator
& validator
= wxDefaultValidator
,
393 const wxString
& name
= wxComboBoxNameStr
);
396 Destructor, destroying the combo control.
398 virtual ~wxComboCtrl();
401 Copies the selected text to the clipboard.
406 Creates the combo control for two-step construction. Derived classes
407 should call or replace this function. See wxComboCtrl() for further
410 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
,
411 const wxString
& value
= wxEmptyString
,
412 const wxPoint
& pos
= wxDefaultPosition
,
413 const wxSize
& size
= wxDefaultSize
,
415 const wxValidator
& validator
= wxDefaultValidator
,
416 const wxString
& name
= wxComboBoxNameStr
);
419 Copies the selected text to the clipboard and removes the selection.
424 Dismisses the popup window.
426 Notice that calling this function will generate a
427 @c wxEVT_COMMAND_COMBOBOX_CLOSEUP event.
431 virtual void Dismiss();
435 Enables or disables popup animation, if any, depending on the value of
438 void EnablePopupAnimation(bool enable
= true);
441 Returns disabled button bitmap that has been set with
444 @return A reference to the disabled state bitmap.
446 const wxBitmap
& GetBitmapDisabled() const;
449 Returns button mouse hover bitmap that has been set with
452 @return A reference to the mouse hover state bitmap.
454 const wxBitmap
& GetBitmapHover() const;
457 Returns default button bitmap that has been set with
460 @return A reference to the normal state bitmap.
462 const wxBitmap
& GetBitmapNormal() const;
465 Returns depressed button bitmap that has been set with
468 @return A reference to the depressed state bitmap.
470 const wxBitmap
& GetBitmapPressed() const;
473 Returns current size of the dropdown button.
475 wxSize
GetButtonSize();
478 Returns custom painted area in control.
480 @see SetCustomPaintWidth().
482 int GetCustomPaintWidth() const;
485 Returns features supported by wxComboCtrl. If needed feature is
486 missing, you need to instead use wxGenericComboCtrl, which however may
487 lack a native look and feel (but otherwise sports identical API).
489 @return Value returned is a combination of the flags defined in
492 static int GetFeatures();
495 Returns the current hint string.
497 See SetHint() for more information about hints.
501 virtual wxString
GetHint() const;
504 Returns the insertion point for the combo control's text field.
506 @note Under Windows, this function always returns 0 if the combo
507 control doesn't have the focus.
509 virtual long GetInsertionPoint() const;
512 Returns the last position in the combo control text field.
514 virtual long GetLastPosition() const;
517 Returns the margins used by the control. The @c x field of the returned
518 point is the horizontal margin and the @c y field is the vertical one.
520 @remarks If given margin cannot be accurately determined, its value
527 wxPoint
GetMargins() const;
530 Returns current popup interface that has been set with
533 wxComboPopup
* GetPopupControl();
536 Returns popup window containing the popup control.
538 wxWindow
* GetPopupWindow() const;
541 Get the text control which is part of the combo control.
543 wxTextCtrl
* GetTextCtrl() const;
546 Returns actual indentation in pixels.
548 @deprecated Use GetMargins() instead.
550 wxCoord
GetTextIndent() const;
553 Returns area covered by the text field (includes everything except
554 borders and the dropdown button).
556 const wxRect
& GetTextRect() const;
559 Returns text representation of the current value. For writable combo
560 control it always returns the value in the text field.
562 virtual wxString
GetValue() const;
565 Dismisses the popup window.
568 Set this to @true in order to generate
569 @c wxEVT_COMMAND_COMBOBOX_CLOSEUP event.
571 @deprecated Use Dismiss() instead.
573 virtual void HidePopup(bool generateEvent
=false);
576 Returns @true if the popup is currently shown
578 bool IsPopupShown() const;
581 Returns @true if the popup window is in the given state. Possible
585 @row2col{wxComboCtrl::Hidden, Popup window is hidden.}
586 @row2col{wxComboCtrl::Animating, Popup window is being shown, but the
587 popup animation has not yet finished.}
588 @row2col{wxComboCtrl::Visible, Popup window is fully visible.}
591 bool IsPopupWindowState(int state
) const;
594 Implement in a derived class to define what happens on dropdown button
595 click. Default action is to show the popup.
597 @note If you implement this to do something else than show the popup,
598 you must then also implement DoSetPopupControl() to always return
601 virtual void OnButtonClick();
604 Pastes text from the clipboard to the text field.
606 virtual void Paste();
609 Shows the popup portion of the combo control.
611 Notice that calling this function will generate a
612 @c wxEVT_COMMAND_COMBOBOX_DROPDOWN event.
616 virtual void Popup();
619 Removes the text between the two positions in the combo control text
627 virtual void Remove(long from
, long to
);
630 Replaces the text between two positions with the given text, in the
631 combo control text field.
640 virtual void Replace(long from
, long to
, const wxString
& text
);
643 Sets custom dropdown button graphics.
646 Default button image.
648 If @true, blank push button background is painted below the image.
650 Depressed button image.
652 Button image when mouse hovers above it. This should be ignored on
653 platforms and themes that do not generally draw different kind of
654 button on mouse hover.
656 Disabled button image.
658 void SetButtonBitmaps(const wxBitmap
& bmpNormal
,
659 bool pushButtonBg
= false,
660 const wxBitmap
& bmpPressed
= wxNullBitmap
,
661 const wxBitmap
& bmpHover
= wxNullBitmap
,
662 const wxBitmap
& bmpDisabled
= wxNullBitmap
);
665 Sets size and position of dropdown button.
668 Button width. Value = 0 specifies default.
670 Button height. Value = 0 specifies default.
672 Indicates which side the button will be placed. Value can be wxLEFT
675 Horizontal spacing around the button. Default is 0.
677 void SetButtonPosition(int width
= -1, int height
= -1,
678 int side
= wxRIGHT
, int spacingX
= 0);
681 Set width, in pixels, of custom painted area in control without
682 @c wxCB_READONLY style. In read-only wxOwnerDrawnComboBox, this is used
683 to indicate area that is not covered by the focus rectangle.
685 void SetCustomPaintWidth(int width
);
688 Sets a hint shown in an empty unfocused combo control.
690 Notice that hints are known as <em>cue banners</em> under MSW or
691 <em>placeholder strings</em> under OS X.
693 @see wxTextEntry::SetHint()
697 virtual bool SetHint(const wxString
& hint
);
700 Sets the insertion point in the text field.
703 The new insertion point.
705 virtual void SetInsertionPoint(long pos
);
708 Sets the insertion point at the end of the combo control text field.
710 virtual void SetInsertionPointEnd();
714 Attempts to set the control margins. When margins are given as wxPoint,
715 x indicates the left and y the top margin. Use -1 to indicate that
716 an existing value should be used.
719 @true if setting of all requested margins was successful.
723 bool SetMargins(const wxPoint
& pt
);
724 bool SetMargins(wxCoord left
, wxCoord top
= -1);
728 Set side of the control to which the popup will align itself. Valid
729 values are @c wxLEFT, @c wxRIGHT and 0. The default value 0 means that
730 the most appropriate side is used (which, currently, is always
733 void SetPopupAnchor(int anchorSide
);
736 Set popup interface class derived from wxComboPopup. This method should
737 be called as soon as possible after the control has been created,
738 unless OnButtonClick() has been overridden.
740 void SetPopupControl(wxComboPopup
* popup
);
743 Extends popup size horizontally, relative to the edges of the combo
747 How many pixel to extend beyond the left edge of the control.
750 How many pixel to extend beyond the right edge of the control.
753 @remarks Popup minimum width may override arguments. It is up to the
754 popup to fully take this into account.
756 void SetPopupExtents(int extLeft
, int extRight
);
759 Sets preferred maximum height of the popup.
761 @remarks Value -1 indicates the default.
763 void SetPopupMaxHeight(int height
);
766 Sets minimum width of the popup. If wider than combo control, it will
769 @remarks Value -1 indicates the default. Also, popup implementation may
770 choose to ignore this.
772 void SetPopupMinWidth(int width
);
775 Selects the text between the two positions, in the combo control text
783 virtual void SetSelection(long from
, long to
);
786 Sets the text for the text field without affecting the popup. Thus,
787 unlike SetValue(), it works equally well with combo control using
788 @c wxCB_READONLY style.
790 void SetText(const wxString
& value
);
793 Set a custom window style for the embedded wxTextCtrl. Usually you
794 will need to use this during two-step creation, just before Create().
798 wxComboCtrl* comboCtrl = new wxComboCtrl();
800 // Let's make the text right-aligned
801 comboCtrl->SetTextCtrlStyle(wxTE_RIGHT);
803 comboCtrl->Create(parent, wxID_ANY, wxEmptyString);
806 void SetTextCtrlStyle( int style
);
809 This will set the space in pixels between left edge of the control and
810 the text, regardless whether control is read-only or not. Value -1 can
811 be given to indicate platform default.
813 @deprecated Use SetMargins() instead.
815 void SetTextIndent(int indent
);
818 Sets the text for the combo control text field.
820 @note For a combo control with @c wxCB_READONLY style the string must
821 be accepted by the popup (for instance, exist in the dropdown
822 list), otherwise the call to SetValue() is ignored.
824 virtual void SetValue(const wxString
& value
);
829 @deprecated Use Popup() instead.
831 virtual void ShowPopup();
834 Undoes the last edit in the text field. Windows only.
839 Enable or disable usage of an alternative popup window, which
840 guarantees ability to focus the popup control, and allows common native
841 controls to function normally. This alternative popup window is usually
842 a wxDialog, and as such, when it is shown, its parent top-level window
843 will appear as if the focus has been lost from it.
845 void UseAltPopupWindow(bool enable
= true);
850 This member function is not normally called in application code.
851 Instead, it can be implemented in a derived class to create a custom
854 The parameters are the same as those for DoShowPopup().
856 @return @true if animation finishes before the function returns,
857 @false otherwise. In the latter case you need to manually call
858 DoShowPopup() after the animation ends.
860 virtual bool AnimateShow(const wxRect
& rect
, int flags
);
863 This member function is not normally called in application code.
864 Instead, it can be implemented in a derived class to return default
865 wxComboPopup, in case @a popup is @NULL.
867 @note If you have implemented OnButtonClick() to do something else than
868 show the popup, then DoSetPopupControl() must always set @a popup
871 virtual void DoSetPopupControl(wxComboPopup
* popup
);
874 This member function is not normally called in application code.
875 Instead, it must be called in a derived class to make sure popup is
876 properly shown after a popup animation has finished (but only if
877 AnimateShow() did not finish the animation within its function scope).
880 Position to show the popup window at, in screen coordinates.
882 Combination of any of the following:
884 @row2col{wxComboCtrl::ShowAbove,
885 Popup is shown above the control instead of below.}
886 @row2col{wxComboCtrl::CanDeferShow,
887 Showing the popup can be deferred to happen sometime after
888 ShowPopup() has finished. In this case, AnimateShow() must
892 virtual void DoShowPopup(const wxRect
& rect
, int flags
);