]> git.saurik.com Git - wxWidgets.git/blob - src/propgrid/propgrid.cpp
Connect wxEVT_KEY_DOWN for both editor controls
[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 }
3432
3433 argWnd->Connect(id, wxEVT_KEY_DOWN,
3434 wxCharEventHandler(wxPropertyGrid::OnChildKeyDown),
3435 NULL, this);
3436 }
3437
3438 void wxPropertyGrid::FreeEditors()
3439 {
3440 //
3441 // Return focus back to canvas from children (this is required at least for
3442 // GTK+, which, unlike Windows, clears focus when control is destroyed
3443 // instead of moving it to closest parent).
3444 wxWindow* focus = wxWindow::FindFocus();
3445 if ( focus )
3446 {
3447 wxWindow* parent = focus->GetParent();
3448 while ( parent )
3449 {
3450 if ( parent == m_canvas )
3451 {
3452 SetFocusOnCanvas();
3453 break;
3454 }
3455 parent = parent->GetParent();
3456 }
3457 }
3458
3459 // Do not free editors immediately if processing events
3460 if ( m_wndEditor2 )
3461 {
3462 m_wndEditor2->Hide();
3463 wxPendingDelete.Append( m_wndEditor2 );
3464 m_wndEditor2 = (wxWindow*) NULL;
3465 }
3466
3467 if ( m_wndEditor )
3468 {
3469 m_wndEditor->Hide();
3470 wxPendingDelete.Append( m_wndEditor );
3471 m_wndEditor = (wxWindow*) NULL;
3472 }
3473 }
3474
3475 // Call with NULL to de-select property
3476 bool wxPropertyGrid::DoSelectProperty( wxPGProperty* p, unsigned int flags )
3477 {
3478 wxPanel* canvas = GetPanel();
3479
3480 /*
3481 if (p)
3482 wxLogDebug(wxT("SelectProperty( %s (%s[%i]) )"),p->m_label.c_str(),
3483 p->m_parent->m_label.c_str(),p->GetIndexInParent());
3484 else
3485 wxLogDebug(wxT("SelectProperty( NULL, -1 )"));
3486 */
3487
3488 if ( m_inDoSelectProperty )
3489 return true;
3490
3491 m_inDoSelectProperty = 1;
3492
3493 wxPGProperty* prev = m_selected;
3494
3495 if ( !m_pState )
3496 {
3497 m_inDoSelectProperty = 0;
3498 return false;
3499 }
3500
3501 /*
3502 if (m_selected)
3503 wxPrintf( "Selected %s\n", m_selected->GetClassInfo()->GetClassName() );
3504 else
3505 wxPrintf( "None selected\n" );
3506
3507 if (p)
3508 wxPrintf( "P = %s\n", p->GetClassInfo()->GetClassName() );
3509 else
3510 wxPrintf( "P = NULL\n" );
3511 */
3512
3513 // If we are frozen, then just set the values.
3514 if ( m_frozen )
3515 {
3516 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3517 m_editorFocused = 0;
3518 m_selected = p;
3519 m_selColumn = 1;
3520 m_pState->m_selected = p;
3521
3522 // If frozen, always free controls. But don't worry, as Thaw will
3523 // recall SelectProperty to recreate them.
3524 FreeEditors();
3525
3526 // Prevent any further selection measures in this call
3527 p = (wxPGProperty*) NULL;
3528 }
3529 else
3530 {
3531 // Is it the same?
3532 if ( m_selected == p && !(flags & wxPG_SEL_FORCE) )
3533 {
3534 // Only set focus if not deselecting
3535 if ( p )
3536 {
3537 if ( flags & wxPG_SEL_FOCUS )
3538 {
3539 if ( m_wndEditor )
3540 {
3541 m_wndEditor->SetFocus();
3542 m_editorFocused = 1;
3543 }
3544 }
3545 else
3546 {
3547 SetFocusOnCanvas();
3548 }
3549 }
3550
3551 m_inDoSelectProperty = 0;
3552 return true;
3553 }
3554
3555 //
3556 // First, deactivate previous
3557 if ( m_selected )
3558 {
3559
3560 OnValidationFailureReset(m_selected);
3561
3562 // Must double-check if this is an selected in case of forceswitch
3563 if ( p != prev )
3564 {
3565 if ( !CommitChangesFromEditor(flags) )
3566 {
3567 // Validation has failed, so we can't exit the previous editor
3568 //::wxMessageBox(_("Please correct the value or press ESC to cancel the edit."),
3569 // _("Invalid Value"),wxOK|wxICON_ERROR);
3570 m_inDoSelectProperty = 0;
3571 return false;
3572 }
3573 }
3574
3575 FreeEditors();
3576 m_selColumn = -1;
3577
3578 m_selected = (wxPGProperty*) NULL;
3579 m_pState->m_selected = (wxPGProperty*) NULL;
3580
3581 // We need to always fully refresh the grid here
3582 Refresh(false);
3583
3584 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3585 EditorsValueWasNotModified();
3586 }
3587
3588 SetInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3589
3590 //
3591 // Then, activate the one given.
3592 if ( p )
3593 {
3594 int propY = p->GetY2(m_lineHeight);
3595
3596 int splitterX = GetSplitterPosition();
3597 m_editorFocused = 0;
3598 m_selected = p;
3599 m_pState->m_selected = p;
3600 m_iFlags |= wxPG_FL_PRIMARY_FILLS_ENTIRE;
3601 if ( p != prev )
3602 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED);
3603
3604 wxASSERT( m_wndEditor == (wxWindow*) NULL );
3605
3606 // Do we need OnMeasureCalls?
3607 wxSize imsz = p->OnMeasureImage();
3608
3609 //
3610 // Only create editor for non-disabled non-caption
3611 if ( !p->IsCategory() && !(p->m_flags & wxPG_PROP_DISABLED) )
3612 {
3613 // do this for non-caption items
3614
3615 m_selColumn = 1;
3616
3617 // Do we need to paint the custom image, if any?
3618 m_iFlags &= ~(wxPG_FL_CUR_USES_CUSTOM_IMAGE);
3619 if ( (p->m_flags & wxPG_PROP_CUSTOMIMAGE) &&
3620 !p->GetEditorClass()->CanContainCustomImage()
3621 )
3622 m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3623
3624 wxRect grect = GetEditorWidgetRect(p, m_selColumn);
3625 wxPoint goodPos = grect.GetPosition();
3626 #if wxPG_CREATE_CONTROLS_HIDDEN
3627 int coord_adjust = m_height - goodPos.y;
3628 goodPos.y += coord_adjust;
3629 #endif
3630
3631 const wxPGEditor* editor = p->GetEditorClass();
3632 wxCHECK_MSG(editor, false,
3633 wxT("NULL editor class not allowed"));
3634
3635 m_iFlags &= ~wxPG_FL_FIXED_WIDTH_EDITOR;
3636
3637 wxPGWindowList wndList = editor->CreateControls(this,
3638 p,
3639 goodPos,
3640 grect.GetSize());
3641
3642 m_wndEditor = wndList.m_primary;
3643 m_wndEditor2 = wndList.m_secondary;
3644 wxWindow* primaryCtrl = GetEditorControl();
3645
3646 //
3647 // Essentially, primaryCtrl == m_wndEditor
3648 //
3649
3650 // NOTE: It is allowed for m_wndEditor to be NULL - in this case
3651 // value is drawn as normal, and m_wndEditor2 is assumed
3652 // to be a right-aligned button that triggers a separate editorCtrl
3653 // window.
3654
3655 if ( m_wndEditor )
3656 {
3657 wxASSERT_MSG( m_wndEditor->GetParent() == canvas,
3658 wxT("CreateControls must use result of wxPropertyGrid::GetPanel() as parent of controls.") );
3659
3660 // Set validator, if any
3661 #if wxUSE_VALIDATORS
3662 wxValidator* validator = p->GetValidator();
3663 if ( validator )
3664 primaryCtrl->SetValidator(*validator);
3665 #endif
3666
3667 if ( m_wndEditor->GetSize().y > (m_lineHeight+6) )
3668 m_iFlags |= wxPG_FL_ABNORMAL_EDITOR;
3669
3670 // If it has modified status, use bold font
3671 // (must be done before capturing m_ctrlXAdjust)
3672 if ( (p->m_flags & wxPG_PROP_MODIFIED) && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3673 SetCurControlBoldFont();
3674
3675 //
3676 // Fix TextCtrl indentation
3677 #if defined(__WXMSW__) && !defined(__WXWINCE__)
3678 wxTextCtrl* tc = NULL;
3679 if ( primaryCtrl->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
3680 tc = ((wxOwnerDrawnComboBox*)primaryCtrl)->GetTextCtrl();
3681 else
3682 tc = wxDynamicCast(primaryCtrl, wxTextCtrl);
3683 if ( tc )
3684 ::SendMessage(GetHwndOf(tc), EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELONG(0, 0));
3685 #endif
3686
3687 // Store x relative to splitter (we'll need it).
3688 m_ctrlXAdjust = m_wndEditor->GetPosition().x - splitterX;
3689
3690 // Check if background clear is not necessary
3691 wxPoint pos = m_wndEditor->GetPosition();
3692 if ( pos.x > (splitterX+1) || pos.y > propY )
3693 {
3694 m_iFlags &= ~(wxPG_FL_PRIMARY_FILLS_ENTIRE);
3695 }
3696
3697 m_wndEditor->SetSizeHints(3, 3);
3698
3699 #if wxPG_CREATE_CONTROLS_HIDDEN
3700 m_wndEditor->Show(false);
3701 m_wndEditor->Freeze();
3702
3703 goodPos = m_wndEditor->GetPosition();
3704 goodPos.y -= coord_adjust;
3705 m_wndEditor->Move( goodPos );
3706 #endif
3707
3708 SetupChildEventHandling(primaryCtrl);
3709
3710 // Focus and select all (wxTextCtrl, wxComboBox etc)
3711 if ( flags & wxPG_SEL_FOCUS )
3712 {
3713 primaryCtrl->SetFocus();
3714
3715 p->GetEditorClass()->OnFocus(p, primaryCtrl);
3716 }
3717 }
3718
3719 if ( m_wndEditor2 )
3720 {
3721 wxASSERT_MSG( m_wndEditor2->GetParent() == canvas,
3722 wxT("CreateControls must use result of wxPropertyGrid::GetPanel() as parent of controls.") );
3723
3724 // Get proper id for wndSecondary
3725 m_wndSecId = m_wndEditor2->GetId();
3726 wxWindowList children = m_wndEditor2->GetChildren();
3727 wxWindowList::iterator node = children.begin();
3728 if ( node != children.end() )
3729 m_wndSecId = ((wxWindow*)*node)->GetId();
3730
3731 m_wndEditor2->SetSizeHints(3,3);
3732
3733 #if wxPG_CREATE_CONTROLS_HIDDEN
3734 wxRect sec_rect = m_wndEditor2->GetRect();
3735 sec_rect.y -= coord_adjust;
3736
3737 // Fine tuning required to fix "oversized"
3738 // button disappearance bug.
3739 if ( sec_rect.y < 0 )
3740 {
3741 sec_rect.height += sec_rect.y;
3742 sec_rect.y = 0;
3743 }
3744 m_wndEditor2->SetSize( sec_rect );
3745 #endif
3746 m_wndEditor2->Show();
3747
3748 SetupChildEventHandling(m_wndEditor2);
3749
3750 // If no primary editor, focus to button to allow
3751 // it to interprete ENTER etc.
3752 // NOTE: Due to problems focusing away from it, this
3753 // has been disabled.
3754 /*
3755 if ( (flags & wxPG_SEL_FOCUS) && !m_wndEditor )
3756 m_wndEditor2->SetFocus();
3757 */
3758 }
3759
3760 if ( flags & wxPG_SEL_FOCUS )
3761 m_editorFocused = 1;
3762
3763 }
3764 else
3765 {
3766 // Make sure focus is in grid canvas (important for wxGTK, at least)
3767 SetFocusOnCanvas();
3768 }
3769
3770 EditorsValueWasNotModified();
3771
3772 // If it's inside collapsed section, expand parent, scroll, etc.
3773 // Also, if it was partially visible, scroll it into view.
3774 if ( !(flags & wxPG_SEL_NONVISIBLE) )
3775 EnsureVisible( p );
3776
3777 if ( m_wndEditor )
3778 {
3779 #if wxPG_CREATE_CONTROLS_HIDDEN
3780 m_wndEditor->Thaw();
3781 #endif
3782 m_wndEditor->Show(true);
3783 }
3784
3785 DrawItems(p, p);
3786 }
3787 else
3788 {
3789 // Make sure focus is in grid canvas
3790 SetFocusOnCanvas();
3791 }
3792
3793 ClearInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3794 }
3795
3796 #if wxUSE_STATUSBAR
3797
3798 //
3799 // Show help text in status bar.
3800 // (if found and grid not embedded in manager with help box and
3801 // style wxPG_EX_HELP_AS_TOOLTIPS is not used).
3802 //
3803
3804 if ( !(GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS) )
3805 {
3806 wxStatusBar* statusbar = (wxStatusBar*) NULL;
3807 if ( !(m_iFlags & wxPG_FL_NOSTATUSBARHELP) )
3808 {
3809 wxFrame* frame = wxDynamicCast(::wxGetTopLevelParent(this),wxFrame);
3810 if ( frame )
3811 statusbar = frame->GetStatusBar();
3812 }
3813
3814 if ( statusbar )
3815 {
3816 const wxString* pHelpString = (const wxString*) NULL;
3817
3818 if ( p )
3819 {
3820 pHelpString = &p->GetHelpString();
3821 if ( pHelpString->length() )
3822 {
3823 // Set help box text.
3824 statusbar->SetStatusText( *pHelpString );
3825 m_iFlags |= wxPG_FL_STRING_IN_STATUSBAR;
3826 }
3827 }
3828
3829 if ( (!pHelpString || !pHelpString->length()) &&
3830 (m_iFlags & wxPG_FL_STRING_IN_STATUSBAR) )
3831 {
3832 // Clear help box - but only if it was written
3833 // by us at previous time.
3834 statusbar->SetStatusText( m_emptyString );
3835 m_iFlags &= ~(wxPG_FL_STRING_IN_STATUSBAR);
3836 }
3837 }
3838 }
3839 #endif
3840
3841 m_inDoSelectProperty = 0;
3842
3843 // call wx event handler (here so that it also occurs on deselection)
3844 SendEvent( wxEVT_PG_SELECTED, m_selected, NULL, flags );
3845
3846 return true;
3847 }
3848
3849 // -----------------------------------------------------------------------
3850
3851 bool wxPropertyGrid::UnfocusEditor()
3852 {
3853 if ( !m_selected || !m_wndEditor || m_frozen )
3854 return true;
3855
3856 if ( !CommitChangesFromEditor(0) )
3857 return false;
3858
3859 SetFocusOnCanvas();
3860 DrawItem(m_selected);
3861
3862 return true;
3863 }
3864
3865 // -----------------------------------------------------------------------
3866
3867 // This method is not inline because it called dozens of times
3868 // (i.e. two-arg function calls create smaller code size).
3869 bool wxPropertyGrid::DoClearSelection()
3870 {
3871 return DoSelectProperty((wxPGProperty*)NULL);
3872 }
3873
3874 // -----------------------------------------------------------------------
3875 // wxPropertyGrid expand/collapse state
3876 // -----------------------------------------------------------------------
3877
3878 bool wxPropertyGrid::DoCollapse( wxPGProperty* p, bool sendEvents )
3879 {
3880 wxPGProperty* pwc = wxStaticCast(p, wxPGProperty);
3881
3882 // If active editor was inside collapsed section, then disable it
3883 if ( m_selected && m_selected->IsSomeParent (p) )
3884 {
3885 if ( !ClearSelection() )
3886 return false;
3887 }
3888
3889 // Store dont-center-splitter flag 'cause we need to temporarily set it
3890 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
3891 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
3892
3893 bool res = m_pState->DoCollapse(pwc);
3894
3895 if ( res )
3896 {
3897 if ( sendEvents )
3898 SendEvent( wxEVT_PG_ITEM_COLLAPSED, p );
3899
3900 RecalculateVirtualSize();
3901
3902 // Redraw etc. only if collapsed was visible.
3903 if (pwc->IsVisible() &&
3904 !m_frozen &&
3905 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) ) )
3906 {
3907 // When item is collapsed so that scrollbar would move,
3908 // graphics mess is about (unless we redraw everything).
3909 Refresh();
3910 }
3911 }
3912
3913 // Clear dont-center-splitter flag if it wasn't set
3914 m_iFlags = (m_iFlags & ~wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
3915
3916 return res;
3917 }
3918
3919 // -----------------------------------------------------------------------
3920
3921 bool wxPropertyGrid::DoExpand( wxPGProperty* p, bool sendEvents )
3922 {
3923 wxCHECK_MSG( p, false, wxT("invalid property id") );
3924
3925 wxPGProperty* pwc = (wxPGProperty*)p;
3926
3927 // Store dont-center-splitter flag 'cause we need to temporarily set it
3928 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
3929 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
3930
3931 bool res = m_pState->DoExpand(pwc);
3932
3933 if ( res )
3934 {
3935 if ( sendEvents )
3936 SendEvent( wxEVT_PG_ITEM_EXPANDED, p );
3937
3938 RecalculateVirtualSize();
3939
3940 // Redraw etc. only if expanded was visible.
3941 if ( pwc->IsVisible() && !m_frozen &&
3942 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) )
3943 )
3944 {
3945 // Redraw
3946 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
3947 Refresh();
3948 #else
3949 DrawItems(pwc, NULL);
3950 #endif
3951 }
3952 }
3953
3954 // Clear dont-center-splitter flag if it wasn't set
3955 m_iFlags = m_iFlags & ~(wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
3956
3957 return res;
3958 }
3959
3960 // -----------------------------------------------------------------------
3961
3962 bool wxPropertyGrid::DoHideProperty( wxPGProperty* p, bool hide, int flags )
3963 {
3964 if ( m_frozen )
3965 return m_pState->DoHideProperty(p, hide, flags);
3966
3967 if ( m_selected &&
3968 ( m_selected == p || m_selected->IsSomeParent(p) )
3969 )
3970 {
3971 if ( !ClearSelection() )
3972 return false;
3973 }
3974
3975 m_pState->DoHideProperty(p, hide, flags);
3976
3977 RecalculateVirtualSize();
3978 Refresh();
3979
3980 return true;
3981 }
3982
3983
3984 // -----------------------------------------------------------------------
3985 // wxPropertyGrid size related methods
3986 // -----------------------------------------------------------------------
3987
3988 void wxPropertyGrid::RecalculateVirtualSize( int forceXPos )
3989 {
3990 if ( (m_iFlags & wxPG_FL_RECALCULATING_VIRTUAL_SIZE) || m_frozen )
3991 return;
3992
3993 //
3994 // If virtual height was changed, then recalculate editor control position(s)
3995 if ( m_pState->m_vhCalcPending )
3996 CorrectEditorWidgetPosY();
3997
3998 m_pState->EnsureVirtualHeight();
3999
4000 #ifdef __WXDEBUG__
4001 int by1 = m_pState->GetVirtualHeight();
4002 int by2 = m_pState->GetActualVirtualHeight();
4003 if ( by1 != by2 )
4004 {
4005 wxString s = wxString::Format(wxT("VirtualHeight=%i, ActualVirtualHeight=%i, should match!"), by1, by2);
4006 wxASSERT_MSG( false,
4007 s.c_str() );
4008 wxLogDebug(s);
4009 }
4010 #endif
4011
4012 m_iFlags |= wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4013
4014 int x = m_pState->m_width;
4015 int y = m_pState->m_virtualHeight;
4016
4017 int width, height;
4018 GetClientSize(&width,&height);
4019
4020 // Now adjust virtual size.
4021 SetVirtualSize(x, y);
4022
4023 int xAmount = 0;
4024 int xPos = 0;
4025
4026 //
4027 // Adjust scrollbars
4028 if ( HasVirtualWidth() )
4029 {
4030 xAmount = x/wxPG_PIXELS_PER_UNIT;
4031 xPos = GetScrollPos( wxHORIZONTAL );
4032 }
4033
4034 if ( forceXPos != -1 )
4035 xPos = forceXPos;
4036 // xPos too high?
4037 else if ( xPos > (xAmount-(width/wxPG_PIXELS_PER_UNIT)) )
4038 xPos = 0;
4039
4040 int yAmount = (y+wxPG_PIXELS_PER_UNIT+2)/wxPG_PIXELS_PER_UNIT;
4041 int yPos = GetScrollPos( wxVERTICAL );
4042
4043 SetScrollbars( wxPG_PIXELS_PER_UNIT, wxPG_PIXELS_PER_UNIT,
4044 xAmount, yAmount, xPos, yPos, true );
4045
4046 // Must re-get size now
4047 GetClientSize(&width,&height);
4048
4049 if ( !HasVirtualWidth() )
4050 {
4051 m_pState->SetVirtualWidth(width);
4052 x = width;
4053 }
4054
4055 m_width = width;
4056 m_height = height;
4057
4058 m_canvas->SetSize( x, y );
4059
4060 m_pState->CheckColumnWidths();
4061
4062 if ( m_selected )
4063 CorrectEditorWidgetSizeX();
4064
4065 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4066 }
4067
4068 // -----------------------------------------------------------------------
4069
4070 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4071 {
4072 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4073 return;
4074
4075 int width, height;
4076 GetClientSize(&width,&height);
4077
4078 m_width = width;
4079 m_height = height;
4080
4081 #if wxPG_DOUBLE_BUFFER
4082 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4083 {
4084 int dblh = (m_lineHeight*2);
4085 if ( !m_doubleBuffer )
4086 {
4087 // Create double buffer bitmap to draw on, if none
4088 int w = (width>250)?width:250;
4089 int h = height + dblh;
4090 h = (h>400)?h:400;
4091 m_doubleBuffer = new wxBitmap( w, h );
4092 }
4093 else
4094 {
4095 int w = m_doubleBuffer->GetWidth();
4096 int h = m_doubleBuffer->GetHeight();
4097
4098 // Double buffer must be large enough
4099 if ( w < width || h < (height+dblh) )
4100 {
4101 if ( w < width ) w = width;
4102 if ( h < (height+dblh) ) h = height + dblh;
4103 delete m_doubleBuffer;
4104 m_doubleBuffer = new wxBitmap( w, h );
4105 }
4106 }
4107 }
4108
4109 #endif
4110
4111 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4112 m_ncWidth = event.GetSize().x;
4113
4114 if ( !m_frozen )
4115 {
4116 if ( m_pState->m_itemsAdded )
4117 PrepareAfterItemsAdded();
4118 else
4119 // Without this, virtual size (atleast under wxGTK) will be skewed
4120 RecalculateVirtualSize();
4121
4122 Refresh();
4123 }
4124 }
4125
4126 // -----------------------------------------------------------------------
4127
4128 void wxPropertyGrid::SetVirtualWidth( int width )
4129 {
4130 if ( width == -1 )
4131 {
4132 // Disable virtual width
4133 width = GetClientSize().x;
4134 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4135 }
4136 else
4137 {
4138 // Enable virtual width
4139 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4140 }
4141 m_pState->SetVirtualWidth( width );
4142 }
4143
4144 void wxPropertyGrid::SetFocusOnCanvas()
4145 {
4146 m_canvas->SetFocusIgnoringChildren();
4147 m_editorFocused = 0;
4148 }
4149
4150 // -----------------------------------------------------------------------
4151 // wxPropertyGrid mouse event handling
4152 // -----------------------------------------------------------------------
4153
4154 // selFlags uses same values DoSelectProperty's flags
4155 // Returns true if event was vetoed.
4156 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p, wxVariant* pValue, unsigned int WXUNUSED(selFlags) )
4157 {
4158 // Send property grid event of specific type and with specific property
4159 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4160 evt.SetPropertyGrid(this);
4161 evt.SetEventObject(m_eventObject);
4162 evt.SetProperty(p);
4163 if ( pValue )
4164 {
4165 evt.SetCanVeto(true);
4166 evt.SetupValidationInfo();
4167 m_validationInfo.m_pValue = pValue;
4168 }
4169 wxEvtHandler* evtHandler = m_eventObject->GetEventHandler();
4170
4171 evtHandler->ProcessEvent(evt);
4172
4173 return evt.WasVetoed();
4174 }
4175
4176 // -----------------------------------------------------------------------
4177
4178 // Return false if should be skipped
4179 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4180 {
4181 bool res = true;
4182
4183 // Need to set focus?
4184 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4185 {
4186 SetFocusOnCanvas();
4187 }
4188
4189 wxPropertyGridPageState* state = m_pState;
4190 int splitterHit;
4191 int splitterHitOffset;
4192 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4193
4194 wxPGProperty* p = DoGetItemAtY(y);
4195
4196 if ( p )
4197 {
4198 int depth = (int)p->GetDepth() - 1;
4199
4200 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4201
4202 if ( x >= marginEnds )
4203 {
4204 // Outside margin.
4205
4206 if ( p->IsCategory() )
4207 {
4208 // This is category.
4209 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4210
4211 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4212
4213 // Expand, collapse, activate etc. if click on text or left of splitter.
4214 if ( x >= textX
4215 &&
4216 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4217 columnHit == 0
4218 )
4219 )
4220 {
4221 if ( !DoSelectProperty( p ) )
4222 return res;
4223
4224 // On double-click, expand/collapse.
4225 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4226 {
4227 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4228 else DoExpand( p, true );
4229 }
4230 }
4231 }
4232 else if ( splitterHit == -1 )
4233 {
4234 // Click on value.
4235 unsigned int selFlag = 0;
4236 if ( columnHit == 1 )
4237 {
4238 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4239 selFlag = wxPG_SEL_FOCUS;
4240 }
4241 if ( !DoSelectProperty( p, selFlag ) )
4242 return res;
4243
4244 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4245
4246 if ( p->GetChildCount() && !p->IsCategory() )
4247 // On double-click, expand/collapse.
4248 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4249 {
4250 wxPGProperty* pwc = (wxPGProperty*)p;
4251 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4252 else DoExpand( p, true );
4253 }
4254
4255 res = false;
4256 }
4257 else
4258 {
4259 // click on splitter
4260 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4261 {
4262 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4263 {
4264 // Double-clicking the splitter causes auto-centering
4265 CenterSplitter( true );
4266 }
4267 else if ( m_dragStatus == 0 )
4268 {
4269 //
4270 // Begin draggin the splitter
4271 //
4272 if ( m_wndEditor )
4273 {
4274 // Changes must be committed here or the
4275 // value won't be drawn correctly
4276 if ( !CommitChangesFromEditor() )
4277 return res;
4278
4279 m_wndEditor->Show ( false );
4280 }
4281
4282 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4283 {
4284 m_canvas->CaptureMouse();
4285 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4286 }
4287
4288 m_dragStatus = 1;
4289 m_draggedSplitter = splitterHit;
4290 m_dragOffset = splitterHitOffset;
4291
4292 wxClientDC dc(m_canvas);
4293
4294 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4295 // Fixes button disappearance bug
4296 if ( m_wndEditor2 )
4297 m_wndEditor2->Show ( false );
4298 #endif
4299
4300 m_startingSplitterX = x - splitterHitOffset;
4301 }
4302 }
4303 }
4304 }
4305 else
4306 {
4307 // Click on margin.
4308 if ( p->GetChildCount() )
4309 {
4310 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4311
4312 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4313 {
4314 int y2 = y % m_lineHeight;
4315 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4316 {
4317 // On click on expander button, expand/collapse
4318 if ( ((wxPGProperty*)p)->IsExpanded() )
4319 DoCollapse( p, true );
4320 else
4321 DoExpand( p, true );
4322 }
4323 }
4324 }
4325 }
4326 }
4327 return res;
4328 }
4329
4330 // -----------------------------------------------------------------------
4331
4332 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x), unsigned int WXUNUSED(y),
4333 wxMouseEvent& WXUNUSED(event) )
4334 {
4335 if ( m_propHover )
4336 {
4337 // Select property here as well
4338 wxPGProperty* p = m_propHover;
4339 if ( p != m_selected )
4340 DoSelectProperty( p );
4341
4342 // Send right click event.
4343 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4344
4345 return true;
4346 }
4347 return false;
4348 }
4349
4350 // -----------------------------------------------------------------------
4351
4352 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x), unsigned int WXUNUSED(y),
4353 wxMouseEvent& WXUNUSED(event) )
4354 {
4355 if ( m_propHover )
4356 {
4357 // Select property here as well
4358 wxPGProperty* p = m_propHover;
4359
4360 if ( p != m_selected )
4361 DoSelectProperty( p );
4362
4363 // Send double-click event.
4364 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4365
4366 return true;
4367 }
4368 return false;
4369 }
4370
4371 // -----------------------------------------------------------------------
4372
4373 #if wxPG_SUPPORT_TOOLTIPS
4374
4375 void wxPropertyGrid::SetToolTip( const wxString& tipString )
4376 {
4377 if ( tipString.length() )
4378 {
4379 m_canvas->SetToolTip(tipString);
4380 }
4381 else
4382 {
4383 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4384 m_canvas->SetToolTip( m_emptyString );
4385 #else
4386 m_canvas->SetToolTip( NULL );
4387 #endif
4388 }
4389 }
4390
4391 #endif // #if wxPG_SUPPORT_TOOLTIPS
4392
4393 // -----------------------------------------------------------------------
4394
4395 // Return false if should be skipped
4396 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y, wxMouseEvent &event )
4397 {
4398 // Safety check (needed because mouse capturing may
4399 // otherwise freeze the control)
4400 if ( m_dragStatus > 0 && !event.Dragging() )
4401 {
4402 HandleMouseUp(x,y,event);
4403 }
4404
4405 wxPropertyGridPageState* state = m_pState;
4406 int splitterHit;
4407 int splitterHitOffset;
4408 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4409 int splitterX = x - splitterHitOffset;
4410
4411 if ( m_dragStatus > 0 )
4412 {
4413 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4414 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4415 {
4416
4417 int newSplitterX = x - m_dragOffset;
4418 int splitterX = x - splitterHitOffset;
4419
4420 // Splitter redraw required?
4421 if ( newSplitterX != splitterX )
4422 {
4423 // Move everything
4424 SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
4425 state->DoSetSplitterPosition( newSplitterX, m_draggedSplitter, false );
4426 state->m_fSplitterX = (float) newSplitterX;
4427
4428 if ( m_selected )
4429 CorrectEditorWidgetSizeX();
4430
4431 Update();
4432 Refresh();
4433 }
4434
4435 m_dragStatus = 2;
4436 }
4437
4438 return false;
4439 }
4440 else
4441 {
4442
4443 int ih = m_lineHeight;
4444 int sy = y;
4445
4446 #if wxPG_SUPPORT_TOOLTIPS
4447 wxPGProperty* prevHover = m_propHover;
4448 unsigned char prevSide = m_mouseSide;
4449 #endif
4450 int curPropHoverY = y - (y % ih);
4451
4452 // On which item it hovers
4453 if ( ( !m_propHover )
4454 ||
4455 ( m_propHover && ( sy < m_propHoverY || sy >= (m_propHoverY+ih) ) )
4456 )
4457 {
4458 // Mouse moves on another property
4459
4460 m_propHover = DoGetItemAtY(y);
4461 m_propHoverY = curPropHoverY;
4462
4463 // Send hover event
4464 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
4465 }
4466
4467 #if wxPG_SUPPORT_TOOLTIPS
4468 // Store which side we are on
4469 m_mouseSide = 0;
4470 if ( columnHit == 1 )
4471 m_mouseSide = 2;
4472 else if ( columnHit == 0 )
4473 m_mouseSide = 1;
4474
4475 //
4476 // If tooltips are enabled, show label or value as a tip
4477 // in case it doesn't otherwise show in full length.
4478 //
4479 if ( m_windowStyle & wxPG_TOOLTIPS )
4480 {
4481 wxToolTip* tooltip = m_canvas->GetToolTip();
4482
4483 if ( m_propHover != prevHover || prevSide != m_mouseSide )
4484 {
4485 if ( m_propHover && !m_propHover->IsCategory() )
4486 {
4487
4488 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
4489 {
4490 // Show help string as a tooltip
4491 wxString tipString = m_propHover->GetHelpString();
4492
4493 SetToolTip(tipString);
4494 }
4495 else
4496 {
4497 // Show cropped value string as a tooltip
4498 wxString tipString;
4499 int space = 0;
4500
4501 if ( m_mouseSide == 1 )
4502 {
4503 tipString = m_propHover->m_label;
4504 space = splitterX-m_marginWidth-3;
4505 }
4506 else if ( m_mouseSide == 2 )
4507 {
4508 tipString = m_propHover->GetDisplayedString();
4509
4510 space = m_width - splitterX;
4511 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
4512 space -= wxPG_CUSTOM_IMAGE_WIDTH + wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
4513 }
4514
4515 if ( space )
4516 {
4517 int tw, th;
4518 GetTextExtent( tipString, &tw, &th, 0, 0, &m_font );
4519 if ( tw > space )
4520 {
4521 SetToolTip( tipString );
4522 }
4523 }
4524 else
4525 {
4526 if ( tooltip )
4527 {
4528 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4529 m_canvas->SetToolTip( m_emptyString );
4530 #else
4531 m_canvas->SetToolTip( NULL );
4532 #endif
4533 }
4534 }
4535
4536 }
4537 }
4538 else
4539 {
4540 if ( tooltip )
4541 {
4542 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4543 m_canvas->SetToolTip( m_emptyString );
4544 #else
4545 m_canvas->SetToolTip( NULL );
4546 #endif
4547 }
4548 }
4549 }
4550 }
4551 #endif
4552
4553 if ( splitterHit == -1 ||
4554 !m_propHover ||
4555 HasFlag(wxPG_STATIC_SPLITTER) )
4556 {
4557 // hovering on something else
4558 if ( m_curcursor != wxCURSOR_ARROW )
4559 CustomSetCursor( wxCURSOR_ARROW );
4560 }
4561 else
4562 {
4563 // Do not allow splitter cursor on caption items.
4564 // (also not if we were dragging and its started
4565 // outside the splitter region)
4566
4567 if ( m_propHover &&
4568 !m_propHover->IsCategory() &&
4569 !event.Dragging() )
4570 {
4571
4572 // hovering on splitter
4573
4574 // NB: Condition disabled since MouseLeave event (from the editor control) cannot be
4575 // reliably detected.
4576 //if ( m_curcursor != wxCURSOR_SIZEWE )
4577 CustomSetCursor( wxCURSOR_SIZEWE, true );
4578
4579 return false;
4580 }
4581 else
4582 {
4583 // hovering on something else
4584 if ( m_curcursor != wxCURSOR_ARROW )
4585 CustomSetCursor( wxCURSOR_ARROW );
4586 }
4587 }
4588 }
4589 return true;
4590 }
4591
4592 // -----------------------------------------------------------------------
4593
4594 // Also handles Leaving event
4595 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
4596 wxMouseEvent &WXUNUSED(event) )
4597 {
4598 wxPropertyGridPageState* state = m_pState;
4599 bool res = false;
4600
4601 int splitterHit;
4602 int splitterHitOffset;
4603 state->HitTestH( x, &splitterHit, &splitterHitOffset );
4604
4605 // No event type check - basicly calling this method should
4606 // just stop dragging.
4607 // Left up after dragged?
4608 if ( m_dragStatus >= 1 )
4609 {
4610 //
4611 // End Splitter Dragging
4612 //
4613 // DO NOT ENABLE FOLLOWING LINE!
4614 // (it is only here as a reminder to not to do it)
4615 //splitterX = x;
4616
4617 // Disable splitter auto-centering
4618 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4619
4620 // This is necessary to return cursor
4621 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
4622 {
4623 m_canvas->ReleaseMouse();
4624 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
4625 }
4626
4627 // Set back the default cursor, if necessary
4628 if ( splitterHit == -1 ||
4629 !m_propHover )
4630 {
4631 CustomSetCursor( wxCURSOR_ARROW );
4632 }
4633
4634 m_dragStatus = 0;
4635
4636 // Control background needs to be cleared
4637 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && m_selected )
4638 DrawItem( m_selected );
4639
4640 if ( m_wndEditor )
4641 {
4642 m_wndEditor->Show ( true );
4643 }
4644
4645 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4646 // Fixes button disappearance bug
4647 if ( m_wndEditor2 )
4648 m_wndEditor2->Show ( true );
4649 #endif
4650
4651 // This clears the focus.
4652 m_editorFocused = 0;
4653
4654 }
4655 return res;
4656 }
4657
4658 // -----------------------------------------------------------------------
4659
4660 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
4661 {
4662 int splitterX = GetSplitterPosition();
4663
4664 //int ux, uy;
4665 //CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
4666 int ux = event.m_x;
4667 int uy = event.m_y;
4668
4669 wxWindow* wnd = GetEditorControl();
4670
4671 // Hide popup on clicks
4672 if ( event.GetEventType() != wxEVT_MOTION )
4673 if ( wnd && wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
4674 {
4675 ((wxOwnerDrawnComboBox*)wnd)->HidePopup();
4676 }
4677
4678 wxRect r;
4679 if ( wnd )
4680 r = wnd->GetRect();
4681 if ( wnd == (wxWindow*) NULL || m_dragStatus ||
4682 (
4683 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
4684 ux >= (r.x+r.width) ||
4685 event.m_y < r.y ||
4686 event.m_y >= (r.y+r.height)
4687 )
4688 )
4689 {
4690 *px = ux;
4691 *py = uy;
4692 return true;
4693 }
4694 else
4695 {
4696 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
4697 }
4698 return false;
4699 }
4700
4701 // -----------------------------------------------------------------------
4702
4703 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
4704 {
4705 int x, y;
4706 if ( OnMouseCommon( event, &x, &y ) )
4707 {
4708 HandleMouseClick(x,y,event);
4709 }
4710 event.Skip();
4711 }
4712
4713 // -----------------------------------------------------------------------
4714
4715 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
4716 {
4717 int x, y;
4718 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
4719 HandleMouseRightClick(x,y,event);
4720 event.Skip();
4721 }
4722
4723 // -----------------------------------------------------------------------
4724
4725 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
4726 {
4727 // Always run standard mouse-down handler as well
4728 OnMouseClick(event);
4729
4730 int x, y;
4731 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
4732 HandleMouseDoubleClick(x,y,event);
4733 event.Skip();
4734 }
4735
4736 // -----------------------------------------------------------------------
4737
4738 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
4739 {
4740 int x, y;
4741 if ( OnMouseCommon( event, &x, &y ) )
4742 {
4743 HandleMouseMove(x,y,event);
4744 }
4745 event.Skip();
4746 }
4747
4748 // -----------------------------------------------------------------------
4749
4750 void wxPropertyGrid::OnMouseMoveBottom( wxMouseEvent& WXUNUSED(event) )
4751 {
4752 // Called when mouse moves in the empty space below the properties.
4753 CustomSetCursor( wxCURSOR_ARROW );
4754 }
4755
4756 // -----------------------------------------------------------------------
4757
4758 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
4759 {
4760 int x, y;
4761 if ( OnMouseCommon( event, &x, &y ) )
4762 {
4763 HandleMouseUp(x,y,event);
4764 }
4765 event.Skip();
4766 }
4767
4768 // -----------------------------------------------------------------------
4769
4770 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
4771 {
4772 // This may get called from child control as well, so event's
4773 // mouse position cannot be relied on.
4774
4775 if ( event.Entering() )
4776 {
4777 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
4778 {
4779 // TODO: Fix this (detect parent and only do
4780 // cursor trick if it is a manager).
4781 wxASSERT( GetParent() );
4782 GetParent()->SetCursor(wxNullCursor);
4783
4784 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
4785 }
4786 else
4787 GetParent()->SetCursor(wxNullCursor);
4788 }
4789 else if ( event.Leaving() )
4790 {
4791 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
4792 m_canvas->SetCursor( wxNullCursor );
4793
4794 // Get real cursor position
4795 wxPoint pt = ScreenToClient(::wxGetMousePosition());
4796
4797 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
4798 {
4799 {
4800 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
4801 {
4802 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
4803 }
4804
4805 if ( m_dragStatus )
4806 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
4807 }
4808 }
4809 }
4810
4811 event.Skip();
4812 }
4813
4814 // -----------------------------------------------------------------------
4815
4816 // Common code used by various OnMouseXXXChild methods.
4817 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
4818 {
4819 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
4820 wxASSERT( topCtrlWnd );
4821 int x, y;
4822 event.GetPosition(&x,&y);
4823
4824 int splitterX = GetSplitterPosition();
4825
4826 wxRect r = topCtrlWnd->GetRect();
4827 if ( !m_dragStatus &&
4828 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
4829 y >= 0 && y < r.height \
4830 )
4831 {
4832 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
4833 event.Skip();
4834 }
4835 else
4836 {
4837 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
4838 px, py );
4839 return true;
4840 }
4841 return false;
4842 }
4843
4844 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
4845 {
4846 int x,y;
4847 if ( OnMouseChildCommon(event,&x,&y) )
4848 {
4849 bool res = HandleMouseClick(x,y,event);
4850 if ( !res ) event.Skip();
4851 }
4852 }
4853
4854 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
4855 {
4856 int x,y;
4857 wxASSERT( m_wndEditor );
4858 // These coords may not be exact (about +-2),
4859 // but that should not matter (right click is about item, not position).
4860 wxPoint pt = m_wndEditor->GetPosition();
4861 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
4862 wxASSERT( m_selected );
4863 m_propHover = m_selected;
4864 bool res = HandleMouseRightClick(x,y,event);
4865 if ( !res ) event.Skip();
4866 }
4867
4868 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
4869 {
4870 int x,y;
4871 if ( OnMouseChildCommon(event,&x,&y) )
4872 {
4873 bool res = HandleMouseMove(x,y,event);
4874 if ( !res ) event.Skip();
4875 }
4876 }
4877
4878 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
4879 {
4880 int x,y;
4881 if ( OnMouseChildCommon(event,&x,&y) )
4882 {
4883 bool res = HandleMouseUp(x,y,event);
4884 if ( !res ) event.Skip();
4885 }
4886 }
4887
4888 // -----------------------------------------------------------------------
4889 // wxPropertyGrid keyboard event handling
4890 // -----------------------------------------------------------------------
4891
4892 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
4893 {
4894 // Translates wxKeyEvent to wxPG_ACTION_XXX
4895
4896 int keycode = event.GetKeyCode();
4897 int modifiers = event.GetModifiers();
4898
4899 wxASSERT( !(modifiers&~(0xFFFF)) );
4900
4901 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
4902
4903 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
4904
4905 if ( it == m_actionTriggers.end() )
4906 return 0;
4907
4908 if ( pSecond )
4909 {
4910 int second = (it->second>>16) & 0xFFFF;
4911 *pSecond = second;
4912 }
4913
4914 return (it->second & 0xFFFF);
4915 }
4916
4917 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
4918 {
4919 wxASSERT( !(modifiers&~(0xFFFF)) );
4920
4921 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
4922
4923 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
4924
4925 if ( it != m_actionTriggers.end() )
4926 {
4927 // This key combination is already used
4928
4929 // Can add secondary?
4930 wxASSERT_MSG( !(it->second&~(0xFFFF)),
4931 wxT("You can only add up to two separate actions per key combination.") );
4932
4933 action = it->second | (action<<16);
4934 }
4935
4936 m_actionTriggers[hashMapKey] = action;
4937 }
4938
4939 void wxPropertyGrid::ClearActionTriggers( int action )
4940 {
4941 wxPGHashMapI2I::iterator it;
4942
4943 for ( it = m_actionTriggers.begin(); it != m_actionTriggers.end(); it++ )
4944 {
4945 if ( it->second == action )
4946 {
4947 m_actionTriggers.erase(it);
4948 }
4949 }
4950 }
4951
4952 void wxPropertyGrid::HandleKeyEvent( wxKeyEvent &event, bool fromChild )
4953 {
4954 //
4955 // Handles key event when editor control is not focused.
4956 //
4957
4958 wxASSERT( !m_frozen );
4959 if ( m_frozen )
4960 return;
4961
4962 // Travelsal between items, collapsing/expanding, etc.
4963 int keycode = event.GetKeyCode();
4964 bool editorFocused = IsEditorFocused();
4965
4966 if ( keycode == WXK_TAB )
4967 {
4968 if ( !event.ShiftDown() )
4969 {
4970 if ( !editorFocused && m_wndEditor )
4971 DoSelectProperty( m_selected, wxPG_SEL_FOCUS );
4972 else
4973 Navigate(wxNavigationKeyEvent::IsForward);
4974 }
4975 else
4976 {
4977 if ( editorFocused )
4978 UnfocusEditor();
4979 else
4980 Navigate(wxNavigationKeyEvent::IsBackward);
4981 }
4982
4983 return;
4984 }
4985
4986 // Ignore Alt and Control when they are down alone
4987 if ( keycode == WXK_ALT ||
4988 keycode == WXK_CONTROL )
4989 {
4990 event.Skip();
4991 return;
4992 }
4993
4994 int secondAction;
4995 int action = KeyEventToActions(event, &secondAction);
4996
4997 if ( editorFocused && action == wxPG_ACTION_CANCEL_EDIT )
4998 {
4999 //
5000 // Esc cancels any changes
5001 if ( IsEditorsValueModified() )
5002 {
5003 EditorsValueWasNotModified();
5004
5005 // Update the control as well
5006 m_selected->GetEditorClass()->SetControlStringValue( m_selected,
5007 GetEditorControl(),
5008 m_selected->GetDisplayedString() );
5009 }
5010
5011 OnValidationFailureReset(m_selected);
5012
5013 UnfocusEditor();
5014 return;
5015 }
5016
5017 // Except for TAB and ESC, handle child control events in child control
5018 if ( fromChild )
5019 {
5020 event.Skip();
5021 return;
5022 }
5023
5024 bool wasHandled = false;
5025
5026 if ( m_selected )
5027 {
5028 // Show dialog?
5029 if ( ButtonTriggerKeyTest(action, event) )
5030 return;
5031
5032 wxPGProperty* p = m_selected;
5033
5034 // Travel and expand/collapse
5035 int selectDir = -2;
5036
5037 if ( p->GetChildCount() &&
5038 !(p->m_flags & wxPG_PROP_DISABLED)
5039 )
5040 {
5041 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5042 {
5043 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5044 wasHandled = true;
5045 }
5046 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5047 {
5048 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5049 wasHandled = true;
5050 }
5051 }
5052
5053 if ( !wasHandled )
5054 {
5055 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5056 {
5057 selectDir = -1;
5058 }
5059 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5060 {
5061 selectDir = 1;
5062 }
5063 }
5064
5065 if ( selectDir >= -1 )
5066 {
5067 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5068 if ( p )
5069 DoSelectProperty(p);
5070 wasHandled = true;
5071 }
5072 }
5073 else
5074 {
5075 // If nothing was selected, select the first item now
5076 // (or navigate out of tab).
5077 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5078 {
5079 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5080 if ( p ) DoSelectProperty(p);
5081 wasHandled = true;
5082 }
5083 }
5084
5085 if ( !wasHandled )
5086 event.Skip();
5087 }
5088
5089 // -----------------------------------------------------------------------
5090
5091 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5092 {
5093 HandleKeyEvent(event, false);
5094 }
5095
5096 // -----------------------------------------------------------------------
5097
5098 bool wxPropertyGrid::ButtonTriggerKeyTest( int action, wxKeyEvent& event )
5099 {
5100 if ( action == -1 )
5101 {
5102 int secondAction;
5103 action = KeyEventToActions(event, &secondAction);
5104 }
5105
5106 // Does the keycode trigger button?
5107 if ( action == wxPG_ACTION_PRESS_BUTTON &&
5108 m_wndEditor2 )
5109 {
5110 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, m_wndEditor2->GetId());
5111 GetEventHandler()->AddPendingEvent(evt);
5112 return true;
5113 }
5114
5115 return false;
5116 }
5117
5118 // -----------------------------------------------------------------------
5119
5120 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5121 {
5122 HandleKeyEvent(event, true);
5123 }
5124
5125 // -----------------------------------------------------------------------
5126 // wxPropertyGrid miscellaneous event handling
5127 // -----------------------------------------------------------------------
5128
5129 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5130 {
5131 //
5132 // Check if the focus is in this control or one of its children
5133 wxWindow* newFocused = wxWindow::FindFocus();
5134
5135 if ( newFocused != m_curFocused )
5136 HandleFocusChange( newFocused );
5137 }
5138
5139 bool wxPropertyGrid::IsEditorFocused() const
5140 {
5141 wxWindow* focus = wxWindow::FindFocus();
5142
5143 if ( focus == m_wndEditor || focus == m_wndEditor2 ||
5144 focus == GetEditorControl() )
5145 return true;
5146
5147 return false;
5148 }
5149
5150 // Called by focus event handlers. newFocused is the window that becomes focused.
5151 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5152 {
5153 unsigned int oldFlags = m_iFlags;
5154
5155 m_iFlags &= ~(wxPG_FL_FOCUSED);
5156
5157 wxWindow* parent = newFocused;
5158
5159 // This must be one of nextFocus' parents.
5160 while ( parent )
5161 {
5162 // Use m_eventObject, which is either wxPropertyGrid or
5163 // wxPropertyGridManager, as appropriate.
5164 if ( parent == m_eventObject )
5165 {
5166 m_iFlags |= wxPG_FL_FOCUSED;
5167 break;
5168 }
5169 parent = parent->GetParent();
5170 }
5171
5172 m_curFocused = newFocused;
5173
5174 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5175 (oldFlags & wxPG_FL_FOCUSED) )
5176 {
5177 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5178 {
5179 // Need to store changed value
5180 CommitChangesFromEditor();
5181 }
5182 else
5183 {
5184 /*
5185 //
5186 // Preliminary code for tab-order respecting
5187 // tab-traversal (but should be moved to
5188 // OnNav handler)
5189 //
5190 wxWindow* prevFocus = event.GetWindow();
5191 wxWindow* useThis = this;
5192 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5193 useThis = GetParent();
5194
5195 if ( prevFocus &&
5196 prevFocus->GetParent() == useThis->GetParent() )
5197 {
5198 wxList& children = useThis->GetParent()->GetChildren();
5199
5200 wxNode* node = children.Find(prevFocus);
5201
5202 if ( node->GetNext() &&
5203 useThis == node->GetNext()->GetData() )
5204 DoSelectProperty(GetFirst());
5205 else if ( node->GetPrevious () &&
5206 useThis == node->GetPrevious()->GetData() )
5207 DoSelectProperty(GetLastProperty());
5208
5209 }
5210 */
5211 }
5212
5213 // Redraw selected
5214 if ( m_selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5215 DrawItem( m_selected );
5216 }
5217 }
5218
5219 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5220 {
5221 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5222 HandleFocusChange((wxWindow*)event.GetEventObject());
5223 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5224 //else if ( event.GetWindow() )
5225 else
5226 HandleFocusChange(event.GetWindow());
5227
5228 event.Skip();
5229 }
5230
5231 // -----------------------------------------------------------------------
5232
5233 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5234 {
5235 HandleFocusChange((wxWindow*)event.GetEventObject());
5236
5237 //
5238 // event.Skip() being commented out is aworkaround for bug reported
5239 // in ticket #4840 (wxScrolledWindow problem with automatic scrolling).
5240 //event.Skip();
5241 }
5242
5243 // -----------------------------------------------------------------------
5244
5245 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5246 {
5247 m_iFlags |= wxPG_FL_SCROLLED;
5248
5249 event.Skip();
5250 }
5251
5252 // -----------------------------------------------------------------------
5253
5254 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5255 {
5256 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5257 {
5258 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5259 }
5260 }
5261
5262 // -----------------------------------------------------------------------
5263 // Property editor related functions
5264 // -----------------------------------------------------------------------
5265
5266 // noDefCheck = true prevents infinite recursion.
5267 wxPGEditor* wxPropertyGrid::RegisterEditorClass( wxPGEditor* editorClass,
5268 bool noDefCheck )
5269 {
5270 wxASSERT( editorClass );
5271
5272 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5273 RegisterDefaultEditors();
5274
5275 wxString name = editorClass->GetName();
5276
5277 // Existing editor under this name?
5278 wxPGHashMapS2P::iterator vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5279
5280 if ( vt_it != wxPGGlobalVars->m_mapEditorClasses.end() )
5281 {
5282 // If this name was already used, try class name.
5283 name = editorClass->GetClassInfo()->GetClassName();
5284 vt_it = wxPGGlobalVars->m_mapEditorClasses.find(name);
5285 }
5286
5287 wxCHECK_MSG( vt_it == wxPGGlobalVars->m_mapEditorClasses.end(),
5288 (wxPGEditor*) vt_it->second,
5289 "Editor with given name was already registered" );
5290
5291 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorClass;
5292
5293 return editorClass;
5294 }
5295
5296 // Use this in RegisterDefaultEditors.
5297 #define wxPGRegisterDefaultEditorClass(EDITOR) \
5298 if ( wxPGEditor_##EDITOR == (wxPGEditor*) NULL ) \
5299 { \
5300 wxPGEditor_##EDITOR = wxPropertyGrid::RegisterEditorClass( \
5301 new wxPG##EDITOR##Editor, true ); \
5302 }
5303
5304 // Registers all default editor classes
5305 void wxPropertyGrid::RegisterDefaultEditors()
5306 {
5307 wxPGRegisterDefaultEditorClass( TextCtrl );
5308 wxPGRegisterDefaultEditorClass( Choice );
5309 wxPGRegisterDefaultEditorClass( ComboBox );
5310 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
5311 #if wxPG_INCLUDE_CHECKBOX
5312 wxPGRegisterDefaultEditorClass( CheckBox );
5313 #endif
5314 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
5315
5316 // Register SpinCtrl etc. editors before use
5317 RegisterAdditionalEditors();
5318 }
5319
5320 // -----------------------------------------------------------------------
5321 // wxPGStringTokenizer
5322 // Needed to handle C-style string lists (e.g. "str1" "str2")
5323 // -----------------------------------------------------------------------
5324
5325 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
5326 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
5327 {
5328 }
5329
5330 wxPGStringTokenizer::~wxPGStringTokenizer()
5331 {
5332 }
5333
5334 bool wxPGStringTokenizer::HasMoreTokens()
5335 {
5336 const wxString& str = *m_str;
5337
5338 wxString::const_iterator i = m_curPos;
5339
5340 wxUniChar delim = m_delimeter;
5341 wxUniChar a;
5342 wxUniChar prev_a = wxT('\0');
5343
5344 bool inToken = false;
5345
5346 while ( i != str.end() )
5347 {
5348 a = *i;
5349
5350 if ( !inToken )
5351 {
5352 if ( a == delim )
5353 {
5354 inToken = true;
5355 m_readyToken.clear();
5356 }
5357 }
5358 else
5359 {
5360 if ( prev_a != wxT('\\') )
5361 {
5362 if ( a != delim )
5363 {
5364 if ( a != wxT('\\') )
5365 m_readyToken << a;
5366 }
5367 else
5368 {
5369 i++;
5370 m_curPos = i;
5371 return true;
5372 }
5373 prev_a = a;
5374 }
5375 else
5376 {
5377 m_readyToken << a;
5378 prev_a = wxT('\0');
5379 }
5380 }
5381 i++;
5382 }
5383
5384 m_curPos = str.end();
5385
5386 if ( inToken )
5387 return true;
5388
5389 return false;
5390 }
5391
5392 wxString wxPGStringTokenizer::GetNextToken()
5393 {
5394 return m_readyToken;
5395 }
5396
5397 // -----------------------------------------------------------------------
5398 // wxPGChoiceEntry
5399 // -----------------------------------------------------------------------
5400
5401 wxPGChoiceEntry::wxPGChoiceEntry()
5402 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
5403 {
5404 }
5405
5406 wxPGChoiceEntry::wxPGChoiceEntry( const wxPGChoiceEntry& entry )
5407 : wxPGCell( entry.GetText(), entry.GetBitmap(),
5408 entry.GetFgCol(), entry.GetBgCol() ), m_value(entry.GetValue())
5409 {
5410 }
5411
5412 // -----------------------------------------------------------------------
5413 // wxPGChoicesData
5414 // -----------------------------------------------------------------------
5415
5416 wxPGChoicesData::wxPGChoicesData()
5417 {
5418 m_refCount = 1;
5419 }
5420
5421 wxPGChoicesData::~wxPGChoicesData()
5422 {
5423 Clear();
5424 }
5425
5426 void wxPGChoicesData::Clear()
5427 {
5428 unsigned int i;
5429
5430 for ( i=0; i<m_items.size(); i++ )
5431 {
5432 delete Item(i);
5433 }
5434
5435 m_items.clear();
5436 }
5437
5438 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
5439 {
5440 wxASSERT( m_items.size() == 0 );
5441
5442 unsigned int i;
5443
5444 for ( i=0; i<data->GetCount(); i++ )
5445 m_items.push_back( new wxPGChoiceEntry(*data->Item(i)) );
5446 }
5447
5448 // -----------------------------------------------------------------------
5449 // wxPGChoices
5450 // -----------------------------------------------------------------------
5451
5452 wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, int value )
5453 {
5454 EnsureData();
5455
5456 wxPGChoiceEntry* p = new wxPGChoiceEntry(label, value);
5457 m_data->Insert( -1, p );
5458 return *p;
5459 }
5460
5461 // -----------------------------------------------------------------------
5462
5463 wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, const wxBitmap& bitmap, int value )
5464 {
5465 EnsureData();
5466
5467 wxPGChoiceEntry* p = new wxPGChoiceEntry(label, value);
5468 p->SetBitmap(bitmap);
5469 m_data->Insert( -1, p );
5470 return *p;
5471 }
5472
5473 // -----------------------------------------------------------------------
5474
5475 wxPGChoiceEntry& wxPGChoices::Insert( const wxPGChoiceEntry& entry, int index )
5476 {
5477 EnsureData();
5478
5479 wxPGChoiceEntry* p = new wxPGChoiceEntry(entry);
5480 m_data->Insert(index, p);
5481 return *p;
5482 }
5483
5484 // -----------------------------------------------------------------------
5485
5486 wxPGChoiceEntry& wxPGChoices::Insert( const wxString& label, int index, int value )
5487 {
5488 EnsureData();
5489
5490 wxPGChoiceEntry* p = new wxPGChoiceEntry(label, value);
5491 m_data->Insert( index, p );
5492 return *p;
5493 }
5494
5495 // -----------------------------------------------------------------------
5496
5497 wxPGChoiceEntry& wxPGChoices::AddAsSorted( const wxString& label, int value )
5498 {
5499 EnsureData();
5500
5501 size_t index = 0;
5502
5503 while ( index < GetCount() )
5504 {
5505 int cmpRes = GetLabel(index).Cmp(label);
5506 if ( cmpRes > 0 )
5507 break;
5508 index++;
5509 }
5510
5511 wxPGChoiceEntry* p = new wxPGChoiceEntry(label, value);
5512 m_data->Insert( index, p );
5513 return *p;
5514 }
5515
5516 // -----------------------------------------------------------------------
5517
5518 void wxPGChoices::Add( const wxChar** labels, const ValArrItem* values )
5519 {
5520 EnsureData();
5521
5522 unsigned int itemcount = 0;
5523 const wxChar** p = &labels[0];
5524 while ( *p ) { p++; itemcount++; }
5525
5526 unsigned int i;
5527 for ( i = 0; i < itemcount; i++ )
5528 {
5529 int value = wxPG_INVALID_VALUE;
5530 if ( values )
5531 value = values[i];
5532 m_data->Insert( -1, new wxPGChoiceEntry(labels[i], value) );
5533 }
5534 }
5535
5536 // -----------------------------------------------------------------------
5537
5538 void wxPGChoices::Add( const wxArrayString& arr, const ValArrItem* values )
5539 {
5540 EnsureData();
5541
5542 unsigned int i;
5543 unsigned int itemcount = arr.size();
5544
5545 for ( i = 0; i < itemcount; i++ )
5546 {
5547 int value = wxPG_INVALID_VALUE;
5548 if ( values )
5549 value = values[i];
5550 m_data->Insert( -1, new wxPGChoiceEntry(arr[i], value) );
5551 }
5552 }
5553
5554 // -----------------------------------------------------------------------
5555
5556 void wxPGChoices::Add( const wxArrayString& arr, const wxArrayInt& arrint )
5557 {
5558 EnsureData();
5559
5560 unsigned int i;
5561 unsigned int itemcount = arr.size();
5562
5563 for ( i = 0; i < itemcount; i++ )
5564 {
5565 int value = wxPG_INVALID_VALUE;
5566 if ( &arrint && arrint.size() )
5567 value = arrint[i];
5568 m_data->Insert( -1, new wxPGChoiceEntry(arr[i], value) );
5569 }
5570 }
5571
5572 // -----------------------------------------------------------------------
5573
5574 void wxPGChoices::RemoveAt(size_t nIndex, size_t count)
5575 {
5576 wxASSERT( m_data->m_refCount != 0xFFFFFFF );
5577 unsigned int i;
5578 for ( i=nIndex; i<(nIndex+count); i++)
5579 delete m_data->Item(i);
5580 m_data->m_items.erase(m_data->m_items.begin()+nIndex,
5581 m_data->m_items.begin()+nIndex+count);
5582 }
5583
5584 // -----------------------------------------------------------------------
5585
5586 int wxPGChoices::Index( const wxString& str ) const
5587 {
5588 if ( IsOk() )
5589 {
5590 unsigned int i;
5591 for ( i=0; i< m_data->GetCount(); i++ )
5592 {
5593 if ( m_data->Item(i)->GetText() == str )
5594 return i;
5595 }
5596 }
5597 return -1;
5598 }
5599
5600 // -----------------------------------------------------------------------
5601
5602 int wxPGChoices::Index( int val ) const
5603 {
5604 if ( IsOk() )
5605 {
5606 unsigned int i;
5607 for ( i=0; i< m_data->GetCount(); i++ )
5608 {
5609 if ( m_data->Item(i)->GetValue() == val )
5610 return i;
5611 }
5612 }
5613 return -1;
5614 }
5615
5616 // -----------------------------------------------------------------------
5617
5618 wxArrayString wxPGChoices::GetLabels() const
5619 {
5620 wxArrayString arr;
5621 unsigned int i;
5622
5623 if ( this && IsOk() )
5624 for ( i=0; i<GetCount(); i++ )
5625 arr.push_back(GetLabel(i));
5626
5627 return arr;
5628 }
5629
5630 // -----------------------------------------------------------------------
5631
5632 bool wxPGChoices::HasValues() const
5633 {
5634 return true;
5635 }
5636
5637 // -----------------------------------------------------------------------
5638
5639 wxArrayInt wxPGChoices::GetValuesForStrings( const wxArrayString& strings ) const
5640 {
5641 wxArrayInt arr;
5642
5643 if ( IsOk() )
5644 {
5645 unsigned int i;
5646 for ( i=0; i< strings.size(); i++ )
5647 {
5648 int index = Index(strings[i]);
5649 if ( index >= 0 )
5650 arr.Add(GetValue(index));
5651 else
5652 arr.Add(wxPG_INVALID_VALUE);
5653 }
5654 }
5655
5656 return arr;
5657 }
5658
5659 // -----------------------------------------------------------------------
5660
5661 wxArrayInt wxPGChoices::GetIndicesForStrings( const wxArrayString& strings,
5662 wxArrayString* unmatched ) const
5663 {
5664 wxArrayInt arr;
5665
5666 if ( IsOk() )
5667 {
5668 unsigned int i;
5669 for ( i=0; i< strings.size(); i++ )
5670 {
5671 const wxString& str = strings[i];
5672 int index = Index(str);
5673 if ( index >= 0 )
5674 arr.Add(index);
5675 else if ( unmatched )
5676 unmatched->Add(str);
5677 }
5678 }
5679
5680 return arr;
5681 }
5682
5683 // -----------------------------------------------------------------------
5684
5685 void wxPGChoices::AssignData( wxPGChoicesData* data )
5686 {
5687 Free();
5688
5689 if ( data != wxPGChoicesEmptyData )
5690 {
5691 m_data = data;
5692 data->m_refCount++;
5693 }
5694 }
5695
5696 // -----------------------------------------------------------------------
5697
5698 void wxPGChoices::Init()
5699 {
5700 m_data = wxPGChoicesEmptyData;
5701 }
5702
5703 // -----------------------------------------------------------------------
5704
5705 void wxPGChoices::Free()
5706 {
5707 if ( m_data != wxPGChoicesEmptyData )
5708 {
5709 m_data->DecRef();
5710 m_data = wxPGChoicesEmptyData;
5711 }
5712 }
5713
5714 // -----------------------------------------------------------------------
5715 // wxPropertyGridEvent
5716 // -----------------------------------------------------------------------
5717
5718 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
5719
5720
5721 DEFINE_EVENT_TYPE( wxEVT_PG_SELECTED )
5722 DEFINE_EVENT_TYPE( wxEVT_PG_CHANGING )
5723 DEFINE_EVENT_TYPE( wxEVT_PG_CHANGED )
5724 DEFINE_EVENT_TYPE( wxEVT_PG_HIGHLIGHTED )
5725 DEFINE_EVENT_TYPE( wxEVT_PG_RIGHT_CLICK )
5726 DEFINE_EVENT_TYPE( wxEVT_PG_PAGE_CHANGED )
5727 DEFINE_EVENT_TYPE( wxEVT_PG_ITEM_EXPANDED )
5728 DEFINE_EVENT_TYPE( wxEVT_PG_ITEM_COLLAPSED )
5729 DEFINE_EVENT_TYPE( wxEVT_PG_DOUBLE_CLICK )
5730
5731
5732 // -----------------------------------------------------------------------
5733
5734 void wxPropertyGridEvent::Init()
5735 {
5736 m_validationInfo = NULL;
5737 m_canVeto = false;
5738 m_wasVetoed = false;
5739 }
5740
5741 // -----------------------------------------------------------------------
5742
5743 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
5744 : wxCommandEvent(commandType,id)
5745 {
5746 m_property = NULL;
5747 Init();
5748 }
5749
5750 // -----------------------------------------------------------------------
5751
5752 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
5753 : wxCommandEvent(event)
5754 {
5755 m_eventType = event.GetEventType();
5756 m_eventObject = event.m_eventObject;
5757 m_pg = event.m_pg;
5758 m_property = event.m_property;
5759 m_validationInfo = event.m_validationInfo;
5760 m_canVeto = event.m_canVeto;
5761 m_wasVetoed = event.m_wasVetoed;
5762 }
5763
5764 // -----------------------------------------------------------------------
5765
5766 wxPropertyGridEvent::~wxPropertyGridEvent()
5767 {
5768 }
5769
5770 // -----------------------------------------------------------------------
5771
5772 wxEvent* wxPropertyGridEvent::Clone() const
5773 {
5774 return new wxPropertyGridEvent( *this );
5775 }
5776
5777 // -----------------------------------------------------------------------
5778 // wxPropertyGridPopulator
5779 // -----------------------------------------------------------------------
5780
5781 wxPropertyGridPopulator::wxPropertyGridPopulator()
5782 {
5783 m_state = NULL;
5784 m_pg = NULL;
5785 wxPGGlobalVars->m_offline++;
5786 }
5787
5788 // -----------------------------------------------------------------------
5789
5790 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
5791 {
5792 m_state = state;
5793 m_propHierarchy.clear();
5794 }
5795
5796 // -----------------------------------------------------------------------
5797
5798 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
5799 {
5800 m_pg = pg;
5801 pg->Freeze();
5802 }
5803
5804 // -----------------------------------------------------------------------
5805
5806 wxPropertyGridPopulator::~wxPropertyGridPopulator()
5807 {
5808 //
5809 // Free unused sets of choices
5810 wxPGHashMapS2P::iterator it;
5811
5812 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
5813 {
5814 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
5815 data->DecRef();
5816 }
5817
5818 if ( m_pg )
5819 {
5820 m_pg->Thaw();
5821 m_pg->GetPanel()->Refresh();
5822 }
5823 wxPGGlobalVars->m_offline--;
5824 }
5825
5826 // -----------------------------------------------------------------------
5827
5828 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
5829 const wxString& propLabel,
5830 const wxString& propName,
5831 const wxString* propValue,
5832 wxPGChoices* pChoices )
5833 {
5834 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
5835 wxPGProperty* parent = GetCurParent();
5836
5837 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
5838 {
5839 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
5840 return NULL;
5841 }
5842
5843 if ( !classInfo || !classInfo->IsKindOf(CLASSINFO(wxPGProperty)) )
5844 {
5845 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
5846 return NULL;
5847 }
5848
5849 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
5850
5851 property->SetLabel(propLabel);
5852 property->DoSetName(propName);
5853
5854 if ( pChoices && pChoices->IsOk() )
5855 property->SetChoices(*pChoices);
5856
5857 m_state->DoInsert(parent, -1, property);
5858
5859 if ( propValue )
5860 property->SetValueFromString( *propValue, wxPG_FULL_VALUE );
5861
5862 return property;
5863 }
5864
5865 // -----------------------------------------------------------------------
5866
5867 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
5868 {
5869 m_propHierarchy.push_back(property);
5870 DoScanForChildren();
5871 m_propHierarchy.pop_back();
5872 }
5873
5874 // -----------------------------------------------------------------------
5875
5876 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
5877 const wxString& idString )
5878 {
5879 wxPGChoices choices;
5880
5881 // Using id?
5882 if ( choicesString[0] == wxT('@') )
5883 {
5884 wxString ids = choicesString.substr(1);
5885 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
5886 if ( it == m_dictIdChoices.end() )
5887 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
5888 else
5889 choices.AssignData((wxPGChoicesData*)it->second);
5890 }
5891 else
5892 {
5893 bool found = false;
5894 if ( idString.length() )
5895 {
5896 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
5897 if ( it != m_dictIdChoices.end() )
5898 {
5899 choices.AssignData((wxPGChoicesData*)it->second);
5900 found = true;
5901 }
5902 }
5903
5904 if ( !found )
5905 {
5906 // Parse choices string
5907 wxString::const_iterator it = choicesString.begin();
5908 wxString label;
5909 wxString value;
5910 int state = 0;
5911 bool labelValid = false;
5912
5913 for ( ; it != choicesString.end(); it++ )
5914 {
5915 wxChar c = *it;
5916
5917 if ( state != 1 )
5918 {
5919 if ( c == wxT('"') )
5920 {
5921 if ( labelValid )
5922 {
5923 long l;
5924 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
5925 choices.Add(label, l);
5926 }
5927 labelValid = false;
5928 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
5929 value.clear();
5930 label.clear();
5931 state = 1;
5932 }
5933 else if ( c == wxT('=') )
5934 {
5935 if ( labelValid )
5936 {
5937 state = 2;
5938 }
5939 }
5940 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
5941 {
5942 value << c;
5943 }
5944 }
5945 else
5946 {
5947 if ( c == wxT('"') )
5948 {
5949 state = 0;
5950 labelValid = true;
5951 }
5952 else
5953 label << c;
5954 }
5955 }
5956
5957 if ( labelValid )
5958 {
5959 long l;
5960 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
5961 choices.Add(label, l);
5962 }
5963
5964 if ( !choices.IsOk() )
5965 {
5966 choices.EnsureData();
5967 }
5968
5969 // Assign to id
5970 if ( idString.length() )
5971 m_dictIdChoices[idString] = choices.GetData();
5972 }
5973 }
5974
5975 return choices;
5976 }
5977
5978 // -----------------------------------------------------------------------
5979
5980 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
5981 {
5982 if ( s.Last() == wxT('%') )
5983 {
5984 wxString s2 = s.substr(0,s.length()-1);
5985 long val;
5986 if ( s2.ToLong(&val, 10) )
5987 {
5988 *pval = (val*max)/100;
5989 return true;
5990 }
5991 return false;
5992 }
5993
5994 return s.ToLong(pval, 10);
5995 }
5996
5997 // -----------------------------------------------------------------------
5998
5999 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
6000 const wxString& type,
6001 const wxString& value )
6002 {
6003 int l = m_propHierarchy.size();
6004 if ( !l )
6005 return false;
6006
6007 wxPGProperty* p = m_propHierarchy[l-1];
6008 wxString valuel = value.Lower();
6009 wxVariant variant;
6010
6011 if ( type.length() == 0 )
6012 {
6013 long v;
6014
6015 // Auto-detect type
6016 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6017 variant = true;
6018 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6019 variant = false;
6020 else if ( value.ToLong(&v, 0) )
6021 variant = v;
6022 else
6023 variant = value;
6024 }
6025 else
6026 {
6027 if ( type == wxT("string") )
6028 {
6029 variant = value;
6030 }
6031 else if ( type == wxT("int") )
6032 {
6033 long v = 0;
6034 value.ToLong(&v, 0);
6035 variant = v;
6036 }
6037 else if ( type == wxT("bool") )
6038 {
6039 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6040 variant = true;
6041 else
6042 variant = false;
6043 }
6044 else
6045 {
6046 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6047 return false;
6048 }
6049 }
6050
6051 p->SetAttribute( name, variant );
6052
6053 return true;
6054 }
6055
6056 // -----------------------------------------------------------------------
6057
6058 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6059 {
6060 wxLogError(_("Error in resource: %s"),msg.c_str());
6061 }
6062
6063 // -----------------------------------------------------------------------
6064
6065 #endif // wxUSE_PROPGRID