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