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