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