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