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