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