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