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