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