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