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