]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/combo.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxComboCtrl and wxComboPopup
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
9 // New window styles for wxComboCtrlBase
13 // Double-clicking a read-only combo triggers call to popup's OnComboPopup.
14 // In wxOwnerDrawnComboBox, for instance, it cycles item.
15 wxCC_SPECIAL_DCLICK
= 0x0100,
17 // Dropbutton acts like standard push button.
18 wxCC_STD_BUTTON
= 0x0200
25 In order to use a custom popup with wxComboCtrl, an interface class must be
26 derived from wxComboPopup.
28 For more information on how to use it, see @ref comboctrl_custompopup.
39 Default constructor. It is recommended that internal variables are
40 prepared in Init() instead (because m_combo is not valid in
46 The derived class must implement this to create the popup control.
48 @return @true if the call succeeded, @false otherwise.
50 virtual bool Create(wxWindow
* parent
) = 0;
53 You only need to implement this member function if you create
54 your popup class in non-standard way. The default implementation can
55 handle both multiple-inherited popup control (as seen in wxComboCtrl
56 samples) and one allocated separately in heap.
58 If you do completely re-implement this function, make sure it calls
59 Destroy() for the popup control and also deletes @a this object
60 (usually as the last thing).
62 virtual void DestroyPopup();
65 Utility function that hides the popup.
70 Implement to customize matching of value string to an item container
74 String entered, usually by user or from SetValue() call.
77 When item matches an entry, but the entry's string representation
78 is not exactly the same (case mismatch, for example), then the
79 true item string should be written back to here, if it is not
83 Default implementation always return true and does not alter
86 virtual bool FindItem(const wxString
& item
, wxString
* trueItem
=NULL
);
89 The derived class may implement this to return adjusted size for the
90 popup control, according to the variables given.
93 Preferred minimum width.
95 Preferred height. May be -1 to indicate no preference.
97 Max height for window, as limited by screen size.
99 @remarks This function is called each time popup is about to be shown.
101 virtual wxSize
GetAdjustedSize(int minWidth
, int prefHeight
, int maxHeight
);
104 Returns pointer to the associated parent wxComboCtrl.
106 wxComboCtrl
* GetComboCtrl() const;
109 The derived class must implement this to return pointer to the
110 associated control created in Create().
112 virtual wxWindow
* GetControl() = 0;
115 The derived class must implement this to return string representation
118 virtual wxString
GetStringValue() const = 0;
121 The derived class must implement this to initialize its internal
122 variables. This method is called immediately after construction
123 finishes. m_combo member variable has been initialized before the call.
128 Utility method that returns @true if Create has been called.
130 Useful in conjunction with LazyCreate().
132 bool IsCreated() const;
135 The derived class may implement this to return @true if it wants to
136 delay call to Create() until the popup is shown for the first time. It
137 is more efficient, but on the other hand it is often more convenient to
138 have the control created immediately.
140 @remarks Base implementation returns @false.
142 virtual bool LazyCreate();
145 The derived class may implement this to do something when the parent
146 wxComboCtrl gets double-clicked.
148 virtual void OnComboDoubleClick();
151 The derived class may implement this to receive key events from the
154 Events not handled should be skipped, as usual.
156 virtual void OnComboKeyEvent(wxKeyEvent
& event
);
159 The derived class may implement this to do special processing when
162 virtual void OnDismiss();
165 The derived class may implement this to do special processing when
168 virtual void OnPopup();
171 The derived class may implement this to paint the parent wxComboCtrl.
173 Default implementation draws value as string.
175 virtual void PaintComboControl(wxDC
& dc
, const wxRect
& rect
);
178 The derived class must implement this to receive string value changes
181 virtual void SetStringValue(const wxString
& value
);
185 Parent wxComboCtrl. This member variable is prepared automatically
186 before Init() is called.
188 wxComboCtrl
* m_combo
;
194 Features enabled for wxComboCtrl.
196 @see wxComboCtrl::GetFeatures()
198 struct wxComboCtrlFeatures
202 MovableButton
= 0x0001, ///< Button can be on either side of control.
203 BitmapButton
= 0x0002, ///< Button may be replaced with bitmap.
204 ButtonSpacing
= 0x0004, ///< Button can have spacing from the edge
206 TextIndent
= 0x0008, ///< wxComboCtrl::SetMargins() can be used.
207 PaintControl
= 0x0010, ///< Combo control itself can be custom painted.
208 PaintWritable
= 0x0020, ///< A variable-width area in front of writable
209 ///< combo control's textctrl can be custom
211 Borderless
= 0x0040, ///< wxNO_BORDER window style works.
213 All
= MovableButton
| BitmapButton
| ButtonSpacing
|
214 TextIndent
| PaintControl
| PaintWritable
|
215 Borderless
///< All features.
223 A combo control is a generic combobox that allows totally custom popup. In
224 addition it has other customization features. For instance, position and
225 size of the dropdown button can be changed.
227 @section comboctrl_custompopup Setting Custom Popup for wxComboCtrl
229 wxComboCtrl needs to be told somehow which control to use and this is done
230 by SetPopupControl(). However, we need something more than just a wxControl
231 in this method as, for example, we need to call
232 SetStringValue("initial text value") and wxControl doesn't have such
233 method. So we also need a wxComboPopup which is an interface which must be
234 implemented by a control to be usable as a popup.
236 We couldn't derive wxComboPopup from wxControl as this would make it
237 impossible to have a class deriving from a wxWidgets control and from it,
238 so instead it is just a mix-in.
240 Here's a minimal sample of wxListView popup:
243 #include <wx/combo.h>
244 #include <wx/listctrl.h>
246 class wxListViewComboPopup : public wxListView, public wxComboPopup
249 // Initialize member variables
255 // Create popup control
256 virtual bool Create(wxWindow* parent)
258 return wxListView::Create(parent,1,wxPoint(0,0),wxDefaultSize);
261 // Return pointer to the created control
262 virtual wxWindow *GetControl() { return this; }
264 // Translate string into a list selection
265 virtual void SetStringValue(const wxString& s)
267 int n = wxListView::FindItem(-1,s);
268 if ( n >= 0 && n < wxListView::GetItemCount() )
269 wxListView::Select(n);
272 // Get list selection as a string
273 virtual wxString GetStringValue() const
276 return wxListView::GetItemText(m_value);
277 return wxEmptyString;
280 // Do mouse hot-tracking (which is typical in list popups)
281 void OnMouseMove(wxMouseEvent& event)
283 // TODO: Move selection to cursor
286 // On mouse left up, set the value and close the popup
287 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
289 m_value = wxListView::GetFirstSelected();
291 // TODO: Send event as well
298 int m_value; // current item index
301 wxDECLARE_EVENT_TABLE();
304 wxBEGIN_EVENT_TABLE(wxListViewComboPopup, wxListView)
305 EVT_MOTION(wxListViewComboPopup::OnMouseMove)
306 EVT_LEFT_UP(wxListViewComboPopup::OnMouseClick)
310 Here's how you would create and populate it in a dialog constructor:
313 wxComboCtrl* comboCtrl = new wxComboCtrl(this, wxID_ANY, wxEmptyString);
315 wxListViewComboPopup* popupCtrl = new wxListViewComboPopup();
317 // It is important to call SetPopupControl() as soon as possible
318 comboCtrl->SetPopupControl(popupCtrl);
320 // Populate using wxListView methods
321 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "First Item");
322 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "Second Item");
323 popupCtrl->InsertItem(popupCtrl->GetItemCount(), "Third Item");
327 @style{wxCB_READONLY}
328 Text will not be editable.
330 Sorts the entries in the list alphabetically.
331 @style{wxTE_PROCESS_ENTER}
332 The control will generate the event @c wxEVT_TEXT_ENTER
333 (otherwise pressing Enter key is either processed internally by the
334 control or used for navigation between dialog controls). Windows
336 @style{wxCC_SPECIAL_DCLICK}
337 Double-clicking triggers a call to popup's OnComboDoubleClick.
338 Actual behaviour is defined by a derived class. For instance,
339 wxOwnerDrawnComboBox will cycle an item. This style only applies if
340 wxCB_READONLY is used as well.
341 @style{wxCC_STD_BUTTON}
342 Drop button will behave more like a standard push button.
345 @beginEventEmissionTable{wxCommandEvent}
346 @event{EVT_TEXT(id, func)}
347 Process a @c wxEVT_TEXT event, when the text changes.
348 @event{EVT_TEXT_ENTER(id, func)}
349 Process a @c wxEVT_TEXT_ENTER event, when RETURN is pressed in
351 @event{EVT_COMBOBOX_DROPDOWN(id, func)}
352 Process a @c wxEVT_COMBOBOX_DROPDOWN event, which is generated
353 when the popup window is shown (drops down).
354 @event{EVT_COMBOBOX_CLOSEUP(id, func)}
355 Process a @c wxEVT_COMBOBOX_CLOSEUP event, which is generated
356 when the popup window of the combo control disappears (closes up).
357 You should avoid adding or deleting items in this event.
362 @appearance{comboctrl}
364 @see wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup,
367 class wxComboCtrl
: public wxControl
,
377 Constructor, creating and showing a combo control.
380 Parent window. Must not be @NULL.
382 Window identifier. The value wxID_ANY indicates a default value.
384 Initial selection string. An empty string indicates no selection.
387 If ::wxDefaultPosition is specified then a default position is chosen.
390 If ::wxDefaultSize is specified then the window is sized appropriately.
392 Window style. See wxComboCtrl.
398 @see Create(), wxValidator
400 wxComboCtrl(wxWindow
* parent
, wxWindowID id
= wxID_ANY
,
401 const wxString
& value
= wxEmptyString
,
402 const wxPoint
& pos
= wxDefaultPosition
,
403 const wxSize
& size
= wxDefaultSize
,
405 const wxValidator
& validator
= wxDefaultValidator
,
406 const wxString
& name
= wxComboBoxNameStr
);
409 Destructor, destroying the combo control.
411 virtual ~wxComboCtrl();
414 Copies the selected text to the clipboard.
419 Creates the combo control for two-step construction. Derived classes
420 should call or replace this function. See wxComboCtrl() for further
423 bool Create(wxWindow
* parent
, wxWindowID id
= wxID_ANY
,
424 const wxString
& value
= wxEmptyString
,
425 const wxPoint
& pos
= wxDefaultPosition
,
426 const wxSize
& size
= wxDefaultSize
,
428 const wxValidator
& validator
= wxDefaultValidator
,
429 const wxString
& name
= wxComboBoxNameStr
);
432 Copies the selected text to the clipboard and removes the selection.
437 Dismisses the popup window.
439 Notice that calling this function will generate a
440 @c wxEVT_COMBOBOX_CLOSEUP event.
444 virtual void Dismiss();
448 Enables or disables popup animation, if any, depending on the value of
451 void EnablePopupAnimation(bool enable
= true);
455 Returns true if given key combination should toggle the popup.
457 virtual bool IsKeyPopupToggle(const wxKeyEvent
& event
) const;
461 Prepare background of combo control or an item in a dropdown list in a
462 way typical on platform. This includes painting the focus/disabled
463 background and setting the clipping region.
465 Unless you plan to paint your own focus indicator, you should always
466 call this in your wxComboPopup::PaintComboControl implementation. In
467 addition, it sets pen and text colour to what looks good and proper
468 against the background.
470 flags: wxRendererNative flags:
471 wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
472 wxCONTROL_SELECTED: list item is selected
473 wxCONTROL_DISABLED: control/item is disabled
475 virtual void PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const;
478 Returns true if focus indicator should be drawn in the control.
480 bool ShouldDrawFocus() const;
483 Returns disabled button bitmap that has been set with
486 @return A reference to the disabled state bitmap.
488 const wxBitmap
& GetBitmapDisabled() const;
491 Returns button mouse hover bitmap that has been set with
494 @return A reference to the mouse hover state bitmap.
496 const wxBitmap
& GetBitmapHover() const;
499 Returns default button bitmap that has been set with
502 @return A reference to the normal state bitmap.
504 const wxBitmap
& GetBitmapNormal() const;
507 Returns depressed button bitmap that has been set with
510 @return A reference to the depressed state bitmap.
512 const wxBitmap
& GetBitmapPressed() const;
515 Returns current size of the dropdown button.
517 wxSize
GetButtonSize();
520 Returns custom painted area in control.
522 @see SetCustomPaintWidth().
524 int GetCustomPaintWidth() const;
527 Returns features supported by wxComboCtrl. If needed feature is
528 missing, you need to instead use wxGenericComboCtrl, which however may
529 lack a native look and feel (but otherwise sports identical API).
531 @return Value returned is a combination of the flags defined in
534 static int GetFeatures();
537 Returns the current hint string.
539 See SetHint() for more information about hints.
543 virtual wxString
GetHint() const;
546 Returns the insertion point for the combo control's text field.
548 @note Under Windows, this function always returns 0 if the combo
549 control doesn't have the focus.
551 virtual long GetInsertionPoint() const;
554 Returns the last position in the combo control text field.
556 virtual long GetLastPosition() const;
559 Returns the margins used by the control. The @c x field of the returned
560 point is the horizontal margin and the @c y field is the vertical one.
562 @remarks If given margin cannot be accurately determined, its value
569 wxPoint
GetMargins() const;
572 Returns current popup interface that has been set with
575 wxComboPopup
* GetPopupControl();
578 Returns popup window containing the popup control.
580 wxWindow
* GetPopupWindow() const;
583 Get the text control which is part of the combo control.
585 wxTextCtrl
* GetTextCtrl() const;
588 Returns actual indentation in pixels.
590 @deprecated Use GetMargins() instead.
592 wxCoord
GetTextIndent() const;
595 Returns area covered by the text field (includes everything except
596 borders and the dropdown button).
598 const wxRect
& GetTextRect() const;
601 Returns text representation of the current value. For writable combo
602 control it always returns the value in the text field.
604 virtual wxString
GetValue() const;
607 Dismisses the popup window.
610 Set this to @true in order to generate
611 @c wxEVT_COMBOBOX_CLOSEUP event.
613 @deprecated Use Dismiss() instead.
615 virtual void HidePopup(bool generateEvent
=false);
618 Returns @true if the popup is currently shown
620 bool IsPopupShown() const;
623 Returns @true if the popup window is in the given state. Possible
627 @row2col{wxComboCtrl::Hidden, Popup window is hidden.}
628 @row2col{wxComboCtrl::Animating, Popup window is being shown, but the
629 popup animation has not yet finished.}
630 @row2col{wxComboCtrl::Visible, Popup window is fully visible.}
633 bool IsPopupWindowState(int state
) const;
636 Implement in a derived class to define what happens on dropdown button
637 click. Default action is to show the popup.
639 @note If you implement this to do something else than show the popup,
640 you must then also implement DoSetPopupControl() to always return
643 virtual void OnButtonClick();
646 Pastes text from the clipboard to the text field.
648 virtual void Paste();
651 Shows the popup portion of the combo control.
653 Notice that calling this function will generate a
654 @c wxEVT_COMBOBOX_DROPDOWN event.
658 virtual void Popup();
661 Removes the text between the two positions in the combo control text
669 virtual void Remove(long from
, long to
);
672 Replaces the text between two positions with the given text, in the
673 combo control text field.
682 virtual void Replace(long from
, long to
, const wxString
& text
);
685 Sets custom dropdown button graphics.
688 Default button image.
690 If @true, blank push button background is painted below the image.
692 Depressed button image.
694 Button image when mouse hovers above it. This should be ignored on
695 platforms and themes that do not generally draw different kind of
696 button on mouse hover.
698 Disabled button image.
700 void SetButtonBitmaps(const wxBitmap
& bmpNormal
,
701 bool pushButtonBg
= false,
702 const wxBitmap
& bmpPressed
= wxNullBitmap
,
703 const wxBitmap
& bmpHover
= wxNullBitmap
,
704 const wxBitmap
& bmpDisabled
= wxNullBitmap
);
707 Sets size and position of dropdown button.
710 Button width. Value = 0 specifies default.
712 Button height. Value = 0 specifies default.
714 Indicates which side the button will be placed. Value can be wxLEFT
717 Horizontal spacing around the button. Default is 0.
719 void SetButtonPosition(int width
= -1, int height
= -1,
720 int side
= wxRIGHT
, int spacingX
= 0);
723 Set width, in pixels, of custom painted area in control without
724 @c wxCB_READONLY style. In read-only wxOwnerDrawnComboBox, this is used
725 to indicate area that is not covered by the focus rectangle.
727 void SetCustomPaintWidth(int width
);
730 Sets a hint shown in an empty unfocused combo control.
732 Notice that hints are known as <em>cue banners</em> under MSW or
733 <em>placeholder strings</em> under OS X.
735 @see wxTextEntry::SetHint()
739 virtual bool SetHint(const wxString
& hint
);
742 Sets the insertion point in the text field.
745 The new insertion point.
747 virtual void SetInsertionPoint(long pos
);
750 Sets the insertion point at the end of the combo control text field.
752 virtual void SetInsertionPointEnd();
756 Attempts to set the control margins. When margins are given as wxPoint,
757 x indicates the left and y the top margin. Use -1 to indicate that
758 an existing value should be used.
761 @true if setting of all requested margins was successful.
765 bool SetMargins(const wxPoint
& pt
);
766 bool SetMargins(wxCoord left
, wxCoord top
= -1);
770 Set side of the control to which the popup will align itself. Valid
771 values are @c wxLEFT, @c wxRIGHT and 0. The default value 0 means that
772 the most appropriate side is used (which, currently, is always
775 void SetPopupAnchor(int anchorSide
);
778 Set popup interface class derived from wxComboPopup. This method should
779 be called as soon as possible after the control has been created,
780 unless OnButtonClick() has been overridden.
782 void SetPopupControl(wxComboPopup
* popup
);
785 Extends popup size horizontally, relative to the edges of the combo
789 How many pixel to extend beyond the left edge of the control.
792 How many pixel to extend beyond the right edge of the control.
795 @remarks Popup minimum width may override arguments. It is up to the
796 popup to fully take this into account.
798 void SetPopupExtents(int extLeft
, int extRight
);
801 Sets preferred maximum height of the popup.
803 @remarks Value -1 indicates the default.
805 void SetPopupMaxHeight(int height
);
808 Sets minimum width of the popup. If wider than combo control, it will
811 @remarks Value -1 indicates the default. Also, popup implementation may
812 choose to ignore this.
814 void SetPopupMinWidth(int width
);
817 Selects the text between the two positions, in the combo control text
825 virtual void SetSelection(long from
, long to
);
828 Sets the text for the text field without affecting the popup. Thus,
829 unlike SetValue(), it works equally well with combo control using
830 @c wxCB_READONLY style.
832 void SetText(const wxString
& value
);
835 Set a custom window style for the embedded wxTextCtrl. Usually you
836 will need to use this during two-step creation, just before Create().
840 wxComboCtrl* comboCtrl = new wxComboCtrl();
842 // Let's make the text right-aligned
843 comboCtrl->SetTextCtrlStyle(wxTE_RIGHT);
845 comboCtrl->Create(parent, wxID_ANY, wxEmptyString);
848 void SetTextCtrlStyle( int style
);
851 This will set the space in pixels between left edge of the control and
852 the text, regardless whether control is read-only or not. Value -1 can
853 be given to indicate platform default.
855 @deprecated Use SetMargins() instead.
857 void SetTextIndent(int indent
);
860 Sets the text for the combo control text field.
862 @note For a combo control with @c wxCB_READONLY style the string must
863 be accepted by the popup (for instance, exist in the dropdown
864 list), otherwise the call to SetValue() is ignored.
866 virtual void SetValue(const wxString
& value
);
869 Changes value of the control as if user had done it by selecting an
870 item from a combo box drop-down list.
872 void SetValueByUser(const wxString
& value
);
877 @deprecated Use Popup() instead.
879 virtual void ShowPopup();
882 Undoes the last edit in the text field. Windows only.
887 Enable or disable usage of an alternative popup window, which
888 guarantees ability to focus the popup control, and allows common native
889 controls to function normally. This alternative popup window is usually
890 a wxDialog, and as such, when it is shown, its parent top-level window
891 will appear as if the focus has been lost from it.
893 void UseAltPopupWindow(bool enable
= true);
898 This member function is not normally called in application code.
899 Instead, it can be implemented in a derived class to create a custom
902 The parameters are the same as those for DoShowPopup().
904 @return @true if animation finishes before the function returns,
905 @false otherwise. In the latter case you need to manually call
906 DoShowPopup() after the animation ends.
908 virtual bool AnimateShow(const wxRect
& rect
, int flags
);
911 This member function is not normally called in application code.
912 Instead, it can be implemented in a derived class to return default
913 wxComboPopup, in case @a popup is @NULL.
915 @note If you have implemented OnButtonClick() to do something else than
916 show the popup, then DoSetPopupControl() must always set @a popup
919 virtual void DoSetPopupControl(wxComboPopup
* popup
);
922 This member function is not normally called in application code.
923 Instead, it must be called in a derived class to make sure popup is
924 properly shown after a popup animation has finished (but only if
925 AnimateShow() did not finish the animation within its function scope).
928 Position to show the popup window at, in screen coordinates.
930 Combination of any of the following:
932 @row2col{wxComboCtrl::ShowAbove,
933 Popup is shown above the control instead of below.}
934 @row2col{wxComboCtrl::CanDeferShow,
935 Showing the popup can be deferred to happen sometime after
936 ShowPopup() has finished. In this case, AnimateShow() must
940 virtual void DoShowPopup(const wxRect
& rect
, int flags
);