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