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