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