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