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