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