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