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