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