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