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