]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/propgrid.h
Use UTF16 for text data object on Mac. Fixes #10902
[wxWidgets.git] / docs / doxygen / overviews / propgrid.h
CommitLineData
1c4293cb
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: propgrid.h
3// Purpose: topic overview
4// Author: wxWidgets team
de003797 5// RCS-ID: $Id$
1c4293cb
VZ
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9/**
10
11@page overview_propgrid wxPropertyGrid Overview
12
13Key Classes:
14@li wxPGProperty
15@li wxPropertyGrid
16@li wxPropertyGridEvent
17@li wxPropertyGridManager
18@li wxPropertyGridPage
19
bba3f9b5
JS
20 wxPropertyGrid is a specialized grid for editing properties - in other
21words name = value pairs. List of ready-to-use property classes include
22strings, numbers, flag sets, fonts, colours and many others. It is possible,
23for example, to categorize properties, set up a complete tree-hierarchy,
24add more than two columns, and set arbitrary per-property attributes.
1c4293cb 25
8622887c
JS
26 As this version of wxPropertyGrid has some backward-incompatible changes
27from version 1.4, everybody who need to maintain custom property classes
28should carefully read final section in @ref propgrid_compat.
29
216214f8
JS
30@li @ref propgrid_basics
31@li @ref propgrid_categories
32@li @ref propgrid_parentprops
33@li @ref propgrid_enumandflags
34@li @ref propgrid_advprops
bba3f9b5 35@li @ref propgrid_processingvalues
216214f8 36@li @ref propgrid_iterating
216214f8
JS
37@li @ref propgrid_events
38@li @ref propgrid_validating
39@li @ref propgrid_populating
40@li @ref propgrid_cellrender
41@li @ref propgrid_customizing
42@li @ref propgrid_usage2
43@li @ref propgrid_subclassing
44@li @ref propgrid_misc
45@li @ref propgrid_proplist
8622887c 46@li @ref propgrid_compat
216214f8
JS
47
48@section propgrid_basics Creating and Populating wxPropertyGrid
1c4293cb
VZ
49
50As seen here, wxPropertyGrid is constructed in the same way as
51other wxWidgets controls:
52
53@code
54
55// Necessary header file
56#include <wx/propgrid/propgrid.h>
57
58...
59
60 // Assumes code is in frame/dialog constructor
61
62 // Construct wxPropertyGrid control
63 wxPropertyGrid* pg = new wxPropertyGrid(
64 this, // parent
65 PGID, // id
66 wxDefaultPosition, // position
67 wxDefaultSize, // size
68 // Here are just some of the supported window styles
69 wxPG_AUTO_SORT | // Automatic sorting after items added
70 wxPG_SPLITTER_AUTO_CENTER | // Automatically center splitter until user manually adjusts it
71 // Default style
72 wxPG_DEFAULT_STYLE );
73
74 // Window style flags are at premium, so some less often needed ones are
75 // available as extra window styles (wxPG_EX_xxx) which must be set using
76 // SetExtraStyle member function. wxPG_EX_HELP_AS_TOOLTIPS, for instance,
bba3f9b5 77 // allows displaying help strings as tool tips.
1c4293cb
VZ
78 pg->SetExtraStyle( wxPG_EX_HELP_AS_TOOLTIPS );
79
80@endcode
81
b55018ec 82 (for complete list of new window styles, see @ref propgrid_window_styles)
1c4293cb
VZ
83
84 wxPropertyGrid is usually populated with lines like this:
85
86@code
87 pg->Append( new wxStringProperty(wxT("Label"),wxT("Name"),wxT("Initial Value")) );
88@endcode
89
90Naturally, wxStringProperty is a property class. Only the first function argument (label)
91is mandatory. Second one, name, defaults to label and, third, the initial value, to
92default value. If constant wxPG_LABEL is used as the name argument, then the label is
d665918b
JS
93automatically used as a name as well (this is more efficient than manually defining both
94as the same). Use of empty name is discouraged and will sometimes result in run-time error.
95Note that all property class constructors have quite similar constructor argument list.
1c4293cb
VZ
96
97To demonstrate other common property classes, here's another code snippet:
98
99@code
100
101 // Add int property
102 pg->Append( new wxIntProperty(wxT("IntProperty"), wxPG_LABEL, 12345678) );
103
104 // Add float property (value type is actually double)
105 pg->Append( new wxFloatProperty(wxT("FloatProperty"), wxPG_LABEL, 12345.678) );
106
107 // Add a bool property
108 pg->Append( new wxBoolProperty(wxT("BoolProperty"), wxPG_LABEL, false) );
109
110 // A string property that can be edited in a separate editor dialog.
111 pg->Append( new wxLongStringProperty(wxT("LongStringProperty"),
112 wxPG_LABEL,
113 wxT("This is much longer string than the ")
114 wxT("first one. Edit it by clicking the button.")));
115
116 // String editor with dir selector button.
117 pg->Append( new wxDirProperty(wxT("DirProperty"), wxPG_LABEL, ::wxGetUserHome()) );
118
119 // wxArrayStringProperty embeds a wxArrayString.
120 pg->Append( new wxArrayStringProperty(wxT("Label of ArrayStringProperty"),
121 wxT("NameOfArrayStringProp")));
122
123 // A file selector property.
124 pg->Append( new wxFileProperty(wxT("FileProperty"), wxPG_LABEL, wxEmptyString) );
125
bba3f9b5 126 // Extra: set wild card for file property (format same as in wxFileDialog).
1c4293cb
VZ
127 pg->SetPropertyAttribute( wxT("FileProperty"),
128 wxPG_FILE_WILDCARD,
129 wxT("All files (*.*)|*.*") );
130
131@endcode
132
bba3f9b5
JS
133 Operations on properties are usually done by directly calling wxPGProperty's
134or wxPropertyGridInterface's member functions. wxPropertyGridInterface is an
135abstract base class for property containers such as wxPropertyGrid,
136wxPropertyGridManager, and wxPropertyGridPage. Note however that wxPGProperty's
137member functions generally do not refresh the grid.
1c4293cb 138
bba3f9b5
JS
139 wxPropertyGridInterface's property operation member functions , such as
140SetPropertyValue() and DisableProperty(), all accept a special wxPGPropArg id
141argument, using which you can refer to properties either by their pointer
142(for performance) or by their name (for convenience). For instance:
1c4293cb
VZ
143
144@code
bba3f9b5
JS
145 // Add a file selector property.
146 wxPGPropety* prop = pg->Append( new wxFileProperty(wxT("FileProperty"),
147 wxPG_LABEL,
148 wxEmptyString) );
1c4293cb 149
bba3f9b5 150 // Valid: Set wild card by name
1c4293cb
VZ
151 pg->SetPropertyAttribute( wxT("FileProperty"),
152 wxPG_FILE_WILDCARD,
153 wxT("All files (*.*)|*.*") );
154
bba3f9b5
JS
155 // Also Valid: Set wild card by property pointer
156 pg->SetPropertyAttribute( prop,
1c4293cb
VZ
157 wxPG_FILE_WILDCARD,
158 wxT("All files (*.*)|*.*") );
159@endcode
160
bba3f9b5
JS
161 Using pointer is faster, since it doesn't require hash map lookup. Anyway,
162you can always get property pointer (wxPGProperty*) as return value from Append()
163or Insert(), or by calling wxPropertyGridInterface::GetPropertyByName() or
164just plain GetProperty().
1c4293cb 165
216214f8 166@section propgrid_categories Categories
1c4293cb 167
bba3f9b5 168 wxPropertyGrid has a hierarchic property storage and display model, which
1c4293cb
VZ
169allows property categories to hold child properties and even other
170categories. Other than that, from the programmer's point of view, categories
171can be treated exactly the same as "other" properties. For example, despite
bba3f9b5
JS
172its name, GetPropertyByName() also returns a category by name. Note however
173that sometimes the label of a property category may be referred as caption
174(for example, there is wxPropertyGrid::SetCaptionTextColour() method
175that sets text colour of property category labels).
1c4293cb
VZ
176
177 When category is added at the top (i.e. root) level of the hierarchy,
178it becomes a *current category*. This means that all other (non-category)
bba3f9b5
JS
179properties after it are automatically appended to it. You may add
180properties to specific categories by using wxPropertyGridInterface::Insert
181or wxPropertyGridInterface::AppendIn.
1c4293cb
VZ
182
183 Category code sample:
184
185@code
186
187 // One way to add category (similar to how other properties are added)
188 pg->Append( new wxPropertyCategory(wxT("Main")) );
189
190 // All these are added to "Main" category
191 pg->Append( new wxStringProperty(wxT("Name")) );
192 pg->Append( new wxIntProperty(wxT("Age"),wxPG_LABEL,25) );
193 pg->Append( new wxIntProperty(wxT("Height"),wxPG_LABEL,180) );
194 pg->Append( new wxIntProperty(wxT("Weight")) );
195
196 // Another one
bba3f9b5 197 pg->Append( new wxPropertyCategory(wxT("Attributes")) );
1c4293cb
VZ
198
199 // All these are added to "Attributes" category
200 pg->Append( new wxIntProperty(wxT("Intelligence")) );
201 pg->Append( new wxIntProperty(wxT("Agility")) );
202 pg->Append( new wxIntProperty(wxT("Strength")) );
203
204@endcode
205
206
216214f8 207@section propgrid_parentprops Tree-like Property Structure
1c4293cb 208
bba3f9b5 209 Basically any property can have children. There are few limitations, however.
1c4293cb
VZ
210
211@remarks
bba3f9b5
JS
212- Names of properties with non-category, non-root parents are not stored in global
213 hash map. Instead, they can be accessed with strings like "Parent.Child".
214 For instance, in the sample below, child property named "Max. Speed (mph)"
215 can be accessed by global name "Car.Speeds.Max Speed (mph)".
216- If you want to property's value to be a string composed of the child property values,
217 you must use wxStringProperty as parent and use magic string "<composed>" as its
218 value.
1c4293cb
VZ
219- Events (eg. change of value) that occur in parent do not propagate to children. Events
220 that occur in children will propagate to parents, but only if they are wxStringProperties
221 with "<composed>" value.
1c4293cb
VZ
222
223Sample:
224
225@code
bba3f9b5
JS
226 wxPGProperty* carProp = pg->Append(new wxStringProperty(wxT("Car"),
227 wxPG_LABEL,
228 wxT("<composed>")));
229
230 pg->AppendIn(carProp, new wxStringProperty(wxT("Model"),
231 wxPG_LABEL,
232 wxT("Lamborghini Diablo SV")));
1c4293cb 233
bba3f9b5 234 pg->AppendIn(carProp, new wxIntProperty(wxT("Engine Size (cc)"),
1c4293cb 235 wxPG_LABEL,
bba3f9b5 236 5707) );
1c4293cb 237
bba3f9b5
JS
238 wxPGProperty* speedsProp = pg->AppendIn(carProp,
239 new wxStringProperty(wxT("Speeds"),
240 wxPG_LABEL,
241 wxT("<composed>")));
1c4293cb 242
bba3f9b5
JS
243 pg->AppendIn( speedsProp, new wxIntProperty(wxT("Max. Speed (mph)"),
244 wxPG_LABEL,290) );
245 pg->AppendIn( speedsProp, new wxFloatProperty(wxT("0-100 mph (sec)"),
246 wxPG_LABEL,3.9) );
247 pg->AppendIn( speedsProp, new wxFloatProperty(wxT("1/4 mile (sec)"),
248 wxPG_LABEL,8.6) );
1c4293cb 249
bba3f9b5 250 // This is how child property can be referred to by name
1c4293cb
VZ
251 pg->SetPropertyValue( wxT("Car.Speeds.Max. Speed (mph)"), 300 );
252
bba3f9b5
JS
253 pg->AppendIn(carProp, new wxIntProperty(wxT("Price ($)"),
254 wxPG_LABEL,
255 300000) );
256
257 // Displayed value of "Car" property is now very close to this:
258 // "Lamborghini Diablo SV; 5707 [300; 3.9; 8.6] 300000"
1c4293cb
VZ
259
260@endcode
261
216214f8 262@section propgrid_enumandflags wxEnumProperty and wxFlagsProperty
1c4293cb
VZ
263
264 wxEnumProperty is used when you want property's (integer or string) value
265to be selected from a popup list of choices.
266
bba3f9b5
JS
267 Creating wxEnumProperty is slightly more complex than those described
268earlier. You have to provide list of constant labels, and optionally relevant
269values (if label indexes are not sufficient).
1c4293cb
VZ
270
271@remarks
272
939d9364
JS
273- Value wxPG_INVALID_VALUE (equals INT_MAX) is not allowed as list
274 item value.
1c4293cb
VZ
275
276A very simple example:
277
278@code
279
280 //
281 // Using wxArrayString
282 //
283 wxArrayString arrDiet;
284 arr.Add(wxT("Herbivore"));
285 arr.Add(wxT("Carnivore"));
286 arr.Add(wxT("Omnivore"));
287
288 pg->Append( new wxEnumProperty(wxT("Diet"),
289 wxPG_LABEL,
290 arrDiet) );
291
1c4293cb
VZ
292 //
293 // Using wxChar* array
294 //
295 const wxChar* arrayDiet[] =
296 { wxT("Herbivore"), wxT("Carnivore"), wxT("Omnivore"), NULL };
297
298 pg->Append( new wxEnumProperty(wxT("Diet"),
299 wxPG_LABEL,
300 arrayDiet) );
301
1c4293cb
VZ
302@endcode
303
304Here's extended example using values as well:
305
306@code
307
308 //
309 // Using wxArrayString and wxArrayInt
310 //
311 wxArrayString arrDiet;
312 arr.Add(wxT("Herbivore"));
313 arr.Add(wxT("Carnivore"));
314 arr.Add(wxT("Omnivore"));
315
316 wxArrayInt arrIds;
317 arrIds.Add(40);
318 arrIds.Add(45);
319 arrIds.Add(50);
320
321 // Note that the initial value (the last argument) is the actual value,
322 // not index or anything like that. Thus, our value selects "Omnivore".
323 pg->Append( new wxEnumProperty(wxT("Diet"),
324 wxPG_LABEL,
325 arrDiet,
326 arrIds,
327 50));
328
1c4293cb
VZ
329@endcode
330
331 wxPGChoices is a class where wxEnumProperty, and other properties which
bba3f9b5
JS
332 require storage for list of items, actually stores strings and values. It is
333 used to facilitate reference counting, and therefore recommended way of
1c4293cb
VZ
334 adding items when multiple properties share the same set.
335
336 You can use wxPGChoices directly as well, filling it and then passing it
bba3f9b5 337 to the constructor. In fact, if you wish to display bitmaps next to labels,
1c4293cb
VZ
338 your best choice is to use this approach.
339
340@code
341
342 wxPGChoices chs;
bba3f9b5
JS
343 chs.Add(wxT("Herbivore"), 40);
344 chs.Add(wxT("Carnivore"), 45);
345 chs.Add(wxT("Omnivore"), 50);
1c4293cb
VZ
346
347 // Let's add an item with bitmap, too
348 chs.Add(wxT("None of the above"), wxBitmap(), 60);
349
939d9364 350 pg->Append( new wxEnumProperty(wxT("Primary Diet"),
1c4293cb
VZ
351 wxPG_LABEL,
352 chs) );
353
354 // Add same choices to another property as well - this is efficient due
355 // to reference counting
939d9364 356 pg->Append( new wxEnumProperty(wxT("Secondary Diet"),
1c4293cb
VZ
357 wxPG_LABEL,
358 chs) );
1c4293cb
VZ
359@endcode
360
d5774494
JS
361You can later change choices of property by using wxPGProperty::AddChoice(),
362wxPGProperty::InsertChoice(), wxPGProperty::DeleteChoice(), and
363wxPGProperty::SetChoices().
1c4293cb 364
bba3f9b5
JS
365<b>wxEditEnumProperty</b> works exactly like wxEnumProperty, except
366is uses non-read-only combo box as default editor, and value is stored as
1c4293cb
VZ
367string when it is not any of the choices.
368
939d9364 369wxFlagsProperty has similar construction:
1c4293cb
VZ
370
371@code
372
373 const wxChar* flags_prop_labels[] = { wxT("wxICONIZE"),
374 wxT("wxCAPTION"), wxT("wxMINIMIZE_BOX"), wxT("wxMAXIMIZE_BOX"), NULL };
375
376 // this value array would be optional if values matched string indexes
377 long flags_prop_values[] = { wxICONIZE, wxCAPTION, wxMINIMIZE_BOX,
378 wxMAXIMIZE_BOX };
379
380 pg->Append( new wxFlagsProperty(wxT("Window Style"),
381 wxPG_LABEL,
382 flags_prop_labels,
383 flags_prop_values,
384 wxDEFAULT_FRAME_STYLE) );
385
386@endcode
387
388wxFlagsProperty can use wxPGChoices just the same way as wxEnumProperty
bba3f9b5 389<b>Note:</b> When changing "choices" (ie. flag labels) of wxFlagsProperty,
939d9364 390you will need to use wxPGProperty::SetChoices() to replace all choices
bba3f9b5 391at once - otherwise implicit child properties will not get updated properly.
1c4293cb 392
216214f8 393@section propgrid_advprops Specialized Properties
1c4293cb
VZ
394
395 This section describes the use of less often needed property classes.
396To use them, you have to include <wx/propgrid/advprops.h>.
397
398@code
399
400// Necessary extra header file
401#include <wx/propgrid/advprops.h>
402
403...
404
405 // Date property.
406 pg->Append( new wxDateProperty(wxT("MyDateProperty"),
407 wxPG_LABEL,
408 wxDateTime::Now()) );
409
bba3f9b5 410 // Image file property. Wild card is auto-generated from available
1c4293cb
VZ
411 // image handlers, so it is not set this time.
412 pg->Append( new wxImageFileProperty(wxT("Label of ImageFileProperty"),
413 wxT("NameOfImageFileProp")) );
414
415 // Font property has sub-properties. Note that we give window's font as
416 // initial value.
417 pg->Append( new wxFontProperty(wxT("Font"),
418 wxPG_LABEL,
419 GetFont()) );
420
421 // Colour property with arbitrary colour.
422 pg->Append( new wxColourProperty(wxT("My Colour 1"),
423 wxPG_LABEL,
424 wxColour(242,109,0) ) );
425
426 // System colour property.
427 pg->Append( new wxSystemColourProperty(wxT("My SysColour 1"),
428 wxPG_LABEL,
429 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)) );
430
431 // System colour property with custom colour.
432 pg->Append( new wxSystemColourProperty(wxT("My SysColour 2"),
433 wxPG_LABEL,
434 wxColour(0,200,160) ) );
435
436 // Cursor property
437 pg->Append( new wxCursorProperty(wxT("My Cursor"),
438 wxPG_LABEL,
439 wxCURSOR_ARROW));
440
441@endcode
442
443
bba3f9b5
JS
444@section propgrid_processingvalues Processing Property Values
445
446Properties store their values internally in wxVariant. You can obtain
447this value using wxPGProperty::GetValue() or wxPropertyGridInterface::
448GetPropertyValue().
449
450If you wish to obtain property value in specific data type, you can
451call various getter functions, such as wxPropertyGridInterface::
452GetPropertyValueAsString(), which, as name might say, returns property
453value's string representation. While this particular function is very
454safe to use for any kind of property, some might display error message
455if property value is not in compatible enough format. For instance,
456wxPropertyGridInterface::GetPropertyValueAsLongLong() will support
457long as well as wxLongLong, but GetPropertyValueAsArrayString() only
458supports wxArrayString and nothing else.
459
460In any case, you will need to take extra care when dealing with
461raw wxVariant values. For instance, wxIntProperty and wxUIntProperty,
462store value internally as wx(U)LongLong when number doesn't fit into
a6162a3e
JS
463standard long type. Using << operator to get wx(U)LongLong from wxVariant
464is customized to work quite safely with various types of variant data.
bba3f9b5
JS
465
466You may have noticed that properties store, in wxVariant, values of many
467types which are not natively supported by it. Custom wxVariantDatas
468are therefore implemented and << and >> operators implemented to
469convert data from and to wxVariant.
470
471Note that in some cases property value can be Null variant, which means
472that property value is unspecified. This usually occurs only when
473wxPG_EX_AUTO_UNSPECIFIED_VALUES extra window style is defined or when you
474manually set property value to Null (or unspecified).
475
476
216214f8 477@section propgrid_iterating Iterating through a property container
1c4293cb
VZ
478
479You can use somewhat STL'ish iterator classes to iterate through the grid.
480Here is a simple example of forward iterating through all individual
bba3f9b5
JS
481properties (not categories nor private child properties that are normally
482'transparent' to application code):
1c4293cb
VZ
483
484@code
485
486 wxPropertyGridIterator it;
487
488 for ( it = pg->GetIterator();
489 !it.AtEnd();
490 it++ )
491 {
492 wxPGProperty* p = *it;
493 // Do something with the property
494 }
495
496@endcode
497
498As expected there is also a const iterator:
499
500@code
501
502 wxPropertyGridConstIterator it;
503
504 for ( it = pg->GetIterator();
505 !it.AtEnd();
506 it++ )
507 {
508 const wxPGProperty* p = *it;
509 // Do something with the property
510 }
511
512@endcode
513
514You can give some arguments to GetIterator to determine which properties
515get automatically filtered out. For complete list of options, see
b55018ec
JS
516@ref propgrid_iterator_flags. GetIterator() also accepts other arguments.
517See wxPropertyGridInterface::GetIterator() for details.
1c4293cb
VZ
518
519This example reverse-iterates through all visible items:
520
521@code
522
523 wxPropertyGridIterator it;
524
525 for ( it = pg->GetIterator(wxPG_ITERATE_VISIBLE, wxBOTTOM);
526 !it.AtEnd();
527 it-- )
528 {
529 wxPGProperty* p = *it;
530 // Do something with the property
531 }
532
533@endcode
534
535<b>wxPython Note:</b> Instead of ++ operator, use Next() method, and instead of
536* operator, use GetProperty() method.
537
538GetIterator() only works with wxPropertyGrid and the individual pages
539of wxPropertyGridManager. In order to iterate through an arbitrary
bba3f9b5
JS
540property container (such as entire wxPropertyGridManager), you need to use
541wxPropertyGridInterface::GetVIterator(). Note however that this virtual
542iterator is limited to forward iteration.
1c4293cb
VZ
543
544@code
545
546 wxPGVIterator it;
547
548 for ( it = manager->GetVIterator();
549 !it.AtEnd();
550 it.Next() )
551 {
552 wxPGProperty* p = it.GetProperty();
553 // Do something with the property
554 }
555
556@endcode
557
216214f8 558@section propgrid_populating Populating wxPropertyGrid Automatically
1c4293cb 559
216214f8 560@subsection propgrid_fromvariants Populating from List of wxVariants
1c4293cb
VZ
561
562Example of populating an empty wxPropertyGrid from a values stored
563in an arbitrary list of wxVariants.
564
565@code
566
bba3f9b5 567 // This is a static method that initializes *all* built-in type handlers
1c4293cb
VZ
568 // available, including those for wxColour and wxFont. Refers to *all*
569 // included properties, so when compiling with static library, this
bba3f9b5 570 // method may increase the executable size noticeably.
1c4293cb
VZ
571 pg->InitAllTypeHandlers();
572
573 // Get contents of the grid as a wxVariant list
574 wxVariant all_values = pg->GetPropertyValues();
575
576 // Populate the list with values. If a property with appropriate
577 // name is not found, it is created according to the type of variant.
578 pg->SetPropertyValues( my_list_variant );
579
1c4293cb
VZ
580@endcode
581
216214f8 582@subsection propgrid_fromfile Loading Population from a Text-based Storage
1c4293cb
VZ
583
584Class wxPropertyGridPopulator may be helpful when writing code that
bba3f9b5
JS
585loads properties from a text-source. In fact, the wxPropertyGrid xrc-handler
586(which may not be currently included in wxWidgets, but probably will be in
587near future) uses it.
1c4293cb 588
bba3f9b5 589@subsection editablestate Saving and Restoring User-Editable State
1c4293cb 590
bba3f9b5
JS
591You can use wxPropertyGridInterface::SaveEditableState() and
592wxPropertyGridInterface::RestoreEditableState() to save and restore
593user-editable state (selected property, expanded/collapsed properties,
594selected page, scrolled position, and splitter positions).
1c4293cb 595
216214f8 596@section propgrid_events Event Handling
1c4293cb
VZ
597
598Probably the most important event is the Changed event which occurs when
599value of any property is changed by the user. Use EVT_PG_CHANGED(id,func)
600in your event table to use it.
601
602For complete list of event types, see wxPropertyGrid class reference.
603
bba3f9b5
JS
604However, one type of event that might need focused attention is EVT_PG_CHANGING,
605which occurs just prior property value is being changed by user. You can
606acquire pending value using wxPropertyGridEvent::GetValue(), and if it is
607not acceptable, call wxPropertyGridEvent::Veto() to prevent the value change
608from taking place.
1c4293cb
VZ
609
610@code
611
1c4293cb
VZ
612void MyForm::OnPropertyGridChanging( wxPropertyGridEvent& event )
613{
614 wxPGProperty* property = event.GetProperty();
615
616 if ( property == m_pWatchThisProperty )
617 {
618 // GetValue() returns the pending value, but is only
619 // supported by wxEVT_PG_CHANGING.
620 if ( event.GetValue().GetString() == g_pThisTextIsNotAllowed )
621 {
622 event.Veto();
623 return;
624 }
625 }
626}
627
628@endcode
629
bba3f9b5
JS
630@remarks On Child Property Event Handling
631- For properties which have private, implicit children (wxFontProperty and
632 wxFlagsProperty), events occur for the main parent property only.
633 For other properties events occur for the children themselves. See
634 @ref propgrid_parentprops.
1c4293cb 635
bba3f9b5 636- When property's child gets changed, you can use wxPropertyGridEvent::GetMainParent()
1c4293cb
VZ
637 to obtain its topmost non-category parent (useful, if you have deeply nested
638 properties).
639
640
216214f8 641@section propgrid_validating Validating Property Values
1c4293cb
VZ
642
643There are various ways to make sure user enters only correct values. First, you
644can use wxValidators similar to as you would with ordinary controls. Use
8622887c 645wxPropertyGridInterface::SetPropertyValidator() to assign wxValidator to
1c4293cb
VZ
646property.
647
648Second, you can subclass a property and override wxPGProperty::ValidateValue(),
bba3f9b5 649or handle wxEVT_PG_CHANGING for the same effect. Both of these ways do not
1c4293cb
VZ
650actually prevent user from temporarily entering invalid text, but they do give
651you an opportunity to warn the user and block changed value from being committed
652in a property.
653
654Various validation failure options can be controlled globally with
655wxPropertyGrid::SetValidationFailureBehavior(), or on an event basis by
656calling wxEvent::SetValidationFailureBehavior(). Here's a code snippet of
657how to handle wxEVT_PG_CHANGING, and to set custom failure behaviour and
658message.
659
660@code
661 void MyFrame::OnPropertyGridChanging(wxPropertyGridEvent& event)
662 {
663 wxPGProperty* property = event.GetProperty();
664
665 // You must use wxPropertyGridEvent::GetValue() to access
666 // the value to be validated.
667 wxVariant pendingValue = event.GetValue();
668
669 if ( property->GetName() == wxT("Font") )
670 {
671 // Make sure value is not unspecified
672 if ( !pendingValue.IsNull() )
673 {
bba3f9b5
JS
674 wxFont font;
675 font << pendingValue;
1c4293cb
VZ
676
677 // Let's just allow Arial font
678 if ( font.GetFaceName() != wxT("Arial") )
679 {
680 event.Veto();
681 event.SetValidationFailureBehavior(wxPG_VFB_STAY_IN_PROPERTY |
682 wxPG_VFB_BEEP |
683 wxPG_VFB_SHOW_MESSAGE);
684 }
685 }
686 }
687 }
688@endcode
689
690
216214f8 691@section propgrid_cellrender Customizing Individual Cell Appearance
1c4293cb
VZ
692
693You can control text colour, background colour, and attached image of
694each cell in the property grid. Use wxPropertyGridInterface::SetPropertyCell() or
695wxPGProperty::SetCell() for this purpose.
696
697In addition, it is possible to control these characteristics for
bba3f9b5 698wxPGChoices list items. See wxPGChoices class reference for more info.
1c4293cb
VZ
699
700
216214f8 701@section propgrid_customizing Customizing Properties (without sub-classing)
1c4293cb
VZ
702
703In this section are presented miscellaneous ways to have custom appearance
704and behavior for your properties without all the necessary hassle
705of sub-classing a property class etc.
706
216214f8 707@subsection propgrid_customimage Setting Value Image
1c4293cb
VZ
708
709Every property can have a small value image placed in front of the
710actual value text. Built-in example of this can be seen with
711wxColourProperty and wxImageFileProperty, but for others it can
712be set using wxPropertyGrid::SetPropertyImage method.
713
216214f8 714@subsection propgrid_customeditor Setting Property's Editor Control(s)
1c4293cb
VZ
715
716You can set editor control (or controls, in case of a control and button),
717of any property using wxPropertyGrid::SetPropertyEditor. Editors are passed
bba3f9b5 718as wxPGEditor_EditorName, and valid built-in EditorNames are
1c4293cb
VZ
719TextCtrl, Choice, ComboBox, CheckBox, TextCtrlAndButton, ChoiceAndButton,
720SpinCtrl, and DatePickerCtrl. Two last mentioned ones require call to
721static member function wxPropertyGrid::RegisterAdditionalEditors().
722
723Following example changes wxColourProperty's editor from default Choice
724to TextCtrlAndButton. wxColourProperty has its internal event handling set
725up so that button click events of the button will be used to trigger
726colour selection dialog.
727
728@code
729
bba3f9b5
JS
730 wxPGProperty* colProp = new wxColourProperty(wxT("Text Colour"));
731 pg->Append(colProp);
732 pg->SetPropertyEditor(colProp, wxPGEditor_TextCtrlAndButton);
1c4293cb
VZ
733
734@endcode
735
736Naturally, creating and setting custom editor classes is a possibility as
737well. For more information, see wxPGEditor class reference.
738
216214f8 739@subsection propgrid_editorattrs Property Attributes Recognized by Editors
1c4293cb 740
bba3f9b5
JS
741<b>SpinCtrl</b> editor can make use of property's "Min", "Max", "Step" and
742"Wrap" attributes.
1c4293cb 743
216214f8 744@subsection propgrid_multiplebuttons Adding Multiple Buttons Next to an Editor
1c4293cb
VZ
745
746See wxPGMultiButton class reference.
747
216214f8 748@subsection propgrid_customeventhandling Handling Events Passed from Properties
1c4293cb
VZ
749
750<b>wxEVT_COMMAND_BUTTON_CLICKED </b>(corresponds to event table macro EVT_BUTTON):
751Occurs when editor button click is not handled by the property itself
752(as is the case, for example, if you set property's editor to TextCtrlAndButton
753from the original TextCtrl).
754
216214f8 755@subsection propgrid_attributes Property Attributes
1c4293cb
VZ
756
757Miscellaneous values, often specific to a property type, can be set
bba3f9b5
JS
758using wxPropertyGridInterface::SetPropertyAttribute() and
759wxPropertyGridInterface::SetPropertyAttributeAll() methods.
1c4293cb
VZ
760
761Attribute names are strings and values wxVariant. Arbitrary names are allowed
bba3f9b5
JS
762in order to store values that are relevant to application only and not
763property grid. Constant equivalents of all attribute string names are
764provided. Some of them are defined as cached strings, so using these constants
765can provide for smaller binary size.
1c4293cb 766
b55018ec 767For complete list of attributes, see @ref propgrid_property_attributes.
1c4293cb 768
1c4293cb 769
216214f8 770@section propgrid_usage2 Using wxPropertyGridManager
1c4293cb
VZ
771
772wxPropertyGridManager is an efficient multi-page version of wxPropertyGrid,
bba3f9b5
JS
773which can optionally have tool bar for mode and page selection, and a help text
774box. For more information, see wxPropertyGridManager class reference.
1c4293cb 775
216214f8 776@subsection propgrid_propgridpage wxPropertyGridPage
1c4293cb
VZ
777
778wxPropertyGridPage is holder of properties for one page in manager. It is derived from
779wxEvtHandler, so you can subclass it to process page-specific property grid events. Hand
bba3f9b5 780over your page instance in wxPropertyGridManager::AddPage().
1c4293cb
VZ
781
782Please note that the wxPropertyGridPage itself only sports subset of wxPropertyGrid API
783(but unlike manager, this include item iteration). Naturally it inherits from
bba3f9b5 784wxPropertyGridInterface.
1c4293cb 785
bba3f9b5 786For more information, see wxPropertyGridPage class reference.
1c4293cb 787
bba3f9b5
JS
788
789@section propgrid_subclassing Sub-classing wxPropertyGrid and wxPropertyGridManager
1c4293cb
VZ
790
791Few things to note:
792
793- Only a small percentage of member functions are virtual. If you need more,
794 just e-mail to wx-dev mailing list.
795
796- Data manipulation is done in wxPropertyGridPageState class. So, instead of
bba3f9b5
JS
797 overriding wxPropertyGrid::Insert(), you'll probably want to override
798 wxPropertyGridPageState::DoInsert(). See header file for details.
1c4293cb 799
bba3f9b5
JS
800- Override wxPropertyGrid::CreateState() to instantiate your derivate
801 wxPropertyGridPageState. For wxPropertyGridManager, you'll need to subclass
802 wxPropertyGridPage instead (since it is derived from wxPropertyGridPageState),
803 and hand over instances in wxPropertyGridManager::AddPage() calls.
1c4293cb 804
bba3f9b5
JS
805- You can use a derivate wxPropertyGrid with manager by overriding
806 wxPropertyGridManager::CreatePropertyGrid() member function.
1c4293cb
VZ
807
808
216214f8 809@section propgrid_misc Miscellaneous Topics
1c4293cb 810
216214f8 811@subsection propgrid_namescope Property Name Scope
1c4293cb 812
258ccb95
JS
813 All properties which parent is category or root can be accessed
814directly by their base name (ie. name given for property in its constructor).
815Other properties can be accessed via "ParentsName.BaseName" notation,
816Naturally, all property names should be unique.
1c4293cb 817
216214f8 818@subsection propgrid_nonuniquelabels Non-unique Labels
258ccb95
JS
819
820 It is possible to have properties with identical label under same parent.
821However, care must be taken to ensure that each property still has
822unique (base) name.
1c4293cb 823
216214f8 824@subsection propgrid_boolproperty wxBoolProperty
1c4293cb 825
bba3f9b5
JS
826 There are few points about wxBoolProperty that require further discussion:
827 - wxBoolProperty can be shown as either normal combo box or as a check box.
1c4293cb
VZ
828 Property attribute wxPG_BOOL_USE_CHECKBOX is used to change this.
829 For example, if you have a wxFlagsProperty, you can
830 set its all items to use check box using the following:
831 @code
832 pg->SetPropertyAttribute(wxT("MyFlagsProperty"),wxPG_BOOL_USE_CHECKBOX,true,wxPG_RECURSE);
833 @endcode
8622887c 834
bba3f9b5
JS
835 Following will set all individual bool properties in your control to
836 use check box:
8622887c 837
bba3f9b5
JS
838 @code
839 pg->SetPropertyAttributeAll(wxPG_BOOL_USE_CHECKBOX, true);
840 @endcode
1c4293cb 841
939d9364 842 - Default item names for wxBoolProperty are ["False", "True"]. This can be
bba3f9b5
JS
843 changed using static function wxPropertyGrid::SetBoolChoices(trueChoice,
844 falseChoice).
1c4293cb 845
216214f8 846@subsection propgrid_textctrlupdates Updates from wxTextCtrl Based Editor
1c4293cb
VZ
847
848 Changes from wxTextCtrl based property editors are committed (ie.
849wxEVT_PG_CHANGED is sent etc.) *only* when (1) user presser enter, (2)
850user moves to edit another property, or (3) when focus leaves
851the grid.
852
853 Because of this, you may find it useful, in some apps, to call
854wxPropertyGrid::CommitChangesFromEditor() just before you need to do any
855computations based on property grid values. Note that CommitChangesFromEditor()
856will dispatch wxEVT_PG_CHANGED with ProcessEvent, so any of your event handlers
857will be called immediately.
858
216214f8 859@subsection propgrid_splittercentering Centering the Splitter
1c4293cb
VZ
860
861 If you need to center the splitter, but only once when the program starts,
862then do <b>not</b> use the wxPG_SPLITTER_AUTO_CENTER window style, but the
863wxPropertyGrid::CenterSplitter() method. <b>However, be sure to call it after
864the sizer setup and SetSize calls!</b> (ie. usually at the end of the
865frame/dialog constructor)
866
216214f8 867@subsection propgrid_splittersetting Setting Splitter Position When Creating Property Grid
1c4293cb
VZ
868
869Splitter position cannot exceed grid size, and therefore setting it during
870form creation may fail as initial grid size is often smaller than desired
871splitter position, especially when sizers are being used.
872
216214f8 873@subsection propgrid_colourproperty wxColourProperty and wxSystemColourProperty
1c4293cb 874
bba3f9b5 875Through sub-classing, these two property classes provide substantial customization
1c4293cb
VZ
876features. Subclass wxSystemColourProperty if you want to use wxColourPropertyValue
877(which features colour type in addition to wxColour), and wxColourProperty if plain
878wxColour is enough.
879
880Override wxSystemColourProperty::ColourToString() to redefine how colours are
881printed as strings.
882
883Override wxSystemColourProperty::GetCustomColourIndex() to redefine location of
884the item that triggers colour picker dialog (default is last).
885
886Override wxSystemColourProperty::GetColour() to determine which colour matches
887which choice entry.
888
216214f8 889@section propgrid_proplist Property Class Descriptions
1c4293cb
VZ
890
891See @ref pgproperty_properties
892
8622887c
JS
893@section propgrid_compat Changes from wxPropertyGrid 1.4
894
895Version of wxPropertyGrid bundled with wxWidgets 2.9+ has various backward-
896incompatible changes from version 1.4, which had a stable API and will remain
897as the last separate branch.
898
899Note that in general any behavior-breaking changes should not compile or run
900without warnings or errors.
901
902@subsection propgrid_compat_general General Changes
903
904 - Tab-traversal can no longer be used to travel between properties. Now
905 it only causes focus to move from main grid to editor of selected property.
906 Arrow keys are now your primary means of navigating between properties,
907 with keyboard. This change allowed fixing broken tab traversal on wxGTK
908 (which is open issue in wxPropertyGrid 1.4).
909
910 - A few member functions were removed from wxPropertyGridInterface.
911 Please use wxPGProperty's counterparts from now on.
912
913 - wxPGChoices now has proper Copy-On-Write behavior.
914
915 - wxPGChoices::SetExclusive() was renamed to AllocExclusive().
916
917 - wxPGProperty::SetPropertyChoicesExclusive() was removed. Instead, use
918 GetChoices().AllocExclusive().
919
920 - wxPGProperty::ClearModifiedStatus() is removed. Please use
921 SetModifiedStatus() instead.
922
923 - wxPropertyGridInterface::GetExpandedProperties() is removed. You should
924 now use wxPropertyGridInterface::GetEditableState() instead.
1c4293cb 925
8622887c
JS
926 - Extended window style wxPG_EX_LEGACY_VALIDATORS was removed.
927
928 - wxPropertyGridManager now has same Get/SetSelection() semantics as
929 wxPropertyGrid.
930
931 - Various wxPropertyGridManager page-related functions now return pointer
932 to the page object instead of index.
933
934 - Instead of calling wxPropertyGrid::SetButtonShortcut(), use
935 wxPropertyGrid::SetActionTrigger(wxPG_ACTION_PRESS_BUTTON).
936
937 - wxPGProperty::GetCell() now returns a reference. AcquireCell() was removed.
938
939 - wxPGMultiButton::FinalizePosition() has been renamed to Finalize(),
940 and it has slightly different argument list.
941
942 - wxPropertyGridEvent::HasProperty() is removed. You can use GetProperty()
943 as immediate replacement when checking if event has a property.
944
945@subsection propgrid_compat_propdev Property and Editor Sub-classing Changes
946
947 - Confusing custom property macros have been eliminated.
948
949 - Implement wxPGProperty::ValueToString() instead of GetValueAsString().
950
951 - wxPGProperty::ChildChanged() must now return the modified value of
952 whole property instead of writing it back into 'thisValue' argument.
953
954 - Removed wxPropertyGrid::PrepareValueForDialogEditing(). Use
955 wxPropertyGrid::GetPendingEditedValue() instead.
956
957 - wxPGProperty::GetChoiceInfo() is removed, as all properties now carry
958 wxPGChoices instance (protected wxPGProperty::m_choices).
959
960 - Connect() should no longer be called in implementations of
961 wxPGEditor::CreateControls(). wxPropertyGrid automatically passes all
962 events from editor to wxPGEditor::OnEvent() and wxPGProperty::OnEvent(),
963 as appropriate.
0cd4552a
JS
964
965 - wxPython: Previously some of the reimplemented member functions needed a
966 'Py' prefix. This is no longer necessary. For instance, if you previously
967 implemented PyStringToValue() for your custom property, you should now
968 just implement StringToValue().
8622887c 969*/