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