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