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