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