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