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