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