]> git.saurik.com Git - wxWidgets.git/blob - src/propgrid/propgrid.cpp
Fixed and clarified editor control event handling
[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 wxWindow* wnd = argWnd;
3477
3478 if ( argWnd == m_wndEditor )
3479 {
3480 this->Connect(id, wxEVT_MOTION,
3481 wxMouseEventHandler(wxPropertyGrid::OnMouseMoveChild));
3482 this->Connect(id, wxEVT_LEFT_UP,
3483 wxMouseEventHandler(wxPropertyGrid::OnMouseUpChild));
3484 this->Connect(id, wxEVT_LEFT_DOWN,
3485 wxMouseEventHandler(wxPropertyGrid::OnMouseClickChild));
3486 this->Connect(id, wxEVT_RIGHT_UP,
3487 wxMouseEventHandler(wxPropertyGrid::OnMouseRightClickChild));
3488 this->Connect(id, wxEVT_ENTER_WINDOW,
3489 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry));
3490 this->Connect(id, wxEVT_LEAVE_WINDOW,
3491 wxMouseEventHandler(wxPropertyGrid::OnMouseEntry));
3492 }
3493 else
3494 {
3495 this->Connect(id, wxEVT_NAVIGATION_KEY,
3496 wxNavigationKeyEventHandler(wxPropertyGrid::OnNavigationKey));
3497 }
3498
3499 this->Connect(id, wxEVT_KEY_DOWN,
3500 wxKeyEventHandler(wxPropertyGrid::OnChildKeyDown));
3501 this->Connect(id, wxEVT_KEY_UP,
3502 wxKeyEventHandler(wxPropertyGrid::OnChildKeyUp));
3503 this->Connect(id, wxEVT_KILL_FOCUS,
3504 wxFocusEventHandler(wxPropertyGrid::OnFocusEvent));
3505 }
3506
3507 void wxPropertyGrid::FreeEditors()
3508 {
3509 // Do not free editors immediately if processing events
3510 if ( !m_windowsToDelete )
3511 m_windowsToDelete = new wxArrayPtrVoid;
3512
3513 if ( m_wndEditor2 )
3514 {
3515 m_windowsToDelete->push_back(m_wndEditor2);
3516 m_wndEditor2->Hide();
3517 m_wndEditor2 = (wxWindow*) NULL;
3518 }
3519
3520 if ( m_wndEditor )
3521 {
3522 m_windowsToDelete->push_back(m_wndEditor);
3523 m_wndEditor->Hide();
3524 m_wndEditor = (wxWindow*) NULL;
3525 }
3526 }
3527
3528 // Call with NULL to de-select property
3529 bool wxPropertyGrid::DoSelectProperty( wxPGProperty* p, unsigned int flags )
3530 {
3531 wxPanel* canvas = GetPanel();
3532
3533 /*
3534 if (p)
3535 wxLogDebug(wxT("SelectProperty( %s (%s[%i]) )"),p->m_label.c_str(),
3536 p->m_parent->m_label.c_str(),p->GetIndexInParent());
3537 else
3538 wxLogDebug(wxT("SelectProperty( NULL, -1 )"));
3539 */
3540
3541 if ( m_inDoSelectProperty )
3542 return true;
3543
3544 m_inDoSelectProperty = 1;
3545
3546 wxPGProperty* prev = m_selected;
3547
3548 //
3549 // Delete windows pending for deletion
3550 if ( m_windowsToDelete && !m_inDoPropertyChanged && m_windowsToDelete->size() )
3551 {
3552 unsigned int i;
3553
3554 for ( i=0; i<m_windowsToDelete->size(); i++ )
3555 delete ((wxWindow*)((*m_windowsToDelete)[i]));
3556
3557 m_windowsToDelete->clear();
3558 }
3559
3560 if ( !m_pState )
3561 {
3562 m_inDoSelectProperty = 0;
3563 return false;
3564 }
3565
3566 //
3567 // If we are frozen, then just set the values.
3568 if ( m_frozen )
3569 {
3570 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3571 m_editorFocused = 0;
3572 m_selected = p;
3573 m_selColumn = 1;
3574 m_pState->m_selected = p;
3575
3576 // If frozen, always free controls. But don't worry, as Thaw will
3577 // recall SelectProperty to recreate them.
3578 FreeEditors();
3579
3580 // Prevent any further selection measures in this call
3581 p = (wxPGProperty*) NULL;
3582 }
3583 else
3584 {
3585 // Is it the same?
3586 if ( m_selected == p && !(flags & wxPG_SEL_FORCE) )
3587 {
3588 // Only set focus if not deselecting
3589 if ( p )
3590 {
3591 if ( flags & wxPG_SEL_FOCUS )
3592 {
3593 if ( m_wndEditor )
3594 {
3595 m_wndEditor->SetFocus();
3596 m_editorFocused = 1;
3597 }
3598 }
3599 else
3600 {
3601 SetFocusOnCanvas();
3602 }
3603 }
3604
3605 m_inDoSelectProperty = 0;
3606 return true;
3607 }
3608
3609 //
3610 // First, deactivate previous
3611 if ( m_selected )
3612 {
3613
3614 OnValidationFailureReset(m_selected);
3615
3616 // Must double-check if this is an selected in case of forceswitch
3617 if ( p != prev )
3618 {
3619 if ( !CommitChangesFromEditor(flags) )
3620 {
3621 // Validation has failed, so we can't exit the previous editor
3622 //::wxMessageBox(_("Please correct the value or press ESC to cancel the edit."),
3623 // _("Invalid Value"),wxOK|wxICON_ERROR);
3624 m_inDoSelectProperty = 0;
3625 return false;
3626 }
3627 }
3628
3629 FreeEditors();
3630 m_selColumn = -1;
3631
3632 m_selected = (wxPGProperty*) NULL;
3633 m_pState->m_selected = (wxPGProperty*) NULL;
3634
3635 // We need to always fully refresh the grid here
3636 Refresh(false);
3637
3638 m_iFlags &= ~(wxPG_FL_ABNORMAL_EDITOR);
3639 EditorsValueWasNotModified();
3640 }
3641
3642 SetInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3643
3644 //
3645 // Then, activate the one given.
3646 if ( p )
3647 {
3648 int propY = p->GetY2(m_lineHeight);
3649
3650 int splitterX = GetSplitterPosition();
3651 m_editorFocused = 0;
3652 m_selected = p;
3653 m_pState->m_selected = p;
3654 m_iFlags |= wxPG_FL_PRIMARY_FILLS_ENTIRE;
3655 if ( p != prev )
3656 m_iFlags &= ~(wxPG_FL_VALIDATION_FAILED);
3657
3658 wxASSERT( m_wndEditor == (wxWindow*) NULL );
3659
3660 // Do we need OnMeasureCalls?
3661 wxSize imsz = p->OnMeasureImage();
3662
3663 //
3664 // Only create editor for non-disabled non-caption
3665 if ( !p->IsCategory() && !(p->m_flags & wxPG_PROP_DISABLED) )
3666 {
3667 // do this for non-caption items
3668
3669 m_selColumn = 1;
3670
3671 // Do we need to paint the custom image, if any?
3672 m_iFlags &= ~(wxPG_FL_CUR_USES_CUSTOM_IMAGE);
3673 if ( (p->m_flags & wxPG_PROP_CUSTOMIMAGE) &&
3674 !p->GetEditorClass()->CanContainCustomImage()
3675 )
3676 m_iFlags |= wxPG_FL_CUR_USES_CUSTOM_IMAGE;
3677
3678 wxRect grect = GetEditorWidgetRect(p, m_selColumn);
3679 wxPoint goodPos = grect.GetPosition();
3680 #if wxPG_CREATE_CONTROLS_HIDDEN
3681 int coord_adjust = m_height - goodPos.y;
3682 goodPos.y += coord_adjust;
3683 #endif
3684
3685 const wxPGEditor* editor = p->GetEditorClass();
3686 wxCHECK_MSG(editor, false,
3687 wxT("NULL editor class not allowed"));
3688
3689 m_iFlags &= ~wxPG_FL_FIXED_WIDTH_EDITOR;
3690
3691 wxPGWindowList wndList = editor->CreateControls(this,
3692 p,
3693 goodPos,
3694 grect.GetSize());
3695
3696 m_wndEditor = wndList.m_primary;
3697 m_wndEditor2 = wndList.m_secondary;
3698
3699 // NOTE: It is allowed for m_wndEditor to be NULL - in this case
3700 // value is drawn as normal, and m_wndEditor2 is assumed
3701 // to be a right-aligned button that triggers a separate editor
3702 // window.
3703
3704 if ( m_wndEditor )
3705 {
3706 wxASSERT_MSG( m_wndEditor->GetParent() == canvas,
3707 wxT("CreateControls must use result of wxPropertyGrid::GetPanel() as parent of controls.") );
3708
3709 // Set validator, if any
3710 #if wxUSE_VALIDATORS
3711 if ( !(GetExtraStyle() & wxPG_EX_LEGACY_VALIDATORS) )
3712 {
3713 wxValidator* validator = p->GetValidator();
3714 if ( validator )
3715 m_wndEditor->SetValidator(*validator);
3716 }
3717 #endif
3718
3719 if ( m_wndEditor->GetSize().y > (m_lineHeight+6) )
3720 m_iFlags |= wxPG_FL_ABNORMAL_EDITOR;
3721
3722 // If it has modified status, use bold font
3723 // (must be done before capturing m_ctrlXAdjust)
3724 if ( (p->m_flags & wxPG_PROP_MODIFIED) && (m_windowStyle & wxPG_BOLD_MODIFIED) )
3725 SetCurControlBoldFont();
3726
3727 //
3728 // Fix TextCtrl indentation
3729 #if defined(__WXMSW__) && !defined(__WXWINCE__)
3730 wxTextCtrl* tc = NULL;
3731 if ( m_wndEditor->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
3732 tc = ((wxOwnerDrawnComboBox*)m_wndEditor)->GetTextCtrl();
3733 else
3734 tc = wxDynamicCast(m_wndEditor, wxTextCtrl);
3735 if ( tc )
3736 ::SendMessage(GetHwndOf(tc), EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELONG(0, 0));
3737 #endif
3738
3739 // Store x relative to splitter (we'll need it).
3740 m_ctrlXAdjust = m_wndEditor->GetPosition().x - splitterX;
3741
3742 // Check if background clear is not necessary
3743 wxPoint pos = m_wndEditor->GetPosition();
3744 if ( pos.x > (splitterX+1) || pos.y > propY )
3745 {
3746 m_iFlags &= ~(wxPG_FL_PRIMARY_FILLS_ENTIRE);
3747 }
3748
3749 m_wndEditor->SetSizeHints(3, 3);
3750
3751 #if wxPG_CREATE_CONTROLS_HIDDEN
3752 m_wndEditor->Show(false);
3753 m_wndEditor->Freeze();
3754
3755 goodPos = m_wndEditor->GetPosition();
3756 goodPos.y -= coord_adjust;
3757 m_wndEditor->Move( goodPos );
3758 #endif
3759
3760 wxWindow* primaryCtrl = GetEditorControl();
3761 SetupChildEventHandling(primaryCtrl, wxPG_SUBID1);
3762
3763 // Focus and select all (wxTextCtrl, wxComboBox etc)
3764 if ( flags & wxPG_SEL_FOCUS )
3765 {
3766 primaryCtrl->SetFocus();
3767
3768 p->GetEditorClass()->OnFocus(p, primaryCtrl);
3769 }
3770 }
3771
3772 if ( m_wndEditor2 )
3773 {
3774 wxASSERT_MSG( m_wndEditor2->GetParent() == canvas,
3775 wxT("CreateControls must use result of wxPropertyGrid::GetPanel() as parent of controls.") );
3776
3777 // Get proper id for wndSecondary
3778 m_wndSecId = m_wndEditor2->GetId();
3779 wxWindowList children = m_wndEditor2->GetChildren();
3780 wxWindowList::iterator node = children.begin();
3781 if ( node != children.end() )
3782 m_wndSecId = ((wxWindow*)*node)->GetId();
3783
3784 m_wndEditor2->SetSizeHints(3,3);
3785
3786 #if wxPG_CREATE_CONTROLS_HIDDEN
3787 wxRect sec_rect = m_wndEditor2->GetRect();
3788 sec_rect.y -= coord_adjust;
3789
3790 // Fine tuning required to fix "oversized"
3791 // button disappearance bug.
3792 if ( sec_rect.y < 0 )
3793 {
3794 sec_rect.height += sec_rect.y;
3795 sec_rect.y = 0;
3796 }
3797 m_wndEditor2->SetSize( sec_rect );
3798 #endif
3799 m_wndEditor2->Show();
3800
3801 SetupChildEventHandling(m_wndEditor2,wxPG_SUBID2);
3802
3803 // If no primary editor, focus to button to allow
3804 // it to interprete ENTER etc.
3805 // NOTE: Due to problems focusing away from it, this
3806 // has been disabled.
3807 /*
3808 if ( (flags & wxPG_SEL_FOCUS) && !m_wndEditor )
3809 m_wndEditor2->SetFocus();
3810 */
3811 }
3812
3813 if ( flags & wxPG_SEL_FOCUS )
3814 m_editorFocused = 1;
3815
3816 }
3817 else
3818 {
3819 // Make sure focus is in grid canvas (important for wxGTK, at least)
3820 SetFocusOnCanvas();
3821 }
3822
3823 EditorsValueWasNotModified();
3824
3825 // If it's inside collapsed section, expand parent, scroll, etc.
3826 // Also, if it was partially visible, scroll it into view.
3827 if ( !(flags & wxPG_SEL_NONVISIBLE) )
3828 EnsureVisible( p );
3829
3830 if ( m_wndEditor )
3831 {
3832 #if wxPG_CREATE_CONTROLS_HIDDEN
3833 m_wndEditor->Thaw();
3834 #endif
3835 m_wndEditor->Show(true);
3836 }
3837
3838 DrawItems(p, p);
3839 }
3840 else
3841 {
3842 // Make sure focus is in grid canvas
3843 SetFocusOnCanvas();
3844 }
3845
3846 ClearInternalFlag(wxPG_FL_IN_SELECT_PROPERTY);
3847 }
3848
3849 #if wxUSE_STATUSBAR
3850
3851 //
3852 // Show help text in status bar.
3853 // (if found and grid not embedded in manager with help box and
3854 // style wxPG_EX_HELP_AS_TOOLTIPS is not used).
3855 //
3856
3857 if ( !(GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS) )
3858 {
3859 wxStatusBar* statusbar = (wxStatusBar*) NULL;
3860 if ( !(m_iFlags & wxPG_FL_NOSTATUSBARHELP) )
3861 {
3862 wxFrame* frame = wxDynamicCast(::wxGetTopLevelParent(this),wxFrame);
3863 if ( frame )
3864 statusbar = frame->GetStatusBar();
3865 }
3866
3867 if ( statusbar )
3868 {
3869 const wxString* pHelpString = (const wxString*) NULL;
3870
3871 if ( p )
3872 {
3873 pHelpString = &p->GetHelpString();
3874 if ( pHelpString->length() )
3875 {
3876 // Set help box text.
3877 statusbar->SetStatusText( *pHelpString );
3878 m_iFlags |= wxPG_FL_STRING_IN_STATUSBAR;
3879 }
3880 }
3881
3882 if ( (!pHelpString || !pHelpString->length()) &&
3883 (m_iFlags & wxPG_FL_STRING_IN_STATUSBAR) )
3884 {
3885 // Clear help box - but only if it was written
3886 // by us at previous time.
3887 statusbar->SetStatusText( m_emptyString );
3888 m_iFlags &= ~(wxPG_FL_STRING_IN_STATUSBAR);
3889 }
3890 }
3891 }
3892 #endif
3893
3894 m_inDoSelectProperty = 0;
3895
3896 // call wx event handler (here so that it also occurs on deselection)
3897 SendEvent( wxEVT_PG_SELECTED, m_selected, NULL, flags );
3898
3899 return true;
3900 }
3901
3902 // -----------------------------------------------------------------------
3903
3904 bool wxPropertyGrid::UnfocusEditor()
3905 {
3906 if ( !m_selected || !m_wndEditor || m_frozen )
3907 return true;
3908
3909 if ( !CommitChangesFromEditor(0) )
3910 return false;
3911
3912 SetFocusOnCanvas();
3913 DrawItem(m_selected);
3914
3915 return true;
3916 }
3917
3918 // -----------------------------------------------------------------------
3919
3920 // This method is not inline because it called dozens of times
3921 // (i.e. two-arg function calls create smaller code size).
3922 bool wxPropertyGrid::DoClearSelection()
3923 {
3924 return DoSelectProperty((wxPGProperty*)NULL);
3925 }
3926
3927 // -----------------------------------------------------------------------
3928 // wxPropertyGrid expand/collapse state
3929 // -----------------------------------------------------------------------
3930
3931 bool wxPropertyGrid::DoCollapse( wxPGProperty* p, bool sendEvents )
3932 {
3933 wxPGProperty* pwc = wxStaticCast(p, wxPGProperty);
3934
3935 // If active editor was inside collapsed section, then disable it
3936 if ( m_selected && m_selected->IsSomeParent (p) )
3937 {
3938 if ( !ClearSelection() )
3939 return false;
3940 }
3941
3942 // Store dont-center-splitter flag 'cause we need to temporarily set it
3943 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
3944 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
3945
3946 bool res = m_pState->DoCollapse(pwc);
3947
3948 if ( res )
3949 {
3950 if ( sendEvents )
3951 SendEvent( wxEVT_PG_ITEM_COLLAPSED, p );
3952
3953 RecalculateVirtualSize();
3954
3955 // Redraw etc. only if collapsed was visible.
3956 if (pwc->IsVisible() &&
3957 !m_frozen &&
3958 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) ) )
3959 {
3960 // When item is collapsed so that scrollbar would move,
3961 // graphics mess is about (unless we redraw everything).
3962 Refresh();
3963 }
3964 }
3965
3966 // Clear dont-center-splitter flag if it wasn't set
3967 m_iFlags = (m_iFlags & ~wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
3968
3969 return res;
3970 }
3971
3972 // -----------------------------------------------------------------------
3973
3974 bool wxPropertyGrid::DoExpand( wxPGProperty* p, bool sendEvents )
3975 {
3976 wxCHECK_MSG( p, false, wxT("invalid property id") );
3977
3978 wxPGProperty* pwc = (wxPGProperty*)p;
3979
3980 // Store dont-center-splitter flag 'cause we need to temporarily set it
3981 wxUint32 old_flag = m_iFlags & wxPG_FL_DONT_CENTER_SPLITTER;
3982 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
3983
3984 bool res = m_pState->DoExpand(pwc);
3985
3986 if ( res )
3987 {
3988 if ( sendEvents )
3989 SendEvent( wxEVT_PG_ITEM_EXPANDED, p );
3990
3991 RecalculateVirtualSize();
3992
3993 // Redraw etc. only if expanded was visible.
3994 if ( pwc->IsVisible() && !m_frozen &&
3995 ( !pwc->IsCategory() || !(m_windowStyle & wxPG_HIDE_CATEGORIES) )
3996 )
3997 {
3998 // Redraw
3999 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4000 Refresh();
4001 #else
4002 DrawItems(pwc, NULL);
4003 #endif
4004 }
4005 }
4006
4007 // Clear dont-center-splitter flag if it wasn't set
4008 m_iFlags = m_iFlags & ~(wxPG_FL_DONT_CENTER_SPLITTER) | old_flag;
4009
4010 return res;
4011 }
4012
4013 // -----------------------------------------------------------------------
4014
4015 bool wxPropertyGrid::DoHideProperty( wxPGProperty* p, bool hide, int flags )
4016 {
4017 if ( m_frozen )
4018 return m_pState->DoHideProperty(p, hide, flags);
4019
4020 if ( m_selected &&
4021 ( m_selected == p || m_selected->IsSomeParent(p) )
4022 )
4023 {
4024 if ( !ClearSelection() )
4025 return false;
4026 }
4027
4028 m_pState->DoHideProperty(p, hide, flags);
4029
4030 RecalculateVirtualSize();
4031 Refresh();
4032
4033 return true;
4034 }
4035
4036
4037 // -----------------------------------------------------------------------
4038 // wxPropertyGrid size related methods
4039 // -----------------------------------------------------------------------
4040
4041 void wxPropertyGrid::RecalculateVirtualSize( int forceXPos )
4042 {
4043 if ( (m_iFlags & wxPG_FL_RECALCULATING_VIRTUAL_SIZE) || m_frozen )
4044 return;
4045
4046 //
4047 // If virtual height was changed, then recalculate editor control position(s)
4048 if ( m_pState->m_vhCalcPending )
4049 CorrectEditorWidgetPosY();
4050
4051 m_pState->EnsureVirtualHeight();
4052
4053 #ifdef __WXDEBUG__
4054 int by1 = m_pState->GetVirtualHeight();
4055 int by2 = m_pState->GetActualVirtualHeight();
4056 if ( by1 != by2 )
4057 {
4058 wxString s = wxString::Format(wxT("VirtualHeight=%i, ActualVirtualHeight=%i, should match!"), by1, by2);
4059 wxASSERT_MSG( false,
4060 s.c_str() );
4061 wxLogDebug(s);
4062 }
4063 #endif
4064
4065 m_iFlags |= wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4066
4067 int x = m_pState->m_width;
4068 int y = m_pState->m_virtualHeight;
4069
4070 int width, height;
4071 GetClientSize(&width,&height);
4072
4073 // Now adjust virtual size.
4074 SetVirtualSize(x, y);
4075
4076 int xAmount = 0;
4077 int xPos = 0;
4078
4079 //
4080 // Adjust scrollbars
4081 if ( HasVirtualWidth() )
4082 {
4083 xAmount = x/wxPG_PIXELS_PER_UNIT;
4084 xPos = GetScrollPos( wxHORIZONTAL );
4085 }
4086
4087 if ( forceXPos != -1 )
4088 xPos = forceXPos;
4089 // xPos too high?
4090 else if ( xPos > (xAmount-(width/wxPG_PIXELS_PER_UNIT)) )
4091 xPos = 0;
4092
4093 int yAmount = (y+wxPG_PIXELS_PER_UNIT+2)/wxPG_PIXELS_PER_UNIT;
4094 int yPos = GetScrollPos( wxVERTICAL );
4095
4096 SetScrollbars( wxPG_PIXELS_PER_UNIT, wxPG_PIXELS_PER_UNIT,
4097 xAmount, yAmount, xPos, yPos, true );
4098
4099 // Must re-get size now
4100 GetClientSize(&width,&height);
4101
4102 if ( !HasVirtualWidth() )
4103 {
4104 m_pState->SetVirtualWidth(width);
4105 x = width;
4106 }
4107
4108 m_width = width;
4109 m_height = height;
4110
4111 m_canvas->SetSize( x, y );
4112
4113 m_pState->CheckColumnWidths();
4114
4115 if ( m_selected )
4116 CorrectEditorWidgetSizeX();
4117
4118 m_iFlags &= ~wxPG_FL_RECALCULATING_VIRTUAL_SIZE;
4119 }
4120
4121 // -----------------------------------------------------------------------
4122
4123 void wxPropertyGrid::OnResize( wxSizeEvent& event )
4124 {
4125 if ( !(m_iFlags & wxPG_FL_INITIALIZED) )
4126 return;
4127
4128 int width, height;
4129 GetClientSize(&width,&height);
4130
4131 m_width = width;
4132 m_height = height;
4133
4134 #if wxPG_DOUBLE_BUFFER
4135 if ( !(GetExtraStyle() & wxPG_EX_NATIVE_DOUBLE_BUFFERING) )
4136 {
4137 int dblh = (m_lineHeight*2);
4138 if ( !m_doubleBuffer )
4139 {
4140 // Create double buffer bitmap to draw on, if none
4141 int w = (width>250)?width:250;
4142 int h = height + dblh;
4143 h = (h>400)?h:400;
4144 m_doubleBuffer = new wxBitmap( w, h );
4145 }
4146 else
4147 {
4148 int w = m_doubleBuffer->GetWidth();
4149 int h = m_doubleBuffer->GetHeight();
4150
4151 // Double buffer must be large enough
4152 if ( w < width || h < (height+dblh) )
4153 {
4154 if ( w < width ) w = width;
4155 if ( h < (height+dblh) ) h = height + dblh;
4156 delete m_doubleBuffer;
4157 m_doubleBuffer = new wxBitmap( w, h );
4158 }
4159 }
4160 }
4161
4162 #endif
4163
4164 m_pState->OnClientWidthChange( width, event.GetSize().x - m_ncWidth, true );
4165 m_ncWidth = event.GetSize().x;
4166
4167 if ( !m_frozen )
4168 {
4169 if ( m_pState->m_itemsAdded )
4170 PrepareAfterItemsAdded();
4171 else
4172 // Without this, virtual size (atleast under wxGTK) will be skewed
4173 RecalculateVirtualSize();
4174
4175 Refresh();
4176 }
4177 }
4178
4179 // -----------------------------------------------------------------------
4180
4181 void wxPropertyGrid::SetVirtualWidth( int width )
4182 {
4183 if ( width == -1 )
4184 {
4185 // Disable virtual width
4186 width = GetClientSize().x;
4187 ClearInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4188 }
4189 else
4190 {
4191 // Enable virtual width
4192 SetInternalFlag(wxPG_FL_HAS_VIRTUAL_WIDTH);
4193 }
4194 m_pState->SetVirtualWidth( width );
4195 }
4196
4197 // -----------------------------------------------------------------------
4198 // wxPropertyGrid mouse event handling
4199 // -----------------------------------------------------------------------
4200
4201 // selFlags uses same values DoSelectProperty's flags
4202 // Returns true if event was vetoed.
4203 bool wxPropertyGrid::SendEvent( int eventType, wxPGProperty* p, wxVariant* pValue, unsigned int WXUNUSED(selFlags) )
4204 {
4205 // Send property grid event of specific type and with specific property
4206 wxPropertyGridEvent evt( eventType, m_eventObject->GetId() );
4207 evt.SetPropertyGrid(this);
4208 evt.SetEventObject(m_eventObject);
4209 evt.SetProperty(p);
4210 if ( pValue )
4211 {
4212 evt.SetCanVeto(true);
4213 evt.SetupValidationInfo();
4214 m_validationInfo.m_pValue = pValue;
4215 }
4216 wxEvtHandler* evtHandler = m_eventObject->GetEventHandler();
4217
4218 evtHandler->ProcessEvent(evt);
4219
4220 return evt.WasVetoed();
4221 }
4222
4223 // -----------------------------------------------------------------------
4224
4225 // Return false if should be skipped
4226 bool wxPropertyGrid::HandleMouseClick( int x, unsigned int y, wxMouseEvent &event )
4227 {
4228 bool res = true;
4229
4230 // Need to set focus?
4231 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
4232 {
4233 SetFocusOnCanvas();
4234 }
4235
4236 wxPropertyGridPageState* state = m_pState;
4237 int splitterHit;
4238 int splitterHitOffset;
4239 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4240
4241 wxPGProperty* p = DoGetItemAtY(y);
4242
4243 if ( p )
4244 {
4245 int depth = (int)p->GetDepth() - 1;
4246
4247 int marginEnds = m_marginWidth + ( depth * m_subgroup_extramargin );
4248
4249 if ( x >= marginEnds )
4250 {
4251 // Outside margin.
4252
4253 if ( p->IsCategory() )
4254 {
4255 // This is category.
4256 wxPropertyCategory* pwc = (wxPropertyCategory*)p;
4257
4258 int textX = m_marginWidth + ((unsigned int)((pwc->m_depth-1)*m_subgroup_extramargin));
4259
4260 // Expand, collapse, activate etc. if click on text or left of splitter.
4261 if ( x >= textX
4262 &&
4263 ( x < (textX+pwc->GetTextExtent(this, m_captionFont)+(wxPG_CAPRECTXMARGIN*2)) ||
4264 columnHit == 0
4265 )
4266 )
4267 {
4268 if ( !DoSelectProperty( p ) )
4269 return res;
4270
4271 // On double-click, expand/collapse.
4272 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4273 {
4274 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4275 else DoExpand( p, true );
4276 }
4277 }
4278 }
4279 else if ( splitterHit == -1 )
4280 {
4281 // Click on value.
4282 unsigned int selFlag = 0;
4283 if ( columnHit == 1 )
4284 {
4285 m_iFlags |= wxPG_FL_ACTIVATION_BY_CLICK;
4286 selFlag = wxPG_SEL_FOCUS;
4287 }
4288 if ( !DoSelectProperty( p, selFlag ) )
4289 return res;
4290
4291 m_iFlags &= ~(wxPG_FL_ACTIVATION_BY_CLICK);
4292
4293 if ( p->GetChildCount() && !p->IsCategory() )
4294 // On double-click, expand/collapse.
4295 if ( event.ButtonDClick() && !(m_windowStyle & wxPG_HIDE_MARGIN) )
4296 {
4297 wxPGProperty* pwc = (wxPGProperty*)p;
4298 if ( pwc->IsExpanded() ) DoCollapse( p, true );
4299 else DoExpand( p, true );
4300 }
4301
4302 res = false;
4303 }
4304 else
4305 {
4306 // click on splitter
4307 if ( !(m_windowStyle & wxPG_STATIC_SPLITTER) )
4308 {
4309 if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
4310 {
4311 // Double-clicking the splitter causes auto-centering
4312 CenterSplitter( true );
4313 }
4314 else if ( m_dragStatus == 0 )
4315 {
4316 //
4317 // Begin draggin the splitter
4318 //
4319 if ( m_wndEditor )
4320 {
4321 // Changes must be committed here or the
4322 // value won't be drawn correctly
4323 if ( !CommitChangesFromEditor() )
4324 return res;
4325
4326 m_wndEditor->Show ( false );
4327 }
4328
4329 if ( !(m_iFlags & wxPG_FL_MOUSE_CAPTURED) )
4330 {
4331 m_canvas->CaptureMouse();
4332 m_iFlags |= wxPG_FL_MOUSE_CAPTURED;
4333 }
4334
4335 m_dragStatus = 1;
4336 m_draggedSplitter = splitterHit;
4337 m_dragOffset = splitterHitOffset;
4338
4339 wxClientDC dc(m_canvas);
4340
4341 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4342 // Fixes button disappearance bug
4343 if ( m_wndEditor2 )
4344 m_wndEditor2->Show ( false );
4345 #endif
4346
4347 m_startingSplitterX = x - splitterHitOffset;
4348 }
4349 }
4350 }
4351 }
4352 else
4353 {
4354 // Click on margin.
4355 if ( p->GetChildCount() )
4356 {
4357 int nx = x + m_marginWidth - marginEnds; // Normalize x.
4358
4359 if ( (nx >= m_gutterWidth && nx < (m_gutterWidth+m_iconWidth)) )
4360 {
4361 int y2 = y % m_lineHeight;
4362 if ( (y2 >= m_buttonSpacingY && y2 < (m_buttonSpacingY+m_iconHeight)) )
4363 {
4364 // On click on expander button, expand/collapse
4365 if ( ((wxPGProperty*)p)->IsExpanded() )
4366 DoCollapse( p, true );
4367 else
4368 DoExpand( p, true );
4369 }
4370 }
4371 }
4372 }
4373 }
4374 return res;
4375 }
4376
4377 // -----------------------------------------------------------------------
4378
4379 bool wxPropertyGrid::HandleMouseRightClick( int WXUNUSED(x), unsigned int WXUNUSED(y),
4380 wxMouseEvent& WXUNUSED(event) )
4381 {
4382 if ( m_propHover )
4383 {
4384 // Select property here as well
4385 wxPGProperty* p = m_propHover;
4386 if ( p != m_selected )
4387 DoSelectProperty( p );
4388
4389 // Send right click event.
4390 SendEvent( wxEVT_PG_RIGHT_CLICK, p );
4391
4392 return true;
4393 }
4394 return false;
4395 }
4396
4397 // -----------------------------------------------------------------------
4398
4399 bool wxPropertyGrid::HandleMouseDoubleClick( int WXUNUSED(x), unsigned int WXUNUSED(y),
4400 wxMouseEvent& WXUNUSED(event) )
4401 {
4402 if ( m_propHover )
4403 {
4404 // Select property here as well
4405 wxPGProperty* p = m_propHover;
4406
4407 if ( p != m_selected )
4408 DoSelectProperty( p );
4409
4410 // Send double-click event.
4411 SendEvent( wxEVT_PG_DOUBLE_CLICK, m_propHover );
4412
4413 return true;
4414 }
4415 return false;
4416 }
4417
4418 // -----------------------------------------------------------------------
4419
4420 #if wxPG_SUPPORT_TOOLTIPS
4421
4422 void wxPropertyGrid::SetToolTip( const wxString& tipString )
4423 {
4424 if ( tipString.length() )
4425 {
4426 m_canvas->SetToolTip(tipString);
4427 }
4428 else
4429 {
4430 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4431 m_canvas->SetToolTip( m_emptyString );
4432 #else
4433 m_canvas->SetToolTip( NULL );
4434 #endif
4435 }
4436 }
4437
4438 #endif // #if wxPG_SUPPORT_TOOLTIPS
4439
4440 // -----------------------------------------------------------------------
4441
4442 // Return false if should be skipped
4443 bool wxPropertyGrid::HandleMouseMove( int x, unsigned int y, wxMouseEvent &event )
4444 {
4445 // Safety check (needed because mouse capturing may
4446 // otherwise freeze the control)
4447 if ( m_dragStatus > 0 && !event.Dragging() )
4448 {
4449 HandleMouseUp(x,y,event);
4450 }
4451
4452 wxPropertyGridPageState* state = m_pState;
4453 int splitterHit;
4454 int splitterHitOffset;
4455 int columnHit = state->HitTestH( x, &splitterHit, &splitterHitOffset );
4456 int splitterX = x - splitterHitOffset;
4457
4458 if ( m_dragStatus > 0 )
4459 {
4460 if ( x > (m_marginWidth + wxPG_DRAG_MARGIN) &&
4461 x < (m_pState->m_width - wxPG_DRAG_MARGIN) )
4462 {
4463
4464 int newSplitterX = x - m_dragOffset;
4465 int splitterX = x - splitterHitOffset;
4466
4467 // Splitter redraw required?
4468 if ( newSplitterX != splitterX )
4469 {
4470 // Move everything
4471 SetInternalFlag(wxPG_FL_DONT_CENTER_SPLITTER);
4472 state->DoSetSplitterPosition( newSplitterX, m_draggedSplitter, false );
4473 state->m_fSplitterX = (float) newSplitterX;
4474
4475 if ( m_selected )
4476 CorrectEditorWidgetSizeX();
4477
4478 Update();
4479 Refresh();
4480 }
4481
4482 m_dragStatus = 2;
4483 }
4484
4485 return false;
4486 }
4487 else
4488 {
4489
4490 int ih = m_lineHeight;
4491 int sy = y;
4492
4493 #if wxPG_SUPPORT_TOOLTIPS
4494 wxPGProperty* prevHover = m_propHover;
4495 unsigned char prevSide = m_mouseSide;
4496 #endif
4497 int curPropHoverY = y - (y % ih);
4498
4499 // On which item it hovers
4500 if ( ( !m_propHover )
4501 ||
4502 ( m_propHover && ( sy < m_propHoverY || sy >= (m_propHoverY+ih) ) )
4503 )
4504 {
4505 // Mouse moves on another property
4506
4507 m_propHover = DoGetItemAtY(y);
4508 m_propHoverY = curPropHoverY;
4509
4510 // Send hover event
4511 SendEvent( wxEVT_PG_HIGHLIGHTED, m_propHover );
4512 }
4513
4514 #if wxPG_SUPPORT_TOOLTIPS
4515 // Store which side we are on
4516 m_mouseSide = 0;
4517 if ( columnHit == 1 )
4518 m_mouseSide = 2;
4519 else if ( columnHit == 0 )
4520 m_mouseSide = 1;
4521
4522 //
4523 // If tooltips are enabled, show label or value as a tip
4524 // in case it doesn't otherwise show in full length.
4525 //
4526 if ( m_windowStyle & wxPG_TOOLTIPS )
4527 {
4528 wxToolTip* tooltip = m_canvas->GetToolTip();
4529
4530 if ( m_propHover != prevHover || prevSide != m_mouseSide )
4531 {
4532 if ( m_propHover && !m_propHover->IsCategory() )
4533 {
4534
4535 if ( GetExtraStyle() & wxPG_EX_HELP_AS_TOOLTIPS )
4536 {
4537 // Show help string as a tooltip
4538 wxString tipString = m_propHover->GetHelpString();
4539
4540 SetToolTip(tipString);
4541 }
4542 else
4543 {
4544 // Show cropped value string as a tooltip
4545 wxString tipString;
4546 int space = 0;
4547
4548 if ( m_mouseSide == 1 )
4549 {
4550 tipString = m_propHover->m_label;
4551 space = splitterX-m_marginWidth-3;
4552 }
4553 else if ( m_mouseSide == 2 )
4554 {
4555 tipString = m_propHover->GetDisplayedString();
4556
4557 space = m_width - splitterX;
4558 if ( m_propHover->m_flags & wxPG_PROP_CUSTOMIMAGE )
4559 space -= wxPG_CUSTOM_IMAGE_WIDTH + wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
4560 }
4561
4562 if ( space )
4563 {
4564 int tw, th;
4565 GetTextExtent( tipString, &tw, &th, 0, 0, &m_font );
4566 if ( tw > space )
4567 {
4568 SetToolTip( tipString );
4569 }
4570 }
4571 else
4572 {
4573 if ( tooltip )
4574 {
4575 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4576 m_canvas->SetToolTip( m_emptyString );
4577 #else
4578 m_canvas->SetToolTip( NULL );
4579 #endif
4580 }
4581 }
4582
4583 }
4584 }
4585 else
4586 {
4587 if ( tooltip )
4588 {
4589 #if wxPG_ALLOW_EMPTY_TOOLTIPS
4590 m_canvas->SetToolTip( m_emptyString );
4591 #else
4592 m_canvas->SetToolTip( NULL );
4593 #endif
4594 }
4595 }
4596 }
4597 }
4598 #endif
4599
4600 if ( splitterHit == -1 ||
4601 !m_propHover ||
4602 HasFlag(wxPG_STATIC_SPLITTER) )
4603 {
4604 // hovering on something else
4605 if ( m_curcursor != wxCURSOR_ARROW )
4606 CustomSetCursor( wxCURSOR_ARROW );
4607 }
4608 else
4609 {
4610 // Do not allow splitter cursor on caption items.
4611 // (also not if we were dragging and its started
4612 // outside the splitter region)
4613
4614 if ( m_propHover &&
4615 !m_propHover->IsCategory() &&
4616 !event.Dragging() )
4617 {
4618
4619 // hovering on splitter
4620
4621 // NB: Condition disabled since MouseLeave event (from the editor control) cannot be
4622 // reliably detected.
4623 //if ( m_curcursor != wxCURSOR_SIZEWE )
4624 CustomSetCursor( wxCURSOR_SIZEWE, true );
4625
4626 return false;
4627 }
4628 else
4629 {
4630 // hovering on something else
4631 if ( m_curcursor != wxCURSOR_ARROW )
4632 CustomSetCursor( wxCURSOR_ARROW );
4633 }
4634 }
4635 }
4636 return true;
4637 }
4638
4639 // -----------------------------------------------------------------------
4640
4641 // Also handles Leaving event
4642 bool wxPropertyGrid::HandleMouseUp( int x, unsigned int WXUNUSED(y),
4643 wxMouseEvent &WXUNUSED(event) )
4644 {
4645 wxPropertyGridPageState* state = m_pState;
4646 bool res = false;
4647
4648 int splitterHit;
4649 int splitterHitOffset;
4650 state->HitTestH( x, &splitterHit, &splitterHitOffset );
4651
4652 // No event type check - basicly calling this method should
4653 // just stop dragging.
4654 // Left up after dragged?
4655 if ( m_dragStatus >= 1 )
4656 {
4657 //
4658 // End Splitter Dragging
4659 //
4660 // DO NOT ENABLE FOLLOWING LINE!
4661 // (it is only here as a reminder to not to do it)
4662 //splitterX = x;
4663
4664 // Disable splitter auto-centering
4665 m_iFlags |= wxPG_FL_DONT_CENTER_SPLITTER;
4666
4667 // This is necessary to return cursor
4668 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
4669 {
4670 m_canvas->ReleaseMouse();
4671 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
4672 }
4673
4674 // Set back the default cursor, if necessary
4675 if ( splitterHit == -1 ||
4676 !m_propHover )
4677 {
4678 CustomSetCursor( wxCURSOR_ARROW );
4679 }
4680
4681 m_dragStatus = 0;
4682
4683 // Control background needs to be cleared
4684 if ( !(m_iFlags & wxPG_FL_PRIMARY_FILLS_ENTIRE) && m_selected )
4685 DrawItem( m_selected );
4686
4687 if ( m_wndEditor )
4688 {
4689 m_wndEditor->Show ( true );
4690 }
4691
4692 #if wxPG_REFRESH_CONTROLS_AFTER_REPAINT
4693 // Fixes button disappearance bug
4694 if ( m_wndEditor2 )
4695 m_wndEditor2->Show ( true );
4696 #endif
4697
4698 // This clears the focus.
4699 m_editorFocused = 0;
4700
4701 }
4702 return res;
4703 }
4704
4705 // -----------------------------------------------------------------------
4706
4707 bool wxPropertyGrid::OnMouseCommon( wxMouseEvent& event, int* px, int* py )
4708 {
4709 int splitterX = GetSplitterPosition();
4710
4711 //int ux, uy;
4712 //CalcUnscrolledPosition( event.m_x, event.m_y, &ux, &uy );
4713 int ux = event.m_x;
4714 int uy = event.m_y;
4715
4716 wxWindow* wnd = m_wndEditor;
4717
4718 // Hide popup on clicks
4719 if ( event.GetEventType() != wxEVT_MOTION )
4720 if ( wnd && wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
4721 {
4722 ((wxOwnerDrawnComboBox*)m_wndEditor)->HidePopup();
4723 }
4724
4725 wxRect r;
4726 if ( wnd )
4727 r = wnd->GetRect();
4728 if ( wnd == (wxWindow*) NULL || m_dragStatus ||
4729 (
4730 ux <= (splitterX + wxPG_SPLITTERX_DETECTMARGIN2) ||
4731 ux >= (r.x+r.width) ||
4732 event.m_y < r.y ||
4733 event.m_y >= (r.y+r.height)
4734 )
4735 )
4736 {
4737 *px = ux;
4738 *py = uy;
4739 return true;
4740 }
4741 else
4742 {
4743 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
4744 }
4745 return false;
4746 }
4747
4748 // -----------------------------------------------------------------------
4749
4750 void wxPropertyGrid::OnMouseClick( wxMouseEvent &event )
4751 {
4752 int x, y;
4753 if ( OnMouseCommon( event, &x, &y ) )
4754 {
4755 HandleMouseClick(x,y,event);
4756 }
4757 event.Skip();
4758 }
4759
4760 // -----------------------------------------------------------------------
4761
4762 void wxPropertyGrid::OnMouseRightClick( wxMouseEvent &event )
4763 {
4764 int x, y;
4765 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
4766 HandleMouseRightClick(x,y,event);
4767 event.Skip();
4768 }
4769
4770 // -----------------------------------------------------------------------
4771
4772 void wxPropertyGrid::OnMouseDoubleClick( wxMouseEvent &event )
4773 {
4774 // Always run standard mouse-down handler as well
4775 OnMouseClick(event);
4776
4777 int x, y;
4778 CalcUnscrolledPosition( event.m_x, event.m_y, &x, &y );
4779 HandleMouseDoubleClick(x,y,event);
4780 event.Skip();
4781 }
4782
4783 // -----------------------------------------------------------------------
4784
4785 void wxPropertyGrid::OnMouseMove( wxMouseEvent &event )
4786 {
4787 int x, y;
4788 if ( OnMouseCommon( event, &x, &y ) )
4789 {
4790 HandleMouseMove(x,y,event);
4791 }
4792 event.Skip();
4793 }
4794
4795 // -----------------------------------------------------------------------
4796
4797 void wxPropertyGrid::OnMouseMoveBottom( wxMouseEvent& WXUNUSED(event) )
4798 {
4799 // Called when mouse moves in the empty space below the properties.
4800 CustomSetCursor( wxCURSOR_ARROW );
4801 }
4802
4803 // -----------------------------------------------------------------------
4804
4805 void wxPropertyGrid::OnMouseUp( wxMouseEvent &event )
4806 {
4807 int x, y;
4808 if ( OnMouseCommon( event, &x, &y ) )
4809 {
4810 HandleMouseUp(x,y,event);
4811 }
4812 event.Skip();
4813 }
4814
4815 // -----------------------------------------------------------------------
4816
4817 void wxPropertyGrid::OnMouseEntry( wxMouseEvent &event )
4818 {
4819 // This may get called from child control as well, so event's
4820 // mouse position cannot be relied on.
4821
4822 if ( event.Entering() )
4823 {
4824 if ( !(m_iFlags & wxPG_FL_MOUSE_INSIDE) )
4825 {
4826 // TODO: Fix this (detect parent and only do
4827 // cursor trick if it is a manager).
4828 wxASSERT( GetParent() );
4829 GetParent()->SetCursor(wxNullCursor);
4830
4831 m_iFlags |= wxPG_FL_MOUSE_INSIDE;
4832 }
4833 else
4834 GetParent()->SetCursor(wxNullCursor);
4835 }
4836 else if ( event.Leaving() )
4837 {
4838 // Without this, wxSpinCtrl editor will sometimes have wrong cursor
4839 m_canvas->SetCursor( wxNullCursor );
4840
4841 // Get real cursor position
4842 wxPoint pt = ScreenToClient(::wxGetMousePosition());
4843
4844 if ( ( pt.x <= 0 || pt.y <= 0 || pt.x >= m_width || pt.y >= m_height ) )
4845 {
4846 {
4847 if ( (m_iFlags & wxPG_FL_MOUSE_INSIDE) )
4848 {
4849 m_iFlags &= ~(wxPG_FL_MOUSE_INSIDE);
4850 }
4851
4852 if ( m_dragStatus )
4853 wxPropertyGrid::HandleMouseUp ( -1, 10000, event );
4854 }
4855 }
4856 }
4857
4858 event.Skip();
4859 }
4860
4861 // -----------------------------------------------------------------------
4862
4863 // Common code used by various OnMouseXXXChild methods.
4864 bool wxPropertyGrid::OnMouseChildCommon( wxMouseEvent &event, int* px, int *py )
4865 {
4866 wxWindow* topCtrlWnd = (wxWindow*)event.GetEventObject();
4867 wxASSERT( topCtrlWnd );
4868 int x, y;
4869 event.GetPosition(&x,&y);
4870
4871 AdjustPosForClipperWindow( topCtrlWnd, &x, &y );
4872
4873 int splitterX = GetSplitterPosition();
4874
4875 wxRect r = topCtrlWnd->GetRect();
4876 if ( !m_dragStatus &&
4877 x > (splitterX-r.x+wxPG_SPLITTERX_DETECTMARGIN2) &&
4878 y >= 0 && y < r.height \
4879 )
4880 {
4881 if ( m_curcursor != wxCURSOR_ARROW ) CustomSetCursor ( wxCURSOR_ARROW );
4882 event.Skip();
4883 }
4884 else
4885 {
4886 CalcUnscrolledPosition( event.m_x + r.x, event.m_y + r.y, \
4887 px, py );
4888 return true;
4889 }
4890 return false;
4891 }
4892
4893 void wxPropertyGrid::OnMouseClickChild( wxMouseEvent &event )
4894 {
4895 int x,y;
4896 if ( OnMouseChildCommon(event,&x,&y) )
4897 {
4898 bool res = HandleMouseClick(x,y,event);
4899 if ( !res ) event.Skip();
4900 }
4901 }
4902
4903 void wxPropertyGrid::OnMouseRightClickChild( wxMouseEvent &event )
4904 {
4905 int x,y;
4906 wxASSERT( m_wndEditor );
4907 // These coords may not be exact (about +-2),
4908 // but that should not matter (right click is about item, not position).
4909 wxPoint pt = m_wndEditor->GetPosition();
4910 CalcUnscrolledPosition( event.m_x + pt.x, event.m_y + pt.y, &x, &y );
4911 wxASSERT( m_selected );
4912 m_propHover = m_selected;
4913 bool res = HandleMouseRightClick(x,y,event);
4914 if ( !res ) event.Skip();
4915 }
4916
4917 void wxPropertyGrid::OnMouseMoveChild( wxMouseEvent &event )
4918 {
4919 int x,y;
4920 if ( OnMouseChildCommon(event,&x,&y) )
4921 {
4922 bool res = HandleMouseMove(x,y,event);
4923 if ( !res ) event.Skip();
4924 }
4925 }
4926
4927 void wxPropertyGrid::OnMouseUpChild( wxMouseEvent &event )
4928 {
4929 int x,y;
4930 if ( OnMouseChildCommon(event,&x,&y) )
4931 {
4932 bool res = HandleMouseUp(x,y,event);
4933 if ( !res ) event.Skip();
4934 }
4935 }
4936
4937 // -----------------------------------------------------------------------
4938 // wxPropertyGrid keyboard event handling
4939 // -----------------------------------------------------------------------
4940
4941 void wxPropertyGrid::SendNavigationKeyEvent( int dir )
4942 {
4943 wxNavigationKeyEvent evt;
4944 evt.SetFlags(wxNavigationKeyEvent::FromTab|
4945 (dir?wxNavigationKeyEvent::IsForward:
4946 wxNavigationKeyEvent::IsBackward));
4947 evt.SetEventObject(this);
4948 m_canvas->GetEventHandler()->AddPendingEvent(evt);
4949 }
4950
4951
4952 int wxPropertyGrid::KeyEventToActions(wxKeyEvent &event, int* pSecond) const
4953 {
4954 // Translates wxKeyEvent to wxPG_ACTION_XXX
4955
4956 int keycode = event.GetKeyCode();
4957 int modifiers = event.GetModifiers();
4958
4959 wxASSERT( !(modifiers&~(0xFFFF)) );
4960
4961 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
4962
4963 wxPGHashMapI2I::const_iterator it = m_actionTriggers.find(hashMapKey);
4964
4965 if ( it == m_actionTriggers.end() )
4966 return 0;
4967
4968 if ( pSecond )
4969 {
4970 int second = (it->second>>16) & 0xFFFF;
4971 *pSecond = second;
4972 }
4973
4974 return (it->second & 0xFFFF);
4975 }
4976
4977 void wxPropertyGrid::AddActionTrigger( int action, int keycode, int modifiers )
4978 {
4979 wxASSERT( !(modifiers&~(0xFFFF)) );
4980
4981 int hashMapKey = (keycode & 0xFFFF) | ((modifiers & 0xFFFF) << 16);
4982
4983 wxPGHashMapI2I::iterator it = m_actionTriggers.find(hashMapKey);
4984
4985 if ( it != m_actionTriggers.end() )
4986 {
4987 // This key combination is already used
4988
4989 // Can add secondary?
4990 wxASSERT_MSG( !(it->second&~(0xFFFF)),
4991 wxT("You can only add up to two separate actions per key combination.") );
4992
4993 action = it->second | (action<<16);
4994 }
4995
4996 m_actionTriggers[hashMapKey] = action;
4997 }
4998
4999 void wxPropertyGrid::ClearActionTriggers( int action )
5000 {
5001 wxPGHashMapI2I::iterator it;
5002
5003 for ( it = m_actionTriggers.begin(); it != m_actionTriggers.end(); it++ )
5004 {
5005 if ( it->second == action )
5006 {
5007 m_actionTriggers.erase(it);
5008 }
5009 }
5010 }
5011
5012 static void CopyTextToClipboard( const wxString& text )
5013 {
5014 if ( wxTheClipboard->Open() )
5015 {
5016 // This data objects are held by the clipboard,
5017 // so do not delete them in the app.
5018 wxTheClipboard->SetData( new wxTextDataObject(text) );
5019 wxTheClipboard->Close();
5020 }
5021 }
5022
5023 void wxPropertyGrid::HandleKeyEvent(wxKeyEvent &event)
5024 {
5025 //
5026 // Handles key event when editor control is not focused.
5027 //
5028
5029 wxASSERT( !m_frozen );
5030 if ( m_frozen )
5031 return;
5032
5033 // Travelsal between items, collapsing/expanding, etc.
5034 int keycode = event.GetKeyCode();
5035
5036 if ( keycode == WXK_TAB )
5037 {
5038 SendNavigationKeyEvent( event.ShiftDown()?0:1 );
5039 return;
5040 }
5041
5042 // Ignore Alt and Control when they are down alone
5043 if ( keycode == WXK_ALT ||
5044 keycode == WXK_CONTROL )
5045 {
5046 event.Skip();
5047 return;
5048 }
5049
5050 int secondAction;
5051 int action = KeyEventToActions(event, &secondAction);
5052
5053 if ( m_selected )
5054 {
5055
5056 // Show dialog?
5057 if ( ButtonTriggerKeyTest(event) )
5058 return;
5059
5060 wxPGProperty* p = m_selected;
5061
5062 if ( action == wxPG_ACTION_COPY )
5063 {
5064 CopyTextToClipboard(p->GetDisplayedString());
5065 }
5066 else
5067 {
5068 // Travel and expand/collapse
5069 int selectDir = -2;
5070
5071 if ( p->GetChildCount() &&
5072 !(p->m_flags & wxPG_PROP_DISABLED)
5073 )
5074 {
5075 if ( action == wxPG_ACTION_COLLAPSE_PROPERTY || secondAction == wxPG_ACTION_COLLAPSE_PROPERTY )
5076 {
5077 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Collapse(p) )
5078 keycode = 0;
5079 }
5080 else if ( action == wxPG_ACTION_EXPAND_PROPERTY || secondAction == wxPG_ACTION_EXPAND_PROPERTY )
5081 {
5082 if ( (m_windowStyle & wxPG_HIDE_MARGIN) || Expand(p) )
5083 keycode = 0;
5084 }
5085 }
5086
5087 if ( keycode )
5088 {
5089 if ( action == wxPG_ACTION_PREV_PROPERTY || secondAction == wxPG_ACTION_PREV_PROPERTY )
5090 {
5091 selectDir = -1;
5092 }
5093 else if ( action == wxPG_ACTION_NEXT_PROPERTY || secondAction == wxPG_ACTION_NEXT_PROPERTY )
5094 {
5095 selectDir = 1;
5096 }
5097 else
5098 {
5099 event.Skip();
5100 }
5101
5102 }
5103
5104 if ( selectDir >= -1 )
5105 {
5106 p = wxPropertyGridIterator::OneStep( m_pState, wxPG_ITERATE_VISIBLE, p, selectDir );
5107 if ( p )
5108 DoSelectProperty(p);
5109 }
5110 }
5111 }
5112 else
5113 {
5114 // If nothing was selected, select the first item now
5115 // (or navigate out of tab).
5116 if ( action != wxPG_ACTION_CANCEL_EDIT && secondAction != wxPG_ACTION_CANCEL_EDIT )
5117 {
5118 wxPGProperty* p = wxPropertyGridInterface::GetFirst();
5119 if ( p ) DoSelectProperty(p);
5120 }
5121 }
5122 }
5123
5124 // -----------------------------------------------------------------------
5125
5126 // Potentially handles a keyboard event for editor controls.
5127 // Returns false if event should *not* be skipped (on true it can
5128 // be optionally skipped).
5129 // Basicly, false means that SelectProperty was called (or was about
5130 // to be called, if canDestroy was false).
5131 bool wxPropertyGrid::HandleChildKey( wxKeyEvent& event )
5132 {
5133 bool res = true;
5134
5135 if ( !m_selected || !m_wndEditor )
5136 {
5137 return true;
5138 }
5139
5140 int action = KeyEventToAction(event);
5141
5142 // Unfocus?
5143 if ( action == wxPG_ACTION_CANCEL_EDIT )
5144 {
5145 //
5146 // Esc cancels any changes
5147 if ( IsEditorsValueModified() )
5148 {
5149 EditorsValueWasNotModified();
5150
5151 // Update the control as well
5152 m_selected->GetEditorClass()->SetControlStringValue( m_selected,
5153 m_wndEditor,
5154 m_selected->GetDisplayedString() );
5155 }
5156
5157 OnValidationFailureReset(m_selected);
5158
5159 res = false;
5160
5161 UnfocusEditor();
5162 }
5163 else if ( action == wxPG_ACTION_COPY )
5164 {
5165 // NB: There is some problem with getting native cut-copy-paste keys to go through
5166 // for embedded editor wxTextCtrl. This is why we emulate.
5167 //
5168 wxTextCtrl* tc = GetEditorTextCtrl();
5169 if ( tc )
5170 {
5171 wxString sel = tc->GetStringSelection();
5172 if ( sel.length() )
5173 CopyTextToClipboard(sel);
5174 }
5175 else
5176 {
5177 CopyTextToClipboard(m_selected->GetDisplayedString());
5178 }
5179 }
5180 else if ( action == wxPG_ACTION_CUT )
5181 {
5182 wxTextCtrl* tc = GetEditorTextCtrl();
5183 if ( tc )
5184 {
5185 long from, to;
5186 tc->GetSelection(&from, &to);
5187 if ( from < to )
5188 {
5189 CopyTextToClipboard(tc->GetStringSelection());
5190 tc->Remove(from, to);
5191 }
5192 }
5193 }
5194 else if ( action == wxPG_ACTION_PASTE )
5195 {
5196 wxTextCtrl* tc = GetEditorTextCtrl();
5197 if ( tc )
5198 {
5199 if (wxTheClipboard->Open())
5200 {
5201 if (wxTheClipboard->IsSupported( wxDF_TEXT ))
5202 {
5203 wxTextDataObject data;
5204 wxTheClipboard->GetData( data );
5205 long from, to;
5206 tc->GetSelection(&from, &to);
5207 if ( from < to )
5208 {
5209 tc->Remove(from, to);
5210 tc->WriteText(data.GetText());
5211 }
5212 else
5213 {
5214 tc->WriteText(data.GetText());
5215 }
5216 }
5217 wxTheClipboard->Close();
5218 }
5219 }
5220 }
5221
5222 return res;
5223 }
5224
5225 // -----------------------------------------------------------------------
5226
5227 void wxPropertyGrid::OnKey( wxKeyEvent &event )
5228 {
5229
5230 //
5231 // Events to editor controls should get relayed here.
5232 //
5233 wxWindow* focused = wxWindow::FindFocus();
5234
5235 wxWindow* primaryCtrl = GetEditorControl();
5236
5237 if ( primaryCtrl &&
5238 (focused==primaryCtrl
5239 || m_editorFocused) )
5240 {
5241 // Child key must be processed here, since it can
5242 // destroy the control which is referred by its own
5243 // event handling.
5244 HandleChildKey( event );
5245 }
5246 else
5247 HandleKeyEvent( event );
5248 }
5249
5250 // -----------------------------------------------------------------------
5251
5252 void wxPropertyGrid::OnKeyUp(wxKeyEvent &event)
5253 {
5254 m_keyComboConsumed = 0;
5255
5256 event.Skip();
5257 }
5258
5259 // -----------------------------------------------------------------------
5260
5261 void wxPropertyGrid::OnNavigationKey( wxNavigationKeyEvent& event )
5262 {
5263 // Ignore events that occur very close to focus set
5264 if ( m_iFlags & wxPG_FL_IGNORE_NEXT_NAVKEY )
5265 {
5266 m_iFlags &= ~(wxPG_FL_IGNORE_NEXT_NAVKEY);
5267 event.Skip();
5268 return;
5269 }
5270
5271 wxPGProperty* next = (wxPGProperty*) NULL;
5272
5273 int dir = event.GetDirection()?1:-1;
5274
5275 if ( m_selected )
5276 {
5277 if ( dir == 1 && (m_wndEditor || m_wndEditor2) )
5278 {
5279 wxWindow* focused = wxWindow::FindFocus();
5280
5281 wxWindow* wndToCheck = GetEditorControl();
5282
5283 // ODComboBox focus goes to its text ctrl, so we need to use it instead
5284 if ( wndToCheck && wndToCheck->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
5285 {
5286 wxTextCtrl* comboTextCtrl = ((wxOwnerDrawnComboBox*)wndToCheck)->GetTextCtrl();
5287 if ( comboTextCtrl )
5288 wndToCheck = comboTextCtrl;
5289 }
5290
5291 /*
5292 // Because of problems navigating from wxButton, do not go to it.
5293 if ( !wndToCheck )
5294 {
5295 // No primary, use secondary
5296 wndToCheck = m_wndEditor2;
5297 }
5298 // If it has editor button, focus to it after the primary editor.
5299 // NB: Doesn't work since wxButton on wxMSW doesn't seem to propagate
5300 // key events (yes, I'm using wxWANTS_CHARS with it, and yes I
5301 // have somewhat debugged in window.cpp itself).
5302 else if ( focused == wndToCheck &&
5303 m_wndEditor2 &&
5304 !(GetExtraStyle() & wxPG_EX_NO_TAB_TO_BUTTON) )
5305 {
5306 wndToCheck = m_wndEditor2;
5307 wxLogDebug(wxT("Exp1"));
5308 }
5309 */
5310
5311 if ( focused != wndToCheck &&
5312 wndToCheck )
5313 {
5314 wndToCheck->SetFocus();
5315
5316 // Select all text in wxTextCtrl etc.
5317 if ( m_wndEditor && wndToCheck == m_wndEditor )
5318 m_selected->GetEditorClass()->OnFocus(m_selected,wndToCheck);
5319
5320 m_editorFocused = 1;
5321 next = m_selected;
5322 }
5323 }
5324
5325 if ( !next )
5326 {
5327 next = wxPropertyGridIterator::OneStep(m_pState, wxPG_ITERATE_VISIBLE, m_selected, dir);
5328
5329 if ( next )
5330 {
5331 // This allows preventing NavigateOut to occur
5332 DoSelectProperty( next, wxPG_SEL_FOCUS );
5333 }
5334 }
5335 }
5336
5337 if ( !next )
5338 event.Skip();
5339 }
5340
5341 // -----------------------------------------------------------------------
5342
5343 bool wxPropertyGrid::ButtonTriggerKeyTest( wxKeyEvent &event )
5344 {
5345 int keycode = event.GetKeyCode();
5346
5347 // Does the keycode trigger button?
5348 if ( keycode == m_pushButKeyCode &&
5349 m_wndEditor2 &&
5350 (!m_pushButKeyCodeNeedsAlt || event.AltDown()) &&
5351 (!m_pushButKeyCodeNeedsCtrl || event.ControlDown()) )
5352 {
5353 m_keyComboConsumed = 1;
5354
5355 wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED,m_wndEditor2->GetId());
5356 GetEventHandler()->AddPendingEvent(evt);
5357 return true;
5358 }
5359
5360 return false;
5361 }
5362
5363 // -----------------------------------------------------------------------
5364
5365 void wxPropertyGrid::OnChildKeyDown( wxKeyEvent &event )
5366 {
5367 int keycode = event.GetKeyCode();
5368
5369 // Ignore Alt and Control when they are down alone
5370 if ( keycode == WXK_ALT ||
5371 keycode == WXK_CONTROL )
5372 {
5373 event.Skip();
5374 return;
5375 }
5376
5377 if ( ButtonTriggerKeyTest(event) )
5378 return;
5379
5380 if ( HandleChildKey(event) == true )
5381 event.Skip();
5382
5383 GetEventHandler()->AddPendingEvent(event);
5384 }
5385
5386 void wxPropertyGrid::OnChildKeyUp( wxKeyEvent &event )
5387 {
5388 m_keyComboConsumed = 0;
5389
5390 GetEventHandler()->AddPendingEvent(event);
5391
5392 event.Skip();
5393 }
5394
5395 // -----------------------------------------------------------------------
5396 // wxPropertyGrid miscellaneous event handling
5397 // -----------------------------------------------------------------------
5398
5399 void wxPropertyGrid::OnIdle( wxIdleEvent& WXUNUSED(event) )
5400 {
5401 //
5402 // Check if the focus is in this control or one of its children
5403 wxWindow* newFocused = wxWindow::FindFocus();
5404
5405 if ( newFocused != m_curFocused )
5406 HandleFocusChange( newFocused );
5407 }
5408
5409 // Called by focus event handlers. newFocused is the window that becomes focused.
5410 void wxPropertyGrid::HandleFocusChange( wxWindow* newFocused )
5411 {
5412 unsigned int oldFlags = m_iFlags;
5413
5414 m_iFlags &= ~(wxPG_FL_FOCUSED);
5415
5416 wxWindow* parent = newFocused;
5417
5418 // This must be one of nextFocus' parents.
5419 while ( parent )
5420 {
5421 // Use m_eventObject, which is either wxPropertyGrid or
5422 // wxPropertyGridManager, as appropriate.
5423 if ( parent == m_eventObject )
5424 {
5425 m_iFlags |= wxPG_FL_FOCUSED;
5426 break;
5427 }
5428 parent = parent->GetParent();
5429 }
5430
5431 m_curFocused = newFocused;
5432
5433 if ( (m_iFlags & wxPG_FL_FOCUSED) !=
5434 (oldFlags & wxPG_FL_FOCUSED) )
5435 {
5436 // On each focus kill, mark the next nav key event
5437 // to be ignored (can't do on set focus since the
5438 // event would occur before it).
5439 if ( !(m_iFlags & wxPG_FL_FOCUSED) )
5440 {
5441 m_iFlags |= wxPG_FL_IGNORE_NEXT_NAVKEY;
5442
5443 // Need to store changed value
5444 CommitChangesFromEditor();
5445 }
5446 else
5447 {
5448 /*
5449 //
5450 // Preliminary code for tab-order respecting
5451 // tab-traversal (but should be moved to
5452 // OnNav handler)
5453 //
5454 wxWindow* prevFocus = event.GetWindow();
5455 wxWindow* useThis = this;
5456 if ( m_iFlags & wxPG_FL_IN_MANAGER )
5457 useThis = GetParent();
5458
5459 if ( prevFocus &&
5460 prevFocus->GetParent() == useThis->GetParent() )
5461 {
5462 wxList& children = useThis->GetParent()->GetChildren();
5463
5464 wxNode* node = children.Find(prevFocus);
5465
5466 if ( node->GetNext() &&
5467 useThis == node->GetNext()->GetData() )
5468 DoSelectProperty(GetFirst());
5469 else if ( node->GetPrevious () &&
5470 useThis == node->GetPrevious()->GetData() )
5471 DoSelectProperty(GetLastProperty());
5472
5473 }
5474 */
5475
5476 m_iFlags &= ~(wxPG_FL_IGNORE_NEXT_NAVKEY);
5477 }
5478
5479 // Redraw selected
5480 if ( m_selected && (m_iFlags & wxPG_FL_INITIALIZED) )
5481 DrawItem( m_selected );
5482 }
5483 }
5484
5485 void wxPropertyGrid::OnFocusEvent( wxFocusEvent& event )
5486 {
5487 if ( event.GetEventType() == wxEVT_SET_FOCUS )
5488 HandleFocusChange((wxWindow*)event.GetEventObject());
5489 // Line changed to "else" when applying wxPropertyGrid patch #1675902
5490 //else if ( event.GetWindow() )
5491 else
5492 HandleFocusChange(event.GetWindow());
5493
5494 event.Skip();
5495 }
5496
5497 // -----------------------------------------------------------------------
5498
5499 void wxPropertyGrid::OnChildFocusEvent( wxChildFocusEvent& event )
5500 {
5501 HandleFocusChange((wxWindow*)event.GetEventObject());
5502
5503 //
5504 // event.Skip() being commented out is aworkaround for bug reported
5505 // in ticket #4840 (wxScrolledWindow problem with automatic scrolling).
5506 //event.Skip();
5507 }
5508
5509 // -----------------------------------------------------------------------
5510
5511 void wxPropertyGrid::OnScrollEvent( wxScrollWinEvent &event )
5512 {
5513 m_iFlags |= wxPG_FL_SCROLLED;
5514
5515 event.Skip();
5516 }
5517
5518 // -----------------------------------------------------------------------
5519
5520 void wxPropertyGrid::OnCaptureChange( wxMouseCaptureChangedEvent& WXUNUSED(event) )
5521 {
5522 if ( m_iFlags & wxPG_FL_MOUSE_CAPTURED )
5523 {
5524 m_iFlags &= ~(wxPG_FL_MOUSE_CAPTURED);
5525 }
5526 }
5527
5528 // -----------------------------------------------------------------------
5529 // Property editor related functions
5530 // -----------------------------------------------------------------------
5531
5532 // noDefCheck = true prevents infinite recursion.
5533 wxPGEditor* wxPropertyGrid::RegisterEditorClass( wxPGEditor* editorclass,
5534 const wxString& name,
5535 bool noDefCheck )
5536 {
5537 wxASSERT( editorclass );
5538
5539 if ( !noDefCheck && wxPGGlobalVars->m_mapEditorClasses.empty() )
5540 RegisterDefaultEditors();
5541
5542 wxPGGlobalVars->m_mapEditorClasses[name] = (void*)editorclass;
5543
5544 return editorclass;
5545 }
5546
5547 // Registers all default editor classes
5548 void wxPropertyGrid::RegisterDefaultEditors()
5549 {
5550 wxPGRegisterDefaultEditorClass( TextCtrl );
5551 wxPGRegisterDefaultEditorClass( Choice );
5552 wxPGRegisterDefaultEditorClass( ComboBox );
5553 wxPGRegisterDefaultEditorClass( TextCtrlAndButton );
5554 #if wxPG_INCLUDE_CHECKBOX
5555 wxPGRegisterDefaultEditorClass( CheckBox );
5556 #endif
5557 wxPGRegisterDefaultEditorClass( ChoiceAndButton );
5558
5559 // Register SpinCtrl etc. editors before use
5560 RegisterAdditionalEditors();
5561 }
5562
5563 // -----------------------------------------------------------------------
5564 // wxPGStringTokenizer
5565 // Needed to handle C-style string lists (e.g. "str1" "str2")
5566 // -----------------------------------------------------------------------
5567
5568 wxPGStringTokenizer::wxPGStringTokenizer( const wxString& str, wxChar delimeter )
5569 : m_str(&str), m_curPos(str.begin()), m_delimeter(delimeter)
5570 {
5571 }
5572
5573 wxPGStringTokenizer::~wxPGStringTokenizer()
5574 {
5575 }
5576
5577 bool wxPGStringTokenizer::HasMoreTokens()
5578 {
5579 const wxString& str = *m_str;
5580
5581 wxString::const_iterator i = m_curPos;
5582
5583 wxUniChar delim = m_delimeter;
5584 wxUniChar a;
5585 wxUniChar prev_a = wxT('\0');
5586
5587 bool inToken = false;
5588
5589 while ( i != str.end() )
5590 {
5591 a = *i;
5592
5593 if ( !inToken )
5594 {
5595 if ( a == delim )
5596 {
5597 inToken = true;
5598 m_readyToken.clear();
5599 }
5600 }
5601 else
5602 {
5603 if ( prev_a != wxT('\\') )
5604 {
5605 if ( a != delim )
5606 {
5607 if ( a != wxT('\\') )
5608 m_readyToken << a;
5609 }
5610 else
5611 {
5612 i++;
5613 m_curPos = i;
5614 return true;
5615 }
5616 prev_a = a;
5617 }
5618 else
5619 {
5620 m_readyToken << a;
5621 prev_a = wxT('\0');
5622 }
5623 }
5624 i++;
5625 }
5626
5627 m_curPos = str.end();
5628
5629 if ( inToken )
5630 return true;
5631
5632 return false;
5633 }
5634
5635 wxString wxPGStringTokenizer::GetNextToken()
5636 {
5637 return m_readyToken;
5638 }
5639
5640 // -----------------------------------------------------------------------
5641 // wxPGChoiceEntry
5642 // -----------------------------------------------------------------------
5643
5644 wxPGChoiceEntry::wxPGChoiceEntry()
5645 : wxPGCell(), m_value(wxPG_INVALID_VALUE)
5646 {
5647 }
5648
5649 wxPGChoiceEntry::wxPGChoiceEntry( const wxPGChoiceEntry& entry )
5650 : wxPGCell( entry.GetText(), entry.GetBitmap(),
5651 entry.GetFgCol(), entry.GetBgCol() ), m_value(entry.GetValue())
5652 {
5653 }
5654
5655 // -----------------------------------------------------------------------
5656 // wxPGChoicesData
5657 // -----------------------------------------------------------------------
5658
5659 wxPGChoicesData::wxPGChoicesData()
5660 {
5661 m_refCount = 1;
5662 }
5663
5664 wxPGChoicesData::~wxPGChoicesData()
5665 {
5666 Clear();
5667 }
5668
5669 void wxPGChoicesData::Clear()
5670 {
5671 unsigned int i;
5672
5673 for ( i=0; i<m_items.size(); i++ )
5674 {
5675 delete Item(i);
5676 }
5677
5678 #if wxUSE_STL
5679 m_items.resize(0);
5680 #else
5681 m_items.Empty();
5682 #endif
5683 }
5684
5685 void wxPGChoicesData::CopyDataFrom( wxPGChoicesData* data )
5686 {
5687 wxASSERT( m_items.size() == 0 );
5688
5689 unsigned int i;
5690
5691 for ( i=0; i<data->GetCount(); i++ )
5692 m_items.push_back( new wxPGChoiceEntry(*data->Item(i)) );
5693 }
5694
5695 // -----------------------------------------------------------------------
5696 // wxPGChoices
5697 // -----------------------------------------------------------------------
5698
5699 wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, int value )
5700 {
5701 EnsureData();
5702
5703 wxPGChoiceEntry* p = new wxPGChoiceEntry(label, value);
5704 m_data->Insert( -1, p );
5705 return *p;
5706 }
5707
5708 // -----------------------------------------------------------------------
5709
5710 wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, const wxBitmap& bitmap, int value )
5711 {
5712 EnsureData();
5713
5714 wxPGChoiceEntry* p = new wxPGChoiceEntry(label, value);
5715 p->SetBitmap(bitmap);
5716 m_data->Insert( -1, p );
5717 return *p;
5718 }
5719
5720 // -----------------------------------------------------------------------
5721
5722 wxPGChoiceEntry& wxPGChoices::Insert( const wxPGChoiceEntry& entry, int index )
5723 {
5724 EnsureData();
5725
5726 wxPGChoiceEntry* p = new wxPGChoiceEntry(entry);
5727 m_data->Insert(index, p);
5728 return *p;
5729 }
5730
5731 // -----------------------------------------------------------------------
5732
5733 wxPGChoiceEntry& wxPGChoices::Insert( const wxString& label, int index, int value )
5734 {
5735 EnsureData();
5736
5737 wxPGChoiceEntry* p = new wxPGChoiceEntry(label, value);
5738 m_data->Insert( index, p );
5739 return *p;
5740 }
5741
5742 // -----------------------------------------------------------------------
5743
5744 wxPGChoiceEntry& wxPGChoices::AddAsSorted( const wxString& label, int value )
5745 {
5746 EnsureData();
5747
5748 size_t index = 0;
5749
5750 while ( index < GetCount() )
5751 {
5752 int cmpRes = GetLabel(index).Cmp(label);
5753 if ( cmpRes > 0 )
5754 break;
5755 index++;
5756 }
5757
5758 wxPGChoiceEntry* p = new wxPGChoiceEntry(label, value);
5759 m_data->Insert( index, p );
5760 return *p;
5761 }
5762
5763 // -----------------------------------------------------------------------
5764
5765 void wxPGChoices::Add( const wxChar** labels, const ValArrItem* values )
5766 {
5767 EnsureData();
5768
5769 unsigned int itemcount = 0;
5770 const wxChar** p = &labels[0];
5771 while ( *p ) { p++; itemcount++; }
5772
5773 unsigned int i;
5774 for ( i = 0; i < itemcount; i++ )
5775 {
5776 int value = wxPG_INVALID_VALUE;
5777 if ( values )
5778 value = values[i];
5779 m_data->Insert( -1, new wxPGChoiceEntry(labels[i], value) );
5780 }
5781 }
5782
5783 // -----------------------------------------------------------------------
5784
5785 void wxPGChoices::Add( const wxArrayString& arr, const ValArrItem* values )
5786 {
5787 EnsureData();
5788
5789 unsigned int i;
5790 unsigned int itemcount = arr.size();
5791
5792 for ( i = 0; i < itemcount; i++ )
5793 {
5794 int value = wxPG_INVALID_VALUE;
5795 if ( values )
5796 value = values[i];
5797 m_data->Insert( -1, new wxPGChoiceEntry(arr[i], value) );
5798 }
5799 }
5800
5801 // -----------------------------------------------------------------------
5802
5803 void wxPGChoices::Add( const wxArrayString& arr, const wxArrayInt& arrint )
5804 {
5805 EnsureData();
5806
5807 unsigned int i;
5808 unsigned int itemcount = arr.size();
5809
5810 for ( i = 0; i < itemcount; i++ )
5811 {
5812 int value = wxPG_INVALID_VALUE;
5813 if ( &arrint && arrint.size() )
5814 value = arrint[i];
5815 m_data->Insert( -1, new wxPGChoiceEntry(arr[i], value) );
5816 }
5817 }
5818
5819 // -----------------------------------------------------------------------
5820
5821 void wxPGChoices::RemoveAt(size_t nIndex, size_t count)
5822 {
5823 wxASSERT( m_data->m_refCount != 0xFFFFFFF );
5824 unsigned int i;
5825 for ( i=nIndex; i<(nIndex+count); i++)
5826 delete m_data->Item(i);
5827 m_data->m_items.RemoveAt(nIndex, count);
5828 }
5829
5830 // -----------------------------------------------------------------------
5831
5832 int wxPGChoices::Index( const wxString& str ) const
5833 {
5834 if ( IsOk() )
5835 {
5836 unsigned int i;
5837 for ( i=0; i< m_data->GetCount(); i++ )
5838 {
5839 if ( m_data->Item(i)->GetText() == str )
5840 return i;
5841 }
5842 }
5843 return -1;
5844 }
5845
5846 // -----------------------------------------------------------------------
5847
5848 int wxPGChoices::Index( int val ) const
5849 {
5850 if ( IsOk() )
5851 {
5852 unsigned int i;
5853 for ( i=0; i< m_data->GetCount(); i++ )
5854 {
5855 if ( m_data->Item(i)->GetValue() == val )
5856 return i;
5857 }
5858 }
5859 return -1;
5860 }
5861
5862 // -----------------------------------------------------------------------
5863
5864 wxArrayString wxPGChoices::GetLabels() const
5865 {
5866 wxArrayString arr;
5867 unsigned int i;
5868
5869 if ( this && IsOk() )
5870 for ( i=0; i<GetCount(); i++ )
5871 arr.push_back(GetLabel(i));
5872
5873 return arr;
5874 }
5875
5876 // -----------------------------------------------------------------------
5877
5878 bool wxPGChoices::HasValues() const
5879 {
5880 return true;
5881 }
5882
5883 // -----------------------------------------------------------------------
5884
5885 wxArrayInt wxPGChoices::GetValuesForStrings( const wxArrayString& strings ) const
5886 {
5887 wxArrayInt arr;
5888
5889 if ( IsOk() )
5890 {
5891 unsigned int i;
5892 for ( i=0; i< strings.size(); i++ )
5893 {
5894 int index = Index(strings[i]);
5895 if ( index >= 0 )
5896 arr.Add(GetValue(index));
5897 else
5898 arr.Add(wxPG_INVALID_VALUE);
5899 }
5900 }
5901
5902 return arr;
5903 }
5904
5905 // -----------------------------------------------------------------------
5906
5907 wxArrayInt wxPGChoices::GetIndicesForStrings( const wxArrayString& strings,
5908 wxArrayString* unmatched ) const
5909 {
5910 wxArrayInt arr;
5911
5912 if ( IsOk() )
5913 {
5914 unsigned int i;
5915 for ( i=0; i< strings.size(); i++ )
5916 {
5917 const wxString& str = strings[i];
5918 int index = Index(str);
5919 if ( index >= 0 )
5920 arr.Add(index);
5921 else if ( unmatched )
5922 unmatched->Add(str);
5923 }
5924 }
5925
5926 return arr;
5927 }
5928
5929 // -----------------------------------------------------------------------
5930
5931 void wxPGChoices::AssignData( wxPGChoicesData* data )
5932 {
5933 Free();
5934
5935 if ( data != wxPGChoicesEmptyData )
5936 {
5937 m_data = data;
5938 data->m_refCount++;
5939 }
5940 }
5941
5942 // -----------------------------------------------------------------------
5943
5944 void wxPGChoices::Init()
5945 {
5946 m_data = wxPGChoicesEmptyData;
5947 }
5948
5949 // -----------------------------------------------------------------------
5950
5951 void wxPGChoices::Free()
5952 {
5953 if ( m_data != wxPGChoicesEmptyData )
5954 {
5955 m_data->DecRef();
5956 m_data = wxPGChoicesEmptyData;
5957 }
5958 }
5959
5960 // -----------------------------------------------------------------------
5961 // wxPropertyGridEvent
5962 // -----------------------------------------------------------------------
5963
5964 IMPLEMENT_DYNAMIC_CLASS(wxPropertyGridEvent, wxCommandEvent)
5965
5966
5967 DEFINE_EVENT_TYPE( wxEVT_PG_SELECTED )
5968 DEFINE_EVENT_TYPE( wxEVT_PG_CHANGING )
5969 DEFINE_EVENT_TYPE( wxEVT_PG_CHANGED )
5970 DEFINE_EVENT_TYPE( wxEVT_PG_HIGHLIGHTED )
5971 DEFINE_EVENT_TYPE( wxEVT_PG_RIGHT_CLICK )
5972 DEFINE_EVENT_TYPE( wxEVT_PG_PAGE_CHANGED )
5973 DEFINE_EVENT_TYPE( wxEVT_PG_ITEM_EXPANDED )
5974 DEFINE_EVENT_TYPE( wxEVT_PG_ITEM_COLLAPSED )
5975 DEFINE_EVENT_TYPE( wxEVT_PG_DOUBLE_CLICK )
5976
5977
5978 // -----------------------------------------------------------------------
5979
5980 void wxPropertyGridEvent::Init()
5981 {
5982 m_validationInfo = NULL;
5983 m_canVeto = false;
5984 m_wasVetoed = false;
5985 }
5986
5987 // -----------------------------------------------------------------------
5988
5989 wxPropertyGridEvent::wxPropertyGridEvent(wxEventType commandType, int id)
5990 : wxCommandEvent(commandType,id)
5991 {
5992 m_property = NULL;
5993 Init();
5994 }
5995
5996 // -----------------------------------------------------------------------
5997
5998 wxPropertyGridEvent::wxPropertyGridEvent(const wxPropertyGridEvent& event)
5999 : wxCommandEvent(event)
6000 {
6001 m_eventType = event.GetEventType();
6002 m_eventObject = event.m_eventObject;
6003 m_pg = event.m_pg;
6004 m_property = event.m_property;
6005 m_validationInfo = event.m_validationInfo;
6006 m_canVeto = event.m_canVeto;
6007 m_wasVetoed = event.m_wasVetoed;
6008 }
6009
6010 // -----------------------------------------------------------------------
6011
6012 wxPropertyGridEvent::~wxPropertyGridEvent()
6013 {
6014 }
6015
6016 // -----------------------------------------------------------------------
6017
6018 wxEvent* wxPropertyGridEvent::Clone() const
6019 {
6020 return new wxPropertyGridEvent( *this );
6021 }
6022
6023 // -----------------------------------------------------------------------
6024 // wxPropertyGridPopulator
6025 // -----------------------------------------------------------------------
6026
6027 wxPropertyGridPopulator::wxPropertyGridPopulator()
6028 {
6029 m_state = NULL;
6030 m_pg = NULL;
6031 wxPGGlobalVars->m_offline++;
6032 }
6033
6034 // -----------------------------------------------------------------------
6035
6036 void wxPropertyGridPopulator::SetState( wxPropertyGridPageState* state )
6037 {
6038 m_state = state;
6039 m_propHierarchy.clear();
6040 }
6041
6042 // -----------------------------------------------------------------------
6043
6044 void wxPropertyGridPopulator::SetGrid( wxPropertyGrid* pg )
6045 {
6046 m_pg = pg;
6047 pg->Freeze();
6048 }
6049
6050 // -----------------------------------------------------------------------
6051
6052 wxPropertyGridPopulator::~wxPropertyGridPopulator()
6053 {
6054 //
6055 // Free unused sets of choices
6056 wxPGHashMapS2P::iterator it;
6057
6058 for( it = m_dictIdChoices.begin(); it != m_dictIdChoices.end(); ++it )
6059 {
6060 wxPGChoicesData* data = (wxPGChoicesData*) it->second;
6061 data->DecRef();
6062 }
6063
6064 if ( m_pg )
6065 {
6066 m_pg->Thaw();
6067 m_pg->GetPanel()->Refresh();
6068 }
6069 wxPGGlobalVars->m_offline--;
6070 }
6071
6072 // -----------------------------------------------------------------------
6073
6074 wxPGProperty* wxPropertyGridPopulator::Add( const wxString& propClass,
6075 const wxString& propLabel,
6076 const wxString& propName,
6077 const wxString* propValue,
6078 wxPGChoices* pChoices )
6079 {
6080 wxClassInfo* classInfo = wxClassInfo::FindClass(propClass);
6081 wxPGProperty* parent = GetCurParent();
6082
6083 if ( parent->HasFlag(wxPG_PROP_AGGREGATE) )
6084 {
6085 ProcessError(wxString::Format(wxT("new children cannot be added to '%s'"),parent->GetName().c_str()));
6086 return NULL;
6087 }
6088
6089 if ( !classInfo || !classInfo->IsKindOf(CLASSINFO(wxPGProperty)) )
6090 {
6091 ProcessError(wxString::Format(wxT("'%s' is not valid property class"),propClass.c_str()));
6092 return NULL;
6093 }
6094
6095 wxPGProperty* property = (wxPGProperty*) classInfo->CreateObject();
6096
6097 property->SetLabel(propLabel);
6098 property->DoSetName(propName);
6099
6100 if ( pChoices && pChoices->IsOk() )
6101 property->SetChoices(*pChoices);
6102
6103 m_state->DoInsert(parent, -1, property);
6104
6105 if ( propValue )
6106 property->SetValueFromString( *propValue, wxPG_FULL_VALUE );
6107
6108 return property;
6109 }
6110
6111 // -----------------------------------------------------------------------
6112
6113 void wxPropertyGridPopulator::AddChildren( wxPGProperty* property )
6114 {
6115 m_propHierarchy.push_back(property);
6116 DoScanForChildren();
6117 m_propHierarchy.pop_back();
6118 }
6119
6120 // -----------------------------------------------------------------------
6121
6122 wxPGChoices wxPropertyGridPopulator::ParseChoices( const wxString& choicesString,
6123 const wxString& idString )
6124 {
6125 wxPGChoices choices;
6126
6127 // Using id?
6128 if ( choicesString[0] == wxT('@') )
6129 {
6130 wxString ids = choicesString.substr(1);
6131 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(ids);
6132 if ( it == m_dictIdChoices.end() )
6133 ProcessError(wxString::Format(wxT("No choices defined for id '%s'"),ids.c_str()));
6134 else
6135 choices.AssignData((wxPGChoicesData*)it->second);
6136 }
6137 else
6138 {
6139 bool found = false;
6140 if ( idString.length() )
6141 {
6142 wxPGHashMapS2P::iterator it = m_dictIdChoices.find(idString);
6143 if ( it != m_dictIdChoices.end() )
6144 {
6145 choices.AssignData((wxPGChoicesData*)it->second);
6146 found = true;
6147 }
6148 }
6149
6150 if ( !found )
6151 {
6152 // Parse choices string
6153 wxString::const_iterator it = choicesString.begin();
6154 wxString label;
6155 wxString value;
6156 int state = 0;
6157 bool labelValid = false;
6158
6159 for ( ; it != choicesString.end(); it++ )
6160 {
6161 wxChar c = *it;
6162
6163 if ( state != 1 )
6164 {
6165 if ( c == wxT('"') )
6166 {
6167 if ( labelValid )
6168 {
6169 long l;
6170 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6171 choices.Add(label, l);
6172 }
6173 labelValid = false;
6174 //wxLogDebug(wxT("%s, %s"),label.c_str(),value.c_str());
6175 value.clear();
6176 label.clear();
6177 state = 1;
6178 }
6179 else if ( c == wxT('=') )
6180 {
6181 if ( labelValid )
6182 {
6183 state = 2;
6184 }
6185 }
6186 else if ( state == 2 && (wxIsalnum(c) || c == wxT('x')) )
6187 {
6188 value << c;
6189 }
6190 }
6191 else
6192 {
6193 if ( c == wxT('"') )
6194 {
6195 state = 0;
6196 labelValid = true;
6197 }
6198 else
6199 label << c;
6200 }
6201 }
6202
6203 if ( labelValid )
6204 {
6205 long l;
6206 if ( !value.ToLong(&l, 0) ) l = wxPG_INVALID_VALUE;
6207 choices.Add(label, l);
6208 }
6209
6210 if ( !choices.IsOk() )
6211 {
6212 choices.EnsureData();
6213 }
6214
6215 // Assign to id
6216 if ( idString.length() )
6217 m_dictIdChoices[idString] = choices.GetData();
6218 }
6219 }
6220
6221 return choices;
6222 }
6223
6224 // -----------------------------------------------------------------------
6225
6226 bool wxPropertyGridPopulator::ToLongPCT( const wxString& s, long* pval, long max )
6227 {
6228 if ( s.Last() == wxT('%') )
6229 {
6230 wxString s2 = s.substr(0,s.length()-1);
6231 long val;
6232 if ( s2.ToLong(&val, 10) )
6233 {
6234 *pval = (val*max)/100;
6235 return true;
6236 }
6237 return false;
6238 }
6239
6240 return s.ToLong(pval, 10);
6241 }
6242
6243 // -----------------------------------------------------------------------
6244
6245 bool wxPropertyGridPopulator::AddAttribute( const wxString& name,
6246 const wxString& type,
6247 const wxString& value )
6248 {
6249 int l = m_propHierarchy.size();
6250 if ( !l )
6251 return false;
6252
6253 wxPGProperty* p = m_propHierarchy[l-1];
6254 wxString valuel = value.Lower();
6255 wxVariant variant;
6256
6257 if ( type.length() == 0 )
6258 {
6259 long v;
6260
6261 // Auto-detect type
6262 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6263 variant = true;
6264 else if ( valuel == wxT("false") || valuel == wxT("no") || valuel == wxT("0") )
6265 variant = false;
6266 else if ( value.ToLong(&v, 0) )
6267 variant = v;
6268 else
6269 variant = value;
6270 }
6271 else
6272 {
6273 if ( type == wxT("string") )
6274 {
6275 variant = value;
6276 }
6277 else if ( type == wxT("int") )
6278 {
6279 long v = 0;
6280 value.ToLong(&v, 0);
6281 variant = v;
6282 }
6283 else if ( type == wxT("bool") )
6284 {
6285 if ( valuel == wxT("true") || valuel == wxT("yes") || valuel == wxT("1") )
6286 variant = true;
6287 else
6288 variant = false;
6289 }
6290 else
6291 {
6292 ProcessError(wxString::Format(wxT("Invalid attribute type '%s'"),type.c_str()));
6293 return false;
6294 }
6295 }
6296
6297 p->SetAttribute( name, variant );
6298
6299 return true;
6300 }
6301
6302 // -----------------------------------------------------------------------
6303
6304 void wxPropertyGridPopulator::ProcessError( const wxString& msg )
6305 {
6306 wxLogError(_("Error in resource: %s"),msg.c_str());
6307 }
6308
6309 // -----------------------------------------------------------------------
6310
6311 #endif // wxUSE_PROPGRID