1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxPropertyGrid editors
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 Base class for custom wxPropertyGrid editors.
16 - Names of built-in property editors are: TextCtrl, Choice,
17 ComboBox, CheckBox, TextCtrlAndButton, and ChoiceAndButton. Additional
18 editors include SpinCtrl and DatePickerCtrl, but using them requires
19 calling wxPropertyGrid::RegisterAdditionalEditors() prior use.
21 - Pointer to built-in editor is available as wxPGEditor_EditorName
22 (eg. wxPGEditor_TextCtrl).
24 - Before you start using new editor you just created, you need to register
25 it using static function
26 wxPropertyGrid::RegisterEditorClass(), with code like this:
28 wxPGEditor* editorPointer = wxPropertyGrid::RegisterEditorClass(new MyEditorClass(), "MyEditor");
30 After that, wxPropertyGrid will take ownership of the given object, but
31 you should still store editorPointer somewhere, so you can pass it to
32 wxPGProperty::SetEditor(), or return it from wxPGEditor::DoGetEditorClass().
37 class wxPGEditor
: public wxObject
45 virtual ~wxPGEditor();
48 Returns pointer to the name of the editor. For example,
49 wxPGEditor_TextCtrl has name "TextCtrl". If you dont' need to access
50 your custom editor by string name, then you do not need to implement
53 virtual wxString
GetName() const;
56 Instantiates editor controls.
59 wxPropertyGrid to which the property belongs (use as parent for control).
62 Property for which this method is called.
65 Position, inside wxPropertyGrid, to create control(s) to.
68 Initial size for control(s).
71 - Primary control shall use id wxPG_SUBID1, and secondary (button) control
72 shall use wxPG_SUBID2.
73 - Implementation shoud connect all necessary events to the
74 wxPropertyGrid::OnCustomEditorEvent(). For example:
76 control->Connect(control->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
77 wxEventHandler(wxPropertyGrid::OnCustomEditorEvent),
80 OnCustomEditorEvent will then forward events, first to
81 wxPGEditor::OnEvent() and then to wxPGProperty::OnEvent().
83 @see wxPropertyGrid::OnCustomEditorEvent(), wxEvtHandler::Connect()
85 virtual wxPGWindowList
CreateControls( wxPropertyGrid
* propgrid
,
86 wxPGProperty
* property
,
88 const wxSize
& size
) const = 0;
90 /** Loads value from property to the control. */
91 virtual void UpdateControl( wxPGProperty
* property
, wxWindow
* ctrl
) const = 0;
94 Draws value for given property.
96 virtual void DrawValue( wxDC
& dc
, const wxRect
& rect
,
97 wxPGProperty
* property
, const wxString
& text
) const;
100 Handles events. Returns @true if value in control was modified
101 (see wxPGProperty::OnEvent() for more information).
103 @remarks wxPropertyGrid will automatically unfocus the editor when
104 wxEVT_COMMAND_TEXT_ENTER is received and when it results in
105 property value being modified. This happens regardless of
106 editor type (ie. behavior is same for any wxTextCtrl and
107 wxComboBox based editor).
109 virtual bool OnEvent( wxPropertyGrid
* propgrid
, wxPGProperty
* property
,
110 wxWindow
* wnd_primary
, wxEvent
& event
) const = 0;
113 Returns value from control, via parameter 'variant'.
114 Usually ends up calling property's StringToValue() or IntToValue().
115 Returns @true if value was different.
117 virtual bool GetValueFromControl( wxVariant
& variant
, wxPGProperty
* property
,
118 wxWindow
* ctrl
) const;
120 /** Sets value in control to unspecified. */
121 virtual void SetValueToUnspecified( wxPGProperty
* property
,
122 wxWindow
* ctrl
) const = 0;
124 /** Sets control's value specifically from string. */
125 virtual void SetControlStringValue( wxPGProperty
* property
,
126 wxWindow
* ctrl
, const wxString
& txt
) const;
128 /** Sets control's value specifically from int (applies to choice etc.). */
129 virtual void SetControlIntValue( wxPGProperty
* property
,
130 wxWindow
* ctrl
, int value
) const;
133 Inserts item to existing control. Index -1 means end of list.
134 Default implementation does nothing. Returns index of item added.
136 virtual int InsertItem( wxWindow
* ctrl
, const wxString
& label
, int index
) const;
139 Deletes item from existing control.
140 Default implementation does nothing.
142 virtual void DeleteItem( wxWindow
* ctrl
, int index
) const;
145 Extra processing when control gains focus.
146 For example, wxTextCtrl based controls should select all text.
148 virtual void OnFocus( wxPGProperty
* property
, wxWindow
* wnd
) const;
151 Returns @true if control itself can contain the custom image.
152 Default implementation returns @false.
154 virtual bool CanContainCustomImage() const;
160 @class wxPGMultiButton
162 This class can be used to have multiple buttons in a property editor.
163 You will need to create a new property editor class, override CreateControls,
164 and have it return wxPGMultiButton instance in wxPGWindowList::SetSecondary().
166 For instance, here we add three buttons to a TextCtrl editor:
169 #include <wx/propgrid/editors.h>
171 class wxSampleMultiButtonEditor : public wxPGTextCtrlEditor
173 DECLARE_DYNAMIC_CLASS(wxSampleMultiButtonEditor)
175 wxSampleMultiButtonEditor() {}
176 virtual ~wxSampleMultiButtonEditor() {}
178 virtual wxString GetName() const { return "SampleMultiButtonEditor"; }
180 virtual wxPGWindowList CreateControls( wxPropertyGrid* propGrid,
181 wxPGProperty* property,
183 const wxSize& sz ) const;
184 virtual bool OnEvent( wxPropertyGrid* propGrid,
185 wxPGProperty* property,
187 wxEvent& event ) const;
190 IMPLEMENT_DYNAMIC_CLASS(wxSampleMultiButtonEditor, wxPGTextCtrlEditor)
192 wxPGWindowList wxSampleMultiButtonEditor::CreateControls( wxPropertyGrid* propGrid,
193 wxPGProperty* property,
195 const wxSize& sz ) const
197 // Create and populate buttons-subwindow
198 wxPGMultiButton* buttons = new wxPGMultiButton( propGrid, sz );
200 // Add two regular buttons
201 buttons->Add( "..." );
203 // Add a bitmap button
204 buttons->Add( wxArtProvider::GetBitmap(wxART_FOLDER) );
206 // Create the 'primary' editor control (textctrl in this case)
207 wxPGWindowList wndList = wxPGTextCtrlEditor::CreateControls
208 ( propGrid, property, pos,
209 buttons->GetPrimarySize() );
211 // Finally, move buttons-subwindow to correct position and make sure
212 // returned wxPGWindowList contains our custom button list.
213 buttons->Finalize(propGrid, pos);
215 wndList.SetSecondary( buttons );
219 bool wxSampleMultiButtonEditor::OnEvent( wxPropertyGrid* propGrid,
220 wxPGProperty* property,
222 wxEvent& event ) const
224 if ( event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
226 wxPGMultiButton* buttons = (wxPGMultiButton*) propGrid->GetEditorControlSecondary();
228 if ( event.GetId() == buttons->GetButtonId(0) )
230 // Do something when first button is pressed
233 if ( event.GetId() == buttons->GetButtonId(1) )
235 // Do something when second button is pressed
238 if ( event.GetId() == buttons->GetButtonId(2) )
240 // Do something when third button is pressed
244 return wxPGTextCtrlEditor::OnEvent(propGrid, property, ctrl, event);
248 Further to use this editor, code like this can be used:
251 // Register editor class - needs only to be called once
252 wxPGEditor* multiButtonEditor = new wxSampleMultiButtonEditor();
253 wxPropertyGrid::RegisterEditorClass( multiButtonEditor );
255 // Insert the property that will have multiple buttons
256 propGrid->Append( new wxLongStringProperty("MultipleButtons", wxPG_LABEL) );
258 // Change property to use editor created in the previous code segment
259 propGrid->SetPropertyEditor( "MultipleButtons", multiButtonEditor );
265 class WXDLLIMPEXP_PROPGRID wxPGMultiButton
: public wxWindow
271 wxPGMultiButton( wxPropertyGrid
* pg
, const wxSize
& sz
);
276 virtual ~wxPGMultiButton() { }
279 Adds new button, with given label.
281 void Add( const wxString
& label
, int id
= -2 );
284 Adds new bitmap button.
286 void Add( const wxBitmap
& bitmap
, int id
= -2 );
289 Call this in CreateControls() of your custom editor class
290 after all buttons have been added.
293 wxPropertyGrid given in CreateControls().
296 wxPoint given in CreateControls().
298 void Finalize( wxPropertyGrid
* propGrid
, const wxPoint
& pos
);
301 Returns pointer to one of the buttons.
303 wxWindow
* GetButton( unsigned int i
);
306 Returns Id of one of the buttons.
307 This is utility function to be used in event handlers.
309 int GetButtonId( unsigned int i
) const;
312 Returns number of buttons.
314 unsigned int GetCount();
317 Returns size of primary editor control, as appropriately
318 reduced by number of buttons present.
320 wxSize
GetPrimarySize() const;