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