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