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