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