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