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