editors.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/propgrid/editors.h
  3. // Purpose: wxPropertyGrid editors
  4. // Author: Jaakko Salli
  5. // Modified by:
  6. // Created: 2007-04-14
  7. // Copyright: (c) Jaakko Salli
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_PROPGRID_EDITORS_H_
  11. #define _WX_PROPGRID_EDITORS_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_PROPGRID
  14. class WXDLLIMPEXP_FWD_PROPGRID wxPGCell;
  15. class WXDLLIMPEXP_FWD_PROPGRID wxPGProperty;
  16. class WXDLLIMPEXP_FWD_PROPGRID wxPropertyGrid;
  17. // -----------------------------------------------------------------------
  18. // wxPGWindowList contains list of editor windows returned by CreateControls.
  19. class wxPGWindowList
  20. {
  21. public:
  22. wxPGWindowList()
  23. {
  24. m_primary = m_secondary = NULL;
  25. }
  26. void SetSecondary( wxWindow* secondary ) { m_secondary = secondary; }
  27. wxWindow* m_primary;
  28. wxWindow* m_secondary;
  29. wxPGWindowList( wxWindow* a )
  30. {
  31. m_primary = a;
  32. m_secondary = NULL;
  33. }
  34. wxPGWindowList( wxWindow* a, wxWindow* b )
  35. {
  36. m_primary = a;
  37. m_secondary = b;
  38. }
  39. };
  40. // -----------------------------------------------------------------------
  41. /** @class wxPGEditor
  42. Base class for custom wxPropertyGrid editors.
  43. @remarks
  44. - Names of builtin property editors are: TextCtrl, Choice,
  45. ComboBox, CheckBox, TextCtrlAndButton, and ChoiceAndButton. Additional
  46. editors include SpinCtrl and DatePickerCtrl, but using them requires
  47. calling wxPropertyGrid::RegisterAdditionalEditors() prior use.
  48. - Pointer to builtin editor is available as wxPGEditor_EditorName
  49. (eg. wxPGEditor_TextCtrl).
  50. - To add new editor you need to register it first using static function
  51. wxPropertyGrid::RegisterEditorClass(), with code like this:
  52. @code
  53. wxPGEditor *editorPointer = wxPropertyGrid::RegisterEditorClass(
  54. new MyEditorClass(), "MyEditor");
  55. @endcode
  56. After that, wxPropertyGrid will take ownership of the given object, but
  57. you should still store editorPointer somewhere, so you can pass it to
  58. wxPGProperty::SetEditor(), or return it from
  59. wxPGEditor::DoGetEditorClass().
  60. @library{wxpropgrid}
  61. @category{propgrid}
  62. */
  63. class WXDLLIMPEXP_PROPGRID wxPGEditor : public wxObject
  64. {
  65. DECLARE_ABSTRACT_CLASS(wxPGEditor)
  66. public:
  67. /** Constructor. */
  68. wxPGEditor()
  69. : wxObject()
  70. {
  71. m_clientData = NULL;
  72. }
  73. /** Destructor. */
  74. virtual ~wxPGEditor();
  75. /**
  76. Returns pointer to the name of the editor. For example,
  77. wxPGEditor_TextCtrl has name "TextCtrl". If you dont' need to access
  78. your custom editor by string name, then you do not need to implement
  79. this function.
  80. */
  81. virtual wxString GetName() const;
  82. /**
  83. Instantiates editor controls.
  84. @param propgrid
  85. wxPropertyGrid to which the property belongs (use as parent for
  86. control).
  87. @param property
  88. Property for which this method is called.
  89. @param pos
  90. Position, inside wxPropertyGrid, to create control(s) to.
  91. @param size
  92. Initial size for control(s).
  93. @remarks
  94. - Primary control shall use id wxPG_SUBID1, and secondary (button)
  95. control shall use wxPG_SUBID2.
  96. - Unlike in previous version of wxPropertyGrid, it is no longer
  97. necessary to call wxEvtHandler::Connect() for interesting editor
  98. events. Instead, all events from control are now automatically
  99. forwarded to wxPGEditor::OnEvent() and wxPGProperty::OnEvent().
  100. */
  101. virtual wxPGWindowList CreateControls(wxPropertyGrid* propgrid,
  102. wxPGProperty* property,
  103. const wxPoint& pos,
  104. const wxSize& size) const = 0;
  105. /** Loads value from property to the control. */
  106. virtual void UpdateControl( wxPGProperty* property,
  107. wxWindow* ctrl ) const = 0;
  108. /**
  109. Used to get the renderer to draw the value with when the control is
  110. hidden.
  111. Default implementation returns g_wxPGDefaultRenderer.
  112. */
  113. //virtual wxPGCellRenderer* GetCellRenderer() const;
  114. /** Draws value for given property.
  115. */
  116. virtual void DrawValue( wxDC& dc,
  117. const wxRect& rect,
  118. wxPGProperty* property,
  119. const wxString& text ) const;
  120. /** Handles events. Returns true if value in control was modified
  121. (see wxPGProperty::OnEvent for more information).
  122. @remarks wxPropertyGrid will automatically unfocus the editor when
  123. wxEVT_TEXT_ENTER is received and when it results in
  124. property value being modified. This happens regardless of
  125. editor type (ie. behaviour is same for any wxTextCtrl and
  126. wxComboBox based editor).
  127. */
  128. virtual bool OnEvent( wxPropertyGrid* propgrid, wxPGProperty* property,
  129. wxWindow* wnd_primary, wxEvent& event ) const = 0;
  130. /** Returns value from control, via parameter 'variant'.
  131. Usually ends up calling property's StringToValue or IntToValue.
  132. Returns true if value was different.
  133. */
  134. virtual bool GetValueFromControl( wxVariant& variant,
  135. wxPGProperty* property,
  136. wxWindow* ctrl ) const;
  137. /**
  138. Sets new appearance for the control. Default implementation
  139. sets foreground colour, background colour, font, plus text
  140. for wxTextCtrl and wxComboCtrl.
  141. @param appearance
  142. New appearance to be applied.
  143. @param oldAppearance
  144. Previously applied appearance. Used to detect which
  145. control attributes need to be changed (e.g. so we only
  146. change background colour if really needed).
  147. @param unspecified
  148. @true if the new appearance represents an unspecified
  149. property value.
  150. */
  151. virtual void SetControlAppearance( wxPropertyGrid* pg,
  152. wxPGProperty* property,
  153. wxWindow* ctrl,
  154. const wxPGCell& appearance,
  155. const wxPGCell& oldAppearance,
  156. bool unspecified ) const;
  157. /**
  158. Sets value in control to unspecified.
  159. */
  160. virtual void SetValueToUnspecified( wxPGProperty* property,
  161. wxWindow* ctrl ) const;
  162. /** Sets control's value specifically from string. */
  163. virtual void SetControlStringValue( wxPGProperty* property,
  164. wxWindow* ctrl,
  165. const wxString& txt ) const;
  166. /** Sets control's value specifically from int (applies to choice etc.). */
  167. virtual void SetControlIntValue( wxPGProperty* property,
  168. wxWindow* ctrl,
  169. int value ) const;
  170. /** Inserts item to existing control. Index -1 means appending.
  171. Default implementation does nothing. Returns index of item added.
  172. */
  173. virtual int InsertItem( wxWindow* ctrl,
  174. const wxString& label,
  175. int index ) const;
  176. /** Deletes item from existing control.
  177. Default implementation does nothing.
  178. */
  179. virtual void DeleteItem( wxWindow* ctrl, int index ) const;
  180. /** Extra processing when control gains focus. For example, wxTextCtrl
  181. based controls should select all text.
  182. */
  183. virtual void OnFocus( wxPGProperty* property, wxWindow* wnd ) const;
  184. /** Returns true if control itself can contain the custom image. Default is
  185. to return false.
  186. */
  187. virtual bool CanContainCustomImage() const;
  188. //
  189. // This member is public so scripting language bindings
  190. // wrapper code can access it freely.
  191. void* m_clientData;
  192. };
  193. #define WX_PG_IMPLEMENT_INTERNAL_EDITOR_CLASS(EDITOR,CLASSNAME,BASECLASS) \
  194. IMPLEMENT_DYNAMIC_CLASS(CLASSNAME, BASECLASS) \
  195. wxString CLASSNAME::GetName() const \
  196. { \
  197. return wxS(#EDITOR); \
  198. } \
  199. wxPGEditor* wxPGEditor_##EDITOR = NULL;
  200. //
  201. // Following are the built-in editor classes.
  202. //
  203. class WXDLLIMPEXP_PROPGRID wxPGTextCtrlEditor : public wxPGEditor
  204. {
  205. DECLARE_DYNAMIC_CLASS(wxPGTextCtrlEditor)
  206. public:
  207. wxPGTextCtrlEditor() {}
  208. virtual ~wxPGTextCtrlEditor();
  209. virtual wxPGWindowList CreateControls(wxPropertyGrid* propgrid,
  210. wxPGProperty* property,
  211. const wxPoint& pos,
  212. const wxSize& size) const;
  213. virtual void UpdateControl( wxPGProperty* property,
  214. wxWindow* ctrl ) const;
  215. virtual bool OnEvent( wxPropertyGrid* propgrid,
  216. wxPGProperty* property,
  217. wxWindow* primaryCtrl,
  218. wxEvent& event ) const;
  219. virtual bool GetValueFromControl( wxVariant& variant,
  220. wxPGProperty* property,
  221. wxWindow* ctrl ) const;
  222. virtual wxString GetName() const;
  223. //virtual wxPGCellRenderer* GetCellRenderer() const;
  224. virtual void SetControlStringValue( wxPGProperty* property,
  225. wxWindow* ctrl,
  226. const wxString& txt ) const;
  227. virtual void OnFocus( wxPGProperty* property, wxWindow* wnd ) const;
  228. // Provided so that, for example, ComboBox editor can use the same code
  229. // (multiple inheritance would get way too messy).
  230. static bool OnTextCtrlEvent( wxPropertyGrid* propgrid,
  231. wxPGProperty* property,
  232. wxWindow* ctrl,
  233. wxEvent& event );
  234. static bool GetTextCtrlValueFromControl( wxVariant& variant,
  235. wxPGProperty* property,
  236. wxWindow* ctrl );
  237. };
  238. class WXDLLIMPEXP_PROPGRID wxPGChoiceEditor : public wxPGEditor
  239. {
  240. DECLARE_DYNAMIC_CLASS(wxPGChoiceEditor)
  241. public:
  242. wxPGChoiceEditor() {}
  243. virtual ~wxPGChoiceEditor();
  244. virtual wxPGWindowList CreateControls(wxPropertyGrid* propgrid,
  245. wxPGProperty* property,
  246. const wxPoint& pos,
  247. const wxSize& size) const;
  248. virtual void UpdateControl( wxPGProperty* property,
  249. wxWindow* ctrl ) const;
  250. virtual bool OnEvent( wxPropertyGrid* propgrid,
  251. wxPGProperty* property,
  252. wxWindow* primaryCtrl,
  253. wxEvent& event ) const;
  254. virtual bool GetValueFromControl( wxVariant& variant,
  255. wxPGProperty* property,
  256. wxWindow* ctrl ) const;
  257. virtual void SetValueToUnspecified( wxPGProperty* property,
  258. wxWindow* ctrl ) const;
  259. virtual wxString GetName() const;
  260. virtual void SetControlIntValue( wxPGProperty* property,
  261. wxWindow* ctrl,
  262. int value ) const;
  263. virtual void SetControlStringValue( wxPGProperty* property,
  264. wxWindow* ctrl,
  265. const wxString& txt ) const;
  266. virtual int InsertItem( wxWindow* ctrl,
  267. const wxString& label,
  268. int index ) const;
  269. virtual void DeleteItem( wxWindow* ctrl, int index ) const;
  270. virtual bool CanContainCustomImage() const;
  271. // CreateControls calls this with CB_READONLY in extraStyle
  272. wxWindow* CreateControlsBase( wxPropertyGrid* propgrid,
  273. wxPGProperty* property,
  274. const wxPoint& pos,
  275. const wxSize& sz,
  276. long extraStyle ) const;
  277. };
  278. class WXDLLIMPEXP_PROPGRID wxPGComboBoxEditor : public wxPGChoiceEditor
  279. {
  280. DECLARE_DYNAMIC_CLASS(wxPGComboBoxEditor)
  281. public:
  282. wxPGComboBoxEditor() {}
  283. virtual ~wxPGComboBoxEditor();
  284. virtual wxPGWindowList CreateControls(wxPropertyGrid* propgrid,
  285. wxPGProperty* property,
  286. const wxPoint& pos,
  287. const wxSize& size) const;
  288. virtual wxString GetName() const;
  289. virtual void UpdateControl( wxPGProperty* property, wxWindow* ctrl ) const;
  290. virtual bool OnEvent( wxPropertyGrid* propgrid, wxPGProperty* property,
  291. wxWindow* ctrl, wxEvent& event ) const;
  292. virtual bool GetValueFromControl( wxVariant& variant,
  293. wxPGProperty* property,
  294. wxWindow* ctrl ) const;
  295. virtual void OnFocus( wxPGProperty* property, wxWindow* wnd ) const;
  296. };
  297. class WXDLLIMPEXP_PROPGRID wxPGChoiceAndButtonEditor : public wxPGChoiceEditor
  298. {
  299. public:
  300. wxPGChoiceAndButtonEditor() {}
  301. virtual ~wxPGChoiceAndButtonEditor();
  302. virtual wxString GetName() const;
  303. virtual wxPGWindowList CreateControls(wxPropertyGrid* propgrid,
  304. wxPGProperty* property,
  305. const wxPoint& pos,
  306. const wxSize& size) const;
  307. DECLARE_DYNAMIC_CLASS(wxPGChoiceAndButtonEditor)
  308. };
  309. class WXDLLIMPEXP_PROPGRID
  310. wxPGTextCtrlAndButtonEditor : public wxPGTextCtrlEditor
  311. {
  312. public:
  313. wxPGTextCtrlAndButtonEditor() {}
  314. virtual ~wxPGTextCtrlAndButtonEditor();
  315. virtual wxString GetName() const;
  316. virtual wxPGWindowList CreateControls(wxPropertyGrid* propgrid,
  317. wxPGProperty* property,
  318. const wxPoint& pos,
  319. const wxSize& size) const;
  320. DECLARE_DYNAMIC_CLASS(wxPGTextCtrlAndButtonEditor)
  321. };
  322. #if wxPG_INCLUDE_CHECKBOX
  323. //
  324. // Use custom check box code instead of native control
  325. // for cleaner (ie. more integrated) look.
  326. //
  327. class WXDLLIMPEXP_PROPGRID wxPGCheckBoxEditor : public wxPGEditor
  328. {
  329. DECLARE_DYNAMIC_CLASS(wxPGCheckBoxEditor)
  330. public:
  331. wxPGCheckBoxEditor() {}
  332. virtual ~wxPGCheckBoxEditor();
  333. virtual wxString GetName() const;
  334. virtual wxPGWindowList CreateControls(wxPropertyGrid* propgrid,
  335. wxPGProperty* property,
  336. const wxPoint& pos,
  337. const wxSize& size) const;
  338. virtual void UpdateControl( wxPGProperty* property,
  339. wxWindow* ctrl ) const;
  340. virtual bool OnEvent( wxPropertyGrid* propgrid,
  341. wxPGProperty* property,
  342. wxWindow* primaryCtrl,
  343. wxEvent& event ) const;
  344. virtual bool GetValueFromControl( wxVariant& variant,
  345. wxPGProperty* property,
  346. wxWindow* ctrl ) const;
  347. virtual void SetValueToUnspecified( wxPGProperty* property,
  348. wxWindow* ctrl ) const;
  349. virtual void DrawValue( wxDC& dc,
  350. const wxRect& rect,
  351. wxPGProperty* property,
  352. const wxString& text ) const;
  353. //virtual wxPGCellRenderer* GetCellRenderer() const;
  354. virtual void SetControlIntValue( wxPGProperty* property,
  355. wxWindow* ctrl,
  356. int value ) const;
  357. };
  358. #endif
  359. // -----------------------------------------------------------------------
  360. // Editor class registeration macro (mostly for internal use)
  361. #define wxPGRegisterEditorClass(EDITOR) \
  362. if ( wxPGEditor_##EDITOR == NULL ) \
  363. { \
  364. wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
  365. new wxPG##EDITOR##Editor ); \
  366. }
  367. // -----------------------------------------------------------------------
  368. /** @class wxPGEditorDialogAdapter
  369. Derive a class from this to adapt an existing editor dialog or function to
  370. be used when editor button of a property is pushed.
  371. You only need to derive class and implement DoShowDialog() to create and
  372. show the dialog, and finally submit the value returned by the dialog
  373. via SetValue().
  374. @library{wxpropgrid}
  375. @category{propgrid}
  376. */
  377. class WXDLLIMPEXP_PROPGRID wxPGEditorDialogAdapter : public wxObject
  378. {
  379. DECLARE_ABSTRACT_CLASS(wxPGEditorDialogAdapter)
  380. public:
  381. wxPGEditorDialogAdapter()
  382. : wxObject()
  383. {
  384. m_clientData = NULL;
  385. }
  386. virtual ~wxPGEditorDialogAdapter() { }
  387. bool ShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property );
  388. virtual bool DoShowDialog( wxPropertyGrid* propGrid,
  389. wxPGProperty* property ) = 0;
  390. void SetValue( wxVariant value )
  391. {
  392. m_value = value;
  393. }
  394. /**
  395. This method is typically only used if deriving class from existing
  396. adapter with value conversion purposes.
  397. */
  398. wxVariant& GetValue() { return m_value; }
  399. //
  400. // This member is public so scripting language bindings
  401. // wrapper code can access it freely.
  402. void* m_clientData;
  403. private:
  404. wxVariant m_value;
  405. };
  406. // -----------------------------------------------------------------------
  407. /** @class wxPGMultiButton
  408. This class can be used to have multiple buttons in a property editor.
  409. You will need to create a new property editor class, override
  410. CreateControls, and have it return wxPGMultiButton instance in
  411. wxPGWindowList::SetSecondary().
  412. */
  413. class WXDLLIMPEXP_PROPGRID wxPGMultiButton : public wxWindow
  414. {
  415. public:
  416. wxPGMultiButton( wxPropertyGrid* pg, const wxSize& sz );
  417. virtual ~wxPGMultiButton() {}
  418. wxWindow* GetButton( unsigned int i ) { return (wxWindow*) m_buttons[i]; }
  419. const wxWindow* GetButton( unsigned int i ) const
  420. { return (const wxWindow*) m_buttons[i]; }
  421. /** Utility function to be used in event handlers.
  422. */
  423. int GetButtonId( unsigned int i ) const { return GetButton(i)->GetId(); }
  424. /** Returns number of buttons.
  425. */
  426. unsigned int GetCount() const { return (unsigned int) m_buttons.size(); }
  427. void Add( const wxString& label, int id = -2 );
  428. #if wxUSE_BMPBUTTON
  429. void Add( const wxBitmap& bitmap, int id = -2 );
  430. #endif
  431. wxSize GetPrimarySize() const
  432. {
  433. return wxSize(m_fullEditorSize.x - m_buttonsWidth, m_fullEditorSize.y);
  434. }
  435. void Finalize( wxPropertyGrid* propGrid, const wxPoint& pos );
  436. protected:
  437. void DoAddButton( wxWindow* button, const wxSize& sz );
  438. int GenId( int id ) const;
  439. wxArrayPtrVoid m_buttons;
  440. wxSize m_fullEditorSize;
  441. int m_buttonsWidth;
  442. };
  443. // -----------------------------------------------------------------------
  444. #endif // wxUSE_PROPGRID
  445. #endif // _WX_PROPGRID_EDITORS_H_