]> git.saurik.com Git - wxWidgets.git/blob - src/propgrid/propgrid.cpp
Revert "Fix off by 1 error in wxGenericListCtrl::HitTest()."
[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 }
4561
4562 m_width = width;
4563 m_height = height;
4564
4565 m_pState->CheckColumnWidths();
4566
4567 if ( GetSelection() )
4568 CorrectEditorWidgetSizeX();
4569
4570 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4571 }
4572
4573 // -----------------------------------------------------------------------
4574
4575 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4576 {
4577 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4578 return;
4579
4580 int width, height;
4581 GetClientSize(&width, &height);
4582
4583 m_width = width;
4584 m_height = height;
4585
4586 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4587 {
4588 int dblh = (m_lineHeight*2);
4589 if ( !m_doubleBuffer )
4590 {
4591 // Create double buffer bitmap to draw on, if none
4592 int w = (width>250)?width:250;
4593 int h = height + dblh;
4594 h = (h>400)?h:400;
4595 m_doubleBuffer = new wxBitmap( w, h );
4596 }
4597 else
4598 {
4599 int w = m_doubleBuffer->GetWidth();
4600 int h = m_doubleBuffer->GetHeight();
4601
4602 // Double buffer must be large enough
4603 if ( w < width || h < (height+dblh) )
4604 {
4605 if ( w < width ) w = width;
4606 if ( h < (height+dblh) ) h = height + dblh;
4607 delete m_doubleBuffer;
4608 m_doubleBuffer = new wxBitmap( w, h );
4609 }
4610 }
4611 }
4612
4613 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4614 m_ncWidth = event.GetSize().x;
4615
4616 if ( !m_frozen )
4617 {
4618 if ( m_pState->m_itemsAdded )
4619 PrepareAfterItemsAdded();
4620 else
4621 // Without this, virtual size (atleast under wxGTK) will be skewed
4622 RecalculateVirtualSize();
4623
4624 Refresh();
4625 }
4626 }
4627
4628 // -----------------------------------------------------------------------
4629
4630 void wxPropertyGrid::SetVirtualWidth( int width )
4631 {
4632 if ( width == -1 )
4633 {
4634 // Disable virtual width
4635 width = GetClientSize().x;
4636 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4637 }
4638 else
4639 {
4640 // Enable virtual width
4641 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4642 }
4643 m_pState->SetVirtualWidth( width );
4644 }
4645
4646 void wxPropertyGrid::SetFocusOnCanvas()
4647 {
4648 // To prevent wxPropertyGrid from stealing focus from other controls,
4649 // only move focus to the grid if it was already in one if its child
4650 // controls.
4651 wxWindow* focus = wxWindow::FindFocus();
4652 if ( focus )
4653 {
4654 wxWindow* parent = focus->GetParent();
4655 while ( parent )
4656 {
4657 if ( parent == this )
4658 {
4659 SetFocus();
4660 break;
4661 }
4662 parent = parent->GetParent();
4663 }
4664 }
4665
4666 m_editorFocused = 0;
4667 }
4668
4669 // -----------------------------------------------------------------------
4670 // wxPropertyGrid mouse event handling
4671 // -----------------------------------------------------------------------
4672
4673 // selFlags uses same values DoSelectProperty's flags
4674 // Returns true if event was vetoed.
4675 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p,
4676 wxVariant* pValue,
4677 unsigned int selFlags,
4678 unsigned int column )
4679 {
4680 // selFlags should have wxPG_SEL_NOVALIDATE if event is not
4681 // vetoable.
4682
4683 // Send property grid event of specific type and with specific property
4684 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4685 evt.SetPropertyGrid(this);
4686 evt.SetEventObject(m_eventObject);
4687 evt.SetProperty(p);
4688 evt.SetColumn(column);
4689 if ( eventType == wxEVT_PG_CHANGING )
4690 {
4691 wxASSERT( pValue );
4692 evt.SetCanVeto(true);
4693 m_validationInfo.m_pValue = pValue;
4694 evt.SetupValidationInfo();
4695 }
4696 else
4697 {
4698 if ( p )
4699 evt.SetPropertyValue(p->GetValue());
4700
4701 if ( !(selFlags & wxPG_SEL_NOVALIDATE) )
4702 evt.SetCanVeto(true);
4703 }
4704
4705 wxPropertyGridEvent* prevProcessedEvent = m_processedEvent;
4706 m_processedEvent = &evt;
4707 m_eventObject->HandleWindowEvent(evt);
4708 m_processedEvent = prevProcessedEvent;
4709
4710 return evt.WasVetoed();
4711 }
4712
4713 // -----------------------------------------------------------------------
4714
4715 // Return false if should be skipped
4716 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4717 {
4718 bool res = true;
4719
4720 // Need to set focus?
4721 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4722 {
4723 SetFocusOnCanvas();
4724 }
4725
4726 wxPropertyGridPageState* state = m_pState;
4727 int splitterHit;
4728 int splitterHitOffset;
4729 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4730
4731 wxPGProperty* p = DoGetItemAtY(y);
4732
4733 if ( p )
4734 {
4735 int depth = (int)p->GetDepth() - 1;
4736
4737 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4738
4739 if ( x >= marginEnds )
4740 {
4741 // Outside margin.
4742
4743 if ( p->IsCategory() )
4744 {
4745 // This is category.
4746 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4747
4748 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4749
4750 // Expand, collapse, activate etc. if click on text or left of splitter.
4751 if ( x >= textX
4752 &&
4753 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4754 columnHit == 0
4755 )
4756 )
4757 {
4758 if ( !AddToSelectionFromInputEvent( p,
4759 columnHit,
4760 &event ) )
4761 return res;
4762
4763 // On double-click, expand/collapse.
4764 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4765 {
4766 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4767 else DoExpand( p, true );
4768 }
4769 }
4770 }
4771 else if ( splitterHit == -1 )
4772 {
4773 // Click on value.
4774 unsigned int selFlag = 0;
4775 if ( columnHit == 1 )
4776 {
4777 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4778 selFlag = wxPG_SEL_FOCUS;
4779 }
4780 if ( !AddToSelectionFromInputEvent( p,
4781 columnHit,
4782 &event,
4783 selFlag ) )
4784 return res;
4785
4786 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4787
4788 if ( p->GetChildCount() && !p->IsCategory() )
4789 // On double-click, expand/collapse.
4790 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4791 {
4792 wxPGProperty* pwc = (wxPGProperty*)p;
4793 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4794 else DoExpand( p, true );
4795 }
4796
4797 // Do not Skip() the event after selection has been made.
4798 // Otherwise default event handling behaviour kicks in
4799 // and may revert focus back to the main canvas.
4800 res = true;
4801 }
4802 else
4803 {
4804 // click on splitter
4805 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4806 {
4807 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4808 {
4809 // Double-clicking the splitter causes auto-centering
4810 if ( m_pState->GetColumnCount() <= 2 )
4811 {
4812 ResetColumnSizes( true );
4813
4814 SendEvent(wxEVT_PG_COL_DRAGGING,
4815 m_propHover,
4816 NULL,
4817 wxPG_SEL_NOVALIDATE,
4818 (unsigned int)m_draggedSplitter);
4819 }
4820 }
4821 else if ( m_dragStatus == 0 )
4822 {
4823 //
4824 // Begin draggin the splitter
4825 //
4826
4827 // send event
4828 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
4829
4830 // Allow application to veto dragging
4831 if ( !SendEvent(wxEVT_PG_COL_BEGIN_DRAG,
4832 p, NULL, 0,
4833 (unsigned int)splitterHit) )
4834 {
4835 if ( m_wndEditor )
4836 {
4837 // Changes must be committed here or the
4838 // value won't be drawn correctly
4839 if ( !CommitChangesFromEditor() )
4840 return res;
4841
4842 m_wndEditor->Show ( false );
4843 }
4844
4845 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4846 {
4847 CaptureMouse();
4848 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4849 }
4850
4851 m_dragStatus = 1;
4852 m_draggedSplitter = splitterHit;
4853 m_dragOffset = splitterHitOffset;
4854
4855 #if wxPG_REFRESH_CONTROLS
4856 // Fixes button disappearance bug
4857 if ( m_wndEditor2 )
4858 m_wndEditor2->Show ( false );
4859 #endif
4860
4861 m_startingSplitterX = x - splitterHitOffset;
4862 }
4863 }
4864 }
4865 }
4866 }
4867 else
4868 {
4869 // Click on margin.
4870 if ( p->GetChildCount() )
4871 {
4872 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4873
4874 // Fine tune cell button x
4875 if ( !p->IsCategory() )
4876 nx -= IN_CELL_EXPANDER_BUTTON_X_ADJUST;
4877
4878 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4879 {
4880 int y2 = y % m_lineHeight;
4881 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4882 {
4883 // On click on expander button, expand/collapse
4884 if ( ((wxPGProperty*)p)->IsExpanded() )
4885 DoCollapse( p, true );
4886 else
4887 DoExpand( p, true );
4888 }
4889 }
4890 }
4891 }
4892 }
4893 return res;
4894 }
4895
4896 // -----------------------------------------------------------------------
4897
4898 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x),
4899 unsigned int WXUNUSED(y),
4900 wxMouseEvent& event )
4901 {
4902 if ( m_propHover )
4903 {
4904 // Select property here as well
4905 wxPGProperty* p = m_propHover;
4906 AddToSelectionFromInputEvent(p, m_colHover, &event);
4907
4908 // Send right click event.
4909 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4910
4911 return true;
4912 }
4913 return false;
4914 }
4915
4916 // -----------------------------------------------------------------------
4917
4918 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x),
4919 unsigned int WXUNUSED(y),
4920 wxMouseEvent& event )
4921 {
4922 if ( m_propHover )
4923 {
4924 // Select property here as well
4925 wxPGProperty* p = m_propHover;
4926
4927 AddToSelectionFromInputEvent(p, m_colHover, &event);
4928
4929 // Send double-click event.
4930 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4931
4932 return true;
4933 }
4934 return false;
4935 }
4936
4937 // -----------------------------------------------------------------------
4938
4939 // Return false if should be skipped
4940 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y,
4941 wxMouseEvent &event )
4942 {
4943 // Safety check (needed because mouse capturing may
4944 // otherwise freeze the control)
4945 if ( m_dragStatus > 0 && !event.Dragging() )
4946 {
4947 HandleMouseUp(x, y, event);
4948 }
4949
4950 wxPropertyGridPageState* state = m_pState;
4951 int splitterHit;
4952 int splitterHitOffset;
4953 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4954 int splitterX = x - splitterHitOffset;
4955
4956 m_colHover = columnHit;
4957
4958 if ( m_dragStatus > 0 )
4959 {
4960 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4961 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4962 {
4963
4964 int newSplitterX = x - m_dragOffset;
4965
4966 // Splitter redraw required?
4967 if ( newSplitterX != splitterX )
4968 {
4969 // Move everything
4970 DoSetSplitterPosition(newSplitterX,
4971 m_draggedSplitter,
4972 wxPG_SPLITTER_REFRESH |
4973 wxPG_SPLITTER_FROM_EVENT);
4974
4975 SendEvent(wxEVT_PG_COL_DRAGGING,
4976 m_propHover,
4977 NULL,
4978 wxPG_SEL_NOVALIDATE,
4979 (unsigned int)m_draggedSplitter);
4980 }
4981
4982 m_dragStatus = 2;
4983 }
4984
4985 return false;
4986 }
4987 else
4988 {
4989
4990 int ih = m_lineHeight;
4991 int sy = y;
4992
4993 #if wxPG_SUPPORT_TOOLTIPS
4994 wxPGProperty* prevHover = m_propHover;
4995 unsigned char prevSide = m_mouseSide;
4996 #endif
4997 int curPropHoverY = y - (y % ih);
4998
4999 // On which item it hovers
5000 if ( !m_propHover
5001 ||
5002 ( sy < m_propHoverY || sy >= (m_propHoverY+ih) )
5003 )
5004 {
5005 // Mouse moves on another property
5006
5007 m_propHover = DoGetItemAtY(y);
5008 m_propHoverY = curPropHoverY;
5009
5010 // Send hover event
5011 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
5012 }
5013
5014 #if wxPG_SUPPORT_TOOLTIPS
5015 // Store which side we are on
5016 m_mouseSide = 0;
5017 if ( columnHit == 1 )
5018 m_mouseSide = 2;
5019 else if ( columnHit == 0 )
5020 m_mouseSide = 1;
5021
5022 //
5023 // If tooltips are enabled, show label or value as a tip
5024 // in case it doesn't otherwise show in full length.
5025 //
5026 if ( m_windowStyle & wxPG_TOOLTIPS )
5027 {
5028 if ( m_propHover != prevHover || prevSide != m_mouseSide )
5029 {
5030 if ( m_propHover && !m_propHover->IsCategory() )
5031 {
5032
5033 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
5034 {
5035 // Show help string as a tooltip
5036 wxString tipString = m_propHover->GetHelpString();
5037
5038 SetToolTip(tipString);
5039 }
5040 else
5041 {
5042 // Show cropped value string as a tooltip
5043 wxString tipString;
5044 int space = 0;
5045
5046 if ( m_mouseSide == 1 )
5047 {
5048 tipString = m_propHover->m_label;
5049 space = splitterX-m_marginWidth-3;
5050 }
5051 else if ( m_mouseSide == 2 )
5052 {
5053 tipString = m_propHover->GetDisplayedString();
5054
5055 space = m_width - splitterX;
5056 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
5057 space -= wxPG_CUSTOM_IMAGE_WIDTH +
5058 wxCC_CUSTOM_IMAGE_MARGIN1 +
5059 wxCC_CUSTOM_IMAGE_MARGIN2;
5060 }
5061
5062 if ( space )
5063 {
5064 int tw, th;
5065 GetTextExtent( tipString, &tw, &th, 0, 0 );
5066 if ( tw > space )
5067 SetToolTip( tipString );
5068 }
5069 else
5070 {
5071 SetToolTip( m_emptyString );
5072 }
5073
5074 }
5075 }
5076 else
5077 {
5078 SetToolTip( m_emptyString );
5079 }
5080 }
5081 }
5082 #endif
5083
5084 if ( splitterHit == -1 ||
5085 !m_propHover ||
5086 HasFlag(wxPG_STATIC_SPLITTER) )
5087 {
5088 // hovering on something else
5089 if ( m_curcursor != wxCURSOR_ARROW )
5090 CustomSetCursor( wxCURSOR_ARROW );
5091 }
5092 else
5093 {
5094 // Do not allow splitter cursor on caption items.
5095 // (also not if we were dragging and its started
5096 // outside the splitter region)
5097
5098 if ( !m_propHover->IsCategory() &&
5099 !event.Dragging() )
5100 {
5101
5102 // hovering on splitter
5103
5104 // NB: Condition disabled since MouseLeave event (from the
5105 // editor control) cannot be reliably detected.
5106 //if ( m_curcursor != wxCURSOR_SIZEWE )
5107 CustomSetCursor( wxCURSOR_SIZEWE, true );
5108
5109 return false;
5110 }
5111 else
5112 {
5113 // hovering on something else
5114 if ( m_curcursor != wxCURSOR_ARROW )
5115 CustomSetCursor( wxCURSOR_ARROW );
5116 }
5117 }
5118
5119 //
5120 // Multi select by dragging
5121 //
5122 if ( (GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION) &&
5123 event.LeftIsDown() &&
5124 m_propHover &&
5125 GetSelection() &&
5126 columnHit != 1 &&
5127 !state->DoIsPropertySelected(m_propHover) )
5128 {
5129 // Additional requirement is that the hovered property
5130 // is adjacent to edges of selection.
5131 const wxArrayPGProperty& selection = GetSelectedProperties();
5132
5133 // Since categories cannot be selected along with 'other'
5134 // properties, exclude them from iterator flags.
5135 int iterFlags = wxPG_ITERATE_VISIBLE & (~wxPG_PROP_CATEGORY);
5136
5137 for ( int i=(selection.size()-1); i>=0; i-- )
5138 {
5139 // TODO: This could be optimized by keeping track of
5140 // which properties are at the edges of selection.
5141 wxPGProperty* selProp = selection[i];
5142 if ( state->ArePropertiesAdjacent(m_propHover, selProp,
5143 iterFlags) )
5144 {
5145 DoAddToSelection(m_propHover);
5146 break;
5147 }
5148 }
5149 }
5150 }
5151 return true;
5152 }
5153
5154 // -----------------------------------------------------------------------
5155
5156 // Also handles Leaving event
5157 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
5158 wxMouseEvent &WXUNUSED(event) )
5159 {
5160 wxPropertyGridPageState* state = m_pState;
5161 bool res = false;
5162
5163 int splitterHit;
5164 int splitterHitOffset;
5165 state->HitTestH( x, &splitterHit, &splitterHitOffset );
5166
5167 // No event type check - basically calling this method should
5168 // just stop dragging.
5169 // Left up after dragged?
5170 if ( m_dragStatus >= 1 )
5171 {
5172 //
5173 // End Splitter Dragging
5174 //
5175 // DO NOT ENABLE FOLLOWING LINE!
5176 // (it is only here as a reminder to not to do it)
5177 //splitterX = x;
5178
5179 SendEvent(wxEVT_PG_COL_END_DRAG,
5180 m_propHover,
5181 NULL,
5182 wxPG_SEL_NOVALIDATE,
5183 (unsigned int)m_draggedSplitter);
5184
5185 // Disable splitter auto-centering (but only if moved any -
5186 // otherwise we end up disabling auto-center even after a
5187 // recentering double-click).
5188 int posDiff = abs(m_startingSplitterX -
5189 GetSplitterPosition(m_draggedSplitter));
5190
5191 if ( posDiff > 1 )
5192 state->m_dontCenterSplitter = true;
5193
5194 // This is necessary to return cursor
5195 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5196 {
5197 ReleaseMouse();
5198 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5199 }
5200
5201 // Set back the default cursor, if necessary
5202 if ( splitterHit == -1 ||
5203 !m_propHover )
5204 {
5205 CustomSetCursor( wxCURSOR_ARROW );
5206 }
5207
5208 m_dragStatus = 0;
5209
5210 // Control background needs to be cleared
5211 wxPGProperty* selected = GetSelection();
5212 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && selected )
5213 DrawItem( selected );
5214
5215 if ( m_wndEditor )
5216 {
5217 m_wndEditor->Show ( true );
5218 }
5219
5220 #if wxPG_REFRESH_CONTROLS
5221 // Fixes button disappearance bug
5222 if ( m_wndEditor2 )
5223 m_wndEditor2->Show ( true );
5224 #endif
5225
5226 // This clears the focus.
5227 m_editorFocused = 0;
5228
5229 }
5230 return res;
5231 }
5232
5233 // -----------------------------------------------------------------------
5234
5235 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
5236 {
5237 int splitterX = GetSplitterPosition();
5238
5239 int ux, uy;
5240 CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
5241
5242 wxWindow* wnd = GetEditorControl();
5243
5244 // Hide popup on clicks
5245 if ( event.GetEventType() != wxEVT_MOTION )
5246 if ( wxDynamicCast(wnd, wxOwnerDrawnComboBox) )
5247 {
5248 ((wxOwnerDrawnComboBox*)wnd)->HidePopup();
5249 }
5250
5251 wxRect r;
5252 if ( wnd )
5253 r = wnd->GetRect();
5254 if ( wnd == NULL || m_dragStatus ||
5255 (
5256 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
5257 ux >= (r.x+r.width) ||
5258 event.m_y < r.y ||
5259 event.m_y >= (r.y+r.height)
5260 )
5261 )
5262 {
5263 *px = ux;
5264 *py = uy;
5265 return true;
5266 }
5267 else
5268 {
5269 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5270 }
5271 return false;
5272 }
5273
5274 // -----------------------------------------------------------------------
5275
5276 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
5277 {
5278 int x, y;
5279 if ( OnMouseCommon( event, &x, &y ) )
5280 {
5281 if ( !HandleMouseClick(x, y, event) )
5282 event.Skip();
5283 }
5284 else
5285 {
5286 event.Skip();
5287 }
5288 }
5289
5290 // -----------------------------------------------------------------------
5291
5292 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
5293 {
5294 int x, y;
5295 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5296 HandleMouseRightClick(x,y,event);
5297 event.Skip();
5298 }
5299
5300 // -----------------------------------------------------------------------
5301
5302 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
5303 {
5304 // Always run standard mouse-down handler as well
5305 OnMouseClick(event);
5306
5307 int x, y;
5308 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5309 HandleMouseDoubleClick(x,y,event);
5310
5311 // Do not Skip() event here - OnMouseClick() call above
5312 // should have already taken care of it.
5313 }
5314
5315 // -----------------------------------------------------------------------
5316
5317 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
5318 {
5319 int x, y;
5320 if ( OnMouseCommon( event, &x, &y ) )
5321 {
5322 HandleMouseMove(x,y,event);
5323 }
5324 event.Skip();
5325 }
5326
5327 // -----------------------------------------------------------------------
5328
5329 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
5330 {
5331 int x, y;
5332 if ( OnMouseCommon( event, &x, &y ) )
5333 {
5334 if ( !HandleMouseUp(x, y, event) )
5335 event.Skip();
5336 }
5337 else
5338 {
5339 event.Skip();
5340 }
5341 }
5342
5343 // -----------------------------------------------------------------------
5344
5345 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
5346 {
5347 // This may get called from child control as well, so event's
5348 // mouse position cannot be relied on.
5349
5350 if ( event.Entering() )
5351 {
5352 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5353 {
5354 // TODO: Fix this (detect parent and only do
5355 // cursor trick if it is a manager).
5356 wxASSERT( GetParent() );
5357 GetParent()->SetCursor(wxNullCursor);
5358
5359 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
5360 }
5361 else
5362 GetParent()->SetCursor(wxNullCursor);
5363 }
5364 else if ( event.Leaving() )
5365 {
5366 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
5367 SetCursor( wxNullCursor );
5368
5369 // Get real cursor position
5370 wxPoint pt = ScreenToClient(::wxGetMousePosition());
5371
5372 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
5373 {
5374 {
5375 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5376 {
5377 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
5378 }
5379
5380 if ( m_dragStatus )
5381 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
5382 }
5383 }
5384 }
5385
5386 event.Skip();
5387 }
5388
5389 // -----------------------------------------------------------------------
5390
5391 // Common code used by various OnMouseXXXChild methods.
5392 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
5393 {
5394 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
5395 wxASSERT( topCtrlWnd );
5396 int x, y;
5397 event.GetPosition(&x,&y);
5398
5399 int splitterX = GetSplitterPosition();
5400
5401 wxRect r = topCtrlWnd->GetRect();
5402 if ( !m_dragStatus &&
5403 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
5404 y >= 0 && y < r.height \
5405 )
5406 {
5407 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5408 event.Skip();
5409 }
5410 else
5411 {
5412 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
5413 px, py );
5414 return true;
5415 }
5416 return false;
5417 }
5418
5419 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
5420 {
5421 int x,y;
5422 if ( OnMouseChildCommon(event,&x,&y) )
5423 {
5424 bool res = HandleMouseClick(x,y,event);
5425 if ( !res ) event.Skip();
5426 }
5427 }
5428
5429 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
5430 {
5431 int x,y;
5432 wxASSERT( m_wndEditor );
5433 // These coords may not be exact (about +-2),
5434 // but that should not matter (right click is about item, not position).
5435 wxPoint pt = m_wndEditor->GetPosition();
5436 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
5437
5438 // FIXME: Used to set m_propHover to selection here. Was it really
5439 // necessary?
5440
5441 bool res = HandleMouseRightClick(x,y,event);
5442 if ( !res ) event.Skip();
5443 }
5444
5445 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
5446 {
5447 int x,y;
5448 if ( OnMouseChildCommon(event,&x,&y) )
5449 {
5450 bool res = HandleMouseMove(x,y,event);
5451 if ( !res ) event.Skip();
5452 }
5453 }
5454
5455 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
5456 {
5457 int x,y;
5458 if ( OnMouseChildCommon(event,&x,&y) )
5459 {
5460 bool res = HandleMouseUp(x,y,event);
5461 if ( !res ) event.Skip();
5462 }
5463 }
5464
5465 // -----------------------------------------------------------------------
5466 // wxPropertyGrid keyboard event handling
5467 // -----------------------------------------------------------------------
5468
5469 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
5470 {
5471 // Translates wxKeyEvent to wxPG_ACTION_XXX
5472
5473 int keycode = event.GetKeyCode();
5474 int modifiers = event.GetModifiers();
5475
5476 wxASSERT( !(modifiers&~(0xFFFF)) );
5477
5478 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5479
5480 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
5481
5482 if ( it == m_actionTriggers.end() )
5483 return 0;
5484
5485 if ( pSecond )
5486 {
5487 int second = (it->second>>16) & 0xFFFF;
5488 *pSecond = second;
5489 }
5490
5491 return (it->second & 0xFFFF);
5492 }
5493
5494 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
5495 {
5496 wxASSERT( !(modifiers&~(0xFFFF)) );
5497
5498 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5499
5500 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
5501
5502 if ( it != m_actionTriggers.end() )
5503 {
5504 // This key combination is already used
5505
5506 // Can add secondary?
5507 wxASSERT_MSG( !(it->second&~(0xFFFF)),
5508 wxT("You can only add up to two separate actions per key combination.") );
5509
5510 action = it->second | (action<<16);
5511 }
5512
5513 m_actionTriggers[hashMapKey] = action;
5514 }
5515
5516 void wxPropertyGrid::ClearActionTriggers( int action )
5517 {
5518 wxPGHashMapI2I::iterator it;
5519 bool didSomething;
5520
5521 do
5522 {
5523 didSomething = false;
5524
5525 for ( it = m_actionTriggers.begin();
5526 it != m_actionTriggers.end();
5527 it++ )
5528 {
5529 if ( it->second == action )
5530 {
5531 m_actionTriggers.erase(it);
5532 didSomething = true;
5533 break;
5534 }
5535 }
5536 }
5537 while ( didSomething );
5538 }
5539
5540 void wxPropertyGrid::HandleKeyEvent( wxKeyEvent &event, bool fromChild )
5541 {
5542 //
5543 // Handles key event when editor control is not focused.
5544 //
5545
5546 wxCHECK2(!m_frozen, return);
5547
5548 // Travelsal between items, collapsing/expanding, etc.
5549 wxPGProperty* selected = GetSelection();
5550 int keycode = event.GetKeyCode();
5551 bool editorFocused = IsEditorFocused();
5552
5553 if ( keycode == WXK_TAB )
5554 {
5555 #if defined(__WXGTK__)
5556 wxWindow* mainControl;
5557
5558 if ( HasInternalFlag(wxPG_FL_IN_MANAGER) )
5559 mainControl = GetParent();
5560 else
5561 mainControl = this;
5562 #endif
5563
5564 if ( !event.ShiftDown() )
5565 {
5566 if ( !editorFocused && m_wndEditor )
5567 {
5568 DoSelectProperty( selected, wxPG_SEL_FOCUS );
5569 }
5570 else
5571 {
5572 // Tab traversal workaround for platforms on which
5573 // wxWindow::Navigate() may navigate into first child
5574 // instead of next sibling. Does not work perfectly
5575 // in every scenario (for instance, when property grid
5576 // is either first or last control).
5577 #if defined(__WXGTK__)
5578 wxWindow* sibling = mainControl->GetNextSibling();
5579 if ( sibling )
5580 sibling->SetFocusFromKbd();
5581 #else
5582 Navigate(wxNavigationKeyEvent::IsForward);
5583 #endif
5584 }
5585 }
5586 else
5587 {
5588 if ( editorFocused )
5589 {
5590 UnfocusEditor();
5591 }
5592 else
5593 {
5594 #if defined(__WXGTK__)
5595 wxWindow* sibling = mainControl->GetPrevSibling();
5596 if ( sibling )
5597 sibling->SetFocusFromKbd();
5598 #else
5599 Navigate(wxNavigationKeyEvent::IsBackward);
5600 #endif
5601 }
5602 }
5603
5604 return;
5605 }
5606
5607 // Ignore Alt and Control when they are down alone
5608 if ( keycode == WXK_ALT ||
5609 keycode == WXK_CONTROL )
5610 {
5611 event.Skip();
5612 return;
5613 }
5614
5615 int secondAction;
5616 int action = KeyEventToActions(event, &secondAction);
5617
5618 if ( editorFocused && action == wxPG_ACTION_CANCEL_EDIT )
5619 {
5620 //
5621 // Esc cancels any changes
5622 if ( IsEditorsValueModified() )
5623 {
5624 EditorsValueWasNotModified();
5625
5626 // Update the control as well
5627 selected->GetEditorClass()->
5628 SetControlStringValue( selected,
5629 GetEditorControl(),
5630 selected->GetDisplayedString() );
5631 }
5632
5633 OnValidationFailureReset(selected);
5634
5635 UnfocusEditor();
5636 return;
5637 }
5638
5639 // Except for TAB, ESC, and any keys specifically dedicated to
5640 // wxPropertyGrid itself, handle child control events in child control.
5641 if ( fromChild &&
5642 wxPGFindInVector(m_dedicatedKeys, keycode) == wxNOT_FOUND )
5643 {
5644 // Only propagate event if it had modifiers
5645 if ( !event.HasModifiers() )
5646 {
5647 event.StopPropagation();
5648 }
5649 event.Skip();
5650 return;
5651 }
5652
5653 bool wasHandled = false;
5654
5655 if ( selected )
5656 {
5657 // Show dialog?
5658 if ( ButtonTriggerKeyTest(action, event) )
5659 return;
5660
5661 wxPGProperty* p = selected;
5662
5663 if ( action == wxPG_ACTION_EDIT && !editorFocused )
5664 {
5665 DoSelectProperty( p, wxPG_SEL_FOCUS );
5666 wasHandled = true;
5667 }
5668
5669 // Travel and expand/collapse
5670 int selectDir = -2;
5671
5672 if ( p->GetChildCount() )
5673 {
5674 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5675 {
5676 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5677 wasHandled = true;
5678 }
5679 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5680 {
5681 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5682 wasHandled = true;
5683 }
5684 }
5685
5686 if ( !wasHandled )
5687 {
5688 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5689 {
5690 selectDir = -1;
5691 }
5692 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5693 {
5694 selectDir = 1;
5695 }
5696 }
5697
5698 if ( selectDir >= -1 )
5699 {
5700 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5701 if ( p )
5702 {
5703 int selFlags = 0;
5704 int reopenLabelEditorCol = -1;
5705
5706 if ( editorFocused )
5707 {
5708 // If editor was focused, then make the next editor
5709 // focused as well
5710 selFlags |= wxPG_SEL_FOCUS;
5711 }
5712 else
5713 {
5714 // Also maintain the same label editor focus state
5715 if ( m_labelEditor )
5716 reopenLabelEditorCol = m_selColumn;
5717 }
5718
5719 DoSelectProperty(p, selFlags);
5720
5721 if ( reopenLabelEditorCol >= 0 )
5722 DoBeginLabelEdit(reopenLabelEditorCol);
5723 }
5724 wasHandled = true;
5725 }
5726 }
5727 else
5728 {
5729 // If nothing was selected, select the first item now
5730 // (or navigate out of tab).
5731 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5732 {
5733 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5734 if ( p ) DoSelectProperty(p);
5735 wasHandled = true;
5736 }
5737 }
5738
5739 if ( !wasHandled )
5740 event.Skip();
5741 }
5742
5743 // -----------------------------------------------------------------------
5744
5745 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5746 {
5747 // If there was editor open and focused, then this event should not
5748 // really be processed here.
5749 if ( IsEditorFocused() )
5750 {
5751 // However, if event had modifiers, it is probably still best
5752 // to skip it.
5753 if ( event.HasModifiers() )
5754 event.Skip();
5755 else
5756 event.StopPropagation();
5757 return;
5758 }
5759
5760 HandleKeyEvent(event, false);
5761 }
5762
5763 // -----------------------------------------------------------------------
5764
5765 bool wxPropertyGrid::ButtonTriggerKeyTest( int action, wxKeyEvent& event )
5766 {
5767 if ( action == -1 )
5768 {
5769 int secondAction;
5770 action = KeyEventToActions(event, &secondAction);
5771 }
5772
5773 // Does the keycode trigger button?
5774 if ( action == wxPG_ACTION_PRESS_BUTTON &&
5775 m_wndEditor2 )
5776 {
5777 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, m_wndEditor2->GetId());
5778 GetEventHandler()->AddPendingEvent(evt);
5779 return true;
5780 }
5781
5782 return false;
5783 }
5784
5785 // -----------------------------------------------------------------------
5786
5787 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5788 {
5789 HandleKeyEvent(event, true);
5790 }
5791
5792 // -----------------------------------------------------------------------
5793 // wxPropertyGrid miscellaneous event handling
5794 // -----------------------------------------------------------------------
5795
5796 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5797 {
5798 //
5799 // Check if the focus is in this control or one of its children
5800 wxWindow* newFocused = wxWindow::FindFocus();
5801
5802 if ( newFocused != m_curFocused )
5803 HandleFocusChange( newFocused );
5804
5805 //
5806 // Check if top-level parent has changed
5807 if ( GetExtraStyle() & wxPG_EX_ENABLE_TLP_TRACKING )
5808 {
5809 wxWindow* tlp = ::wxGetTopLevelParent(this);
5810 if ( tlp != m_tlp )
5811 OnTLPChanging(tlp);
5812 }
5813
5814 //
5815 // Resolve pending property removals
5816 if ( m_deletedProperties.size() > 0 )
5817 {
5818 wxArrayPGProperty& arr = m_deletedProperties;
5819 for ( unsigned int i=0; i<arr.size(); i++ )
5820 {
5821 DeleteProperty(arr[i]);
5822 }
5823 arr.clear();
5824 }
5825 if ( m_removedProperties.size() > 0 )
5826 {
5827 wxArrayPGProperty& arr = m_removedProperties;
5828 for ( unsigned int i=0; i<arr.size(); i++ )
5829 {
5830 RemoveProperty(arr[i]);
5831 }
5832 arr.clear();
5833 }
5834 }
5835
5836 bool wxPropertyGrid::IsEditorFocused() const
5837 {
5838 wxWindow* focus = wxWindow::FindFocus();
5839
5840 if ( focus == m_wndEditor || focus == m_wndEditor2 ||
5841 focus == GetEditorControl() )
5842 return true;
5843
5844 return false;
5845 }
5846
5847 // Called by focus event handlers. newFocused is the window that becomes focused.
5848 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5849 {
5850 //
5851 // Never allow focus to be changed when handling editor event.
5852 // Especially because they may be displaing a dialog which
5853 // could cause all kinds of weird (native) focus changes.
5854 if ( HasInternalFlag(wxPG_FL_IN_HANDLECUSTOMEDITOREVENT) )
5855 return;
5856
5857 unsigned int oldFlags = m_iFlags;
5858 bool wasEditorFocused = false;
5859 wxWindow* wndEditor = m_wndEditor;
5860
5861 m_iFlags &= ~(wxPG_FL_FOCUSED);
5862
5863 wxWindow* parent = newFocused;
5864
5865 // This must be one of nextFocus' parents.
5866 while ( parent )
5867 {
5868 if ( parent == wndEditor )
5869 {
5870 wasEditorFocused = true;
5871 }
5872 // Use m_eventObject, which is either wxPropertyGrid or
5873 // wxPropertyGridManager, as appropriate.
5874 else if ( parent == m_eventObject )
5875 {
5876 m_iFlags |= wxPG_FL_FOCUSED;
5877 break;
5878 }
5879 parent = parent->GetParent();
5880 }
5881
5882 // Notify editor control when it receives a focus
5883 if ( wasEditorFocused && m_curFocused != newFocused )
5884 {
5885 wxPGProperty* p = GetSelection();
5886 if ( p )
5887 {
5888 const wxPGEditor* editor = p->GetEditorClass();
5889 ResetEditorAppearance();
5890 editor->OnFocus(p, GetEditorControl());
5891 }
5892 }
5893
5894 m_curFocused = newFocused;
5895
5896 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5897 (oldFlags & wxPG_FL_FOCUSED) )
5898 {
5899 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5900 {
5901 // Need to store changed value
5902 CommitChangesFromEditor();
5903 }
5904 else
5905 {
5906 /*
5907 //
5908 // Preliminary code for tab-order respecting
5909 // tab-traversal (but should be moved to
5910 // OnNav handler)
5911 //
5912 wxWindow* prevFocus = event.GetWindow();
5913 wxWindow* useThis = this;
5914 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5915 useThis = GetParent();
5916
5917 if ( prevFocus &&
5918 prevFocus->GetParent() == useThis->GetParent() )
5919 {
5920 wxList& children = useThis->GetParent()->GetChildren();
5921
5922 wxNode* node = children.Find(prevFocus);
5923
5924 if ( node->GetNext() &&
5925 useThis == node->GetNext()->GetData() )
5926 DoSelectProperty(GetFirst());
5927 else if ( node->GetPrevious () &&
5928 useThis == node->GetPrevious()->GetData() )
5929 DoSelectProperty(GetLastProperty());
5930
5931 }
5932 */
5933 }
5934
5935 // Redraw selected
5936 wxPGProperty* selected = GetSelection();
5937 if ( selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5938 DrawItem( selected );
5939 }
5940 }
5941
5942 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5943 {
5944 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5945 HandleFocusChange((wxWindow*)event.GetEventObject());
5946 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5947 //else if ( event.GetWindow() )
5948 else
5949 HandleFocusChange(event.GetWindow());
5950
5951 event.Skip();
5952 }
5953
5954 // -----------------------------------------------------------------------
5955
5956 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5957 {
5958 HandleFocusChange((wxWindow*)event.GetEventObject());
5959 event.Skip();
5960 }
5961
5962 // -----------------------------------------------------------------------
5963
5964 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5965 {
5966 m_iFlags |= wxPG_FL_SCROLLED;
5967
5968 event.Skip();
5969 }
5970
5971 // -----------------------------------------------------------------------
5972
5973 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5974 {
5975 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5976 {
5977 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5978 }
5979 }
5980
5981 // -----------------------------------------------------------------------
5982 // Property editor related functions
5983 // -----------------------------------------------------------------------
5984
5985 // noDefCheck = true prevents infinite recursion.
5986 wxPGEditor* wxPropertyGrid::DoRegisterEditorClass( wxPGEditor* editorClass,
5987 const wxString& editorName,
5988 bool noDefCheck )
5989 {
5990 wxASSERT( editorClass );
5991
5992 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5993 RegisterDefaultEditors();
5994
5995 wxString name = editorName;
5996 if ( name.empty() )
5997 name = editorClass->GetName();
5998
5999 // Existing editor under this name?
6000 wxPGHashMapS2P::iterator vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
6001
6002 if ( vt_it != wxPGGlobalVars->m_mapEditorClasses.end() )
6003 {
6004 // If this name was already used, try class name.
6005 name = editorClass->GetClassInfo()->GetClassName();
6006 vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
6007 }
6008
6009 wxCHECK_MSG( vt_it == wxPGGlobalVars->m_mapEditorClasses.end(),
6010 (wxPGEditor*) vt_it->second,
6011 "Editor with given name was already registered" );
6012
6013 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorClass;
6014
6015 return editorClass;
6016 }
6017
6018 // Use this in RegisterDefaultEditors.
6019 #define wxPGRegisterDefaultEditorClass(EDITOR) \
6020 if ( wxPGEditor_##EDITOR == NULL ) \
6021 { \
6022 wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
6023 new wxPG##EDITOR##Editor, true ); \
6024 }
6025
6026 // Registers all default editor classes
6027 void wxPropertyGrid::RegisterDefaultEditors()
6028 {
6029 wxPGRegisterDefaultEditorClass( TextCtrl );
6030 wxPGRegisterDefaultEditorClass( Choice );
6031 wxPGRegisterDefaultEditorClass( ComboBox );
6032 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
6033 #if wxPG_INCLUDE_CHECKBOX
6034 wxPGRegisterDefaultEditorClass( CheckBox );
6035 #endif
6036 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
6037
6038 // Register SpinCtrl etc. editors before use
6039 RegisterAdditionalEditors();
6040 }
6041
6042 // -----------------------------------------------------------------------
6043 // wxPGStringTokenizer
6044 // Needed to handle C-style string lists (e.g. "str1" "str2")
6045 // -----------------------------------------------------------------------
6046
6047 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
6048 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
6049 {
6050 }
6051
6052 wxPGStringTokenizer::~wxPGStringTokenizer()
6053 {
6054 }
6055
6056 bool wxPGStringTokenizer::HasMoreTokens()
6057 {
6058 const wxString& str = *m_str;
6059
6060 wxString::const_iterator i = m_curPos;
6061
6062 wxUniChar delim = m_delimeter;
6063 wxUniChar a;
6064 wxUniChar prev_a = wxT('\0');
6065
6066 bool inToken = false;
6067
6068 while ( i != str.end() )
6069 {
6070 a = *i;
6071
6072 if ( !inToken )
6073 {
6074 if ( a == delim )
6075 {
6076 inToken = true;
6077 m_readyToken.clear();
6078 }
6079 }
6080 else
6081 {
6082 if ( prev_a != wxT('\\') )
6083 {
6084 if ( a != delim )
6085 {
6086 if ( a != wxT('\\') )
6087 m_readyToken << a;
6088 }
6089 else
6090 {
6091 ++i;
6092 m_curPos = i;
6093 return true;
6094 }
6095 prev_a = a;
6096 }
6097 else
6098 {
6099 m_readyToken << a;
6100 prev_a = wxT('\0');
6101 }
6102 }
6103 ++i;
6104 }
6105
6106 m_curPos = str.end();
6107
6108 if ( inToken )
6109 return true;
6110
6111 return false;
6112 }
6113
6114 wxString wxPGStringTokenizer::GetNextToken()
6115 {
6116 return m_readyToken;
6117 }
6118
6119 // -----------------------------------------------------------------------
6120 // wxPGChoiceEntry
6121 // -----------------------------------------------------------------------
6122
6123 wxPGChoiceEntry::wxPGChoiceEntry()
6124 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
6125 {
6126 }
6127
6128 // -----------------------------------------------------------------------
6129 // wxPGChoicesData
6130 // -----------------------------------------------------------------------
6131
6132 wxPGChoicesData::wxPGChoicesData()
6133 {
6134 }
6135
6136 wxPGChoicesData::~wxPGChoicesData()
6137 {
6138 Clear();
6139 }
6140
6141 void wxPGChoicesData::Clear()
6142 {
6143 m_items.clear();
6144 }
6145
6146 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
6147 {
6148 wxASSERT( m_items.size() == 0 );
6149
6150 m_items = data->m_items;
6151 }
6152
6153 wxPGChoiceEntry& wxPGChoicesData::Insert( int index,
6154 const wxPGChoiceEntry& item )
6155 {
6156 wxVector<wxPGChoiceEntry>::iterator it;
6157 if ( index == -1 )
6158 {
6159 it = m_items.end();
6160 index = (int) m_items.size();
6161 }
6162 else
6163 {
6164 it = m_items.begin() + index;
6165 }
6166
6167 m_items.insert(it, item);
6168
6169 wxPGChoiceEntry& ownEntry = m_items[index];
6170
6171 // Need to fix value?
6172 if ( ownEntry.GetValue() == wxPG_INVALID_VALUE )
6173 ownEntry.SetValue(index);
6174
6175 return ownEntry;
6176 }
6177
6178 // -----------------------------------------------------------------------
6179 // wxPropertyGridEvent
6180 // -----------------------------------------------------------------------
6181
6182 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
6183
6184
6185 wxDEFINE_EVENT( wxEVT_PG_SELECTED, wxPropertyGridEvent );
6186 wxDEFINE_EVENT( wxEVT_PG_CHANGING, wxPropertyGridEvent );
6187 wxDEFINE_EVENT( wxEVT_PG_CHANGED, wxPropertyGridEvent );
6188 wxDEFINE_EVENT( wxEVT_PG_HIGHLIGHTED, wxPropertyGridEvent );
6189 wxDEFINE_EVENT( wxEVT_PG_RIGHT_CLICK, wxPropertyGridEvent );
6190 wxDEFINE_EVENT( wxEVT_PG_PAGE_CHANGED, wxPropertyGridEvent );
6191 wxDEFINE_EVENT( wxEVT_PG_ITEM_EXPANDED, wxPropertyGridEvent );
6192 wxDEFINE_EVENT( wxEVT_PG_ITEM_COLLAPSED, wxPropertyGridEvent );
6193 wxDEFINE_EVENT( wxEVT_PG_DOUBLE_CLICK, wxPropertyGridEvent );
6194 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_BEGIN, wxPropertyGridEvent );
6195 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_ENDING, wxPropertyGridEvent );
6196 wxDEFINE_EVENT( wxEVT_PG_COL_BEGIN_DRAG, wxPropertyGridEvent );
6197 wxDEFINE_EVENT( wxEVT_PG_COL_DRAGGING, wxPropertyGridEvent );
6198 wxDEFINE_EVENT( wxEVT_PG_COL_END_DRAG, wxPropertyGridEvent );
6199
6200 // -----------------------------------------------------------------------
6201
6202 void wxPropertyGridEvent::Init()
6203 {
6204 m_validationInfo = NULL;
6205 m_column = 1;
6206 m_canVeto = false;
6207 m_wasVetoed = false;
6208 m_pg = NULL;
6209 }
6210
6211 // -----------------------------------------------------------------------
6212
6213 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
6214 : wxCommandEvent(commandType,id)
6215 {
6216 m_property = NULL;
6217 Init();
6218 }
6219
6220 // -----------------------------------------------------------------------
6221
6222 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
6223 : wxCommandEvent(event)
6224 {
6225 m_eventType = event.GetEventType();
6226 m_eventObject = event.m_eventObject;
6227 m_pg = event.m_pg;
6228 OnPropertyGridSet();
6229 m_property = event.m_property;
6230 m_validationInfo = event.m_validationInfo;
6231 m_canVeto = event.m_canVeto;
6232 m_wasVetoed = event.m_wasVetoed;
6233 }
6234
6235 // -----------------------------------------------------------------------
6236
6237 void wxPropertyGridEvent::OnPropertyGridSet()
6238 {
6239 if ( !m_pg )
6240 return;
6241
6242 #if wxUSE_THREADS
6243 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
6244 #endif
6245 m_pg->m_liveEvents.push_back(this);
6246 }
6247
6248 // -----------------------------------------------------------------------
6249
6250 wxPropertyGridEvent::~wxPropertyGridEvent()
6251 {
6252 if ( m_pg )
6253 {
6254 #if wxUSE_THREADS
6255 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
6256 #endif
6257
6258 // Use iterate from the back since it is more likely that the event
6259 // being desroyed is at the end of the array.
6260 wxVector<wxPropertyGridEvent*>& liveEvents = m_pg->m_liveEvents;
6261
6262 for ( int i = liveEvents.size()-1; i >= 0; i-- )
6263 {
6264 if ( liveEvents[i] == this )
6265 {
6266 liveEvents.erase(liveEvents.begin() + i);
6267 break;
6268 }
6269 }
6270 }
6271 }
6272
6273 // -----------------------------------------------------------------------
6274
6275 wxEvent* wxPropertyGridEvent::Clone() const
6276 {
6277 return new wxPropertyGridEvent( *this );
6278 }
6279
6280 // -----------------------------------------------------------------------
6281 // wxPropertyGridPopulator
6282 // -----------------------------------------------------------------------
6283
6284 wxPropertyGridPopulator::wxPropertyGridPopulator()
6285 {
6286 m_state = NULL;
6287 m_pg = NULL;
6288 wxPGGlobalVars->m_offline++;
6289 }
6290
6291 // -----------------------------------------------------------------------
6292
6293 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
6294 {
6295 m_state = state;
6296 m_propHierarchy.clear();
6297 }
6298
6299 // -----------------------------------------------------------------------
6300
6301 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
6302 {
6303 m_pg = pg;
6304 pg->Freeze();
6305 }
6306
6307 // -----------------------------------------------------------------------
6308
6309 wxPropertyGridPopulator::~wxPropertyGridPopulator()
6310 {
6311 //
6312 // Free unused sets of choices
6313 wxPGHashMapS2P::iterator it;
6314
6315 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
6316 {
6317 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
6318 data->DecRef();
6319 }
6320
6321 if ( m_pg )
6322 {
6323 m_pg->Thaw();
6324 m_pg->GetPanel()->Refresh();
6325 }
6326 wxPGGlobalVars->m_offline--;
6327 }
6328
6329 // -----------------------------------------------------------------------
6330
6331 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
6332 const wxString& propLabel,
6333 const wxString& propName,
6334 const wxString* propValue,
6335 wxPGChoices* pChoices )
6336 {
6337 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
6338 wxPGProperty* parent = GetCurParent();
6339
6340 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
6341 {
6342 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
6343 return NULL;
6344 }
6345
6346 if ( !classInfo || !classInfo->IsKindOf(wxCLASSINFO(wxPGProperty)) )
6347 {
6348 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
6349 return NULL;
6350 }
6351
6352 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
6353
6354 property->SetLabel(propLabel);
6355 property->DoSetName(propName);
6356
6357 if ( pChoices && pChoices->IsOk() )
6358 property->SetChoices(*pChoices);
6359
6360 m_state->DoInsert(parent, -1, property);
6361
6362 if ( propValue )
6363 property->SetValueFromString( *propValue, wxPG_FULL_VALUE|
6364 wxPG_PROGRAMMATIC_VALUE );
6365
6366 return property;
6367 }
6368
6369 // -----------------------------------------------------------------------
6370
6371 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
6372 {
6373 m_propHierarchy.push_back(property);
6374 DoScanForChildren();
6375 m_propHierarchy.pop_back();
6376 }
6377
6378 // -----------------------------------------------------------------------
6379
6380 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
6381 const wxString& idString )
6382 {
6383 wxPGChoices choices;
6384
6385 // Using id?
6386 if ( choicesString[0] == wxT('@') )
6387 {
6388 wxString ids = choicesString.substr(1);
6389 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
6390 if ( it == m_dictIdChoices.end() )
6391 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
6392 else
6393 choices.AssignData((wxPGChoicesData*)it->second);
6394 }
6395 else
6396 {
6397 bool found = false;
6398 if ( !idString.empty() )
6399 {
6400 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
6401 if ( it != m_dictIdChoices.end() )
6402 {
6403 choices.AssignData((wxPGChoicesData*)it->second);
6404 found = true;
6405 }
6406 }
6407
6408 if ( !found )
6409 {
6410 // Parse choices string
6411 wxString::const_iterator it = choicesString.begin();
6412 wxString label;
6413 wxString value;
6414 int state = 0;
6415 bool labelValid = false;
6416
6417 for ( ; it != choicesString.end(); ++it )
6418 {
6419 wxChar c = *it;
6420
6421 if ( state != 1 )
6422 {
6423 if ( c == wxT('"') )
6424 {
6425 if ( labelValid )
6426 {
6427 long l;
6428 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6429 choices.Add(label, l);
6430 }
6431 labelValid = false;
6432 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
6433 value.clear();
6434 label.clear();
6435 state = 1;
6436 }
6437 else if ( c == wxT('=') )
6438 {
6439 if ( labelValid )
6440 {
6441 state = 2;
6442 }
6443 }
6444 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
6445 {
6446 value << c;
6447 }
6448 }
6449 else
6450 {
6451 if ( c == wxT('"') )
6452 {
6453 state = 0;
6454 labelValid = true;
6455 }
6456 else
6457 label << c;
6458 }
6459 }
6460
6461 if ( labelValid )
6462 {
6463 long l;
6464 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6465 choices.Add(label, l);
6466 }
6467
6468 if ( !choices.IsOk() )
6469 {
6470 choices.EnsureData();
6471 }
6472
6473 // Assign to id
6474 if ( !idString.empty() )
6475 m_dictIdChoices[idString] = choices.GetData();
6476 }
6477 }
6478
6479 return choices;
6480 }
6481
6482 // -----------------------------------------------------------------------
6483
6484 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
6485 {
6486 if ( s.Last() == wxT('%') )
6487 {
6488 wxString s2 = s.substr(0,s.length()-1);
6489 long val;
6490 if ( s2.ToLong(&val, 10) )
6491 {
6492 *pval = (val*max)/100;
6493 return true;
6494 }
6495 return false;
6496 }
6497
6498 return s.ToLong(pval, 10);
6499 }
6500
6501 // -----------------------------------------------------------------------
6502
6503 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
6504 const wxString& type,
6505 const wxString& value )
6506 {
6507 int l = m_propHierarchy.size();
6508 if ( !l )
6509 return false;
6510
6511 wxPGProperty* p = m_propHierarchy[l-1];
6512 wxString valuel = value.Lower();
6513 wxVariant variant;
6514
6515 if ( type.empty() )
6516 {
6517 long v;
6518
6519 // Auto-detect type
6520 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6521 variant = true;
6522 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6523 variant = false;
6524 else if ( value.ToLong(&v, 0) )
6525 variant = v;
6526 else
6527 variant = value;
6528 }
6529 else
6530 {
6531 if ( type == wxT("string") )
6532 {
6533 variant = value;
6534 }
6535 else if ( type == wxT("int") )
6536 {
6537 long v = 0;
6538 value.ToLong(&v, 0);
6539 variant = v;
6540 }
6541 else if ( type == wxT("bool") )
6542 {
6543 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6544 variant = true;
6545 else
6546 variant = false;
6547 }
6548 else
6549 {
6550 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6551 return false;
6552 }
6553 }
6554
6555 p->SetAttribute( name, variant );
6556
6557 return true;
6558 }
6559
6560 // -----------------------------------------------------------------------
6561
6562 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6563 {
6564 wxLogError(_("Error in resource: %s"),msg.c_str());
6565 }
6566
6567 // -----------------------------------------------------------------------
6568
6569 #endif // wxUSE_PROPGRID