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