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