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