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