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