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