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