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