Added wxPropertyGrid::SetUnspecifiedValueAppearance(); Added wxPGEditor::SetControlAp...
[wxWidgets.git] / src / propgrid / propgrid.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/propgrid.cpp
3 // Purpose: wxPropertyGrid
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: 2004-09-25
7 // RCS-ID: $Id$
8 // Copyright: (c) Jaakko Salli
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_PROPGRID
20
21 #ifndef WX_PRECOMP
22 #include "wx/defs.h"
23 #include "wx/object.h"
24 #include "wx/hash.h"
25 #include "wx/string.h"
26 #include "wx/log.h"
27 #include "wx/event.h"
28 #include "wx/window.h"
29 #include "wx/panel.h"
30 #include "wx/dc.h"
31 #include "wx/dcmemory.h"
32 #include "wx/button.h"
33 #include "wx/pen.h"
34 #include "wx/brush.h"
35 #include "wx/cursor.h"
36 #include "wx/dialog.h"
37 #include "wx/settings.h"
38 #include "wx/msgdlg.h"
39 #include "wx/choice.h"
40 #include "wx/stattext.h"
41 #include "wx/scrolwin.h"
42 #include "wx/dirdlg.h"
43 #include "wx/sizer.h"
44 #include "wx/textdlg.h"
45 #include "wx/filedlg.h"
46 #include "wx/statusbr.h"
47 #include "wx/intl.h"
48 #include "wx/frame.h"
49 #endif
50
51
52 // This define is necessary to prevent macro clearing
53 #define __wxPG_SOURCE_FILE__
54
55 #include "wx/propgrid/propgrid.h"
56 #include "wx/propgrid/editors.h"
57
58 #if wxPG_USE_RENDERER_NATIVE
59 #include "wx/renderer.h"
60 #endif
61
62 #include "wx/odcombo.h"
63
64 #include "wx/timer.h"
65 #include "wx/dcbuffer.h"
66
67 // Two pics for the expand / collapse buttons.
68 // Files are not supplied with this project (since it is
69 // recommended to use either custom or native rendering).
70 // If you want them, get wxTreeMultiCtrl by Jorgen Bodde,
71 // and copy xpm files from archive to wxPropertyGrid src directory
72 // (and also comment/undef wxPG_ICON_WIDTH in propGrid.h
73 // and set wxPG_USE_RENDERER_NATIVE to 0).
74 #ifndef wxPG_ICON_WIDTH
75 #if defined(__WXMAC__)
76 #include "mac_collapse.xpm"
77 #include "mac_expand.xpm"
78 #elif defined(__WXGTK__)
79 #include "linux_collapse.xpm"
80 #include "linux_expand.xpm"
81 #else
82 #include "default_collapse.xpm"
83 #include "default_expand.xpm"
84 #endif
85 #endif
86
87
88 //#define wxPG_TEXT_INDENT 4 // For the wxComboControl
89 //#define wxPG_ALLOW_CLIPPING 1 // If 1, GetUpdateRegion() in OnPaint event handler is not ignored
90 #define wxPG_GUTTER_DIV 3 // gutter is max(iconwidth/gutter_div,gutter_min)
91 #define wxPG_GUTTER_MIN 3 // gutter before and after image of [+] or [-]
92 #define wxPG_YSPACING_MIN 1
93 #define wxPG_DEFAULT_VSPACING 2 // This matches .NET propertygrid's value,
94 // but causes normal combobox to spill out under MSW
95
96 //#define wxPG_OPTIMAL_WIDTH 200 // Arbitrary
97
98 //#define wxPG_MIN_SCROLLBAR_WIDTH 10 // Smallest scrollbar width on any platform
99 // Must be larger than largest control border
100 // width * 2.
101
102
103 #define wxPG_DEFAULT_CURSOR wxNullCursor
104
105
106 //#define wxPG_NAT_CHOICE_BORDER_ANY 0
107
108 //#define wxPG_HIDER_BUTTON_HEIGHT 25
109
110 #define wxPG_PIXELS_PER_UNIT m_lineHeight
111
112 #ifdef wxPG_ICON_WIDTH
113 #define m_iconHeight m_iconWidth
114 #endif
115
116 //#define wxPG_TOOLTIP_DELAY 1000
117
118 // This is the number of pixels the expander button inside
119 // property cells (i.e. not in the grey margin area are
120 // adjusted.
121 #define IN_CELL_EXPANDER_BUTTON_X_ADJUST 2
122
123 // -----------------------------------------------------------------------
124
125 #if wxUSE_INTL
126 void wxPropertyGrid::AutoGetTranslation ( bool enable )
127 {
128 wxPGGlobalVars->m_autoGetTranslation = enable;
129 }
130 #else
131 void wxPropertyGrid::AutoGetTranslation ( bool ) { }
132 #endif
133
134 // -----------------------------------------------------------------------
135
136 const char wxPropertyGridNameStr[] = "wxPropertyGrid";
137
138 // -----------------------------------------------------------------------
139 // Statics in one class for easy destruction.
140 // -----------------------------------------------------------------------
141
142 #include "wx/module.h"
143
144 class wxPGGlobalVarsClassManager : public wxModule
145 {
146 DECLARE_DYNAMIC_CLASS(wxPGGlobalVarsClassManager)
147 public:
148 wxPGGlobalVarsClassManager() {}
149 virtual bool OnInit() { wxPGGlobalVars = new wxPGGlobalVarsClass(); return true; }
150 virtual void OnExit() { delete wxPGGlobalVars; wxPGGlobalVars = NULL; }
151 };
152
153 IMPLEMENT_DYNAMIC_CLASS(wxPGGlobalVarsClassManager, wxModule)
154
155
156 // When wxPG is loaded dynamically after the application is already running
157 // then the built-in module system won't pick this one up. Add it manually.
158 void wxPGInitResourceModule()
159 {
160 wxModule* module = new wxPGGlobalVarsClassManager;
161 module->Init();
162 wxModule::RegisterModule(module);
163 }
164
165 wxPGGlobalVarsClass* wxPGGlobalVars = NULL;
166
167
168 wxPGGlobalVarsClass::wxPGGlobalVarsClass()
169 {
170 wxPGProperty::sm_wxPG_LABEL = new wxString(wxPG_LABEL_STRING);
171
172 m_boolChoices.Add(_("False"));
173 m_boolChoices.Add(_("True"));
174
175 m_fontFamilyChoices = NULL;
176
177 m_defaultRenderer = new wxPGDefaultRenderer();
178
179 m_autoGetTranslation = false;
180
181 m_offline = 0;
182
183 m_extraStyle = 0;
184
185 wxVariant v;
186
187 // Prepare some shared variants
188 m_vEmptyString = wxString();
189 m_vZero = (long) 0;
190 m_vMinusOne = (long) -1;
191 m_vTrue = true;
192 m_vFalse = false;
193
194 // Prepare cached string constants
195 m_strstring = wxS("string");
196 m_strlong = wxS("long");
197 m_strbool = wxS("bool");
198 m_strlist = wxS("list");
199 m_strDefaultValue = wxS("DefaultValue");
200 m_strMin = wxS("Min");
201 m_strMax = wxS("Max");
202 m_strUnits = wxS("Units");
203 m_strInlineHelp = wxS("InlineHelp");
204
205 m_warnings = 0;
206 }
207
208
209 wxPGGlobalVarsClass::~wxPGGlobalVarsClass()
210 {
211 size_t i;
212
213 delete m_defaultRenderer;
214
215 // This will always have one ref
216 delete m_fontFamilyChoices;
217
218 #if wxUSE_VALIDATORS
219 for ( i=0; i<m_arrValidators.size(); i++ )
220 delete ((wxValidator*)m_arrValidators[i]);
221 #endif
222
223 //
224 // Destroy value type class instances.
225 wxPGHashMapS2P::iterator vt_it;
226
227 // Destroy editor class instances.
228 // iterate over all the elements in the class
229 for( vt_it = m_mapEditorClasses.begin(); vt_it != m_mapEditorClasses.end(); ++vt_it )
230 {
231 delete ((wxPGEditor*)vt_it->second);
232 }
233
234 delete wxPGProperty::sm_wxPG_LABEL;
235 }
236
237 void wxPropertyGridInitGlobalsIfNeeded()
238 {
239 }
240
241 // -----------------------------------------------------------------------
242 // wxPropertyGrid
243 // -----------------------------------------------------------------------
244
245 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGrid, wxScrolledWindow)
246
247 BEGIN_EVENT_TABLE(wxPropertyGrid, wxScrolledWindow)
248 EVT_IDLE(wxPropertyGrid::OnIdle)
249 EVT_PAINT(wxPropertyGrid::OnPaint)
250 EVT_SIZE(wxPropertyGrid::OnResize)
251 EVT_ENTER_WINDOW(wxPropertyGrid::OnMouseEntry)
252 EVT_LEAVE_WINDOW(wxPropertyGrid::OnMouseEntry)
253 EVT_MOUSE_CAPTURE_CHANGED(wxPropertyGrid::OnCaptureChange)
254 EVT_SCROLLWIN(wxPropertyGrid::OnScrollEvent)
255 EVT_CHILD_FOCUS(wxPropertyGrid::OnChildFocusEvent)
256 EVT_SET_FOCUS(wxPropertyGrid::OnFocusEvent)
257 EVT_KILL_FOCUS(wxPropertyGrid::OnFocusEvent)
258 EVT_SYS_COLOUR_CHANGED(wxPropertyGrid::OnSysColourChanged)
259 EVT_MOTION(wxPropertyGrid::OnMouseMove)
260 EVT_LEFT_DOWN(wxPropertyGrid::OnMouseClick)
261 EVT_LEFT_UP(wxPropertyGrid::OnMouseUp)
262 EVT_RIGHT_UP(wxPropertyGrid::OnMouseRightClick)
263 EVT_LEFT_DCLICK(wxPropertyGrid::OnMouseDoubleClick)
264 EVT_KEY_DOWN(wxPropertyGrid::OnKey)
265 END_EVENT_TABLE()
266
267 // -----------------------------------------------------------------------
268
269 wxPropertyGrid::wxPropertyGrid()
270 : wxScrolledWindow()
271 {
272 Init1();
273 }
274
275 // -----------------------------------------------------------------------
276
277 wxPropertyGrid::wxPropertyGrid( wxWindow *parent,
278 wxWindowID id,
279 const wxPoint& pos,
280 const wxSize& size,
281 long style,
282 const wxString& name )
283 : wxScrolledWindow()
284 {
285 Init1();
286 Create(parent,id,pos,size,style,name);
287 }
288
289 // -----------------------------------------------------------------------
290
291 bool wxPropertyGrid::Create( wxWindow *parent,
292 wxWindowID id,
293 const wxPoint& pos,
294 const wxSize& size,
295 long style,
296 const wxString& name )
297 {
298
299 if (!(style&wxBORDER_MASK))
300 {
301 style |= wxBORDER_THEME;
302 }
303
304 style |= wxVSCROLL;
305
306 // Filter out wxTAB_TRAVERSAL - we will handle TABs manually
307 style &= ~(wxTAB_TRAVERSAL);
308 style |= wxWANTS_CHARS;
309
310 wxScrolledWindow::Create(parent,id,pos,size,style,name);
311
312 Init2();
313
314 return true;
315 }
316
317 // -----------------------------------------------------------------------
318
319 //
320 // Initialize values to defaults
321 //
322 void wxPropertyGrid::Init1()
323 {
324 // Register editor classes, if necessary.
325 if ( wxPGGlobalVars->m_mapEditorClasses.empty() )
326 wxPropertyGrid::RegisterDefaultEditors();
327
328 m_iFlags = 0;
329 m_pState = NULL;
330 m_wndEditor = m_wndEditor2 = NULL;
331 m_selColumn = 1;
332 m_colHover = 1;
333 m_propHover = NULL;
334 m_labelEditor = NULL;
335 m_labelEditorProperty = NULL;
336 m_eventObject = this;
337 m_curFocused = NULL;
338 m_processedEvent = NULL;
339 m_sortFunction = NULL;
340 m_inDoPropertyChanged = 0;
341 m_inCommitChangesFromEditor = 0;
342 m_inDoSelectProperty = 0;
343 m_permanentValidationFailureBehavior = wxPG_VFB_DEFAULT;
344 m_dragStatus = 0;
345 m_mouseSide = 16;
346 m_editorFocused = 0;
347
348 // Must set empty but valid data
349 m_unspecifiedAppearance.SetEmptyData();
350
351 // Set default keys
352 AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_RIGHT );
353 AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_DOWN );
354 AddActionTrigger( wxPG_ACTION_PREV_PROPERTY, WXK_LEFT );
355 AddActionTrigger( wxPG_ACTION_PREV_PROPERTY, WXK_UP );
356 AddActionTrigger( wxPG_ACTION_EXPAND_PROPERTY, WXK_RIGHT);
357 AddActionTrigger( wxPG_ACTION_COLLAPSE_PROPERTY, WXK_LEFT);
358 AddActionTrigger( wxPG_ACTION_CANCEL_EDIT, WXK_ESCAPE );
359 AddActionTrigger( wxPG_ACTION_PRESS_BUTTON, WXK_DOWN, wxMOD_ALT );
360 AddActionTrigger( wxPG_ACTION_PRESS_BUTTON, WXK_F4 );
361
362 m_coloursCustomized = 0;
363 m_frozen = 0;
364
365 #if wxPG_DOUBLE_BUFFER
366 m_doubleBuffer = NULL;
367 #endif
368
369 #ifndef wxPG_ICON_WIDTH
370 m_expandbmp = NULL;
371 m_collbmp = NULL;
372 m_iconWidth = 11;
373 m_iconHeight = 11;
374 #else
375 m_iconWidth = wxPG_ICON_WIDTH;
376 #endif
377
378 m_prevVY = -1;
379
380 m_gutterWidth = wxPG_GUTTER_MIN;
381 m_subgroup_extramargin = 10;
382
383 m_lineHeight = 0;
384
385 m_width = m_height = 0;
386
387 m_commonValues.push_back(new wxPGCommonValue(_("Unspecified"), wxPGGlobalVars->m_defaultRenderer) );
388 m_cvUnspecified = 0;
389
390 m_chgInfo_changedProperty = NULL;
391 }
392
393 // -----------------------------------------------------------------------
394
395 //
396 // Initialize after parent etc. set
397 //
398 void wxPropertyGrid::Init2()
399 {
400 wxASSERT( !(m_iFlags & wxPG_FL_INITIALIZED ) );
401
402 #ifdef __WXMAC__
403 // Smaller controls on Mac
404 SetWindowVariant(wxWINDOW_VARIANT_SMALL);
405 #endif
406
407 // Now create state, if one didn't exist already
408 // (wxPropertyGridManager might have created it for us).
409 if ( !m_pState )
410 {
411 m_pState = CreateState();
412 m_pState->m_pPropGrid = this;
413 m_iFlags |= wxPG_FL_CREATEDSTATE;
414 }
415
416 if ( !(m_windowStyle & wxPG_SPLITTER_AUTO_CENTER) )
417 m_pState->m_dontCenterSplitter = true;
418
419 if ( m_windowStyle & wxPG_HIDE_CATEGORIES )
420 {
421 m_pState->InitNonCatMode();
422
423 m_pState->m_properties = m_pState->m_abcArray;
424 }
425
426 GetClientSize(&m_width,&m_height);
427
428 #ifndef wxPG_ICON_WIDTH
429 // create two bitmap nodes for drawing
430 m_expandbmp = new wxBitmap(expand_xpm);
431 m_collbmp = new wxBitmap(collapse_xpm);
432
433 // calculate average font height for bitmap centering
434
435 m_iconWidth = m_expandbmp->GetWidth();
436 m_iconHeight = m_expandbmp->GetHeight();
437 #endif
438
439 m_curcursor = wxCURSOR_ARROW;
440 m_cursorSizeWE = new wxCursor( wxCURSOR_SIZEWE );
441
442 // adjust bitmap icon y position so they are centered
443 m_vspacing = wxPG_DEFAULT_VSPACING;
444
445 CalculateFontAndBitmapStuff( wxPG_DEFAULT_VSPACING );
446
447 // Allocate cell datas
448 m_propertyDefaultCell.SetEmptyData();
449 m_categoryDefaultCell.SetEmptyData();
450
451 RegainColours();
452
453 // This helps with flicker
454 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
455
456 // Hook the top-level parent
457 m_tlp = NULL;
458 m_tlpClosed = NULL;
459 m_tlpClosedTime = 0;
460
461 // set virtual size to this window size
462 wxSize wndsize = GetSize();
463 SetVirtualSize(wndsize.GetWidth(), wndsize.GetWidth());
464
465 m_timeCreated = ::wxGetLocalTimeMillis();
466
467 //m_canvas->Create(this, wxID_ANY, wxPoint(0, 0), GetClientSize(),
468 // wxWANTS_CHARS | wxCLIP_CHILDREN);
469 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
470
471 m_iFlags |= wxPG_FL_INITIALIZED;
472
473 m_ncWidth = wndsize.GetWidth();
474
475 // Need to call OnResize handler or size given in constructor/Create
476 // will never work.
477 wxSizeEvent sizeEvent(wndsize,0);
478 OnResize(sizeEvent);
479 }
480
481 // -----------------------------------------------------------------------
482
483 wxPropertyGrid::~wxPropertyGrid()
484 {
485 size_t i;
486
487 #if wxUSE_THREADS
488 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
489 #endif
490
491 //
492 // Remove grid and property pointers from live wxPropertyGridEvents.
493 for ( i=0; i<m_liveEvents.size(); i++ )
494 {
495 wxPropertyGridEvent* evt = m_liveEvents[i];
496 evt->SetPropertyGrid(NULL);
497 evt->SetProperty(NULL);
498 }
499 m_liveEvents.clear();
500
501 if ( m_processedEvent )
502 {
503 // All right... we are being deleted while wxPropertyGrid event
504 // is being sent. Make sure that event propagates as little
505 // as possible (although usually this is not enough to prevent
506 // a crash).
507 m_processedEvent->Skip(false);
508 m_processedEvent->StopPropagation();
509
510 // Let's use wxMessageBox to make the message appear more
511 // reliably (and *before* the crash can happen).
512 ::wxMessageBox("wxPropertyGrid was being destroyed in an event "
513 "generated by it. This usually leads to a crash "
514 "so it is recommended to destroy the control "
515 "at idle time instead.");
516 }
517
518 DoSelectProperty(NULL, wxPG_SEL_NOVALIDATE|wxPG_SEL_DONT_SEND_EVENT);
519
520 // This should do prevent things from going too badly wrong
521 m_iFlags &= ~(wxPG_FL_INITIALIZED);
522
523 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
524 ReleaseMouse();
525
526 // Call with NULL to disconnect event handling
527 if ( GetExtraStyle() & wxPG_EX_ENABLE_TLP_TRACKING )
528 {
529 OnTLPChanging(NULL);
530
531 wxASSERT_MSG( !IsEditorsValueModified(),
532 wxS("Most recent change in property editor was ")
533 wxS("lost!!! (if you don't want this to happen, ")
534 wxS("close your frames and dialogs using ")
535 wxS("Close(false).)") );
536 }
537
538 #if wxPG_DOUBLE_BUFFER
539 if ( m_doubleBuffer )
540 delete m_doubleBuffer;
541 #endif
542
543 if ( m_iFlags & wxPG_FL_CREATEDSTATE )
544 delete m_pState;
545
546 delete m_cursorSizeWE;
547
548 #ifndef wxPG_ICON_WIDTH
549 delete m_expandbmp;
550 delete m_collbmp;
551 #endif
552
553 // Delete common value records
554 for ( i=0; i<m_commonValues.size(); i++ )
555 {
556 // Use temporary variable to work around possible strange VC6 (asserts because m_size is zero)
557 wxPGCommonValue* value = m_commonValues[i];
558 delete value;
559 }
560 }
561
562 // -----------------------------------------------------------------------
563
564 bool wxPropertyGrid::Destroy()
565 {
566 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
567 ReleaseMouse();
568
569 return wxScrolledWindow::Destroy();
570 }
571
572 // -----------------------------------------------------------------------
573
574 wxPropertyGridPageState* wxPropertyGrid::CreateState() const
575 {
576 return new wxPropertyGridPageState();
577 }
578
579 // -----------------------------------------------------------------------
580 // wxPropertyGrid overridden wxWindow methods
581 // -----------------------------------------------------------------------
582
583 void wxPropertyGrid::SetWindowStyleFlag( long style )
584 {
585 long old_style = m_windowStyle;
586
587 if ( m_iFlags & wxPG_FL_INITIALIZED )
588 {
589 wxASSERT( m_pState );
590
591 if ( !(style & wxPG_HIDE_CATEGORIES) && (old_style & wxPG_HIDE_CATEGORIES) )
592 {
593 // Enable categories
594 EnableCategories( true );
595 }
596 else if ( (style & wxPG_HIDE_CATEGORIES) && !(old_style & wxPG_HIDE_CATEGORIES) )
597 {
598 // Disable categories
599 EnableCategories( false );
600 }
601 if ( !(old_style & wxPG_AUTO_SORT) && (style & wxPG_AUTO_SORT) )
602 {
603 //
604 // Autosort enabled
605 //
606 if ( !m_frozen )
607 PrepareAfterItemsAdded();
608 else
609 m_pState->m_itemsAdded = 1;
610 }
611 #if wxPG_SUPPORT_TOOLTIPS
612 if ( !(old_style & wxPG_TOOLTIPS) && (style & wxPG_TOOLTIPS) )
613 {
614 //
615 // Tooltips enabled
616 //
617 /*
618 wxToolTip* tooltip = new wxToolTip ( wxEmptyString );
619 SetToolTip ( tooltip );
620 tooltip->SetDelay ( wxPG_TOOLTIP_DELAY );
621 */
622 }
623 else if ( (old_style & wxPG_TOOLTIPS) && !(style & wxPG_TOOLTIPS) )
624 {
625 //
626 // Tooltips disabled
627 //
628 wxScrolledWindow::SetToolTip( NULL );
629 }
630 #endif
631 }
632
633 wxScrolledWindow::SetWindowStyleFlag ( style );
634
635 if ( m_iFlags & wxPG_FL_INITIALIZED )
636 {
637 if ( (old_style & wxPG_HIDE_MARGIN) != (style & wxPG_HIDE_MARGIN) )
638 {
639 CalculateFontAndBitmapStuff( m_vspacing );
640 Refresh();
641 }
642 }
643 }
644
645 // -----------------------------------------------------------------------
646
647 void wxPropertyGrid::Freeze()
648 {
649 if ( !m_frozen )
650 {
651 wxScrolledWindow::Freeze();
652 }
653 m_frozen++;
654 }
655
656 // -----------------------------------------------------------------------
657
658 void wxPropertyGrid::Thaw()
659 {
660 m_frozen--;
661
662 if ( !m_frozen )
663 {
664 wxScrolledWindow::Thaw();
665 RecalculateVirtualSize();
666 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
667 Refresh();
668 #endif
669
670 // Force property re-selection
671 // NB: We must copy the selection.
672 wxArrayPGProperty selection = m_pState->m_selection;
673 DoSetSelection(selection, wxPG_SEL_FORCE | wxPG_SEL_NONVISIBLE);
674 }
675 }
676
677 // -----------------------------------------------------------------------
678
679 bool wxPropertyGrid::DoAddToSelection( wxPGProperty* prop, int selFlags )
680 {
681 wxCHECK( prop, false );
682
683 if ( !(GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION) )
684 return DoSelectProperty(prop, selFlags);
685
686 wxArrayPGProperty& selection = m_pState->m_selection;
687
688 if ( !selection.size() )
689 {
690 return DoSelectProperty(prop, selFlags);
691 }
692 else
693 {
694 // For categories, only one can be selected at a time
695 if ( prop->IsCategory() || selection[0]->IsCategory() )
696 return true;
697
698 selection.push_back(prop);
699
700 if ( !(selFlags & wxPG_SEL_DONT_SEND_EVENT) )
701 {
702 SendEvent( wxEVT_PG_SELECTED, prop, NULL );
703 }
704
705 DrawItem(prop);
706 }
707
708 return true;
709 }
710
711 // -----------------------------------------------------------------------
712
713 bool wxPropertyGrid::DoRemoveFromSelection( wxPGProperty* prop, int selFlags )
714 {
715 wxCHECK( prop, false );
716 bool res;
717
718 wxArrayPGProperty& selection = m_pState->m_selection;
719 if ( selection.size() <= 1 )
720 {
721 res = DoSelectProperty(NULL, selFlags);
722 }
723 else
724 {
725 m_pState->DoRemoveFromSelection(prop);
726 DrawItem(prop);
727 res = true;
728 }
729
730 return res;
731 }
732
733 // -----------------------------------------------------------------------
734
735 bool wxPropertyGrid::DoSelectAndEdit( wxPGProperty* prop,
736 unsigned int colIndex,
737 unsigned int selFlags )
738 {
739 //
740 // NB: Enable following if label editor background colour is
741 // ever changed to any other than m_colSelBack.
742 //
743 // We use this workaround to prevent visible flicker when editing
744 // a cell. Atleast on wxMSW, there is a difficult to find
745 // (and perhaps prevent) redraw somewhere between making property
746 // selected and enabling label editing.
747 //
748 //wxColour prevColSelBack = m_colSelBack;
749 //m_colSelBack = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
750
751 bool res;
752
753 if ( colIndex == 1 )
754 {
755 res = DoSelectProperty(prop, selFlags);
756 }
757 else
758 {
759 // send event
760 DoClearSelection(false, wxPG_SEL_NO_REFRESH);
761
762 if ( m_pState->m_editableColumns.Index(colIndex) == wxNOT_FOUND )
763 {
764 res = DoAddToSelection(prop, selFlags);
765 }
766 else
767 {
768 res = DoAddToSelection(prop, selFlags|wxPG_SEL_NO_REFRESH);
769
770 DoBeginLabelEdit(colIndex, selFlags);
771 }
772 }
773
774 //m_colSelBack = prevColSelBack;
775 return res;
776 }
777
778 // -----------------------------------------------------------------------
779
780 bool wxPropertyGrid::AddToSelectionFromInputEvent( wxPGProperty* prop,
781 unsigned int colIndex,
782 wxMouseEvent* mouseEvent,
783 int selFlags )
784 {
785 const wxArrayPGProperty& selection = GetSelectedProperties();
786 bool alreadySelected = m_pState->DoIsPropertySelected(prop);
787 bool res = true;
788
789 // Set to 2 if also add all items in between
790 int addToExistingSelection = 0;
791
792 if ( GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION )
793 {
794 if ( mouseEvent )
795 {
796 if ( mouseEvent->GetEventType() == wxEVT_RIGHT_DOWN ||
797 mouseEvent->GetEventType() == wxEVT_RIGHT_UP )
798 {
799 // Allow right-click for context menu without
800 // disturbing the selection.
801 if ( GetSelectedProperties().size() <= 1 ||
802 !alreadySelected )
803 return DoSelectAndEdit(prop, colIndex, selFlags);
804 return true;
805 }
806 else
807 {
808 if ( mouseEvent->ControlDown() )
809 {
810 addToExistingSelection = 1;
811 }
812 else if ( mouseEvent->ShiftDown() )
813 {
814 if ( selection.size() > 0 && !prop->IsCategory() )
815 addToExistingSelection = 2;
816 else
817 addToExistingSelection = 1;
818 }
819 }
820 }
821 }
822
823 if ( addToExistingSelection == 1 )
824 {
825 // Add/remove one
826 if ( !alreadySelected )
827 {
828 res = DoAddToSelection(prop, selFlags);
829 }
830 else if ( GetSelectedProperties().size() > 1 )
831 {
832 res = DoRemoveFromSelection(prop, selFlags);
833 }
834 }
835 else if ( addToExistingSelection == 2 )
836 {
837 // Add this, and all in between
838
839 // Find top selected property
840 wxPGProperty* topSelProp = selection[0];
841 int topSelPropY = topSelProp->GetY();
842 for ( unsigned int i=1; i<selection.size(); i++ )
843 {
844 wxPGProperty* p = selection[i];
845 int y = p->GetY();
846 if ( y < topSelPropY )
847 {
848 topSelProp = p;
849 topSelPropY = y;
850 }
851 }
852
853 wxPGProperty* startFrom;
854 wxPGProperty* stopAt;
855
856 if ( prop->GetY() <= topSelPropY )
857 {
858 // Property is above selection (or same)
859 startFrom = prop;
860 stopAt = topSelProp;
861 }
862 else
863 {
864 // Property is below selection
865 startFrom = topSelProp;
866 stopAt = prop;
867 }
868
869 // Iterate through properties in-between, and select them
870 wxPropertyGridIterator it;
871
872 for ( it = GetIterator(wxPG_ITERATE_VISIBLE, startFrom);
873 !it.AtEnd();
874 it++ )
875 {
876 wxPGProperty* p = *it;
877
878 if ( !p->IsCategory() &&
879 !m_pState->DoIsPropertySelected(p) )
880 {
881 DoAddToSelection(p, selFlags);
882 }
883
884 if ( p == stopAt )
885 break;
886 }
887 }
888 else
889 {
890 res = DoSelectAndEdit(prop, colIndex, selFlags);
891 }
892
893 return res;
894 }
895
896 // -----------------------------------------------------------------------
897
898 void wxPropertyGrid::DoSetSelection( const wxArrayPGProperty& newSelection,
899 int selFlags )
900 {
901 if ( newSelection.size() > 0 )
902 {
903 if ( !DoSelectProperty(newSelection[0], selFlags) )
904 return;
905 }
906 else
907 {
908 DoClearSelection(false, selFlags);
909 }
910
911 for ( unsigned int i = 1; i < newSelection.size(); i++ )
912 {
913 DoAddToSelection(newSelection[i], selFlags);
914 }
915
916 Refresh();
917 }
918
919 // -----------------------------------------------------------------------
920
921 void wxPropertyGrid::MakeColumnEditable( unsigned int column,
922 bool editable )
923 {
924 wxASSERT( column != 1 );
925
926 wxArrayInt& cols = m_pState->m_editableColumns;
927
928 if ( editable )
929 {
930 cols.push_back(column);
931 }
932 else
933 {
934 for ( int i = cols.size() - 1; i > 0; i-- )
935 {
936 if ( cols[i] == (int)column )
937 cols.erase( cols.begin() + i );
938 }
939 }
940 }
941
942 // -----------------------------------------------------------------------
943
944 void wxPropertyGrid::DoBeginLabelEdit( unsigned int colIndex,
945 int selFlags )
946 {
947 wxPGProperty* selected = GetSelection();
948 wxCHECK_RET(selected, wxT("No property selected"));
949 wxCHECK_RET(colIndex != 1, wxT("Do not use this for column 1"));
950
951 if ( !(selFlags & wxPG_SEL_DONT_SEND_EVENT) )
952 {
953 if ( SendEvent( wxEVT_PG_LABEL_EDIT_BEGIN,
954 selected, NULL, 0,
955 colIndex ) )
956 return;
957 }
958
959 wxString text;
960 const wxPGCell* cell = NULL;
961 if ( selected->HasCell(colIndex) )
962 {
963 cell = &selected->GetCell(colIndex);
964 if ( !cell->HasText() && colIndex == 0 )
965 text = selected->GetLabel();
966 }
967
968 if ( !cell )
969 {
970 if ( colIndex == 0 )
971 text = selected->GetLabel();
972 else
973 cell = &selected->GetOrCreateCell(colIndex);
974 }
975
976 if ( cell && cell->HasText() )
977 text = cell->GetText();
978
979 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE); // send event
980
981 m_selColumn = colIndex;
982
983 wxRect r = GetEditorWidgetRect(selected, m_selColumn);
984
985 wxWindow* tc = GenerateEditorTextCtrl(r.GetPosition(),
986 r.GetSize(),
987 text,
988 NULL,
989 wxTE_PROCESS_ENTER,
990 0,
991 colIndex);
992
993 wxWindowID id = tc->GetId();
994 tc->Connect(id, wxEVT_COMMAND_TEXT_ENTER,
995 wxCommandEventHandler(wxPropertyGrid::OnLabelEditorEnterPress),
996 NULL, this);
997 tc->Connect(id, wxEVT_KEY_DOWN,
998 wxKeyEventHandler(wxPropertyGrid::OnLabelEditorKeyPress),
999 NULL, this);
1000
1001 tc->SetFocus();
1002
1003 m_labelEditor = wxStaticCast(tc, wxTextCtrl);
1004 m_labelEditorProperty = selected;
1005 }
1006
1007 // -----------------------------------------------------------------------
1008
1009 void
1010 wxPropertyGrid::OnLabelEditorEnterPress( wxCommandEvent& WXUNUSED(event) )
1011 {
1012 DoEndLabelEdit(true);
1013 }
1014
1015 // -----------------------------------------------------------------------
1016
1017 void wxPropertyGrid::OnLabelEditorKeyPress( wxKeyEvent& event )
1018 {
1019 int keycode = event.GetKeyCode();
1020
1021 if ( keycode == WXK_ESCAPE )
1022 {
1023 DoEndLabelEdit(false);
1024 }
1025 else
1026 {
1027 event.Skip();
1028 }
1029 }
1030
1031 // -----------------------------------------------------------------------
1032
1033 void wxPropertyGrid::DoEndLabelEdit( bool commit, int selFlags )
1034 {
1035 if ( !m_labelEditor )
1036 return;
1037
1038 wxPGProperty* prop = m_labelEditorProperty;
1039 wxASSERT(prop);
1040
1041 if ( commit )
1042 {
1043 if ( !(selFlags & wxPG_SEL_DONT_SEND_EVENT) )
1044 {
1045 // wxPG_SEL_NOVALIDATE is passed correctly in selFlags
1046 if ( SendEvent( wxEVT_PG_LABEL_EDIT_ENDING,
1047 prop, NULL, selFlags,
1048 m_selColumn ) )
1049 return;
1050 }
1051
1052 wxString text = m_labelEditor->GetValue();
1053 wxPGCell* cell = NULL;
1054 if ( prop->HasCell(m_selColumn) )
1055 {
1056 cell = &prop->GetCell(m_selColumn);
1057 }
1058 else
1059 {
1060 if ( m_selColumn == 0 )
1061 prop->SetLabel(text);
1062 else
1063 cell = &prop->GetOrCreateCell(m_selColumn);
1064 }
1065
1066 if ( cell )
1067 cell->SetText(text);
1068 }
1069
1070 m_selColumn = 1;
1071
1072 DestroyEditorWnd(m_labelEditor);
1073 m_labelEditor = NULL;
1074 m_labelEditorProperty = NULL;
1075
1076 DrawItem(prop);
1077 }
1078
1079 // -----------------------------------------------------------------------
1080
1081 void wxPropertyGrid::SetExtraStyle( long exStyle )
1082 {
1083 if ( exStyle & wxPG_EX_ENABLE_TLP_TRACKING )
1084 OnTLPChanging(::wxGetTopLevelParent(this));
1085 else
1086 OnTLPChanging(NULL);
1087
1088 if ( exStyle & wxPG_EX_NATIVE_DOUBLE_BUFFERING )
1089 {
1090 #if defined(__WXMSW__)
1091
1092 /*
1093 // Don't use WS_EX_COMPOSITED just now.
1094 HWND hWnd;
1095
1096 if ( m_iFlags & wxPG_FL_IN_MANAGER )
1097 hWnd = (HWND)GetParent()->GetHWND();
1098 else
1099 hWnd = (HWND)GetHWND();
1100
1101 ::SetWindowLong( hWnd, GWL_EXSTYLE,
1102 ::GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_COMPOSITED );
1103 */
1104
1105 //#elif defined(__WXGTK20__)
1106 #endif
1107 // Only apply wxPG_EX_NATIVE_DOUBLE_BUFFERING if the window
1108 // truly was double-buffered.
1109 if ( !this->IsDoubleBuffered() )
1110 {
1111 exStyle &= ~(wxPG_EX_NATIVE_DOUBLE_BUFFERING);
1112 }
1113 else
1114 {
1115 #if wxPG_DOUBLE_BUFFER
1116 delete m_doubleBuffer;
1117 m_doubleBuffer = NULL;
1118 #endif
1119 }
1120 }
1121
1122 wxScrolledWindow::SetExtraStyle( exStyle );
1123
1124 if ( exStyle & wxPG_EX_INIT_NOCAT )
1125 m_pState->InitNonCatMode();
1126
1127 if ( exStyle & wxPG_EX_HELP_AS_TOOLTIPS )
1128 m_windowStyle |= wxPG_TOOLTIPS;
1129
1130 // Set global style
1131 wxPGGlobalVars->m_extraStyle = exStyle;
1132 }
1133
1134 // -----------------------------------------------------------------------
1135
1136 // returns the best acceptable minimal size
1137 wxSize wxPropertyGrid::DoGetBestSize() const
1138 {
1139 int lineHeight = wxMax(15, m_lineHeight);
1140
1141 // don't make the grid too tall (limit height to 10 items) but don't
1142 // make it too small neither
1143 int numLines = wxMin
1144 (
1145 wxMax(m_pState->m_properties->GetChildCount(), 3),
1146 10
1147 );
1148
1149 wxClientDC dc(const_cast<wxPropertyGrid *>(this));
1150 int width = m_marginWidth;
1151 for ( unsigned int i = 0; i < m_pState->m_colWidths.size(); i++ )
1152 {
1153 width += m_pState->GetColumnFitWidth(dc, m_pState->DoGetRoot(), i, true);
1154 }
1155
1156 const wxSize sz = wxSize(width, lineHeight*numLines + 40);
1157
1158 CacheBestSize(sz);
1159 return sz;
1160 }
1161
1162 // -----------------------------------------------------------------------
1163
1164 void wxPropertyGrid::OnTLPChanging( wxWindow* newTLP )
1165 {
1166 if ( newTLP == m_tlp )
1167 return;
1168
1169 wxLongLong currentTime = ::wxGetLocalTimeMillis();
1170
1171 //
1172 // Parent changed so let's redetermine and re-hook the
1173 // correct top-level window.
1174 if ( m_tlp )
1175 {
1176 m_tlp->Disconnect( wxEVT_CLOSE_WINDOW,
1177 wxCloseEventHandler(wxPropertyGrid::OnTLPClose),
1178 NULL, this );
1179 m_tlpClosed = m_tlp;
1180 m_tlpClosedTime = currentTime;
1181 }
1182
1183 if ( newTLP )
1184 {
1185 // Only accept new tlp if same one was not just dismissed.
1186 if ( newTLP != m_tlpClosed ||
1187 m_tlpClosedTime+250 < currentTime )
1188 {
1189 newTLP->Connect( wxEVT_CLOSE_WINDOW,
1190 wxCloseEventHandler(wxPropertyGrid::OnTLPClose),
1191 NULL, this );
1192 m_tlpClosed = NULL;
1193 }
1194 else
1195 {
1196 newTLP = NULL;
1197 }
1198 }
1199
1200 m_tlp = newTLP;
1201 }
1202
1203 // -----------------------------------------------------------------------
1204
1205 void wxPropertyGrid::OnTLPClose( wxCloseEvent& event )
1206 {
1207 // ClearSelection forces value validation/commit.
1208 if ( event.CanVeto() && !DoClearSelection() )
1209 {
1210 event.Veto();
1211 return;
1212 }
1213
1214 // Ok, it can close, set tlp pointer to NULL. Some other event
1215 // handler can of course veto the close, but our OnIdle() should
1216 // then be able to regain the tlp pointer.
1217 OnTLPChanging(NULL);
1218
1219 event.Skip();
1220 }
1221
1222 // -----------------------------------------------------------------------
1223
1224 bool wxPropertyGrid::Reparent( wxWindowBase *newParent )
1225 {
1226 OnTLPChanging((wxWindow*)newParent);
1227
1228 bool res = wxScrolledWindow::Reparent(newParent);
1229
1230 return res;
1231 }
1232
1233 // -----------------------------------------------------------------------
1234 // wxPropertyGrid Font and Colour Methods
1235 // -----------------------------------------------------------------------
1236
1237 void wxPropertyGrid::CalculateFontAndBitmapStuff( int vspacing )
1238 {
1239 int x = 0, y = 0;
1240
1241 m_captionFont = wxScrolledWindow::GetFont();
1242
1243 GetTextExtent(wxS("jG"), &x, &y, 0, 0, &m_captionFont);
1244 m_subgroup_extramargin = x + (x/2);
1245 m_fontHeight = y;
1246
1247 #if wxPG_USE_RENDERER_NATIVE
1248 m_iconWidth = wxPG_ICON_WIDTH;
1249 #elif wxPG_ICON_WIDTH
1250 // scale icon
1251 m_iconWidth = (m_fontHeight * wxPG_ICON_WIDTH) / 13;
1252 if ( m_iconWidth < 5 ) m_iconWidth = 5;
1253 else if ( !(m_iconWidth & 0x01) ) m_iconWidth++; // must be odd
1254
1255 #endif
1256
1257 m_gutterWidth = m_iconWidth / wxPG_GUTTER_DIV;
1258 if ( m_gutterWidth < wxPG_GUTTER_MIN )
1259 m_gutterWidth = wxPG_GUTTER_MIN;
1260
1261 int vdiv = 6;
1262 if ( vspacing <= 1 ) vdiv = 12;
1263 else if ( vspacing >= 3 ) vdiv = 3;
1264
1265 m_spacingy = m_fontHeight / vdiv;
1266 if ( m_spacingy < wxPG_YSPACING_MIN )
1267 m_spacingy = wxPG_YSPACING_MIN;
1268
1269 m_marginWidth = 0;
1270 if ( !(m_windowStyle & wxPG_HIDE_MARGIN) )
1271 m_marginWidth = m_gutterWidth*2 + m_iconWidth;
1272
1273 m_captionFont.SetWeight(wxBOLD);
1274 GetTextExtent(wxS("jG"), &x, &y, 0, 0, &m_captionFont);
1275
1276 m_lineHeight = m_fontHeight+(2*m_spacingy)+1;
1277
1278 // button spacing
1279 m_buttonSpacingY = (m_lineHeight - m_iconHeight) / 2;
1280 if ( m_buttonSpacingY < 0 ) m_buttonSpacingY = 0;
1281
1282 if ( m_pState )
1283 m_pState->CalculateFontAndBitmapStuff(vspacing);
1284
1285 if ( m_iFlags & wxPG_FL_INITIALIZED )
1286 RecalculateVirtualSize();
1287
1288 InvalidateBestSize();
1289 }
1290
1291 // -----------------------------------------------------------------------
1292
1293 void wxPropertyGrid::OnSysColourChanged( wxSysColourChangedEvent &WXUNUSED(event) )
1294 {
1295 RegainColours();
1296 Refresh();
1297 }
1298
1299 // -----------------------------------------------------------------------
1300
1301 static wxColour wxPGAdjustColour(const wxColour& src, int ra,
1302 int ga = 1000, int ba = 1000,
1303 bool forceDifferent = false)
1304 {
1305 if ( ga >= 1000 )
1306 ga = ra;
1307 if ( ba >= 1000 )
1308 ba = ra;
1309
1310 // Recursion guard (allow 2 max)
1311 static int isinside = 0;
1312 isinside++;
1313 wxCHECK_MSG( isinside < 3,
1314 *wxBLACK,
1315 wxT("wxPGAdjustColour should not be recursively called more than once") );
1316
1317 wxColour dst;
1318
1319 int r = src.Red();
1320 int g = src.Green();
1321 int b = src.Blue();
1322 int r2 = r + ra;
1323 if ( r2>255 ) r2 = 255;
1324 else if ( r2<0) r2 = 0;
1325 int g2 = g + ga;
1326 if ( g2>255 ) g2 = 255;
1327 else if ( g2<0) g2 = 0;
1328 int b2 = b + ba;
1329 if ( b2>255 ) b2 = 255;
1330 else if ( b2<0) b2 = 0;
1331
1332 // Make sure they are somewhat different
1333 if ( forceDifferent && (abs((r+g+b)-(r2+g2+b2)) < abs(ra/2)) )
1334 dst = wxPGAdjustColour(src,-(ra*2));
1335 else
1336 dst = wxColour(r2,g2,b2);
1337
1338 // Recursion guard (allow 2 max)
1339 isinside--;
1340
1341 return dst;
1342 }
1343
1344
1345 static int wxPGGetColAvg( const wxColour& col )
1346 {
1347 return (col.Red() + col.Green() + col.Blue()) / 3;
1348 }
1349
1350
1351 void wxPropertyGrid::RegainColours()
1352 {
1353 if ( !(m_coloursCustomized & 0x0002) )
1354 {
1355 wxColour col = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
1356
1357 // Make sure colour is dark enough
1358 #ifdef __WXGTK__
1359 int colDec = wxPGGetColAvg(col) - 230;
1360 #else
1361 int colDec = wxPGGetColAvg(col) - 200;
1362 #endif
1363 if ( colDec > 0 )
1364 m_colCapBack = wxPGAdjustColour(col,-colDec);
1365 else
1366 m_colCapBack = col;
1367 m_categoryDefaultCell.GetData()->SetBgCol(m_colCapBack);
1368 }
1369
1370 if ( !(m_coloursCustomized & 0x0001) )
1371 m_colMargin = m_colCapBack;
1372
1373 if ( !(m_coloursCustomized & 0x0004) )
1374 {
1375 #ifdef __WXGTK__
1376 int colDec = -90;
1377 #else
1378 int colDec = -72;
1379 #endif
1380 wxColour capForeCol = wxPGAdjustColour(m_colCapBack,colDec,5000,5000,true);
1381 m_colCapFore = capForeCol;
1382 m_categoryDefaultCell.GetData()->SetFgCol(capForeCol);
1383 }
1384
1385 if ( !(m_coloursCustomized & 0x0008) )
1386 {
1387 wxColour bgCol = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
1388 m_colPropBack = bgCol;
1389 m_propertyDefaultCell.GetData()->SetBgCol(bgCol);
1390 if ( !m_unspecifiedAppearance.GetBgCol().IsOk() )
1391 m_unspecifiedAppearance.SetBgCol(bgCol);
1392 }
1393
1394 if ( !(m_coloursCustomized & 0x0010) )
1395 {
1396 wxColour fgCol = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
1397 m_colPropFore = fgCol;
1398 m_propertyDefaultCell.GetData()->SetFgCol(fgCol);
1399 if ( !m_unspecifiedAppearance.GetFgCol().IsOk() )
1400 m_unspecifiedAppearance.SetFgCol(fgCol);
1401 }
1402
1403 if ( !(m_coloursCustomized & 0x0020) )
1404 m_colSelBack = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
1405
1406 if ( !(m_coloursCustomized & 0x0040) )
1407 m_colSelFore = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
1408
1409 if ( !(m_coloursCustomized & 0x0080) )
1410 m_colLine = m_colCapBack;
1411
1412 if ( !(m_coloursCustomized & 0x0100) )
1413 m_colDisPropFore = m_colCapFore;
1414
1415 m_colEmptySpace = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
1416 }
1417
1418 // -----------------------------------------------------------------------
1419
1420 void wxPropertyGrid::ResetColours()
1421 {
1422 m_coloursCustomized = 0;
1423
1424 RegainColours();
1425
1426 Refresh();
1427 }
1428
1429 // -----------------------------------------------------------------------
1430
1431 bool wxPropertyGrid::SetFont( const wxFont& font )
1432 {
1433 // Must disable active editor.
1434 DoClearSelection();
1435
1436 bool res = wxScrolledWindow::SetFont( font );
1437 if ( res && GetParent()) // may not have been Create()ed yet if SetFont called from SetWindowVariant
1438 {
1439 CalculateFontAndBitmapStuff( m_vspacing );
1440 Refresh();
1441 }
1442
1443 return res;
1444 }
1445
1446 // -----------------------------------------------------------------------
1447
1448 void wxPropertyGrid::SetLineColour( const wxColour& col )
1449 {
1450 m_colLine = col;
1451 m_coloursCustomized |= 0x80;
1452 Refresh();
1453 }
1454
1455 // -----------------------------------------------------------------------
1456
1457 void wxPropertyGrid::SetMarginColour( const wxColour& col )
1458 {
1459 m_colMargin = col;
1460 m_coloursCustomized |= 0x01;
1461 Refresh();
1462 }
1463
1464 // -----------------------------------------------------------------------
1465
1466 void wxPropertyGrid::SetCellBackgroundColour( const wxColour& col )
1467 {
1468 m_colPropBack = col;
1469 m_coloursCustomized |= 0x08;
1470
1471 m_propertyDefaultCell.GetData()->SetBgCol(col);
1472 m_unspecifiedAppearance.SetBgCol(col);
1473
1474 Refresh();
1475 }
1476
1477 // -----------------------------------------------------------------------
1478
1479 void wxPropertyGrid::SetCellTextColour( const wxColour& col )
1480 {
1481 m_colPropFore = col;
1482 m_coloursCustomized |= 0x10;
1483
1484 m_propertyDefaultCell.GetData()->SetFgCol(col);
1485 m_unspecifiedAppearance.SetFgCol(col);
1486
1487 Refresh();
1488 }
1489
1490 // -----------------------------------------------------------------------
1491
1492 void wxPropertyGrid::SetEmptySpaceColour( const wxColour& col )
1493 {
1494 m_colEmptySpace = col;
1495
1496 Refresh();
1497 }
1498
1499 // -----------------------------------------------------------------------
1500
1501 void wxPropertyGrid::SetCellDisabledTextColour( const wxColour& col )
1502 {
1503 m_colDisPropFore = col;
1504 m_coloursCustomized |= 0x100;
1505 Refresh();
1506 }
1507
1508 // -----------------------------------------------------------------------
1509
1510 void wxPropertyGrid::SetSelectionBackgroundColour( const wxColour& col )
1511 {
1512 m_colSelBack = col;
1513 m_coloursCustomized |= 0x20;
1514 Refresh();
1515 }
1516
1517 // -----------------------------------------------------------------------
1518
1519 void wxPropertyGrid::SetSelectionTextColour( const wxColour& col )
1520 {
1521 m_colSelFore = col;
1522 m_coloursCustomized |= 0x40;
1523 Refresh();
1524 }
1525
1526 // -----------------------------------------------------------------------
1527
1528 void wxPropertyGrid::SetCaptionBackgroundColour( const wxColour& col )
1529 {
1530 m_colCapBack = col;
1531 m_coloursCustomized |= 0x02;
1532
1533 m_categoryDefaultCell.GetData()->SetBgCol(col);
1534
1535 Refresh();
1536 }
1537
1538 // -----------------------------------------------------------------------
1539
1540 void wxPropertyGrid::SetCaptionTextColour( const wxColour& col )
1541 {
1542 m_colCapFore = col;
1543 m_coloursCustomized |= 0x04;
1544
1545 m_categoryDefaultCell.GetData()->SetFgCol(col);
1546
1547 Refresh();
1548 }
1549
1550 // -----------------------------------------------------------------------
1551 // wxPropertyGrid property adding and removal
1552 // -----------------------------------------------------------------------
1553
1554 void wxPropertyGrid::PrepareAfterItemsAdded()
1555 {
1556 if ( !m_pState || !m_pState->m_itemsAdded ) return;
1557
1558 m_pState->m_itemsAdded = 0;
1559
1560 if ( m_windowStyle & wxPG_AUTO_SORT )
1561 Sort(wxPG_SORT_TOP_LEVEL_ONLY);
1562
1563 RecalculateVirtualSize();
1564 }
1565
1566 // -----------------------------------------------------------------------
1567 // wxPropertyGrid property operations
1568 // -----------------------------------------------------------------------
1569
1570 bool wxPropertyGrid::EnsureVisible( wxPGPropArg id )
1571 {
1572 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
1573
1574 Update();
1575
1576 bool changed = false;
1577
1578 // Is it inside collapsed section?
1579 if ( !p->IsVisible() )
1580 {
1581 // expand parents
1582 wxPGProperty* parent = p->GetParent();
1583 wxPGProperty* grandparent = parent->GetParent();
1584
1585 if ( grandparent && grandparent != m_pState->m_properties )
1586 Expand( grandparent );
1587
1588 Expand( parent );
1589 changed = true;
1590 }
1591
1592 // Need to scroll?
1593 int vx, vy;
1594 GetViewStart(&vx,&vy);
1595 vy*=wxPG_PIXELS_PER_UNIT;
1596
1597 int y = p->GetY();
1598
1599 if ( y < vy )
1600 {
1601 Scroll(vx, y/wxPG_PIXELS_PER_UNIT );
1602 m_iFlags |= wxPG_FL_SCROLLED;
1603 changed = true;
1604 }
1605 else if ( (y+m_lineHeight) > (vy+m_height) )
1606 {
1607 Scroll(vx, (y-m_height+(m_lineHeight*2))/wxPG_PIXELS_PER_UNIT );
1608 m_iFlags |= wxPG_FL_SCROLLED;
1609 changed = true;
1610 }
1611
1612 if ( changed )
1613 DrawItems( p, p );
1614
1615 return changed;
1616 }
1617
1618 // -----------------------------------------------------------------------
1619 // wxPropertyGrid helper methods called by properties
1620 // -----------------------------------------------------------------------
1621
1622 // Control font changer helper.
1623 void wxPropertyGrid::SetCurControlBoldFont()
1624 {
1625 wxASSERT( m_wndEditor );
1626 m_wndEditor->SetFont( m_captionFont );
1627 }
1628
1629 // -----------------------------------------------------------------------
1630
1631 wxPoint wxPropertyGrid::GetGoodEditorDialogPosition( wxPGProperty* p,
1632 const wxSize& sz )
1633 {
1634 #if wxPG_SMALL_SCREEN
1635 // On small-screen devices, always show dialogs with default position and size.
1636 return wxDefaultPosition;
1637 #else
1638 int splitterX = GetSplitterPosition();
1639 int x = splitterX;
1640 int y = p->GetY();
1641
1642 wxCHECK_MSG( y >= 0, wxPoint(-1,-1), wxT("invalid y?") );
1643
1644 ImprovedClientToScreen( &x, &y );
1645
1646 int sw = wxSystemSettings::GetMetric( ::wxSYS_SCREEN_X );
1647 int sh = wxSystemSettings::GetMetric( ::wxSYS_SCREEN_Y );
1648
1649 int new_x;
1650 int new_y;
1651
1652 if ( x > (sw/2) )
1653 // left
1654 new_x = x + (m_width-splitterX) - sz.x;
1655 else
1656 // right
1657 new_x = x;
1658
1659 if ( y > (sh/2) )
1660 // above
1661 new_y = y - sz.y;
1662 else
1663 // below
1664 new_y = y + m_lineHeight;
1665
1666 return wxPoint(new_x,new_y);
1667 #endif
1668 }
1669
1670 // -----------------------------------------------------------------------
1671
1672 wxString& wxPropertyGrid::ExpandEscapeSequences( wxString& dst_str, wxString& src_str )
1673 {
1674 if ( src_str.length() == 0 )
1675 {
1676 dst_str = src_str;
1677 return src_str;
1678 }
1679
1680 bool prev_is_slash = false;
1681
1682 wxString::const_iterator i = src_str.begin();
1683
1684 dst_str.clear();
1685
1686 for ( ; i != src_str.end(); ++i )
1687 {
1688 wxUniChar a = *i;
1689
1690 if ( a != wxS('\\') )
1691 {
1692 if ( !prev_is_slash )
1693 {
1694 dst_str << a;
1695 }
1696 else
1697 {
1698 if ( a == wxS('n') )
1699 {
1700 #ifdef __WXMSW__
1701 dst_str << wxS('\n');
1702 #else
1703 dst_str << wxS('\n');
1704 #endif
1705 }
1706 else if ( a == wxS('t') )
1707 dst_str << wxS('\t');
1708 else
1709 dst_str << a;
1710 }
1711 prev_is_slash = false;
1712 }
1713 else
1714 {
1715 if ( prev_is_slash )
1716 {
1717 dst_str << wxS('\\');
1718 prev_is_slash = false;
1719 }
1720 else
1721 {
1722 prev_is_slash = true;
1723 }
1724 }
1725 }
1726 return dst_str;
1727 }
1728
1729 // -----------------------------------------------------------------------
1730
1731 wxString& wxPropertyGrid::CreateEscapeSequences( wxString& dst_str, wxString& src_str )
1732 {
1733 if ( src_str.length() == 0 )
1734 {
1735 dst_str = src_str;
1736 return src_str;
1737 }
1738
1739 wxString::const_iterator i = src_str.begin();
1740 wxUniChar prev_a = wxS('\0');
1741
1742 dst_str.clear();
1743
1744 for ( ; i != src_str.end(); ++i )
1745 {
1746 wxChar a = *i;
1747
1748 if ( a >= wxS(' ') )
1749 {
1750 // This surely is not something that requires an escape sequence.
1751 dst_str << a;
1752 }
1753 else
1754 {
1755 // This might need...
1756 if ( a == wxS('\r') )
1757 {
1758 // DOS style line end.
1759 // Already taken care below
1760 }
1761 else if ( a == wxS('\n') )
1762 // UNIX style line end.
1763 dst_str << wxS("\\n");
1764 else if ( a == wxS('\t') )
1765 // Tab.
1766 dst_str << wxS('\t');
1767 else
1768 {
1769 //wxLogDebug(wxT("WARNING: Could not create escape sequence for character #%i"),(int)a);
1770 dst_str << a;
1771 }
1772 }
1773
1774 prev_a = a;
1775 }
1776 return dst_str;
1777 }
1778
1779 // -----------------------------------------------------------------------
1780
1781 wxPGProperty* wxPropertyGrid::DoGetItemAtY( int y ) const
1782 {
1783 // Outside?
1784 if ( y < 0 )
1785 return NULL;
1786
1787 unsigned int a = 0;
1788 return m_pState->m_properties->GetItemAtY(y, m_lineHeight, &a);
1789 }
1790
1791 // -----------------------------------------------------------------------
1792 // wxPropertyGrid graphics related methods
1793 // -----------------------------------------------------------------------
1794
1795 void wxPropertyGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
1796 {
1797 wxPaintDC dc(this);
1798 PrepareDC(dc);
1799
1800 // Don't paint after destruction has begun
1801 if ( !HasInternalFlag(wxPG_FL_INITIALIZED) )
1802 return;
1803
1804 // Find out where the window is scrolled to
1805 int vx,vy; // Top left corner of client
1806 GetViewStart(&vx,&vy);
1807 vy *= wxPG_PIXELS_PER_UNIT;
1808
1809 // Update everything inside the box
1810 wxRect r = GetUpdateRegion().GetBox();
1811
1812 r.y += vy;
1813
1814 // FIXME: This is just a workaround for a bug that causes splitters not
1815 // to paint when other windows are being dragged over the grid.
1816 r.x = 0;
1817 r.width = GetClientSize().x;
1818
1819 // Repaint this rectangle
1820 DrawItems( dc, r.y, r.y + r.height, &r );
1821
1822 // We assume that the size set when grid is shown
1823 // is what is desired.
1824 SetInternalFlag(wxPG_FL_GOOD_SIZE_SET);
1825 }
1826
1827 // -----------------------------------------------------------------------
1828
1829 void wxPropertyGrid::DrawExpanderButton( wxDC& dc, const wxRect& rect,
1830 wxPGProperty* property ) const
1831 {
1832 // Prepare rectangle to be used
1833 wxRect r(rect);
1834 r.x += m_gutterWidth; r.y += m_buttonSpacingY;
1835 r.width = m_iconWidth; r.height = m_iconHeight;
1836
1837 #if (wxPG_USE_RENDERER_NATIVE)
1838 //
1839 #elif wxPG_ICON_WIDTH
1840 // Drawing expand/collapse button manually
1841 dc.SetPen(m_colPropFore);
1842 if ( property->IsCategory() )
1843 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1844 else
1845 dc.SetBrush(m_colPropBack);
1846
1847 dc.DrawRectangle( r );
1848 int _y = r.y+(m_iconWidth/2);
1849 dc.DrawLine(r.x+2,_y,r.x+m_iconWidth-2,_y);
1850 #else
1851 wxBitmap* bmp;
1852 #endif
1853
1854 if ( property->IsExpanded() )
1855 {
1856 // wxRenderer functions are non-mutating in nature, so it
1857 // should be safe to cast "const wxPropertyGrid*" to "wxWindow*".
1858 // Hopefully this does not cause problems.
1859 #if (wxPG_USE_RENDERER_NATIVE)
1860 wxRendererNative::Get().DrawTreeItemButton(
1861 (wxWindow*)this,
1862 dc,
1863 r,
1864 wxCONTROL_EXPANDED
1865 );
1866 #elif wxPG_ICON_WIDTH
1867 //
1868 #else
1869 bmp = m_collbmp;
1870 #endif
1871
1872 }
1873 else
1874 {
1875 #if (wxPG_USE_RENDERER_NATIVE)
1876 wxRendererNative::Get().DrawTreeItemButton(
1877 (wxWindow*)this,
1878 dc,
1879 r,
1880 0
1881 );
1882 #elif wxPG_ICON_WIDTH
1883 int _x = r.x+(m_iconWidth/2);
1884 dc.DrawLine(_x,r.y+2,_x,r.y+m_iconWidth-2);
1885 #else
1886 bmp = m_expandbmp;
1887 #endif
1888 }
1889
1890 #if (wxPG_USE_RENDERER_NATIVE)
1891 //
1892 #elif wxPG_ICON_WIDTH
1893 //
1894 #else
1895 dc.DrawBitmap( *bmp, r.x, r.y, true );
1896 #endif
1897 }
1898
1899 // -----------------------------------------------------------------------
1900
1901 //
1902 // This is the one called by OnPaint event handler and others.
1903 // topy and bottomy are already unscrolled (ie. physical)
1904 //
1905 void wxPropertyGrid::DrawItems( wxDC& dc,
1906 unsigned int topItemY,
1907 unsigned int bottomItemY,
1908 const wxRect* drawRect )
1909 {
1910 if ( m_frozen ||
1911 m_height < 1 ||
1912 bottomItemY < topItemY ||
1913 !m_pState )
1914 return;
1915
1916 m_pState->EnsureVirtualHeight();
1917
1918 wxRect tempDrawRect;
1919 if ( !drawRect )
1920 {
1921 tempDrawRect = wxRect(0, topItemY,
1922 m_pState->m_width,
1923 bottomItemY);
1924 drawRect = &tempDrawRect;
1925 }
1926
1927 // items added check
1928 if ( m_pState->m_itemsAdded ) PrepareAfterItemsAdded();
1929
1930 int paintFinishY = 0;
1931
1932 if ( m_pState->m_properties->GetChildCount() > 0 )
1933 {
1934 wxDC* dcPtr = &dc;
1935 bool isBuffered = false;
1936
1937 #if wxPG_DOUBLE_BUFFER
1938 wxMemoryDC* bufferDC = NULL;
1939
1940 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
1941 {
1942 if ( !m_doubleBuffer )
1943 {
1944 paintFinishY = drawRect->y;
1945 dcPtr = NULL;
1946 }
1947 else
1948 {
1949 bufferDC = new wxMemoryDC();
1950
1951 // If nothing was changed, then just copy from double-buffer
1952 bufferDC->SelectObject( *m_doubleBuffer );
1953 dcPtr = bufferDC;
1954
1955 isBuffered = true;
1956 }
1957 }
1958 #endif
1959
1960 if ( dcPtr )
1961 {
1962 dc.SetClippingRegion( *drawRect );
1963 paintFinishY = DoDrawItems( *dcPtr, drawRect, isBuffered );
1964 int drawBottomY = drawRect->y + drawRect->height;
1965
1966 // Clear area beyond last painted property
1967 if ( paintFinishY < drawBottomY )
1968 {
1969 dcPtr->SetPen(m_colEmptySpace);
1970 dcPtr->SetBrush(m_colEmptySpace);
1971 dcPtr->DrawRectangle(0, paintFinishY,
1972 m_width,
1973 drawBottomY );
1974 }
1975
1976 dc.DestroyClippingRegion();
1977 }
1978
1979 #if wxPG_DOUBLE_BUFFER
1980 if ( bufferDC )
1981 {
1982 dc.Blit( drawRect->x, drawRect->y, drawRect->width,
1983 drawRect->height,
1984 bufferDC, 0, 0, wxCOPY );
1985 delete bufferDC;
1986 }
1987 #endif
1988 }
1989 else
1990 {
1991 // Just clear the area
1992 dc.SetPen(m_colEmptySpace);
1993 dc.SetBrush(m_colEmptySpace);
1994 dc.DrawRectangle(*drawRect);
1995 }
1996 }
1997
1998 // -----------------------------------------------------------------------
1999
2000 int wxPropertyGrid::DoDrawItems( wxDC& dc,
2001 const wxRect* drawRect,
2002 bool isBuffered ) const
2003 {
2004 const wxPGProperty* firstItem;
2005 const wxPGProperty* lastItem;
2006
2007 firstItem = DoGetItemAtY(drawRect->y);
2008 lastItem = DoGetItemAtY(drawRect->y+drawRect->height-1);
2009
2010 if ( !lastItem )
2011 lastItem = GetLastItem( wxPG_ITERATE_VISIBLE );
2012
2013 if ( m_frozen || m_height < 1 || firstItem == NULL )
2014 return drawRect->y;
2015
2016 wxCHECK_MSG( !m_pState->m_itemsAdded, drawRect->y,
2017 "no items added" );
2018 wxASSERT( m_pState->m_properties->GetChildCount() );
2019
2020 int lh = m_lineHeight;
2021
2022 int firstItemTopY;
2023 int lastItemBottomY;
2024
2025 firstItemTopY = drawRect->y;
2026 lastItemBottomY = drawRect->y + drawRect->height;
2027
2028 // Align y coordinates to item boundaries
2029 firstItemTopY -= firstItemTopY % lh;
2030 lastItemBottomY += lh - (lastItemBottomY % lh);
2031 lastItemBottomY -= 1;
2032
2033 // Entire range outside scrolled, visible area?
2034 if ( firstItemTopY >= (int)m_pState->GetVirtualHeight() ||
2035 lastItemBottomY <= 0 )
2036 return drawRect->y;
2037
2038 wxCHECK_MSG( firstItemTopY < lastItemBottomY,
2039 drawRect->y,
2040 "invalid y values" );
2041
2042 /*
2043 wxLogDebug(" -> DoDrawItems ( \"%s\" -> \"%s\"
2044 "height=%i (ch=%i), drawRect = 0x%lX )",
2045 firstItem->GetLabel().c_str(),
2046 lastItem->GetLabel().c_str(),
2047 (int)(lastItemBottomY - firstItemTopY),
2048 (int)m_height,
2049 (unsigned long)drawRect );
2050 */
2051
2052 wxRect r;
2053
2054 long windowStyle = m_windowStyle;
2055
2056 int xRelMod = 0;
2057
2058 //
2059 // With wxPG_DOUBLE_BUFFER, do double buffering
2060 // - buffer's y = 0, so align drawRect and coordinates to that
2061 //
2062 #if wxPG_DOUBLE_BUFFER
2063 int yRelMod = 0;
2064
2065 wxRect cr2;
2066
2067 if ( isBuffered )
2068 {
2069 xRelMod = drawRect->x;
2070 yRelMod = drawRect->y;
2071
2072 //
2073 // drawRect conversion
2074 cr2 = *drawRect;
2075 cr2.x -= xRelMod;
2076 cr2.y -= yRelMod;
2077 drawRect = &cr2;
2078 firstItemTopY -= yRelMod;
2079 lastItemBottomY -= yRelMod;
2080 }
2081 #else
2082 wxUnusedVar(isBuffered);
2083 #endif
2084
2085 int x = m_marginWidth - xRelMod;
2086
2087 wxFont normalFont = GetFont();
2088
2089 bool reallyFocused = (m_iFlags & wxPG_FL_FOCUSED) != 0;
2090
2091 bool isPgEnabled = IsEnabled();
2092
2093 //
2094 // Prepare some pens and brushes that are often changed to.
2095 //
2096
2097 wxBrush marginBrush(m_colMargin);
2098 wxPen marginPen(m_colMargin);
2099 wxBrush capbgbrush(m_colCapBack,wxSOLID);
2100 wxPen linepen(m_colLine,1,wxSOLID);
2101
2102 wxColour selBackCol;
2103 if ( isPgEnabled )
2104 selBackCol = m_colSelBack;
2105 else
2106 selBackCol = m_colMargin;
2107
2108 // pen that has same colour as text
2109 wxPen outlinepen(m_colPropFore,1,wxSOLID);
2110
2111 //
2112 // Clear margin with background colour
2113 //
2114 dc.SetBrush( marginBrush );
2115 if ( !(windowStyle & wxPG_HIDE_MARGIN) )
2116 {
2117 dc.SetPen( *wxTRANSPARENT_PEN );
2118 dc.DrawRectangle(-1-xRelMod,firstItemTopY-1,x+2,lastItemBottomY-firstItemTopY+2);
2119 }
2120
2121 const wxPGProperty* firstSelected = GetSelection();
2122 const wxPropertyGridPageState* state = m_pState;
2123
2124 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2125 bool wasSelectedPainted = false;
2126 #endif
2127
2128 // TODO: Only render columns that are within clipping region.
2129
2130 dc.SetFont(normalFont);
2131
2132 wxPropertyGridConstIterator it( state, wxPG_ITERATE_VISIBLE, firstItem );
2133 int endScanBottomY = lastItemBottomY + lh;
2134 int y = firstItemTopY;
2135
2136 //
2137 // Pregenerate list of visible properties.
2138 wxArrayPGProperty visPropArray;
2139 visPropArray.reserve((m_height/m_lineHeight)+6);
2140
2141 for ( ; !it.AtEnd(); it.Next() )
2142 {
2143 const wxPGProperty* p = *it;
2144
2145 if ( !p->HasFlag(wxPG_PROP_HIDDEN) )
2146 {
2147 visPropArray.push_back((wxPGProperty*)p);
2148
2149 if ( y > endScanBottomY )
2150 break;
2151
2152 y += lh;
2153 }
2154 }
2155
2156 visPropArray.push_back(NULL);
2157
2158 wxPGProperty* nextP = visPropArray[0];
2159
2160 int gridWidth = state->m_width;
2161
2162 y = firstItemTopY;
2163 for ( unsigned int arrInd=1;
2164 nextP && y <= lastItemBottomY;
2165 arrInd++ )
2166 {
2167 wxPGProperty* p = nextP;
2168 nextP = visPropArray[arrInd];
2169
2170 int rowHeight = m_fontHeight+(m_spacingy*2)+1;
2171 int textMarginHere = x;
2172 int renderFlags = 0;
2173
2174 int greyDepth = m_marginWidth;
2175 if ( !(windowStyle & wxPG_HIDE_CATEGORIES) )
2176 greyDepth = (((int)p->m_depthBgCol)-1) * m_subgroup_extramargin + m_marginWidth;
2177
2178 int greyDepthX = greyDepth - xRelMod;
2179
2180 // Use basic depth if in non-categoric mode and parent is base array.
2181 if ( !(windowStyle & wxPG_HIDE_CATEGORIES) || p->GetParent() != m_pState->m_properties )
2182 {
2183 textMarginHere += ((unsigned int)((p->m_depth-1)*m_subgroup_extramargin));
2184 }
2185
2186 // Paint margin area
2187 dc.SetBrush(marginBrush);
2188 dc.SetPen(marginPen);
2189 dc.DrawRectangle( -xRelMod, y, greyDepth, lh );
2190
2191 dc.SetPen( linepen );
2192
2193 int y2 = y + lh;
2194
2195 #ifdef __WXMSW__
2196 // Margin Edge
2197 // Modified by JACS to not draw a margin if wxPG_HIDE_MARGIN is specified, since it
2198 // looks better, at least under Windows when we have a themed border (the themed-window-specific
2199 // whitespace between the real border and the propgrid margin exacerbates the double-border look).
2200
2201 // Is this or its parent themed?
2202 bool suppressMarginEdge = (GetWindowStyle() & wxPG_HIDE_MARGIN) &&
2203 (((GetWindowStyle() & wxBORDER_MASK) == wxBORDER_THEME) ||
2204 (((GetWindowStyle() & wxBORDER_MASK) == wxBORDER_NONE) && ((GetParent()->GetWindowStyle() & wxBORDER_MASK) == wxBORDER_THEME)));
2205 #else
2206 bool suppressMarginEdge = false;
2207 #endif
2208 if (!suppressMarginEdge)
2209 dc.DrawLine( greyDepthX, y, greyDepthX, y2 );
2210 else
2211 {
2212 // Blank out the margin edge
2213 dc.SetPen(wxPen(GetBackgroundColour()));
2214 dc.DrawLine( greyDepthX, y, greyDepthX, y2 );
2215 dc.SetPen( linepen );
2216 }
2217
2218 // Splitters
2219 unsigned int si;
2220 int sx = x;
2221
2222 for ( si=0; si<state->m_colWidths.size(); si++ )
2223 {
2224 sx += state->m_colWidths[si];
2225 dc.DrawLine( sx, y, sx, y2 );
2226 }
2227
2228 // Horizontal Line, below
2229 // (not if both this and next is category caption)
2230 if ( p->IsCategory() &&
2231 nextP && nextP->IsCategory() )
2232 dc.SetPen(m_colCapBack);
2233
2234 dc.DrawLine( greyDepthX, y2-1, gridWidth-xRelMod, y2-1 );
2235
2236 //
2237 // Need to override row colours?
2238 wxColour rowFgCol;
2239 wxColour rowBgCol;
2240
2241 bool isSelected = state->DoIsPropertySelected(p);
2242
2243 if ( !isSelected )
2244 {
2245 // Disabled may get different colour.
2246 if ( !p->IsEnabled() )
2247 {
2248 renderFlags |= wxPGCellRenderer::Disabled |
2249 wxPGCellRenderer::DontUseCellFgCol;
2250 rowFgCol = m_colDisPropFore;
2251 }
2252 }
2253 else
2254 {
2255 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2256 if ( p == firstSelected )
2257 wasSelectedPainted = true;
2258 #endif
2259
2260 renderFlags |= wxPGCellRenderer::Selected;
2261
2262 if ( !p->IsCategory() )
2263 {
2264 renderFlags |= wxPGCellRenderer::DontUseCellFgCol |
2265 wxPGCellRenderer::DontUseCellBgCol;
2266
2267 if ( reallyFocused && p == firstSelected )
2268 {
2269 rowFgCol = m_colSelFore;
2270 rowBgCol = selBackCol;
2271 }
2272 else if ( isPgEnabled )
2273 {
2274 rowFgCol = m_colPropFore;
2275 if ( p == firstSelected )
2276 rowBgCol = m_colMargin;
2277 else
2278 rowBgCol = selBackCol;
2279 }
2280 else
2281 {
2282 rowFgCol = m_colDisPropFore;
2283 rowBgCol = selBackCol;
2284 }
2285 }
2286 }
2287
2288 wxBrush rowBgBrush;
2289
2290 if ( rowBgCol.IsOk() )
2291 rowBgBrush = wxBrush(rowBgCol);
2292
2293 if ( HasInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL) )
2294 renderFlags = renderFlags & ~wxPGCellRenderer::DontUseCellColours;
2295
2296 //
2297 // Fill additional margin area with background colour of first cell
2298 if ( greyDepthX < textMarginHere )
2299 {
2300 if ( !(renderFlags & wxPGCellRenderer::DontUseCellBgCol) )
2301 {
2302 wxPGCell& cell = p->GetCell(0);
2303 rowBgCol = cell.GetBgCol();
2304 rowBgBrush = wxBrush(rowBgCol);
2305 }
2306 dc.SetBrush(rowBgBrush);
2307 dc.SetPen(rowBgCol);
2308 dc.DrawRectangle(greyDepthX+1, y,
2309 textMarginHere-greyDepthX, lh-1);
2310 }
2311
2312 bool fontChanged = false;
2313
2314 // Expander button rectangle
2315 wxRect butRect( ((p->m_depth - 1) * m_subgroup_extramargin) - xRelMod,
2316 y,
2317 m_marginWidth,
2318 lh );
2319
2320 if ( p->IsCategory() )
2321 {
2322 // Captions have their cell areas merged as one
2323 dc.SetFont(m_captionFont);
2324 fontChanged = true;
2325 wxRect cellRect(greyDepthX, y, gridWidth - greyDepth + 2, rowHeight-1 );
2326
2327 if ( renderFlags & wxPGCellRenderer::DontUseCellBgCol )
2328 {
2329 dc.SetBrush(rowBgBrush);
2330 dc.SetPen(rowBgCol);
2331 }
2332
2333 if ( renderFlags & wxPGCellRenderer::DontUseCellFgCol )
2334 {
2335 dc.SetTextForeground(rowFgCol);
2336 }
2337
2338 wxPGCellRenderer* renderer = p->GetCellRenderer(0);
2339 renderer->Render( dc, cellRect, this, p, 0, -1, renderFlags );
2340
2341 // Tree Item Button
2342 if ( !HasFlag(wxPG_HIDE_MARGIN) && p->HasVisibleChildren() )
2343 DrawExpanderButton( dc, butRect, p );
2344 }
2345 else
2346 {
2347 // Fine tune button rectangle to actually fit the cell
2348 if ( butRect.x > 0 )
2349 butRect.x += IN_CELL_EXPANDER_BUTTON_X_ADJUST;
2350
2351 if ( p->m_flags & wxPG_PROP_MODIFIED && (windowStyle & wxPG_BOLD_MODIFIED) )
2352 {
2353 dc.SetFont(m_captionFont);
2354 fontChanged = true;
2355 }
2356
2357 unsigned int ci;
2358 int cellX = x + 1;
2359 int nextCellWidth = state->m_colWidths[0] -
2360 (greyDepthX - m_marginWidth);
2361 wxRect cellRect(greyDepthX+1, y, 0, rowHeight-1);
2362 int textXAdd = textMarginHere - greyDepthX;
2363
2364 for ( ci=0; ci<state->m_colWidths.size(); ci++ )
2365 {
2366 cellRect.width = nextCellWidth - 1;
2367
2368 wxWindow* cellEditor = NULL;
2369 int cellRenderFlags = renderFlags;
2370
2371 // Tree Item Button (must be drawn before clipping is set up)
2372 if ( ci == 0 && !HasFlag(wxPG_HIDE_MARGIN) && p->HasVisibleChildren() )
2373 DrawExpanderButton( dc, butRect, p );
2374
2375 // Background
2376 if ( isSelected && (ci == 1 || ci == m_selColumn) )
2377 {
2378 if ( p == firstSelected )
2379 {
2380 if ( ci == 1 && m_wndEditor )
2381 cellEditor = m_wndEditor;
2382 else if ( ci == m_selColumn && m_labelEditor )
2383 cellEditor = m_labelEditor;
2384 }
2385
2386 if ( cellEditor )
2387 {
2388 wxColour editorBgCol =
2389 cellEditor->GetBackgroundColour();
2390 dc.SetBrush(editorBgCol);
2391 dc.SetPen(editorBgCol);
2392 dc.SetTextForeground(m_colPropFore);
2393 dc.DrawRectangle(cellRect);
2394
2395 if ( m_dragStatus != 0 ||
2396 (m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE) )
2397 cellEditor = NULL;
2398 }
2399 else
2400 {
2401 dc.SetBrush(m_colPropBack);
2402 dc.SetPen(m_colPropBack);
2403 dc.SetTextForeground(m_colDisPropFore);
2404 if ( p->IsEnabled() )
2405 dc.SetTextForeground(rowFgCol);
2406 else
2407 dc.SetTextForeground(m_colDisPropFore);
2408 }
2409 }
2410 else
2411 {
2412 if ( renderFlags & wxPGCellRenderer::DontUseCellBgCol )
2413 {
2414 dc.SetBrush(rowBgBrush);
2415 dc.SetPen(rowBgCol);
2416 }
2417
2418 if ( renderFlags & wxPGCellRenderer::DontUseCellFgCol )
2419 {
2420 dc.SetTextForeground(rowFgCol);
2421 }
2422 }
2423
2424 dc.SetClippingRegion(cellRect);
2425
2426 cellRect.x += textXAdd;
2427 cellRect.width -= textXAdd;
2428
2429 // Foreground
2430 if ( !cellEditor )
2431 {
2432 wxPGCellRenderer* renderer;
2433 int cmnVal = p->GetCommonValue();
2434 if ( cmnVal == -1 || ci != 1 )
2435 {
2436 renderer = p->GetCellRenderer(ci);
2437 renderer->Render( dc, cellRect, this, p, ci, -1,
2438 cellRenderFlags );
2439 }
2440 else
2441 {
2442 renderer = GetCommonValue(cmnVal)->GetRenderer();
2443 renderer->Render( dc, cellRect, this, p, ci, -1,
2444 cellRenderFlags );
2445 }
2446 }
2447
2448 cellX += state->m_colWidths[ci];
2449 if ( ci < (state->m_colWidths.size()-1) )
2450 nextCellWidth = state->m_colWidths[ci+1];
2451 cellRect.x = cellX;
2452 dc.DestroyClippingRegion(); // Is this really necessary?
2453 textXAdd = 0;
2454 }
2455 }
2456
2457 if ( fontChanged )
2458 dc.SetFont(normalFont);
2459
2460 y += rowHeight;
2461 }
2462
2463 // Refresh editor controls (seems not needed on msw)
2464 // NOTE: This code is mandatory for GTK!
2465 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2466 if ( wasSelectedPainted )
2467 {
2468 if ( m_wndEditor )
2469 m_wndEditor->Refresh();
2470 if ( m_wndEditor2 )
2471 m_wndEditor2->Refresh();
2472 }
2473 #endif
2474
2475 return y;
2476 }
2477
2478 // -----------------------------------------------------------------------
2479
2480 wxRect wxPropertyGrid::GetPropertyRect( const wxPGProperty* p1, const wxPGProperty* p2 ) const
2481 {
2482 wxRect r;
2483
2484 if ( m_width < 10 || m_height < 10 ||
2485 !m_pState->m_properties->GetChildCount() ||
2486 p1 == NULL )
2487 return wxRect(0,0,0,0);
2488
2489 int vy = 0;
2490
2491 //
2492 // Return rect which encloses the given property range
2493
2494 int visTop = p1->GetY();
2495 int visBottom;
2496 if ( p2 )
2497 visBottom = p2->GetY() + m_lineHeight;
2498 else
2499 visBottom = m_height + visTop;
2500
2501 // If seleced property is inside the range, we'll extend the range to include
2502 // control's size.
2503 wxPGProperty* selected = GetSelection();
2504 if ( selected )
2505 {
2506 int selectedY = selected->GetY();
2507 if ( selectedY >= visTop && selectedY < visBottom )
2508 {
2509 wxWindow* editor = GetEditorControl();
2510 if ( editor )
2511 {
2512 int visBottom2 = selectedY + editor->GetSize().y;
2513 if ( visBottom2 > visBottom )
2514 visBottom = visBottom2;
2515 }
2516 }
2517 }
2518
2519 return wxRect(0,visTop-vy,m_pState->m_width,visBottom-visTop);
2520 }
2521
2522 // -----------------------------------------------------------------------
2523
2524 void wxPropertyGrid::DrawItems( const wxPGProperty* p1, const wxPGProperty* p2 )
2525 {
2526 if ( m_frozen )
2527 return;
2528
2529 if ( m_pState->m_itemsAdded )
2530 PrepareAfterItemsAdded();
2531
2532 wxRect r = GetPropertyRect(p1, p2);
2533 if ( r.width > 0 )
2534 {
2535 RefreshRect(r);
2536 }
2537 }
2538
2539 // -----------------------------------------------------------------------
2540
2541 void wxPropertyGrid::RefreshProperty( wxPGProperty* p )
2542 {
2543 if ( m_pState->DoIsPropertySelected(p) )
2544 {
2545 // NB: We must copy the selection.
2546 wxArrayPGProperty selection = m_pState->m_selection;
2547 DoSetSelection(selection, wxPG_SEL_FORCE);
2548 }
2549
2550 DrawItemAndChildren(p);
2551 }
2552
2553 // -----------------------------------------------------------------------
2554
2555 void wxPropertyGrid::DrawItemAndValueRelated( wxPGProperty* p )
2556 {
2557 if ( m_frozen )
2558 return;
2559
2560 // Draw item, children, and parent too, if it is not category
2561 wxPGProperty* parent = p->GetParent();
2562
2563 while ( parent &&
2564 !parent->IsCategory() &&
2565 parent->GetParent() )
2566 {
2567 DrawItem(parent);
2568 parent = parent->GetParent();
2569 }
2570
2571 DrawItemAndChildren(p);
2572 }
2573
2574 void wxPropertyGrid::DrawItemAndChildren( wxPGProperty* p )
2575 {
2576 wxCHECK_RET( p, wxT("invalid property id") );
2577
2578 // Do not draw if in non-visible page
2579 if ( p->GetParentState() != m_pState )
2580 return;
2581
2582 // do not draw a single item if multiple pending
2583 if ( m_pState->m_itemsAdded || m_frozen )
2584 return;
2585
2586 // Update child control.
2587 wxPGProperty* selected = GetSelection();
2588 if ( selected && selected->GetParent() == p )
2589 RefreshEditor();
2590
2591 const wxPGProperty* lastDrawn = p->GetLastVisibleSubItem();
2592
2593 DrawItems(p, lastDrawn);
2594 }
2595
2596 // -----------------------------------------------------------------------
2597
2598 void wxPropertyGrid::Refresh( bool WXUNUSED(eraseBackground),
2599 const wxRect *rect )
2600 {
2601 PrepareAfterItemsAdded();
2602
2603 wxWindow::Refresh(false, rect);
2604
2605 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2606 // I think this really helps only GTK+1.2
2607 if ( m_wndEditor ) m_wndEditor->Refresh();
2608 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
2609 #endif
2610 }
2611
2612 // -----------------------------------------------------------------------
2613 // wxPropertyGrid global operations
2614 // -----------------------------------------------------------------------
2615
2616 void wxPropertyGrid::Clear()
2617 {
2618 m_pState->DoClear();
2619
2620 m_propHover = NULL;
2621
2622 m_prevVY = 0;
2623
2624 RecalculateVirtualSize();
2625
2626 // Need to clear some area at the end
2627 if ( !m_frozen )
2628 RefreshRect(wxRect(0, 0, m_width, m_height));
2629 }
2630
2631 // -----------------------------------------------------------------------
2632
2633 bool wxPropertyGrid::EnableCategories( bool enable )
2634 {
2635 DoClearSelection();
2636
2637 if ( enable )
2638 {
2639 //
2640 // Enable categories
2641 //
2642
2643 m_windowStyle &= ~(wxPG_HIDE_CATEGORIES);
2644 }
2645 else
2646 {
2647 //
2648 // Disable categories
2649 //
2650 m_windowStyle |= wxPG_HIDE_CATEGORIES;
2651 }
2652
2653 if ( !m_pState->EnableCategories(enable) )
2654 return false;
2655
2656 if ( !m_frozen )
2657 {
2658 if ( m_windowStyle & wxPG_AUTO_SORT )
2659 {
2660 m_pState->m_itemsAdded = 1; // force
2661 PrepareAfterItemsAdded();
2662 }
2663 }
2664 else
2665 m_pState->m_itemsAdded = 1;
2666
2667 // No need for RecalculateVirtualSize() here - it is already called in
2668 // wxPropertyGridPageState method above.
2669
2670 Refresh();
2671
2672 return true;
2673 }
2674
2675 // -----------------------------------------------------------------------
2676
2677 void wxPropertyGrid::SwitchState( wxPropertyGridPageState* pNewState )
2678 {
2679 wxASSERT( pNewState );
2680 wxASSERT( pNewState->GetGrid() );
2681
2682 if ( pNewState == m_pState )
2683 return;
2684
2685 wxArrayPGProperty oldSelection = m_pState->m_selection;
2686
2687 // Call ClearSelection() instead of DoClearSelection()
2688 // so that selection clear events are not sent.
2689 ClearSelection();
2690
2691 m_pState->m_selection = oldSelection;
2692
2693 bool orig_mode = m_pState->IsInNonCatMode();
2694 bool new_state_mode = pNewState->IsInNonCatMode();
2695
2696 m_pState = pNewState;
2697
2698 // Validate width
2699 int pgWidth = GetClientSize().x;
2700 if ( HasVirtualWidth() )
2701 {
2702 int minWidth = pgWidth;
2703 if ( pNewState->m_width < minWidth )
2704 {
2705 pNewState->m_width = minWidth;
2706 pNewState->CheckColumnWidths();
2707 }
2708 }
2709 else
2710 {
2711 //
2712 // Just in case, fully re-center splitter
2713 //if ( HasFlag( wxPG_SPLITTER_AUTO_CENTER ) )
2714 // pNewState->m_fSplitterX = -1.0;
2715
2716 pNewState->OnClientWidthChange(pgWidth,
2717 pgWidth - pNewState->m_width);
2718 }
2719
2720 m_propHover = NULL;
2721
2722 // If necessary, convert state to correct mode.
2723 if ( orig_mode != new_state_mode )
2724 {
2725 // This should refresh as well.
2726 EnableCategories( orig_mode?false:true );
2727 }
2728 else if ( !m_frozen )
2729 {
2730 // Refresh, if not frozen.
2731 m_pState->PrepareAfterItemsAdded();
2732
2733 // Reselect (Use SetSelection() instead of Do-variant so that
2734 // events won't be sent).
2735 SetSelection(m_pState->m_selection);
2736
2737 RecalculateVirtualSize(0);
2738 Refresh();
2739 }
2740 else
2741 m_pState->m_itemsAdded = 1;
2742 }
2743
2744 // -----------------------------------------------------------------------
2745
2746 // Call to SetSplitterPosition will always disable splitter auto-centering
2747 // if parent window is shown.
2748 void wxPropertyGrid::DoSetSplitterPosition( int newxpos,
2749 int splitterIndex,
2750 int flags )
2751 {
2752 if ( ( newxpos < wxPG_DRAG_MARGIN ) )
2753 return;
2754
2755 wxPropertyGridPageState* state = m_pState;
2756
2757 if ( flags & wxPG_SPLITTER_FROM_EVENT )
2758 state->m_dontCenterSplitter = true;
2759
2760 state->DoSetSplitterPosition(newxpos, splitterIndex, flags);
2761
2762 if ( flags & wxPG_SPLITTER_REFRESH )
2763 {
2764 if ( GetSelection() )
2765 CorrectEditorWidgetSizeX();
2766
2767 Refresh();
2768 }
2769
2770 return;
2771 }
2772
2773 // -----------------------------------------------------------------------
2774
2775 void wxPropertyGrid::CenterSplitter( bool enableAutoCentering )
2776 {
2777 SetSplitterPosition( m_width/2 );
2778 if ( enableAutoCentering && HasFlag(wxPG_SPLITTER_AUTO_CENTER) )
2779 m_pState->m_dontCenterSplitter = false;
2780 }
2781
2782 // -----------------------------------------------------------------------
2783 // wxPropertyGrid item iteration (GetNextProperty etc.) methods
2784 // -----------------------------------------------------------------------
2785
2786 // Returns nearest paint visible property (such that will be painted unless
2787 // window is scrolled or resized). If given property is paint visible, then
2788 // it itself will be returned
2789 wxPGProperty* wxPropertyGrid::GetNearestPaintVisible( wxPGProperty* p ) const
2790 {
2791 int vx,vy1;// Top left corner of client
2792 GetViewStart(&vx,&vy1);
2793 vy1 *= wxPG_PIXELS_PER_UNIT;
2794
2795 int vy2 = vy1 + m_height;
2796 int propY = p->GetY2(m_lineHeight);
2797
2798 if ( (propY + m_lineHeight) < vy1 )
2799 {
2800 // Too high
2801 return DoGetItemAtY( vy1 );
2802 }
2803 else if ( propY > vy2 )
2804 {
2805 // Too low
2806 return DoGetItemAtY( vy2 );
2807 }
2808
2809 // Itself paint visible
2810 return p;
2811
2812 }
2813
2814 // -----------------------------------------------------------------------
2815 // Methods related to change in value, value modification and sending events
2816 // -----------------------------------------------------------------------
2817
2818 // commits any changes in editor of selected property
2819 // return true if validation did not fail
2820 // flags are same as with DoSelectProperty
2821 bool wxPropertyGrid::CommitChangesFromEditor( wxUint32 flags )
2822 {
2823 // Committing already?
2824 if ( m_inCommitChangesFromEditor )
2825 return true;
2826
2827 // Don't do this if already processing editor event. It might
2828 // induce recursive dialogs and crap like that.
2829 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
2830 {
2831 if ( m_inDoPropertyChanged )
2832 return true;
2833
2834 return false;
2835 }
2836
2837 wxPGProperty* selected = GetSelection();
2838
2839 if ( m_wndEditor &&
2840 IsEditorsValueModified() &&
2841 (m_iFlags & wxPG_FL_INITIALIZED) &&
2842 selected )
2843 {
2844 m_inCommitChangesFromEditor = 1;
2845
2846 wxVariant variant(selected->GetValueRef());
2847 bool valueIsPending = false;
2848
2849 // JACS - necessary to avoid new focus being found spuriously within OnIdle
2850 // due to another window getting focus
2851 wxWindow* oldFocus = m_curFocused;
2852
2853 bool validationFailure = false;
2854 bool forceSuccess = (flags & (wxPG_SEL_NOVALIDATE|wxPG_SEL_FORCE)) ? true : false;
2855
2856 m_chgInfo_changedProperty = NULL;
2857
2858 // If truly modified, schedule value as pending.
2859 if ( selected->GetEditorClass()->
2860 GetValueFromControl( variant,
2861 selected,
2862 GetEditorControl() ) )
2863 {
2864 if ( DoEditorValidate() &&
2865 PerformValidation(selected, variant) )
2866 {
2867 valueIsPending = true;
2868 }
2869 else
2870 {
2871 validationFailure = true;
2872 }
2873 }
2874 else
2875 {
2876 EditorsValueWasNotModified();
2877 }
2878
2879 bool res = true;
2880
2881 m_inCommitChangesFromEditor = 0;
2882
2883 if ( validationFailure && !forceSuccess )
2884 {
2885 if (oldFocus)
2886 {
2887 oldFocus->SetFocus();
2888 m_curFocused = oldFocus;
2889 }
2890
2891 res = OnValidationFailure(selected, variant);
2892
2893 // Now prevent further validation failure messages
2894 if ( res )
2895 {
2896 EditorsValueWasNotModified();
2897 OnValidationFailureReset(selected);
2898 }
2899 }
2900 else if ( valueIsPending )
2901 {
2902 DoPropertyChanged( selected, flags );
2903 EditorsValueWasNotModified();
2904 }
2905
2906 return res;
2907 }
2908
2909 return true;
2910 }
2911
2912 // -----------------------------------------------------------------------
2913
2914 bool wxPropertyGrid::PerformValidation( wxPGProperty* p, wxVariant& pendingValue,
2915 int flags )
2916 {
2917 //
2918 // Runs all validation functionality.
2919 // Returns true if value passes all tests.
2920 //
2921
2922 m_validationInfo.m_failureBehavior = m_permanentValidationFailureBehavior;
2923
2924 //
2925 // Variant list a special value that cannot be validated
2926 // by normal means.
2927 if ( pendingValue.GetType() != wxPG_VARIANT_TYPE_LIST )
2928 {
2929 if ( !p->ValidateValue(pendingValue, m_validationInfo) )
2930 return false;
2931 }
2932
2933 //
2934 // Adapt list to child values, if necessary
2935 wxVariant listValue = pendingValue;
2936 wxVariant* pPendingValue = &pendingValue;
2937 wxVariant* pList = NULL;
2938
2939 // If parent has wxPG_PROP_AGGREGATE flag, or uses composite
2940 // string value, then we need treat as it was changed instead
2941 // (or, in addition, as is the case with composite string parent).
2942 // This includes creating list variant for child values.
2943
2944 wxPGProperty* pwc = p->GetParent();
2945 wxPGProperty* changedProperty = p;
2946 wxPGProperty* baseChangedProperty = changedProperty;
2947 wxVariant bcpPendingList;
2948
2949 listValue = pendingValue;
2950 listValue.SetName(p->GetBaseName());
2951
2952 while ( pwc &&
2953 (pwc->HasFlag(wxPG_PROP_AGGREGATE) || pwc->HasFlag(wxPG_PROP_COMPOSED_VALUE)) )
2954 {
2955 wxVariantList tempList;
2956 wxVariant lv(tempList, pwc->GetBaseName());
2957 lv.Append(listValue);
2958 listValue = lv;
2959 pPendingValue = &listValue;
2960
2961 if ( pwc->HasFlag(wxPG_PROP_AGGREGATE) )
2962 {
2963 baseChangedProperty = pwc;
2964 bcpPendingList = lv;
2965 }
2966
2967 changedProperty = pwc;
2968 pwc = pwc->GetParent();
2969 }
2970
2971 wxVariant value;
2972 wxPGProperty* evtChangingProperty = changedProperty;
2973
2974 if ( pPendingValue->GetType() != wxPG_VARIANT_TYPE_LIST )
2975 {
2976 value = *pPendingValue;
2977 }
2978 else
2979 {
2980 // Convert list to child values
2981 pList = pPendingValue;
2982 changedProperty->AdaptListToValue( *pPendingValue, &value );
2983 }
2984
2985 wxVariant evtChangingValue = value;
2986
2987 if ( flags & SendEvtChanging )
2988 {
2989 // FIXME: After proper ValueToString()s added, remove
2990 // this. It is just a temporary fix, as evt_changing
2991 // will simply not work for wxPG_PROP_COMPOSED_VALUE
2992 // (unless it is selected, and textctrl editor is open).
2993 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
2994 {
2995 evtChangingProperty = baseChangedProperty;
2996 if ( evtChangingProperty != p )
2997 {
2998 evtChangingProperty->AdaptListToValue( bcpPendingList, &evtChangingValue );
2999 }
3000 else
3001 {
3002 evtChangingValue = pendingValue;
3003 }
3004 }
3005
3006 if ( evtChangingProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
3007 {
3008 if ( changedProperty == GetSelection() )
3009 {
3010 wxWindow* editor = GetEditorControl();
3011 wxASSERT( editor->IsKindOf(CLASSINFO(wxTextCtrl)) );
3012 evtChangingValue = wxStaticCast(editor, wxTextCtrl)->GetValue();
3013 }
3014 else
3015 {
3016 wxLogDebug(wxT("WARNING: wxEVT_PG_CHANGING is about to happen with old value."));
3017 }
3018 }
3019 }
3020
3021 wxASSERT( m_chgInfo_changedProperty == NULL );
3022 m_chgInfo_changedProperty = changedProperty;
3023 m_chgInfo_baseChangedProperty = baseChangedProperty;
3024 m_chgInfo_pendingValue = value;
3025
3026 if ( pList )
3027 m_chgInfo_valueList = *pList;
3028 else
3029 m_chgInfo_valueList.MakeNull();
3030
3031 // If changedProperty is not property which value was edited,
3032 // then call wxPGProperty::ValidateValue() for that as well.
3033 if ( p != changedProperty && value.GetType() != wxPG_VARIANT_TYPE_LIST )
3034 {
3035 if ( !changedProperty->ValidateValue(value, m_validationInfo) )
3036 return false;
3037 }
3038
3039 if ( flags & SendEvtChanging )
3040 {
3041 // SendEvent returns true if event was vetoed
3042 if ( SendEvent( wxEVT_PG_CHANGING, evtChangingProperty,
3043 &evtChangingValue ) )
3044 return false;
3045 }
3046
3047 if ( flags & IsStandaloneValidation )
3048 {
3049 // If called in 'generic' context, we need to reset
3050 // m_chgInfo_changedProperty and write back translated value.
3051 m_chgInfo_changedProperty = NULL;
3052 pendingValue = value;
3053 }
3054
3055 return true;
3056 }
3057
3058 // -----------------------------------------------------------------------
3059
3060 void wxPropertyGrid::DoShowPropertyError( wxPGProperty* WXUNUSED(property), const wxString& msg )
3061 {
3062 if ( !msg.length() )
3063 return;
3064
3065 #if wxUSE_STATUSBAR
3066 if ( !wxPGGlobalVars->m_offline )
3067 {
3068 wxWindow* topWnd = ::wxGetTopLevelParent(this);
3069 if ( topWnd )
3070 {
3071 wxFrame* pFrame = wxDynamicCast(topWnd, wxFrame);
3072 if ( pFrame )
3073 {
3074 wxStatusBar* pStatusBar = pFrame->GetStatusBar();
3075 if ( pStatusBar )
3076 {
3077 pStatusBar->SetStatusText(msg);
3078 return;
3079 }
3080 }
3081 }
3082 }
3083 #endif
3084
3085 ::wxMessageBox(msg, wxT("Property Error"));
3086 }
3087
3088 // -----------------------------------------------------------------------
3089
3090 bool wxPropertyGrid::OnValidationFailure( wxPGProperty* property,
3091 wxVariant& invalidValue )
3092 {
3093 wxWindow* editor = GetEditorControl();
3094
3095 // First call property's handler
3096 property->OnValidationFailure(invalidValue);
3097
3098 bool res = DoOnValidationFailure(property, invalidValue);
3099
3100 //
3101 // For non-wxTextCtrl editors, we do need to revert the value
3102 if ( !editor->IsKindOf(CLASSINFO(wxTextCtrl)) &&
3103 property == GetSelection() )
3104 {
3105 property->GetEditorClass()->UpdateControl(property, editor);
3106 }
3107
3108 property->SetFlag(wxPG_PROP_INVALID_VALUE);
3109
3110 return res;
3111 }
3112
3113 bool wxPropertyGrid::DoOnValidationFailure( wxPGProperty* property, wxVariant& WXUNUSED(invalidValue) )
3114 {
3115 int vfb = m_validationInfo.m_failureBehavior;
3116
3117 if ( vfb & wxPG_VFB_BEEP )
3118 ::wxBell();
3119
3120 if ( (vfb & wxPG_VFB_MARK_CELL) &&
3121 !property->HasFlag(wxPG_PROP_INVALID_VALUE) )
3122 {
3123 unsigned int colCount = m_pState->GetColumnCount();
3124
3125 // We need backup marked property's cells
3126 m_propCellsBackup = property->m_cells;
3127
3128 wxColour vfbFg = *wxWHITE;
3129 wxColour vfbBg = *wxRED;
3130
3131 property->EnsureCells(colCount);
3132
3133 for ( unsigned int i=0; i<colCount; i++ )
3134 {
3135 wxPGCell& cell = property->m_cells[i];
3136 cell.SetFgCol(vfbFg);
3137 cell.SetBgCol(vfbBg);
3138 }
3139
3140 DrawItemAndChildren(property);
3141
3142 if ( property == GetSelection() )
3143 {
3144 SetInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3145
3146 wxWindow* editor = GetEditorControl();
3147 if ( editor )
3148 {
3149 editor->SetForegroundColour(vfbFg);
3150 editor->SetBackgroundColour(vfbBg);
3151 }
3152 }
3153 }
3154
3155 if ( vfb & wxPG_VFB_SHOW_MESSAGE )
3156 {
3157 wxString msg = m_validationInfo.m_failureMessage;
3158
3159 if ( !msg.length() )
3160 msg = wxT("You have entered invalid value. Press ESC to cancel editing.");
3161
3162 DoShowPropertyError(property, msg);
3163 }
3164
3165 return (vfb & wxPG_VFB_STAY_IN_PROPERTY) ? false : true;
3166 }
3167
3168 // -----------------------------------------------------------------------
3169
3170 void wxPropertyGrid::DoOnValidationFailureReset( wxPGProperty* property )
3171 {
3172 int vfb = m_validationInfo.m_failureBehavior;
3173
3174 if ( vfb & wxPG_VFB_MARK_CELL )
3175 {
3176 // Revert cells
3177 property->m_cells = m_propCellsBackup;
3178
3179 ClearInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3180
3181 if ( property == GetSelection() && GetEditorControl() )
3182 {
3183 // Calling this will recreate the control, thus resetting its colour
3184 RefreshProperty(property);
3185 }
3186 else
3187 {
3188 DrawItemAndChildren(property);
3189 }
3190 }
3191 }
3192
3193 // -----------------------------------------------------------------------
3194
3195 // flags are same as with DoSelectProperty
3196 bool wxPropertyGrid::DoPropertyChanged( wxPGProperty* p, unsigned int selFlags )
3197 {
3198 if ( m_inDoPropertyChanged )
3199 return true;
3200
3201 wxWindow* editor = GetEditorControl();
3202 wxPGProperty* selected = GetSelection();
3203
3204 m_pState->m_anyModified = 1;
3205
3206 m_inDoPropertyChanged = 1;
3207
3208 // Maybe need to update control
3209 wxASSERT( m_chgInfo_changedProperty != NULL );
3210
3211 // These values were calculated in PerformValidation()
3212 wxPGProperty* changedProperty = m_chgInfo_changedProperty;
3213 wxVariant value = m_chgInfo_pendingValue;
3214
3215 wxPGProperty* topPaintedProperty = changedProperty;
3216
3217 while ( !topPaintedProperty->IsCategory() &&
3218 !topPaintedProperty->IsRoot() )
3219 {
3220 topPaintedProperty = topPaintedProperty->GetParent();
3221 }
3222
3223 changedProperty->SetValue(value, &m_chgInfo_valueList, wxPG_SETVAL_BY_USER);
3224
3225 // Set as Modified (not if dragging just began)
3226 if ( !(p->m_flags & wxPG_PROP_MODIFIED) )
3227 {
3228 p->m_flags |= wxPG_PROP_MODIFIED;
3229 if ( p == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3230 {
3231 if ( editor )
3232 SetCurControlBoldFont();
3233 }
3234 }
3235
3236 wxPGProperty* pwc;
3237
3238 // Propagate updates to parent(s)
3239 pwc = p;
3240 wxPGProperty* prevPwc = NULL;
3241
3242 while ( prevPwc != topPaintedProperty )
3243 {
3244 pwc->m_flags |= wxPG_PROP_MODIFIED;
3245
3246 if ( pwc == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3247 {
3248 if ( editor )
3249 SetCurControlBoldFont();
3250 }
3251
3252 prevPwc = pwc;
3253 pwc = pwc->GetParent();
3254 }
3255
3256 // Draw the actual property
3257 DrawItemAndChildren( topPaintedProperty );
3258
3259 //
3260 // If value was set by wxPGProperty::OnEvent, then update the editor
3261 // control.
3262 if ( selFlags & wxPG_SEL_DIALOGVAL )
3263 {
3264 RefreshEditor();
3265 }
3266 else
3267 {
3268 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
3269 if ( m_wndEditor ) m_wndEditor->Refresh();
3270 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
3271 #endif
3272 }
3273
3274 // Sanity check
3275 wxASSERT( !changedProperty->GetParent()->HasFlag(wxPG_PROP_AGGREGATE) );
3276
3277 // If top parent has composite string value, then send to child parents,
3278 // starting from baseChangedProperty.
3279 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
3280 {
3281 pwc = m_chgInfo_baseChangedProperty;
3282
3283 while ( pwc != changedProperty )
3284 {
3285 SendEvent( wxEVT_PG_CHANGED, pwc, NULL );
3286 pwc = pwc->GetParent();
3287 }
3288 }
3289
3290 SendEvent( wxEVT_PG_CHANGED, changedProperty, NULL );
3291
3292 m_inDoPropertyChanged = 0;
3293
3294 return true;
3295 }
3296
3297 // -----------------------------------------------------------------------
3298
3299 bool wxPropertyGrid::ChangePropertyValue( wxPGPropArg id, wxVariant newValue )
3300 {
3301 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
3302
3303 m_chgInfo_changedProperty = NULL;
3304
3305 if ( PerformValidation(p, newValue) )
3306 {
3307 DoPropertyChanged(p);
3308 return true;
3309 }
3310 else
3311 {
3312 OnValidationFailure(p, newValue);
3313 }
3314
3315 return false;
3316 }
3317
3318 // -----------------------------------------------------------------------
3319
3320 wxVariant wxPropertyGrid::GetUncommittedPropertyValue()
3321 {
3322 wxPGProperty* prop = GetSelectedProperty();
3323
3324 if ( !prop )
3325 return wxNullVariant;
3326
3327 wxTextCtrl* tc = GetEditorTextCtrl();
3328 wxVariant value = prop->GetValue();
3329
3330 if ( !tc || !IsEditorsValueModified() )
3331 return value;
3332
3333 if ( !prop->StringToValue(value, tc->GetValue()) )
3334 return value;
3335
3336 if ( !PerformValidation(prop, value, IsStandaloneValidation) )
3337 return prop->GetValue();
3338
3339 return value;
3340 }
3341
3342 // -----------------------------------------------------------------------
3343
3344 // Runs wxValidator for the selected property
3345 bool wxPropertyGrid::DoEditorValidate()
3346 {
3347 return true;
3348 }
3349
3350 // -----------------------------------------------------------------------
3351
3352 void wxPropertyGrid::HandleCustomEditorEvent( wxEvent &event )
3353 {
3354 // It is possible that this handler receives event even before
3355 // the control has been properly initialized. Let's skip the
3356 // event handling in that case.
3357 if ( !m_pState )
3358 return;
3359
3360 wxPGProperty* selected = GetSelection();
3361
3362 // Somehow, event is handled after property has been deselected.
3363 // Possibly, but very rare.
3364 if ( !selected ||
3365 selected->HasFlag(wxPG_PROP_BEING_DELETED) ||
3366 // Also don't handle editor event if wxEVT_PG_CHANGED or
3367 // similar is currently doing something (showing a
3368 // message box, for instance).
3369 m_processedEvent )
3370 return;
3371
3372 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
3373 return;
3374
3375 wxVariant pendingValue(selected->GetValueRef());
3376 wxWindow* wnd = GetEditorControl();
3377 wxWindow* editorWnd = wxDynamicCast(event.GetEventObject(), wxWindow);
3378 int selFlags = 0;
3379 bool wasUnspecified = selected->IsValueUnspecified();
3380 int usesAutoUnspecified = selected->UsesAutoUnspecified();
3381 bool valueIsPending = false;
3382
3383 m_chgInfo_changedProperty = NULL;
3384
3385 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED|wxPG_FL_VALUE_CHANGE_IN_EVENT);
3386
3387 //
3388 // Filter out excess wxTextCtrl modified events
3389 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED && wnd )
3390 {
3391 if ( wnd->IsKindOf(CLASSINFO(wxTextCtrl)) )
3392 {
3393 wxTextCtrl* tc = (wxTextCtrl*) wnd;
3394
3395 wxString newTcValue = tc->GetValue();
3396 if ( m_prevTcValue == newTcValue )
3397 return;
3398 m_prevTcValue = newTcValue;
3399 }
3400 else if ( wnd->IsKindOf(CLASSINFO(wxComboCtrl)) )
3401 {
3402 wxComboCtrl* cc = (wxComboCtrl*) wnd;
3403
3404 wxString newTcValue = cc->GetTextCtrl()->GetValue();
3405 if ( m_prevTcValue == newTcValue )
3406 return;
3407 m_prevTcValue = newTcValue;
3408 }
3409 }
3410
3411 SetInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3412
3413 bool validationFailure = false;
3414 bool buttonWasHandled = false;
3415
3416 //
3417 // Try common button handling
3418 if ( m_wndEditor2 && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3419 {
3420 wxPGEditorDialogAdapter* adapter = selected->GetEditorDialog();
3421
3422 if ( adapter )
3423 {
3424 buttonWasHandled = true;
3425 // Store as res2, as previously (and still currently alternatively)
3426 // dialogs can be shown by handling wxEVT_COMMAND_BUTTON_CLICKED
3427 // in wxPGProperty::OnEvent().
3428 adapter->ShowDialog( this, selected );
3429 delete adapter;
3430 }
3431 }
3432
3433 if ( !buttonWasHandled )
3434 {
3435 if ( wnd || m_wndEditor2 )
3436 {
3437 // First call editor class' event handler.
3438 const wxPGEditor* editor = selected->GetEditorClass();
3439
3440 if ( editor->OnEvent( this, selected, editorWnd, event ) )
3441 {
3442 // If changes, validate them
3443 if ( DoEditorValidate() )
3444 {
3445 if ( editor->GetValueFromControl( pendingValue,
3446 selected,
3447 wnd ) )
3448 valueIsPending = true;
3449 }
3450 else
3451 {
3452 validationFailure = true;
3453 }
3454 }
3455 }
3456
3457 // Then the property's custom handler (must be always called, unless
3458 // validation failed).
3459 if ( !validationFailure )
3460 buttonWasHandled = selected->OnEvent( this, editorWnd, event );
3461 }
3462
3463 // SetValueInEvent(), as called in one of the functions referred above
3464 // overrides editor's value.
3465 if ( m_iFlags & wxPG_FL_VALUE_CHANGE_IN_EVENT )
3466 {
3467 valueIsPending = true;
3468 pendingValue = m_changeInEventValue;
3469 selFlags |= wxPG_SEL_DIALOGVAL;
3470 }
3471
3472 if ( !validationFailure && valueIsPending )
3473 if ( !PerformValidation(selected, pendingValue) )
3474 validationFailure = true;
3475
3476 if ( validationFailure)
3477 {
3478 OnValidationFailure(selected, pendingValue);
3479 }
3480 else if ( valueIsPending )
3481 {
3482 selFlags |= ( !wasUnspecified && selected->IsValueUnspecified() && usesAutoUnspecified ) ? wxPG_SEL_SETUNSPEC : 0;
3483
3484 DoPropertyChanged(selected, selFlags);
3485 EditorsValueWasNotModified();
3486
3487 // Regardless of editor type, unfocus editor on
3488 // text-editing related enter press.
3489 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3490 {
3491 SetFocusOnCanvas();
3492 }
3493 }
3494 else
3495 {
3496 // No value after all
3497
3498 // Regardless of editor type, unfocus editor on
3499 // text-editing related enter press.
3500 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3501 {
3502 SetFocusOnCanvas();
3503 }
3504
3505 // Let unhandled button click events go to the parent
3506 if ( !buttonWasHandled && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3507 {
3508 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED,GetId());
3509 GetEventHandler()->AddPendingEvent(evt);
3510 }
3511 }
3512
3513 ClearInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3514 }
3515
3516 // -----------------------------------------------------------------------
3517 // wxPropertyGrid editor control helper methods
3518 // -----------------------------------------------------------------------
3519
3520 wxRect wxPropertyGrid::GetEditorWidgetRect( wxPGProperty* p, int column ) const
3521 {
3522 int itemy = p->GetY2(m_lineHeight);
3523 int splitterX = m_pState->DoGetSplitterPosition(column-1);
3524 int colEnd = splitterX + m_pState->m_colWidths[column];
3525 int imageOffset = 0;
3526
3527 int vx, vy; // Top left corner of client
3528 GetViewStart(&vx, &vy);
3529 vy *= wxPG_PIXELS_PER_UNIT;
3530
3531 // TODO: If custom image detection changes from current, change this.
3532 if ( m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE )
3533 {
3534 //m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3535 int iw = p->OnMeasureImage().x;
3536 if ( iw < 1 )
3537 iw = wxPG_CUSTOM_IMAGE_WIDTH;
3538 imageOffset = p->GetImageOffset(iw);
3539 }
3540
3541 return wxRect
3542 (
3543 splitterX+imageOffset+wxPG_XBEFOREWIDGET+wxPG_CONTROL_MARGIN+1,
3544 itemy-vy,
3545 colEnd-splitterX-wxPG_XBEFOREWIDGET-wxPG_CONTROL_MARGIN-imageOffset-1,
3546 m_lineHeight-1
3547 );
3548 }
3549
3550 // -----------------------------------------------------------------------
3551
3552 wxRect wxPropertyGrid::GetImageRect( wxPGProperty* p, int item ) const
3553 {
3554 wxSize sz = GetImageSize(p, item);
3555 return wxRect(wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
3556 wxPG_CUSTOM_IMAGE_SPACINGY,
3557 sz.x,
3558 sz.y);
3559 }
3560
3561 // return size of custom paint image
3562 wxSize wxPropertyGrid::GetImageSize( wxPGProperty* p, int item ) const
3563 {
3564 // If called with NULL property, then return default image
3565 // size for properties that use image.
3566 if ( !p )
3567 return wxSize(wxPG_CUSTOM_IMAGE_WIDTH,wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight));
3568
3569 wxSize cis = p->OnMeasureImage(item);
3570
3571 int choiceCount = p->m_choices.GetCount();
3572 int comVals = p->GetDisplayedCommonValueCount();
3573 if ( item >= choiceCount && comVals > 0 )
3574 {
3575 unsigned int cvi = item-choiceCount;
3576 cis = GetCommonValue(cvi)->GetRenderer()->GetImageSize(NULL, 1, cvi);
3577 }
3578 else if ( item >= 0 && choiceCount == 0 )
3579 return wxSize(0, 0);
3580
3581 if ( cis.x < 0 )
3582 {
3583 if ( cis.x <= -1 )
3584 cis.x = wxPG_CUSTOM_IMAGE_WIDTH;
3585 }
3586 if ( cis.y <= 0 )
3587 {
3588 if ( cis.y >= -1 )
3589 cis.y = wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight);
3590 else
3591 cis.y = -cis.y;
3592 }
3593 return cis;
3594 }
3595
3596 // -----------------------------------------------------------------------
3597
3598 // takes scrolling into account
3599 void wxPropertyGrid::ImprovedClientToScreen( int* px, int* py )
3600 {
3601 int vx, vy;
3602 GetViewStart(&vx,&vy);
3603 vy*=wxPG_PIXELS_PER_UNIT;
3604 vx*=wxPG_PIXELS_PER_UNIT;
3605 *px -= vx;
3606 *py -= vy;
3607 ClientToScreen( px, py );
3608 }
3609
3610 // -----------------------------------------------------------------------
3611
3612 wxPropertyGridHitTestResult wxPropertyGrid::HitTest( const wxPoint& pt ) const
3613 {
3614 wxPoint pt2;
3615 GetViewStart(&pt2.x,&pt2.y);
3616 pt2.x *= wxPG_PIXELS_PER_UNIT;
3617 pt2.y *= wxPG_PIXELS_PER_UNIT;
3618 pt2.x += pt.x;
3619 pt2.y += pt.y;
3620
3621 return m_pState->HitTest(pt2);
3622 }
3623
3624 // -----------------------------------------------------------------------
3625
3626 // custom set cursor
3627 void wxPropertyGrid::CustomSetCursor( int type, bool override )
3628 {
3629 if ( type == m_curcursor && !override ) return;
3630
3631 wxCursor* cursor = &wxPG_DEFAULT_CURSOR;
3632
3633 if ( type == wxCURSOR_SIZEWE )
3634 cursor = m_cursorSizeWE;
3635
3636 SetCursor( *cursor );
3637
3638 m_curcursor = type;
3639 }
3640
3641 // -----------------------------------------------------------------------
3642
3643 wxString
3644 wxPropertyGrid::GetUnspecifiedValueText( int argFlags ) const
3645 {
3646 const wxPGCell& ua = GetUnspecifiedValueAppearance();
3647
3648 if ( ua.HasText() &&
3649 !(argFlags & wxPG_FULL_VALUE) &&
3650 !(argFlags & wxPG_EDITABLE_VALUE) )
3651 return ua.GetText();
3652
3653 return wxEmptyString;
3654 }
3655
3656 // -----------------------------------------------------------------------
3657 // wxPropertyGrid property selection, editor creation
3658 // -----------------------------------------------------------------------
3659
3660 //
3661 // This class forwards events from property editor controls to wxPropertyGrid.
3662 class wxPropertyGridEditorEventForwarder : public wxEvtHandler
3663 {
3664 public:
3665 wxPropertyGridEditorEventForwarder( wxPropertyGrid* propGrid )
3666 : wxEvtHandler(), m_propGrid(propGrid)
3667 {
3668 }
3669
3670 virtual ~wxPropertyGridEditorEventForwarder()
3671 {
3672 }
3673
3674 private:
3675 bool ProcessEvent( wxEvent& event )
3676 {
3677 // Always skip
3678 event.Skip();
3679
3680 m_propGrid->HandleCustomEditorEvent(event);
3681
3682 return wxEvtHandler::ProcessEvent(event);
3683 }
3684
3685 wxPropertyGrid* m_propGrid;
3686 };
3687
3688 // Setups event handling for child control
3689 void wxPropertyGrid::SetupChildEventHandling( wxWindow* argWnd )
3690 {
3691 wxWindowID id = argWnd->GetId();
3692
3693 if ( argWnd == m_wndEditor )
3694 {
3695 argWnd->Connect(id, wxEVT_MOTION,
3696 wxMouseEventHandler(wxPropertyGrid::OnMouseMoveChild),
3697 NULL, this);
3698 argWnd->Connect(id, wxEVT_LEFT_UP,
3699 wxMouseEventHandler(wxPropertyGrid::OnMouseUpChild),
3700 NULL, this);
3701 argWnd->Connect(id, wxEVT_LEFT_DOWN,
3702 wxMouseEventHandler(wxPropertyGrid::OnMouseClickChild),
3703 NULL, this);
3704 argWnd->Connect(id, wxEVT_RIGHT_UP,
3705 wxMouseEventHandler(wxPropertyGrid::OnMouseRightClickChild),
3706 NULL, this);
3707 argWnd->Connect(id, wxEVT_ENTER_WINDOW,
3708 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3709 NULL, this);
3710 argWnd->Connect(id, wxEVT_LEAVE_WINDOW,
3711 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3712 NULL, this);
3713 }
3714
3715 wxPropertyGridEditorEventForwarder* forwarder;
3716 forwarder = new wxPropertyGridEditorEventForwarder(this);
3717 argWnd->PushEventHandler(forwarder);
3718
3719 argWnd->Connect(id, wxEVT_KEY_DOWN,
3720 wxCharEventHandler(wxPropertyGrid::OnChildKeyDown),
3721 NULL, this);
3722 }
3723
3724 void wxPropertyGrid::DestroyEditorWnd( wxWindow* wnd )
3725 {
3726 if ( !wnd )
3727 return;
3728
3729 wnd->Hide();
3730
3731 // Do not free editors immediately (for sake of processing events)
3732 wxPendingDelete.Append(wnd);
3733 }
3734
3735 void wxPropertyGrid::FreeEditors()
3736 {
3737 //
3738 // Return focus back to canvas from children (this is required at least for
3739 // GTK+, which, unlike Windows, clears focus when control is destroyed
3740 // instead of moving it to closest parent).
3741 wxWindow* focus = wxWindow::FindFocus();
3742 if ( focus )
3743 {
3744 wxWindow* parent = focus->GetParent();
3745 while ( parent )
3746 {
3747 if ( parent == this )
3748 {
3749 SetFocusOnCanvas();
3750 break;
3751 }
3752 parent = parent->GetParent();
3753 }
3754 }
3755
3756 // Do not free editors immediately if processing events
3757 if ( m_wndEditor2 )
3758 {
3759 wxEvtHandler* handler = m_wndEditor2->PopEventHandler(false);
3760 m_wndEditor2->Hide();
3761 wxPendingDelete.Append( handler );
3762 DestroyEditorWnd(m_wndEditor2);
3763 m_wndEditor2 = NULL;
3764 }
3765
3766 if ( m_wndEditor )
3767 {
3768 wxEvtHandler* handler = m_wndEditor->PopEventHandler(false);
3769 m_wndEditor->Hide();
3770 wxPendingDelete.Append( handler );
3771 DestroyEditorWnd(m_wndEditor);
3772 m_wndEditor = NULL;
3773 }
3774 }
3775
3776 // Call with NULL to de-select property
3777 bool wxPropertyGrid::DoSelectProperty( wxPGProperty* p, unsigned int flags )
3778 {
3779 /*
3780 if (p)
3781 {
3782 wxLogDebug(wxT("SelectProperty( %s (%s[%i]) )"),p->m_label.c_str(),
3783 p->m_parent->m_label.c_str(),p->GetIndexInParent());
3784 }
3785 else
3786 {
3787 wxLogDebug(wxT("SelectProperty( NULL, -1 )"));
3788 }
3789 */
3790
3791 if ( m_inDoSelectProperty )
3792 return true;
3793
3794 m_inDoSelectProperty = 1;
3795
3796 if ( !m_pState )
3797 {
3798 m_inDoSelectProperty = 0;
3799 return false;
3800 }
3801
3802 wxArrayPGProperty prevSelection = m_pState->m_selection;
3803 wxPGProperty* prevFirstSel;
3804
3805 if ( prevSelection.size() > 0 )
3806 prevFirstSel = prevSelection[0];
3807 else
3808 prevFirstSel = NULL;
3809
3810 if ( prevFirstSel && prevFirstSel->HasFlag(wxPG_PROP_BEING_DELETED) )
3811 prevFirstSel = NULL;
3812
3813 // Always send event, as this is indirect call
3814 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
3815
3816 /*
3817 if ( prevFirstSel )
3818 wxPrintf( "Selected %s\n", prevFirstSel->GetClassInfo()->GetClassName() );
3819 else
3820 wxPrintf( "None selected\n" );
3821
3822 if (p)
3823 wxPrintf( "P = %s\n", p->GetClassInfo()->GetClassName() );
3824 else
3825 wxPrintf( "P = NULL\n" );
3826 */
3827
3828 // If we are frozen, then just set the values.
3829 if ( m_frozen )
3830 {
3831 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3832 m_editorFocused = 0;
3833 m_pState->DoSetSelection(p);
3834
3835 // If frozen, always free controls. But don't worry, as Thaw will
3836 // recall SelectProperty to recreate them.
3837 FreeEditors();
3838
3839 // Prevent any further selection measures in this call
3840 p = NULL;
3841 }
3842 else
3843 {
3844 // Is it the same?
3845 if ( prevFirstSel == p &&
3846 prevSelection.size() <= 1 &&
3847 !(flags & wxPG_SEL_FORCE) )
3848 {
3849 // Only set focus if not deselecting
3850 if ( p )
3851 {
3852 if ( flags & wxPG_SEL_FOCUS )
3853 {
3854 if ( m_wndEditor )
3855 {
3856 m_wndEditor->SetFocus();
3857 m_editorFocused = 1;
3858 }
3859 }
3860 else
3861 {
3862 SetFocusOnCanvas();
3863 }
3864 }
3865
3866 m_inDoSelectProperty = 0;
3867 return true;
3868 }
3869
3870 //
3871 // First, deactivate previous
3872 if ( prevFirstSel )
3873 {
3874 OnValidationFailureReset(prevFirstSel);
3875
3876 // Must double-check if this is an selected in case of forceswitch
3877 if ( p != prevFirstSel )
3878 {
3879 if ( !CommitChangesFromEditor(flags) )
3880 {
3881 // Validation has failed, so we can't exit the previous editor
3882 //::wxMessageBox(_("Please correct the value or press ESC to cancel the edit."),
3883 // _("Invalid Value"),wxOK|wxICON_ERROR);
3884 m_inDoSelectProperty = 0;
3885 return false;
3886 }
3887 }
3888
3889 FreeEditors();
3890
3891 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3892 EditorsValueWasNotModified();
3893 }
3894
3895 SetInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3896
3897 m_pState->DoSetSelection(p);
3898
3899 // Redraw unselected
3900 for ( unsigned int i=0; i<prevSelection.size(); i++ )
3901 {
3902 DrawItem(prevSelection[i]);
3903 }
3904
3905 //
3906 // Then, activate the one given.
3907 if ( p )
3908 {
3909 int propY = p->GetY2(m_lineHeight);
3910
3911 int splitterX = GetSplitterPosition();
3912 m_editorFocused = 0;
3913 m_iFlags |= wxPG_FL_PRIMARY_FILLS_ENTIRE;
3914 if ( p != prevFirstSel )
3915 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED);
3916
3917 wxASSERT( m_wndEditor == NULL );
3918
3919 //
3920 // Only create editor for non-disabled non-caption
3921 if ( !p->IsCategory() && !(p->m_flags & wxPG_PROP_DISABLED) )
3922 {
3923 // do this for non-caption items
3924
3925 m_selColumn = 1;
3926
3927 // Do we need to paint the custom image, if any?
3928 m_iFlags &= ~(wxPG_FL_CUR_USES_CUSTOM_IMAGE);
3929 if ( (p->m_flags & wxPG_PROP_CUSTOMIMAGE) &&
3930 !p->GetEditorClass()->CanContainCustomImage()
3931 )
3932 m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3933
3934 wxRect grect = GetEditorWidgetRect(p, m_selColumn);
3935 wxPoint goodPos = grect.GetPosition();
3936
3937 // Editor appearance can now be considered clear
3938 m_editorAppearance.SetEmptyData();
3939
3940 const wxPGEditor* editor = p->GetEditorClass();
3941 wxCHECK_MSG(editor, false,
3942 wxT("NULL editor class not allowed"));
3943
3944 m_iFlags &= ~wxPG_FL_FIXED_WIDTH_EDITOR;
3945
3946 wxPGWindowList wndList =
3947 editor->CreateControls(this,
3948 p,
3949 goodPos,
3950 grect.GetSize());
3951
3952 m_wndEditor = wndList.m_primary;
3953 m_wndEditor2 = wndList.m_secondary;
3954 wxWindow* primaryCtrl = GetEditorControl();
3955
3956 //
3957 // Essentially, primaryCtrl == m_wndEditor
3958 //
3959
3960 // NOTE: It is allowed for m_wndEditor to be NULL - in this
3961 // case value is drawn as normal, and m_wndEditor2 is
3962 // assumed to be a right-aligned button that triggers
3963 // a separate editorCtrl window.
3964
3965 if ( m_wndEditor )
3966 {
3967 wxASSERT_MSG( m_wndEditor->GetParent() == GetPanel(),
3968 "CreateControls must use result of "
3969 "wxPropertyGrid::GetPanel() as parent "
3970 "of controls." );
3971
3972 // Set validator, if any
3973 #if wxUSE_VALIDATORS
3974 wxValidator* validator = p->GetValidator();
3975 if ( validator )
3976 primaryCtrl->SetValidator(*validator);
3977 #endif
3978
3979 if ( m_wndEditor->GetSize().y > (m_lineHeight+6) )
3980 m_iFlags |= wxPG_FL_ABNORMAL_EDITOR;
3981
3982 // If it has modified status, use bold font
3983 // (must be done before capturing m_ctrlXAdjust)
3984 if ( (p->m_flags & wxPG_PROP_MODIFIED) &&
3985 (m_windowStyle & wxPG_BOLD_MODIFIED) )
3986 SetCurControlBoldFont();
3987
3988 // Store x relative to splitter (we'll need it).
3989 m_ctrlXAdjust = m_wndEditor->GetPosition().x - splitterX;
3990
3991 // Check if background clear is not necessary
3992 wxPoint pos = m_wndEditor->GetPosition();
3993 if ( pos.x > (splitterX+1) || pos.y > propY )
3994 {
3995 m_iFlags &= ~(wxPG_FL_PRIMARY_FILLS_ENTIRE);
3996 }
3997
3998 m_wndEditor->SetSizeHints(3, 3);
3999
4000 SetupChildEventHandling(primaryCtrl);
4001
4002 // Focus and select all (wxTextCtrl, wxComboBox etc)
4003 if ( flags & wxPG_SEL_FOCUS )
4004 {
4005 primaryCtrl->SetFocus();
4006
4007 p->GetEditorClass()->OnFocus(p, primaryCtrl);
4008 }
4009 else
4010 {
4011 if ( p->IsValueUnspecified() )
4012 SetEditorAppearance(m_unspecifiedAppearance,
4013 true);
4014 }
4015 }
4016
4017 if ( m_wndEditor2 )
4018 {
4019 wxASSERT_MSG( m_wndEditor2->GetParent() == GetPanel(),
4020 "CreateControls must use result of "
4021 "wxPropertyGrid::GetPanel() as parent "
4022 "of controls." );
4023
4024 // Get proper id for wndSecondary
4025 m_wndSecId = m_wndEditor2->GetId();
4026 wxWindowList children = m_wndEditor2->GetChildren();
4027 wxWindowList::iterator node = children.begin();
4028 if ( node != children.end() )
4029 m_wndSecId = ((wxWindow*)*node)->GetId();
4030
4031 m_wndEditor2->SetSizeHints(3,3);
4032
4033 m_wndEditor2->Show();
4034
4035 SetupChildEventHandling(m_wndEditor2);
4036
4037 // If no primary editor, focus to button to allow
4038 // it to interprete ENTER etc.
4039 // NOTE: Due to problems focusing away from it, this
4040 // has been disabled.
4041 /*
4042 if ( (flags & wxPG_SEL_FOCUS) && !m_wndEditor )
4043 m_wndEditor2->SetFocus();
4044 */
4045 }
4046
4047 if ( flags & wxPG_SEL_FOCUS )
4048 m_editorFocused = 1;
4049
4050 }
4051 else
4052 {
4053 // Make sure focus is in grid canvas (important for wxGTK,
4054 // at least)
4055 SetFocusOnCanvas();
4056 }
4057
4058 EditorsValueWasNotModified();
4059
4060 // If it's inside collapsed section, expand parent, scroll, etc.
4061 // Also, if it was partially visible, scroll it into view.
4062 if ( !(flags & wxPG_SEL_NONVISIBLE) )
4063 EnsureVisible( p );
4064
4065 if ( m_wndEditor )
4066 {
4067 m_wndEditor->Show(true);
4068 }
4069
4070 if ( !(flags & wxPG_SEL_NO_REFRESH) )
4071 DrawItem(p);
4072 }
4073 else
4074 {
4075 // Make sure focus is in grid canvas
4076 SetFocusOnCanvas();
4077 }
4078
4079 ClearInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
4080 }
4081
4082 #if wxUSE_STATUSBAR
4083
4084 //
4085 // Show help text in status bar.
4086 // (if found and grid not embedded in manager with help box and
4087 // style wxPG_EX_HELP_AS_TOOLTIPS is not used).
4088 //
4089
4090 if ( !(GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS) )
4091 {
4092 wxStatusBar* statusbar = NULL;
4093 if ( !(m_iFlags & wxPG_FL_NOSTATUSBARHELP) )
4094 {
4095 wxFrame* frame = wxDynamicCast(::wxGetTopLevelParent(this),wxFrame);
4096 if ( frame )
4097 statusbar = frame->GetStatusBar();
4098 }
4099
4100 if ( statusbar )
4101 {
4102 const wxString* pHelpString = (const wxString*) NULL;
4103
4104 if ( p )
4105 {
4106 pHelpString = &p->GetHelpString();
4107 if ( pHelpString->length() )
4108 {
4109 // Set help box text.
4110 statusbar->SetStatusText( *pHelpString );
4111 m_iFlags |= wxPG_FL_STRING_IN_STATUSBAR;
4112 }
4113 }
4114
4115 if ( (!pHelpString || !pHelpString->length()) &&
4116 (m_iFlags & wxPG_FL_STRING_IN_STATUSBAR) )
4117 {
4118 // Clear help box - but only if it was written
4119 // by us at previous time.
4120 statusbar->SetStatusText( m_emptyString );
4121 m_iFlags &= ~(wxPG_FL_STRING_IN_STATUSBAR);
4122 }
4123 }
4124 }
4125 #endif
4126
4127 m_inDoSelectProperty = 0;
4128
4129 // call wx event handler (here so that it also occurs on deselection)
4130 if ( !(flags & wxPG_SEL_DONT_SEND_EVENT) )
4131 SendEvent( wxEVT_PG_SELECTED, p, NULL );
4132
4133 return true;
4134 }
4135
4136 // -----------------------------------------------------------------------
4137
4138 bool wxPropertyGrid::UnfocusEditor()
4139 {
4140 wxPGProperty* selected = GetSelection();
4141
4142 if ( !selected || !m_wndEditor || m_frozen )
4143 return true;
4144
4145 if ( !CommitChangesFromEditor(0) )
4146 return false;
4147
4148 SetFocusOnCanvas();
4149 DrawItem(selected);
4150
4151 return true;
4152 }
4153
4154 // -----------------------------------------------------------------------
4155
4156 void wxPropertyGrid::RefreshEditor()
4157 {
4158 wxPGProperty* p = GetSelection();
4159 if ( !p )
4160 return;
4161
4162 wxWindow* wnd = GetEditorControl();
4163 if ( !wnd )
4164 return;
4165
4166 // Set editor font boldness - must do this before
4167 // calling UpdateControl().
4168 if ( HasFlag(wxPG_BOLD_MODIFIED) )
4169 {
4170 if ( p->HasFlag(wxPG_PROP_MODIFIED) )
4171 wnd->SetFont(GetCaptionFont());
4172 else
4173 wnd->SetFont(GetFont());
4174 }
4175
4176 const wxPGEditor* editorClass = p->GetEditorClass();
4177
4178 editorClass->UpdateControl(p, wnd);
4179
4180 if ( p->IsValueUnspecified() )
4181 SetEditorAppearance(m_unspecifiedAppearance, true);
4182 }
4183
4184 // -----------------------------------------------------------------------
4185
4186 bool wxPropertyGrid::SelectProperty( wxPGPropArg id, bool focus )
4187 {
4188 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
4189
4190 int flags = wxPG_SEL_DONT_SEND_EVENT;
4191 if ( focus )
4192 flags |= wxPG_SEL_FOCUS;
4193
4194 return DoSelectProperty(p, flags);
4195 }
4196
4197 // -----------------------------------------------------------------------
4198 // wxPropertyGrid expand/collapse state
4199 // -----------------------------------------------------------------------
4200
4201 bool wxPropertyGrid::DoCollapse( wxPGProperty* p, bool sendEvents )
4202 {
4203 wxPGProperty* pwc = wxStaticCast(p, wxPGProperty);
4204 wxPGProperty* selected = GetSelection();
4205
4206 // If active editor was inside collapsed section, then disable it
4207 if ( selected && selected->IsSomeParent(p) )
4208 {
4209 DoClearSelection();
4210 }
4211
4212 // Store dont-center-splitter flag 'cause we need to temporarily set it
4213 bool prevDontCenterSplitter = m_pState->m_dontCenterSplitter;
4214 m_pState->m_dontCenterSplitter = true;
4215
4216 bool res = m_pState->DoCollapse(pwc);
4217
4218 if ( res )
4219 {
4220 if ( sendEvents )
4221 SendEvent( wxEVT_PG_ITEM_COLLAPSED, p );
4222
4223 RecalculateVirtualSize();
4224 Refresh();
4225 }
4226
4227 m_pState->m_dontCenterSplitter = prevDontCenterSplitter;
4228
4229 return res;
4230 }
4231
4232 // -----------------------------------------------------------------------
4233
4234 bool wxPropertyGrid::DoExpand( wxPGProperty* p, bool sendEvents )
4235 {
4236 wxCHECK_MSG( p, false, wxT("invalid property id") );
4237
4238 wxPGProperty* pwc = (wxPGProperty*)p;
4239
4240 // Store dont-center-splitter flag 'cause we need to temporarily set it
4241 bool prevDontCenterSplitter = m_pState->m_dontCenterSplitter;
4242 m_pState->m_dontCenterSplitter = true;
4243
4244 bool res = m_pState->DoExpand(pwc);
4245
4246 if ( res )
4247 {
4248 if ( sendEvents )
4249 SendEvent( wxEVT_PG_ITEM_EXPANDED, p );
4250
4251 RecalculateVirtualSize();
4252 Refresh();
4253 }
4254
4255 m_pState->m_dontCenterSplitter = prevDontCenterSplitter;
4256
4257 return res;
4258 }
4259
4260 // -----------------------------------------------------------------------
4261
4262 bool wxPropertyGrid::DoHideProperty( wxPGProperty* p, bool hide, int flags )
4263 {
4264 if ( m_frozen )
4265 return m_pState->DoHideProperty(p, hide, flags);
4266
4267 wxArrayPGProperty selection = m_pState->m_selection; // Must use a copy
4268 int selRemoveCount = 0;
4269 for ( unsigned int i=0; i<selection.size(); i++ )
4270 {
4271 wxPGProperty* selected = selection[i];
4272 if ( selected == p || selected->IsSomeParent(p) )
4273 {
4274 if ( !DoRemoveFromSelection(p, flags) )
4275 return false;
4276 selRemoveCount += 1;
4277 }
4278 }
4279
4280 m_pState->DoHideProperty(p, hide, flags);
4281
4282 RecalculateVirtualSize();
4283 Refresh();
4284
4285 return true;
4286 }
4287
4288
4289 // -----------------------------------------------------------------------
4290 // wxPropertyGrid size related methods
4291 // -----------------------------------------------------------------------
4292
4293 void wxPropertyGrid::RecalculateVirtualSize( int forceXPos )
4294 {
4295 // Don't check for !HasInternalFlag(wxPG_FL_INITIALIZED) here. Otherwise
4296 // virtual size calculation may go wrong.
4297 if ( HasInternalFlag(wxPG_FL_RECALCULATING_VIRTUAL_SIZE) ||
4298 m_frozen ||
4299 !m_pState )
4300 return;
4301
4302 //
4303 // If virtual height was changed, then recalculate editor control position(s)
4304 if ( m_pState->m_vhCalcPending )
4305 CorrectEditorWidgetPosY();
4306
4307 m_pState->EnsureVirtualHeight();
4308
4309 wxASSERT_LEVEL_2_MSG(
4310 m_pState->GetVirtualHeight() == m_pState->GetActualVirtualHeight(),
4311 "VirtualHeight and ActualVirtualHeight should match"
4312 );
4313
4314 m_iFlags |= wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4315
4316 int x = m_pState->m_width;
4317 int y = m_pState->m_virtualHeight;
4318
4319 int width, height;
4320 GetClientSize(&width,&height);
4321
4322 // Now adjust virtual size.
4323 SetVirtualSize(x, y);
4324
4325 int xAmount = 0;
4326 int xPos = 0;
4327
4328 //
4329 // Adjust scrollbars
4330 if ( HasVirtualWidth() )
4331 {
4332 xAmount = x/wxPG_PIXELS_PER_UNIT;
4333 xPos = GetScrollPos( wxHORIZONTAL );
4334 }
4335
4336 if ( forceXPos != -1 )
4337 xPos = forceXPos;
4338 // xPos too high?
4339 else if ( xPos > (xAmount-(width/wxPG_PIXELS_PER_UNIT)) )
4340 xPos = 0;
4341
4342 int yAmount = y / wxPG_PIXELS_PER_UNIT;
4343 int yPos = GetScrollPos( wxVERTICAL );
4344
4345 SetScrollbars( wxPG_PIXELS_PER_UNIT, wxPG_PIXELS_PER_UNIT,
4346 xAmount, yAmount, xPos, yPos, true );
4347
4348 // Must re-get size now
4349 GetClientSize(&width,&height);
4350
4351 if ( !HasVirtualWidth() )
4352 {
4353 m_pState->SetVirtualWidth(width);
4354 x = width;
4355 }
4356
4357 m_width = width;
4358 m_height = height;
4359
4360 m_pState->CheckColumnWidths();
4361
4362 if ( GetSelection() )
4363 CorrectEditorWidgetSizeX();
4364
4365 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4366 }
4367
4368 // -----------------------------------------------------------------------
4369
4370 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4371 {
4372 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4373 return;
4374
4375 int width, height;
4376 GetClientSize(&width, &height);
4377
4378 m_width = width;
4379 m_height = height;
4380
4381 #if wxPG_DOUBLE_BUFFER
4382 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4383 {
4384 int dblh = (m_lineHeight*2);
4385 if ( !m_doubleBuffer )
4386 {
4387 // Create double buffer bitmap to draw on, if none
4388 int w = (width>250)?width:250;
4389 int h = height + dblh;
4390 h = (h>400)?h:400;
4391 m_doubleBuffer = new wxBitmap( w, h );
4392 }
4393 else
4394 {
4395 int w = m_doubleBuffer->GetWidth();
4396 int h = m_doubleBuffer->GetHeight();
4397
4398 // Double buffer must be large enough
4399 if ( w < width || h < (height+dblh) )
4400 {
4401 if ( w < width ) w = width;
4402 if ( h < (height+dblh) ) h = height + dblh;
4403 delete m_doubleBuffer;
4404 m_doubleBuffer = new wxBitmap( w, h );
4405 }
4406 }
4407 }
4408
4409 #endif
4410
4411 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4412 m_ncWidth = event.GetSize().x;
4413
4414 if ( !m_frozen )
4415 {
4416 if ( m_pState->m_itemsAdded )
4417 PrepareAfterItemsAdded();
4418 else
4419 // Without this, virtual size (atleast under wxGTK) will be skewed
4420 RecalculateVirtualSize();
4421
4422 Refresh();
4423 }
4424 }
4425
4426 // -----------------------------------------------------------------------
4427
4428 void wxPropertyGrid::SetVirtualWidth( int width )
4429 {
4430 if ( width == -1 )
4431 {
4432 // Disable virtual width
4433 width = GetClientSize().x;
4434 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4435 }
4436 else
4437 {
4438 // Enable virtual width
4439 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4440 }
4441 m_pState->SetVirtualWidth( width );
4442 }
4443
4444 void wxPropertyGrid::SetFocusOnCanvas()
4445 {
4446 SetFocusIgnoringChildren();
4447 m_editorFocused = 0;
4448 }
4449
4450 // -----------------------------------------------------------------------
4451 // wxPropertyGrid mouse event handling
4452 // -----------------------------------------------------------------------
4453
4454 // selFlags uses same values DoSelectProperty's flags
4455 // Returns true if event was vetoed.
4456 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p,
4457 wxVariant* pValue,
4458 unsigned int selFlags,
4459 unsigned int column )
4460 {
4461 // selFlags should have wxPG_SEL_NOVALIDATE if event is not
4462 // vetoable.
4463
4464 // Send property grid event of specific type and with specific property
4465 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4466 evt.SetPropertyGrid(this);
4467 evt.SetEventObject(m_eventObject);
4468 evt.SetProperty(p);
4469 evt.SetColumn(column);
4470 if ( eventType == wxEVT_PG_CHANGING )
4471 {
4472 wxASSERT( pValue );
4473 evt.SetCanVeto(true);
4474 m_validationInfo.m_pValue = pValue;
4475 evt.SetupValidationInfo();
4476 }
4477 else
4478 {
4479 if ( p )
4480 evt.SetPropertyValue(p->GetValue());
4481
4482 if ( !(selFlags & wxPG_SEL_NOVALIDATE) )
4483 evt.SetCanVeto(true);
4484 }
4485
4486 m_processedEvent = &evt;
4487 m_eventObject->HandleWindowEvent(evt);
4488 m_processedEvent = NULL;
4489
4490 return evt.WasVetoed();
4491 }
4492
4493 // -----------------------------------------------------------------------
4494
4495 // Return false if should be skipped
4496 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4497 {
4498 bool res = true;
4499
4500 // Need to set focus?
4501 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4502 {
4503 SetFocusOnCanvas();
4504 }
4505
4506 wxPropertyGridPageState* state = m_pState;
4507 int splitterHit;
4508 int splitterHitOffset;
4509 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4510
4511 wxPGProperty* p = DoGetItemAtY(y);
4512
4513 if ( p )
4514 {
4515 int depth = (int)p->GetDepth() - 1;
4516
4517 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4518
4519 if ( x >= marginEnds )
4520 {
4521 // Outside margin.
4522
4523 if ( p->IsCategory() )
4524 {
4525 // This is category.
4526 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4527
4528 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4529
4530 // Expand, collapse, activate etc. if click on text or left of splitter.
4531 if ( x >= textX
4532 &&
4533 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4534 columnHit == 0
4535 )
4536 )
4537 {
4538 if ( !AddToSelectionFromInputEvent( p,
4539 columnHit,
4540 &event ) )
4541 return res;
4542
4543 // On double-click, expand/collapse.
4544 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4545 {
4546 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4547 else DoExpand( p, true );
4548 }
4549 }
4550 }
4551 else if ( splitterHit == -1 )
4552 {
4553 // Click on value.
4554 unsigned int selFlag = 0;
4555 if ( columnHit == 1 )
4556 {
4557 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4558 selFlag = wxPG_SEL_FOCUS;
4559 }
4560 if ( !AddToSelectionFromInputEvent( p,
4561 columnHit,
4562 &event,
4563 selFlag ) )
4564 return res;
4565
4566 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4567
4568 if ( p->GetChildCount() && !p->IsCategory() )
4569 // On double-click, expand/collapse.
4570 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4571 {
4572 wxPGProperty* pwc = (wxPGProperty*)p;
4573 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4574 else DoExpand( p, true );
4575 }
4576
4577 res = false;
4578 }
4579 else
4580 {
4581 // click on splitter
4582 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4583 {
4584 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4585 {
4586 // Double-clicking the splitter causes auto-centering
4587 if ( m_pState->GetColumnCount() <= 2 )
4588 {
4589 CenterSplitter( true );
4590
4591 SendEvent(wxEVT_PG_COL_DRAGGING,
4592 m_propHover,
4593 NULL,
4594 wxPG_SEL_NOVALIDATE,
4595 (unsigned int)m_draggedSplitter);
4596 }
4597 }
4598 else if ( m_dragStatus == 0 )
4599 {
4600 //
4601 // Begin draggin the splitter
4602 //
4603
4604 // send event
4605 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
4606
4607 // Allow application to veto dragging
4608 if ( !SendEvent(wxEVT_PG_COL_BEGIN_DRAG,
4609 p, NULL, 0,
4610 (unsigned int)splitterHit) )
4611 {
4612 if ( m_wndEditor )
4613 {
4614 // Changes must be committed here or the
4615 // value won't be drawn correctly
4616 if ( !CommitChangesFromEditor() )
4617 return res;
4618
4619 m_wndEditor->Show ( false );
4620 }
4621
4622 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4623 {
4624 CaptureMouse();
4625 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4626 }
4627
4628 m_dragStatus = 1;
4629 m_draggedSplitter = splitterHit;
4630 m_dragOffset = splitterHitOffset;
4631
4632 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4633 // Fixes button disappearance bug
4634 if ( m_wndEditor2 )
4635 m_wndEditor2->Show ( false );
4636 #endif
4637
4638 m_startingSplitterX = x - splitterHitOffset;
4639 }
4640 }
4641 }
4642 }
4643 }
4644 else
4645 {
4646 // Click on margin.
4647 if ( p->GetChildCount() )
4648 {
4649 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4650
4651 // Fine tune cell button x
4652 if ( !p->IsCategory() )
4653 nx -= IN_CELL_EXPANDER_BUTTON_X_ADJUST;
4654
4655 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4656 {
4657 int y2 = y % m_lineHeight;
4658 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4659 {
4660 // On click on expander button, expand/collapse
4661 if ( ((wxPGProperty*)p)->IsExpanded() )
4662 DoCollapse( p, true );
4663 else
4664 DoExpand( p, true );
4665 }
4666 }
4667 }
4668 }
4669 }
4670 return res;
4671 }
4672
4673 // -----------------------------------------------------------------------
4674
4675 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x),
4676 unsigned int WXUNUSED(y),
4677 wxMouseEvent& event )
4678 {
4679 if ( m_propHover )
4680 {
4681 // Select property here as well
4682 wxPGProperty* p = m_propHover;
4683 AddToSelectionFromInputEvent(p, m_colHover, &event);
4684
4685 // Send right click event.
4686 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4687
4688 return true;
4689 }
4690 return false;
4691 }
4692
4693 // -----------------------------------------------------------------------
4694
4695 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x),
4696 unsigned int WXUNUSED(y),
4697 wxMouseEvent& event )
4698 {
4699 if ( m_propHover )
4700 {
4701 // Select property here as well
4702 wxPGProperty* p = m_propHover;
4703
4704 AddToSelectionFromInputEvent(p, m_colHover, &event);
4705
4706 // Send double-click event.
4707 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4708
4709 return true;
4710 }
4711 return false;
4712 }
4713
4714 // -----------------------------------------------------------------------
4715
4716 #if wxPG_SUPPORT_TOOLTIPS
4717
4718 void wxPropertyGrid::SetToolTip( const wxString& tipString )
4719 {
4720 if ( tipString.length() )
4721 {
4722 wxScrolledWindow::SetToolTip(tipString);
4723 }
4724 else
4725 {
4726 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4727 wxScrolledWindow::SetToolTip( m_emptyString );
4728 #else
4729 wxScrolledWindow::SetToolTip( NULL );
4730 #endif
4731 }
4732 }
4733
4734 #endif // #if wxPG_SUPPORT_TOOLTIPS
4735
4736 // -----------------------------------------------------------------------
4737
4738 // Return false if should be skipped
4739 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y,
4740 wxMouseEvent &event )
4741 {
4742 // Safety check (needed because mouse capturing may
4743 // otherwise freeze the control)
4744 if ( m_dragStatus > 0 && !event.Dragging() )
4745 {
4746 HandleMouseUp(x, y, event);
4747 }
4748
4749 wxPropertyGridPageState* state = m_pState;
4750 int splitterHit;
4751 int splitterHitOffset;
4752 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4753 int splitterX = x - splitterHitOffset;
4754
4755 m_colHover = columnHit;
4756
4757 if ( m_dragStatus > 0 )
4758 {
4759 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4760 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4761 {
4762
4763 int newSplitterX = x - m_dragOffset;
4764 int splitterX = x - splitterHitOffset;
4765
4766 // Splitter redraw required?
4767 if ( newSplitterX != splitterX )
4768 {
4769 // Move everything
4770 DoSetSplitterPosition(newSplitterX,
4771 m_draggedSplitter,
4772 wxPG_SPLITTER_REFRESH |
4773 wxPG_SPLITTER_FROM_EVENT);
4774
4775 SendEvent(wxEVT_PG_COL_DRAGGING,
4776 m_propHover,
4777 NULL,
4778 wxPG_SEL_NOVALIDATE,
4779 (unsigned int)m_draggedSplitter);
4780 }
4781
4782 m_dragStatus = 2;
4783 }
4784
4785 return false;
4786 }
4787 else
4788 {
4789
4790 int ih = m_lineHeight;
4791 int sy = y;
4792
4793 #if wxPG_SUPPORT_TOOLTIPS
4794 wxPGProperty* prevHover = m_propHover;
4795 unsigned char prevSide = m_mouseSide;
4796 #endif
4797 int curPropHoverY = y - (y % ih);
4798
4799 // On which item it hovers
4800 if ( !m_propHover
4801 ||
4802 ( sy < m_propHoverY || sy >= (m_propHoverY+ih) )
4803 )
4804 {
4805 // Mouse moves on another property
4806
4807 m_propHover = DoGetItemAtY(y);
4808 m_propHoverY = curPropHoverY;
4809
4810 // Send hover event
4811 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
4812 }
4813
4814 #if wxPG_SUPPORT_TOOLTIPS
4815 // Store which side we are on
4816 m_mouseSide = 0;
4817 if ( columnHit == 1 )
4818 m_mouseSide = 2;
4819 else if ( columnHit == 0 )
4820 m_mouseSide = 1;
4821
4822 //
4823 // If tooltips are enabled, show label or value as a tip
4824 // in case it doesn't otherwise show in full length.
4825 //
4826 if ( m_windowStyle & wxPG_TOOLTIPS )
4827 {
4828 wxToolTip* tooltip = GetToolTip();
4829
4830 if ( m_propHover != prevHover || prevSide != m_mouseSide )
4831 {
4832 if ( m_propHover && !m_propHover->IsCategory() )
4833 {
4834
4835 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
4836 {
4837 // Show help string as a tooltip
4838 wxString tipString = m_propHover->GetHelpString();
4839
4840 SetToolTip(tipString);
4841 }
4842 else
4843 {
4844 // Show cropped value string as a tooltip
4845 wxString tipString;
4846 int space = 0;
4847
4848 if ( m_mouseSide == 1 )
4849 {
4850 tipString = m_propHover->m_label;
4851 space = splitterX-m_marginWidth-3;
4852 }
4853 else if ( m_mouseSide == 2 )
4854 {
4855 tipString = m_propHover->GetDisplayedString();
4856
4857 space = m_width - splitterX;
4858 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
4859 space -= wxPG_CUSTOM_IMAGE_WIDTH +
4860 wxCC_CUSTOM_IMAGE_MARGIN1 +
4861 wxCC_CUSTOM_IMAGE_MARGIN2;
4862 }
4863
4864 if ( space )
4865 {
4866 int tw, th;
4867 GetTextExtent( tipString, &tw, &th, 0, 0 );
4868 if ( tw > space )
4869 {
4870 SetToolTip( tipString );
4871 }
4872 }
4873 else
4874 {
4875 if ( tooltip )
4876 {
4877 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4878 SetToolTip( m_emptyString );
4879 #else
4880 wxScrolledWindow::SetToolTip( NULL );
4881 #endif
4882 }
4883 }
4884
4885 }
4886 }
4887 else
4888 {
4889 if ( tooltip )
4890 {
4891 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4892 SetToolTip( m_emptyString );
4893 #else
4894 wxScrolledWindow::SetToolTip( NULL );
4895 #endif
4896 }
4897 }
4898 }
4899 }
4900 #endif
4901
4902 if ( splitterHit == -1 ||
4903 !m_propHover ||
4904 HasFlag(wxPG_STATIC_SPLITTER) )
4905 {
4906 // hovering on something else
4907 if ( m_curcursor != wxCURSOR_ARROW )
4908 CustomSetCursor( wxCURSOR_ARROW );
4909 }
4910 else
4911 {
4912 // Do not allow splitter cursor on caption items.
4913 // (also not if we were dragging and its started
4914 // outside the splitter region)
4915
4916 if ( !m_propHover->IsCategory() &&
4917 !event.Dragging() )
4918 {
4919
4920 // hovering on splitter
4921
4922 // NB: Condition disabled since MouseLeave event (from the
4923 // editor control) cannot be reliably detected.
4924 //if ( m_curcursor != wxCURSOR_SIZEWE )
4925 CustomSetCursor( wxCURSOR_SIZEWE, true );
4926
4927 return false;
4928 }
4929 else
4930 {
4931 // hovering on something else
4932 if ( m_curcursor != wxCURSOR_ARROW )
4933 CustomSetCursor( wxCURSOR_ARROW );
4934 }
4935 }
4936
4937 //
4938 // Multi select by dragging
4939 //
4940 if ( (GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION) &&
4941 event.LeftIsDown() &&
4942 m_propHover &&
4943 GetSelection() &&
4944 columnHit != 1 &&
4945 !state->DoIsPropertySelected(m_propHover) )
4946 {
4947 // Additional requirement is that the hovered property
4948 // is adjacent to edges of selection.
4949 const wxArrayPGProperty& selection = GetSelectedProperties();
4950
4951 // Since categories cannot be selected along with 'other'
4952 // properties, exclude them from iterator flags.
4953 int iterFlags = wxPG_ITERATE_VISIBLE & (~wxPG_PROP_CATEGORY);
4954
4955 for ( int i=(selection.size()-1); i>=0; i-- )
4956 {
4957 // TODO: This could be optimized by keeping track of
4958 // which properties are at the edges of selection.
4959 wxPGProperty* selProp = selection[i];
4960 if ( state->ArePropertiesAdjacent(m_propHover, selProp,
4961 iterFlags) )
4962 {
4963 DoAddToSelection(m_propHover);
4964 break;
4965 }
4966 }
4967 }
4968 }
4969 return true;
4970 }
4971
4972 // -----------------------------------------------------------------------
4973
4974 // Also handles Leaving event
4975 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
4976 wxMouseEvent &WXUNUSED(event) )
4977 {
4978 wxPropertyGridPageState* state = m_pState;
4979 bool res = false;
4980
4981 int splitterHit;
4982 int splitterHitOffset;
4983 state->HitTestH( x, &splitterHit, &splitterHitOffset );
4984
4985 // No event type check - basicly calling this method should
4986 // just stop dragging.
4987 // Left up after dragged?
4988 if ( m_dragStatus >= 1 )
4989 {
4990 //
4991 // End Splitter Dragging
4992 //
4993 // DO NOT ENABLE FOLLOWING LINE!
4994 // (it is only here as a reminder to not to do it)
4995 //splitterX = x;
4996
4997 SendEvent(wxEVT_PG_COL_END_DRAG,
4998 m_propHover,
4999 NULL,
5000 wxPG_SEL_NOVALIDATE,
5001 (unsigned int)m_draggedSplitter);
5002
5003 // Disable splitter auto-centering
5004 state->m_dontCenterSplitter = true;
5005
5006 // This is necessary to return cursor
5007 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5008 {
5009 ReleaseMouse();
5010 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5011 }
5012
5013 // Set back the default cursor, if necessary
5014 if ( splitterHit == -1 ||
5015 !m_propHover )
5016 {
5017 CustomSetCursor( wxCURSOR_ARROW );
5018 }
5019
5020 m_dragStatus = 0;
5021
5022 // Control background needs to be cleared
5023 wxPGProperty* selected = GetSelection();
5024 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && selected )
5025 DrawItem( selected );
5026
5027 if ( m_wndEditor )
5028 {
5029 m_wndEditor->Show ( true );
5030 }
5031
5032 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
5033 // Fixes button disappearance bug
5034 if ( m_wndEditor2 )
5035 m_wndEditor2->Show ( true );
5036 #endif
5037
5038 // This clears the focus.
5039 m_editorFocused = 0;
5040
5041 }
5042 return res;
5043 }
5044
5045 // -----------------------------------------------------------------------
5046
5047 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
5048 {
5049 int splitterX = GetSplitterPosition();
5050
5051 int ux, uy;
5052 CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
5053
5054 wxWindow* wnd = GetEditorControl();
5055
5056 // Hide popup on clicks
5057 if ( event.GetEventType() != wxEVT_MOTION )
5058 if ( wnd && wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
5059 {
5060 ((wxOwnerDrawnComboBox*)wnd)->HidePopup();
5061 }
5062
5063 wxRect r;
5064 if ( wnd )
5065 r = wnd->GetRect();
5066 if ( wnd == NULL || m_dragStatus ||
5067 (
5068 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
5069 ux >= (r.x+r.width) ||
5070 event.m_y < r.y ||
5071 event.m_y >= (r.y+r.height)
5072 )
5073 )
5074 {
5075 *px = ux;
5076 *py = uy;
5077 return true;
5078 }
5079 else
5080 {
5081 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5082 }
5083 return false;
5084 }
5085
5086 // -----------------------------------------------------------------------
5087
5088 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
5089 {
5090 int x, y;
5091 if ( OnMouseCommon( event, &x, &y ) )
5092 {
5093 HandleMouseClick(x,y,event);
5094 }
5095 event.Skip();
5096 }
5097
5098 // -----------------------------------------------------------------------
5099
5100 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
5101 {
5102 int x, y;
5103 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5104 HandleMouseRightClick(x,y,event);
5105 event.Skip();
5106 }
5107
5108 // -----------------------------------------------------------------------
5109
5110 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
5111 {
5112 // Always run standard mouse-down handler as well
5113 OnMouseClick(event);
5114
5115 int x, y;
5116 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5117 HandleMouseDoubleClick(x,y,event);
5118 event.Skip();
5119 }
5120
5121 // -----------------------------------------------------------------------
5122
5123 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
5124 {
5125 int x, y;
5126 if ( OnMouseCommon( event, &x, &y ) )
5127 {
5128 HandleMouseMove(x,y,event);
5129 }
5130 event.Skip();
5131 }
5132
5133 // -----------------------------------------------------------------------
5134
5135 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
5136 {
5137 int x, y;
5138 if ( OnMouseCommon( event, &x, &y ) )
5139 {
5140 HandleMouseUp(x,y,event);
5141 }
5142 event.Skip();
5143 }
5144
5145 // -----------------------------------------------------------------------
5146
5147 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
5148 {
5149 // This may get called from child control as well, so event's
5150 // mouse position cannot be relied on.
5151
5152 if ( event.Entering() )
5153 {
5154 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5155 {
5156 // TODO: Fix this (detect parent and only do
5157 // cursor trick if it is a manager).
5158 wxASSERT( GetParent() );
5159 GetParent()->SetCursor(wxNullCursor);
5160
5161 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
5162 }
5163 else
5164 GetParent()->SetCursor(wxNullCursor);
5165 }
5166 else if ( event.Leaving() )
5167 {
5168 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
5169 SetCursor( wxNullCursor );
5170
5171 // Get real cursor position
5172 wxPoint pt = ScreenToClient(::wxGetMousePosition());
5173
5174 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
5175 {
5176 {
5177 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5178 {
5179 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
5180 }
5181
5182 if ( m_dragStatus )
5183 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
5184 }
5185 }
5186 }
5187
5188 event.Skip();
5189 }
5190
5191 // -----------------------------------------------------------------------
5192
5193 // Common code used by various OnMouseXXXChild methods.
5194 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
5195 {
5196 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
5197 wxASSERT( topCtrlWnd );
5198 int x, y;
5199 event.GetPosition(&x,&y);
5200
5201 int splitterX = GetSplitterPosition();
5202
5203 wxRect r = topCtrlWnd->GetRect();
5204 if ( !m_dragStatus &&
5205 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
5206 y >= 0 && y < r.height \
5207 )
5208 {
5209 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5210 event.Skip();
5211 }
5212 else
5213 {
5214 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
5215 px, py );
5216 return true;
5217 }
5218 return false;
5219 }
5220
5221 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
5222 {
5223 int x,y;
5224 if ( OnMouseChildCommon(event,&x,&y) )
5225 {
5226 bool res = HandleMouseClick(x,y,event);
5227 if ( !res ) event.Skip();
5228 }
5229 }
5230
5231 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
5232 {
5233 int x,y;
5234 wxASSERT( m_wndEditor );
5235 // These coords may not be exact (about +-2),
5236 // but that should not matter (right click is about item, not position).
5237 wxPoint pt = m_wndEditor->GetPosition();
5238 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
5239
5240 // FIXME: Used to set m_propHover to selection here. Was it really
5241 // necessary?
5242
5243 bool res = HandleMouseRightClick(x,y,event);
5244 if ( !res ) event.Skip();
5245 }
5246
5247 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
5248 {
5249 int x,y;
5250 if ( OnMouseChildCommon(event,&x,&y) )
5251 {
5252 bool res = HandleMouseMove(x,y,event);
5253 if ( !res ) event.Skip();
5254 }
5255 }
5256
5257 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
5258 {
5259 int x,y;
5260 if ( OnMouseChildCommon(event,&x,&y) )
5261 {
5262 bool res = HandleMouseUp(x,y,event);
5263 if ( !res ) event.Skip();
5264 }
5265 }
5266
5267 // -----------------------------------------------------------------------
5268 // wxPropertyGrid keyboard event handling
5269 // -----------------------------------------------------------------------
5270
5271 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
5272 {
5273 // Translates wxKeyEvent to wxPG_ACTION_XXX
5274
5275 int keycode = event.GetKeyCode();
5276 int modifiers = event.GetModifiers();
5277
5278 wxASSERT( !(modifiers&~(0xFFFF)) );
5279
5280 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5281
5282 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
5283
5284 if ( it == m_actionTriggers.end() )
5285 return 0;
5286
5287 if ( pSecond )
5288 {
5289 int second = (it->second>>16) & 0xFFFF;
5290 *pSecond = second;
5291 }
5292
5293 return (it->second & 0xFFFF);
5294 }
5295
5296 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
5297 {
5298 wxASSERT( !(modifiers&~(0xFFFF)) );
5299
5300 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5301
5302 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
5303
5304 if ( it != m_actionTriggers.end() )
5305 {
5306 // This key combination is already used
5307
5308 // Can add secondary?
5309 wxASSERT_MSG( !(it->second&~(0xFFFF)),
5310 wxT("You can only add up to two separate actions per key combination.") );
5311
5312 action = it->second | (action<<16);
5313 }
5314
5315 m_actionTriggers[hashMapKey] = action;
5316 }
5317
5318 void wxPropertyGrid::ClearActionTriggers( int action )
5319 {
5320 wxPGHashMapI2I::iterator it;
5321 bool didSomething;
5322
5323 do
5324 {
5325 didSomething = false;
5326
5327 for ( it = m_actionTriggers.begin();
5328 it != m_actionTriggers.end();
5329 it++ )
5330 {
5331 if ( it->second == action )
5332 {
5333 m_actionTriggers.erase(it);
5334 didSomething = true;
5335 break;
5336 }
5337 }
5338 }
5339 while ( didSomething );
5340 }
5341
5342 void wxPropertyGrid::HandleKeyEvent( wxKeyEvent &event, bool fromChild )
5343 {
5344 //
5345 // Handles key event when editor control is not focused.
5346 //
5347
5348 wxCHECK2(!m_frozen, return);
5349
5350 // Travelsal between items, collapsing/expanding, etc.
5351 wxPGProperty* selected = GetSelection();
5352 int keycode = event.GetKeyCode();
5353 bool editorFocused = IsEditorFocused();
5354
5355 if ( keycode == WXK_TAB )
5356 {
5357 wxWindow* mainControl;
5358
5359 if ( HasInternalFlag(wxPG_FL_IN_MANAGER) )
5360 mainControl = GetParent();
5361 else
5362 mainControl = this;
5363
5364 if ( !event.ShiftDown() )
5365 {
5366 if ( !editorFocused && m_wndEditor )
5367 {
5368 DoSelectProperty( selected, wxPG_SEL_FOCUS );
5369 }
5370 else
5371 {
5372 // Tab traversal workaround for platforms on which
5373 // wxWindow::Navigate() may navigate into first child
5374 // instead of next sibling. Does not work perfectly
5375 // in every scenario (for instance, when property grid
5376 // is either first or last control).
5377 #if defined(__WXGTK__)
5378 wxWindow* sibling = mainControl->GetNextSibling();
5379 if ( sibling )
5380 sibling->SetFocusFromKbd();
5381 #else
5382 Navigate(wxNavigationKeyEvent::IsForward);
5383 #endif
5384 }
5385 }
5386 else
5387 {
5388 if ( editorFocused )
5389 {
5390 UnfocusEditor();
5391 }
5392 else
5393 {
5394 #if defined(__WXGTK__)
5395 wxWindow* sibling = mainControl->GetPrevSibling();
5396 if ( sibling )
5397 sibling->SetFocusFromKbd();
5398 #else
5399 Navigate(wxNavigationKeyEvent::IsBackward);
5400 #endif
5401 }
5402 }
5403
5404 return;
5405 }
5406
5407 // Ignore Alt and Control when they are down alone
5408 if ( keycode == WXK_ALT ||
5409 keycode == WXK_CONTROL )
5410 {
5411 event.Skip();
5412 return;
5413 }
5414
5415 int secondAction;
5416 int action = KeyEventToActions(event, &secondAction);
5417
5418 if ( editorFocused && action == wxPG_ACTION_CANCEL_EDIT )
5419 {
5420 //
5421 // Esc cancels any changes
5422 if ( IsEditorsValueModified() )
5423 {
5424 EditorsValueWasNotModified();
5425
5426 // Update the control as well
5427 selected->GetEditorClass()->
5428 SetControlStringValue( selected,
5429 GetEditorControl(),
5430 selected->GetDisplayedString() );
5431 }
5432
5433 OnValidationFailureReset(selected);
5434
5435 UnfocusEditor();
5436 return;
5437 }
5438
5439 // Except for TAB and ESC, handle child control events in child control
5440 if ( fromChild )
5441 {
5442 // Only propagate event if it had modifiers
5443 if ( !event.HasModifiers() )
5444 {
5445 event.StopPropagation();
5446 }
5447 event.Skip();
5448 return;
5449 }
5450
5451 bool wasHandled = false;
5452
5453 if ( selected )
5454 {
5455 // Show dialog?
5456 if ( ButtonTriggerKeyTest(action, event) )
5457 return;
5458
5459 wxPGProperty* p = selected;
5460
5461 // Travel and expand/collapse
5462 int selectDir = -2;
5463
5464 if ( p->GetChildCount() )
5465 {
5466 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5467 {
5468 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5469 wasHandled = true;
5470 }
5471 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5472 {
5473 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5474 wasHandled = true;
5475 }
5476 }
5477
5478 if ( !wasHandled )
5479 {
5480 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5481 {
5482 selectDir = -1;
5483 }
5484 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5485 {
5486 selectDir = 1;
5487 }
5488 }
5489
5490 if ( selectDir >= -1 )
5491 {
5492 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5493 if ( p )
5494 DoSelectProperty(p);
5495 wasHandled = true;
5496 }
5497 }
5498 else
5499 {
5500 // If nothing was selected, select the first item now
5501 // (or navigate out of tab).
5502 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5503 {
5504 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5505 if ( p ) DoSelectProperty(p);
5506 wasHandled = true;
5507 }
5508 }
5509
5510 if ( !wasHandled )
5511 event.Skip();
5512 }
5513
5514 // -----------------------------------------------------------------------
5515
5516 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5517 {
5518 // If there was editor open and focused, then this event should not
5519 // really be processed here.
5520 if ( IsEditorFocused() )
5521 {
5522 // However, if event had modifiers, it is probably still best
5523 // to skip it.
5524 if ( event.HasModifiers() )
5525 event.Skip();
5526 else
5527 event.StopPropagation();
5528 return;
5529 }
5530
5531 HandleKeyEvent(event, false);
5532 }
5533
5534 // -----------------------------------------------------------------------
5535
5536 bool wxPropertyGrid::ButtonTriggerKeyTest( int action, wxKeyEvent& event )
5537 {
5538 if ( action == -1 )
5539 {
5540 int secondAction;
5541 action = KeyEventToActions(event, &secondAction);
5542 }
5543
5544 // Does the keycode trigger button?
5545 if ( action == wxPG_ACTION_PRESS_BUTTON &&
5546 m_wndEditor2 )
5547 {
5548 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, m_wndEditor2->GetId());
5549 GetEventHandler()->AddPendingEvent(evt);
5550 return true;
5551 }
5552
5553 return false;
5554 }
5555
5556 // -----------------------------------------------------------------------
5557
5558 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5559 {
5560 HandleKeyEvent(event, true);
5561 }
5562
5563 // -----------------------------------------------------------------------
5564 // wxPropertyGrid miscellaneous event handling
5565 // -----------------------------------------------------------------------
5566
5567 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5568 {
5569 //
5570 // Check if the focus is in this control or one of its children
5571 wxWindow* newFocused = wxWindow::FindFocus();
5572
5573 if ( newFocused != m_curFocused )
5574 HandleFocusChange( newFocused );
5575
5576 //
5577 // Check if top-level parent has changed
5578 if ( GetExtraStyle() & wxPG_EX_ENABLE_TLP_TRACKING )
5579 {
5580 wxWindow* tlp = ::wxGetTopLevelParent(this);
5581 if ( tlp != m_tlp )
5582 OnTLPChanging(tlp);
5583 }
5584 }
5585
5586 bool wxPropertyGrid::IsEditorFocused() const
5587 {
5588 wxWindow* focus = wxWindow::FindFocus();
5589
5590 if ( focus == m_wndEditor || focus == m_wndEditor2 ||
5591 focus == GetEditorControl() )
5592 return true;
5593
5594 return false;
5595 }
5596
5597 // Called by focus event handlers. newFocused is the window that becomes focused.
5598 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5599 {
5600 unsigned int oldFlags = m_iFlags;
5601 bool wasEditorFocused = false;
5602 wxWindow* wndEditor = m_wndEditor;
5603
5604 m_iFlags &= ~(wxPG_FL_FOCUSED);
5605
5606 wxWindow* parent = newFocused;
5607
5608 // This must be one of nextFocus' parents.
5609 while ( parent )
5610 {
5611 if ( parent == wndEditor )
5612 {
5613 wasEditorFocused = true;
5614 }
5615 // Use m_eventObject, which is either wxPropertyGrid or
5616 // wxPropertyGridManager, as appropriate.
5617 else if ( parent == m_eventObject )
5618 {
5619 m_iFlags |= wxPG_FL_FOCUSED;
5620 break;
5621 }
5622 parent = parent->GetParent();
5623 }
5624
5625 // Notify editor control when it receives a focus
5626 if ( wasEditorFocused && m_curFocused != newFocused )
5627 {
5628 wxPGProperty* p = GetSelection();
5629 if ( p )
5630 {
5631 const wxPGEditor* editor = p->GetEditorClass();
5632 editor->OnFocus(p, GetEditorControl());
5633 }
5634 }
5635
5636 m_curFocused = newFocused;
5637
5638 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5639 (oldFlags & wxPG_FL_FOCUSED) )
5640 {
5641 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5642 {
5643 // Need to store changed value
5644 CommitChangesFromEditor();
5645 }
5646 else
5647 {
5648 /*
5649 //
5650 // Preliminary code for tab-order respecting
5651 // tab-traversal (but should be moved to
5652 // OnNav handler)
5653 //
5654 wxWindow* prevFocus = event.GetWindow();
5655 wxWindow* useThis = this;
5656 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5657 useThis = GetParent();
5658
5659 if ( prevFocus &&
5660 prevFocus->GetParent() == useThis->GetParent() )
5661 {
5662 wxList& children = useThis->GetParent()->GetChildren();
5663
5664 wxNode* node = children.Find(prevFocus);
5665
5666 if ( node->GetNext() &&
5667 useThis == node->GetNext()->GetData() )
5668 DoSelectProperty(GetFirst());
5669 else if ( node->GetPrevious () &&
5670 useThis == node->GetPrevious()->GetData() )
5671 DoSelectProperty(GetLastProperty());
5672
5673 }
5674 */
5675 }
5676
5677 // Redraw selected
5678 wxPGProperty* selected = GetSelection();
5679 if ( selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5680 DrawItem( selected );
5681 }
5682 }
5683
5684 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5685 {
5686 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5687 HandleFocusChange((wxWindow*)event.GetEventObject());
5688 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5689 //else if ( event.GetWindow() )
5690 else
5691 HandleFocusChange(event.GetWindow());
5692
5693 event.Skip();
5694 }
5695
5696 // -----------------------------------------------------------------------
5697
5698 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5699 {
5700 HandleFocusChange((wxWindow*)event.GetEventObject());
5701 event.Skip();
5702 }
5703
5704 // -----------------------------------------------------------------------
5705
5706 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5707 {
5708 m_iFlags |= wxPG_FL_SCROLLED;
5709
5710 event.Skip();
5711 }
5712
5713 // -----------------------------------------------------------------------
5714
5715 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5716 {
5717 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5718 {
5719 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5720 }
5721 }
5722
5723 // -----------------------------------------------------------------------
5724 // Property editor related functions
5725 // -----------------------------------------------------------------------
5726
5727 // noDefCheck = true prevents infinite recursion.
5728 wxPGEditor* wxPropertyGrid::DoRegisterEditorClass( wxPGEditor* editorClass,
5729 const wxString& editorName,
5730 bool noDefCheck )
5731 {
5732 wxASSERT( editorClass );
5733
5734 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5735 RegisterDefaultEditors();
5736
5737 wxString name = editorName;
5738 if ( name.length() == 0 )
5739 name = editorClass->GetName();
5740
5741 // Existing editor under this name?
5742 wxPGHashMapS2P::iterator vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5743
5744 if ( vt_it != wxPGGlobalVars->m_mapEditorClasses.end() )
5745 {
5746 // If this name was already used, try class name.
5747 name = editorClass->GetClassInfo()->GetClassName();
5748 vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5749 }
5750
5751 wxCHECK_MSG( vt_it == wxPGGlobalVars->m_mapEditorClasses.end(),
5752 (wxPGEditor*) vt_it->second,
5753 "Editor with given name was already registered" );
5754
5755 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorClass;
5756
5757 return editorClass;
5758 }
5759
5760 // Use this in RegisterDefaultEditors.
5761 #define wxPGRegisterDefaultEditorClass(EDITOR) \
5762 if ( wxPGEditor_##EDITOR == NULL ) \
5763 { \
5764 wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
5765 new wxPG##EDITOR##Editor, true ); \
5766 }
5767
5768 // Registers all default editor classes
5769 void wxPropertyGrid::RegisterDefaultEditors()
5770 {
5771 wxPGRegisterDefaultEditorClass( TextCtrl );
5772 wxPGRegisterDefaultEditorClass( Choice );
5773 wxPGRegisterDefaultEditorClass( ComboBox );
5774 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
5775 #if wxPG_INCLUDE_CHECKBOX
5776 wxPGRegisterDefaultEditorClass( CheckBox );
5777 #endif
5778 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
5779
5780 // Register SpinCtrl etc. editors before use
5781 RegisterAdditionalEditors();
5782 }
5783
5784 // -----------------------------------------------------------------------
5785 // wxPGStringTokenizer
5786 // Needed to handle C-style string lists (e.g. "str1" "str2")
5787 // -----------------------------------------------------------------------
5788
5789 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
5790 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
5791 {
5792 }
5793
5794 wxPGStringTokenizer::~wxPGStringTokenizer()
5795 {
5796 }
5797
5798 bool wxPGStringTokenizer::HasMoreTokens()
5799 {
5800 const wxString& str = *m_str;
5801
5802 wxString::const_iterator i = m_curPos;
5803
5804 wxUniChar delim = m_delimeter;
5805 wxUniChar a;
5806 wxUniChar prev_a = wxT('\0');
5807
5808 bool inToken = false;
5809
5810 while ( i != str.end() )
5811 {
5812 a = *i;
5813
5814 if ( !inToken )
5815 {
5816 if ( a == delim )
5817 {
5818 inToken = true;
5819 m_readyToken.clear();
5820 }
5821 }
5822 else
5823 {
5824 if ( prev_a != wxT('\\') )
5825 {
5826 if ( a != delim )
5827 {
5828 if ( a != wxT('\\') )
5829 m_readyToken << a;
5830 }
5831 else
5832 {
5833 ++i;
5834 m_curPos = i;
5835 return true;
5836 }
5837 prev_a = a;
5838 }
5839 else
5840 {
5841 m_readyToken << a;
5842 prev_a = wxT('\0');
5843 }
5844 }
5845 ++i;
5846 }
5847
5848 m_curPos = str.end();
5849
5850 if ( inToken )
5851 return true;
5852
5853 return false;
5854 }
5855
5856 wxString wxPGStringTokenizer::GetNextToken()
5857 {
5858 return m_readyToken;
5859 }
5860
5861 // -----------------------------------------------------------------------
5862 // wxPGChoiceEntry
5863 // -----------------------------------------------------------------------
5864
5865 wxPGChoiceEntry::wxPGChoiceEntry()
5866 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
5867 {
5868 }
5869
5870 // -----------------------------------------------------------------------
5871 // wxPGChoicesData
5872 // -----------------------------------------------------------------------
5873
5874 wxPGChoicesData::wxPGChoicesData()
5875 {
5876 }
5877
5878 wxPGChoicesData::~wxPGChoicesData()
5879 {
5880 Clear();
5881 }
5882
5883 void wxPGChoicesData::Clear()
5884 {
5885 m_items.clear();
5886 }
5887
5888 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
5889 {
5890 wxASSERT( m_items.size() == 0 );
5891
5892 m_items = data->m_items;
5893 }
5894
5895 wxPGChoiceEntry& wxPGChoicesData::Insert( int index,
5896 const wxPGChoiceEntry& item )
5897 {
5898 wxVector<wxPGChoiceEntry>::iterator it;
5899 if ( index == -1 )
5900 {
5901 it = m_items.end();
5902 index = (int) m_items.size();
5903 }
5904 else
5905 {
5906 it = m_items.begin() + index;
5907 }
5908
5909 m_items.insert(it, item);
5910
5911 wxPGChoiceEntry& ownEntry = m_items[index];
5912
5913 // Need to fix value?
5914 if ( ownEntry.GetValue() == wxPG_INVALID_VALUE )
5915 ownEntry.SetValue(index);
5916
5917 return ownEntry;
5918 }
5919
5920 // -----------------------------------------------------------------------
5921 // wxPropertyGridEvent
5922 // -----------------------------------------------------------------------
5923
5924 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
5925
5926
5927 wxDEFINE_EVENT( wxEVT_PG_SELECTED, wxPropertyGridEvent );
5928 wxDEFINE_EVENT( wxEVT_PG_CHANGING, wxPropertyGridEvent );
5929 wxDEFINE_EVENT( wxEVT_PG_CHANGED, wxPropertyGridEvent );
5930 wxDEFINE_EVENT( wxEVT_PG_HIGHLIGHTED, wxPropertyGridEvent );
5931 wxDEFINE_EVENT( wxEVT_PG_RIGHT_CLICK, wxPropertyGridEvent );
5932 wxDEFINE_EVENT( wxEVT_PG_PAGE_CHANGED, wxPropertyGridEvent );
5933 wxDEFINE_EVENT( wxEVT_PG_ITEM_EXPANDED, wxPropertyGridEvent );
5934 wxDEFINE_EVENT( wxEVT_PG_ITEM_COLLAPSED, wxPropertyGridEvent );
5935 wxDEFINE_EVENT( wxEVT_PG_DOUBLE_CLICK, wxPropertyGridEvent );
5936 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_BEGIN, wxPropertyGridEvent );
5937 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_ENDING, wxPropertyGridEvent );
5938 wxDEFINE_EVENT( wxEVT_PG_COL_BEGIN_DRAG, wxPropertyGridEvent );
5939 wxDEFINE_EVENT( wxEVT_PG_COL_DRAGGING, wxPropertyGridEvent );
5940 wxDEFINE_EVENT( wxEVT_PG_COL_END_DRAG, wxPropertyGridEvent );
5941
5942 // -----------------------------------------------------------------------
5943
5944 void wxPropertyGridEvent::Init()
5945 {
5946 m_validationInfo = NULL;
5947 m_column = 1;
5948 m_canVeto = false;
5949 m_wasVetoed = false;
5950 }
5951
5952 // -----------------------------------------------------------------------
5953
5954 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
5955 : wxCommandEvent(commandType,id)
5956 {
5957 m_property = NULL;
5958 Init();
5959 }
5960
5961 // -----------------------------------------------------------------------
5962
5963 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
5964 : wxCommandEvent(event)
5965 {
5966 m_eventType = event.GetEventType();
5967 m_eventObject = event.m_eventObject;
5968 m_pg = event.m_pg;
5969 OnPropertyGridSet();
5970 m_property = event.m_property;
5971 m_validationInfo = event.m_validationInfo;
5972 m_canVeto = event.m_canVeto;
5973 m_wasVetoed = event.m_wasVetoed;
5974 }
5975
5976 // -----------------------------------------------------------------------
5977
5978 void wxPropertyGridEvent::OnPropertyGridSet()
5979 {
5980 if ( !m_pg )
5981 return;
5982
5983 #if wxUSE_THREADS
5984 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
5985 #endif
5986 m_pg->m_liveEvents.push_back(this);
5987 }
5988
5989 // -----------------------------------------------------------------------
5990
5991 wxPropertyGridEvent::~wxPropertyGridEvent()
5992 {
5993 if ( m_pg )
5994 {
5995 #if wxUSE_THREADS
5996 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
5997 #endif
5998
5999 // Use iterate from the back since it is more likely that the event
6000 // being desroyed is at the end of the array.
6001 wxVector<wxPropertyGridEvent*>& liveEvents = m_pg->m_liveEvents;
6002
6003 for ( int i = liveEvents.size()-1; i >= 0; i-- )
6004 {
6005 if ( liveEvents[i] == this )
6006 {
6007 liveEvents.erase(liveEvents.begin() + i);
6008 break;
6009 }
6010 }
6011 }
6012 }
6013
6014 // -----------------------------------------------------------------------
6015
6016 wxEvent* wxPropertyGridEvent::Clone() const
6017 {
6018 return new wxPropertyGridEvent( *this );
6019 }
6020
6021 // -----------------------------------------------------------------------
6022 // wxPropertyGridPopulator
6023 // -----------------------------------------------------------------------
6024
6025 wxPropertyGridPopulator::wxPropertyGridPopulator()
6026 {
6027 m_state = NULL;
6028 m_pg = NULL;
6029 wxPGGlobalVars->m_offline++;
6030 }
6031
6032 // -----------------------------------------------------------------------
6033
6034 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
6035 {
6036 m_state = state;
6037 m_propHierarchy.clear();
6038 }
6039
6040 // -----------------------------------------------------------------------
6041
6042 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
6043 {
6044 m_pg = pg;
6045 pg->Freeze();
6046 }
6047
6048 // -----------------------------------------------------------------------
6049
6050 wxPropertyGridPopulator::~wxPropertyGridPopulator()
6051 {
6052 //
6053 // Free unused sets of choices
6054 wxPGHashMapS2P::iterator it;
6055
6056 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
6057 {
6058 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
6059 data->DecRef();
6060 }
6061
6062 if ( m_pg )
6063 {
6064 m_pg->Thaw();
6065 m_pg->GetPanel()->Refresh();
6066 }
6067 wxPGGlobalVars->m_offline--;
6068 }
6069
6070 // -----------------------------------------------------------------------
6071
6072 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
6073 const wxString& propLabel,
6074 const wxString& propName,
6075 const wxString* propValue,
6076 wxPGChoices* pChoices )
6077 {
6078 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
6079 wxPGProperty* parent = GetCurParent();
6080
6081 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
6082 {
6083 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
6084 return NULL;
6085 }
6086
6087 if ( !classInfo || !classInfo->IsKindOf(CLASSINFO(wxPGProperty)) )
6088 {
6089 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
6090 return NULL;
6091 }
6092
6093 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
6094
6095 property->SetLabel(propLabel);
6096 property->DoSetName(propName);
6097
6098 if ( pChoices && pChoices->IsOk() )
6099 property->SetChoices(*pChoices);
6100
6101 m_state->DoInsert(parent, -1, property);
6102
6103 if ( propValue )
6104 property->SetValueFromString( *propValue, wxPG_FULL_VALUE|
6105 wxPG_PROGRAMMATIC_VALUE );
6106
6107 return property;
6108 }
6109
6110 // -----------------------------------------------------------------------
6111
6112 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
6113 {
6114 m_propHierarchy.push_back(property);
6115 DoScanForChildren();
6116 m_propHierarchy.pop_back();
6117 }
6118
6119 // -----------------------------------------------------------------------
6120
6121 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
6122 const wxString& idString )
6123 {
6124 wxPGChoices choices;
6125
6126 // Using id?
6127 if ( choicesString[0] == wxT('@') )
6128 {
6129 wxString ids = choicesString.substr(1);
6130 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
6131 if ( it == m_dictIdChoices.end() )
6132 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
6133 else
6134 choices.AssignData((wxPGChoicesData*)it->second);
6135 }
6136 else
6137 {
6138 bool found = false;
6139 if ( idString.length() )
6140 {
6141 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
6142 if ( it != m_dictIdChoices.end() )
6143 {
6144 choices.AssignData((wxPGChoicesData*)it->second);
6145 found = true;
6146 }
6147 }
6148
6149 if ( !found )
6150 {
6151 // Parse choices string
6152 wxString::const_iterator it = choicesString.begin();
6153 wxString label;
6154 wxString value;
6155 int state = 0;
6156 bool labelValid = false;
6157
6158 for ( ; it != choicesString.end(); ++it )
6159 {
6160 wxChar c = *it;
6161
6162 if ( state != 1 )
6163 {
6164 if ( c == wxT('"') )
6165 {
6166 if ( labelValid )
6167 {
6168 long l;
6169 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6170 choices.Add(label, l);
6171 }
6172 labelValid = false;
6173 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
6174 value.clear();
6175 label.clear();
6176 state = 1;
6177 }
6178 else if ( c == wxT('=') )
6179 {
6180 if ( labelValid )
6181 {
6182 state = 2;
6183 }
6184 }
6185 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
6186 {
6187 value << c;
6188 }
6189 }
6190 else
6191 {
6192 if ( c == wxT('"') )
6193 {
6194 state = 0;
6195 labelValid = true;
6196 }
6197 else
6198 label << c;
6199 }
6200 }
6201
6202 if ( labelValid )
6203 {
6204 long l;
6205 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6206 choices.Add(label, l);
6207 }
6208
6209 if ( !choices.IsOk() )
6210 {
6211 choices.EnsureData();
6212 }
6213
6214 // Assign to id
6215 if ( idString.length() )
6216 m_dictIdChoices[idString] = choices.GetData();
6217 }
6218 }
6219
6220 return choices;
6221 }
6222
6223 // -----------------------------------------------------------------------
6224
6225 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
6226 {
6227 if ( s.Last() == wxT('%') )
6228 {
6229 wxString s2 = s.substr(0,s.length()-1);
6230 long val;
6231 if ( s2.ToLong(&val, 10) )
6232 {
6233 *pval = (val*max)/100;
6234 return true;
6235 }
6236 return false;
6237 }
6238
6239 return s.ToLong(pval, 10);
6240 }
6241
6242 // -----------------------------------------------------------------------
6243
6244 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
6245 const wxString& type,
6246 const wxString& value )
6247 {
6248 int l = m_propHierarchy.size();
6249 if ( !l )
6250 return false;
6251
6252 wxPGProperty* p = m_propHierarchy[l-1];
6253 wxString valuel = value.Lower();
6254 wxVariant variant;
6255
6256 if ( type.length() == 0 )
6257 {
6258 long v;
6259
6260 // Auto-detect type
6261 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6262 variant = true;
6263 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6264 variant = false;
6265 else if ( value.ToLong(&v, 0) )
6266 variant = v;
6267 else
6268 variant = value;
6269 }
6270 else
6271 {
6272 if ( type == wxT("string") )
6273 {
6274 variant = value;
6275 }
6276 else if ( type == wxT("int") )
6277 {
6278 long v = 0;
6279 value.ToLong(&v, 0);
6280 variant = v;
6281 }
6282 else if ( type == wxT("bool") )
6283 {
6284 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6285 variant = true;
6286 else
6287 variant = false;
6288 }
6289 else
6290 {
6291 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6292 return false;
6293 }
6294 }
6295
6296 p->SetAttribute( name, variant );
6297
6298 return true;
6299 }
6300
6301 // -----------------------------------------------------------------------
6302
6303 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6304 {
6305 wxLogError(_("Error in resource: %s"),msg.c_str());
6306 }
6307
6308 // -----------------------------------------------------------------------
6309
6310 #endif // wxUSE_PROPGRID