1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxPropertyGrid editors
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
9 // -----------------------------------------------------------------------
13 Base class for custom wxPropertyGrid editors.
16 - Names of builtin property editors are: TextCtrl, Choice,
17 ComboBox, CheckBox, TextCtrlAndButton, and ChoiceAndButton. Additional editors
18 include SpinCtrl and DatePickerCtrl, but using them requires calling
19 wxPropertyGrid::RegisterAdditionalEditors() prior use.
21 - Pointer to builtin editor is available as wxPGEditor_EditorName
22 (eg. wxPGEditor_TextCtrl).
24 - To add new editor you need to register it first using static function
25 wxPropertyGrid::RegisterEditorClass(), with code like this:
27 wxPGEditor* editorPointer = wxPropertyGrid::RegisterEditorClass(new MyEditorClass(), "MyEditor");
29 After that, wxPropertyGrid will take ownership of the given object, but
30 you should still store editorPointer somewhere, so you can pass it to
31 wxPGProperty::SetEditor(), or return it from wxPGEditor::DoGetEditorClass().
36 class wxPGEditor
: public wxObject
48 virtual ~wxPGEditor();
50 /** Returns pointer to the name of the editor. For example, wxPG_EDITOR(TextCtrl)
51 has name "TextCtrl". This method is autogenerated for custom editors.
53 virtual wxString
GetName() const = 0;
55 /** Instantiates editor controls.
57 wxPropertyGrid to which the property belongs (use as parent for control).
59 Property for which this method is called.
61 Position, inside wxPropertyGrid, to create control(s) to.
63 Initial size for control(s).
66 - Primary control shall use id wxPG_SUBID1, and secondary (button) control
67 shall use wxPG_SUBID2.
68 - Implementation shoud use connect all necessary events to the
69 wxPropertyGrid::OnCustomEditorEvent. For Example:
71 // Relays wxEVT_COMMAND_TEXT_UPDATED events of primary editor
72 // control to the OnEvent.
73 // NOTE: This event in particular is actually automatically conveyed, but
74 // it is just used as an example.
75 propgrid->Connect( wxPG_SUBID1, wxEVT_COMMAND_TEXT_UPDATED,
76 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent) );
78 OnCustomEditorEvent will then forward events, first to wxPGEditor::OnEvent and then to wxPGProperty::OnEvent.
81 virtual wxPGWindowList
CreateControls( wxPropertyGrid
* propgrid
, wxPGProperty
* property
,
82 const wxPoint
& pos
, const wxSize
& size
) const = 0;
84 /** Loads value from property to the control. */
85 virtual void UpdateControl( wxPGProperty
* property
, wxWindow
* ctrl
) const = 0;
87 /** Draws value for given property.
89 virtual void DrawValue( wxDC
& dc
, const wxRect
& rect
, wxPGProperty
* property
, const wxString
& text
) const;
91 /** Handles events. Returns true if value in control was modified
92 (see wxPGProperty::OnEvent for more information).
94 virtual bool OnEvent( wxPropertyGrid
* propgrid
, wxPGProperty
* property
,
95 wxWindow
* wnd_primary
, wxEvent
& event
) const = 0;
97 /** Returns value from control, via parameter 'variant'.
98 Usually ends up calling property's StringToValue or IntToValue.
99 Returns true if value was different.
101 virtual bool GetValueFromControl( wxVariant
& variant
, wxPGProperty
* property
, wxWindow
* ctrl
) const;
103 /** Sets value in control to unspecified. */
104 virtual void SetValueToUnspecified( wxPGProperty
* property
, wxWindow
* ctrl
) const = 0;
106 /** Sets control's value specifically from string. */
107 virtual void SetControlStringValue( wxPGProperty
* property
, wxWindow
* ctrl
, const wxString
& txt
) const;
109 /** Sets control's value specifically from int (applies to choice etc.). */
110 virtual void SetControlIntValue( wxPGProperty
* property
, wxWindow
* ctrl
, int value
) const;
112 /** Inserts item to existing control. Index -1 means appending.
113 Default implementation does nothing. Returns index of item added.
115 virtual int InsertItem( wxWindow
* ctrl
, const wxString
& label
, int index
) const;
117 /** Deletes item from existing control.
118 Default implementation does nothing.
120 virtual void DeleteItem( wxWindow
* ctrl
, int index
) const;
122 /** Extra processing when control gains focus. For example, wxTextCtrl
123 based controls should select all text.
125 virtual void OnFocus( wxPGProperty
* property
, wxWindow
* wnd
) const;
127 /** Returns true if control itself can contain the custom image. Default is
130 virtual bool CanContainCustomImage() const;
133 // This member is public so scripting language bindings
134 // wrapper code can access it freely.
138 // -----------------------------------------------------------------------
140 /** @class wxPGMultiButton
142 This class can be used to have multiple buttons in a property editor.
143 You will need to create a new property editor class, override CreateControls,
144 and have it return wxPGMultiButton instance in wxPGWindowList::SetSecondary().
145 For instance, here we add three buttons to a textctrl editor:
149 #include <wx/propgrid/editors.h>
151 class wxMultiButtonTextCtrlEditor : public wxPGTextCtrlEditor
153 WX_PG_DECLARE_EDITOR_CLASS(wxMultiButtonTextCtrlEditor)
155 wxMultiButtonTextCtrlEditor() {}
156 virtual ~wxMultiButtonTextCtrlEditor() {}
158 wxPG_DECLARE_CREATECONTROLS
159 virtual bool OnEvent( wxPropertyGrid* propGrid,
160 wxPGProperty* property,
162 wxEvent& event ) const;
166 WX_PG_IMPLEMENT_EDITOR_CLASS(MultiButtonTextCtrlEditor, wxMultiButtonTextCtrlEditor,
169 wxPGWindowList wxMultiButtonTextCtrlEditor::CreateControls( wxPropertyGrid* propGrid,
170 wxPGProperty* property,
172 const wxSize& sz ) const
174 // Create and populate buttons-subwindow
175 wxPGMultiButton* buttons = new wxPGMultiButton( propGrid, sz );
177 // Add two regular buttons
178 buttons->Add( "..." );
180 // Add a bitmap button
181 buttons->Add( wxArtProvider::GetBitmap(wxART_FOLDER) );
183 // Create the 'primary' editor control (textctrl in this case)
184 wxPGWindowList wndList = wxPGTextCtrlEditor::CreateControls
185 ( propGrid, property, pos, buttons->GetPrimarySize() );
187 // Finally, move buttons-subwindow to correct position and make sure
188 // returned wxPGWindowList contains our custom button list.
189 buttons->FinalizePosition(pos);
191 wndList.SetSecondary( buttons );
195 bool wxMultiButtonTextCtrlEditor::OnEvent( wxPropertyGrid* propGrid,
196 wxPGProperty* property,
198 wxEvent& event ) const
200 if ( event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
202 wxPGMultiButton* buttons = (wxPGMultiButton*) propGrid->GetEditorControlSecondary();
204 if ( event.GetId() == buttons->GetButtonId(0) )
206 // Do something when first button is pressed
209 if ( event.GetId() == buttons->GetButtonId(1) )
211 // Do something when first button is pressed
214 if ( event.GetId() == buttons->GetButtonId(2) )
216 // Do something when second button is pressed
220 return wxPGTextCtrlEditor::OnEvent(propGrid, property, ctrl, event);
225 Further to use this editor, code like this can be used:
229 // Register editor class - needs only to be called once
230 wxPGRegisterEditorClass( MultiButtonTextCtrlEditor );
232 // Insert the property that will have multiple buttons
233 propGrid->Append( new wxLongStringProperty("MultipleButtons", wxPG_LABEL) );
235 // Change property to use editor created in the previous code segment
236 propGrid->SetPropertyEditor( "MultipleButtons", wxPG_EDITOR(MultiButtonTextCtrlEditor) );
243 class WXDLLIMPEXP_PROPGRID wxPGMultiButton
: public wxWindow
247 wxPGMultiButton( wxPropertyGrid
* pg
, const wxSize
& sz
);
249 virtual ~wxPGMultiButton() { }
251 wxWindow
* GetButton( unsigned int i
) { return (wxWindow
*) m_buttons
[i
]; }
252 const wxWindow
* GetButton( unsigned int i
) const { return (const wxWindow
*) m_buttons
[i
]; }
254 /** Utility function to be used in event handlers.
256 int GetButtonId( unsigned int i
) const { return GetButton(i
)->GetId(); }
258 /** Returns number of buttons.
260 int GetCount() const { return m_buttons
.Count(); }
262 void Add( const wxString
& label
, int id
= -2 );
263 void Add( const wxBitmap
& bitmap
, int id
= -2 );
265 wxSize
GetPrimarySize() const
267 return wxSize(m_fullEditorSize
.x
- m_buttonsWidth
, m_fullEditorSize
.y
);
270 void FinalizePosition( const wxPoint
& pos
)
272 Move( pos
.x
+ m_fullEditorSize
.x
- m_buttonsWidth
, pos
.y
);
276 // -----------------------------------------------------------------------
278 #endif // _WX_PROPGRID_EDITORS_H_