editors.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: editors.h
  3. // Purpose: interface of wxPropertyGrid editors
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxPGEditor
  9. Base class for custom wxPropertyGrid editors.
  10. @remarks
  11. - Names of built-in property editors are: TextCtrl, Choice,
  12. ComboBox, CheckBox, TextCtrlAndButton, and ChoiceAndButton. Additional
  13. editors include SpinCtrl and DatePickerCtrl, but using them requires
  14. calling wxPropertyGrid::RegisterAdditionalEditors() prior use.
  15. - Pointer to built-in editor is available as wxPGEditor_EditorName
  16. (eg. wxPGEditor_TextCtrl).
  17. - Before you start using new editor you just created, you need to register
  18. it using static function
  19. wxPropertyGrid::RegisterEditorClass(), with code like this:
  20. @code
  21. wxPGEditor* editorPointer = wxPropertyGrid::RegisterEditorClass(new MyEditorClass(), "MyEditor");
  22. @endcode
  23. After that, wxPropertyGrid will take ownership of the given object, but
  24. you should still store editorPointer somewhere, so you can pass it to
  25. wxPGProperty::SetEditor(), or return it from wxPGEditor::DoGetEditorClass().
  26. @library{wxpropgrid}
  27. @category{propgrid}
  28. */
  29. class wxPGEditor : public wxObject
  30. {
  31. public:
  32. /** Constructor. */
  33. wxPGEditor();
  34. /** Destructor. */
  35. virtual ~wxPGEditor();
  36. /**
  37. Returns pointer to the name of the editor. For example,
  38. wxPGEditor_TextCtrl has name "TextCtrl". If you dont' need to access
  39. your custom editor by string name, then you do not need to implement
  40. this function.
  41. */
  42. virtual wxString GetName() const;
  43. /**
  44. Instantiates editor controls.
  45. @param propgrid
  46. wxPropertyGrid to which the property belongs (use as parent for control).
  47. @param property
  48. Property for which this method is called.
  49. @param pos
  50. Position, inside wxPropertyGrid, to create control(s) to.
  51. @param size
  52. Initial size for control(s).
  53. @remarks
  54. - Primary control shall use id wxPG_SUBID1, and secondary (button) control
  55. shall use wxPG_SUBID2.
  56. - Unlike in previous version of wxPropertyGrid, it is no longer
  57. necessary to call wxEvtHandler::Connect() for interesting editor
  58. events. Instead, all events from control are now automatically
  59. forwarded to wxPGEditor::OnEvent() and wxPGProperty::OnEvent().
  60. */
  61. virtual wxPGWindowList CreateControls( wxPropertyGrid* propgrid,
  62. wxPGProperty* property,
  63. const wxPoint& pos,
  64. const wxSize& size ) const = 0;
  65. /** Loads value from property to the control. */
  66. virtual void UpdateControl( wxPGProperty* property, wxWindow* ctrl ) const = 0;
  67. /**
  68. Draws value for given property.
  69. */
  70. virtual void DrawValue( wxDC& dc, const wxRect& rect,
  71. wxPGProperty* property, const wxString& text ) const;
  72. /**
  73. Handles events. Returns @true if value in control was modified
  74. (see wxPGProperty::OnEvent() for more information).
  75. @remarks wxPropertyGrid will automatically unfocus the editor when
  76. @c wxEVT_TEXT_ENTER is received and when it results in
  77. property value being modified. This happens regardless of
  78. editor type (ie. behaviour is same for any wxTextCtrl and
  79. wxComboBox based editor).
  80. */
  81. virtual bool OnEvent( wxPropertyGrid* propgrid, wxPGProperty* property,
  82. wxWindow* wnd_primary, wxEvent& event ) const = 0;
  83. /**
  84. Returns value from control, via parameter 'variant'.
  85. Usually ends up calling property's StringToValue() or IntToValue().
  86. Returns @true if value was different.
  87. */
  88. virtual bool GetValueFromControl( wxVariant& variant, wxPGProperty* property,
  89. wxWindow* ctrl ) const;
  90. /** Sets value in control to unspecified. */
  91. virtual void SetValueToUnspecified( wxPGProperty* property,
  92. wxWindow* ctrl ) const = 0;
  93. /**
  94. Called by property grid to set new appearance for the control.
  95. Default implementation sets foreground colour, background colour,
  96. font, plus text for wxTextCtrl and wxComboCtrl.
  97. The parameter @a appearance represents the new appearance to be applied.
  98. The parameter @a oldAppearance is the previously applied appearance.
  99. Used to detect which control attributes need to be changed (e.g. so we only
  100. change background colour if really needed).
  101. Finally, the parameter @a unspecified if @true tells this function that
  102. the new appearance represents an unspecified property value.
  103. */
  104. virtual void SetControlAppearance( wxPropertyGrid* pg,
  105. wxPGProperty* property,
  106. wxWindow* ctrl,
  107. const wxPGCell& appearance,
  108. const wxPGCell& oldAppearance,
  109. bool unspecified ) const;
  110. /** Sets control's value specifically from string. */
  111. virtual void SetControlStringValue( wxPGProperty* property,
  112. wxWindow* ctrl, const wxString& txt ) const;
  113. /** Sets control's value specifically from int (applies to choice etc.). */
  114. virtual void SetControlIntValue( wxPGProperty* property,
  115. wxWindow* ctrl, int value ) const;
  116. /**
  117. Inserts item to existing control. Index -1 means end of list.
  118. Default implementation does nothing. Returns index of item added.
  119. */
  120. virtual int InsertItem( wxWindow* ctrl, const wxString& label, int index ) const;
  121. /**
  122. Deletes item from existing control.
  123. Default implementation does nothing.
  124. */
  125. virtual void DeleteItem( wxWindow* ctrl, int index ) const;
  126. /**
  127. Extra processing when control gains focus.
  128. For example, wxTextCtrl based controls should select all text.
  129. */
  130. virtual void OnFocus( wxPGProperty* property, wxWindow* wnd ) const;
  131. /**
  132. Returns @true if control itself can contain the custom image.
  133. Default implementation returns @false.
  134. */
  135. virtual bool CanContainCustomImage() const;
  136. };
  137. /**
  138. @class wxPGMultiButton
  139. This class can be used to have multiple buttons in a property editor.
  140. You will need to create a new property editor class, override CreateControls,
  141. and have it return wxPGMultiButton instance in wxPGWindowList::SetSecondary().
  142. For instance, here we add three buttons to a TextCtrl editor:
  143. @code
  144. #include <wx/propgrid/editors.h>
  145. class wxSampleMultiButtonEditor : public wxPGTextCtrlEditor
  146. {
  147. wxDECLARE_DYNAMIC_CLASS(wxSampleMultiButtonEditor);
  148. public:
  149. wxSampleMultiButtonEditor() {}
  150. virtual ~wxSampleMultiButtonEditor() {}
  151. virtual wxString GetName() const { return "SampleMultiButtonEditor"; }
  152. virtual wxPGWindowList CreateControls( wxPropertyGrid* propGrid,
  153. wxPGProperty* property,
  154. const wxPoint& pos,
  155. const wxSize& sz ) const;
  156. virtual bool OnEvent( wxPropertyGrid* propGrid,
  157. wxPGProperty* property,
  158. wxWindow* ctrl,
  159. wxEvent& event ) const;
  160. };
  161. wxIMPLEMENT_DYNAMIC_CLASS(wxSampleMultiButtonEditor, wxPGTextCtrlEditor);
  162. wxPGWindowList wxSampleMultiButtonEditor::CreateControls( wxPropertyGrid* propGrid,
  163. wxPGProperty* property,
  164. const wxPoint& pos,
  165. const wxSize& sz ) const
  166. {
  167. // Create and populate buttons-subwindow
  168. wxPGMultiButton* buttons = new wxPGMultiButton( propGrid, sz );
  169. // Add two regular buttons
  170. buttons->Add( "..." );
  171. buttons->Add( "A" );
  172. // Add a bitmap button
  173. buttons->Add( wxArtProvider::GetBitmap(wxART_FOLDER) );
  174. // Create the 'primary' editor control (textctrl in this case)
  175. wxPGWindowList wndList = wxPGTextCtrlEditor::CreateControls
  176. ( propGrid, property, pos,
  177. buttons->GetPrimarySize() );
  178. // Finally, move buttons-subwindow to correct position and make sure
  179. // returned wxPGWindowList contains our custom button list.
  180. buttons->Finalize(propGrid, pos);
  181. wndList.SetSecondary( buttons );
  182. return wndList;
  183. }
  184. bool wxSampleMultiButtonEditor::OnEvent( wxPropertyGrid* propGrid,
  185. wxPGProperty* property,
  186. wxWindow* ctrl,
  187. wxEvent& event ) const
  188. {
  189. if ( event.GetEventType() == wxEVT_BUTTON )
  190. {
  191. wxPGMultiButton* buttons = (wxPGMultiButton*) propGrid->GetEditorControlSecondary();
  192. if ( event.GetId() == buttons->GetButtonId(0) )
  193. {
  194. // Do something when the first button is pressed
  195. // Return true if the action modified the value in editor.
  196. ...
  197. }
  198. if ( event.GetId() == buttons->GetButtonId(1) )
  199. {
  200. // Do something when the second button is pressed
  201. ...
  202. }
  203. if ( event.GetId() == buttons->GetButtonId(2) )
  204. {
  205. // Do something when the third button is pressed
  206. ...
  207. }
  208. }
  209. return wxPGTextCtrlEditor::OnEvent(propGrid, property, ctrl, event);
  210. }
  211. @endcode
  212. Further to use this editor, code like this can be used:
  213. @code
  214. // Register editor class - needs only to be called once
  215. wxPGEditor* multiButtonEditor = new wxSampleMultiButtonEditor();
  216. wxPropertyGrid::RegisterEditorClass( multiButtonEditor );
  217. // Insert the property that will have multiple buttons
  218. propGrid->Append( new wxLongStringProperty("MultipleButtons", wxPG_LABEL) );
  219. // Change property to use editor created in the previous code segment
  220. propGrid->SetPropertyEditor( "MultipleButtons", multiButtonEditor );
  221. @endcode
  222. @library{wxpropgrid}
  223. @category{propgrid}
  224. */
  225. class WXDLLIMPEXP_PROPGRID wxPGMultiButton : public wxWindow
  226. {
  227. public:
  228. /**
  229. Constructor.
  230. */
  231. wxPGMultiButton( wxPropertyGrid* pg, const wxSize& sz );
  232. /**
  233. Destructor.
  234. */
  235. virtual ~wxPGMultiButton() { }
  236. /**
  237. Adds new button, with given label.
  238. */
  239. void Add( const wxString& label, int id = -2 );
  240. /**
  241. Adds new bitmap button.
  242. */
  243. void Add( const wxBitmap& bitmap, int id = -2 );
  244. /**
  245. Call this in CreateControls() of your custom editor class
  246. after all buttons have been added.
  247. @param propGrid
  248. wxPropertyGrid given in CreateControls().
  249. @param pos
  250. wxPoint given in CreateControls().
  251. */
  252. void Finalize( wxPropertyGrid* propGrid, const wxPoint& pos );
  253. /**
  254. Returns pointer to one of the buttons.
  255. */
  256. wxWindow* GetButton( unsigned int i );
  257. /**
  258. Returns Id of one of the buttons.
  259. This is utility function to be used in event handlers.
  260. */
  261. int GetButtonId( unsigned int i ) const;
  262. /**
  263. Returns number of buttons.
  264. */
  265. unsigned int GetCount();
  266. /**
  267. Returns size of primary editor control, as appropriately
  268. reduced by number of buttons present.
  269. */
  270. wxSize GetPrimarySize() const;
  271. };