Slightly improved check whether or not to execute wxPropertyGrid::RecalculateVirtualS...
[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 ( HasInternalFlag(wxPG_FL_RECALCULATING_VIRTUAL_SIZE) ||
4254 m_frozen ||
4255 !m_pState ||
4256 !HasInternalFlag(wxPG_FL_INITIALIZED) )
4257 return;
4258
4259 //
4260 // If virtual height was changed, then recalculate editor control position(s)
4261 if ( m_pState->m_vhCalcPending )
4262 CorrectEditorWidgetPosY();
4263
4264 m_pState->EnsureVirtualHeight();
4265
4266 wxASSERT_LEVEL_2_MSG(
4267 m_pState->GetVirtualHeight() == m_pState->GetActualVirtualHeight(),
4268 "VirtualHeight and ActualVirtualHeight should match"
4269 );
4270
4271 m_iFlags |= wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4272
4273 int x = m_pState->m_width;
4274 int y = m_pState->m_virtualHeight;
4275
4276 int width, height;
4277 GetClientSize(&width,&height);
4278
4279 // Now adjust virtual size.
4280 SetVirtualSize(x, y);
4281
4282 int xAmount = 0;
4283 int xPos = 0;
4284
4285 //
4286 // Adjust scrollbars
4287 if ( HasVirtualWidth() )
4288 {
4289 xAmount = x/wxPG_PIXELS_PER_UNIT;
4290 xPos = GetScrollPos( wxHORIZONTAL );
4291 }
4292
4293 if ( forceXPos != -1 )
4294 xPos = forceXPos;
4295 // xPos too high?
4296 else if ( xPos > (xAmount-(width/wxPG_PIXELS_PER_UNIT)) )
4297 xPos = 0;
4298
4299 int yAmount = y / wxPG_PIXELS_PER_UNIT;
4300 int yPos = GetScrollPos( wxVERTICAL );
4301
4302 SetScrollbars( wxPG_PIXELS_PER_UNIT, wxPG_PIXELS_PER_UNIT,
4303 xAmount, yAmount, xPos, yPos, true );
4304
4305 // Must re-get size now
4306 GetClientSize(&width,&height);
4307
4308 if ( !HasVirtualWidth() )
4309 {
4310 m_pState->SetVirtualWidth(width);
4311 x = width;
4312 }
4313
4314 m_width = width;
4315 m_height = height;
4316
4317 m_pState->CheckColumnWidths();
4318
4319 if ( GetSelection() )
4320 CorrectEditorWidgetSizeX();
4321
4322 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4323 }
4324
4325 // -----------------------------------------------------------------------
4326
4327 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4328 {
4329 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4330 return;
4331
4332 int width, height;
4333 GetClientSize(&width, &height);
4334
4335 m_width = width;
4336 m_height = height;
4337
4338 #if wxPG_DOUBLE_BUFFER
4339 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4340 {
4341 int dblh = (m_lineHeight*2);
4342 if ( !m_doubleBuffer )
4343 {
4344 // Create double buffer bitmap to draw on, if none
4345 int w = (width>250)?width:250;
4346 int h = height + dblh;
4347 h = (h>400)?h:400;
4348 m_doubleBuffer = new wxBitmap( w, h );
4349 }
4350 else
4351 {
4352 int w = m_doubleBuffer->GetWidth();
4353 int h = m_doubleBuffer->GetHeight();
4354
4355 // Double buffer must be large enough
4356 if ( w < width || h < (height+dblh) )
4357 {
4358 if ( w < width ) w = width;
4359 if ( h < (height+dblh) ) h = height + dblh;
4360 delete m_doubleBuffer;
4361 m_doubleBuffer = new wxBitmap( w, h );
4362 }
4363 }
4364 }
4365
4366 #endif
4367
4368 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4369 m_ncWidth = event.GetSize().x;
4370
4371 if ( !m_frozen )
4372 {
4373 if ( m_pState->m_itemsAdded )
4374 PrepareAfterItemsAdded();
4375 else
4376 // Without this, virtual size (atleast under wxGTK) will be skewed
4377 RecalculateVirtualSize();
4378
4379 Refresh();
4380 }
4381 }
4382
4383 // -----------------------------------------------------------------------
4384
4385 void wxPropertyGrid::SetVirtualWidth( int width )
4386 {
4387 if ( width == -1 )
4388 {
4389 // Disable virtual width
4390 width = GetClientSize().x;
4391 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4392 }
4393 else
4394 {
4395 // Enable virtual width
4396 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4397 }
4398 m_pState->SetVirtualWidth( width );
4399 }
4400
4401 void wxPropertyGrid::SetFocusOnCanvas()
4402 {
4403 SetFocusIgnoringChildren();
4404 m_editorFocused = 0;
4405 }
4406
4407 // -----------------------------------------------------------------------
4408 // wxPropertyGrid mouse event handling
4409 // -----------------------------------------------------------------------
4410
4411 // selFlags uses same values DoSelectProperty's flags
4412 // Returns true if event was vetoed.
4413 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p,
4414 wxVariant* pValue,
4415 unsigned int selFlags,
4416 unsigned int column )
4417 {
4418 // selFlags should have wxPG_SEL_NOVALIDATE if event is not
4419 // vetoable.
4420
4421 // Send property grid event of specific type and with specific property
4422 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4423 evt.SetPropertyGrid(this);
4424 evt.SetEventObject(m_eventObject);
4425 evt.SetProperty(p);
4426 evt.SetColumn(column);
4427 if ( eventType == wxEVT_PG_CHANGING )
4428 {
4429 wxASSERT( pValue );
4430 evt.SetCanVeto(true);
4431 m_validationInfo.m_pValue = pValue;
4432 evt.SetupValidationInfo();
4433 }
4434 else
4435 {
4436 if ( p )
4437 evt.SetPropertyValue(p->GetValue());
4438
4439 if ( !(selFlags & wxPG_SEL_NOVALIDATE) )
4440 evt.SetCanVeto(true);
4441 }
4442
4443 m_processedEvent = &evt;
4444 m_eventObject->HandleWindowEvent(evt);
4445 m_processedEvent = NULL;
4446
4447 return evt.WasVetoed();
4448 }
4449
4450 // -----------------------------------------------------------------------
4451
4452 // Return false if should be skipped
4453 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4454 {
4455 bool res = true;
4456
4457 // Need to set focus?
4458 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4459 {
4460 SetFocusOnCanvas();
4461 }
4462
4463 wxPropertyGridPageState* state = m_pState;
4464 int splitterHit;
4465 int splitterHitOffset;
4466 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4467
4468 wxPGProperty* p = DoGetItemAtY(y);
4469
4470 if ( p )
4471 {
4472 int depth = (int)p->GetDepth() - 1;
4473
4474 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4475
4476 if ( x >= marginEnds )
4477 {
4478 // Outside margin.
4479
4480 if ( p->IsCategory() )
4481 {
4482 // This is category.
4483 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4484
4485 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4486
4487 // Expand, collapse, activate etc. if click on text or left of splitter.
4488 if ( x >= textX
4489 &&
4490 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4491 columnHit == 0
4492 )
4493 )
4494 {
4495 if ( !AddToSelectionFromInputEvent( p,
4496 columnHit,
4497 &event ) )
4498 return res;
4499
4500 // On double-click, expand/collapse.
4501 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4502 {
4503 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4504 else DoExpand( p, true );
4505 }
4506 }
4507 }
4508 else if ( splitterHit == -1 )
4509 {
4510 // Click on value.
4511 unsigned int selFlag = 0;
4512 if ( columnHit == 1 )
4513 {
4514 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4515 selFlag = wxPG_SEL_FOCUS;
4516 }
4517 if ( !AddToSelectionFromInputEvent( p,
4518 columnHit,
4519 &event,
4520 selFlag ) )
4521 return res;
4522
4523 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4524
4525 if ( p->GetChildCount() && !p->IsCategory() )
4526 // On double-click, expand/collapse.
4527 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4528 {
4529 wxPGProperty* pwc = (wxPGProperty*)p;
4530 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4531 else DoExpand( p, true );
4532 }
4533
4534 res = false;
4535 }
4536 else
4537 {
4538 // click on splitter
4539 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4540 {
4541 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4542 {
4543 // Double-clicking the splitter causes auto-centering
4544 if ( m_pState->GetColumnCount() <= 2 )
4545 {
4546 CenterSplitter( true );
4547
4548 SendEvent(wxEVT_PG_COL_DRAGGING,
4549 m_propHover,
4550 NULL,
4551 wxPG_SEL_NOVALIDATE,
4552 (unsigned int)m_draggedSplitter);
4553 }
4554 }
4555 else if ( m_dragStatus == 0 )
4556 {
4557 //
4558 // Begin draggin the splitter
4559 //
4560
4561 // send event
4562 DoEndLabelEdit(true, wxPG_SEL_NOVALIDATE);
4563
4564 // Allow application to veto dragging
4565 if ( !SendEvent(wxEVT_PG_COL_BEGIN_DRAG,
4566 p, NULL, 0,
4567 (unsigned int)splitterHit) )
4568 {
4569 if ( m_wndEditor )
4570 {
4571 // Changes must be committed here or the
4572 // value won't be drawn correctly
4573 if ( !CommitChangesFromEditor() )
4574 return res;
4575
4576 m_wndEditor->Show ( false );
4577 }
4578
4579 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4580 {
4581 CaptureMouse();
4582 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4583 }
4584
4585 m_dragStatus = 1;
4586 m_draggedSplitter = splitterHit;
4587 m_dragOffset = splitterHitOffset;
4588
4589 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4590 // Fixes button disappearance bug
4591 if ( m_wndEditor2 )
4592 m_wndEditor2->Show ( false );
4593 #endif
4594
4595 m_startingSplitterX = x - splitterHitOffset;
4596 }
4597 }
4598 }
4599 }
4600 }
4601 else
4602 {
4603 // Click on margin.
4604 if ( p->GetChildCount() )
4605 {
4606 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4607
4608 // Fine tune cell button x
4609 if ( !p->IsCategory() )
4610 nx -= IN_CELL_EXPANDER_BUTTON_X_ADJUST;
4611
4612 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4613 {
4614 int y2 = y % m_lineHeight;
4615 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4616 {
4617 // On click on expander button, expand/collapse
4618 if ( ((wxPGProperty*)p)->IsExpanded() )
4619 DoCollapse( p, true );
4620 else
4621 DoExpand( p, true );
4622 }
4623 }
4624 }
4625 }
4626 }
4627 return res;
4628 }
4629
4630 // -----------------------------------------------------------------------
4631
4632 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x),
4633 unsigned int WXUNUSED(y),
4634 wxMouseEvent& event )
4635 {
4636 if ( m_propHover )
4637 {
4638 // Select property here as well
4639 wxPGProperty* p = m_propHover;
4640 AddToSelectionFromInputEvent(p, m_colHover, &event);
4641
4642 // Send right click event.
4643 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4644
4645 return true;
4646 }
4647 return false;
4648 }
4649
4650 // -----------------------------------------------------------------------
4651
4652 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x),
4653 unsigned int WXUNUSED(y),
4654 wxMouseEvent& event )
4655 {
4656 if ( m_propHover )
4657 {
4658 // Select property here as well
4659 wxPGProperty* p = m_propHover;
4660
4661 AddToSelectionFromInputEvent(p, m_colHover, &event);
4662
4663 // Send double-click event.
4664 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4665
4666 return true;
4667 }
4668 return false;
4669 }
4670
4671 // -----------------------------------------------------------------------
4672
4673 #if wxPG_SUPPORT_TOOLTIPS
4674
4675 void wxPropertyGrid::SetToolTip( const wxString& tipString )
4676 {
4677 if ( tipString.length() )
4678 {
4679 wxScrolledWindow::SetToolTip(tipString);
4680 }
4681 else
4682 {
4683 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4684 wxScrolledWindow::SetToolTip( m_emptyString );
4685 #else
4686 wxScrolledWindow::SetToolTip( NULL );
4687 #endif
4688 }
4689 }
4690
4691 #endif // #if wxPG_SUPPORT_TOOLTIPS
4692
4693 // -----------------------------------------------------------------------
4694
4695 // Return false if should be skipped
4696 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y,
4697 wxMouseEvent &event )
4698 {
4699 // Safety check (needed because mouse capturing may
4700 // otherwise freeze the control)
4701 if ( m_dragStatus > 0 && !event.Dragging() )
4702 {
4703 HandleMouseUp(x, y, event);
4704 }
4705
4706 wxPropertyGridPageState* state = m_pState;
4707 int splitterHit;
4708 int splitterHitOffset;
4709 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4710 int splitterX = x - splitterHitOffset;
4711
4712 m_colHover = columnHit;
4713
4714 if ( m_dragStatus > 0 )
4715 {
4716 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4717 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4718 {
4719
4720 int newSplitterX = x - m_dragOffset;
4721 int splitterX = x - splitterHitOffset;
4722
4723 // Splitter redraw required?
4724 if ( newSplitterX != splitterX )
4725 {
4726 // Move everything
4727 DoSetSplitterPosition(newSplitterX,
4728 m_draggedSplitter,
4729 wxPG_SPLITTER_REFRESH |
4730 wxPG_SPLITTER_FROM_EVENT);
4731
4732 SendEvent(wxEVT_PG_COL_DRAGGING,
4733 m_propHover,
4734 NULL,
4735 wxPG_SEL_NOVALIDATE,
4736 (unsigned int)m_draggedSplitter);
4737 }
4738
4739 m_dragStatus = 2;
4740 }
4741
4742 return false;
4743 }
4744 else
4745 {
4746
4747 int ih = m_lineHeight;
4748 int sy = y;
4749
4750 #if wxPG_SUPPORT_TOOLTIPS
4751 wxPGProperty* prevHover = m_propHover;
4752 unsigned char prevSide = m_mouseSide;
4753 #endif
4754 int curPropHoverY = y - (y % ih);
4755
4756 // On which item it hovers
4757 if ( !m_propHover
4758 ||
4759 ( sy < m_propHoverY || sy >= (m_propHoverY+ih) )
4760 )
4761 {
4762 // Mouse moves on another property
4763
4764 m_propHover = DoGetItemAtY(y);
4765 m_propHoverY = curPropHoverY;
4766
4767 // Send hover event
4768 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
4769 }
4770
4771 #if wxPG_SUPPORT_TOOLTIPS
4772 // Store which side we are on
4773 m_mouseSide = 0;
4774 if ( columnHit == 1 )
4775 m_mouseSide = 2;
4776 else if ( columnHit == 0 )
4777 m_mouseSide = 1;
4778
4779 //
4780 // If tooltips are enabled, show label or value as a tip
4781 // in case it doesn't otherwise show in full length.
4782 //
4783 if ( m_windowStyle & wxPG_TOOLTIPS )
4784 {
4785 wxToolTip* tooltip = GetToolTip();
4786
4787 if ( m_propHover != prevHover || prevSide != m_mouseSide )
4788 {
4789 if ( m_propHover && !m_propHover->IsCategory() )
4790 {
4791
4792 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
4793 {
4794 // Show help string as a tooltip
4795 wxString tipString = m_propHover->GetHelpString();
4796
4797 SetToolTip(tipString);
4798 }
4799 else
4800 {
4801 // Show cropped value string as a tooltip
4802 wxString tipString;
4803 int space = 0;
4804
4805 if ( m_mouseSide == 1 )
4806 {
4807 tipString = m_propHover->m_label;
4808 space = splitterX-m_marginWidth-3;
4809 }
4810 else if ( m_mouseSide == 2 )
4811 {
4812 tipString = m_propHover->GetDisplayedString();
4813
4814 space = m_width - splitterX;
4815 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
4816 space -= wxPG_CUSTOM_IMAGE_WIDTH +
4817 wxCC_CUSTOM_IMAGE_MARGIN1 +
4818 wxCC_CUSTOM_IMAGE_MARGIN2;
4819 }
4820
4821 if ( space )
4822 {
4823 int tw, th;
4824 GetTextExtent( tipString, &tw, &th, 0, 0 );
4825 if ( tw > space )
4826 {
4827 SetToolTip( tipString );
4828 }
4829 }
4830 else
4831 {
4832 if ( tooltip )
4833 {
4834 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4835 SetToolTip( m_emptyString );
4836 #else
4837 wxScrolledWindow::SetToolTip( NULL );
4838 #endif
4839 }
4840 }
4841
4842 }
4843 }
4844 else
4845 {
4846 if ( tooltip )
4847 {
4848 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4849 SetToolTip( m_emptyString );
4850 #else
4851 wxScrolledWindow::SetToolTip( NULL );
4852 #endif
4853 }
4854 }
4855 }
4856 }
4857 #endif
4858
4859 if ( splitterHit == -1 ||
4860 !m_propHover ||
4861 HasFlag(wxPG_STATIC_SPLITTER) )
4862 {
4863 // hovering on something else
4864 if ( m_curcursor != wxCURSOR_ARROW )
4865 CustomSetCursor( wxCURSOR_ARROW );
4866 }
4867 else
4868 {
4869 // Do not allow splitter cursor on caption items.
4870 // (also not if we were dragging and its started
4871 // outside the splitter region)
4872
4873 if ( !m_propHover->IsCategory() &&
4874 !event.Dragging() )
4875 {
4876
4877 // hovering on splitter
4878
4879 // NB: Condition disabled since MouseLeave event (from the
4880 // editor control) cannot be reliably detected.
4881 //if ( m_curcursor != wxCURSOR_SIZEWE )
4882 CustomSetCursor( wxCURSOR_SIZEWE, true );
4883
4884 return false;
4885 }
4886 else
4887 {
4888 // hovering on something else
4889 if ( m_curcursor != wxCURSOR_ARROW )
4890 CustomSetCursor( wxCURSOR_ARROW );
4891 }
4892 }
4893
4894 //
4895 // Multi select by dragging
4896 //
4897 if ( (GetExtraStyle() & wxPG_EX_MULTIPLE_SELECTION) &&
4898 event.LeftIsDown() &&
4899 m_propHover &&
4900 GetSelection() &&
4901 columnHit != 1 &&
4902 !state->DoIsPropertySelected(m_propHover) )
4903 {
4904 // Additional requirement is that the hovered property
4905 // is adjacent to edges of selection.
4906 const wxArrayPGProperty& selection = GetSelectedProperties();
4907
4908 // Since categories cannot be selected along with 'other'
4909 // properties, exclude them from iterator flags.
4910 int iterFlags = wxPG_ITERATE_VISIBLE & (~wxPG_PROP_CATEGORY);
4911
4912 for ( int i=(selection.size()-1); i>=0; i-- )
4913 {
4914 // TODO: This could be optimized by keeping track of
4915 // which properties are at the edges of selection.
4916 wxPGProperty* selProp = selection[i];
4917 if ( state->ArePropertiesAdjacent(m_propHover, selProp,
4918 iterFlags) )
4919 {
4920 DoAddToSelection(m_propHover);
4921 break;
4922 }
4923 }
4924 }
4925 }
4926 return true;
4927 }
4928
4929 // -----------------------------------------------------------------------
4930
4931 // Also handles Leaving event
4932 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
4933 wxMouseEvent &WXUNUSED(event) )
4934 {
4935 wxPropertyGridPageState* state = m_pState;
4936 bool res = false;
4937
4938 int splitterHit;
4939 int splitterHitOffset;
4940 state->HitTestH( x, &splitterHit, &splitterHitOffset );
4941
4942 // No event type check - basicly calling this method should
4943 // just stop dragging.
4944 // Left up after dragged?
4945 if ( m_dragStatus >= 1 )
4946 {
4947 //
4948 // End Splitter Dragging
4949 //
4950 // DO NOT ENABLE FOLLOWING LINE!
4951 // (it is only here as a reminder to not to do it)
4952 //splitterX = x;
4953
4954 SendEvent(wxEVT_PG_COL_END_DRAG,
4955 m_propHover,
4956 NULL,
4957 wxPG_SEL_NOVALIDATE,
4958 (unsigned int)m_draggedSplitter);
4959
4960 // Disable splitter auto-centering
4961 state->m_dontCenterSplitter = true;
4962
4963 // This is necessary to return cursor
4964 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
4965 {
4966 ReleaseMouse();
4967 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
4968 }
4969
4970 // Set back the default cursor, if necessary
4971 if ( splitterHit == -1 ||
4972 !m_propHover )
4973 {
4974 CustomSetCursor( wxCURSOR_ARROW );
4975 }
4976
4977 m_dragStatus = 0;
4978
4979 // Control background needs to be cleared
4980 wxPGProperty* selected = GetSelection();
4981 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && selected )
4982 DrawItem( selected );
4983
4984 if ( m_wndEditor )
4985 {
4986 m_wndEditor->Show ( true );
4987 }
4988
4989 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4990 // Fixes button disappearance bug
4991 if ( m_wndEditor2 )
4992 m_wndEditor2->Show ( true );
4993 #endif
4994
4995 // This clears the focus.
4996 m_editorFocused = 0;
4997
4998 }
4999 return res;
5000 }
5001
5002 // -----------------------------------------------------------------------
5003
5004 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
5005 {
5006 int splitterX = GetSplitterPosition();
5007
5008 int ux, uy;
5009 CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
5010
5011 wxWindow* wnd = GetEditorControl();
5012
5013 // Hide popup on clicks
5014 if ( event.GetEventType() != wxEVT_MOTION )
5015 if ( wnd && wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
5016 {
5017 ((wxOwnerDrawnComboBox*)wnd)->HidePopup();
5018 }
5019
5020 wxRect r;
5021 if ( wnd )
5022 r = wnd->GetRect();
5023 if ( wnd == NULL || m_dragStatus ||
5024 (
5025 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
5026 ux >= (r.x+r.width) ||
5027 event.m_y < r.y ||
5028 event.m_y >= (r.y+r.height)
5029 )
5030 )
5031 {
5032 *px = ux;
5033 *py = uy;
5034 return true;
5035 }
5036 else
5037 {
5038 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5039 }
5040 return false;
5041 }
5042
5043 // -----------------------------------------------------------------------
5044
5045 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
5046 {
5047 int x, y;
5048 if ( OnMouseCommon( event, &x, &y ) )
5049 {
5050 HandleMouseClick(x,y,event);
5051 }
5052 event.Skip();
5053 }
5054
5055 // -----------------------------------------------------------------------
5056
5057 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
5058 {
5059 int x, y;
5060 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5061 HandleMouseRightClick(x,y,event);
5062 event.Skip();
5063 }
5064
5065 // -----------------------------------------------------------------------
5066
5067 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
5068 {
5069 // Always run standard mouse-down handler as well
5070 OnMouseClick(event);
5071
5072 int x, y;
5073 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
5074 HandleMouseDoubleClick(x,y,event);
5075 event.Skip();
5076 }
5077
5078 // -----------------------------------------------------------------------
5079
5080 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
5081 {
5082 int x, y;
5083 if ( OnMouseCommon( event, &x, &y ) )
5084 {
5085 HandleMouseMove(x,y,event);
5086 }
5087 event.Skip();
5088 }
5089
5090 // -----------------------------------------------------------------------
5091
5092 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
5093 {
5094 int x, y;
5095 if ( OnMouseCommon( event, &x, &y ) )
5096 {
5097 HandleMouseUp(x,y,event);
5098 }
5099 event.Skip();
5100 }
5101
5102 // -----------------------------------------------------------------------
5103
5104 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
5105 {
5106 // This may get called from child control as well, so event's
5107 // mouse position cannot be relied on.
5108
5109 if ( event.Entering() )
5110 {
5111 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5112 {
5113 // TODO: Fix this (detect parent and only do
5114 // cursor trick if it is a manager).
5115 wxASSERT( GetParent() );
5116 GetParent()->SetCursor(wxNullCursor);
5117
5118 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
5119 }
5120 else
5121 GetParent()->SetCursor(wxNullCursor);
5122 }
5123 else if ( event.Leaving() )
5124 {
5125 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
5126 SetCursor( wxNullCursor );
5127
5128 // Get real cursor position
5129 wxPoint pt = ScreenToClient(::wxGetMousePosition());
5130
5131 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
5132 {
5133 {
5134 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
5135 {
5136 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
5137 }
5138
5139 if ( m_dragStatus )
5140 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
5141 }
5142 }
5143 }
5144
5145 event.Skip();
5146 }
5147
5148 // -----------------------------------------------------------------------
5149
5150 // Common code used by various OnMouseXXXChild methods.
5151 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
5152 {
5153 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
5154 wxASSERT( topCtrlWnd );
5155 int x, y;
5156 event.GetPosition(&x,&y);
5157
5158 int splitterX = GetSplitterPosition();
5159
5160 wxRect r = topCtrlWnd->GetRect();
5161 if ( !m_dragStatus &&
5162 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
5163 y >= 0 && y < r.height \
5164 )
5165 {
5166 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
5167 event.Skip();
5168 }
5169 else
5170 {
5171 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
5172 px, py );
5173 return true;
5174 }
5175 return false;
5176 }
5177
5178 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
5179 {
5180 int x,y;
5181 if ( OnMouseChildCommon(event,&x,&y) )
5182 {
5183 bool res = HandleMouseClick(x,y,event);
5184 if ( !res ) event.Skip();
5185 }
5186 }
5187
5188 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
5189 {
5190 int x,y;
5191 wxASSERT( m_wndEditor );
5192 // These coords may not be exact (about +-2),
5193 // but that should not matter (right click is about item, not position).
5194 wxPoint pt = m_wndEditor->GetPosition();
5195 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
5196
5197 // FIXME: Used to set m_propHover to selection here. Was it really
5198 // necessary?
5199
5200 bool res = HandleMouseRightClick(x,y,event);
5201 if ( !res ) event.Skip();
5202 }
5203
5204 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
5205 {
5206 int x,y;
5207 if ( OnMouseChildCommon(event,&x,&y) )
5208 {
5209 bool res = HandleMouseMove(x,y,event);
5210 if ( !res ) event.Skip();
5211 }
5212 }
5213
5214 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
5215 {
5216 int x,y;
5217 if ( OnMouseChildCommon(event,&x,&y) )
5218 {
5219 bool res = HandleMouseUp(x,y,event);
5220 if ( !res ) event.Skip();
5221 }
5222 }
5223
5224 // -----------------------------------------------------------------------
5225 // wxPropertyGrid keyboard event handling
5226 // -----------------------------------------------------------------------
5227
5228 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
5229 {
5230 // Translates wxKeyEvent to wxPG_ACTION_XXX
5231
5232 int keycode = event.GetKeyCode();
5233 int modifiers = event.GetModifiers();
5234
5235 wxASSERT( !(modifiers&~(0xFFFF)) );
5236
5237 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5238
5239 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
5240
5241 if ( it == m_actionTriggers.end() )
5242 return 0;
5243
5244 if ( pSecond )
5245 {
5246 int second = (it->second>>16) & 0xFFFF;
5247 *pSecond = second;
5248 }
5249
5250 return (it->second & 0xFFFF);
5251 }
5252
5253 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
5254 {
5255 wxASSERT( !(modifiers&~(0xFFFF)) );
5256
5257 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
5258
5259 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
5260
5261 if ( it != m_actionTriggers.end() )
5262 {
5263 // This key combination is already used
5264
5265 // Can add secondary?
5266 wxASSERT_MSG( !(it->second&~(0xFFFF)),
5267 wxT("You can only add up to two separate actions per key combination.") );
5268
5269 action = it->second | (action<<16);
5270 }
5271
5272 m_actionTriggers[hashMapKey] = action;
5273 }
5274
5275 void wxPropertyGrid::ClearActionTriggers( int action )
5276 {
5277 wxPGHashMapI2I::iterator it;
5278 bool didSomething;
5279
5280 do
5281 {
5282 didSomething = false;
5283
5284 for ( it = m_actionTriggers.begin();
5285 it != m_actionTriggers.end();
5286 it++ )
5287 {
5288 if ( it->second == action )
5289 {
5290 m_actionTriggers.erase(it);
5291 didSomething = true;
5292 break;
5293 }
5294 }
5295 }
5296 while ( didSomething );
5297 }
5298
5299 void wxPropertyGrid::HandleKeyEvent( wxKeyEvent &event, bool fromChild )
5300 {
5301 //
5302 // Handles key event when editor control is not focused.
5303 //
5304
5305 wxCHECK2(!m_frozen, return);
5306
5307 // Travelsal between items, collapsing/expanding, etc.
5308 wxPGProperty* selected = GetSelection();
5309 int keycode = event.GetKeyCode();
5310 bool editorFocused = IsEditorFocused();
5311
5312 if ( keycode == WXK_TAB )
5313 {
5314 wxWindow* mainControl;
5315
5316 if ( HasInternalFlag(wxPG_FL_IN_MANAGER) )
5317 mainControl = GetParent();
5318 else
5319 mainControl = this;
5320
5321 if ( !event.ShiftDown() )
5322 {
5323 if ( !editorFocused && m_wndEditor )
5324 {
5325 DoSelectProperty( selected, wxPG_SEL_FOCUS );
5326 }
5327 else
5328 {
5329 // Tab traversal workaround for platforms on which
5330 // wxWindow::Navigate() may navigate into first child
5331 // instead of next sibling. Does not work perfectly
5332 // in every scenario (for instance, when property grid
5333 // is either first or last control).
5334 #if defined(__WXGTK__)
5335 wxWindow* sibling = mainControl->GetNextSibling();
5336 if ( sibling )
5337 sibling->SetFocusFromKbd();
5338 #else
5339 Navigate(wxNavigationKeyEvent::IsForward);
5340 #endif
5341 }
5342 }
5343 else
5344 {
5345 if ( editorFocused )
5346 {
5347 UnfocusEditor();
5348 }
5349 else
5350 {
5351 #if defined(__WXGTK__)
5352 wxWindow* sibling = mainControl->GetPrevSibling();
5353 if ( sibling )
5354 sibling->SetFocusFromKbd();
5355 #else
5356 Navigate(wxNavigationKeyEvent::IsBackward);
5357 #endif
5358 }
5359 }
5360
5361 return;
5362 }
5363
5364 // Ignore Alt and Control when they are down alone
5365 if ( keycode == WXK_ALT ||
5366 keycode == WXK_CONTROL )
5367 {
5368 event.Skip();
5369 return;
5370 }
5371
5372 int secondAction;
5373 int action = KeyEventToActions(event, &secondAction);
5374
5375 if ( editorFocused && action == wxPG_ACTION_CANCEL_EDIT )
5376 {
5377 //
5378 // Esc cancels any changes
5379 if ( IsEditorsValueModified() )
5380 {
5381 EditorsValueWasNotModified();
5382
5383 // Update the control as well
5384 selected->GetEditorClass()->
5385 SetControlStringValue( selected,
5386 GetEditorControl(),
5387 selected->GetDisplayedString() );
5388 }
5389
5390 OnValidationFailureReset(selected);
5391
5392 UnfocusEditor();
5393 return;
5394 }
5395
5396 // Except for TAB and ESC, handle child control events in child control
5397 if ( fromChild )
5398 {
5399 // Only propagate event if it had modifiers
5400 if ( !event.HasModifiers() )
5401 {
5402 event.StopPropagation();
5403 }
5404 event.Skip();
5405 return;
5406 }
5407
5408 bool wasHandled = false;
5409
5410 if ( selected )
5411 {
5412 // Show dialog?
5413 if ( ButtonTriggerKeyTest(action, event) )
5414 return;
5415
5416 wxPGProperty* p = selected;
5417
5418 // Travel and expand/collapse
5419 int selectDir = -2;
5420
5421 if ( p->GetChildCount() )
5422 {
5423 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5424 {
5425 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5426 wasHandled = true;
5427 }
5428 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5429 {
5430 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5431 wasHandled = true;
5432 }
5433 }
5434
5435 if ( !wasHandled )
5436 {
5437 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5438 {
5439 selectDir = -1;
5440 }
5441 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5442 {
5443 selectDir = 1;
5444 }
5445 }
5446
5447 if ( selectDir >= -1 )
5448 {
5449 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5450 if ( p )
5451 DoSelectProperty(p);
5452 wasHandled = true;
5453 }
5454 }
5455 else
5456 {
5457 // If nothing was selected, select the first item now
5458 // (or navigate out of tab).
5459 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5460 {
5461 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5462 if ( p ) DoSelectProperty(p);
5463 wasHandled = true;
5464 }
5465 }
5466
5467 if ( !wasHandled )
5468 event.Skip();
5469 }
5470
5471 // -----------------------------------------------------------------------
5472
5473 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5474 {
5475 // If there was editor open and focused, then this event should not
5476 // really be processed here.
5477 if ( IsEditorFocused() )
5478 {
5479 // However, if event had modifiers, it is probably still best
5480 // to skip it.
5481 if ( event.HasModifiers() )
5482 event.Skip();
5483 else
5484 event.StopPropagation();
5485 return;
5486 }
5487
5488 HandleKeyEvent(event, false);
5489 }
5490
5491 // -----------------------------------------------------------------------
5492
5493 bool wxPropertyGrid::ButtonTriggerKeyTest( int action, wxKeyEvent& event )
5494 {
5495 if ( action == -1 )
5496 {
5497 int secondAction;
5498 action = KeyEventToActions(event, &secondAction);
5499 }
5500
5501 // Does the keycode trigger button?
5502 if ( action == wxPG_ACTION_PRESS_BUTTON &&
5503 m_wndEditor2 )
5504 {
5505 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, m_wndEditor2->GetId());
5506 GetEventHandler()->AddPendingEvent(evt);
5507 return true;
5508 }
5509
5510 return false;
5511 }
5512
5513 // -----------------------------------------------------------------------
5514
5515 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5516 {
5517 HandleKeyEvent(event, true);
5518 }
5519
5520 // -----------------------------------------------------------------------
5521 // wxPropertyGrid miscellaneous event handling
5522 // -----------------------------------------------------------------------
5523
5524 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5525 {
5526 //
5527 // Check if the focus is in this control or one of its children
5528 wxWindow* newFocused = wxWindow::FindFocus();
5529
5530 if ( newFocused != m_curFocused )
5531 HandleFocusChange( newFocused );
5532
5533 //
5534 // Check if top-level parent has changed
5535 if ( GetExtraStyle() & wxPG_EX_ENABLE_TLP_TRACKING )
5536 {
5537 wxWindow* tlp = ::wxGetTopLevelParent(this);
5538 if ( tlp != m_tlp )
5539 OnTLPChanging(tlp);
5540 }
5541 }
5542
5543 bool wxPropertyGrid::IsEditorFocused() const
5544 {
5545 wxWindow* focus = wxWindow::FindFocus();
5546
5547 if ( focus == m_wndEditor || focus == m_wndEditor2 ||
5548 focus == GetEditorControl() )
5549 return true;
5550
5551 return false;
5552 }
5553
5554 // Called by focus event handlers. newFocused is the window that becomes focused.
5555 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5556 {
5557 unsigned int oldFlags = m_iFlags;
5558
5559 m_iFlags &= ~(wxPG_FL_FOCUSED);
5560
5561 wxWindow* parent = newFocused;
5562
5563 // This must be one of nextFocus' parents.
5564 while ( parent )
5565 {
5566 // Use m_eventObject, which is either wxPropertyGrid or
5567 // wxPropertyGridManager, as appropriate.
5568 if ( parent == m_eventObject )
5569 {
5570 m_iFlags |= wxPG_FL_FOCUSED;
5571 break;
5572 }
5573 parent = parent->GetParent();
5574 }
5575
5576 m_curFocused = newFocused;
5577
5578 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5579 (oldFlags & wxPG_FL_FOCUSED) )
5580 {
5581 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5582 {
5583 // Need to store changed value
5584 CommitChangesFromEditor();
5585 }
5586 else
5587 {
5588 /*
5589 //
5590 // Preliminary code for tab-order respecting
5591 // tab-traversal (but should be moved to
5592 // OnNav handler)
5593 //
5594 wxWindow* prevFocus = event.GetWindow();
5595 wxWindow* useThis = this;
5596 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5597 useThis = GetParent();
5598
5599 if ( prevFocus &&
5600 prevFocus->GetParent() == useThis->GetParent() )
5601 {
5602 wxList& children = useThis->GetParent()->GetChildren();
5603
5604 wxNode* node = children.Find(prevFocus);
5605
5606 if ( node->GetNext() &&
5607 useThis == node->GetNext()->GetData() )
5608 DoSelectProperty(GetFirst());
5609 else if ( node->GetPrevious () &&
5610 useThis == node->GetPrevious()->GetData() )
5611 DoSelectProperty(GetLastProperty());
5612
5613 }
5614 */
5615 }
5616
5617 // Redraw selected
5618 wxPGProperty* selected = GetSelection();
5619 if ( selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5620 DrawItem( selected );
5621 }
5622 }
5623
5624 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5625 {
5626 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5627 HandleFocusChange((wxWindow*)event.GetEventObject());
5628 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5629 //else if ( event.GetWindow() )
5630 else
5631 HandleFocusChange(event.GetWindow());
5632
5633 event.Skip();
5634 }
5635
5636 // -----------------------------------------------------------------------
5637
5638 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5639 {
5640 HandleFocusChange((wxWindow*)event.GetEventObject());
5641 event.Skip();
5642 }
5643
5644 // -----------------------------------------------------------------------
5645
5646 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5647 {
5648 m_iFlags |= wxPG_FL_SCROLLED;
5649
5650 event.Skip();
5651 }
5652
5653 // -----------------------------------------------------------------------
5654
5655 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5656 {
5657 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5658 {
5659 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5660 }
5661 }
5662
5663 // -----------------------------------------------------------------------
5664 // Property editor related functions
5665 // -----------------------------------------------------------------------
5666
5667 // noDefCheck = true prevents infinite recursion.
5668 wxPGEditor* wxPropertyGrid::DoRegisterEditorClass( wxPGEditor* editorClass,
5669 const wxString& editorName,
5670 bool noDefCheck )
5671 {
5672 wxASSERT( editorClass );
5673
5674 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5675 RegisterDefaultEditors();
5676
5677 wxString name = editorName;
5678 if ( name.length() == 0 )
5679 name = editorClass->GetName();
5680
5681 // Existing editor under this name?
5682 wxPGHashMapS2P::iterator vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5683
5684 if ( vt_it != wxPGGlobalVars->m_mapEditorClasses.end() )
5685 {
5686 // If this name was already used, try class name.
5687 name = editorClass->GetClassInfo()->GetClassName();
5688 vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5689 }
5690
5691 wxCHECK_MSG( vt_it == wxPGGlobalVars->m_mapEditorClasses.end(),
5692 (wxPGEditor*) vt_it->second,
5693 "Editor with given name was already registered" );
5694
5695 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorClass;
5696
5697 return editorClass;
5698 }
5699
5700 // Use this in RegisterDefaultEditors.
5701 #define wxPGRegisterDefaultEditorClass(EDITOR) \
5702 if ( wxPGEditor_##EDITOR == NULL ) \
5703 { \
5704 wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
5705 new wxPG##EDITOR##Editor, true ); \
5706 }
5707
5708 // Registers all default editor classes
5709 void wxPropertyGrid::RegisterDefaultEditors()
5710 {
5711 wxPGRegisterDefaultEditorClass( TextCtrl );
5712 wxPGRegisterDefaultEditorClass( Choice );
5713 wxPGRegisterDefaultEditorClass( ComboBox );
5714 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
5715 #if wxPG_INCLUDE_CHECKBOX
5716 wxPGRegisterDefaultEditorClass( CheckBox );
5717 #endif
5718 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
5719
5720 // Register SpinCtrl etc. editors before use
5721 RegisterAdditionalEditors();
5722 }
5723
5724 // -----------------------------------------------------------------------
5725 // wxPGStringTokenizer
5726 // Needed to handle C-style string lists (e.g. "str1" "str2")
5727 // -----------------------------------------------------------------------
5728
5729 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
5730 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
5731 {
5732 }
5733
5734 wxPGStringTokenizer::~wxPGStringTokenizer()
5735 {
5736 }
5737
5738 bool wxPGStringTokenizer::HasMoreTokens()
5739 {
5740 const wxString& str = *m_str;
5741
5742 wxString::const_iterator i = m_curPos;
5743
5744 wxUniChar delim = m_delimeter;
5745 wxUniChar a;
5746 wxUniChar prev_a = wxT('\0');
5747
5748 bool inToken = false;
5749
5750 while ( i != str.end() )
5751 {
5752 a = *i;
5753
5754 if ( !inToken )
5755 {
5756 if ( a == delim )
5757 {
5758 inToken = true;
5759 m_readyToken.clear();
5760 }
5761 }
5762 else
5763 {
5764 if ( prev_a != wxT('\\') )
5765 {
5766 if ( a != delim )
5767 {
5768 if ( a != wxT('\\') )
5769 m_readyToken << a;
5770 }
5771 else
5772 {
5773 ++i;
5774 m_curPos = i;
5775 return true;
5776 }
5777 prev_a = a;
5778 }
5779 else
5780 {
5781 m_readyToken << a;
5782 prev_a = wxT('\0');
5783 }
5784 }
5785 ++i;
5786 }
5787
5788 m_curPos = str.end();
5789
5790 if ( inToken )
5791 return true;
5792
5793 return false;
5794 }
5795
5796 wxString wxPGStringTokenizer::GetNextToken()
5797 {
5798 return m_readyToken;
5799 }
5800
5801 // -----------------------------------------------------------------------
5802 // wxPGChoiceEntry
5803 // -----------------------------------------------------------------------
5804
5805 wxPGChoiceEntry::wxPGChoiceEntry()
5806 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
5807 {
5808 }
5809
5810 // -----------------------------------------------------------------------
5811 // wxPGChoicesData
5812 // -----------------------------------------------------------------------
5813
5814 wxPGChoicesData::wxPGChoicesData()
5815 {
5816 }
5817
5818 wxPGChoicesData::~wxPGChoicesData()
5819 {
5820 Clear();
5821 }
5822
5823 void wxPGChoicesData::Clear()
5824 {
5825 m_items.clear();
5826 }
5827
5828 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
5829 {
5830 wxASSERT( m_items.size() == 0 );
5831
5832 m_items = data->m_items;
5833 }
5834
5835 wxPGChoiceEntry& wxPGChoicesData::Insert( int index,
5836 const wxPGChoiceEntry& item )
5837 {
5838 wxVector<wxPGChoiceEntry>::iterator it;
5839 if ( index == -1 )
5840 {
5841 it = m_items.end();
5842 index = (int) m_items.size();
5843 }
5844 else
5845 {
5846 it = m_items.begin() + index;
5847 }
5848
5849 m_items.insert(it, item);
5850
5851 wxPGChoiceEntry& ownEntry = m_items[index];
5852
5853 // Need to fix value?
5854 if ( ownEntry.GetValue() == wxPG_INVALID_VALUE )
5855 ownEntry.SetValue(index);
5856
5857 return ownEntry;
5858 }
5859
5860 // -----------------------------------------------------------------------
5861 // wxPropertyGridEvent
5862 // -----------------------------------------------------------------------
5863
5864 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
5865
5866
5867 wxDEFINE_EVENT( wxEVT_PG_SELECTED, wxPropertyGridEvent );
5868 wxDEFINE_EVENT( wxEVT_PG_CHANGING, wxPropertyGridEvent );
5869 wxDEFINE_EVENT( wxEVT_PG_CHANGED, wxPropertyGridEvent );
5870 wxDEFINE_EVENT( wxEVT_PG_HIGHLIGHTED, wxPropertyGridEvent );
5871 wxDEFINE_EVENT( wxEVT_PG_RIGHT_CLICK, wxPropertyGridEvent );
5872 wxDEFINE_EVENT( wxEVT_PG_PAGE_CHANGED, wxPropertyGridEvent );
5873 wxDEFINE_EVENT( wxEVT_PG_ITEM_EXPANDED, wxPropertyGridEvent );
5874 wxDEFINE_EVENT( wxEVT_PG_ITEM_COLLAPSED, wxPropertyGridEvent );
5875 wxDEFINE_EVENT( wxEVT_PG_DOUBLE_CLICK, wxPropertyGridEvent );
5876 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_BEGIN, wxPropertyGridEvent );
5877 wxDEFINE_EVENT( wxEVT_PG_LABEL_EDIT_ENDING, wxPropertyGridEvent );
5878 wxDEFINE_EVENT( wxEVT_PG_COL_BEGIN_DRAG, wxPropertyGridEvent );
5879 wxDEFINE_EVENT( wxEVT_PG_COL_DRAGGING, wxPropertyGridEvent );
5880 wxDEFINE_EVENT( wxEVT_PG_COL_END_DRAG, wxPropertyGridEvent );
5881
5882 // -----------------------------------------------------------------------
5883
5884 void wxPropertyGridEvent::Init()
5885 {
5886 m_validationInfo = NULL;
5887 m_column = 1;
5888 m_canVeto = false;
5889 m_wasVetoed = false;
5890 }
5891
5892 // -----------------------------------------------------------------------
5893
5894 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
5895 : wxCommandEvent(commandType,id)
5896 {
5897 m_property = NULL;
5898 Init();
5899 }
5900
5901 // -----------------------------------------------------------------------
5902
5903 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
5904 : wxCommandEvent(event)
5905 {
5906 m_eventType = event.GetEventType();
5907 m_eventObject = event.m_eventObject;
5908 m_pg = event.m_pg;
5909 OnPropertyGridSet();
5910 m_property = event.m_property;
5911 m_validationInfo = event.m_validationInfo;
5912 m_canVeto = event.m_canVeto;
5913 m_wasVetoed = event.m_wasVetoed;
5914 }
5915
5916 // -----------------------------------------------------------------------
5917
5918 void wxPropertyGridEvent::OnPropertyGridSet()
5919 {
5920 if ( !m_pg )
5921 return;
5922
5923 #if wxUSE_THREADS
5924 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
5925 #endif
5926 m_pg->m_liveEvents.push_back(this);
5927 }
5928
5929 // -----------------------------------------------------------------------
5930
5931 wxPropertyGridEvent::~wxPropertyGridEvent()
5932 {
5933 if ( m_pg )
5934 {
5935 #if wxUSE_THREADS
5936 wxCriticalSectionLocker(wxPGGlobalVars->m_critSect);
5937 #endif
5938
5939 // Use iterate from the back since it is more likely that the event
5940 // being desroyed is at the end of the array.
5941 wxVector<wxPropertyGridEvent*>& liveEvents = m_pg->m_liveEvents;
5942
5943 for ( int i = liveEvents.size()-1; i >= 0; i-- )
5944 {
5945 if ( liveEvents[i] == this )
5946 {
5947 liveEvents.erase(liveEvents.begin() + i);
5948 break;
5949 }
5950 }
5951 }
5952 }
5953
5954 // -----------------------------------------------------------------------
5955
5956 wxEvent* wxPropertyGridEvent::Clone() const
5957 {
5958 return new wxPropertyGridEvent( *this );
5959 }
5960
5961 // -----------------------------------------------------------------------
5962 // wxPropertyGridPopulator
5963 // -----------------------------------------------------------------------
5964
5965 wxPropertyGridPopulator::wxPropertyGridPopulator()
5966 {
5967 m_state = NULL;
5968 m_pg = NULL;
5969 wxPGGlobalVars->m_offline++;
5970 }
5971
5972 // -----------------------------------------------------------------------
5973
5974 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
5975 {
5976 m_state = state;
5977 m_propHierarchy.clear();
5978 }
5979
5980 // -----------------------------------------------------------------------
5981
5982 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
5983 {
5984 m_pg = pg;
5985 pg->Freeze();
5986 }
5987
5988 // -----------------------------------------------------------------------
5989
5990 wxPropertyGridPopulator::~wxPropertyGridPopulator()
5991 {
5992 //
5993 // Free unused sets of choices
5994 wxPGHashMapS2P::iterator it;
5995
5996 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
5997 {
5998 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
5999 data->DecRef();
6000 }
6001
6002 if ( m_pg )
6003 {
6004 m_pg->Thaw();
6005 m_pg->GetPanel()->Refresh();
6006 }
6007 wxPGGlobalVars->m_offline--;
6008 }
6009
6010 // -----------------------------------------------------------------------
6011
6012 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
6013 const wxString& propLabel,
6014 const wxString& propName,
6015 const wxString* propValue,
6016 wxPGChoices* pChoices )
6017 {
6018 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
6019 wxPGProperty* parent = GetCurParent();
6020
6021 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
6022 {
6023 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
6024 return NULL;
6025 }
6026
6027 if ( !classInfo || !classInfo->IsKindOf(CLASSINFO(wxPGProperty)) )
6028 {
6029 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
6030 return NULL;
6031 }
6032
6033 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
6034
6035 property->SetLabel(propLabel);
6036 property->DoSetName(propName);
6037
6038 if ( pChoices && pChoices->IsOk() )
6039 property->SetChoices(*pChoices);
6040
6041 m_state->DoInsert(parent, -1, property);
6042
6043 if ( propValue )
6044 property->SetValueFromString( *propValue, wxPG_FULL_VALUE|
6045 wxPG_PROGRAMMATIC_VALUE );
6046
6047 return property;
6048 }
6049
6050 // -----------------------------------------------------------------------
6051
6052 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
6053 {
6054 m_propHierarchy.push_back(property);
6055 DoScanForChildren();
6056 m_propHierarchy.pop_back();
6057 }
6058
6059 // -----------------------------------------------------------------------
6060
6061 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
6062 const wxString& idString )
6063 {
6064 wxPGChoices choices;
6065
6066 // Using id?
6067 if ( choicesString[0] == wxT('@') )
6068 {
6069 wxString ids = choicesString.substr(1);
6070 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
6071 if ( it == m_dictIdChoices.end() )
6072 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
6073 else
6074 choices.AssignData((wxPGChoicesData*)it->second);
6075 }
6076 else
6077 {
6078 bool found = false;
6079 if ( idString.length() )
6080 {
6081 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
6082 if ( it != m_dictIdChoices.end() )
6083 {
6084 choices.AssignData((wxPGChoicesData*)it->second);
6085 found = true;
6086 }
6087 }
6088
6089 if ( !found )
6090 {
6091 // Parse choices string
6092 wxString::const_iterator it = choicesString.begin();
6093 wxString label;
6094 wxString value;
6095 int state = 0;
6096 bool labelValid = false;
6097
6098 for ( ; it != choicesString.end(); ++it )
6099 {
6100 wxChar c = *it;
6101
6102 if ( state != 1 )
6103 {
6104 if ( c == wxT('"') )
6105 {
6106 if ( labelValid )
6107 {
6108 long l;
6109 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6110 choices.Add(label, l);
6111 }
6112 labelValid = false;
6113 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
6114 value.clear();
6115 label.clear();
6116 state = 1;
6117 }
6118 else if ( c == wxT('=') )
6119 {
6120 if ( labelValid )
6121 {
6122 state = 2;
6123 }
6124 }
6125 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
6126 {
6127 value << c;
6128 }
6129 }
6130 else
6131 {
6132 if ( c == wxT('"') )
6133 {
6134 state = 0;
6135 labelValid = true;
6136 }
6137 else
6138 label << c;
6139 }
6140 }
6141
6142 if ( labelValid )
6143 {
6144 long l;
6145 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6146 choices.Add(label, l);
6147 }
6148
6149 if ( !choices.IsOk() )
6150 {
6151 choices.EnsureData();
6152 }
6153
6154 // Assign to id
6155 if ( idString.length() )
6156 m_dictIdChoices[idString] = choices.GetData();
6157 }
6158 }
6159
6160 return choices;
6161 }
6162
6163 // -----------------------------------------------------------------------
6164
6165 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
6166 {
6167 if ( s.Last() == wxT('%') )
6168 {
6169 wxString s2 = s.substr(0,s.length()-1);
6170 long val;
6171 if ( s2.ToLong(&val, 10) )
6172 {
6173 *pval = (val*max)/100;
6174 return true;
6175 }
6176 return false;
6177 }
6178
6179 return s.ToLong(pval, 10);
6180 }
6181
6182 // -----------------------------------------------------------------------
6183
6184 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
6185 const wxString& type,
6186 const wxString& value )
6187 {
6188 int l = m_propHierarchy.size();
6189 if ( !l )
6190 return false;
6191
6192 wxPGProperty* p = m_propHierarchy[l-1];
6193 wxString valuel = value.Lower();
6194 wxVariant variant;
6195
6196 if ( type.length() == 0 )
6197 {
6198 long v;
6199
6200 // Auto-detect type
6201 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6202 variant = true;
6203 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6204 variant = false;
6205 else if ( value.ToLong(&v, 0) )
6206 variant = v;
6207 else
6208 variant = value;
6209 }
6210 else
6211 {
6212 if ( type == wxT("string") )
6213 {
6214 variant = value;
6215 }
6216 else if ( type == wxT("int") )
6217 {
6218 long v = 0;
6219 value.ToLong(&v, 0);
6220 variant = v;
6221 }
6222 else if ( type == wxT("bool") )
6223 {
6224 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6225 variant = true;
6226 else
6227 variant = false;
6228 }
6229 else
6230 {
6231 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6232 return false;
6233 }
6234 }
6235
6236 p->SetAttribute( name, variant );
6237
6238 return true;
6239 }
6240
6241 // -----------------------------------------------------------------------
6242
6243 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6244 {
6245 wxLogError(_("Error in resource: %s"),msg.c_str());
6246 }
6247
6248 // -----------------------------------------------------------------------
6249
6250 #endif // wxUSE_PROPGRID