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