]> git.saurik.com Git - wxWidgets.git/blame - interface/combo.h
SF bug 1895101
[wxWidgets.git] / interface / combo.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: combo.h
e54c96f1 3// Purpose: interface of wxComboPopup
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10 @class wxComboPopup
11 @wxheader{combo.h}
7c913512 12
1f1d2182
FM
13 In order to use a custom popup with wxComboCtrl, an interface class must
14 be derived from wxComboPopup.
15
16 For more information on how to use it, see @ref overview_wxcomboctrl "Setting Custom Popup for
23324ae1 17 wxComboCtrl".
7c913512 18
23324ae1
FM
19 @library{wxcore}
20 @category{FIXME}
7c913512 21
e54c96f1 22 @see wxComboCtrl
23324ae1 23*/
7c913512 24class wxComboPopup
23324ae1
FM
25{
26public:
27 /**
28 Default constructor. It is recommended that internal variables
29 are prepared in Init() instead
30 (because @ref mcombo() m_combo is not valid in constructor).
31 */
32 wxComboPopup();
33
34 /**
35 The derived class must implement this to create the popup control.
3c4f71cc 36
23324ae1
FM
37 @returns @true if the call succeeded, @false otherwise.
38 */
39 bool Create(wxWindow* parent);
40
41 /**
42 Utility function that hides the popup.
43 */
44 void Dismiss();
45
46 /**
47 The derived class may implement this to return adjusted size
48 for the popup control, according to the variables given.
3c4f71cc 49
7c913512 50 @param minWidth
4cc4bfaf 51 Preferred minimum width.
7c913512 52 @param prefHeight
4cc4bfaf
FM
53 Preferred height. May be -1 to indicate
54 no preference.
7c913512 55 @param maxWidth
4cc4bfaf
FM
56 Max height for window, as limited by
57 screen size.
3c4f71cc 58
23324ae1
FM
59 @remarks Called each time popup is about to be shown.
60 */
61 wxSize GetAdjustedSize(int minWidth, int prefHeight,
62 int maxHeight);
63
64 /**
65 The derived class must implement this to return pointer
66 to the associated control created in Create().
67 */
68 wxWindow* GetControl();
69
70 /**
71 The derived class must implement this to return
72 string representation of the value.
73 */
328f5751 74 wxString GetStringValue() const;
23324ae1
FM
75
76 /**
77 The derived class must implement this to initialize
78 its internal variables. This method is called immediately
79 after construction finishes. @ref mcombo() m_combo
80 member variable has been initialized before the call.
81 */
82 void Init();
83
84 /**
85 Utility method that returns @true if Create has been called.
23324ae1
FM
86 Useful in conjunction with LazyCreate().
87 */
328f5751 88 bool IsCreated() const;
23324ae1
FM
89
90 /**
91 The derived class may implement this to return
92 @true if it wants to delay call to Create()
93 until the popup is shown for the first time. It is more
94 efficient, but on the other hand it is often more convenient
95 to have the control created immediately.
3c4f71cc 96
23324ae1
FM
97 @remarks Base implementation returns @false.
98 */
99 bool LazyCreate();
100
101 /**
102 The derived class may implement this to do something
103 when the parent wxComboCtrl gets double-clicked.
104 */
105 void OnComboDoubleClick();
106
107 /**
108 The derived class may implement this to receive
109 key events from the parent wxComboCtrl.
23324ae1
FM
110 Events not handled should be skipped, as usual.
111 */
112 void OnComboKeyEvent(wxKeyEvent& event);
113
114 /**
115 The derived class may implement this to do
116 special processing when popup is hidden.
117 */
118 void OnDismiss();
119
120 /**
121 The derived class may implement this to do
122 special processing when popup is shown.
123 */
124 void OnPopup();
125
126 /**
127 The derived class may implement this to paint
128 the parent wxComboCtrl.
23324ae1
FM
129 Default implementation draws value as string.
130 */
131 void PaintComboControl(wxDC& dc, const wxRect& rect);
132
133 /**
134 The derived class must implement this to receive
135 string value changes from wxComboCtrl.
136 */
137 void SetStringValue(const wxString& value);
138
139 /**
140 wxComboCtrl m_combo
23324ae1
FM
141 Parent wxComboCtrl. This is parameter has
142 been prepared before Init() is called.
143 */
144};
145
146
e54c96f1 147
23324ae1
FM
148/**
149 @class wxComboCtrl
150 @wxheader{combo.h}
7c913512 151
1f1d2182
FM
152 A combo control is a generic combobox that allows totally custom popup.
153 In addition it has other customization features.
154 For instance, position and size of the dropdown button can be changed.
155
156 @section wxcomboctrl_custompopup Setting Custom Popup for wxComboCtrl
157
158 wxComboCtrl needs to be told somehow which control to use and this is done
159 by SetPopupControl().
160 However, we need something more than just a wxControl in this method as,
161 for example, we need to call SetStringValue("initial text value") and
162 wxControl doesn't have such method. So we also need a wxComboPopup which
163 is an interface which must be implemented by a control to be usable as a popup.
164
165 We couldn't derive wxComboPopup from wxControl as this would make it
166 impossible to have a class deriving from a wxWidgets control and from it,
167 so instead it is just a mix-in.
168
169 Here's a minimal sample of wxListView popup:
170
171 @code
172 #include <wx/combo.h>
173 #include <wx/listctrl.h>
174
175 class wxListViewComboPopup : public wxListView,
176 public wxComboPopup
177 {
178 public:
179
180 // Initialize member variables
181 virtual void Init()
182 {
183 m_value = -1;
184 }
185
186 // Create popup control
187 virtual bool Create(wxWindow* parent)
188 {
189 return wxListView::Create(parent,1,wxPoint(0,0),wxDefaultSize);
190 }
191
192 // Return pointer to the created control
193 virtual wxWindow *GetControl() { return this; }
194
195 // Translate string into a list selection
196 virtual void SetStringValue(const wxString& s)
197 {
198 int n = wxListView::FindItem(-1,s);
199 if ( n >= 0 && n < wxListView::GetItemCount() )
200 wxListView::Select(n);
201 }
202
203 // Get list selection as a string
204 virtual wxString GetStringValue() const
205 {
206 if ( m_value >= 0 )
207 return wxListView::GetItemText(m_value);
208 return wxEmptyString;
209 }
210
211 // Do mouse hot-tracking (which is typical in list popups)
212 void OnMouseMove(wxMouseEvent& event)
213 {
214 // TODO: Move selection to cursor
215 }
216
217 // On mouse left up, set the value and close the popup
218 void OnMouseClick(wxMouseEvent& WXUNUSED(event))
219 {
220 m_value = wxListView::GetFirstSelected();
221
222 // TODO: Send event as well
223
224 Dismiss();
225 }
226
227 protected:
228 int m_value; // current item index
229
230 private:
231 DECLARE_EVENT_TABLE()
232 };
233
234 BEGIN_EVENT_TABLE(wxListViewComboPopup, wxListView)
235 EVT_MOTION(wxListViewComboPopup::OnMouseMove)
236 EVT_LEFT_UP(wxListViewComboPopup::OnMouseClick)
237 END_EVENT_TABLE()
238 @endcode
239
240 Here's how you would create and populate it in a dialog constructor:
241
242 @code
243 wxComboCtrl* comboCtrl = new wxComboCtrl(this,wxID_ANY,wxEmptyString);
244
245 wxListViewComboPopup* popupCtrl = new wxListViewComboPopup();
246
247 comboCtrl->SetPopupControl(popupCtrl);
248
249 // Populate using wxListView methods
250 popupCtrl->InsertItem(popupCtrl->GetItemCount(),wxT("First Item"));
251 popupCtrl->InsertItem(popupCtrl->GetItemCount(),wxT("Second Item"));
252 popupCtrl->InsertItem(popupCtrl->GetItemCount(),wxT("Third Item"));
253 @endcode
7c913512 254
23324ae1
FM
255 @beginStyleTable
256 @style{wxCB_READONLY}:
257 Text will not be editable.
258 @style{wxCB_SORT}:
259 Sorts the entries in the list alphabetically.
260 @style{wxTE_PROCESS_ENTER}:
261 The control will generate the event wxEVT_COMMAND_TEXT_ENTER
262 (otherwise pressing Enter key is either processed internally by the
263 control or used for navigation between dialog controls). Windows
264 only.
265 @style{wxCC_SPECIAL_DCLICK}:
266 Double-clicking triggers a call to popup's OnComboDoubleClick.
267 Actual behaviour is defined by a derived class. For instance,
268 wxOwnerDrawnComboBox will cycle an item. This style only applies if
269 wxCB_READONLY is used as well.
270 @style{wxCC_STD_BUTTON}:
271 Drop button will behave more like a standard push button.
272 @endStyleTable
7c913512 273
1f1d2182 274 @beginEventTable{wxCommandEvent}
4cc4bfaf 275 @event{EVT_TEXT(id, func)}:
23324ae1 276 Process a wxEVT_COMMAND_TEXT_UPDATED event, when the text changes.
4cc4bfaf 277 @event{EVT_TEXT_ENTER(id, func)}:
23324ae1
FM
278 Process a wxEVT_COMMAND_TEXT_ENTER event, when RETURN is pressed in
279 the combo control.
280 @endEventTable
7c913512 281
23324ae1
FM
282 @library{wxbase}
283 @category{ctrl}
284 @appearance{comboctrl.png}
7c913512 285
e54c96f1 286 @see wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup, wxCommandEvent
23324ae1
FM
287*/
288class wxComboCtrl : public wxControl
289{
290public:
291 //@{
292 /**
293 Constructor, creating and showing a combo control.
3c4f71cc 294
7c913512 295 @param parent
4cc4bfaf 296 Parent window. Must not be @NULL.
7c913512 297 @param id
4cc4bfaf 298 Window identifier. The value wxID_ANY indicates a default value.
7c913512 299 @param value
4cc4bfaf 300 Initial selection string. An empty string indicates no selection.
7c913512 301 @param pos
4cc4bfaf 302 Window position.
7c913512 303 @param size
4cc4bfaf
FM
304 Window size. If wxDefaultSize is specified then the window is
305 sized
306 appropriately.
7c913512 307 @param style
4cc4bfaf 308 Window style. See wxComboCtrl.
7c913512 309 @param validator
4cc4bfaf 310 Window validator.
7c913512 311 @param name
4cc4bfaf 312 Window name.
3c4f71cc 313
4cc4bfaf 314 @see Create(), wxValidator
23324ae1
FM
315 */
316 wxComboCtrl();
7c913512
FM
317 wxComboCtrl(wxWindow* parent, wxWindowID id,
318 const wxString& value = "",
319 const wxPoint& pos = wxDefaultPosition,
320 const wxSize& size = wxDefaultSize,
321 long style = 0,
322 const wxValidator& validator = wxDefaultValidator,
323 const wxString& name = "comboCtrl");
23324ae1
FM
324 //@}
325
326 /**
327 Destructor, destroying the combo control.
328 */
329 ~wxComboCtrl();
330
331 /**
332 This member function is not normally called in application code.
333 Instead, it can be implemented in a derived class to create a
334 custom popup animation.
3c4f71cc 335
23324ae1 336 @returns @true if animation finishes before the function returns. @false
4cc4bfaf
FM
337 otherwise. In the latter case you need to manually call
338 DoShowPopup after the animation ends.
23324ae1
FM
339 */
340 virtual bool AnimateShow(const wxRect& rect, int flags);
341
342 /**
343 Copies the selected text to the clipboard.
344 */
345 void Copy();
346
347 /**
348 Creates the combo control for two-step construction. Derived classes
349 should call or replace this function. See wxComboCtrl()
350 for further details.
351 */
352 bool Create(wxWindow* parent, wxWindowID id,
353 const wxString& value = "",
354 const wxPoint& pos = wxDefaultPosition,
355 const wxSize& size = wxDefaultSize,
356 long style = 0,
357 const wxValidator& validator = wxDefaultValidator,
358 const wxString& name = "comboCtrl");
359
360 /**
361 Copies the selected text to the clipboard and removes the selection.
362 */
4cc4bfaf 363 void Cut();
23324ae1
FM
364
365 /**
366 This member function is not normally called in application code.
367 Instead, it can be implemented in a derived class to return
368 default wxComboPopup, incase @c popup is @NULL.
cdbcf4c2 369 @note If you have implemented OnButtonClick to do
23324ae1
FM
370 something else than show the popup, then DoSetPopupControl
371 must always return @NULL.
372 */
373 void DoSetPopupControl(wxComboPopup* popup);
374
375 /**
376 This member function is not normally called in application code.
377 Instead, it must be called in a derived class to make sure popup
378 is properly shown after a popup animation has finished (but only
379 if AnimateShow() did not finish
380 the animation within it's function scope).
3c4f71cc 381
7c913512 382 @param rect
4cc4bfaf 383 Position to show the popup window at, in screen coordinates.
7c913512 384 @param flags
4cc4bfaf 385 Combination of any of the following:
23324ae1
FM
386 */
387 virtual void DoShowPopup(const wxRect& rect, int flags);
388
389 /**
390 Enables or disables popup animation, if any, depending on the value of
391 the argument.
392 */
4cc4bfaf 393 void EnablePopupAnimation(bool enable = true);
23324ae1
FM
394
395 /**
396 Returns disabled button bitmap that has been set with
397 SetButtonBitmaps().
3c4f71cc 398
23324ae1
FM
399 @returns A reference to the disabled state bitmap.
400 */
328f5751 401 const wxBitmap GetBitmapDisabled() const;
23324ae1
FM
402
403 /**
404 Returns button mouse hover bitmap that has been set with
405 SetButtonBitmaps().
3c4f71cc 406
23324ae1
FM
407 @returns A reference to the mouse hover state bitmap.
408 */
328f5751 409 const wxBitmap GetBitmapHover() const;
23324ae1
FM
410
411 /**
412 Returns default button bitmap that has been set with
413 SetButtonBitmaps().
3c4f71cc 414
23324ae1
FM
415 @returns A reference to the normal state bitmap.
416 */
328f5751 417 const wxBitmap GetBitmapNormal() const;
23324ae1
FM
418
419 /**
420 Returns depressed button bitmap that has been set with
421 SetButtonBitmaps().
3c4f71cc 422
23324ae1
FM
423 @returns A reference to the depressed state bitmap.
424 */
328f5751 425 const wxBitmap GetBitmapPressed() const;
23324ae1
FM
426
427 /**
428 Returns current size of the dropdown button.
429 */
430 wxSize GetButtonSize();
431
432 /**
433 Returns custom painted area in control.
3c4f71cc 434
4cc4bfaf 435 @see SetCustomPaintWidth().
23324ae1 436 */
328f5751 437 int GetCustomPaintWidth() const;
23324ae1
FM
438
439 /**
440 Returns features supported by wxComboCtrl. If needed feature is missing,
441 you need to instead use wxGenericComboCtrl, which however may lack
442 native look and feel (but otherwise sports identical API).
3c4f71cc 443
23324ae1
FM
444 @returns Value returned is a combination of following flags:
445 */
446 static int GetFeatures();
447
448 /**
449 Returns the insertion point for the combo control's text field.
cdbcf4c2 450 @note Under wxMSW, this function always returns 0 if the combo control
23324ae1
FM
451 doesn't have the focus.
452 */
328f5751 453 long GetInsertionPoint() const;
23324ae1
FM
454
455 /**
456 Returns the last position in the combo control text field.
457 */
328f5751 458 long GetLastPosition() const;
23324ae1
FM
459
460 /**
461 Returns current popup interface that has been set with SetPopupControl.
462 */
463 wxComboPopup* GetPopupControl();
464
465 /**
466 Returns popup window containing the popup control.
467 */
328f5751 468 wxWindow* GetPopupWindow() const;
23324ae1
FM
469
470 /**
471 Get the text control which is part of the combo control.
472 */
328f5751 473 wxTextCtrl* GetTextCtrl() const;
23324ae1
FM
474
475 /**
476 Returns actual indentation in pixels.
477 */
328f5751 478 wxCoord GetTextIndent() const;
23324ae1
FM
479
480 /**
481 Returns area covered by the text field (includes everything except
482 borders and the dropdown button).
483 */
328f5751 484 const wxRect GetTextRect() const;
23324ae1
FM
485
486 /**
487 Returns text representation of the current value. For writable
488 combo control it always returns the value in the text field.
489 */
328f5751 490 wxString GetValue() const;
23324ae1
FM
491
492 /**
493 Dismisses the popup window.
494 */
495 void HidePopup();
496
497 /**
498 Returns @true if the popup is currently shown
499 */
328f5751 500 bool IsPopupShown() const;
23324ae1
FM
501
502 /**
503 Returns @true if the popup window is in the given state.
504 Possible values are:
3c4f71cc 505
23324ae1 506 @c Hidden()
3c4f71cc 507
23324ae1 508 Popup window is hidden.
3c4f71cc 509
23324ae1 510 @c Animating()
3c4f71cc 511
23324ae1
FM
512 Popup window is being shown, but the
513 popup animation has not yet finished.
3c4f71cc 514
23324ae1 515 @c Visible()
3c4f71cc 516
23324ae1
FM
517 Popup window is fully visible.
518 */
328f5751 519 bool IsPopupWindowState(int state) const;
23324ae1
FM
520
521 /**
522 Implement in a derived class to define what happens on
523 dropdown button click.
23324ae1 524 Default action is to show the popup.
cdbcf4c2 525 @note If you implement this to do something else than
23324ae1
FM
526 show the popup, you must then also implement
527 DoSetPopupControl() to always
528 return @NULL.
529 */
530 void OnButtonClick();
531
532 /**
533 Pastes text from the clipboard to the text field.
534 */
535 void Paste();
536
537 /**
538 Removes the text between the two positions in the combo control text field.
3c4f71cc 539
7c913512 540 @param from
4cc4bfaf 541 The first position.
7c913512 542 @param to
4cc4bfaf 543 The last position.
23324ae1
FM
544 */
545 void Remove(long from, long to);
546
547 /**
548 Replaces the text between two positions with the given text, in the combo
549 control text field.
3c4f71cc 550
7c913512 551 @param from
4cc4bfaf 552 The first position.
7c913512 553 @param to
4cc4bfaf 554 The second position.
7c913512 555 @param text
4cc4bfaf 556 The text to insert.
23324ae1
FM
557 */
558 void Replace(long from, long to, const wxString& value);
559
560 /**
561 Sets custom dropdown button graphics.
3c4f71cc 562
7c913512 563 @param bmpNormal
4cc4bfaf 564 Default button image.
7c913512 565 @param pushButtonBg
4cc4bfaf
FM
566 If @true, blank push button background is painted
567 below the image.
7c913512 568 @param bmpPressed
4cc4bfaf 569 Depressed button image.
7c913512 570 @param bmpHover
4cc4bfaf
FM
571 Button image when mouse hovers above it. This
572 should be ignored on platforms and themes that do not generally draw
573 different kind of button on mouse hover.
7c913512 574 @param bmpDisabled
4cc4bfaf 575 Disabled button image.
23324ae1
FM
576 */
577 void SetButtonBitmaps(const wxBitmap& bmpNormal,
4cc4bfaf 578 bool pushButtonBg = false,
23324ae1
FM
579 const wxBitmap& bmpPressed = wxNullBitmap,
580 const wxBitmap& bmpHover = wxNullBitmap,
581 const wxBitmap& bmpDisabled = wxNullBitmap);
582
583 /**
584 Sets size and position of dropdown button.
3c4f71cc 585
7c913512 586 @param width
4cc4bfaf 587 Button width. Value = 0 specifies default.
7c913512 588 @param height
4cc4bfaf 589 Button height. Value = 0 specifies default.
7c913512 590 @param side
4cc4bfaf
FM
591 Indicates which side the button will be placed.
592 Value can be wxLEFT or wxRIGHT.
7c913512 593 @param spacingX
4cc4bfaf 594 Horizontal spacing around the button. Default is 0.
23324ae1
FM
595 */
596 void SetButtonPosition(int width = -1, int height = -1,
597 int side = wxRIGHT,
598 int spacingX = 0);
599
600 /**
601 Set width, in pixels, of custom painted area in control without @c wxCB_READONLY
602 style. In read-only wxOwnerDrawnComboBox, this is used
603 to indicate area that is not covered by the focus rectangle.
604 */
605 void SetCustomPaintWidth(int width);
606
607 /**
608 Sets the insertion point in the text field.
3c4f71cc 609
7c913512 610 @param pos
4cc4bfaf 611 The new insertion point.
23324ae1
FM
612 */
613 void SetInsertionPoint(long pos);
614
615 /**
616 Sets the insertion point at the end of the combo control text field.
617 */
618 void SetInsertionPointEnd();
619
620 /**
621 Set side of the control to which the popup will align itself. Valid values are
622 @c wxLEFT, @c wxRIGHT and 0. The default value 0 means that the most appropriate
623 side is used (which, currently, is always @c wxLEFT).
624 */
625 void SetPopupAnchor(int anchorSide);
626
627 /**
628 Set popup interface class derived from wxComboPopup.
629 This method should be called as soon as possible after the control
630 has been created, unless OnButtonClick()
631 has been overridden.
632 */
633 void SetPopupControl(wxComboPopup* popup);
634
635 /**
636 Extends popup size horizontally, relative to the edges of the combo control.
3c4f71cc 637
7c913512 638 @param extLeft
4cc4bfaf
FM
639 How many pixel to extend beyond the left edge of the
640 control. Default is 0.
7c913512 641 @param extRight
4cc4bfaf
FM
642 How many pixel to extend beyond the right edge of the
643 control. Default is 0.
3c4f71cc 644
23324ae1
FM
645 @remarks Popup minimum width may override arguments.
646 */
647 void SetPopupExtents(int extLeft, int extRight);
648
649 /**
650 Sets preferred maximum height of the popup.
3c4f71cc 651
23324ae1
FM
652 @remarks Value -1 indicates the default.
653 */
654 void SetPopupMaxHeight(int height);
655
656 /**
657 Sets minimum width of the popup. If wider than combo control, it will extend to
658 the left.
3c4f71cc 659
23324ae1
FM
660 @remarks Value -1 indicates the default.
661 */
662 void SetPopupMinWidth(int width);
663
664 /**
665 Selects the text between the two positions, in the combo control text field.
3c4f71cc 666
7c913512 667 @param from
4cc4bfaf 668 The first position.
7c913512 669 @param to
4cc4bfaf 670 The second position.
23324ae1
FM
671 */
672 void SetSelection(long from, long to);
673
674 /**
675 Sets the text for the text field without affecting the
676 popup. Thus, unlike SetValue(), it works
677 equally well with combo control using @c wxCB_READONLY style.
678 */
679 void SetText(const wxString& value);
680
681 /**
682 This will set the space in pixels between left edge of the control and the
683 text, regardless whether control is read-only or not. Value -1 can be
684 given to indicate platform default.
685 */
686 void SetTextIndent(int indent);
687
688 /**
689 Sets the text for the combo control text field.
1f1d2182 690 @note For a combo control with @c wxCB_READONLY style the
23324ae1
FM
691 string must be accepted by the popup (for instance, exist in the dropdown
692 list), otherwise the call to SetValue() is ignored
693 */
694 void SetValue(const wxString& value);
695
696 /**
697 Same as SetValue, but also sends wxCommandEvent of type
698 wxEVT_COMMAND_TEXT_UPDATED
699 if @c withEvent is @true.
700 */
701 void SetValueWithEvent(const wxString& value,
4cc4bfaf 702 bool withEvent = true);
23324ae1
FM
703
704 /**
705 Show the popup.
706 */
707 void ShowPopup();
708
709 /**
710 Undoes the last edit in the text field. Windows only.
711 */
712 void Undo();
713
714 /**
715 Enable or disable usage of an alternative popup window, which guarantees
716 ability to focus the popup control, and allows common native controls to
717 function normally. This alternative popup window is usually a wxDialog,
718 and as such, when it is shown, its parent top-level window will appear
719 as if the focus has been lost from it.
720 */
4cc4bfaf 721 void UseAltPopupWindow(bool enable = true);
23324ae1 722};
e54c96f1 723