Removed intermediate wxPanel from wxPropertyGrid.
[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* clipRect )
1837 {
1838 if ( m_frozen || m_height < 1 || bottomy < topy || !m_pState )
1839 return;
1840
1841 m_pState->EnsureVirtualHeight();
1842
1843 wxRect tempClipRect;
1844 if ( !clipRect )
1845 {
1846 tempClipRect = wxRect(0,topy,m_pState->m_width,bottomy);
1847 clipRect = &tempClipRect;
1848 }
1849
1850 // items added check
1851 if ( m_pState->m_itemsAdded ) PrepareAfterItemsAdded();
1852
1853 int paintFinishY = 0;
1854
1855 if ( m_pState->m_properties->GetChildCount() > 0 )
1856 {
1857 wxDC* dcPtr = &dc;
1858 bool isBuffered = false;
1859
1860 #if wxPG_DOUBLE_BUFFER
1861 wxMemoryDC* bufferDC = NULL;
1862
1863 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
1864 {
1865 if ( !m_doubleBuffer )
1866 {
1867 paintFinishY = clipRect->y;
1868 dcPtr = NULL;
1869 }
1870 else
1871 {
1872 bufferDC = new wxMemoryDC();
1873
1874 // If nothing was changed, then just copy from double-buffer
1875 bufferDC->SelectObject( *m_doubleBuffer );
1876 dcPtr = bufferDC;
1877
1878 isBuffered = true;
1879 }
1880 }
1881 #endif
1882
1883 if ( dcPtr )
1884 {
1885 dc.SetClippingRegion( *clipRect );
1886 paintFinishY = DoDrawItems( *dcPtr, clipRect, isBuffered );
1887
1888 // Clear area beyond bottomY?
1889 if ( paintFinishY < (clipRect->y+clipRect->height) )
1890 {
1891 dcPtr->SetPen(m_colEmptySpace);
1892 dcPtr->SetBrush(m_colEmptySpace);
1893 dcPtr->DrawRectangle( 0, paintFinishY, m_width,
1894 (clipRect->y+clipRect->height) );
1895 }
1896 }
1897
1898 #if wxPG_DOUBLE_BUFFER
1899 if ( bufferDC )
1900 {
1901 dc.Blit( clipRect->x, clipRect->y, clipRect->width,
1902 clipRect->height,
1903 bufferDC, 0, 0, wxCOPY );
1904 dc.DestroyClippingRegion(); // Is this really necessary?
1905 delete bufferDC;
1906 }
1907 #endif
1908 }
1909 else
1910 {
1911 // Just clear the area
1912 dc.SetPen(m_colEmptySpace);
1913 dc.SetBrush(m_colEmptySpace);
1914 dc.DrawRectangle(*clipRect);
1915 }
1916 }
1917
1918 // -----------------------------------------------------------------------
1919
1920 int wxPropertyGrid::DoDrawItems( wxDC& dc,
1921 const wxRect* clipRect,
1922 bool isBuffered ) const
1923 {
1924 const wxPGProperty* firstItem;
1925 const wxPGProperty* lastItem;
1926
1927 firstItem = DoGetItemAtY(clipRect->y);
1928 lastItem = DoGetItemAtY(clipRect->y+clipRect->height-1);
1929
1930 if ( !lastItem )
1931 lastItem = GetLastItem( wxPG_ITERATE_VISIBLE );
1932
1933 if ( m_frozen || m_height < 1 || firstItem == NULL )
1934 return clipRect->y;
1935
1936 wxCHECK_MSG( !m_pState->m_itemsAdded, clipRect->y, wxT("no items added") );
1937 wxASSERT( m_pState->m_properties->GetChildCount() );
1938
1939 int lh = m_lineHeight;
1940
1941 int firstItemTopY;
1942 int lastItemBottomY;
1943
1944 firstItemTopY = clipRect->y;
1945 lastItemBottomY = clipRect->y + clipRect->height;
1946
1947 // Align y coordinates to item boundaries
1948 firstItemTopY -= firstItemTopY % lh;
1949 lastItemBottomY += lh - (lastItemBottomY % lh);
1950 lastItemBottomY -= 1;
1951
1952 // Entire range outside scrolled, visible area?
1953 if ( firstItemTopY >= (int)m_pState->GetVirtualHeight() || lastItemBottomY <= 0 )
1954 return clipRect->y;
1955
1956 wxCHECK_MSG( firstItemTopY < lastItemBottomY, clipRect->y, wxT("invalid y values") );
1957
1958
1959 /*
1960 wxLogDebug(wxT(" -> DoDrawItems ( \"%s\" -> \"%s\", height=%i (ch=%i), clipRect = 0x%lX )"),
1961 firstItem->GetLabel().c_str(),
1962 lastItem->GetLabel().c_str(),
1963 (int)(lastItemBottomY - firstItemTopY),
1964 (int)m_height,
1965 (unsigned long)clipRect );
1966 */
1967
1968 wxRect r;
1969
1970 long windowStyle = m_windowStyle;
1971
1972 int xRelMod = 0;
1973 int yRelMod = 0;
1974
1975 //
1976 // With wxPG_DOUBLE_BUFFER, do double buffering
1977 // - buffer's y = 0, so align cliprect and coordinates to that
1978 //
1979 #if wxPG_DOUBLE_BUFFER
1980
1981 wxRect cr2;
1982
1983 if ( isBuffered )
1984 {
1985 xRelMod = clipRect->x;
1986 yRelMod = clipRect->y;
1987
1988 //
1989 // clipRect conversion
1990 cr2 = *clipRect;
1991 cr2.x -= xRelMod;
1992 cr2.y -= yRelMod;
1993 clipRect = &cr2;
1994 firstItemTopY -= yRelMod;
1995 lastItemBottomY -= yRelMod;
1996 }
1997 #else
1998 wxUnusedVar(isBuffered);
1999 #endif
2000
2001 int x = m_marginWidth - xRelMod;
2002
2003 wxFont normalFont = GetFont();
2004
2005 bool reallyFocused = (m_iFlags & wxPG_FL_FOCUSED) != 0;
2006
2007 bool isPgEnabled = IsEnabled();
2008
2009 //
2010 // Prepare some pens and brushes that are often changed to.
2011 //
2012
2013 wxBrush marginBrush(m_colMargin);
2014 wxPen marginPen(m_colMargin);
2015 wxBrush capbgbrush(m_colCapBack,wxSOLID);
2016 wxPen linepen(m_colLine,1,wxSOLID);
2017
2018 wxColour selBackCol;
2019 if ( isPgEnabled )
2020 selBackCol = m_colSelBack;
2021 else
2022 selBackCol = m_colMargin;
2023
2024 // pen that has same colour as text
2025 wxPen outlinepen(m_colPropFore,1,wxSOLID);
2026
2027 //
2028 // Clear margin with background colour
2029 //
2030 dc.SetBrush( marginBrush );
2031 if ( !(windowStyle & wxPG_HIDE_MARGIN) )
2032 {
2033 dc.SetPen( *wxTRANSPARENT_PEN );
2034 dc.DrawRectangle(-1-xRelMod,firstItemTopY-1,x+2,lastItemBottomY-firstItemTopY+2);
2035 }
2036
2037 const wxPGProperty* firstSelected = GetSelection();
2038 const wxPropertyGridPageState* state = m_pState;
2039
2040 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2041 bool wasSelectedPainted = false;
2042 #endif
2043
2044 // TODO: Only render columns that are within clipping region.
2045
2046 dc.SetFont(normalFont);
2047
2048 wxPropertyGridConstIterator it( state, wxPG_ITERATE_VISIBLE, firstItem );
2049 int endScanBottomY = lastItemBottomY + lh;
2050 int y = firstItemTopY;
2051
2052 //
2053 // Pregenerate list of visible properties.
2054 wxArrayPGProperty visPropArray;
2055 visPropArray.reserve((m_height/m_lineHeight)+6);
2056
2057 for ( ; !it.AtEnd(); it.Next() )
2058 {
2059 const wxPGProperty* p = *it;
2060
2061 if ( !p->HasFlag(wxPG_PROP_HIDDEN) )
2062 {
2063 visPropArray.push_back((wxPGProperty*)p);
2064
2065 if ( y > endScanBottomY )
2066 break;
2067
2068 y += lh;
2069 }
2070 }
2071
2072 visPropArray.push_back(NULL);
2073
2074 wxPGProperty* nextP = visPropArray[0];
2075
2076 int gridWidth = state->m_width;
2077
2078 y = firstItemTopY;
2079 for ( unsigned int arrInd=1;
2080 nextP && y <= lastItemBottomY;
2081 arrInd++ )
2082 {
2083 wxPGProperty* p = nextP;
2084 nextP = visPropArray[arrInd];
2085
2086 int rowHeight = m_fontHeight+(m_spacingy*2)+1;
2087 int textMarginHere = x;
2088 int renderFlags = 0;
2089
2090 int greyDepth = m_marginWidth;
2091 if ( !(windowStyle & wxPG_HIDE_CATEGORIES) )
2092 greyDepth = (((int)p->m_depthBgCol)-1) * m_subgroup_extramargin + m_marginWidth;
2093
2094 int greyDepthX = greyDepth - xRelMod;
2095
2096 // Use basic depth if in non-categoric mode and parent is base array.
2097 if ( !(windowStyle & wxPG_HIDE_CATEGORIES) || p->GetParent() != m_pState->m_properties )
2098 {
2099 textMarginHere += ((unsigned int)((p->m_depth-1)*m_subgroup_extramargin));
2100 }
2101
2102 // Paint margin area
2103 dc.SetBrush(marginBrush);
2104 dc.SetPen(marginPen);
2105 dc.DrawRectangle( -xRelMod, y, greyDepth, lh );
2106
2107 dc.SetPen( linepen );
2108
2109 int y2 = y + lh;
2110
2111 #ifdef __WXMSW__
2112 // Margin Edge
2113 // Modified by JACS to not draw a margin if wxPG_HIDE_MARGIN is specified, since it
2114 // looks better, at least under Windows when we have a themed border (the themed-window-specific
2115 // whitespace between the real border and the propgrid margin exacerbates the double-border look).
2116
2117 // Is this or its parent themed?
2118 bool suppressMarginEdge = (GetWindowStyle() & wxPG_HIDE_MARGIN) &&
2119 (((GetWindowStyle() & wxBORDER_MASK) == wxBORDER_THEME) ||
2120 (((GetWindowStyle() & wxBORDER_MASK) == wxBORDER_NONE) && ((GetParent()->GetWindowStyle() & wxBORDER_MASK) == wxBORDER_THEME)));
2121 #else
2122 bool suppressMarginEdge = false;
2123 #endif
2124 if (!suppressMarginEdge)
2125 dc.DrawLine( greyDepthX, y, greyDepthX, y2 );
2126 else
2127 {
2128 // Blank out the margin edge
2129 dc.SetPen(wxPen(GetBackgroundColour()));
2130 dc.DrawLine( greyDepthX, y, greyDepthX, y2 );
2131 dc.SetPen( linepen );
2132 }
2133
2134 // Splitters
2135 unsigned int si;
2136 int sx = x;
2137
2138 for ( si=0; si<state->m_colWidths.size(); si++ )
2139 {
2140 sx += state->m_colWidths[si];
2141 dc.DrawLine( sx, y, sx, y2 );
2142 }
2143
2144 // Horizontal Line, below
2145 // (not if both this and next is category caption)
2146 if ( p->IsCategory() &&
2147 nextP && nextP->IsCategory() )
2148 dc.SetPen(m_colCapBack);
2149
2150 dc.DrawLine( greyDepthX, y2-1, gridWidth-xRelMod, y2-1 );
2151
2152 //
2153 // Need to override row colours?
2154 wxColour rowFgCol;
2155 wxColour rowBgCol;
2156
2157 bool isSelected = state->DoIsPropertySelected(p);
2158
2159 if ( !isSelected )
2160 {
2161 // Disabled may get different colour.
2162 if ( !p->IsEnabled() )
2163 {
2164 renderFlags |= wxPGCellRenderer::Disabled |
2165 wxPGCellRenderer::DontUseCellFgCol;
2166 rowFgCol = m_colDisPropFore;
2167 }
2168 }
2169 else
2170 {
2171 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2172 if ( p == firstSelected )
2173 wasSelectedPainted = true;
2174 #endif
2175
2176 renderFlags |= wxPGCellRenderer::Selected;
2177
2178 if ( !p->IsCategory() )
2179 {
2180 renderFlags |= wxPGCellRenderer::DontUseCellFgCol |
2181 wxPGCellRenderer::DontUseCellBgCol;
2182
2183 if ( reallyFocused && p == firstSelected )
2184 {
2185 rowFgCol = m_colSelFore;
2186 rowBgCol = selBackCol;
2187 }
2188 else if ( isPgEnabled )
2189 {
2190 rowFgCol = m_colPropFore;
2191 if ( p == firstSelected )
2192 rowBgCol = m_colMargin;
2193 else
2194 rowBgCol = selBackCol;
2195 }
2196 else
2197 {
2198 rowFgCol = m_colDisPropFore;
2199 rowBgCol = selBackCol;
2200 }
2201 }
2202 }
2203
2204 wxBrush rowBgBrush;
2205
2206 if ( rowBgCol.IsOk() )
2207 rowBgBrush = wxBrush(rowBgCol);
2208
2209 if ( HasInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL) )
2210 renderFlags = renderFlags & ~wxPGCellRenderer::DontUseCellColours;
2211
2212 //
2213 // Fill additional margin area with background colour of first cell
2214 if ( greyDepthX < textMarginHere )
2215 {
2216 if ( !(renderFlags & wxPGCellRenderer::DontUseCellBgCol) )
2217 {
2218 wxPGCell& cell = p->GetCell(0);
2219 rowBgCol = cell.GetBgCol();
2220 rowBgBrush = wxBrush(rowBgCol);
2221 }
2222 dc.SetBrush(rowBgBrush);
2223 dc.SetPen(rowBgCol);
2224 dc.DrawRectangle(greyDepthX+1, y,
2225 textMarginHere-greyDepthX, lh-1);
2226 }
2227
2228 bool fontChanged = false;
2229
2230 // Expander button rectangle
2231 wxRect butRect( ((p->m_depth - 1) * m_subgroup_extramargin) - xRelMod,
2232 y,
2233 m_marginWidth,
2234 lh );
2235
2236 if ( p->IsCategory() )
2237 {
2238 // Captions have their cell areas merged as one
2239 dc.SetFont(m_captionFont);
2240 fontChanged = true;
2241 wxRect cellRect(greyDepthX, y, gridWidth - greyDepth + 2, rowHeight-1 );
2242
2243 if ( renderFlags & wxPGCellRenderer::DontUseCellBgCol )
2244 {
2245 dc.SetBrush(rowBgBrush);
2246 dc.SetPen(rowBgCol);
2247 }
2248
2249 if ( renderFlags & wxPGCellRenderer::DontUseCellFgCol )
2250 {
2251 dc.SetTextForeground(rowFgCol);
2252 }
2253
2254 wxPGCellRenderer* renderer = p->GetCellRenderer(0);
2255 renderer->Render( dc, cellRect, this, p, 0, -1, renderFlags );
2256
2257 // Tree Item Button
2258 if ( !HasFlag(wxPG_HIDE_MARGIN) && p->HasVisibleChildren() )
2259 DrawExpanderButton( dc, butRect, p );
2260 }
2261 else
2262 {
2263 if ( p->m_flags & wxPG_PROP_MODIFIED && (windowStyle & wxPG_BOLD_MODIFIED) )
2264 {
2265 dc.SetFont(m_captionFont);
2266 fontChanged = true;
2267 }
2268
2269 unsigned int ci;
2270 int cellX = x + 1;
2271 int nextCellWidth = state->m_colWidths[0] -
2272 (greyDepthX - m_marginWidth);
2273 wxRect cellRect(greyDepthX+1, y, 0, rowHeight-1);
2274 int textXAdd = textMarginHere - greyDepthX;
2275
2276 for ( ci=0; ci<state->m_colWidths.size(); ci++ )
2277 {
2278 cellRect.width = nextCellWidth - 1;
2279
2280 wxWindow* cellEditor = NULL;
2281 int cellRenderFlags = renderFlags;
2282
2283 // Tree Item Button (must be drawn before clipping is set up)
2284 if ( ci == 0 && !HasFlag(wxPG_HIDE_MARGIN) && p->HasVisibleChildren() )
2285 DrawExpanderButton( dc, butRect, p );
2286
2287 // Background
2288 if ( isSelected && (ci == 1 || ci == m_selColumn) )
2289 {
2290 if ( p == firstSelected )
2291 {
2292 if ( ci == 1 && m_wndEditor )
2293 cellEditor = m_wndEditor;
2294 else if ( ci == m_selColumn && m_labelEditor )
2295 cellEditor = m_labelEditor;
2296 }
2297
2298 if ( cellEditor )
2299 {
2300 wxColour editorBgCol =
2301 cellEditor->GetBackgroundColour();
2302 dc.SetBrush(editorBgCol);
2303 dc.SetPen(editorBgCol);
2304 dc.SetTextForeground(m_colPropFore);
2305 dc.DrawRectangle(cellRect);
2306
2307 if ( m_dragStatus != 0 ||
2308 (m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE) )
2309 cellEditor = NULL;
2310 }
2311 else
2312 {
2313 dc.SetBrush(m_colPropBack);
2314 dc.SetPen(m_colPropBack);
2315 dc.SetTextForeground(m_colDisPropFore);
2316 if ( p->IsEnabled() )
2317 dc.SetTextForeground(rowFgCol);
2318 else
2319 dc.SetTextForeground(m_colDisPropFore);
2320 }
2321 }
2322 else
2323 {
2324 if ( renderFlags & wxPGCellRenderer::DontUseCellBgCol )
2325 {
2326 dc.SetBrush(rowBgBrush);
2327 dc.SetPen(rowBgCol);
2328 }
2329
2330 if ( renderFlags & wxPGCellRenderer::DontUseCellFgCol )
2331 {
2332 dc.SetTextForeground(rowFgCol);
2333 }
2334 }
2335
2336 dc.SetClippingRegion(cellRect);
2337
2338 cellRect.x += textXAdd;
2339 cellRect.width -= textXAdd;
2340
2341 // Foreground
2342 if ( !cellEditor )
2343 {
2344 wxPGCellRenderer* renderer;
2345 int cmnVal = p->GetCommonValue();
2346 if ( cmnVal == -1 || ci != 1 )
2347 {
2348 renderer = p->GetCellRenderer(ci);
2349 renderer->Render( dc, cellRect, this, p, ci, -1,
2350 cellRenderFlags );
2351 }
2352 else
2353 {
2354 renderer = GetCommonValue(cmnVal)->GetRenderer();
2355 renderer->Render( dc, cellRect, this, p, ci, -1,
2356 cellRenderFlags );
2357 }
2358 }
2359
2360 cellX += state->m_colWidths[ci];
2361 if ( ci < (state->m_colWidths.size()-1) )
2362 nextCellWidth = state->m_colWidths[ci+1];
2363 cellRect.x = cellX;
2364 dc.DestroyClippingRegion(); // Is this really necessary?
2365 textXAdd = 0;
2366 }
2367 }
2368
2369 if ( fontChanged )
2370 dc.SetFont(normalFont);
2371
2372 y += rowHeight;
2373 }
2374
2375 // Refresh editor controls (seems not needed on msw)
2376 // NOTE: This code is mandatory for GTK!
2377 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2378 if ( wasSelectedPainted )
2379 {
2380 if ( m_wndEditor )
2381 m_wndEditor->Refresh();
2382 if ( m_wndEditor2 )
2383 m_wndEditor2->Refresh();
2384 }
2385 #endif
2386
2387 return y + yRelMod;
2388 }
2389
2390 // -----------------------------------------------------------------------
2391
2392 wxRect wxPropertyGrid::GetPropertyRect( const wxPGProperty* p1, const wxPGProperty* p2 ) const
2393 {
2394 wxRect r;
2395
2396 if ( m_width < 10 || m_height < 10 ||
2397 !m_pState->m_properties->GetChildCount() ||
2398 p1 == NULL )
2399 return wxRect(0,0,0,0);
2400
2401 int vy = 0;
2402
2403 //
2404 // Return rect which encloses the given property range
2405
2406 int visTop = p1->GetY();
2407 int visBottom;
2408 if ( p2 )
2409 visBottom = p2->GetY() + m_lineHeight;
2410 else
2411 visBottom = m_height + visTop;
2412
2413 // If seleced property is inside the range, we'll extend the range to include
2414 // control's size.
2415 wxPGProperty* selected = GetSelection();
2416 if ( selected )
2417 {
2418 int selectedY = selected->GetY();
2419 if ( selectedY >= visTop && selectedY < visBottom )
2420 {
2421 wxWindow* editor = GetEditorControl();
2422 if ( editor )
2423 {
2424 int visBottom2 = selectedY + editor->GetSize().y;
2425 if ( visBottom2 > visBottom )
2426 visBottom = visBottom2;
2427 }
2428 }
2429 }
2430
2431 return wxRect(0,visTop-vy,m_pState->m_width,visBottom-visTop);
2432 }
2433
2434 // -----------------------------------------------------------------------
2435
2436 void wxPropertyGrid::DrawItems( const wxPGProperty* p1, const wxPGProperty* p2 )
2437 {
2438 if ( m_frozen )
2439 return;
2440
2441 if ( m_pState->m_itemsAdded )
2442 PrepareAfterItemsAdded();
2443
2444 wxRect r = GetPropertyRect(p1, p2);
2445 if ( r.width > 0 )
2446 {
2447 RefreshRect(r);
2448 }
2449 }
2450
2451 // -----------------------------------------------------------------------
2452
2453 void wxPropertyGrid::RefreshProperty( wxPGProperty* p )
2454 {
2455 if ( m_pState->DoIsPropertySelected(p) )
2456 {
2457 // NB: We must copy the selection.
2458 wxArrayPGProperty selection = m_pState->m_selection;
2459 DoSetSelection(selection, wxPG_SEL_FORCE);
2460 }
2461
2462 DrawItemAndChildren(p);
2463 }
2464
2465 // -----------------------------------------------------------------------
2466
2467 void wxPropertyGrid::DrawItemAndValueRelated( wxPGProperty* p )
2468 {
2469 if ( m_frozen )
2470 return;
2471
2472 // Draw item, children, and parent too, if it is not category
2473 wxPGProperty* parent = p->GetParent();
2474
2475 while ( parent &&
2476 !parent->IsCategory() &&
2477 parent->GetParent() )
2478 {
2479 DrawItem(parent);
2480 parent = parent->GetParent();
2481 }
2482
2483 DrawItemAndChildren(p);
2484 }
2485
2486 void wxPropertyGrid::DrawItemAndChildren( wxPGProperty* p )
2487 {
2488 wxCHECK_RET( p, wxT("invalid property id") );
2489
2490 // Do not draw if in non-visible page
2491 if ( p->GetParentState() != m_pState )
2492 return;
2493
2494 // do not draw a single item if multiple pending
2495 if ( m_pState->m_itemsAdded || m_frozen )
2496 return;
2497
2498 // Update child control.
2499 wxPGProperty* selected = GetSelection();
2500 if ( selected && selected->GetParent() == p )
2501 RefreshEditor();
2502
2503 const wxPGProperty* lastDrawn = p->GetLastVisibleSubItem();
2504
2505 DrawItems(p, lastDrawn);
2506 }
2507
2508 // -----------------------------------------------------------------------
2509
2510 void wxPropertyGrid::Refresh( bool WXUNUSED(eraseBackground),
2511 const wxRect *rect )
2512 {
2513 PrepareAfterItemsAdded();
2514
2515 wxWindow::Refresh(false, rect);
2516
2517 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
2518 // I think this really helps only GTK+1.2
2519 if ( m_wndEditor ) m_wndEditor->Refresh();
2520 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
2521 #endif
2522 }
2523
2524 // -----------------------------------------------------------------------
2525 // wxPropertyGrid global operations
2526 // -----------------------------------------------------------------------
2527
2528 void wxPropertyGrid::Clear()
2529 {
2530 m_pState->DoClear();
2531
2532 m_propHover = NULL;
2533
2534 m_prevVY = 0;
2535
2536 RecalculateVirtualSize();
2537
2538 // Need to clear some area at the end
2539 if ( !m_frozen )
2540 RefreshRect(wxRect(0, 0, m_width, m_height));
2541 }
2542
2543 // -----------------------------------------------------------------------
2544
2545 bool wxPropertyGrid::EnableCategories( bool enable )
2546 {
2547 DoClearSelection();
2548
2549 if ( enable )
2550 {
2551 //
2552 // Enable categories
2553 //
2554
2555 m_windowStyle &= ~(wxPG_HIDE_CATEGORIES);
2556 }
2557 else
2558 {
2559 //
2560 // Disable categories
2561 //
2562 m_windowStyle |= wxPG_HIDE_CATEGORIES;
2563 }
2564
2565 if ( !m_pState->EnableCategories(enable) )
2566 return false;
2567
2568 if ( !m_frozen )
2569 {
2570 if ( m_windowStyle & wxPG_AUTO_SORT )
2571 {
2572 m_pState->m_itemsAdded = 1; // force
2573 PrepareAfterItemsAdded();
2574 }
2575 }
2576 else
2577 m_pState->m_itemsAdded = 1;
2578
2579 // No need for RecalculateVirtualSize() here - it is already called in
2580 // wxPropertyGridPageState method above.
2581
2582 Refresh();
2583
2584 return true;
2585 }
2586
2587 // -----------------------------------------------------------------------
2588
2589 void wxPropertyGrid::SwitchState( wxPropertyGridPageState* pNewState )
2590 {
2591 wxASSERT( pNewState );
2592 wxASSERT( pNewState->GetGrid() );
2593
2594 if ( pNewState == m_pState )
2595 return;
2596
2597 wxArrayPGProperty oldSelection = m_pState->m_selection;
2598
2599 // Call ClearSelection() instead of DoClearSelection()
2600 // so that selection clear events are not sent.
2601 ClearSelection();
2602
2603 m_pState->m_selection = oldSelection;
2604
2605 bool orig_mode = m_pState->IsInNonCatMode();
2606 bool new_state_mode = pNewState->IsInNonCatMode();
2607
2608 m_pState = pNewState;
2609
2610 // Validate width
2611 int pgWidth = GetClientSize().x;
2612 if ( HasVirtualWidth() )
2613 {
2614 int minWidth = pgWidth;
2615 if ( pNewState->m_width < minWidth )
2616 {
2617 pNewState->m_width = minWidth;
2618 pNewState->CheckColumnWidths();
2619 }
2620 }
2621 else
2622 {
2623 //
2624 // Just in case, fully re-center splitter
2625 if ( HasFlag( wxPG_SPLITTER_AUTO_CENTER ) )
2626 pNewState->m_fSplitterX = -1.0;
2627
2628 pNewState->OnClientWidthChange( pgWidth, pgWidth - pNewState->m_width );
2629 }
2630
2631 m_propHover = NULL;
2632
2633 // If necessary, convert state to correct mode.
2634 if ( orig_mode != new_state_mode )
2635 {
2636 // This should refresh as well.
2637 EnableCategories( orig_mode?false:true );
2638 }
2639 else if ( !m_frozen )
2640 {
2641 // Refresh, if not frozen.
2642 m_pState->PrepareAfterItemsAdded();
2643
2644 // Reselect (Use SetSelection() instead of Do-variant so that
2645 // events won't be sent).
2646 SetSelection(m_pState->m_selection);
2647
2648 RecalculateVirtualSize(0);
2649 Refresh();
2650 }
2651 else
2652 m_pState->m_itemsAdded = 1;
2653 }
2654
2655 // -----------------------------------------------------------------------
2656
2657 // Call to SetSplitterPosition will always disable splitter auto-centering
2658 // if parent window is shown.
2659 void wxPropertyGrid::DoSetSplitterPosition_( int newxpos, bool refresh, int splitterIndex, bool allPages )
2660 {
2661 if ( ( newxpos < wxPG_DRAG_MARGIN ) )
2662 return;
2663
2664 wxPropertyGridPageState* state = m_pState;
2665
2666 state->DoSetSplitterPosition( newxpos, splitterIndex, allPages );
2667
2668 if ( refresh )
2669 {
2670 if ( GetSelection() )
2671 CorrectEditorWidgetSizeX();
2672
2673 Refresh();
2674 }
2675 }
2676
2677 // -----------------------------------------------------------------------
2678
2679 void wxPropertyGrid::CenterSplitter( bool enableAutoCentering )
2680 {
2681 SetSplitterPosition( m_width/2, true );
2682 if ( enableAutoCentering && ( m_windowStyle & wxPG_SPLITTER_AUTO_CENTER ) )
2683 m_iFlags &= ~(wxPG_FL_DONT_CENTER_SPLITTER);
2684 }
2685
2686 // -----------------------------------------------------------------------
2687 // wxPropertyGrid item iteration (GetNextProperty etc.) methods
2688 // -----------------------------------------------------------------------
2689
2690 // Returns nearest paint visible property (such that will be painted unless
2691 // window is scrolled or resized). If given property is paint visible, then
2692 // it itself will be returned
2693 wxPGProperty* wxPropertyGrid::GetNearestPaintVisible( wxPGProperty* p ) const
2694 {
2695 int vx,vy1;// Top left corner of client
2696 GetViewStart(&vx,&vy1);
2697 vy1 *= wxPG_PIXELS_PER_UNIT;
2698
2699 int vy2 = vy1 + m_height;
2700 int propY = p->GetY2(m_lineHeight);
2701
2702 if ( (propY + m_lineHeight) < vy1 )
2703 {
2704 // Too high
2705 return DoGetItemAtY( vy1 );
2706 }
2707 else if ( propY > vy2 )
2708 {
2709 // Too low
2710 return DoGetItemAtY( vy2 );
2711 }
2712
2713 // Itself paint visible
2714 return p;
2715
2716 }
2717
2718 // -----------------------------------------------------------------------
2719 // Methods related to change in value, value modification and sending events
2720 // -----------------------------------------------------------------------
2721
2722 // commits any changes in editor of selected property
2723 // return true if validation did not fail
2724 // flags are same as with DoSelectProperty
2725 bool wxPropertyGrid::CommitChangesFromEditor( wxUint32 flags )
2726 {
2727 // Committing already?
2728 if ( m_inCommitChangesFromEditor )
2729 return true;
2730
2731 // Don't do this if already processing editor event. It might
2732 // induce recursive dialogs and crap like that.
2733 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
2734 {
2735 if ( m_inDoPropertyChanged )
2736 return true;
2737
2738 return false;
2739 }
2740
2741 wxPGProperty* selected = GetSelection();
2742
2743 if ( m_wndEditor &&
2744 IsEditorsValueModified() &&
2745 (m_iFlags & wxPG_FL_INITIALIZED) &&
2746 selected )
2747 {
2748 m_inCommitChangesFromEditor = 1;
2749
2750 wxVariant variant(selected->GetValueRef());
2751 bool valueIsPending = false;
2752
2753 // JACS - necessary to avoid new focus being found spuriously within OnIdle
2754 // due to another window getting focus
2755 wxWindow* oldFocus = m_curFocused;
2756
2757 bool validationFailure = false;
2758 bool forceSuccess = (flags & (wxPG_SEL_NOVALIDATE|wxPG_SEL_FORCE)) ? true : false;
2759
2760 m_chgInfo_changedProperty = NULL;
2761
2762 // If truly modified, schedule value as pending.
2763 if ( selected->GetEditorClass()->
2764 GetValueFromControl( variant,
2765 selected,
2766 GetEditorControl() ) )
2767 {
2768 if ( DoEditorValidate() &&
2769 PerformValidation(selected, variant) )
2770 {
2771 valueIsPending = true;
2772 }
2773 else
2774 {
2775 validationFailure = true;
2776 }
2777 }
2778 else
2779 {
2780 EditorsValueWasNotModified();
2781 }
2782
2783 bool res = true;
2784
2785 m_inCommitChangesFromEditor = 0;
2786
2787 if ( validationFailure && !forceSuccess )
2788 {
2789 if (oldFocus)
2790 {
2791 oldFocus->SetFocus();
2792 m_curFocused = oldFocus;
2793 }
2794
2795 res = OnValidationFailure(selected, variant);
2796
2797 // Now prevent further validation failure messages
2798 if ( res )
2799 {
2800 EditorsValueWasNotModified();
2801 OnValidationFailureReset(selected);
2802 }
2803 }
2804 else if ( valueIsPending )
2805 {
2806 DoPropertyChanged( selected, flags );
2807 EditorsValueWasNotModified();
2808 }
2809
2810 return res;
2811 }
2812
2813 return true;
2814 }
2815
2816 // -----------------------------------------------------------------------
2817
2818 bool wxPropertyGrid::PerformValidation( wxPGProperty* p, wxVariant& pendingValue,
2819 int flags )
2820 {
2821 //
2822 // Runs all validation functionality.
2823 // Returns true if value passes all tests.
2824 //
2825
2826 m_validationInfo.m_failureBehavior = m_permanentValidationFailureBehavior;
2827
2828 //
2829 // Variant list a special value that cannot be validated
2830 // by normal means.
2831 if ( pendingValue.GetType() != wxPG_VARIANT_TYPE_LIST )
2832 {
2833 if ( !p->ValidateValue(pendingValue, m_validationInfo) )
2834 return false;
2835 }
2836
2837 //
2838 // Adapt list to child values, if necessary
2839 wxVariant listValue = pendingValue;
2840 wxVariant* pPendingValue = &pendingValue;
2841 wxVariant* pList = NULL;
2842
2843 // If parent has wxPG_PROP_AGGREGATE flag, or uses composite
2844 // string value, then we need treat as it was changed instead
2845 // (or, in addition, as is the case with composite string parent).
2846 // This includes creating list variant for child values.
2847
2848 wxPGProperty* pwc = p->GetParent();
2849 wxPGProperty* changedProperty = p;
2850 wxPGProperty* baseChangedProperty = changedProperty;
2851 wxVariant bcpPendingList;
2852
2853 listValue = pendingValue;
2854 listValue.SetName(p->GetBaseName());
2855
2856 while ( pwc &&
2857 (pwc->HasFlag(wxPG_PROP_AGGREGATE) || pwc->HasFlag(wxPG_PROP_COMPOSED_VALUE)) )
2858 {
2859 wxVariantList tempList;
2860 wxVariant lv(tempList, pwc->GetBaseName());
2861 lv.Append(listValue);
2862 listValue = lv;
2863 pPendingValue = &listValue;
2864
2865 if ( pwc->HasFlag(wxPG_PROP_AGGREGATE) )
2866 {
2867 baseChangedProperty = pwc;
2868 bcpPendingList = lv;
2869 }
2870
2871 changedProperty = pwc;
2872 pwc = pwc->GetParent();
2873 }
2874
2875 wxVariant value;
2876 wxPGProperty* evtChangingProperty = changedProperty;
2877
2878 if ( pPendingValue->GetType() != wxPG_VARIANT_TYPE_LIST )
2879 {
2880 value = *pPendingValue;
2881 }
2882 else
2883 {
2884 // Convert list to child values
2885 pList = pPendingValue;
2886 changedProperty->AdaptListToValue( *pPendingValue, &value );
2887 }
2888
2889 wxVariant evtChangingValue = value;
2890
2891 if ( flags & SendEvtChanging )
2892 {
2893 // FIXME: After proper ValueToString()s added, remove
2894 // this. It is just a temporary fix, as evt_changing
2895 // will simply not work for wxPG_PROP_COMPOSED_VALUE
2896 // (unless it is selected, and textctrl editor is open).
2897 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
2898 {
2899 evtChangingProperty = baseChangedProperty;
2900 if ( evtChangingProperty != p )
2901 {
2902 evtChangingProperty->AdaptListToValue( bcpPendingList, &evtChangingValue );
2903 }
2904 else
2905 {
2906 evtChangingValue = pendingValue;
2907 }
2908 }
2909
2910 if ( evtChangingProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
2911 {
2912 if ( changedProperty == GetSelection() )
2913 {
2914 wxWindow* editor = GetEditorControl();
2915 wxASSERT( editor->IsKindOf(CLASSINFO(wxTextCtrl)) );
2916 evtChangingValue = wxStaticCast(editor, wxTextCtrl)->GetValue();
2917 }
2918 else
2919 {
2920 wxLogDebug(wxT("WARNING: wxEVT_PG_CHANGING is about to happen with old value."));
2921 }
2922 }
2923 }
2924
2925 wxASSERT( m_chgInfo_changedProperty == NULL );
2926 m_chgInfo_changedProperty = changedProperty;
2927 m_chgInfo_baseChangedProperty = baseChangedProperty;
2928 m_chgInfo_pendingValue = value;
2929
2930 if ( pList )
2931 m_chgInfo_valueList = *pList;
2932 else
2933 m_chgInfo_valueList.MakeNull();
2934
2935 // If changedProperty is not property which value was edited,
2936 // then call wxPGProperty::ValidateValue() for that as well.
2937 if ( p != changedProperty && value.GetType() != wxPG_VARIANT_TYPE_LIST )
2938 {
2939 if ( !changedProperty->ValidateValue(value, m_validationInfo) )
2940 return false;
2941 }
2942
2943 if ( flags & SendEvtChanging )
2944 {
2945 // SendEvent returns true if event was vetoed
2946 if ( SendEvent( wxEVT_PG_CHANGING, evtChangingProperty,
2947 &evtChangingValue ) )
2948 return false;
2949 }
2950
2951 if ( flags & IsStandaloneValidation )
2952 {
2953 // If called in 'generic' context, we need to reset
2954 // m_chgInfo_changedProperty and write back translated value.
2955 m_chgInfo_changedProperty = NULL;
2956 pendingValue = value;
2957 }
2958
2959 return true;
2960 }
2961
2962 // -----------------------------------------------------------------------
2963
2964 void wxPropertyGrid::DoShowPropertyError( wxPGProperty* WXUNUSED(property), const wxString& msg )
2965 {
2966 if ( !msg.length() )
2967 return;
2968
2969 #if wxUSE_STATUSBAR
2970 if ( !wxPGGlobalVars->m_offline )
2971 {
2972 wxWindow* topWnd = ::wxGetTopLevelParent(this);
2973 if ( topWnd )
2974 {
2975 wxFrame* pFrame = wxDynamicCast(topWnd, wxFrame);
2976 if ( pFrame )
2977 {
2978 wxStatusBar* pStatusBar = pFrame->GetStatusBar();
2979 if ( pStatusBar )
2980 {
2981 pStatusBar->SetStatusText(msg);
2982 return;
2983 }
2984 }
2985 }
2986 }
2987 #endif
2988
2989 ::wxMessageBox(msg, wxT("Property Error"));
2990 }
2991
2992 // -----------------------------------------------------------------------
2993
2994 bool wxPropertyGrid::OnValidationFailure( wxPGProperty* property,
2995 wxVariant& invalidValue )
2996 {
2997 wxWindow* editor = GetEditorControl();
2998
2999 // First call property's handler
3000 property->OnValidationFailure(invalidValue);
3001
3002 bool res = DoOnValidationFailure(property, invalidValue);
3003
3004 //
3005 // For non-wxTextCtrl editors, we do need to revert the value
3006 if ( !editor->IsKindOf(CLASSINFO(wxTextCtrl)) &&
3007 property == GetSelection() )
3008 {
3009 property->GetEditorClass()->UpdateControl(property, editor);
3010 }
3011
3012 property->SetFlag(wxPG_PROP_INVALID_VALUE);
3013
3014 return res;
3015 }
3016
3017 bool wxPropertyGrid::DoOnValidationFailure( wxPGProperty* property, wxVariant& WXUNUSED(invalidValue) )
3018 {
3019 int vfb = m_validationInfo.m_failureBehavior;
3020
3021 if ( vfb & wxPG_VFB_BEEP )
3022 ::wxBell();
3023
3024 if ( (vfb & wxPG_VFB_MARK_CELL) &&
3025 !property->HasFlag(wxPG_PROP_INVALID_VALUE) )
3026 {
3027 unsigned int colCount = m_pState->GetColumnCount();
3028
3029 // We need backup marked property's cells
3030 m_propCellsBackup = property->m_cells;
3031
3032 wxColour vfbFg = *wxWHITE;
3033 wxColour vfbBg = *wxRED;
3034
3035 property->EnsureCells(colCount);
3036
3037 for ( unsigned int i=0; i<colCount; i++ )
3038 {
3039 wxPGCell& cell = property->m_cells[i];
3040 cell.SetFgCol(vfbFg);
3041 cell.SetBgCol(vfbBg);
3042 }
3043
3044 DrawItemAndChildren(property);
3045
3046 if ( property == GetSelection() )
3047 {
3048 SetInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3049
3050 wxWindow* editor = GetEditorControl();
3051 if ( editor )
3052 {
3053 editor->SetForegroundColour(vfbFg);
3054 editor->SetBackgroundColour(vfbBg);
3055 }
3056 }
3057 }
3058
3059 if ( vfb & wxPG_VFB_SHOW_MESSAGE )
3060 {
3061 wxString msg = m_validationInfo.m_failureMessage;
3062
3063 if ( !msg.length() )
3064 msg = wxT("You have entered invalid value. Press ESC to cancel editing.");
3065
3066 DoShowPropertyError(property, msg);
3067 }
3068
3069 return (vfb & wxPG_VFB_STAY_IN_PROPERTY) ? false : true;
3070 }
3071
3072 // -----------------------------------------------------------------------
3073
3074 void wxPropertyGrid::DoOnValidationFailureReset( wxPGProperty* property )
3075 {
3076 int vfb = m_validationInfo.m_failureBehavior;
3077
3078 if ( vfb & wxPG_VFB_MARK_CELL )
3079 {
3080 // Revert cells
3081 property->m_cells = m_propCellsBackup;
3082
3083 ClearInternalFlag(wxPG_FL_CELL_OVERRIDES_SEL);
3084
3085 if ( property == GetSelection() && GetEditorControl() )
3086 {
3087 // Calling this will recreate the control, thus resetting its colour
3088 RefreshProperty(property);
3089 }
3090 else
3091 {
3092 DrawItemAndChildren(property);
3093 }
3094 }
3095 }
3096
3097 // -----------------------------------------------------------------------
3098
3099 // flags are same as with DoSelectProperty
3100 bool wxPropertyGrid::DoPropertyChanged( wxPGProperty* p, unsigned int selFlags )
3101 {
3102 if ( m_inDoPropertyChanged )
3103 return true;
3104
3105 wxWindow* editor = GetEditorControl();
3106 wxPGProperty* selected = GetSelection();
3107
3108 m_pState->m_anyModified = 1;
3109
3110 m_inDoPropertyChanged = 1;
3111
3112 // Maybe need to update control
3113 wxASSERT( m_chgInfo_changedProperty != NULL );
3114
3115 // These values were calculated in PerformValidation()
3116 wxPGProperty* changedProperty = m_chgInfo_changedProperty;
3117 wxVariant value = m_chgInfo_pendingValue;
3118
3119 wxPGProperty* topPaintedProperty = changedProperty;
3120
3121 while ( !topPaintedProperty->IsCategory() &&
3122 !topPaintedProperty->IsRoot() )
3123 {
3124 topPaintedProperty = topPaintedProperty->GetParent();
3125 }
3126
3127 changedProperty->SetValue(value, &m_chgInfo_valueList, wxPG_SETVAL_BY_USER);
3128
3129 // Set as Modified (not if dragging just began)
3130 if ( !(p->m_flags & wxPG_PROP_MODIFIED) )
3131 {
3132 p->m_flags |= wxPG_PROP_MODIFIED;
3133 if ( p == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3134 {
3135 if ( editor )
3136 SetCurControlBoldFont();
3137 }
3138 }
3139
3140 wxPGProperty* pwc;
3141
3142 // Propagate updates to parent(s)
3143 pwc = p;
3144 wxPGProperty* prevPwc = NULL;
3145
3146 while ( prevPwc != topPaintedProperty )
3147 {
3148 pwc->m_flags |= wxPG_PROP_MODIFIED;
3149
3150 if ( pwc == selected && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3151 {
3152 if ( editor )
3153 SetCurControlBoldFont();
3154 }
3155
3156 prevPwc = pwc;
3157 pwc = pwc->GetParent();
3158 }
3159
3160 // Draw the actual property
3161 DrawItemAndChildren( topPaintedProperty );
3162
3163 //
3164 // If value was set by wxPGProperty::OnEvent, then update the editor
3165 // control.
3166 if ( selFlags & wxPG_SEL_DIALOGVAL )
3167 {
3168 RefreshEditor();
3169 }
3170 else
3171 {
3172 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
3173 if ( m_wndEditor ) m_wndEditor->Refresh();
3174 if ( m_wndEditor2 ) m_wndEditor2->Refresh();
3175 #endif
3176 }
3177
3178 // Sanity check
3179 wxASSERT( !changedProperty->GetParent()->HasFlag(wxPG_PROP_AGGREGATE) );
3180
3181 // If top parent has composite string value, then send to child parents,
3182 // starting from baseChangedProperty.
3183 if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) )
3184 {
3185 pwc = m_chgInfo_baseChangedProperty;
3186
3187 while ( pwc != changedProperty )
3188 {
3189 SendEvent( wxEVT_PG_CHANGED, pwc, NULL );
3190 pwc = pwc->GetParent();
3191 }
3192 }
3193
3194 SendEvent( wxEVT_PG_CHANGED, changedProperty, NULL );
3195
3196 m_inDoPropertyChanged = 0;
3197
3198 return true;
3199 }
3200
3201 // -----------------------------------------------------------------------
3202
3203 bool wxPropertyGrid::ChangePropertyValue( wxPGPropArg id, wxVariant newValue )
3204 {
3205 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
3206
3207 m_chgInfo_changedProperty = NULL;
3208
3209 if ( PerformValidation(p, newValue) )
3210 {
3211 DoPropertyChanged(p);
3212 return true;
3213 }
3214 else
3215 {
3216 OnValidationFailure(p, newValue);
3217 }
3218
3219 return false;
3220 }
3221
3222 // -----------------------------------------------------------------------
3223
3224 wxVariant wxPropertyGrid::GetUncommittedPropertyValue()
3225 {
3226 wxPGProperty* prop = GetSelectedProperty();
3227
3228 if ( !prop )
3229 return wxNullVariant;
3230
3231 wxTextCtrl* tc = GetEditorTextCtrl();
3232 wxVariant value = prop->GetValue();
3233
3234 if ( !tc || !IsEditorsValueModified() )
3235 return value;
3236
3237 if ( !prop->StringToValue(value, tc->GetValue()) )
3238 return value;
3239
3240 if ( !PerformValidation(prop, value, IsStandaloneValidation) )
3241 return prop->GetValue();
3242
3243 return value;
3244 }
3245
3246 // -----------------------------------------------------------------------
3247
3248 // Runs wxValidator for the selected property
3249 bool wxPropertyGrid::DoEditorValidate()
3250 {
3251 return true;
3252 }
3253
3254 // -----------------------------------------------------------------------
3255
3256 void wxPropertyGrid::HandleCustomEditorEvent( wxEvent &event )
3257 {
3258 wxPGProperty* selected = GetSelection();
3259
3260 // Somehow, event is handled after property has been deselected.
3261 // Possibly, but very rare.
3262 if ( !selected ||
3263 selected->HasFlag(wxPG_PROP_BEING_DELETED) ||
3264 // Also don't handle editor event if wxEVT_PG_CHANGED or
3265 // similar is currently doing something (showing a
3266 // message box, for instance).
3267 m_processedEvent )
3268 return;
3269
3270 if ( m_iFlags & wxPG_FL_IN_HANDLECUSTOMEDITOREVENT )
3271 return;
3272
3273 wxVariant pendingValue(selected->GetValueRef());
3274 wxWindow* wnd = GetEditorControl();
3275 wxWindow* editorWnd = wxDynamicCast(event.GetEventObject(), wxWindow);
3276 int selFlags = 0;
3277 bool wasUnspecified = selected->IsValueUnspecified();
3278 int usesAutoUnspecified = selected->UsesAutoUnspecified();
3279 bool valueIsPending = false;
3280
3281 m_chgInfo_changedProperty = NULL;
3282
3283 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED|wxPG_FL_VALUE_CHANGE_IN_EVENT);
3284
3285 //
3286 // Filter out excess wxTextCtrl modified events
3287 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED &&
3288 wnd &&
3289 wnd->IsKindOf(CLASSINFO(wxTextCtrl)) )
3290 {
3291 wxTextCtrl* tc = (wxTextCtrl*) wnd;
3292
3293 wxString newTcValue = tc->GetValue();
3294 if ( m_prevTcValue == newTcValue )
3295 return;
3296
3297 m_prevTcValue = newTcValue;
3298 }
3299
3300 SetInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3301
3302 bool validationFailure = false;
3303 bool buttonWasHandled = false;
3304
3305 //
3306 // Try common button handling
3307 if ( m_wndEditor2 && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3308 {
3309 wxPGEditorDialogAdapter* adapter = selected->GetEditorDialog();
3310
3311 if ( adapter )
3312 {
3313 buttonWasHandled = true;
3314 // Store as res2, as previously (and still currently alternatively)
3315 // dialogs can be shown by handling wxEVT_COMMAND_BUTTON_CLICKED
3316 // in wxPGProperty::OnEvent().
3317 adapter->ShowDialog( this, selected );
3318 delete adapter;
3319 }
3320 }
3321
3322 if ( !buttonWasHandled )
3323 {
3324 if ( wnd || m_wndEditor2 )
3325 {
3326 // First call editor class' event handler.
3327 const wxPGEditor* editor = selected->GetEditorClass();
3328
3329 if ( editor->OnEvent( this, selected, editorWnd, event ) )
3330 {
3331 // If changes, validate them
3332 if ( DoEditorValidate() )
3333 {
3334 if ( editor->GetValueFromControl( pendingValue,
3335 selected,
3336 wnd ) )
3337 valueIsPending = true;
3338 }
3339 else
3340 {
3341 validationFailure = true;
3342 }
3343 }
3344 }
3345
3346 // Then the property's custom handler (must be always called, unless
3347 // validation failed).
3348 if ( !validationFailure )
3349 buttonWasHandled = selected->OnEvent( this, editorWnd, event );
3350 }
3351
3352 // SetValueInEvent(), as called in one of the functions referred above
3353 // overrides editor's value.
3354 if ( m_iFlags & wxPG_FL_VALUE_CHANGE_IN_EVENT )
3355 {
3356 valueIsPending = true;
3357 pendingValue = m_changeInEventValue;
3358 selFlags |= wxPG_SEL_DIALOGVAL;
3359 }
3360
3361 if ( !validationFailure && valueIsPending )
3362 if ( !PerformValidation(selected, pendingValue) )
3363 validationFailure = true;
3364
3365 if ( validationFailure)
3366 {
3367 OnValidationFailure(selected, pendingValue);
3368 }
3369 else if ( valueIsPending )
3370 {
3371 selFlags |= ( !wasUnspecified && selected->IsValueUnspecified() && usesAutoUnspecified ) ? wxPG_SEL_SETUNSPEC : 0;
3372
3373 DoPropertyChanged(selected, selFlags);
3374 EditorsValueWasNotModified();
3375
3376 // Regardless of editor type, unfocus editor on
3377 // text-editing related enter press.
3378 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3379 {
3380 SetFocusOnCanvas();
3381 }
3382 }
3383 else
3384 {
3385 // No value after all
3386
3387 // Regardless of editor type, unfocus editor on
3388 // text-editing related enter press.
3389 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
3390 {
3391 SetFocusOnCanvas();
3392 }
3393
3394 // Let unhandled button click events go to the parent
3395 if ( !buttonWasHandled && event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
3396 {
3397 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED,GetId());
3398 GetEventHandler()->AddPendingEvent(evt);
3399 }
3400 }
3401
3402 ClearInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT);
3403 }
3404
3405 // -----------------------------------------------------------------------
3406 // wxPropertyGrid editor control helper methods
3407 // -----------------------------------------------------------------------
3408
3409 wxRect wxPropertyGrid::GetEditorWidgetRect( wxPGProperty* p, int column ) const
3410 {
3411 int itemy = p->GetY2(m_lineHeight);
3412 int splitterX = m_pState->DoGetSplitterPosition(column-1);
3413 int colEnd = splitterX + m_pState->m_colWidths[column];
3414 int imageOffset = 0;
3415
3416 int vx, vy; // Top left corner of client
3417 GetViewStart(&vx, &vy);
3418 vy *= wxPG_PIXELS_PER_UNIT;
3419
3420 // TODO: If custom image detection changes from current, change this.
3421 if ( m_iFlags & wxPG_FL_CUR_USES_CUSTOM_IMAGE )
3422 {
3423 //m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3424 int iw = p->OnMeasureImage().x;
3425 if ( iw < 1 )
3426 iw = wxPG_CUSTOM_IMAGE_WIDTH;
3427 imageOffset = p->GetImageOffset(iw);
3428 }
3429
3430 return wxRect
3431 (
3432 splitterX+imageOffset+wxPG_XBEFOREWIDGET+wxPG_CONTROL_MARGIN+1,
3433 itemy-vy,
3434 colEnd-splitterX-wxPG_XBEFOREWIDGET-wxPG_CONTROL_MARGIN-imageOffset-1,
3435 m_lineHeight-1
3436 );
3437 }
3438
3439 // -----------------------------------------------------------------------
3440
3441 wxRect wxPropertyGrid::GetImageRect( wxPGProperty* p, int item ) const
3442 {
3443 wxSize sz = GetImageSize(p, item);
3444 return wxRect(wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1,
3445 wxPG_CUSTOM_IMAGE_SPACINGY,
3446 sz.x,
3447 sz.y);
3448 }
3449
3450 // return size of custom paint image
3451 wxSize wxPropertyGrid::GetImageSize( wxPGProperty* p, int item ) const
3452 {
3453 // If called with NULL property, then return default image
3454 // size for properties that use image.
3455 if ( !p )
3456 return wxSize(wxPG_CUSTOM_IMAGE_WIDTH,wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight));
3457
3458 wxSize cis = p->OnMeasureImage(item);
3459
3460 int choiceCount = p->m_choices.GetCount();
3461 int comVals = p->GetDisplayedCommonValueCount();
3462 if ( item >= choiceCount && comVals > 0 )
3463 {
3464 unsigned int cvi = item-choiceCount;
3465 cis = GetCommonValue(cvi)->GetRenderer()->GetImageSize(NULL, 1, cvi);
3466 }
3467 else if ( item >= 0 && choiceCount == 0 )
3468 return wxSize(0, 0);
3469
3470 if ( cis.x < 0 )
3471 {
3472 if ( cis.x <= -1 )
3473 cis.x = wxPG_CUSTOM_IMAGE_WIDTH;
3474 }
3475 if ( cis.y <= 0 )
3476 {
3477 if ( cis.y >= -1 )
3478 cis.y = wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight);
3479 else
3480 cis.y = -cis.y;
3481 }
3482 return cis;
3483 }
3484
3485 // -----------------------------------------------------------------------
3486
3487 // takes scrolling into account
3488 void wxPropertyGrid::ImprovedClientToScreen( int* px, int* py )
3489 {
3490 int vx, vy;
3491 GetViewStart(&vx,&vy);
3492 vy*=wxPG_PIXELS_PER_UNIT;
3493 vx*=wxPG_PIXELS_PER_UNIT;
3494 *px -= vx;
3495 *py -= vy;
3496 ClientToScreen( px, py );
3497 }
3498
3499 // -----------------------------------------------------------------------
3500
3501 wxPropertyGridHitTestResult wxPropertyGrid::HitTest( const wxPoint& pt ) const
3502 {
3503 wxPoint pt2;
3504 GetViewStart(&pt2.x,&pt2.y);
3505 pt2.x *= wxPG_PIXELS_PER_UNIT;
3506 pt2.y *= wxPG_PIXELS_PER_UNIT;
3507 pt2.x += pt.x;
3508 pt2.y += pt.y;
3509
3510 return m_pState->HitTest(pt2);
3511 }
3512
3513 // -----------------------------------------------------------------------
3514
3515 // custom set cursor
3516 void wxPropertyGrid::CustomSetCursor( int type, bool override )
3517 {
3518 if ( type == m_curcursor && !override ) return;
3519
3520 wxCursor* cursor = &wxPG_DEFAULT_CURSOR;
3521
3522 if ( type == wxCURSOR_SIZEWE )
3523 cursor = m_cursorSizeWE;
3524
3525 SetCursor( *cursor );
3526
3527 m_curcursor = type;
3528 }
3529
3530 // -----------------------------------------------------------------------
3531 // wxPropertyGrid property selection, editor creation
3532 // -----------------------------------------------------------------------
3533
3534 //
3535 // This class forwards events from property editor controls to wxPropertyGrid.
3536 class wxPropertyGridEditorEventForwarder : public wxEvtHandler
3537 {
3538 public:
3539 wxPropertyGridEditorEventForwarder( wxPropertyGrid* propGrid )
3540 : wxEvtHandler(), m_propGrid(propGrid)
3541 {
3542 }
3543
3544 virtual ~wxPropertyGridEditorEventForwarder()
3545 {
3546 }
3547
3548 private:
3549 bool ProcessEvent( wxEvent& event )
3550 {
3551 // Always skip
3552 event.Skip();
3553
3554 m_propGrid->HandleCustomEditorEvent(event);
3555
3556 return wxEvtHandler::ProcessEvent(event);
3557 }
3558
3559 wxPropertyGrid* m_propGrid;
3560 };
3561
3562 // Setups event handling for child control
3563 void wxPropertyGrid::SetupChildEventHandling( wxWindow* argWnd )
3564 {
3565 wxWindowID id = argWnd->GetId();
3566
3567 if ( argWnd == m_wndEditor )
3568 {
3569 argWnd->Connect(id, wxEVT_MOTION,
3570 wxMouseEventHandler(wxPropertyGrid::OnMouseMoveChild),
3571 NULL, this);
3572 argWnd->Connect(id, wxEVT_LEFT_UP,
3573 wxMouseEventHandler(wxPropertyGrid::OnMouseUpChild),
3574 NULL, this);
3575 argWnd->Connect(id, wxEVT_LEFT_DOWN,
3576 wxMouseEventHandler(wxPropertyGrid::OnMouseClickChild),
3577 NULL, this);
3578 argWnd->Connect(id, wxEVT_RIGHT_UP,
3579 wxMouseEventHandler(wxPropertyGrid::OnMouseRightClickChild),
3580 NULL, this);
3581 argWnd->Connect(id, wxEVT_ENTER_WINDOW,
3582 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3583 NULL, this);
3584 argWnd->Connect(id, wxEVT_LEAVE_WINDOW,
3585 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry),
3586 NULL, this);
3587 }
3588
3589 wxPropertyGridEditorEventForwarder* forwarder;
3590 forwarder = new wxPropertyGridEditorEventForwarder(this);
3591 argWnd->PushEventHandler(forwarder);
3592
3593 argWnd->Connect(id, wxEVT_KEY_DOWN,
3594 wxCharEventHandler(wxPropertyGrid::OnChildKeyDown),
3595 NULL, this);
3596 }
3597
3598 void wxPropertyGrid::DestroyEditorWnd( wxWindow* wnd )
3599 {
3600 if ( !wnd )
3601 return;
3602
3603 wnd->Hide();
3604
3605 // Do not free editors immediately (for sake of processing events)
3606 wxPendingDelete.Append(wnd);
3607 }
3608
3609 void wxPropertyGrid::FreeEditors()
3610 {
3611 //
3612 // Return focus back to canvas from children (this is required at least for
3613 // GTK+, which, unlike Windows, clears focus when control is destroyed
3614 // instead of moving it to closest parent).
3615 wxWindow* focus = wxWindow::FindFocus();
3616 if ( focus )
3617 {
3618 wxWindow* parent = focus->GetParent();
3619 while ( parent )
3620 {
3621 if ( parent == this )
3622 {
3623 SetFocusOnCanvas();
3624 break;
3625 }
3626 parent = parent->GetParent();
3627 }
3628 }
3629
3630 // Do not free editors immediately if processing events
3631 if ( m_wndEditor2 )
3632 {
3633 wxEvtHandler* handler = m_wndEditor2->PopEventHandler(false);
3634 m_wndEditor2->Hide();
3635 wxPendingDelete.Append( handler );
3636 DestroyEditorWnd(m_wndEditor2);
3637 m_wndEditor2 = NULL;
3638 }
3639
3640 if ( m_wndEditor )
3641 {
3642 wxEvtHandler* handler = m_wndEditor->PopEventHandler(false);
3643 m_wndEditor->Hide();
3644 wxPendingDelete.Append( handler );
3645 DestroyEditorWnd(m_wndEditor);
3646 m_wndEditor = NULL;
3647 }
3648 }
3649
3650 // Call with NULL to de-select property
3651 bool wxPropertyGrid::DoSelectProperty( wxPGProperty* p, unsigned int flags )
3652 {
3653 /*
3654 if (p)
3655 {
3656 wxLogDebug(wxT("SelectProperty( %s (%s[%i]) )"),p->m_label.c_str(),
3657 p->m_parent->m_label.c_str(),p->GetIndexInParent());
3658 }
3659 else
3660 {
3661 wxLogDebug(wxT("SelectProperty( NULL, -1 )"));
3662 }
3663 */
3664
3665 if ( m_inDoSelectProperty )
3666 return true;
3667
3668 m_inDoSelectProperty = 1;
3669
3670 if ( !m_pState )
3671 {
3672 m_inDoSelectProperty = 0;
3673 return false;
3674 }
3675
3676 wxArrayPGProperty prevSelection = m_pState->m_selection;
3677 wxPGProperty* prevFirstSel;
3678
3679 if ( prevSelection.size() > 0 )
3680 prevFirstSel = prevSelection[0];
3681 else
3682 prevFirstSel = NULL;
3683
3684 if ( prevFirstSel && prevFirstSel->HasFlag(wxPG_PROP_BEING_DELETED) )
3685 prevFirstSel = NULL;
3686
3687 // Always send event, as this is indirect call
3688 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
3689
3690 /*
3691 if ( prevFirstSel )
3692 wxPrintf( "Selected %s\n", prevFirstSel->GetClassInfo()->GetClassName() );
3693 else
3694 wxPrintf( "None selected\n" );
3695
3696 if (p)
3697 wxPrintf( "P = %s\n", p->GetClassInfo()->GetClassName() );
3698 else
3699 wxPrintf( "P = NULL\n" );
3700 */
3701
3702 // If we are frozen, then just set the values.
3703 if ( m_frozen )
3704 {
3705 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3706 m_editorFocused = 0;
3707 m_pState->DoSetSelection(p);
3708
3709 // If frozen, always free controls. But don't worry, as Thaw will
3710 // recall SelectProperty to recreate them.
3711 FreeEditors();
3712
3713 // Prevent any further selection measures in this call
3714 p = NULL;
3715 }
3716 else
3717 {
3718 // Is it the same?
3719 if ( prevFirstSel == p &&
3720 prevSelection.size() <= 1 &&
3721 !(flags & wxPG_SEL_FORCE) )
3722 {
3723 // Only set focus if not deselecting
3724 if ( p )
3725 {
3726 if ( flags & wxPG_SEL_FOCUS )
3727 {
3728 if ( m_wndEditor )
3729 {
3730 m_wndEditor->SetFocus();
3731 m_editorFocused = 1;
3732 }
3733 }
3734 else
3735 {
3736 SetFocusOnCanvas();
3737 }
3738 }
3739
3740 m_inDoSelectProperty = 0;
3741 return true;
3742 }
3743
3744 //
3745 // First, deactivate previous
3746 if ( prevFirstSel )
3747 {
3748 OnValidationFailureReset(prevFirstSel);
3749
3750 // Must double-check if this is an selected in case of forceswitch
3751 if ( p != prevFirstSel )
3752 {
3753 if ( !CommitChangesFromEditor(flags) )
3754 {
3755 // Validation has failed, so we can't exit the previous editor
3756 //::wxMessageBox(_("Please correct the value or press ESC to cancel the edit."),
3757 // _("Invalid Value"),wxOK|wxICON_ERROR);
3758 m_inDoSelectProperty = 0;
3759 return false;
3760 }
3761 }
3762
3763 FreeEditors();
3764
3765 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3766 EditorsValueWasNotModified();
3767 }
3768
3769 SetInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3770
3771 m_pState->DoSetSelection(p);
3772
3773 // Redraw unselected
3774 for ( unsigned int i=0; i<prevSelection.size(); i++ )
3775 {
3776 DrawItem(prevSelection[i]);
3777 }
3778
3779 //
3780 // Then, activate the one given.
3781 if ( p )
3782 {
3783 int propY = p->GetY2(m_lineHeight);
3784
3785 int splitterX = GetSplitterPosition();
3786 m_editorFocused = 0;
3787 m_iFlags |= wxPG_FL_PRIMARY_FILLS_ENTIRE;
3788 if ( p != prevFirstSel )
3789 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED);
3790
3791 wxASSERT( m_wndEditor == NULL );
3792
3793 //
3794 // Only create editor for non-disabled non-caption
3795 if ( !p->IsCategory() && !(p->m_flags & wxPG_PROP_DISABLED) )
3796 {
3797 // do this for non-caption items
3798
3799 m_selColumn = 1;
3800
3801 // Do we need to paint the custom image, if any?
3802 m_iFlags &= ~(wxPG_FL_CUR_USES_CUSTOM_IMAGE);
3803 if ( (p->m_flags & wxPG_PROP_CUSTOMIMAGE) &&
3804 !p->GetEditorClass()->CanContainCustomImage()
3805 )
3806 m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3807
3808 wxRect grect = GetEditorWidgetRect(p, m_selColumn);
3809 wxPoint goodPos = grect.GetPosition();
3810
3811 const wxPGEditor* editor = p->GetEditorClass();
3812 wxCHECK_MSG(editor, false,
3813 wxT("NULL editor class not allowed"));
3814
3815 m_iFlags &= ~wxPG_FL_FIXED_WIDTH_EDITOR;
3816
3817 wxPGWindowList wndList =
3818 editor->CreateControls(this,
3819 p,
3820 goodPos,
3821 grect.GetSize());
3822
3823 m_wndEditor = wndList.m_primary;
3824 m_wndEditor2 = wndList.m_secondary;
3825 wxWindow* primaryCtrl = GetEditorControl();
3826
3827 //
3828 // Essentially, primaryCtrl == m_wndEditor
3829 //
3830
3831 // NOTE: It is allowed for m_wndEditor to be NULL - in this
3832 // case value is drawn as normal, and m_wndEditor2 is
3833 // assumed to be a right-aligned button that triggers
3834 // a separate editorCtrl window.
3835
3836 if ( m_wndEditor )
3837 {
3838 wxASSERT_MSG( m_wndEditor->GetParent() == GetPanel(),
3839 "CreateControls must use result of "
3840 "wxPropertyGrid::GetPanel() as parent "
3841 "of controls." );
3842
3843 // Set validator, if any
3844 #if wxUSE_VALIDATORS
3845 wxValidator* validator = p->GetValidator();
3846 if ( validator )
3847 primaryCtrl->SetValidator(*validator);
3848 #endif
3849
3850 if ( m_wndEditor->GetSize().y > (m_lineHeight+6) )
3851 m_iFlags |= wxPG_FL_ABNORMAL_EDITOR;
3852
3853 // If it has modified status, use bold font
3854 // (must be done before capturing m_ctrlXAdjust)
3855 if ( (p->m_flags & wxPG_PROP_MODIFIED) &&
3856 (m_windowStyle & wxPG_BOLD_MODIFIED) )
3857 SetCurControlBoldFont();
3858
3859 // Store x relative to splitter (we'll need it).
3860 m_ctrlXAdjust = m_wndEditor->GetPosition().x - splitterX;
3861
3862 // Check if background clear is not necessary
3863 wxPoint pos = m_wndEditor->GetPosition();
3864 if ( pos.x > (splitterX+1) || pos.y > propY )
3865 {
3866 m_iFlags &= ~(wxPG_FL_PRIMARY_FILLS_ENTIRE);
3867 }
3868
3869 m_wndEditor->SetSizeHints(3, 3);
3870
3871 SetupChildEventHandling(primaryCtrl);
3872
3873 // Focus and select all (wxTextCtrl, wxComboBox etc)
3874 if ( flags & wxPG_SEL_FOCUS )
3875 {
3876 primaryCtrl->SetFocus();
3877
3878 p->GetEditorClass()->OnFocus(p, primaryCtrl);
3879 }
3880 }
3881
3882 if ( m_wndEditor2 )
3883 {
3884 wxASSERT_MSG( m_wndEditor2->GetParent() == GetPanel(),
3885 "CreateControls must use result of "
3886 "wxPropertyGrid::GetPanel() as parent "
3887 "of controls." );
3888
3889 // Get proper id for wndSecondary
3890 m_wndSecId = m_wndEditor2->GetId();
3891 wxWindowList children = m_wndEditor2->GetChildren();
3892 wxWindowList::iterator node = children.begin();
3893 if ( node != children.end() )
3894 m_wndSecId = ((wxWindow*)*node)->GetId();
3895
3896 m_wndEditor2->SetSizeHints(3,3);
3897
3898 m_wndEditor2->Show();
3899
3900 SetupChildEventHandling(m_wndEditor2);
3901
3902 // If no primary editor, focus to button to allow
3903 // it to interprete ENTER etc.
3904 // NOTE: Due to problems focusing away from it, this
3905 // has been disabled.
3906 /*
3907 if ( (flags & wxPG_SEL_FOCUS) && !m_wndEditor )
3908 m_wndEditor2->SetFocus();
3909 */
3910 }
3911
3912 if ( flags & wxPG_SEL_FOCUS )
3913 m_editorFocused = 1;
3914
3915 }
3916 else
3917 {
3918 // Make sure focus is in grid canvas (important for wxGTK,
3919 // at least)
3920 SetFocusOnCanvas();
3921 }
3922
3923 EditorsValueWasNotModified();
3924
3925 // If it's inside collapsed section, expand parent, scroll, etc.
3926 // Also, if it was partially visible, scroll it into view.
3927 if ( !(flags & wxPG_SEL_NONVISIBLE) )
3928 EnsureVisible( p );
3929
3930 if ( m_wndEditor )
3931 {
3932 m_wndEditor->Show(true);
3933 }
3934
3935 if ( !(flags & wxPG_SEL_NO_REFRESH) )
3936 DrawItem(p);
3937 }
3938 else
3939 {
3940 // Make sure focus is in grid canvas
3941 SetFocusOnCanvas();
3942 }
3943
3944 ClearInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3945 }
3946
3947 #if wxUSE_STATUSBAR
3948
3949 //
3950 // Show help text in status bar.
3951 // (if found and grid not embedded in manager with help box and
3952 // style wxPG_EX_HELP_AS_TOOLTIPS is not used).
3953 //
3954
3955 if ( !(GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS) )
3956 {
3957 wxStatusBar* statusbar = NULL;
3958 if ( !(m_iFlags & wxPG_FL_NOSTATUSBARHELP) )
3959 {
3960 wxFrame* frame = wxDynamicCast(::wxGetTopLevelParent(this),wxFrame);
3961 if ( frame )
3962 statusbar = frame->GetStatusBar();
3963 }
3964
3965 if ( statusbar )
3966 {
3967 const wxString* pHelpString = (const wxString*) NULL;
3968
3969 if ( p )
3970 {
3971 pHelpString = &p->GetHelpString();
3972 if ( pHelpString->length() )
3973 {
3974 // Set help box text.
3975 statusbar->SetStatusText( *pHelpString );
3976 m_iFlags |= wxPG_FL_STRING_IN_STATUSBAR;
3977 }
3978 }
3979
3980 if ( (!pHelpString || !pHelpString->length()) &&
3981 (m_iFlags & wxPG_FL_STRING_IN_STATUSBAR) )
3982 {
3983 // Clear help box - but only if it was written
3984 // by us at previous time.
3985 statusbar->SetStatusText( m_emptyString );
3986 m_iFlags &= ~(wxPG_FL_STRING_IN_STATUSBAR);
3987 }
3988 }
3989 }
3990 #endif
3991
3992 m_inDoSelectProperty = 0;
3993
3994 // call wx event handler (here so that it also occurs on deselection)
3995 if ( !(flags & wxPG_SEL_DONT_SEND_EVENT) )
3996 SendEvent( wxEVT_PG_SELECTED, p, NULL );
3997
3998 return true;
3999 }
4000
4001 // -----------------------------------------------------------------------
4002
4003 bool wxPropertyGrid::UnfocusEditor()
4004 {
4005 wxPGProperty* selected = GetSelection();
4006
4007 if ( !selected || !m_wndEditor || m_frozen )
4008 return true;
4009
4010 if ( !CommitChangesFromEditor(0) )
4011 return false;
4012
4013 SetFocusOnCanvas();
4014 DrawItem(selected);
4015
4016 return true;
4017 }
4018
4019 // -----------------------------------------------------------------------
4020
4021 void wxPropertyGrid::RefreshEditor()
4022 {
4023 wxPGProperty* p = GetSelection();
4024 if ( !p )
4025 return;
4026
4027 wxWindow* wnd = GetEditorControl();
4028 if ( !wnd )
4029 return;
4030
4031 // Set editor font boldness - must do this before
4032 // calling UpdateControl().
4033 if ( HasFlag(wxPG_BOLD_MODIFIED) )
4034 {
4035 if ( p->HasFlag(wxPG_PROP_MODIFIED) )
4036 wnd->SetFont(GetCaptionFont());
4037 else
4038 wnd->SetFont(GetFont());
4039 }
4040
4041 const wxPGEditor* editorClass = p->GetEditorClass();
4042
4043 editorClass->UpdateControl(p, wnd);
4044
4045 if ( p->IsValueUnspecified() )
4046 editorClass ->SetValueToUnspecified(p, wnd);
4047 }
4048
4049 // -----------------------------------------------------------------------
4050
4051 bool wxPropertyGrid::SelectProperty( wxPGPropArg id, bool focus )
4052 {
4053 wxPG_PROP_ARG_CALL_PROLOG_RETVAL(false)
4054
4055 int flags = wxPG_SEL_DONT_SEND_EVENT;
4056 if ( focus )
4057 flags |= wxPG_SEL_FOCUS;
4058
4059 return DoSelectProperty(p, flags);
4060 }
4061
4062 // -----------------------------------------------------------------------
4063 // wxPropertyGrid expand/collapse state
4064 // -----------------------------------------------------------------------
4065
4066 bool wxPropertyGrid::DoCollapse( wxPGProperty* p, bool sendEvents )
4067 {
4068 wxPGProperty* pwc = wxStaticCast(p, wxPGProperty);
4069 wxPGProperty* selected = GetSelection();
4070
4071 // If active editor was inside collapsed section, then disable it
4072 if ( selected && selected->IsSomeParent(p) )
4073 {
4074 DoClearSelection();
4075 }
4076
4077 // Store dont-center-splitter flag 'cause we need to temporarily set it
4078 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
4079 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4080
4081 bool res = m_pState->DoCollapse(pwc);
4082
4083 if ( res )
4084 {
4085 if ( sendEvents )
4086 SendEvent( wxEVT_PG_ITEM_COLLAPSED, p );
4087
4088 RecalculateVirtualSize();
4089
4090 // Redraw etc. only if collapsed was visible.
4091 if (pwc->IsVisible() &&
4092 !m_frozen &&
4093 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) ) )
4094 {
4095 // When item is collapsed so that scrollbar would move,
4096 // graphics mess is about (unless we redraw everything).
4097 Refresh();
4098 }
4099 }
4100
4101 // Clear dont-center-splitter flag if it wasn't set
4102 m_iFlags = (m_iFlags & ~wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
4103
4104 return res;
4105 }
4106
4107 // -----------------------------------------------------------------------
4108
4109 bool wxPropertyGrid::DoExpand( wxPGProperty* p, bool sendEvents )
4110 {
4111 wxCHECK_MSG( p, false, wxT("invalid property id") );
4112
4113 wxPGProperty* pwc = (wxPGProperty*)p;
4114
4115 // Store dont-center-splitter flag 'cause we need to temporarily set it
4116 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
4117 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4118
4119 bool res = m_pState->DoExpand(pwc);
4120
4121 if ( res )
4122 {
4123 if ( sendEvents )
4124 SendEvent( wxEVT_PG_ITEM_EXPANDED, p );
4125
4126 RecalculateVirtualSize();
4127
4128 // Redraw etc. only if expanded was visible.
4129 if ( pwc->IsVisible() && !m_frozen &&
4130 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) )
4131 )
4132 {
4133 // Redraw
4134 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4135 Refresh();
4136 #else
4137 DrawItems(pwc, NULL);
4138 #endif
4139 }
4140 }
4141
4142 // Clear dont-center-splitter flag if it wasn't set
4143 m_iFlags = (m_iFlags & ~wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
4144
4145 return res;
4146 }
4147
4148 // -----------------------------------------------------------------------
4149
4150 bool wxPropertyGrid::DoHideProperty( wxPGProperty* p, bool hide, int flags )
4151 {
4152 if ( m_frozen )
4153 return m_pState->DoHideProperty(p, hide, flags);
4154
4155 wxArrayPGProperty selection = m_pState->m_selection; // Must use a copy
4156 int selRemoveCount = 0;
4157 for ( unsigned int i=0; i<selection.size(); i++ )
4158 {
4159 wxPGProperty* selected = selection[i];
4160 if ( selected == p || selected->IsSomeParent(p) )
4161 {
4162 if ( !DoRemoveFromSelection(p, flags) )
4163 return false;
4164 selRemoveCount += 1;
4165 }
4166 }
4167
4168 m_pState->DoHideProperty(p, hide, flags);
4169
4170 RecalculateVirtualSize();
4171 Refresh();
4172
4173 return true;
4174 }
4175
4176
4177 // -----------------------------------------------------------------------
4178 // wxPropertyGrid size related methods
4179 // -----------------------------------------------------------------------
4180
4181 void wxPropertyGrid::RecalculateVirtualSize( int forceXPos )
4182 {
4183 if ( (m_iFlags & wxPG_FL_RECALCULATING_VIRTUAL_SIZE) || m_frozen )
4184 return;
4185
4186 //
4187 // If virtual height was changed, then recalculate editor control position(s)
4188 if ( m_pState->m_vhCalcPending )
4189 CorrectEditorWidgetPosY();
4190
4191 m_pState->EnsureVirtualHeight();
4192
4193 wxASSERT_LEVEL_2_MSG(
4194 m_pState->GetVirtualHeight() == m_pState->GetActualVirtualHeight(),
4195 "VirtualHeight and ActualVirtualHeight should match"
4196 );
4197
4198 m_iFlags |= wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4199
4200 int x = m_pState->m_width;
4201 int y = m_pState->m_virtualHeight;
4202
4203 int width, height;
4204 GetClientSize(&width,&height);
4205
4206 // Now adjust virtual size.
4207 SetVirtualSize(x, y);
4208
4209 int xAmount = 0;
4210 int xPos = 0;
4211
4212 //
4213 // Adjust scrollbars
4214 if ( HasVirtualWidth() )
4215 {
4216 xAmount = x/wxPG_PIXELS_PER_UNIT;
4217 xPos = GetScrollPos( wxHORIZONTAL );
4218 }
4219
4220 if ( forceXPos != -1 )
4221 xPos = forceXPos;
4222 // xPos too high?
4223 else if ( xPos > (xAmount-(width/wxPG_PIXELS_PER_UNIT)) )
4224 xPos = 0;
4225
4226 int yAmount = y / wxPG_PIXELS_PER_UNIT;
4227 int yPos = GetScrollPos( wxVERTICAL );
4228
4229 SetScrollbars( wxPG_PIXELS_PER_UNIT, wxPG_PIXELS_PER_UNIT,
4230 xAmount, yAmount, xPos, yPos, true );
4231
4232 // Must re-get size now
4233 GetClientSize(&width,&height);
4234
4235 if ( !HasVirtualWidth() )
4236 {
4237 m_pState->SetVirtualWidth(width);
4238 x = width;
4239 }
4240
4241 m_width = width;
4242 m_height = height;
4243
4244 m_pState->CheckColumnWidths();
4245
4246 if ( GetSelection() )
4247 CorrectEditorWidgetSizeX();
4248
4249 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4250 }
4251
4252 // -----------------------------------------------------------------------
4253
4254 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4255 {
4256 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4257 return;
4258
4259 int width, height;
4260 GetClientSize(&width, &height);
4261
4262 m_width = width;
4263 m_height = height;
4264
4265 #if wxPG_DOUBLE_BUFFER
4266 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4267 {
4268 int dblh = (m_lineHeight*2);
4269 if ( !m_doubleBuffer )
4270 {
4271 // Create double buffer bitmap to draw on, if none
4272 int w = (width>250)?width:250;
4273 int h = height + dblh;
4274 h = (h>400)?h:400;
4275 m_doubleBuffer = new wxBitmap( w, h );
4276 }
4277 else
4278 {
4279 int w = m_doubleBuffer->GetWidth();
4280 int h = m_doubleBuffer->GetHeight();
4281
4282 // Double buffer must be large enough
4283 if ( w < width || h < (height+dblh) )
4284 {
4285 if ( w < width ) w = width;
4286 if ( h < (height+dblh) ) h = height + dblh;
4287 delete m_doubleBuffer;
4288 m_doubleBuffer = new wxBitmap( w, h );
4289 }
4290 }
4291 }
4292
4293 #endif
4294
4295 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4296 m_ncWidth = event.GetSize().x;
4297
4298 if ( !m_frozen )
4299 {
4300 if ( m_pState->m_itemsAdded )
4301 PrepareAfterItemsAdded();
4302 else
4303 // Without this, virtual size (atleast under wxGTK) will be skewed
4304 RecalculateVirtualSize();
4305
4306 Refresh();
4307 }
4308 }
4309
4310 // -----------------------------------------------------------------------
4311
4312 void wxPropertyGrid::SetVirtualWidth( int width )
4313 {
4314 if ( width == -1 )
4315 {
4316 // Disable virtual width
4317 width = GetClientSize().x;
4318 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4319 }
4320 else
4321 {
4322 // Enable virtual width
4323 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4324 }
4325 m_pState->SetVirtualWidth( width );
4326 }
4327
4328 void wxPropertyGrid::SetFocusOnCanvas()
4329 {
4330 SetFocusIgnoringChildren();
4331 m_editorFocused = 0;
4332 }
4333
4334 // -----------------------------------------------------------------------
4335 // wxPropertyGrid mouse event handling
4336 // -----------------------------------------------------------------------
4337
4338 // selFlags uses same values DoSelectProperty's flags
4339 // Returns true if event was vetoed.
4340 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p,
4341 wxVariant* pValue,
4342 unsigned int selFlags,
4343 unsigned int column )
4344 {
4345 // Send property grid event of specific type and with specific property
4346 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4347 evt.SetPropertyGrid(this);
4348 evt.SetEventObject(m_eventObject);
4349 evt.SetProperty(p);
4350 evt.SetColumn(column);
4351 if ( eventType == wxEVT_PG_CHANGING )
4352 {
4353 wxASSERT( pValue );
4354 evt.SetCanVeto(true);
4355 m_validationInfo.m_pValue = pValue;
4356 evt.SetupValidationInfo();
4357 }
4358 else
4359 {
4360 if ( p )
4361 evt.SetPropertyValue(p->GetValue());
4362
4363 if ( !(selFlags & wxPG_SEL_NOVALIDATE) )
4364 evt.SetCanVeto(true);
4365 }
4366
4367 m_processedEvent = &evt;
4368 m_eventObject->HandleWindowEvent(evt);
4369 m_processedEvent = NULL;
4370
4371 return evt.WasVetoed();
4372 }
4373
4374 // -----------------------------------------------------------------------
4375
4376 // Return false if should be skipped
4377 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4378 {
4379 bool res = true;
4380
4381 // Need to set focus?
4382 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4383 {
4384 SetFocusOnCanvas();
4385 }
4386
4387 wxPropertyGridPageState* state = m_pState;
4388 int splitterHit;
4389 int splitterHitOffset;
4390 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4391
4392 wxPGProperty* p = DoGetItemAtY(y);
4393
4394 if ( p )
4395 {
4396 int depth = (int)p->GetDepth() - 1;
4397
4398 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4399
4400 if ( x >= marginEnds )
4401 {
4402 // Outside margin.
4403
4404 if ( p->IsCategory() )
4405 {
4406 // This is category.
4407 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4408
4409 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4410
4411 // Expand, collapse, activate etc. if click on text or left of splitter.
4412 if ( x >= textX
4413 &&
4414 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4415 columnHit == 0
4416 )
4417 )
4418 {
4419 if ( !AddToSelectionFromInputEvent( p,
4420 columnHit,
4421 &event ) )
4422 return res;
4423
4424 // On double-click, expand/collapse.
4425 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4426 {
4427 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4428 else DoExpand( p, true );
4429 }
4430 }
4431 }
4432 else if ( splitterHit == -1 )
4433 {
4434 // Click on value.
4435 unsigned int selFlag = 0;
4436 if ( columnHit == 1 )
4437 {
4438 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4439 selFlag = wxPG_SEL_FOCUS;
4440 }
4441 if ( !AddToSelectionFromInputEvent( p,
4442 columnHit,
4443 &event,
4444 selFlag ) )
4445 return res;
4446
4447 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4448
4449 if ( p->GetChildCount() && !p->IsCategory() )
4450 // On double-click, expand/collapse.
4451 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4452 {
4453 wxPGProperty* pwc = (wxPGProperty*)p;
4454 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4455 else DoExpand( p, true );
4456 }
4457
4458 res = false;
4459 }
4460 else
4461 {
4462 // click on splitter
4463 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4464 {
4465 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4466 {
4467 // Double-clicking the splitter causes auto-centering
4468 CenterSplitter( true );
4469 }
4470 else if ( m_dragStatus == 0 )
4471 {
4472 //
4473 // Begin draggin the splitter
4474 //
4475
4476 // send event
4477 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
4478
4479 if ( m_wndEditor )
4480 {
4481 // Changes must be committed here or the
4482 // value won't be drawn correctly
4483 if ( !CommitChangesFromEditor() )
4484 return res;
4485
4486 m_wndEditor->Show ( false );
4487 }
4488
4489 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4490 {
4491 CaptureMouse();
4492 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4493 }
4494
4495 m_dragStatus = 1;
4496 m_draggedSplitter = splitterHit;
4497 m_dragOffset = splitterHitOffset;
4498
4499 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4500 // Fixes button disappearance bug
4501 if ( m_wndEditor2 )
4502 m_wndEditor2->Show ( false );
4503 #endif
4504
4505 m_startingSplitterX = x - splitterHitOffset;
4506 }
4507 }
4508 }
4509 }
4510 else
4511 {
4512 // Click on margin.
4513 if ( p->GetChildCount() )
4514 {
4515 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4516
4517 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4518 {
4519 int y2 = y % m_lineHeight;
4520 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4521 {
4522 // On click on expander button, expand/collapse
4523 if ( ((wxPGProperty*)p)->IsExpanded() )
4524 DoCollapse( p, true );
4525 else
4526 DoExpand( p, true );
4527 }
4528 }
4529 }
4530 }
4531 }
4532 return res;
4533 }
4534
4535 // -----------------------------------------------------------------------
4536
4537 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x),
4538 unsigned int WXUNUSED(y),
4539 wxMouseEvent& event )
4540 {
4541 if ( m_propHover )
4542 {
4543 // Select property here as well
4544 wxPGProperty* p = m_propHover;
4545 AddToSelectionFromInputEvent(p, m_colHover, &event);
4546
4547 // Send right click event.
4548 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4549
4550 return true;
4551 }
4552 return false;
4553 }
4554
4555 // -----------------------------------------------------------------------
4556
4557 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x),
4558 unsigned int WXUNUSED(y),
4559 wxMouseEvent& event )
4560 {
4561 if ( m_propHover )
4562 {
4563 // Select property here as well
4564 wxPGProperty* p = m_propHover;
4565
4566 AddToSelectionFromInputEvent(p, m_colHover, &event);
4567
4568 // Send double-click event.
4569 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4570
4571 return true;
4572 }
4573 return false;
4574 }
4575
4576 // -----------------------------------------------------------------------
4577
4578 #if wxPG_SUPPORT_TOOLTIPS
4579
4580 void wxPropertyGrid::SetToolTip( const wxString& tipString )
4581 {
4582 if ( tipString.length() )
4583 {
4584 wxScrolledWindow::SetToolTip(tipString);
4585 }
4586 else
4587 {
4588 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4589 wxScrolledWindow::SetToolTip( m_emptyString );
4590 #else
4591 wxScrolledWindow::SetToolTip( NULL );
4592 #endif
4593 }
4594 }
4595
4596 #endif // #if wxPG_SUPPORT_TOOLTIPS
4597
4598 // -----------------------------------------------------------------------
4599
4600 // Return false if should be skipped
4601 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y,
4602 wxMouseEvent &event )
4603 {
4604 // Safety check (needed because mouse capturing may
4605 // otherwise freeze the control)
4606 if ( m_dragStatus > 0 && !event.Dragging() )
4607 {
4608 HandleMouseUp(x, y, event);
4609 }
4610
4611 wxPropertyGridPageState* state = m_pState;
4612 int splitterHit;
4613 int splitterHitOffset;
4614 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4615 int splitterX = x - splitterHitOffset;
4616
4617 m_colHover = columnHit;
4618
4619 if ( m_dragStatus > 0 )
4620 {
4621 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4622 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4623 {
4624
4625 int newSplitterX = x - m_dragOffset;
4626 int splitterX = x - splitterHitOffset;
4627
4628 // Splitter redraw required?
4629 if ( newSplitterX != splitterX )
4630 {
4631 // Move everything
4632 SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
4633 state->DoSetSplitterPosition(newSplitterX,
4634 m_draggedSplitter,
4635 false);
4636 state->m_fSplitterX = (float) newSplitterX;
4637
4638 if ( GetSelection() )
4639 CorrectEditorWidgetSizeX();
4640
4641 Update();
4642 Refresh();
4643 }
4644
4645 m_dragStatus = 2;
4646 }
4647
4648 return false;
4649 }
4650 else
4651 {
4652
4653 int ih = m_lineHeight;
4654 int sy = y;
4655
4656 #if wxPG_SUPPORT_TOOLTIPS
4657 wxPGProperty* prevHover = m_propHover;
4658 unsigned char prevSide = m_mouseSide;
4659 #endif
4660 int curPropHoverY = y - (y % ih);
4661
4662 // On which item it hovers
4663 if ( !m_propHover
4664 ||
4665 ( sy < m_propHoverY || sy >= (m_propHoverY+ih) )
4666 )
4667 {
4668 // Mouse moves on another property
4669
4670 m_propHover = DoGetItemAtY(y);
4671 m_propHoverY = curPropHoverY;
4672
4673 // Send hover event
4674 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
4675 }
4676
4677 #if wxPG_SUPPORT_TOOLTIPS
4678 // Store which side we are on
4679 m_mouseSide = 0;
4680 if ( columnHit == 1 )
4681 m_mouseSide = 2;
4682 else if ( columnHit == 0 )
4683 m_mouseSide = 1;
4684
4685 //
4686 // If tooltips are enabled, show label or value as a tip
4687 // in case it doesn't otherwise show in full length.
4688 //
4689 if ( m_windowStyle & wxPG_TOOLTIPS )
4690 {
4691 wxToolTip* tooltip = GetToolTip();
4692
4693 if ( m_propHover != prevHover || prevSide != m_mouseSide )
4694 {
4695 if ( m_propHover && !m_propHover->IsCategory() )
4696 {
4697
4698 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
4699 {
4700 // Show help string as a tooltip
4701 wxString tipString = m_propHover->GetHelpString();
4702
4703 SetToolTip(tipString);
4704 }
4705 else
4706 {
4707 // Show cropped value string as a tooltip
4708 wxString tipString;
4709 int space = 0;
4710
4711 if ( m_mouseSide == 1 )
4712 {
4713 tipString = m_propHover->m_label;
4714 space = splitterX-m_marginWidth-3;
4715 }
4716 else if ( m_mouseSide == 2 )
4717 {
4718 tipString = m_propHover->GetDisplayedString();
4719
4720 space = m_width - splitterX;
4721 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
4722 space -= wxPG_CUSTOM_IMAGE_WIDTH +
4723 wxCC_CUSTOM_IMAGE_MARGIN1 +
4724 wxCC_CUSTOM_IMAGE_MARGIN2;
4725 }
4726
4727 if ( space )
4728 {
4729 int tw, th;
4730 GetTextExtent( tipString, &tw, &th, 0, 0 );
4731 if ( tw > space )
4732 {
4733 SetToolTip( tipString );
4734 }
4735 }
4736 else
4737 {
4738 if ( tooltip )
4739 {
4740 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4741 SetToolTip( m_emptyString );
4742 #else
4743 wxScrolledWindow::SetToolTip( NULL );
4744 #endif
4745 }
4746 }
4747
4748 }
4749 }
4750 else
4751 {
4752 if ( tooltip )
4753 {
4754 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4755 SetToolTip( m_emptyString );
4756 #else
4757 wxScrolledWindow::SetToolTip( NULL );
4758 #endif
4759 }
4760 }
4761 }
4762 }
4763 #endif
4764
4765 if ( splitterHit == -1 ||
4766 !m_propHover ||
4767 HasFlag(wxPG_STATIC_SPLITTER) )
4768 {
4769 // hovering on something else
4770 if ( m_curcursor != wxCURSOR_ARROW )
4771 CustomSetCursor( wxCURSOR_ARROW );
4772 }
4773 else
4774 {
4775 // Do not allow splitter cursor on caption items.
4776 // (also not if we were dragging and its started
4777 // outside the splitter region)
4778
4779 if ( !m_propHover->IsCategory() &&
4780 !event.Dragging() )
4781 {
4782
4783 // hovering on splitter
4784
4785 // NB: Condition disabled since MouseLeave event (from the
4786 // editor control) cannot be reliably detected.
4787 //if ( m_curcursor != wxCURSOR_SIZEWE )
4788 CustomSetCursor( wxCURSOR_SIZEWE, true );
4789
4790 return false;
4791 }
4792 else
4793 {
4794 // hovering on something else
4795 if ( m_curcursor != wxCURSOR_ARROW )
4796 CustomSetCursor( wxCURSOR_ARROW );
4797 }
4798 }
4799
4800 //
4801 // Multi select by dragging
4802 //
4803 if ( (GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION) &&
4804 event.LeftIsDown() &&
4805 m_propHover &&
4806 GetSelection() &&
4807 columnHit != 1 &&
4808 !state->DoIsPropertySelected(m_propHover) )
4809 {
4810 // Additional requirement is that the hovered property
4811 // is adjacent to edges of selection.
4812 const wxArrayPGProperty& selection = GetSelectedProperties();
4813
4814 // Since categories cannot be selected along with 'other'
4815 // properties, exclude them from iterator flags.
4816 int iterFlags = wxPG_ITERATE_VISIBLE & (~wxPG_PROP_CATEGORY);
4817
4818 for ( int i=(selection.size()-1); i>=0; i-- )
4819 {
4820 // TODO: This could be optimized by keeping track of
4821 // which properties are at the edges of selection.
4822 wxPGProperty* selProp = selection[i];
4823 if ( state->ArePropertiesAdjacent(m_propHover, selProp,
4824 iterFlags) )
4825 {
4826 DoAddToSelection(m_propHover);
4827 break;
4828 }
4829 }
4830 }
4831 }
4832 return true;
4833 }
4834
4835 // -----------------------------------------------------------------------
4836
4837 // Also handles Leaving event
4838 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
4839 wxMouseEvent &WXUNUSED(event) )
4840 {
4841 wxPropertyGridPageState* state = m_pState;
4842 bool res = false;
4843
4844 int splitterHit;
4845 int splitterHitOffset;
4846 state->HitTestH( x, &splitterHit, &splitterHitOffset );
4847
4848 // No event type check - basicly calling this method should
4849 // just stop dragging.
4850 // Left up after dragged?
4851 if ( m_dragStatus >= 1 )
4852 {
4853 //
4854 // End Splitter Dragging
4855 //
4856 // DO NOT ENABLE FOLLOWING LINE!
4857 // (it is only here as a reminder to not to do it)
4858 //splitterX = x;
4859
4860 // Disable splitter auto-centering
4861 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4862
4863 // This is necessary to return cursor
4864 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
4865 {
4866 ReleaseMouse();
4867 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
4868 }
4869
4870 // Set back the default cursor, if necessary
4871 if ( splitterHit == -1 ||
4872 !m_propHover )
4873 {
4874 CustomSetCursor( wxCURSOR_ARROW );
4875 }
4876
4877 m_dragStatus = 0;
4878
4879 // Control background needs to be cleared
4880 wxPGProperty* selected = GetSelection();
4881 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && selected )
4882 DrawItem( selected );
4883
4884 if ( m_wndEditor )
4885 {
4886 m_wndEditor->Show ( true );
4887 }
4888
4889 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4890 // Fixes button disappearance bug
4891 if ( m_wndEditor2 )
4892 m_wndEditor2->Show ( true );
4893 #endif
4894
4895 // This clears the focus.
4896 m_editorFocused = 0;
4897
4898 }
4899 return res;
4900 }
4901
4902 // -----------------------------------------------------------------------
4903
4904 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
4905 {
4906 int splitterX = GetSplitterPosition();
4907
4908 int ux, uy;
4909 CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
4910
4911 wxWindow* wnd = GetEditorControl();
4912
4913 // Hide popup on clicks
4914 if ( event.GetEventType() != wxEVT_MOTION )
4915 if ( wnd && wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
4916 {
4917 ((wxOwnerDrawnComboBox*)wnd)->HidePopup();
4918 }
4919
4920 wxRect r;
4921 if ( wnd )
4922 r = wnd->GetRect();
4923 if ( wnd == NULL || m_dragStatus ||
4924 (
4925 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
4926 ux >= (r.x+r.width) ||
4927 event.m_y < r.y ||
4928 event.m_y >= (r.y+r.height)
4929 )
4930 )
4931 {
4932 *px = ux;
4933 *py = uy;
4934 return true;
4935 }
4936 else
4937 {
4938 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
4939 }
4940 return false;
4941 }
4942
4943 // -----------------------------------------------------------------------
4944
4945 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
4946 {
4947 int x, y;
4948 if ( OnMouseCommon( event, &x, &y ) )
4949 {
4950 HandleMouseClick(x,y,event);
4951 }
4952 event.Skip();
4953 }
4954
4955 // -----------------------------------------------------------------------
4956
4957 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
4958 {
4959 int x, y;
4960 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
4961 HandleMouseRightClick(x,y,event);
4962 event.Skip();
4963 }
4964
4965 // -----------------------------------------------------------------------
4966
4967 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
4968 {
4969 // Always run standard mouse-down handler as well
4970 OnMouseClick(event);
4971
4972 int x, y;
4973 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
4974 HandleMouseDoubleClick(x,y,event);
4975 event.Skip();
4976 }
4977
4978 // -----------------------------------------------------------------------
4979
4980 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
4981 {
4982 int x, y;
4983 if ( OnMouseCommon( event, &x, &y ) )
4984 {
4985 HandleMouseMove(x,y,event);
4986 }
4987 event.Skip();
4988 }
4989
4990 // -----------------------------------------------------------------------
4991
4992 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
4993 {
4994 int x, y;
4995 if ( OnMouseCommon( event, &x, &y ) )
4996 {
4997 HandleMouseUp(x,y,event);
4998 }
4999 event.Skip();
5000 }
5001
5002 // -----------------------------------------------------------------------
5003
5004 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
5005 {
5006 // This may get called from child control as well, so event's
5007 // mouse position cannot be relied on.
5008
5009 if ( event.Entering() )
5010 {
5011 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5012 {
5013 // TODO: Fix this (detect parent and only do
5014 // cursor trick if it is a manager).
5015 wxASSERT( GetParent() );
5016 GetParent()->SetCursor(wxNullCursor);
5017
5018 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
5019 }
5020 else
5021 GetParent()->SetCursor(wxNullCursor);
5022 }
5023 else if ( event.Leaving() )
5024 {
5025 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
5026 SetCursor( wxNullCursor );
5027
5028 // Get real cursor position
5029 wxPoint pt = ScreenToClient(::wxGetMousePosition());
5030
5031 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
5032 {
5033 {
5034 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5035 {
5036 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
5037 }
5038
5039 if ( m_dragStatus )
5040 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
5041 }
5042 }
5043 }
5044
5045 event.Skip();
5046 }
5047
5048 // -----------------------------------------------------------------------
5049
5050 // Common code used by various OnMouseXXXChild methods.
5051 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
5052 {
5053 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
5054 wxASSERT( topCtrlWnd );
5055 int x, y;
5056 event.GetPosition(&x,&y);
5057
5058 int splitterX = GetSplitterPosition();
5059
5060 wxRect r = topCtrlWnd->GetRect();
5061 if ( !m_dragStatus &&
5062 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
5063 y >= 0 && y < r.height \
5064 )
5065 {
5066 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5067 event.Skip();
5068 }
5069 else
5070 {
5071 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
5072 px, py );
5073 return true;
5074 }
5075 return false;
5076 }
5077
5078 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
5079 {
5080 int x,y;
5081 if ( OnMouseChildCommon(event,&x,&y) )
5082 {
5083 bool res = HandleMouseClick(x,y,event);
5084 if ( !res ) event.Skip();
5085 }
5086 }
5087
5088 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
5089 {
5090 int x,y;
5091 wxASSERT( m_wndEditor );
5092 // These coords may not be exact (about +-2),
5093 // but that should not matter (right click is about item, not position).
5094 wxPoint pt = m_wndEditor->GetPosition();
5095 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
5096
5097 // FIXME: Used to set m_propHover to selection here. Was it really
5098 // necessary?
5099
5100 bool res = HandleMouseRightClick(x,y,event);
5101 if ( !res ) event.Skip();
5102 }
5103
5104 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
5105 {
5106 int x,y;
5107 if ( OnMouseChildCommon(event,&x,&y) )
5108 {
5109 bool res = HandleMouseMove(x,y,event);
5110 if ( !res ) event.Skip();
5111 }
5112 }
5113
5114 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
5115 {
5116 int x,y;
5117 if ( OnMouseChildCommon(event,&x,&y) )
5118 {
5119 bool res = HandleMouseUp(x,y,event);
5120 if ( !res ) event.Skip();
5121 }
5122 }
5123
5124 // -----------------------------------------------------------------------
5125 // wxPropertyGrid keyboard event handling
5126 // -----------------------------------------------------------------------
5127
5128 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
5129 {
5130 // Translates wxKeyEvent to wxPG_ACTION_XXX
5131
5132 int keycode = event.GetKeyCode();
5133 int modifiers = event.GetModifiers();
5134
5135 wxASSERT( !(modifiers&~(0xFFFF)) );
5136
5137 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5138
5139 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
5140
5141 if ( it == m_actionTriggers.end() )
5142 return 0;
5143
5144 if ( pSecond )
5145 {
5146 int second = (it->second>>16) & 0xFFFF;
5147 *pSecond = second;
5148 }
5149
5150 return (it->second & 0xFFFF);
5151 }
5152
5153 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
5154 {
5155 wxASSERT( !(modifiers&~(0xFFFF)) );
5156
5157 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5158
5159 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
5160
5161 if ( it != m_actionTriggers.end() )
5162 {
5163 // This key combination is already used
5164
5165 // Can add secondary?
5166 wxASSERT_MSG( !(it->second&~(0xFFFF)),
5167 wxT("You can only add up to two separate actions per key combination.") );
5168
5169 action = it->second | (action<<16);
5170 }
5171
5172 m_actionTriggers[hashMapKey] = action;
5173 }
5174
5175 void wxPropertyGrid::ClearActionTriggers( int action )
5176 {
5177 wxPGHashMapI2I::iterator it;
5178 bool didSomething;
5179
5180 do
5181 {
5182 didSomething = false;
5183
5184 for ( it = m_actionTriggers.begin();
5185 it != m_actionTriggers.end();
5186 it++ )
5187 {
5188 if ( it->second == action )
5189 {
5190 m_actionTriggers.erase(it);
5191 didSomething = true;
5192 break;
5193 }
5194 }
5195 }
5196 while ( didSomething );
5197 }
5198
5199 void wxPropertyGrid::HandleKeyEvent( wxKeyEvent &event, bool fromChild )
5200 {
5201 //
5202 // Handles key event when editor control is not focused.
5203 //
5204
5205 wxCHECK2(!m_frozen, return);
5206
5207 // Travelsal between items, collapsing/expanding, etc.
5208 wxPGProperty* selected = GetSelection();
5209 int keycode = event.GetKeyCode();
5210 bool editorFocused = IsEditorFocused();
5211
5212 if ( keycode == WXK_TAB )
5213 {
5214 wxWindow* mainControl;
5215
5216 if ( HasInternalFlag(wxPG_FL_IN_MANAGER) )
5217 mainControl = GetParent();
5218 else
5219 mainControl = this;
5220
5221 if ( !event.ShiftDown() )
5222 {
5223 if ( !editorFocused && m_wndEditor )
5224 {
5225 DoSelectProperty( selected, wxPG_SEL_FOCUS );
5226 }
5227 else
5228 {
5229 // Tab traversal workaround for platforms on which
5230 // wxWindow::Navigate() may navigate into first child
5231 // instead of next sibling. Does not work perfectly
5232 // in every scenario (for instance, when property grid
5233 // is either first or last control).
5234 #if defined(__WXGTK__)
5235 wxWindow* sibling = mainControl->GetNextSibling();
5236 if ( sibling )
5237 sibling->SetFocusFromKbd();
5238 #else
5239 Navigate(wxNavigationKeyEvent::IsForward);
5240 #endif
5241 }
5242 }
5243 else
5244 {
5245 if ( editorFocused )
5246 {
5247 UnfocusEditor();
5248 }
5249 else
5250 {
5251 #if defined(__WXGTK__)
5252 wxWindow* sibling = mainControl->GetPrevSibling();
5253 if ( sibling )
5254 sibling->SetFocusFromKbd();
5255 #else
5256 Navigate(wxNavigationKeyEvent::IsBackward);
5257 #endif
5258 }
5259 }
5260
5261 return;
5262 }
5263
5264 // Ignore Alt and Control when they are down alone
5265 if ( keycode == WXK_ALT ||
5266 keycode == WXK_CONTROL )
5267 {
5268 event.Skip();
5269 return;
5270 }
5271
5272 int secondAction;
5273 int action = KeyEventToActions(event, &secondAction);
5274
5275 if ( editorFocused && action == wxPG_ACTION_CANCEL_EDIT )
5276 {
5277 //
5278 // Esc cancels any changes
5279 if ( IsEditorsValueModified() )
5280 {
5281 EditorsValueWasNotModified();
5282
5283 // Update the control as well
5284 selected->GetEditorClass()->
5285 SetControlStringValue( selected,
5286 GetEditorControl(),
5287 selected->GetDisplayedString() );
5288 }
5289
5290 OnValidationFailureReset(selected);
5291
5292 UnfocusEditor();
5293 return;
5294 }
5295
5296 // Except for TAB and ESC, handle child control events in child control
5297 if ( fromChild )
5298 {
5299 // Only propagate event if it had modifiers
5300 if ( !event.HasModifiers() )
5301 {
5302 event.StopPropagation();
5303 }
5304 event.Skip();
5305 return;
5306 }
5307
5308 bool wasHandled = false;
5309
5310 if ( selected )
5311 {
5312 // Show dialog?
5313 if ( ButtonTriggerKeyTest(action, event) )
5314 return;
5315
5316 wxPGProperty* p = selected;
5317
5318 // Travel and expand/collapse
5319 int selectDir = -2;
5320
5321 if ( p->GetChildCount() )
5322 {
5323 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5324 {
5325 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5326 wasHandled = true;
5327 }
5328 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5329 {
5330 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5331 wasHandled = true;
5332 }
5333 }
5334
5335 if ( !wasHandled )
5336 {
5337 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5338 {
5339 selectDir = -1;
5340 }
5341 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5342 {
5343 selectDir = 1;
5344 }
5345 }
5346
5347 if ( selectDir >= -1 )
5348 {
5349 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5350 if ( p )
5351 DoSelectProperty(p);
5352 wasHandled = true;
5353 }
5354 }
5355 else
5356 {
5357 // If nothing was selected, select the first item now
5358 // (or navigate out of tab).
5359 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5360 {
5361 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5362 if ( p ) DoSelectProperty(p);
5363 wasHandled = true;
5364 }
5365 }
5366
5367 if ( !wasHandled )
5368 event.Skip();
5369 }
5370
5371 // -----------------------------------------------------------------------
5372
5373 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5374 {
5375 // If there was editor open and focused, then this event should not
5376 // really be processed here.
5377 if ( IsEditorFocused() )
5378 {
5379 // However, if event had modifiers, it is probably still best
5380 // to skip it.
5381 if ( event.HasModifiers() )
5382 event.Skip();
5383 else
5384 event.StopPropagation();
5385 return;
5386 }
5387
5388 HandleKeyEvent(event, false);
5389 }
5390
5391 // -----------------------------------------------------------------------
5392
5393 bool wxPropertyGrid::ButtonTriggerKeyTest( int action, wxKeyEvent& event )
5394 {
5395 if ( action == -1 )
5396 {
5397 int secondAction;
5398 action = KeyEventToActions(event, &secondAction);
5399 }
5400
5401 // Does the keycode trigger button?
5402 if ( action == wxPG_ACTION_PRESS_BUTTON &&
5403 m_wndEditor2 )
5404 {
5405 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, m_wndEditor2->GetId());
5406 GetEventHandler()->AddPendingEvent(evt);
5407 return true;
5408 }
5409
5410 return false;
5411 }
5412
5413 // -----------------------------------------------------------------------
5414
5415 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5416 {
5417 HandleKeyEvent(event, true);
5418 }
5419
5420 // -----------------------------------------------------------------------
5421 // wxPropertyGrid miscellaneous event handling
5422 // -----------------------------------------------------------------------
5423
5424 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5425 {
5426 //
5427 // Check if the focus is in this control or one of its children
5428 wxWindow* newFocused = wxWindow::FindFocus();
5429
5430 if ( newFocused != m_curFocused )
5431 HandleFocusChange( newFocused );
5432
5433 //
5434 // Check if top-level parent has changed
5435 if ( GetExtraStyle() & wxPG_EX_ENABLE_TLP_TRACKING )
5436 {
5437 wxWindow* tlp = ::wxGetTopLevelParent(this);
5438 if ( tlp != m_tlp )
5439 OnTLPChanging(tlp);
5440 }
5441 }
5442
5443 bool wxPropertyGrid::IsEditorFocused() const
5444 {
5445 wxWindow* focus = wxWindow::FindFocus();
5446
5447 if ( focus == m_wndEditor || focus == m_wndEditor2 ||
5448 focus == GetEditorControl() )
5449 return true;
5450
5451 return false;
5452 }
5453
5454 // Called by focus event handlers. newFocused is the window that becomes focused.
5455 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5456 {
5457 unsigned int oldFlags = m_iFlags;
5458
5459 m_iFlags &= ~(wxPG_FL_FOCUSED);
5460
5461 wxWindow* parent = newFocused;
5462
5463 // This must be one of nextFocus' parents.
5464 while ( parent )
5465 {
5466 // Use m_eventObject, which is either wxPropertyGrid or
5467 // wxPropertyGridManager, as appropriate.
5468 if ( parent == m_eventObject )
5469 {
5470 m_iFlags |= wxPG_FL_FOCUSED;
5471 break;
5472 }
5473 parent = parent->GetParent();
5474 }
5475
5476 m_curFocused = newFocused;
5477
5478 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5479 (oldFlags & wxPG_FL_FOCUSED) )
5480 {
5481 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5482 {
5483 // Need to store changed value
5484 CommitChangesFromEditor();
5485 }
5486 else
5487 {
5488 /*
5489 //
5490 // Preliminary code for tab-order respecting
5491 // tab-traversal (but should be moved to
5492 // OnNav handler)
5493 //
5494 wxWindow* prevFocus = event.GetWindow();
5495 wxWindow* useThis = this;
5496 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5497 useThis = GetParent();
5498
5499 if ( prevFocus &&
5500 prevFocus->GetParent() == useThis->GetParent() )
5501 {
5502 wxList& children = useThis->GetParent()->GetChildren();
5503
5504 wxNode* node = children.Find(prevFocus);
5505
5506 if ( node->GetNext() &&
5507 useThis == node->GetNext()->GetData() )
5508 DoSelectProperty(GetFirst());
5509 else if ( node->GetPrevious () &&
5510 useThis == node->GetPrevious()->GetData() )
5511 DoSelectProperty(GetLastProperty());
5512
5513 }
5514 */
5515 }
5516
5517 // Redraw selected
5518 wxPGProperty* selected = GetSelection();
5519 if ( selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5520 DrawItem( selected );
5521 }
5522 }
5523
5524 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5525 {
5526 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5527 HandleFocusChange((wxWindow*)event.GetEventObject());
5528 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5529 //else if ( event.GetWindow() )
5530 else
5531 HandleFocusChange(event.GetWindow());
5532
5533 event.Skip();
5534 }
5535
5536 // -----------------------------------------------------------------------
5537
5538 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5539 {
5540 HandleFocusChange((wxWindow*)event.GetEventObject());
5541 event.Skip();
5542 }
5543
5544 // -----------------------------------------------------------------------
5545
5546 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5547 {
5548 m_iFlags |= wxPG_FL_SCROLLED;
5549
5550 event.Skip();
5551 }
5552
5553 // -----------------------------------------------------------------------
5554
5555 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5556 {
5557 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5558 {
5559 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5560 }
5561 }
5562
5563 // -----------------------------------------------------------------------
5564 // Property editor related functions
5565 // -----------------------------------------------------------------------
5566
5567 // noDefCheck = true prevents infinite recursion.
5568 wxPGEditor* wxPropertyGrid::DoRegisterEditorClass( wxPGEditor* editorClass,
5569 const wxString& editorName,
5570 bool noDefCheck )
5571 {
5572 wxASSERT( editorClass );
5573
5574 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5575 RegisterDefaultEditors();
5576
5577 wxString name = editorName;
5578 if ( name.length() == 0 )
5579 name = editorClass->GetName();
5580
5581 // Existing editor under this name?
5582 wxPGHashMapS2P::iterator vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5583
5584 if ( vt_it != wxPGGlobalVars->m_mapEditorClasses.end() )
5585 {
5586 // If this name was already used, try class name.
5587 name = editorClass->GetClassInfo()->GetClassName();
5588 vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5589 }
5590
5591 wxCHECK_MSG( vt_it == wxPGGlobalVars->m_mapEditorClasses.end(),
5592 (wxPGEditor*) vt_it->second,
5593 "Editor with given name was already registered" );
5594
5595 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorClass;
5596
5597 return editorClass;
5598 }
5599
5600 // Use this in RegisterDefaultEditors.
5601 #define wxPGRegisterDefaultEditorClass(EDITOR) \
5602 if ( wxPGEditor_##EDITOR == NULL ) \
5603 { \
5604 wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
5605 new wxPG##EDITOR##Editor, true ); \
5606 }
5607
5608 // Registers all default editor classes
5609 void wxPropertyGrid::RegisterDefaultEditors()
5610 {
5611 wxPGRegisterDefaultEditorClass( TextCtrl );
5612 wxPGRegisterDefaultEditorClass( Choice );
5613 wxPGRegisterDefaultEditorClass( ComboBox );
5614 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
5615 #if wxPG_INCLUDE_CHECKBOX
5616 wxPGRegisterDefaultEditorClass( CheckBox );
5617 #endif
5618 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
5619
5620 // Register SpinCtrl etc. editors before use
5621 RegisterAdditionalEditors();
5622 }
5623
5624 // -----------------------------------------------------------------------
5625 // wxPGStringTokenizer
5626 // Needed to handle C-style string lists (e.g. "str1" "str2")
5627 // -----------------------------------------------------------------------
5628
5629 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
5630 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
5631 {
5632 }
5633
5634 wxPGStringTokenizer::~wxPGStringTokenizer()
5635 {
5636 }
5637
5638 bool wxPGStringTokenizer::HasMoreTokens()
5639 {
5640 const wxString& str = *m_str;
5641
5642 wxString::const_iterator i = m_curPos;
5643
5644 wxUniChar delim = m_delimeter;
5645 wxUniChar a;
5646 wxUniChar prev_a = wxT('\0');
5647
5648 bool inToken = false;
5649
5650 while ( i != str.end() )
5651 {
5652 a = *i;
5653
5654 if ( !inToken )
5655 {
5656 if ( a == delim )
5657 {
5658 inToken = true;
5659 m_readyToken.clear();
5660 }
5661 }
5662 else
5663 {
5664 if ( prev_a != wxT('\\') )
5665 {
5666 if ( a != delim )
5667 {
5668 if ( a != wxT('\\') )
5669 m_readyToken << a;
5670 }
5671 else
5672 {
5673 ++i;
5674 m_curPos = i;
5675 return true;
5676 }
5677 prev_a = a;
5678 }
5679 else
5680 {
5681 m_readyToken << a;
5682 prev_a = wxT('\0');
5683 }
5684 }
5685 ++i;
5686 }
5687
5688 m_curPos = str.end();
5689
5690 if ( inToken )
5691 return true;
5692
5693 return false;
5694 }
5695
5696 wxString wxPGStringTokenizer::GetNextToken()
5697 {
5698 return m_readyToken;
5699 }
5700
5701 // -----------------------------------------------------------------------
5702 // wxPGChoiceEntry
5703 // -----------------------------------------------------------------------
5704
5705 wxPGChoiceEntry::wxPGChoiceEntry()
5706 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
5707 {
5708 }
5709
5710 // -----------------------------------------------------------------------
5711 // wxPGChoicesData
5712 // -----------------------------------------------------------------------
5713
5714 wxPGChoicesData::wxPGChoicesData()
5715 {
5716 }
5717
5718 wxPGChoicesData::~wxPGChoicesData()
5719 {
5720 Clear();
5721 }
5722
5723 void wxPGChoicesData::Clear()
5724 {
5725 m_items.clear();
5726 }
5727
5728 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
5729 {
5730 wxASSERT( m_items.size() == 0 );
5731
5732 m_items = data->m_items;
5733 }
5734
5735 wxPGChoiceEntry& wxPGChoicesData::Insert( int index,
5736 const wxPGChoiceEntry& item )
5737 {
5738 wxVector<wxPGChoiceEntry>::iterator it;
5739 if ( index == -1 )
5740 {
5741 it = m_items.end();
5742 index = (int) m_items.size();
5743 }
5744 else
5745 {
5746 it = m_items.begin() + index;
5747 }
5748
5749 m_items.insert(it, item);
5750
5751 wxPGChoiceEntry& ownEntry = m_items[index];
5752
5753 // Need to fix value?
5754 if ( ownEntry.GetValue() == wxPG_INVALID_VALUE )
5755 ownEntry.SetValue(index);
5756
5757 return ownEntry;
5758 }
5759
5760 // -----------------------------------------------------------------------
5761 // wxPropertyGridEvent
5762 // -----------------------------------------------------------------------
5763
5764 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
5765
5766
5767 wxDEFINE_EVENT( wxEVT_PG_SELECTED, wxPropertyGridEvent );
5768 wxDEFINE_EVENT( wxEVT_PG_CHANGING, wxPropertyGridEvent );
5769 wxDEFINE_EVENT( wxEVT_PG_CHANGED, wxPropertyGridEvent );
5770 wxDEFINE_EVENT( wxEVT_PG_HIGHLIGHTED, wxPropertyGridEvent );
5771 wxDEFINE_EVENT( wxEVT_PG_RIGHT_CLICK, wxPropertyGridEvent );
5772 wxDEFINE_EVENT( wxEVT_PG_PAGE_CHANGED, wxPropertyGridEvent );
5773 wxDEFINE_EVENT( wxEVT_PG_ITEM_EXPANDED, wxPropertyGridEvent );
5774 wxDEFINE_EVENT( wxEVT_PG_ITEM_COLLAPSED, wxPropertyGridEvent );
5775 wxDEFINE_EVENT( wxEVT_PG_DOUBLE_CLICK, wxPropertyGridEvent );
5776 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_BEGIN, wxPropertyGridEvent );
5777 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_ENDING, wxPropertyGridEvent );
5778
5779 // -----------------------------------------------------------------------
5780
5781 void wxPropertyGridEvent::Init()
5782 {
5783 m_validationInfo = NULL;
5784 m_column = 1;
5785 m_canVeto = false;
5786 m_wasVetoed = false;
5787 }
5788
5789 // -----------------------------------------------------------------------
5790
5791 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
5792 : wxCommandEvent(commandType,id)
5793 {
5794 m_property = NULL;
5795 Init();
5796 }
5797
5798 // -----------------------------------------------------------------------
5799
5800 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
5801 : wxCommandEvent(event)
5802 {
5803 m_eventType = event.GetEventType();
5804 m_eventObject = event.m_eventObject;
5805 m_pg = event.m_pg;
5806 OnPropertyGridSet();
5807 m_property = event.m_property;
5808 m_validationInfo = event.m_validationInfo;
5809 m_canVeto = event.m_canVeto;
5810 m_wasVetoed = event.m_wasVetoed;
5811 }
5812
5813 // -----------------------------------------------------------------------
5814
5815 void wxPropertyGridEvent::OnPropertyGridSet()
5816 {
5817 if ( !m_pg )
5818 return;
5819
5820 #if wxUSE_THREADS
5821 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
5822 #endif
5823 m_pg->m_liveEvents.push_back(this);
5824 }
5825
5826 // -----------------------------------------------------------------------
5827
5828 wxPropertyGridEvent::~wxPropertyGridEvent()
5829 {
5830 if ( m_pg )
5831 {
5832 #if wxUSE_THREADS
5833 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
5834 #endif
5835
5836 // Use iterate from the back since it is more likely that the event
5837 // being desroyed is at the end of the array.
5838 wxVector<wxPropertyGridEvent*>& liveEvents = m_pg->m_liveEvents;
5839
5840 for ( int i = liveEvents.size()-1; i >= 0; i-- )
5841 {
5842 if ( liveEvents[i] == this )
5843 {
5844 liveEvents.erase(liveEvents.begin() + i);
5845 break;
5846 }
5847 }
5848 }
5849 }
5850
5851 // -----------------------------------------------------------------------
5852
5853 wxEvent* wxPropertyGridEvent::Clone() const
5854 {
5855 return new wxPropertyGridEvent( *this );
5856 }
5857
5858 // -----------------------------------------------------------------------
5859 // wxPropertyGridPopulator
5860 // -----------------------------------------------------------------------
5861
5862 wxPropertyGridPopulator::wxPropertyGridPopulator()
5863 {
5864 m_state = NULL;
5865 m_pg = NULL;
5866 wxPGGlobalVars->m_offline++;
5867 }
5868
5869 // -----------------------------------------------------------------------
5870
5871 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
5872 {
5873 m_state = state;
5874 m_propHierarchy.clear();
5875 }
5876
5877 // -----------------------------------------------------------------------
5878
5879 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
5880 {
5881 m_pg = pg;
5882 pg->Freeze();
5883 }
5884
5885 // -----------------------------------------------------------------------
5886
5887 wxPropertyGridPopulator::~wxPropertyGridPopulator()
5888 {
5889 //
5890 // Free unused sets of choices
5891 wxPGHashMapS2P::iterator it;
5892
5893 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
5894 {
5895 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
5896 data->DecRef();
5897 }
5898
5899 if ( m_pg )
5900 {
5901 m_pg->Thaw();
5902 m_pg->GetPanel()->Refresh();
5903 }
5904 wxPGGlobalVars->m_offline--;
5905 }
5906
5907 // -----------------------------------------------------------------------
5908
5909 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
5910 const wxString& propLabel,
5911 const wxString& propName,
5912 const wxString* propValue,
5913 wxPGChoices* pChoices )
5914 {
5915 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
5916 wxPGProperty* parent = GetCurParent();
5917
5918 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
5919 {
5920 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
5921 return NULL;
5922 }
5923
5924 if ( !classInfo || !classInfo->IsKindOf(CLASSINFO(wxPGProperty)) )
5925 {
5926 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
5927 return NULL;
5928 }
5929
5930 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
5931
5932 property->SetLabel(propLabel);
5933 property->DoSetName(propName);
5934
5935 if ( pChoices && pChoices->IsOk() )
5936 property->SetChoices(*pChoices);
5937
5938 m_state->DoInsert(parent, -1, property);
5939
5940 if ( propValue )
5941 property->SetValueFromString( *propValue, wxPG_FULL_VALUE|
5942 wxPG_PROGRAMMATIC_VALUE );
5943
5944 return property;
5945 }
5946
5947 // -----------------------------------------------------------------------
5948
5949 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
5950 {
5951 m_propHierarchy.push_back(property);
5952 DoScanForChildren();
5953 m_propHierarchy.pop_back();
5954 }
5955
5956 // -----------------------------------------------------------------------
5957
5958 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
5959 const wxString& idString )
5960 {
5961 wxPGChoices choices;
5962
5963 // Using id?
5964 if ( choicesString[0] == wxT('@') )
5965 {
5966 wxString ids = choicesString.substr(1);
5967 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
5968 if ( it == m_dictIdChoices.end() )
5969 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
5970 else
5971 choices.AssignData((wxPGChoicesData*)it->second);
5972 }
5973 else
5974 {
5975 bool found = false;
5976 if ( idString.length() )
5977 {
5978 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
5979 if ( it != m_dictIdChoices.end() )
5980 {
5981 choices.AssignData((wxPGChoicesData*)it->second);
5982 found = true;
5983 }
5984 }
5985
5986 if ( !found )
5987 {
5988 // Parse choices string
5989 wxString::const_iterator it = choicesString.begin();
5990 wxString label;
5991 wxString value;
5992 int state = 0;
5993 bool labelValid = false;
5994
5995 for ( ; it != choicesString.end(); ++it )
5996 {
5997 wxChar c = *it;
5998
5999 if ( state != 1 )
6000 {
6001 if ( c == wxT('"') )
6002 {
6003 if ( labelValid )
6004 {
6005 long l;
6006 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6007 choices.Add(label, l);
6008 }
6009 labelValid = false;
6010 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
6011 value.clear();
6012 label.clear();
6013 state = 1;
6014 }
6015 else if ( c == wxT('=') )
6016 {
6017 if ( labelValid )
6018 {
6019 state = 2;
6020 }
6021 }
6022 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
6023 {
6024 value << c;
6025 }
6026 }
6027 else
6028 {
6029 if ( c == wxT('"') )
6030 {
6031 state = 0;
6032 labelValid = true;
6033 }
6034 else
6035 label << c;
6036 }
6037 }
6038
6039 if ( labelValid )
6040 {
6041 long l;
6042 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6043 choices.Add(label, l);
6044 }
6045
6046 if ( !choices.IsOk() )
6047 {
6048 choices.EnsureData();
6049 }
6050
6051 // Assign to id
6052 if ( idString.length() )
6053 m_dictIdChoices[idString] = choices.GetData();
6054 }
6055 }
6056
6057 return choices;
6058 }
6059
6060 // -----------------------------------------------------------------------
6061
6062 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
6063 {
6064 if ( s.Last() == wxT('%') )
6065 {
6066 wxString s2 = s.substr(0,s.length()-1);
6067 long val;
6068 if ( s2.ToLong(&val, 10) )
6069 {
6070 *pval = (val*max)/100;
6071 return true;
6072 }
6073 return false;
6074 }
6075
6076 return s.ToLong(pval, 10);
6077 }
6078
6079 // -----------------------------------------------------------------------
6080
6081 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
6082 const wxString& type,
6083 const wxString& value )
6084 {
6085 int l = m_propHierarchy.size();
6086 if ( !l )
6087 return false;
6088
6089 wxPGProperty* p = m_propHierarchy[l-1];
6090 wxString valuel = value.Lower();
6091 wxVariant variant;
6092
6093 if ( type.length() == 0 )
6094 {
6095 long v;
6096
6097 // Auto-detect type
6098 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6099 variant = true;
6100 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6101 variant = false;
6102 else if ( value.ToLong(&v, 0) )
6103 variant = v;
6104 else
6105 variant = value;
6106 }
6107 else
6108 {
6109 if ( type == wxT("string") )
6110 {
6111 variant = value;
6112 }
6113 else if ( type == wxT("int") )
6114 {
6115 long v = 0;
6116 value.ToLong(&v, 0);
6117 variant = v;
6118 }
6119 else if ( type == wxT("bool") )
6120 {
6121 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6122 variant = true;
6123 else
6124 variant = false;
6125 }
6126 else
6127 {
6128 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6129 return false;
6130 }
6131 }
6132
6133 p->SetAttribute( name, variant );
6134
6135 return true;
6136 }
6137
6138 // -----------------------------------------------------------------------
6139
6140 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6141 {
6142 wxLogError(_("Error in resource: %s"),msg.c_str());
6143 }
6144
6145 // -----------------------------------------------------------------------
6146
6147 #endif // wxUSE_PROPGRID