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