combo.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/combo.h
  3. // Purpose: wxComboCtrl declaration
  4. // Author: Jaakko Salli
  5. // Modified by:
  6. // Created: Apr-30-2006
  7. // Copyright: (c) Jaakko Salli
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_COMBOCONTROL_H_BASE_
  11. #define _WX_COMBOCONTROL_H_BASE_
  12. /*
  13. A few words about all the classes defined in this file are probably in
  14. order: why do we need extra wxComboCtrl and wxComboPopup classes?
  15. This is because a traditional combobox is a combination of a text control
  16. (with a button allowing to open the pop down list) with a listbox and
  17. wxComboBox class is exactly such control, however we want to also have other
  18. combinations - in fact, we want to allow anything at all to be used as pop
  19. down list, not just a wxListBox.
  20. So we define a base wxComboCtrl which can use any control as pop down
  21. list and wxComboBox deriving from it which implements the standard wxWidgets
  22. combobox API. wxComboCtrl needs to be told somehow which control to use
  23. and this is done by SetPopupControl(). However, we need something more than
  24. just a wxControl in this method as, for example, we need to call
  25. SetSelection("initial text value") and wxControl doesn't have such method.
  26. So we also need a wxComboPopup which is just a very simple interface which
  27. must be implemented by a control to be usable as a popup.
  28. We couldn't derive wxComboPopup from wxControl as this would make it
  29. impossible to have a class deriving from both wxListBx and from it, so
  30. instead it is just a mix-in.
  31. */
  32. #include "wx/defs.h"
  33. #if wxUSE_COMBOCTRL
  34. #include "wx/control.h"
  35. #include "wx/renderer.h" // this is needed for wxCONTROL_XXX flags
  36. #include "wx/bitmap.h" // wxBitmap used by-value
  37. #include "wx/textentry.h"
  38. class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
  39. class WXDLLIMPEXP_FWD_CORE wxComboPopup;
  40. //
  41. // New window styles for wxComboCtrlBase
  42. //
  43. enum
  44. {
  45. // Double-clicking a read-only combo triggers call to popup's OnComboPopup.
  46. // In wxOwnerDrawnComboBox, for instance, it cycles item.
  47. wxCC_SPECIAL_DCLICK = 0x0100,
  48. // Dropbutton acts like standard push button.
  49. wxCC_STD_BUTTON = 0x0200
  50. };
  51. // wxComboCtrl internal flags
  52. enum
  53. {
  54. // First those that can be passed to Customize.
  55. // It is Windows style for all flags to be clear.
  56. // Button is preferred outside the border (GTK style)
  57. wxCC_BUTTON_OUTSIDE_BORDER = 0x0001,
  58. // Show popup on mouse up instead of mouse down (which is the Windows style)
  59. wxCC_POPUP_ON_MOUSE_UP = 0x0002,
  60. // All text is not automatically selected on click
  61. wxCC_NO_TEXT_AUTO_SELECT = 0x0004,
  62. // Drop-button stays down as long as popup is displayed.
  63. wxCC_BUTTON_STAYS_DOWN = 0x0008,
  64. // Drop-button covers the entire control.
  65. wxCC_FULL_BUTTON = 0x0010,
  66. // Drop-button goes over the custom-border (used under WinVista).
  67. wxCC_BUTTON_COVERS_BORDER = 0x0020,
  68. // Internal use: signals creation is complete
  69. wxCC_IFLAG_CREATED = 0x0100,
  70. // Internal use: really put button outside
  71. wxCC_IFLAG_BUTTON_OUTSIDE = 0x0200,
  72. // Internal use: SetMargins has been successfully called
  73. wxCC_IFLAG_LEFT_MARGIN_SET = 0x0400,
  74. // Internal use: Set wxTAB_TRAVERSAL to parent when popup is dismissed
  75. wxCC_IFLAG_PARENT_TAB_TRAVERSAL = 0x0800,
  76. // Internal use: Secondary popup window type should be used (if available).
  77. wxCC_IFLAG_USE_ALT_POPUP = 0x1000,
  78. // Internal use: Skip popup animation.
  79. wxCC_IFLAG_DISABLE_POPUP_ANIM = 0x2000,
  80. // Internal use: Drop-button is a bitmap button or has non-default size
  81. // (but can still be on either side of the control), regardless whether
  82. // specified by the platform or the application.
  83. wxCC_IFLAG_HAS_NONSTANDARD_BUTTON = 0x4000
  84. };
  85. // Flags used by PreprocessMouseEvent and HandleButtonMouseEvent
  86. enum
  87. {
  88. wxCC_MF_ON_BUTTON = 0x0001, // cursor is on dropbutton area
  89. wxCC_MF_ON_CLICK_AREA = 0x0002 // cursor is on dropbutton or other area
  90. // that can be clicked to show the popup.
  91. };
  92. // Namespace for wxComboCtrl feature flags
  93. struct wxComboCtrlFeatures
  94. {
  95. enum
  96. {
  97. MovableButton = 0x0001, // Button can be on either side of control
  98. BitmapButton = 0x0002, // Button may be replaced with bitmap
  99. ButtonSpacing = 0x0004, // Button can have spacing from the edge
  100. // of the control
  101. TextIndent = 0x0008, // SetMargins can be used to control
  102. // left margin.
  103. PaintControl = 0x0010, // Combo control itself can be custom painted
  104. PaintWritable = 0x0020, // A variable-width area in front of writable
  105. // combo control's textctrl can be custom
  106. // painted
  107. Borderless = 0x0040, // wxNO_BORDER window style works
  108. // There are no feature flags for...
  109. // PushButtonBitmapBackground - if its in wxRendererNative, then it should be
  110. // not an issue to have it automatically under the bitmap.
  111. All = MovableButton|BitmapButton|
  112. ButtonSpacing|TextIndent|
  113. PaintControl|PaintWritable|
  114. Borderless
  115. };
  116. };
  117. class WXDLLIMPEXP_CORE wxComboCtrlBase : public wxControl,
  118. public wxTextEntry
  119. {
  120. friend class wxComboPopup;
  121. friend class wxComboPopupEvtHandler;
  122. public:
  123. // ctors and such
  124. wxComboCtrlBase() : wxControl(), wxTextEntry() { Init(); }
  125. bool Create(wxWindow *parent,
  126. wxWindowID id,
  127. const wxString& value,
  128. const wxPoint& pos,
  129. const wxSize& size,
  130. long style,
  131. const wxValidator& validator,
  132. const wxString& name);
  133. virtual ~wxComboCtrlBase();
  134. // Show/hide popup window (wxComboBox-compatible methods)
  135. virtual void Popup();
  136. virtual void Dismiss()
  137. {
  138. HidePopup(true);
  139. }
  140. // Show/hide popup window.
  141. // TODO: Maybe deprecate in favor of Popup()/Dismiss().
  142. // However, these functions are still called internally
  143. // so it is not straightforward.
  144. virtual void ShowPopup();
  145. virtual void HidePopup(bool generateEvent=false);
  146. // Override for totally custom combo action
  147. virtual void OnButtonClick();
  148. // return true if the popup is currently shown
  149. bool IsPopupShown() const { return m_popupWinState == Visible; }
  150. // set interface class instance derived from wxComboPopup
  151. // NULL popup can be used to indicate default in a derived class
  152. void SetPopupControl( wxComboPopup* popup )
  153. {
  154. DoSetPopupControl(popup);
  155. }
  156. // get interface class instance derived from wxComboPopup
  157. wxComboPopup* GetPopupControl()
  158. {
  159. EnsurePopupControl();
  160. return m_popupInterface;
  161. }
  162. // get the popup window containing the popup control
  163. wxWindow *GetPopupWindow() const { return m_winPopup; }
  164. // Get the text control which is part of the combobox.
  165. wxTextCtrl *GetTextCtrl() const { return m_text; }
  166. // get the dropdown button which is part of the combobox
  167. // note: its not necessarily a wxButton or wxBitmapButton
  168. wxWindow *GetButton() const { return m_btn; }
  169. // forward these methods to all subcontrols
  170. virtual bool Enable(bool enable = true);
  171. virtual bool Show(bool show = true);
  172. virtual bool SetFont(const wxFont& font);
  173. //
  174. // wxTextEntry methods
  175. //
  176. // NB: We basically need to override all of them because there is
  177. // no guarantee how platform-specific wxTextEntry is implemented.
  178. //
  179. virtual void SetValue(const wxString& value)
  180. { wxTextEntryBase::SetValue(value); }
  181. virtual void ChangeValue(const wxString& value)
  182. { wxTextEntryBase::ChangeValue(value); }
  183. virtual void WriteText(const wxString& text);
  184. virtual void AppendText(const wxString& text)
  185. { wxTextEntryBase::AppendText(text); }
  186. virtual wxString GetValue() const
  187. { return wxTextEntryBase::GetValue(); }
  188. virtual wxString GetRange(long from, long to) const
  189. { return wxTextEntryBase::GetRange(from, to); }
  190. // Replace() and DoSetValue() need to be fully re-implemented since
  191. // EventSuppressor utility class does not work with the way
  192. // wxComboCtrl is implemented.
  193. virtual void Replace(long from, long to, const wxString& value);
  194. virtual void Remove(long from, long to);
  195. virtual void Copy();
  196. virtual void Cut();
  197. virtual void Paste();
  198. virtual void Undo();
  199. virtual void Redo();
  200. virtual bool CanUndo() const;
  201. virtual bool CanRedo() const;
  202. virtual void SetInsertionPoint(long pos);
  203. virtual long GetInsertionPoint() const;
  204. virtual long GetLastPosition() const;
  205. virtual void SetSelection(long from, long to);
  206. virtual void GetSelection(long *from, long *to) const;
  207. virtual bool IsEditable() const;
  208. virtual void SetEditable(bool editable);
  209. virtual bool SetHint(const wxString& hint);
  210. virtual wxString GetHint() const;
  211. // This method sets the text without affecting list selection
  212. // (ie. wxComboPopup::SetStringValue doesn't get called).
  213. void SetText(const wxString& value);
  214. // This method sets value and also optionally sends EVT_TEXT
  215. // (needed by combo popups)
  216. wxDEPRECATED( void SetValueWithEvent(const wxString& value,
  217. bool withEvent = true) );
  218. // Changes value of the control as if user had done it by selecting an
  219. // item from a combo box drop-down list. Needs to be public so that
  220. // derived popup classes can call it.
  221. void SetValueByUser(const wxString& value);
  222. //
  223. // Popup customization methods
  224. //
  225. // Sets minimum width of the popup. If wider than combo control, it will extend to the left.
  226. // Remarks:
  227. // * Value -1 indicates the default.
  228. // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
  229. void SetPopupMinWidth( int width )
  230. {
  231. m_widthMinPopup = width;
  232. }
  233. // Sets preferred maximum height of the popup.
  234. // Remarks:
  235. // * Value -1 indicates the default.
  236. // * Custom popup may choose to ignore this (wxOwnerDrawnComboBox does not).
  237. void SetPopupMaxHeight( int height )
  238. {
  239. m_heightPopup = height;
  240. }
  241. // Extends popup size horizontally, relative to the edges of the combo control.
  242. // Remarks:
  243. // * Popup minimum width may override extLeft (ie. it has higher precedence).
  244. // * Values 0 indicate default.
  245. // * Custom popup may not take this fully into account (wxOwnerDrawnComboBox takes).
  246. void SetPopupExtents( int extLeft, int extRight )
  247. {
  248. m_extLeft = extLeft;
  249. m_extRight = extRight;
  250. }
  251. // Set width, in pixels, of custom paint area in writable combo.
  252. // In read-only, used to indicate area that is not covered by the
  253. // focus rectangle (which may or may not be drawn, depending on the
  254. // popup type).
  255. void SetCustomPaintWidth( int width );
  256. int GetCustomPaintWidth() const { return m_widthCustomPaint; }
  257. // Set side of the control to which the popup will align itself.
  258. // Valid values are wxLEFT, wxRIGHT and 0. The default value 0 wmeans
  259. // that the side of the button will be used.
  260. void SetPopupAnchor( int anchorSide )
  261. {
  262. m_anchorSide = anchorSide;
  263. }
  264. // Set position of dropdown button.
  265. // width: button width. <= 0 for default.
  266. // height: button height. <= 0 for default.
  267. // side: wxLEFT or wxRIGHT, indicates on which side the button will be placed.
  268. // spacingX: empty space on sides of the button. Default is 0.
  269. // Remarks:
  270. // There is no spacingY - the button will be centered vertically.
  271. void SetButtonPosition( int width = -1,
  272. int height = -1,
  273. int side = wxRIGHT,
  274. int spacingX = 0 );
  275. // Returns current size of the dropdown button.
  276. wxSize GetButtonSize();
  277. //
  278. // Sets dropbutton to be drawn with custom bitmaps.
  279. //
  280. // bmpNormal: drawn when cursor is not on button
  281. // pushButtonBg: Draw push button background below the image.
  282. // NOTE! This is usually only properly supported on platforms with appropriate
  283. // method in wxRendererNative.
  284. // bmpPressed: drawn when button is depressed
  285. // bmpHover: drawn when cursor hovers on button. This is ignored on platforms
  286. // that do not generally display hover differently.
  287. // bmpDisabled: drawn when combobox is disabled.
  288. void SetButtonBitmaps( const wxBitmap& bmpNormal,
  289. bool pushButtonBg = false,
  290. const wxBitmap& bmpPressed = wxNullBitmap,
  291. const wxBitmap& bmpHover = wxNullBitmap,
  292. const wxBitmap& bmpDisabled = wxNullBitmap );
  293. #if WXWIN_COMPATIBILITY_2_8
  294. //
  295. // This will set the space in pixels between left edge of the control and the
  296. // text, regardless whether control is read-only (ie. no wxTextCtrl) or not.
  297. // Platform-specific default can be set with value-1.
  298. // Remarks
  299. // * This method may do nothing on some native implementations.
  300. wxDEPRECATED( void SetTextIndent( int indent ) );
  301. // Returns actual indentation in pixels.
  302. wxDEPRECATED( wxCoord GetTextIndent() const );
  303. #endif
  304. // Returns area covered by the text field.
  305. const wxRect& GetTextRect() const
  306. {
  307. return m_tcArea;
  308. }
  309. // Call with enable as true to use a type of popup window that guarantees ability
  310. // to focus the popup control, and normal function of common native controls.
  311. // This alternative popup window is usually a wxDialog, and as such it's parent
  312. // frame will appear as if the focus has been lost from it.
  313. void UseAltPopupWindow( bool enable = true )
  314. {
  315. wxASSERT_MSG( !m_winPopup,
  316. wxT("call this only before SetPopupControl") );
  317. if ( enable )
  318. m_iFlags |= wxCC_IFLAG_USE_ALT_POPUP;
  319. else
  320. m_iFlags &= ~wxCC_IFLAG_USE_ALT_POPUP;
  321. }
  322. // Call with false to disable popup animation, if any.
  323. void EnablePopupAnimation( bool enable = true )
  324. {
  325. if ( enable )
  326. m_iFlags &= ~wxCC_IFLAG_DISABLE_POPUP_ANIM;
  327. else
  328. m_iFlags |= wxCC_IFLAG_DISABLE_POPUP_ANIM;
  329. }
  330. //
  331. // Utilies needed by the popups or native implementations
  332. //
  333. // Returns true if given key combination should toggle the popup.
  334. // NB: This is a separate from other keyboard handling because:
  335. // 1) Replaceability.
  336. // 2) Centralized code (otherwise it'd be split up between
  337. // wxComboCtrl key handler and wxVListBoxComboPopup's
  338. // key handler).
  339. virtual bool IsKeyPopupToggle(const wxKeyEvent& event) const = 0;
  340. // Prepare background of combo control or an item in a dropdown list
  341. // in a way typical on platform. This includes painting the focus/disabled
  342. // background and setting the clipping region.
  343. // Unless you plan to paint your own focus indicator, you should always call this
  344. // in your wxComboPopup::PaintComboControl implementation.
  345. // In addition, it sets pen and text colour to what looks good and proper
  346. // against the background.
  347. // flags: wxRendererNative flags: wxCONTROL_ISSUBMENU: is drawing a list item instead of combo control
  348. // wxCONTROL_SELECTED: list item is selected
  349. // wxCONTROL_DISABLED: control/item is disabled
  350. virtual void PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const;
  351. // Returns true if focus indicator should be drawn in the control.
  352. bool ShouldDrawFocus() const
  353. {
  354. const wxWindow* curFocus = FindFocus();
  355. return ( IsPopupWindowState(Hidden) &&
  356. (curFocus == m_mainCtrlWnd || (m_btn && curFocus == m_btn)) &&
  357. (m_windowStyle & wxCB_READONLY) );
  358. }
  359. // These methods return references to appropriate dropbutton bitmaps
  360. const wxBitmap& GetBitmapNormal() const { return m_bmpNormal; }
  361. const wxBitmap& GetBitmapPressed() const { return m_bmpPressed; }
  362. const wxBitmap& GetBitmapHover() const { return m_bmpHover; }
  363. const wxBitmap& GetBitmapDisabled() const { return m_bmpDisabled; }
  364. // Set custom style flags for embedded wxTextCtrl. Usually must be used
  365. // with two-step creation, before Create() call.
  366. void SetTextCtrlStyle( int style );
  367. // Return internal flags
  368. wxUint32 GetInternalFlags() const { return m_iFlags; }
  369. // Return true if Create has finished
  370. bool IsCreated() const { return m_iFlags & wxCC_IFLAG_CREATED ? true : false; }
  371. // Need to override to return text area background colour
  372. wxColour GetBackgroundColour() const;
  373. // common code to be called on popup hide/dismiss
  374. void OnPopupDismiss(bool generateEvent);
  375. // PopupShown states
  376. enum
  377. {
  378. Hidden = 0,
  379. //Closing = 1,
  380. Animating = 2,
  381. Visible = 3
  382. };
  383. bool IsPopupWindowState( int state ) const { return (state == m_popupWinState) ? true : false; }
  384. wxByte GetPopupWindowState() const { return m_popupWinState; }
  385. // Set value returned by GetMainWindowOfCompositeControl
  386. void SetCtrlMainWnd( wxWindow* wnd ) { m_mainCtrlWnd = wnd; }
  387. // This is public so we can access it from wxComboCtrlTextCtrl
  388. virtual wxWindow *GetMainWindowOfCompositeControl()
  389. { return m_mainCtrlWnd; }
  390. // also set the embedded wxTextCtrl colours
  391. virtual bool SetForegroundColour(const wxColour& colour);
  392. virtual bool SetBackgroundColour(const wxColour& colour);
  393. protected:
  394. // Returns true if hint text should be drawn in the control
  395. bool ShouldUseHintText(int flags = 0) const
  396. {
  397. return ( !m_text &&
  398. !(flags & wxCONTROL_ISSUBMENU) &&
  399. !m_valueString.length() &&
  400. m_hintText.length() &&
  401. !ShouldDrawFocus() );
  402. }
  403. //
  404. // Override these for customization purposes
  405. //
  406. // called from wxSizeEvent handler
  407. virtual void OnResize() = 0;
  408. // Return native text identation
  409. // (i.e. text margin, for pure text, not textctrl)
  410. virtual wxCoord GetNativeTextIndent() const;
  411. // Called in syscolourchanged handler and base create
  412. virtual void OnThemeChange();
  413. // Creates wxTextCtrl.
  414. // extraStyle: Extra style parameters
  415. void CreateTextCtrl( int extraStyle );
  416. // Called when text was changed programmatically
  417. // (e.g. from WriteText())
  418. void OnSetValue(const wxString& value);
  419. // Installs standard input handler to combo (and optionally to the textctrl)
  420. void InstallInputHandlers();
  421. // Flags for DrawButton
  422. enum
  423. {
  424. Button_PaintBackground = 0x0001, // Paints control background below the button
  425. Button_BitmapOnly = 0x0002 // Only paints the bitmap
  426. };
  427. // Draws dropbutton. Using wxRenderer or bitmaps, as appropriate.
  428. // Flags are defined above.
  429. virtual void DrawButton( wxDC& dc, const wxRect& rect, int flags = Button_PaintBackground );
  430. // Call if cursor is on button area or mouse is captured for the button.
  431. //bool HandleButtonMouseEvent( wxMouseEvent& event, bool isInside );
  432. bool HandleButtonMouseEvent( wxMouseEvent& event, int flags );
  433. // returns true if event was consumed or filtered (event type is also set to 0 in this case)
  434. bool PreprocessMouseEvent( wxMouseEvent& event, int flags );
  435. //
  436. // This will handle left_down and left_dclick events outside button in a Windows-like manner.
  437. // If you need alternate behaviour, it is recommended you manipulate and filter events to it
  438. // instead of building your own handling routine (for reference, on wxEVT_LEFT_DOWN it will
  439. // toggle popup and on wxEVT_LEFT_DCLICK it will do the same or run the popup's dclick method,
  440. // if defined - you should pass events of other types of it for common processing).
  441. void HandleNormalMouseEvent( wxMouseEvent& event );
  442. // Creates popup window, calls interface->Create(), etc
  443. void CreatePopup();
  444. // Destroy popup window and all related constructs
  445. void DestroyPopup();
  446. // override the base class virtuals involved in geometry calculations
  447. // The common version only sets a default width, so the derived classes
  448. // should override it and set the height and change the width as needed.
  449. virtual wxSize DoGetBestSize() const;
  450. virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const;
  451. // NULL popup can be used to indicate default in a derived class
  452. virtual void DoSetPopupControl(wxComboPopup* popup);
  453. // ensures there is atleast the default popup
  454. void EnsurePopupControl();
  455. // Recalculates button and textctrl areas. Called when size or button setup change.
  456. // btnWidth: default/calculated width of the dropbutton. 0 means unchanged,
  457. // just recalculate.
  458. void CalculateAreas( int btnWidth = 0 );
  459. // Standard textctrl positioning routine. Just give it platform-dependent
  460. // textctrl coordinate adjustment.
  461. virtual void PositionTextCtrl( int textCtrlXAdjust = 0,
  462. int textCtrlYAdjust = 0);
  463. // event handlers
  464. void OnSizeEvent( wxSizeEvent& event );
  465. void OnFocusEvent(wxFocusEvent& event);
  466. void OnIdleEvent(wxIdleEvent& event);
  467. void OnTextCtrlEvent(wxCommandEvent& event);
  468. void OnSysColourChanged(wxSysColourChangedEvent& event);
  469. void OnKeyEvent(wxKeyEvent& event);
  470. void OnCharEvent(wxKeyEvent& event);
  471. // Set customization flags (directs how wxComboCtrlBase helpers behave)
  472. void Customize( wxUint32 flags ) { m_iFlags |= flags; }
  473. // Dispatches size event and refreshes
  474. void RecalcAndRefresh();
  475. // Flags for DoShowPopup and AnimateShow
  476. enum
  477. {
  478. ShowBelow = 0x0000, // Showing popup below the control
  479. ShowAbove = 0x0001, // Showing popup above the control
  480. CanDeferShow = 0x0002 // Can only return true from AnimateShow if this is set
  481. };
  482. // Shows and positions the popup.
  483. virtual void DoShowPopup( const wxRect& rect, int flags );
  484. // Implement in derived class to create a drop-down animation.
  485. // Return true if finished immediately. Otherwise popup is only
  486. // shown when the derived class call DoShowPopup.
  487. // Flags are same as for DoShowPopup.
  488. virtual bool AnimateShow( const wxRect& rect, int flags );
  489. #if wxUSE_TOOLTIPS
  490. virtual void DoSetToolTip( wxToolTip *tip );
  491. #endif
  492. // protected wxTextEntry methods
  493. virtual void DoSetValue(const wxString& value, int flags);
  494. virtual wxString DoGetValue() const;
  495. virtual wxWindow *GetEditableWindow() { return this; }
  496. // margins functions
  497. virtual bool DoSetMargins(const wxPoint& pt);
  498. virtual wxPoint DoGetMargins() const;
  499. // This is used when m_text is hidden (readonly).
  500. wxString m_valueString;
  501. // This is used when control is unfocused and m_valueString is empty
  502. wxString m_hintText;
  503. // the text control and button we show all the time
  504. wxTextCtrl* m_text;
  505. wxWindow* m_btn;
  506. // wxPopupWindow or similar containing the window managed by the interface.
  507. wxWindow* m_winPopup;
  508. // the popup control/panel
  509. wxWindow* m_popup;
  510. // popup interface
  511. wxComboPopup* m_popupInterface;
  512. // this is input etc. handler for the text control
  513. wxEvtHandler* m_textEvtHandler;
  514. // this is for the top level window
  515. wxEvtHandler* m_toplevEvtHandler;
  516. // this is for the control in popup
  517. wxEvtHandler* m_popupEvtHandler;
  518. // this is for the popup window
  519. wxEvtHandler* m_popupWinEvtHandler;
  520. // main (ie. topmost) window of a composite control (default = this)
  521. wxWindow* m_mainCtrlWnd;
  522. // used to prevent immediate re-popupping in case closed popup
  523. // by clicking on the combo control (needed because of inconsistent
  524. // transient implementation across platforms).
  525. wxLongLong m_timeCanAcceptClick;
  526. // how much popup should expand to the left/right of the control
  527. wxCoord m_extLeft;
  528. wxCoord m_extRight;
  529. // minimum popup width
  530. wxCoord m_widthMinPopup;
  531. // preferred popup height
  532. wxCoord m_heightPopup;
  533. // how much of writable combo is custom-paint by callback?
  534. // also used to indicate area that is not covered by "blue"
  535. // selection indicator.
  536. wxCoord m_widthCustomPaint;
  537. // left margin, in pixels
  538. wxCoord m_marginLeft;
  539. // side on which the popup is aligned
  540. int m_anchorSide;
  541. // Width of the "fake" border
  542. wxCoord m_widthCustomBorder;
  543. // The button and textctrl click/paint areas
  544. wxRect m_tcArea;
  545. wxRect m_btnArea;
  546. // Colour of the text area, in case m_text is NULL
  547. wxColour m_tcBgCol;
  548. // current button state (uses renderer flags)
  549. int m_btnState;
  550. // button position
  551. int m_btnWid;
  552. int m_btnHei;
  553. int m_btnSide;
  554. int m_btnSpacingX;
  555. // last default button width
  556. int m_btnWidDefault;
  557. // custom dropbutton bitmaps
  558. wxBitmap m_bmpNormal;
  559. wxBitmap m_bmpPressed;
  560. wxBitmap m_bmpHover;
  561. wxBitmap m_bmpDisabled;
  562. // area used by the button
  563. wxSize m_btnSize;
  564. // platform-dependent customization and other flags
  565. wxUint32 m_iFlags;
  566. // custom style for m_text
  567. int m_textCtrlStyle;
  568. // draw blank button background under bitmap?
  569. bool m_blankButtonBg;
  570. // is the popup window currenty shown?
  571. wxByte m_popupWinState;
  572. // should the focus be reset to the textctrl in idle time?
  573. bool m_resetFocus;
  574. // is the text-area background colour overridden?
  575. bool m_hasTcBgCol;
  576. private:
  577. void Init();
  578. wxByte m_ignoreEvtText; // Number of next EVT_TEXTs to ignore
  579. // Is popup window wxPopupTransientWindow, wxPopupWindow or wxDialog?
  580. wxByte m_popupWinType;
  581. DECLARE_EVENT_TABLE()
  582. DECLARE_ABSTRACT_CLASS(wxComboCtrlBase)
  583. };
  584. // ----------------------------------------------------------------------------
  585. // wxComboPopup is the interface which must be implemented by a control to be
  586. // used as a popup by wxComboCtrl
  587. // ----------------------------------------------------------------------------
  588. // wxComboPopup internal flags
  589. enum
  590. {
  591. wxCP_IFLAG_CREATED = 0x0001 // Set by wxComboCtrlBase after Create is called
  592. };
  593. class WXDLLIMPEXP_FWD_CORE wxComboCtrl;
  594. class WXDLLIMPEXP_CORE wxComboPopup
  595. {
  596. friend class wxComboCtrlBase;
  597. public:
  598. wxComboPopup()
  599. {
  600. m_combo = NULL;
  601. m_iFlags = 0;
  602. }
  603. // This is called immediately after construction finishes. m_combo member
  604. // variable has been initialized before the call.
  605. // NOTE: It is not in constructor so the derived class doesn't need to redefine
  606. // a default constructor of its own.
  607. virtual void Init() { }
  608. virtual ~wxComboPopup();
  609. // Create the popup child control.
  610. // Return true for success.
  611. virtual bool Create(wxWindow* parent) = 0;
  612. // Calls Destroy() for the popup control (i.e. one returned by
  613. // GetControl()) and makes sure that 'this' is deleted at the end.
  614. // Default implementation works for both cases where popup control
  615. // class is multiple inherited or created on heap as a separate
  616. // object.
  617. virtual void DestroyPopup();
  618. // We must have an associated control which is subclassed by the combobox.
  619. virtual wxWindow *GetControl() = 0;
  620. // Called immediately after the popup is shown
  621. virtual void OnPopup();
  622. // Called when popup is dismissed
  623. virtual void OnDismiss();
  624. // Called just prior to displaying popup.
  625. // Default implementation does nothing.
  626. virtual void SetStringValue( const wxString& value );
  627. // Gets displayed string representation of the value.
  628. virtual wxString GetStringValue() const = 0;
  629. // Called to check if the popup - when an item container - actually
  630. // has matching item. Case-sensitivity checking etc. is up to the
  631. // implementation. If the found item matched the string, but is
  632. // different, it should be written back to pItem. Default implementation
  633. // always return true and does not alter trueItem.
  634. virtual bool FindItem(const wxString& item, wxString* trueItem=NULL);
  635. // This is called to custom paint in the combo control itself (ie. not the popup).
  636. // Default implementation draws value as string.
  637. virtual void PaintComboControl( wxDC& dc, const wxRect& rect );
  638. // Receives wxEVT_KEY_DOWN key events from the parent wxComboCtrl.
  639. // Events not handled should be skipped, as usual.
  640. virtual void OnComboKeyEvent( wxKeyEvent& event );
  641. // Receives wxEVT_CHAR key events from the parent wxComboCtrl.
  642. // Events not handled should be skipped, as usual.
  643. virtual void OnComboCharEvent( wxKeyEvent& event );
  644. // Implement if you need to support special action when user
  645. // double-clicks on the parent wxComboCtrl.
  646. virtual void OnComboDoubleClick();
  647. // Return final size of popup. Called on every popup, just prior to OnShow.
  648. // minWidth = preferred minimum width for window
  649. // prefHeight = preferred height. Only applies if > 0,
  650. // maxHeight = max height for window, as limited by screen size
  651. // and should only be rounded down, if necessary.
  652. virtual wxSize GetAdjustedSize( int minWidth, int prefHeight, int maxHeight );
  653. // Return true if you want delay call to Create until the popup is shown
  654. // for the first time. It is more efficient, but note that it is often
  655. // more convenient to have the control created immediately.
  656. // Default returns false.
  657. virtual bool LazyCreate();
  658. //
  659. // Utilies
  660. //
  661. // Hides the popup
  662. void Dismiss();
  663. // Returns true if Create has been called.
  664. bool IsCreated() const
  665. {
  666. return (m_iFlags & wxCP_IFLAG_CREATED) ? true : false;
  667. }
  668. // Returns pointer to the associated parent wxComboCtrl.
  669. wxComboCtrl* GetComboCtrl() const;
  670. // Default PaintComboControl behaviour
  671. static void DefaultPaintComboControl( wxComboCtrlBase* combo,
  672. wxDC& dc,
  673. const wxRect& rect );
  674. protected:
  675. wxComboCtrlBase* m_combo;
  676. wxUint32 m_iFlags;
  677. private:
  678. // Called in wxComboCtrlBase::SetPopupControl
  679. void InitBase(wxComboCtrlBase *combo)
  680. {
  681. m_combo = combo;
  682. }
  683. };
  684. // ----------------------------------------------------------------------------
  685. // include the platform-dependent header defining the real class
  686. // ----------------------------------------------------------------------------
  687. #if defined(__WXUNIVERSAL__)
  688. // No native universal (but it must still be first in the list)
  689. #elif defined(__WXMSW__)
  690. #include "wx/msw/combo.h"
  691. #endif
  692. // Any ports may need generic as an alternative
  693. #include "wx/generic/combo.h"
  694. #endif // wxUSE_COMBOCTRL
  695. #endif
  696. // _WX_COMBOCONTROL_H_BASE_