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