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