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". If you dont' need to access
55 your custom editor by string name, then you do not need to implement
58 virtual wxString
GetName() const;
61 Instantiates editor controls.
64 wxPropertyGrid to which the property belongs (use as parent for control).
67 Property for which this method is called.
70 Position, inside wxPropertyGrid, to create control(s) to.
73 Initial size for control(s).
76 - Primary control shall use id wxPG_SUBID1, and secondary (button) control
77 shall use wxPG_SUBID2.
78 - Implementation shoud connect all necessary events to the
79 wxPropertyGrid::OnCustomEditorEvent. For Example:
81 // Relays wxEVT_COMMAND_TEXT_UPDATED events of primary editor
82 // control to the OnEvent.
83 control->Connect(control->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
84 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent),
87 OnCustomEditorEvent will then forward events, first to
88 wxPGEditor::OnEvent() and then to wxPGProperty::OnEvent().
90 virtual wxPGWindowList
CreateControls( wxPropertyGrid
* propgrid
,
91 wxPGProperty
* property
,
93 const wxSize
& size
) const = 0;
95 /** Loads value from property to the control. */
96 virtual void UpdateControl( wxPGProperty
* property
, wxWindow
* ctrl
) const = 0;
99 Draws value for given property.
101 virtual void DrawValue( wxDC
& dc
, const wxRect
& rect
,
102 wxPGProperty
* property
, const wxString
& text
) const;
105 Handles events. Returns @true if value in control was modified
106 (see wxPGProperty::OnEvent() for more information).
108 @remarks wxPropertyGrid will automatically unfocus the editor when
109 wxEVT_COMMAND_TEXT_ENTER is received and when it results in
110 property value being modified. This happens regardless of
111 editor type (ie. behavior is same for any wxTextCtrl and
112 wxComboBox based editor).
114 virtual bool OnEvent( wxPropertyGrid
* propgrid
, wxPGProperty
* property
,
115 wxWindow
* wnd_primary
, wxEvent
& event
) const = 0;
118 Returns value from control, via parameter 'variant'.
119 Usually ends up calling property's StringToValue() or IntToValue().
120 Returns @true if value was different.
122 virtual bool GetValueFromControl( wxVariant
& variant
, wxPGProperty
* property
,
123 wxWindow
* ctrl
) const;
125 /** Sets value in control to unspecified. */
126 virtual void SetValueToUnspecified( wxPGProperty
* property
,
127 wxWindow
* ctrl
) const = 0;
129 /** Sets control's value specifically from string. */
130 virtual void SetControlStringValue( wxPGProperty
* property
,
131 wxWindow
* ctrl
, const wxString
& txt
) const;
133 /** Sets control's value specifically from int (applies to choice etc.). */
134 virtual void SetControlIntValue( wxPGProperty
* property
,
135 wxWindow
* ctrl
, int value
) const;
138 Inserts item to existing control. Index -1 means end of list.
139 Default implementation does nothing. Returns index of item added.
141 virtual int InsertItem( wxWindow
* ctrl
, const wxString
& label
,
145 Deletes item from existing control.
146 Default implementation does nothing.
148 virtual void DeleteItem( wxWindow
* ctrl
, int index
) const;
151 Extra processing when control gains focus. For example, wxTextCtrl
152 based controls should select all text.
154 virtual void OnFocus( wxPGProperty
* property
, wxWindow
* wnd
) const;
157 Returns @true if control itself can contain the custom image. Default
158 implementation returns @false.
160 virtual bool CanContainCustomImage() const;
163 // -----------------------------------------------------------------------
166 @class wxPGMultiButton
168 This class can be used to have multiple buttons in a property editor.
169 You will need to create a new property editor class, override CreateControls,
170 and have it return wxPGMultiButton instance in wxPGWindowList::SetSecondary().
172 For instance, here we add three buttons to a TextCtrl editor:
176 #include <wx/propgrid/editors.h>
178 class wxSampleMultiButtonEditor : public wxPGTextCtrlEditor
180 DECLARE_DYNAMIC_CLASS(wxSampleMultiButtonEditor)
182 wxSampleMultiButtonEditor() {}
183 virtual ~wxSampleMultiButtonEditor() {}
185 virtual wxString GetName() const { return "SampleMultiButtonEditor"; }
187 virtual wxPGWindowList CreateControls( wxPropertyGrid* propGrid,
188 wxPGProperty* property,
190 const wxSize& sz ) const;
191 virtual bool OnEvent( wxPropertyGrid* propGrid,
192 wxPGProperty* property,
194 wxEvent& event ) const;
197 IMPLEMENT_DYNAMIC_CLASS(wxSampleMultiButtonEditor, wxPGTextCtrlEditor)
199 wxPGWindowList wxSampleMultiButtonEditor::CreateControls( wxPropertyGrid* propGrid,
200 wxPGProperty* property,
202 const wxSize& sz ) const
204 // Create and populate buttons-subwindow
205 wxPGMultiButton* buttons = new wxPGMultiButton( propGrid, sz );
207 // Add two regular buttons
208 buttons->Add( "..." );
210 // Add a bitmap button
211 buttons->Add( wxArtProvider::GetBitmap(wxART_FOLDER) );
213 // Create the 'primary' editor control (textctrl in this case)
214 wxPGWindowList wndList = wxPGTextCtrlEditor::CreateControls
215 ( propGrid, property, pos,
216 buttons->GetPrimarySize() );
218 // Finally, move buttons-subwindow to correct position and make sure
219 // returned wxPGWindowList contains our custom button list.
220 buttons->Finalize(propGrid, pos);
222 wndList.SetSecondary( buttons );
226 bool wxSampleMultiButtonEditor::OnEvent( wxPropertyGrid* propGrid,
227 wxPGProperty* property,
229 wxEvent& event ) const
231 if ( event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
233 wxPGMultiButton* buttons = (wxPGMultiButton*) propGrid->GetEditorControlSecondary();
235 if ( event.GetId() == buttons->GetButtonId(0) )
237 // Do something when first button is pressed
240 if ( event.GetId() == buttons->GetButtonId(1) )
242 // Do something when second button is pressed
245 if ( event.GetId() == buttons->GetButtonId(2) )
247 // Do something when third button is pressed
251 return wxPGTextCtrlEditor::OnEvent(propGrid, property, ctrl, event);
256 Further to use this editor, code like this can be used:
260 // Register editor class - needs only to be called once
261 wxPGEditor* multiButtonEditor = new wxSampleMultiButtonEditor();
262 wxPropertyGrid::RegisterEditorClass( multiButtonEditor );
264 // Insert the property that will have multiple buttons
265 propGrid->Append( new wxLongStringProperty("MultipleButtons", wxPG_LABEL) );
267 // Change property to use editor created in the previous code segment
268 propGrid->SetPropertyEditor( "MultipleButtons", multiButtonEditor );
275 class WXDLLIMPEXP_PROPGRID wxPGMultiButton
: public wxWindow
282 wxPGMultiButton( wxPropertyGrid
* pg
, const wxSize
& sz
);
287 virtual ~wxPGMultiButton() { }
290 Adds new button, with given label.
292 void Add( const wxString
& label
, int id
= -2 );
295 Adds new bitmap button.
297 void Add( const wxBitmap
& bitmap
, int id
= -2 );
300 Call this in CreateControls() of your custom editor class
301 after all buttons have been added.
304 wxPropertyGrid given in CreateControls().
307 wxPoint given in CreateControls().
309 void Finalize( wxPropertyGrid
* propGrid
, const wxPoint
& pos
);
312 Returns pointer to one of the buttons.
314 wxWindow
* GetButton( unsigned int i
);
317 Returns Id of one of the buttons. This is utility function to be
318 used in event handlers.
320 int GetButtonId( unsigned int i
) const;
323 Returns number of buttons.
325 unsigned int GetCount();
328 Returns size of primary editor control, as appropriately
329 reduced by number of buttons present.
331 wxSize
GetPrimarySize() const;
334 // -----------------------------------------------------------------------