1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxPropertyGrid editors
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
9 // -----------------------------------------------------------------------
14 Base class for custom wxPropertyGrid editors.
17 - Names of built-in property editors are: TextCtrl, Choice,
18 ComboBox, CheckBox, TextCtrlAndButton, and ChoiceAndButton. Additional
19 editors include SpinCtrl and DatePickerCtrl, but using them requires
20 calling wxPropertyGrid::RegisterAdditionalEditors() prior use.
22 - Pointer to built-in editor is available as wxPGEditor_EditorName
23 (eg. wxPGEditor_TextCtrl).
25 - Before you start using new editor you just created, you need to register
26 it using static function
27 wxPropertyGrid::RegisterEditorClass(), with code like this:
29 wxPGEditor* editorPointer = wxPropertyGrid::RegisterEditorClass(new MyEditorClass(), "MyEditor");
31 After that, wxPropertyGrid will take ownership of the given object, but
32 you should still store editorPointer somewhere, so you can pass it to
33 wxPGProperty::SetEditor(), or return it from wxPGEditor::DoGetEditorClass().
38 class wxPGEditor
: public wxObject
50 virtual ~wxPGEditor();
53 Returns pointer to the name of the editor. For example,
54 wxPGEditor_TextCtrl has name "TextCtrl".
56 virtual wxString
GetName() const = 0;
59 Instantiates editor controls.
62 wxPropertyGrid to which the property belongs (use as parent for control).
65 Property for which this method is called.
68 Position, inside wxPropertyGrid, to create control(s) to.
71 Initial size for control(s).
74 - Primary control shall use id wxPG_SUBID1, and secondary (button) control
75 shall use wxPG_SUBID2.
76 - Implementation shoud connect all necessary events to the
77 wxPropertyGrid::OnCustomEditorEvent. For Example:
79 // Relays wxEVT_COMMAND_TEXT_UPDATED events of primary editor
80 // control to the OnEvent.
81 control->Connect(control->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
82 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent),
85 OnCustomEditorEvent will then forward events, first to
86 wxPGEditor::OnEvent() and then to wxPGProperty::OnEvent().
88 virtual wxPGWindowList
CreateControls( wxPropertyGrid
* propgrid
,
89 wxPGProperty
* property
,
91 const wxSize
& size
) const = 0;
93 /** Loads value from property to the control. */
94 virtual void UpdateControl( wxPGProperty
* property
, wxWindow
* ctrl
) const = 0;
97 Draws value for given property.
99 virtual void DrawValue( wxDC
& dc
, const wxRect
& rect
,
100 wxPGProperty
* property
, const wxString
& text
) const;
103 Handles events. Returns @true if value in control was modified
104 (see wxPGProperty::OnEvent() for more information).
106 virtual bool OnEvent( wxPropertyGrid
* propgrid
, wxPGProperty
* property
,
107 wxWindow
* wnd_primary
, wxEvent
& event
) const = 0;
110 Returns value from control, via parameter 'variant'.
111 Usually ends up calling property's StringToValue() or IntToValue().
112 Returns @true if value was different.
114 virtual bool GetValueFromControl( wxVariant
& variant
, wxPGProperty
* property
,
115 wxWindow
* ctrl
) const;
117 /** Sets value in control to unspecified. */
118 virtual void SetValueToUnspecified( wxPGProperty
* property
,
119 wxWindow
* ctrl
) const = 0;
121 /** Sets control's value specifically from string. */
122 virtual void SetControlStringValue( wxPGProperty
* property
,
123 wxWindow
* ctrl
, const wxString
& txt
) const;
125 /** Sets control's value specifically from int (applies to choice etc.). */
126 virtual void SetControlIntValue( wxPGProperty
* property
,
127 wxWindow
* ctrl
, int value
) const;
130 Inserts item to existing control. Index -1 means end of list.
131 Default implementation does nothing. Returns index of item added.
133 virtual int InsertItem( wxWindow
* ctrl
, const wxString
& label
,
137 Deletes item from existing control.
138 Default implementation does nothing.
140 virtual void DeleteItem( wxWindow
* ctrl
, int index
) const;
143 Extra processing when control gains focus. For example, wxTextCtrl
144 based controls should select all text.
146 virtual void OnFocus( wxPGProperty
* property
, wxWindow
* wnd
) const;
149 Returns @true if control itself can contain the custom image. Default
150 implementation returns @false.
152 virtual bool CanContainCustomImage() const;
155 // -----------------------------------------------------------------------
158 @class wxPGMultiButton
160 This class can be used to have multiple buttons in a property editor.
161 You will need to create a new property editor class, override CreateControls,
162 and have it return wxPGMultiButton instance in wxPGWindowList::SetSecondary().
164 For instance, here we add three buttons to a TextCtrl editor:
168 #include <wx/propgrid/editors.h>
170 class wxSampleMultiButtonEditor : public wxPGTextCtrlEditor
172 DECLARE_DYNAMIC_CLASS(wxSampleMultiButtonEditor)
174 wxSampleMultiButtonEditor() {}
175 virtual ~wxSampleMultiButtonEditor() {}
177 virtual wxString GetName() const { return "SampleMultiButtonEditor"; }
179 virtual wxPGWindowList CreateControls( wxPropertyGrid* propGrid,
180 wxPGProperty* property,
182 const wxSize& sz ) const;
183 virtual bool OnEvent( wxPropertyGrid* propGrid,
184 wxPGProperty* property,
186 wxEvent& event ) const;
189 IMPLEMENT_DYNAMIC_CLASS(wxSampleMultiButtonEditor, wxPGTextCtrlEditor)
191 wxPGWindowList wxSampleMultiButtonEditor::CreateControls( wxPropertyGrid* propGrid,
192 wxPGProperty* property,
194 const wxSize& sz ) const
196 // Create and populate buttons-subwindow
197 wxPGMultiButton* buttons = new wxPGMultiButton( propGrid, sz );
199 // Add two regular buttons
200 buttons->Add( "..." );
202 // Add a bitmap button
203 buttons->Add( wxArtProvider::GetBitmap(wxART_FOLDER) );
205 // Create the 'primary' editor control (textctrl in this case)
206 wxPGWindowList wndList = wxPGTextCtrlEditor::CreateControls
207 ( propGrid, property, pos,
208 buttons->GetPrimarySize() );
210 // Finally, move buttons-subwindow to correct position and make sure
211 // returned wxPGWindowList contains our custom button list.
212 buttons->Finalize(propGrid, pos);
214 wndList.SetSecondary( buttons );
218 bool wxSampleMultiButtonEditor::OnEvent( wxPropertyGrid* propGrid,
219 wxPGProperty* property,
221 wxEvent& event ) const
223 if ( event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
225 wxPGMultiButton* buttons = (wxPGMultiButton*) propGrid->GetEditorControlSecondary();
227 if ( event.GetId() == buttons->GetButtonId(0) )
229 // Do something when first button is pressed
232 if ( event.GetId() == buttons->GetButtonId(1) )
234 // Do something when second button is pressed
237 if ( event.GetId() == buttons->GetButtonId(2) )
239 // Do something when third button is pressed
243 return wxPGTextCtrlEditor::OnEvent(propGrid, property, ctrl, event);
248 Further to use this editor, code like this can be used:
252 // Register editor class - needs only to be called once
253 wxPGEditor* multiButtonEditor = new wxSampleMultiButtonEditor();
254 wxPropertyGrid::RegisterEditorClass( multiButtonEditor );
256 // Insert the property that will have multiple buttons
257 propGrid->Append( new wxLongStringProperty("MultipleButtons", wxPG_LABEL) );
259 // Change property to use editor created in the previous code segment
260 propGrid->SetPropertyEditor( "MultipleButtons", multiButtonEditor );
267 class WXDLLIMPEXP_PROPGRID wxPGMultiButton
: public wxWindow
274 wxPGMultiButton( wxPropertyGrid
* pg
, const wxSize
& sz
);
279 virtual ~wxPGMultiButton() { }
282 Adds new button, with given label.
284 void Add( const wxString
& label
, int id
= -2 );
287 Adds new bitmap button.
289 void Add( const wxBitmap
& bitmap
, int id
= -2 );
292 Call this in CreateControls() of your custom editor class
293 after all buttons have been added.
296 wxPropertyGrid given in CreateControls().
299 wxPoint given in CreateControls().
301 void Finalize( wxPropertyGrid
* propGrid
, const wxPoint
& pos
);
304 Returns pointer to one of the buttons.
306 wxWindow
* GetButton( unsigned int i
);
309 Returns Id of one of the buttons. This is utility function to be
310 used in event handlers.
312 int GetButtonId( unsigned int i
) const;
315 Returns number of buttons.
320 Returns size of primary editor control, as appropriately
321 reduced by number of buttons present.
323 wxSize
GetPrimarySize() const;
326 // -----------------------------------------------------------------------