]>
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 /////////////////////////////////////////////////////////////////////////////
10 // New window styles for wxComboCtrlBase
14 // Double-clicking a read-only combo triggers call to popup's OnComboPopup.
15 // In wxOwnerDrawnComboBox, for instance, it cycles item.
16 wxCC_SPECIAL_DCLICK
= 0x0100,
18 // Dropbutton acts like standard push button.
19 wxCC_STD_BUTTON
= 0x0200
26 In order to use a custom popup with wxComboCtrl, an interface class must be
27 derived from wxComboPopup.
29 For more information on how to use it, see @ref comboctrl_custompopup.
40 Default constructor. It is recommended that internal variables are
41 prepared in Init() instead (because m_combo is not valid in
47 The derived class must implement this to create the popup control.
49 @return @true if the call succeeded, @false otherwise.
51 virtual bool Create(wxWindow
* parent
) = 0;
54 You only need to implement this member function if you create
55 your popup class in non-standard way. The default implementation can
56 handle both multiple-inherited popup control (as seen in wxComboCtrl
57 samples) and one allocated separately in heap.
59 If you do completely re-implement this function, make sure it calls
60 Destroy() for the popup control and also deletes @a this object
61 (usually as the last thing).
63 virtual void DestroyPopup();
66 Utility function that hides the popup.
71 Implement to customize matching of value string to an item container
75 String entered, usually by user or from SetValue() call.
78 When item matches an entry, but the entry's string representation
79 is not exactly the same (case mismatch, for example), then the
80 true item string should be written back to here, if it is not
84 Default implementation always return true and does not alter
87 virtual bool FindItem(const wxString
& item
, wxString
* trueItem
=NULL
);
90 The derived class may implement this to return adjusted size for the
91 popup control, according to the variables given.
94 Preferred minimum width.
96 Preferred height. May be -1 to indicate no preference.
98 Max height for window, as limited by screen size.
100 @remarks This function is called each time popup is about to be shown.
102 virtual wxSize
GetAdjustedSize(int minWidth
, int prefHeight
, int maxHeight
);
105 Returns pointer to the associated parent wxComboCtrl.
107 wxComboCtrl
* GetComboCtrl() const;
110 The derived class must implement this to return pointer to the
111 associated control created in Create().
113 virtual wxWindow
* GetControl() = 0;
116 The derived class must implement this to return string representation
119 virtual wxString
GetStringValue() const = 0;
122 The derived class must implement this to initialize its internal
123 variables. This method is called immediately after construction
124 finishes. m_combo member variable has been initialized before the call.
129 Utility method that returns @true if Create has been called.
131 Useful in conjunction with LazyCreate().
133 bool IsCreated() const;
136 The derived class may implement this to return @true if it wants to
137 delay call to Create() until the popup is shown for the first time. It
138 is more efficient, but on the other hand it is often more convenient to
139 have the control created immediately.
141 @remarks Base implementation returns @false.
143 virtual bool LazyCreate();
146 The derived class may implement this to do something when the parent
147 wxComboCtrl gets double-clicked.
149 virtual void OnComboDoubleClick();
152 The derived class may implement this to receive key events from the
155 Events not handled should be skipped, as usual.
157 virtual void OnComboKeyEvent(wxKeyEvent
& event
);
160 The derived class may implement this to do special processing when
163 virtual void OnDismiss();
166 The derived class may implement this to do special processing when
169 virtual void OnPopup();
172 The derived class may implement this to paint the parent wxComboCtrl.
174 Default implementation draws value as string.
176 virtual void PaintComboControl(wxDC
& dc
, const wxRect
& rect
);
179 The derived class must implement this to receive string value changes
182 virtual void SetStringValue(const wxString
& value
);
186 Parent wxComboCtrl. This member variable is prepared automatically
187 before Init() is called.
189 wxComboCtrl
* m_combo
;
195 Features enabled for wxComboCtrl.
197 @see wxComboCtrl::GetFeatures()
199 struct wxComboCtrlFeatures
203 MovableButton
= 0x0001, ///< Button can be on either side of control.
204 BitmapButton
= 0x0002, ///< Button may be replaced with bitmap.
205 ButtonSpacing
= 0x0004, ///< Button can have spacing from the edge
207 TextIndent
= 0x0008, ///< wxComboCtrl::SetMargins() can be used.
208 PaintControl
= 0x0010, ///< Combo control itself can be custom painted.
209 PaintWritable
= 0x0020, ///< A variable-width area in front of writable
210 ///< combo control's textctrl can be custom
212 Borderless
= 0x0040, ///< wxNO_BORDER window style works.
214 All
= MovableButton
| BitmapButton
| ButtonSpacing
|
215 TextIndent
| PaintControl
| PaintWritable
|
216 Borderless
///< All features.
224 A combo control is a generic combobox that allows totally custom popup. In
225 addition it has other customization features. For instance, position and
226 size of the dropdown button can be changed.
228 @section comboctrl_custompopup Setting Custom Popup for wxComboCtrl
230 wxComboCtrl needs to be told somehow which control to use and this is done
231 by SetPopupControl(). However, we need something more than just a wxControl
232 in this method as, for example, we need to call
233 SetStringValue("initial text value") and wxControl doesn't have such
234 method. So we also need a wxComboPopup which is an interface which must be
235 implemented by a control to be usable as a popup.
237 We couldn't derive wxComboPopup from wxControl as this would make it
238 impossible to have a class deriving from a wxWidgets control and from it,
239 so instead it is just a mix-in.
241 Here's a minimal sample of wxListView popup:
244 #include <wx/combo.h>
245 #include <wx/listctrl.h>
247 class wxListViewComboPopup : public wxListView, public wxComboPopup
250 // Initialize member variables
256 // Create popup control
257 virtual bool Create(wxWindow* parent)
259 return wxListView::Create(parent,1,wxPoint(0,0),wxDefaultSize);
262 // Return pointer to the created control
263 virtual wxWindow *GetControl() { return this; }
265 // Translate string into a list selection
266 virtual void SetStringValue(const wxString& s)
268 int n = wxListView::FindItem(-1,s);
269 if ( n >= 0 && n < wxListView::GetItemCount() )
270 wxListView::Select(n);
273 // Get list selection as a string
274 virtual wxString GetStringValue() const
277 return wxListView::GetItemText(m_value);
278 return wxEmptyString;
281 // Do mouse hot-tracking (which is typical in list popups)
282 void OnMouseMove(wxMouseEvent& event)
284 // TODO: Move selection to cursor
287 // On mouse left up, set the value and close the popup
288 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
290 m_value = wxListView::GetFirstSelected();
292 // TODO: Send event as well
299 int m_value; // current item index
302 wxDECLARE_EVENT_TABLE();
305 wxBEGIN_EVENT_TABLE(wxListViewComboPopup, wxListView)
306 EVT_MOTION(wxListViewComboPopup::OnMouseMove)
307 EVT_LEFT_UP(wxListViewComboPopup::OnMouseClick)
311 Here's how you would create and populate it in a dialog constructor:
314 wxComboCtrl* comboCtrl = new wxComboCtrl(this, wxID_ANY, wxEmptyString);
316 wxListViewComboPopup* popupCtrl = new wxListViewComboPopup();
318 // It is important to call SetPopupControl() as soon as possible
319 comboCtrl->SetPopupControl(popupCtrl);
321 // Populate using wxListView methods
322 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "First Item");
323 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "Second Item");
324 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "Third Item");
328 @style{wxCB_READONLY}
329 Text will not be editable.
331 Sorts the entries in the list alphabetically.
332 @style{wxTE_PROCESS_ENTER}
333 The control will generate the event @c wxEVT_COMMAND_TEXT_ENTER
334 (otherwise pressing Enter key is either processed internally by the
335 control or used for navigation between dialog controls). Windows
337 @style{wxCC_SPECIAL_DCLICK}
338 Double-clicking triggers a call to popup's OnComboDoubleClick.
339 Actual behaviour is defined by a derived class. For instance,
340 wxOwnerDrawnComboBox will cycle an item. This style only applies if
341 wxCB_READONLY is used as well.
342 @style{wxCC_STD_BUTTON}
343 Drop button will behave more like a standard push button.
346 @beginEventEmissionTable{wxCommandEvent}
347 @event{EVT_TEXT(id, func)}
348 Process a @c wxEVT_COMMAND_TEXT_UPDATED event, when the text changes.
349 @event{EVT_TEXT_ENTER(id, func)}
350 Process a @c wxEVT_COMMAND_TEXT_ENTER event, when RETURN is pressed in
352 @event{EVT_COMBOBOX_DROPDOWN(id, func)}
353 Process a @c wxEVT_COMMAND_COMBOBOX_DROPDOWN event, which is generated
354 when the popup window is shown (drops down).
355 @event{EVT_COMBOBOX_CLOSEUP(id, func)}
356 Process a @c wxEVT_COMMAND_COMBOBOX_CLOSEUP event, which is generated
357 when the popup window of the combo control disappears (closes up).
358 You should avoid adding or deleting items in this event.
363 @appearance{comboctrl}
365 @see wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup,
368 class wxComboCtrl
: public wxControl
,
378 Constructor, creating and showing a combo control.
381 Parent window. Must not be @NULL.
383 Window identifier. The value wxID_ANY indicates a default value.
385 Initial selection string. An empty string indicates no selection.
388 If ::wxDefaultPosition is specified then a default position is chosen.
391 If ::wxDefaultSize is specified then the window is sized appropriately.
393 Window style. See wxComboCtrl.
399 @see Create(), wxValidator
401 wxComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
,
402 const wxString
& value
= wxEmptyString
,
403 const wxPoint
& pos
= wxDefaultPosition
,
404 const wxSize
& size
= wxDefaultSize
,
406 const wxValidator
& validator
= wxDefaultValidator
,
407 const wxString
& name
= wxComboBoxNameStr
);
410 Destructor, destroying the combo control.
412 virtual ~wxComboCtrl();
415 Copies the selected text to the clipboard.
420 Creates the combo control for two-step construction. Derived classes
421 should call or replace this function. See wxComboCtrl() for further
424 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
,
425 const wxString
& value
= wxEmptyString
,
426 const wxPoint
& pos
= wxDefaultPosition
,
427 const wxSize
& size
= wxDefaultSize
,
429 const wxValidator
& validator
= wxDefaultValidator
,
430 const wxString
& name
= wxComboBoxNameStr
);
433 Copies the selected text to the clipboard and removes the selection.
438 Dismisses the popup window.
440 Notice that calling this function will generate a
441 @c wxEVT_COMMAND_COMBOBOX_CLOSEUP event.
445 virtual void Dismiss();
449 Enables or disables popup animation, if any, depending on the value of
452 void EnablePopupAnimation(bool enable
= true);
456 Returns true if given key combination should toggle the popup.
458 virtual bool IsKeyPopupToggle(const wxKeyEvent
& event
) const;
462 Prepare background of combo control or an item in a dropdown list in a
463 way typical on platform. This includes painting the focus/disabled
464 background and setting the clipping region.
466 Unless you plan to paint your own focus indicator, you should always
467 call this in your wxComboPopup::PaintComboControl implementation. In
468 addition, it sets pen and text colour to what looks good and proper
469 against the background.
471 flags: wxRendererNative flags:
472 wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
473 wxCONTROL_SELECTED: list item is selected
474 wxCONTROL_DISABLED: control/item is disabled
476 virtual void PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const;
479 Returns true if focus indicator should be drawn in the control.
481 bool ShouldDrawFocus() const;
484 Returns disabled button bitmap that has been set with
487 @return A reference to the disabled state bitmap.
489 const wxBitmap
& GetBitmapDisabled() const;
492 Returns button mouse hover bitmap that has been set with
495 @return A reference to the mouse hover state bitmap.
497 const wxBitmap
& GetBitmapHover() const;
500 Returns default button bitmap that has been set with
503 @return A reference to the normal state bitmap.
505 const wxBitmap
& GetBitmapNormal() const;
508 Returns depressed button bitmap that has been set with
511 @return A reference to the depressed state bitmap.
513 const wxBitmap
& GetBitmapPressed() const;
516 Returns current size of the dropdown button.
518 wxSize
GetButtonSize();
521 Returns custom painted area in control.
523 @see SetCustomPaintWidth().
525 int GetCustomPaintWidth() const;
528 Returns features supported by wxComboCtrl. If needed feature is
529 missing, you need to instead use wxGenericComboCtrl, which however may
530 lack a native look and feel (but otherwise sports identical API).
532 @return Value returned is a combination of the flags defined in
535 static int GetFeatures();
538 Returns the current hint string.
540 See SetHint() for more information about hints.
544 virtual wxString
GetHint() const;
547 Returns the insertion point for the combo control's text field.
549 @note Under Windows, this function always returns 0 if the combo
550 control doesn't have the focus.
552 virtual long GetInsertionPoint() const;
555 Returns the last position in the combo control text field.
557 virtual long GetLastPosition() const;
560 Returns the margins used by the control. The @c x field of the returned
561 point is the horizontal margin and the @c y field is the vertical one.
563 @remarks If given margin cannot be accurately determined, its value
570 wxPoint
GetMargins() const;
573 Returns current popup interface that has been set with
576 wxComboPopup
* GetPopupControl();
579 Returns popup window containing the popup control.
581 wxWindow
* GetPopupWindow() const;
584 Get the text control which is part of the combo control.
586 wxTextCtrl
* GetTextCtrl() const;
589 Returns actual indentation in pixels.
591 @deprecated Use GetMargins() instead.
593 wxCoord
GetTextIndent() const;
596 Returns area covered by the text field (includes everything except
597 borders and the dropdown button).
599 const wxRect
& GetTextRect() const;
602 Returns text representation of the current value. For writable combo
603 control it always returns the value in the text field.
605 virtual wxString
GetValue() const;
608 Dismisses the popup window.
611 Set this to @true in order to generate
612 @c wxEVT_COMMAND_COMBOBOX_CLOSEUP event.
614 @deprecated Use Dismiss() instead.
616 virtual void HidePopup(bool generateEvent
=false);
619 Returns @true if the popup is currently shown
621 bool IsPopupShown() const;
624 Returns @true if the popup window is in the given state. Possible
628 @row2col{wxComboCtrl::Hidden, Popup window is hidden.}
629 @row2col{wxComboCtrl::Animating, Popup window is being shown, but the
630 popup animation has not yet finished.}
631 @row2col{wxComboCtrl::Visible, Popup window is fully visible.}
634 bool IsPopupWindowState(int state
) const;
637 Implement in a derived class to define what happens on dropdown button
638 click. Default action is to show the popup.
640 @note If you implement this to do something else than show the popup,
641 you must then also implement DoSetPopupControl() to always return
644 virtual void OnButtonClick();
647 Pastes text from the clipboard to the text field.
649 virtual void Paste();
652 Shows the popup portion of the combo control.
654 Notice that calling this function will generate a
655 @c wxEVT_COMMAND_COMBOBOX_DROPDOWN event.
659 virtual void Popup();
662 Removes the text between the two positions in the combo control text
670 virtual void Remove(long from
, long to
);
673 Replaces the text between two positions with the given text, in the
674 combo control text field.
683 virtual void Replace(long from
, long to
, const wxString
& text
);
686 Sets custom dropdown button graphics.
689 Default button image.
691 If @true, blank push button background is painted below the image.
693 Depressed button image.
695 Button image when mouse hovers above it. This should be ignored on
696 platforms and themes that do not generally draw different kind of
697 button on mouse hover.
699 Disabled button image.
701 void SetButtonBitmaps(const wxBitmap
& bmpNormal
,
702 bool pushButtonBg
= false,
703 const wxBitmap
& bmpPressed
= wxNullBitmap
,
704 const wxBitmap
& bmpHover
= wxNullBitmap
,
705 const wxBitmap
& bmpDisabled
= wxNullBitmap
);
708 Sets size and position of dropdown button.
711 Button width. Value = 0 specifies default.
713 Button height. Value = 0 specifies default.
715 Indicates which side the button will be placed. Value can be wxLEFT
718 Horizontal spacing around the button. Default is 0.
720 void SetButtonPosition(int width
= -1, int height
= -1,
721 int side
= wxRIGHT
, int spacingX
= 0);
724 Set width, in pixels, of custom painted area in control without
725 @c wxCB_READONLY style. In read-only wxOwnerDrawnComboBox, this is used
726 to indicate area that is not covered by the focus rectangle.
728 void SetCustomPaintWidth(int width
);
731 Sets a hint shown in an empty unfocused combo control.
733 Notice that hints are known as <em>cue banners</em> under MSW or
734 <em>placeholder strings</em> under OS X.
736 @see wxTextEntry::SetHint()
740 virtual bool SetHint(const wxString
& hint
);
743 Sets the insertion point in the text field.
746 The new insertion point.
748 virtual void SetInsertionPoint(long pos
);
751 Sets the insertion point at the end of the combo control text field.
753 virtual void SetInsertionPointEnd();
757 Attempts to set the control margins. When margins are given as wxPoint,
758 x indicates the left and y the top margin. Use -1 to indicate that
759 an existing value should be used.
762 @true if setting of all requested margins was successful.
766 bool SetMargins(const wxPoint
& pt
);
767 bool SetMargins(wxCoord left
, wxCoord top
= -1);
771 Set side of the control to which the popup will align itself. Valid
772 values are @c wxLEFT, @c wxRIGHT and 0. The default value 0 means that
773 the most appropriate side is used (which, currently, is always
776 void SetPopupAnchor(int anchorSide
);
779 Set popup interface class derived from wxComboPopup. This method should
780 be called as soon as possible after the control has been created,
781 unless OnButtonClick() has been overridden.
783 void SetPopupControl(wxComboPopup
* popup
);
786 Extends popup size horizontally, relative to the edges of the combo
790 How many pixel to extend beyond the left edge of the control.
793 How many pixel to extend beyond the right edge of the control.
796 @remarks Popup minimum width may override arguments. It is up to the
797 popup to fully take this into account.
799 void SetPopupExtents(int extLeft
, int extRight
);
802 Sets preferred maximum height of the popup.
804 @remarks Value -1 indicates the default.
806 void SetPopupMaxHeight(int height
);
809 Sets minimum width of the popup. If wider than combo control, it will
812 @remarks Value -1 indicates the default. Also, popup implementation may
813 choose to ignore this.
815 void SetPopupMinWidth(int width
);
818 Selects the text between the two positions, in the combo control text
826 virtual void SetSelection(long from
, long to
);
829 Sets the text for the text field without affecting the popup. Thus,
830 unlike SetValue(), it works equally well with combo control using
831 @c wxCB_READONLY style.
833 void SetText(const wxString
& value
);
836 Set a custom window style for the embedded wxTextCtrl. Usually you
837 will need to use this during two-step creation, just before Create().
841 wxComboCtrl* comboCtrl = new wxComboCtrl();
843 // Let's make the text right-aligned
844 comboCtrl->SetTextCtrlStyle(wxTE_RIGHT);
846 comboCtrl->Create(parent, wxID_ANY, wxEmptyString);
849 void SetTextCtrlStyle( int style
);
852 This will set the space in pixels between left edge of the control and
853 the text, regardless whether control is read-only or not. Value -1 can
854 be given to indicate platform default.
856 @deprecated Use SetMargins() instead.
858 void SetTextIndent(int indent
);
861 Sets the text for the combo control text field.
863 @note For a combo control with @c wxCB_READONLY style the string must
864 be accepted by the popup (for instance, exist in the dropdown
865 list), otherwise the call to SetValue() is ignored.
867 virtual void SetValue(const wxString
& value
);
870 Changes value of the control as if user had done it by selecting an
871 item from a combo box drop-down list.
873 void SetValueByUser(const wxString
& value
);
878 @deprecated Use Popup() instead.
880 virtual void ShowPopup();
883 Undoes the last edit in the text field. Windows only.
888 Enable or disable usage of an alternative popup window, which
889 guarantees ability to focus the popup control, and allows common native
890 controls to function normally. This alternative popup window is usually
891 a wxDialog, and as such, when it is shown, its parent top-level window
892 will appear as if the focus has been lost from it.
894 void UseAltPopupWindow(bool enable
= true);
899 This member function is not normally called in application code.
900 Instead, it can be implemented in a derived class to create a custom
903 The parameters are the same as those for DoShowPopup().
905 @return @true if animation finishes before the function returns,
906 @false otherwise. In the latter case you need to manually call
907 DoShowPopup() after the animation ends.
909 virtual bool AnimateShow(const wxRect
& rect
, int flags
);
912 This member function is not normally called in application code.
913 Instead, it can be implemented in a derived class to return default
914 wxComboPopup, in case @a popup is @NULL.
916 @note If you have implemented OnButtonClick() to do something else than
917 show the popup, then DoSetPopupControl() must always set @a popup
920 virtual void DoSetPopupControl(wxComboPopup
* popup
);
923 This member function is not normally called in application code.
924 Instead, it must be called in a derived class to make sure popup is
925 properly shown after a popup animation has finished (but only if
926 AnimateShow() did not finish the animation within its function scope).
929 Position to show the popup window at, in screen coordinates.
931 Combination of any of the following:
933 @row2col{wxComboCtrl::ShowAbove,
934 Popup is shown above the control instead of below.}
935 @row2col{wxComboCtrl::CanDeferShow,
936 Showing the popup can be deferred to happen sometime after
937 ShowPopup() has finished. In this case, AnimateShow() must
941 virtual void DoShowPopup(const wxRect
& rect
, int flags
);