When registering editor, try wxRTTI class name in additon to result of wxPGEditor...
[wxWidgets.git] / src / propgrid / editors.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/propgrid/editors.cpp
3 // Purpose: wxPropertyGrid editors
4 // Author: Jaakko Salli
5 // Modified by:
6 // Created: 2007-04-14
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/dcclient.h"
32 #include "wx/dcmemory.h"
33 #include "wx/button.h"
34 #include "wx/pen.h"
35 #include "wx/brush.h"
36 #include "wx/cursor.h"
37 #include "wx/dialog.h"
38 #include "wx/settings.h"
39 #include "wx/msgdlg.h"
40 #include "wx/choice.h"
41 #include "wx/stattext.h"
42 #include "wx/scrolwin.h"
43 #include "wx/dirdlg.h"
44 #include "wx/sizer.h"
45 #include "wx/textdlg.h"
46 #include "wx/filedlg.h"
47 #include "wx/statusbr.h"
48 #include "wx/intl.h"
49 #include "wx/frame.h"
50 #endif
51
52
53 #include "wx/timer.h"
54 #include "wx/dcbuffer.h"
55 #include "wx/bmpbuttn.h"
56
57
58 // This define is necessary to prevent macro clearing
59 #define __wxPG_SOURCE_FILE__
60
61 #include "wx/propgrid/propgrid.h"
62 #include "wx/propgrid/editors.h"
63 #include "wx/propgrid/props.h"
64
65 #if wxPG_USE_RENDERER_NATIVE
66 #include "wx/renderer.h"
67 #endif
68
69 // How many pixels between textctrl and button
70 #ifdef __WXMAC__
71 #define wxPG_TEXTCTRL_AND_BUTTON_SPACING 8
72 #else
73 #define wxPG_TEXTCTRL_AND_BUTTON_SPACING 2
74 #endif
75
76 #define wxPG_BUTTON_SIZEDEC 0
77
78 #include "wx/odcombo.h"
79
80 #ifdef __WXMSW__
81 #include "wx/msw/private.h"
82 #endif
83
84 // -----------------------------------------------------------------------
85
86 #if defined(__WXMSW__)
87 // tested
88 #define wxPG_NAT_BUTTON_BORDER_ANY 1
89 #define wxPG_NAT_BUTTON_BORDER_X 1
90 #define wxPG_NAT_BUTTON_BORDER_Y 1
91
92 #define wxPG_CHECKMARK_XADJ 1
93 #define wxPG_CHECKMARK_YADJ (-1)
94 #define wxPG_CHECKMARK_WADJ 0
95 #define wxPG_CHECKMARK_HADJ 0
96 #define wxPG_CHECKMARK_DEFLATE 0
97
98 #define wxPG_TEXTCTRLYADJUST (m_spacingy+0)
99
100 #elif defined(__WXGTK__)
101 // tested
102 #define wxPG_CHECKMARK_XADJ 0
103 #define wxPG_CHECKMARK_YADJ 0
104 #define wxPG_CHECKMARK_WADJ (-1)
105 #define wxPG_CHECKMARK_HADJ (-1)
106 #define wxPG_CHECKMARK_DEFLATE 3
107
108 #define wxPG_NAT_BUTTON_BORDER_ANY 1
109 #define wxPG_NAT_BUTTON_BORDER_X 1
110 #define wxPG_NAT_BUTTON_BORDER_Y 1
111
112 #define wxPG_TEXTCTRLYADJUST 0
113
114 #elif defined(__WXMAC__)
115 // *not* tested
116 #define wxPG_CHECKMARK_XADJ 0
117 #define wxPG_CHECKMARK_YADJ 0
118 #define wxPG_CHECKMARK_WADJ 0
119 #define wxPG_CHECKMARK_HADJ 0
120 #define wxPG_CHECKMARK_DEFLATE 0
121
122 #define wxPG_NAT_BUTTON_BORDER_ANY 0
123 #define wxPG_NAT_BUTTON_BORDER_X 0
124 #define wxPG_NAT_BUTTON_BORDER_Y 0
125
126 #define wxPG_TEXTCTRLYADJUST 3
127
128 #else
129 // defaults
130 #define wxPG_CHECKMARK_XADJ 0
131 #define wxPG_CHECKMARK_YADJ 0
132 #define wxPG_CHECKMARK_WADJ 0
133 #define wxPG_CHECKMARK_HADJ 0
134 #define wxPG_CHECKMARK_DEFLATE 0
135
136 #define wxPG_NAT_BUTTON_BORDER_ANY 0
137 #define wxPG_NAT_BUTTON_BORDER_X 0
138 #define wxPG_NAT_BUTTON_BORDER_Y 0
139
140 #define wxPG_TEXTCTRLYADJUST 0
141
142 #endif
143
144 // for odcombo
145 #define wxPG_CHOICEXADJUST 0
146 #define wxPG_CHOICEYADJUST 0
147
148 #define ODCB_CUST_PAINT_MARGIN 6 // Number added to image width for SetCustomPaintWidth
149
150 // Milliseconds to wait for two mouse-ups after focus inorder
151 // to trigger a double-click.
152 #define DOUBLE_CLICK_CONVERSION_TRESHOLD 500
153
154 // -----------------------------------------------------------------------
155 // wxPGEditor
156 // -----------------------------------------------------------------------
157
158 IMPLEMENT_ABSTRACT_CLASS(wxPGEditor, wxObject)
159
160
161 wxPGEditor::~wxPGEditor()
162 {
163 }
164
165 wxString wxPGEditor::GetName() const
166 {
167 return GetClassInfo()->GetClassName();
168 }
169
170 void wxPGEditor::DrawValue( wxDC& dc, const wxRect& rect, wxPGProperty* property, const wxString& text ) const
171 {
172 if ( !property->IsValueUnspecified() )
173 dc.DrawText( text, rect.x+wxPG_XBEFORETEXT, rect.y );
174 }
175
176 bool wxPGEditor::GetValueFromControl( wxVariant&, wxPGProperty*, wxWindow* ) const
177 {
178 return false;
179 }
180
181 void wxPGEditor::SetControlStringValue( wxPGProperty* WXUNUSED(property), wxWindow*, const wxString& ) const
182 {
183 }
184
185
186 void wxPGEditor::SetControlIntValue( wxPGProperty* WXUNUSED(property), wxWindow*, int ) const
187 {
188 }
189
190
191 int wxPGEditor::InsertItem( wxWindow*, const wxString&, int ) const
192 {
193 return -1;
194 }
195
196
197 void wxPGEditor::DeleteItem( wxWindow*, int ) const
198 {
199 return;
200 }
201
202
203 void wxPGEditor::OnFocus( wxPGProperty*, wxWindow* ) const
204 {
205 }
206
207
208 bool wxPGEditor::CanContainCustomImage() const
209 {
210 return false;
211 }
212
213 // -----------------------------------------------------------------------
214 // wxPGTextCtrlEditor
215 // -----------------------------------------------------------------------
216
217 WX_PG_IMPLEMENT_EDITOR_CLASS(TextCtrl,wxPGTextCtrlEditor,wxPGEditor)
218
219
220 wxPGWindowList wxPGTextCtrlEditor::CreateControls( wxPropertyGrid* propGrid,
221 wxPGProperty* property,
222 const wxPoint& pos,
223 const wxSize& sz ) const
224 {
225 wxString text;
226
227 //
228 // If has children, and limited editing is specified, then don't create.
229 if ( (property->GetFlags() & wxPG_PROP_NOEDITOR) &&
230 property->GetChildCount() )
231 return (wxWindow*) NULL;
232
233 if ( !property->IsValueUnspecified() )
234 text = property->GetValueString(property->HasFlag(wxPG_PROP_READONLY)?0:wxPG_EDITABLE_VALUE);
235
236 int flags = 0;
237 if ( (property->GetFlags() & wxPG_PROP_PASSWORD) &&
238 property->IsKindOf(CLASSINFO(wxStringProperty)) )
239 flags |= wxTE_PASSWORD;
240
241 wxWindow* wnd = propGrid->GenerateEditorTextCtrl(pos,sz,text,(wxWindow*)NULL,flags,
242 property->GetMaxLength());
243
244 return wnd;
245 }
246
247 #if 0
248 void wxPGTextCtrlEditor::DrawValue( wxDC& dc, wxPGProperty* property, const wxRect& rect ) const
249 {
250 if ( !property->IsValueUnspecified() )
251 {
252 wxString drawStr = property->GetDisplayedString();
253
254 // Code below should no longer be needed, as the obfuscation
255 // is now done in GetValueAsString.
256 /*if ( (property->GetFlags() & wxPG_PROP_PASSWORD) &&
257 property->IsKindOf(WX_PG_CLASSINFO(wxStringProperty)) )
258 {
259 size_t a = drawStr.length();
260 drawStr.Empty();
261 drawStr.Append(wxS('*'),a);
262 }*/
263 dc.DrawText( drawStr, rect.x+wxPG_XBEFORETEXT, rect.y );
264 }
265 }
266 #endif
267
268 void wxPGTextCtrlEditor::UpdateControl( wxPGProperty* property, wxWindow* ctrl ) const
269 {
270 wxTextCtrl* tc = wxDynamicCast(ctrl, wxTextCtrl);
271 if (!tc) return;
272
273 wxString s;
274
275 if ( tc->HasFlag(wxTE_PASSWORD) )
276 s = property->GetValueAsString(wxPG_FULL_VALUE);
277 else
278 s = property->GetDisplayedString();
279
280 tc->SetValue(s);
281 }
282
283
284 // Provided so that, for example, ComboBox editor can use the same code
285 // (multiple inheritance would get way too messy).
286 bool wxPGTextCtrlEditor::OnTextCtrlEvent( wxPropertyGrid* propGrid,
287 wxPGProperty* WXUNUSED(property),
288 wxWindow* ctrl,
289 wxEvent& event )
290 {
291 if ( !ctrl )
292 return false;
293
294 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER )
295 {
296 if ( propGrid->IsEditorsValueModified() )
297 {
298 return true;
299 }
300 }
301 else if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
302 {
303 //
304 // Pass this event outside wxPropertyGrid so that,
305 // if necessary, program can tell when user is editing
306 // a textctrl.
307 // FIXME: Is it safe to change event id in the middle of event
308 // processing (seems to work, but...)?
309 event.Skip();
310 event.SetId(propGrid->GetId());
311
312 propGrid->EditorsValueWasModified();
313 }
314 return false;
315 }
316
317
318 bool wxPGTextCtrlEditor::OnEvent( wxPropertyGrid* propGrid,
319 wxPGProperty* property,
320 wxWindow* ctrl,
321 wxEvent& event ) const
322 {
323 return wxPGTextCtrlEditor::OnTextCtrlEvent(propGrid,property,ctrl,event);
324 }
325
326
327 bool wxPGTextCtrlEditor::GetTextCtrlValueFromControl( wxVariant& variant, wxPGProperty* property, wxWindow* ctrl )
328 {
329 wxTextCtrl* tc = wxStaticCast(ctrl, wxTextCtrl);
330 wxString textVal = tc->GetValue();
331
332 if ( property->UsesAutoUnspecified() && !textVal.length() )
333 {
334 variant.MakeNull();
335 return true;
336 }
337
338 bool res = property->StringToValue(variant, textVal, wxPG_EDITABLE_VALUE);
339
340 // Changing unspecified always causes event (returning
341 // true here should be enough to trigger it).
342 // TODO: Move to propgrid.cpp
343 if ( !res && variant.IsNull() )
344 res = true;
345
346 return res;
347 }
348
349
350 bool wxPGTextCtrlEditor::GetValueFromControl( wxVariant& variant, wxPGProperty* property, wxWindow* ctrl ) const
351 {
352 return wxPGTextCtrlEditor::GetTextCtrlValueFromControl(variant, property, ctrl);
353 }
354
355
356 void wxPGTextCtrlEditor::SetValueToUnspecified( wxPGProperty* property, wxWindow* ctrl ) const
357 {
358 wxTextCtrl* tc = wxStaticCast(ctrl, wxTextCtrl);
359
360 wxPropertyGrid* pg = property->GetGrid();
361 wxASSERT(pg); // Really, property grid should exist if editor does
362 if ( pg )
363 tc->SetValue(wxEmptyString);
364 }
365
366
367 void wxPGTextCtrlEditor::SetControlStringValue( wxPGProperty* property, wxWindow* ctrl, const wxString& txt ) const
368 {
369 wxTextCtrl* tc = wxStaticCast(ctrl, wxTextCtrl);
370
371 wxPropertyGrid* pg = property->GetGrid();
372 wxASSERT(pg); // Really, property grid should exist if editor does
373 if ( pg )
374 tc->SetValue(txt);
375 }
376
377
378 void wxPGTextCtrlEditor::OnFocus( wxPGProperty*, wxWindow* wnd ) const
379 {
380 wxTextCtrl* tc = wxStaticCast(wnd, wxTextCtrl);
381
382 tc->SetSelection(-1,-1);
383 }
384
385
386 wxPGTextCtrlEditor::~wxPGTextCtrlEditor() { }
387
388
389 // -----------------------------------------------------------------------
390 // wxPGChoiceEditor
391 // -----------------------------------------------------------------------
392
393
394 WX_PG_IMPLEMENT_EDITOR_CLASS(Choice,wxPGChoiceEditor,wxPGEditor)
395
396
397 // This is a special enhanced double-click processor class.
398 // In essence, it allows for double-clicks for which the
399 // first click "created" the control.
400 class wxPGDoubleClickProcessor : public wxEvtHandler
401 {
402 public:
403
404 wxPGDoubleClickProcessor( wxOwnerDrawnComboBox* combo, wxPGProperty* property )
405 : wxEvtHandler()
406 {
407 m_timeLastMouseUp = 0;
408 m_combo = combo;
409 m_property = property;
410 m_downReceived = false;
411 }
412
413 protected:
414
415 void OnMouseEvent( wxMouseEvent& event )
416 {
417 wxLongLong t = ::wxGetLocalTimeMillis();
418 int evtType = event.GetEventType();
419
420 if ( m_property->HasFlag(wxPG_PROP_USE_DCC) &&
421 m_property->IsKindOf(CLASSINFO(wxBoolProperty)) &&
422 !m_combo->IsPopupShown() )
423 {
424 // Just check that it is in the text area
425 wxPoint pt = event.GetPosition();
426 if ( m_combo->GetTextRect().Contains(pt) )
427 {
428 if ( evtType == wxEVT_LEFT_DOWN )
429 {
430 // Set value to avoid up-events without corresponding downs
431 m_downReceived = true;
432 }
433 else if ( evtType == wxEVT_LEFT_DCLICK )
434 {
435 // We'll make our own double-clicks
436 event.SetEventType(0);
437 return;
438 }
439 else if ( evtType == wxEVT_LEFT_UP )
440 {
441 if ( m_downReceived || m_timeLastMouseUp == 1 )
442 {
443 wxLongLong timeFromLastUp = (t-m_timeLastMouseUp);
444
445 if ( timeFromLastUp < DOUBLE_CLICK_CONVERSION_TRESHOLD )
446 {
447 event.SetEventType(wxEVT_LEFT_DCLICK);
448 m_timeLastMouseUp = 1;
449 }
450 else
451 {
452 m_timeLastMouseUp = t;
453 }
454 }
455 }
456 }
457 }
458
459 event.Skip();
460 }
461
462 void OnSetFocus( wxFocusEvent& event )
463 {
464 m_timeLastMouseUp = ::wxGetLocalTimeMillis();
465 event.Skip();
466 }
467
468 private:
469 wxLongLong m_timeLastMouseUp;
470 wxOwnerDrawnComboBox* m_combo;
471 wxPGProperty* m_property; // Selected property
472 bool m_downReceived;
473
474 DECLARE_EVENT_TABLE()
475 };
476
477 BEGIN_EVENT_TABLE(wxPGDoubleClickProcessor, wxEvtHandler)
478 EVT_MOUSE_EVENTS(wxPGDoubleClickProcessor::OnMouseEvent)
479 EVT_SET_FOCUS(wxPGDoubleClickProcessor::OnSetFocus)
480 END_EVENT_TABLE()
481
482
483
484 class wxPGComboBox : public wxOwnerDrawnComboBox
485 {
486 public:
487
488 wxPGComboBox()
489 : wxOwnerDrawnComboBox()
490 {
491 m_dclickProcessor = (wxPGDoubleClickProcessor*) NULL;
492 m_sizeEventCalled = false;
493 }
494
495 ~wxPGComboBox()
496 {
497 if ( m_dclickProcessor )
498 {
499 RemoveEventHandler(m_dclickProcessor);
500 delete m_dclickProcessor;
501 }
502 }
503
504 bool Create(wxWindow *parent,
505 wxWindowID id,
506 const wxString& value,
507 const wxPoint& pos,
508 const wxSize& size,
509 const wxArrayString& choices,
510 long style = 0,
511 const wxValidator& validator = wxDefaultValidator,
512 const wxString& name = wxS("wxOwnerDrawnComboBox"))
513 {
514 if ( !wxOwnerDrawnComboBox::Create( parent,
515 id,
516 value,
517 pos,
518 size,
519 choices,
520 style,
521 validator,
522 name ) )
523 return false;
524
525 m_dclickProcessor = new wxPGDoubleClickProcessor(this, GetGrid()->GetSelection() );
526
527 PushEventHandler(m_dclickProcessor);
528
529 return true;
530 }
531
532 virtual void OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const
533 {
534 wxPropertyGrid* pg = GetGrid();
535 pg->OnComboItemPaint((wxPGCustomComboControl*)this,item,dc,(wxRect&)rect,flags);
536 }
537 virtual wxCoord OnMeasureItem( size_t item ) const
538 {
539 wxPropertyGrid* pg = GetGrid();
540 wxRect rect;
541 rect.x = -1;
542 rect.width = 0;
543 pg->OnComboItemPaint((wxPGCustomComboControl*)this,item,*((wxDC*)NULL),rect,0);
544 return rect.height;
545 }
546
547 wxPropertyGrid* GetGrid() const
548 {
549 wxPropertyGrid* pg = wxDynamicCast(GetParent()->GetParent(),wxPropertyGrid);
550 wxASSERT(pg);
551 return pg;
552 }
553
554 virtual wxCoord OnMeasureItemWidth( size_t item ) const
555 {
556 wxPropertyGrid* pg = GetGrid();
557 wxRect rect;
558 rect.x = -1;
559 rect.width = -1;
560 pg->OnComboItemPaint((wxPGCustomComboControl*)this,item,*((wxDC*)NULL),rect,0);
561 return rect.width;
562 }
563
564 virtual void PositionTextCtrl( int WXUNUSED(textCtrlXAdjust), int WXUNUSED(textCtrlYAdjust) )
565 {
566 wxPropertyGrid* pg = GetGrid();
567 wxOwnerDrawnComboBox::PositionTextCtrl(
568 wxPG_TEXTCTRLXADJUST - (wxPG_XBEFOREWIDGET+wxPG_CONTROL_MARGIN+1) - 1,
569 pg->GetSpacingY() + 2
570 );
571 }
572
573 private:
574 wxPGDoubleClickProcessor* m_dclickProcessor;
575 bool m_sizeEventCalled;
576 };
577
578
579 void wxPropertyGrid::OnComboItemPaint( wxPGCustomComboControl* pCc,
580 int item,
581 wxDC& dc,
582 wxRect& rect,
583 int flags )
584 {
585 wxPGComboBox* pCb = (wxPGComboBox*)pCc;
586
587 // Sanity check
588 wxASSERT( IsKindOf(CLASSINFO(wxPropertyGrid)) );
589
590 wxPGProperty* p = m_selected;
591 wxString text;
592
593 const wxPGChoices& choices = p->GetChoices();
594 const wxPGCommonValue* comVal = NULL;
595 int comVals = p->GetDisplayedCommonValueCount();
596 int comValIndex = -1;
597
598 int choiceCount = 0;
599 if ( choices.IsOk() )
600 choiceCount = choices.GetCount();
601
602 if ( item >= choiceCount && comVals > 0 )
603 {
604 comValIndex = item - choiceCount;
605 comVal = GetCommonValue(comValIndex);
606 if ( !p->IsValueUnspecified() )
607 text = comVal->GetLabel();
608 }
609 else
610 {
611 if ( !(flags & wxODCB_PAINTING_CONTROL) )
612 {
613 text = pCb->GetString(item);
614 }
615 else
616 {
617 if ( !p->IsValueUnspecified() )
618 text = p->GetValueString(0);
619 }
620 }
621
622 if ( item < 0 )
623 return;
624
625 wxSize cis;
626
627 const wxBitmap* itemBitmap = NULL;
628
629 if ( item >= 0 && choices.IsOk() && choices.Item(item).GetBitmap().Ok() && comValIndex == -1 )
630 itemBitmap = &choices.Item(item).GetBitmap();
631
632 //
633 // Decide what custom image size to use
634 if ( itemBitmap )
635 {
636 cis.x = itemBitmap->GetWidth();
637 cis.y = itemBitmap->GetHeight();
638 }
639 else
640 {
641 cis = GetImageSize(p, item);
642 }
643
644 if ( rect.x < 0 )
645 {
646 // Default measure behaviour (no flexible, custom paint image only)
647 if ( rect.width < 0 )
648 {
649 wxCoord x, y;
650 GetTextExtent(text, &x, &y, 0, 0, &m_font);
651 rect.width = cis.x + wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2 + 9 + x;
652 }
653
654 rect.height = cis.y + 2;
655 return;
656 }
657
658 wxPGPaintData paintdata;
659 paintdata.m_parent = NULL;
660 paintdata.m_choiceItem = item;
661
662 // This is by the current (1.0.0b) spec - if painting control, item is -1
663 if ( (flags & wxODCB_PAINTING_CONTROL) )
664 paintdata.m_choiceItem = -1;
665
666 if ( &dc )
667 dc.SetBrush(*wxWHITE_BRUSH);
668
669 if ( rect.x >= 0 )
670 {
671 //
672 // DrawItem call
673
674 wxPoint pt(rect.x + wxPG_CONTROL_MARGIN - wxPG_CHOICEXADJUST - 1,
675 rect.y + 1);
676
677 int renderFlags = 0;
678
679 if ( flags & wxODCB_PAINTING_CONTROL )
680 renderFlags |= wxPGCellRenderer::Control;
681
682 if ( flags & wxODCB_PAINTING_SELECTED )
683 renderFlags |= wxPGCellRenderer::Selected;
684
685 if ( cis.x > 0 && (p->HasFlag(wxPG_PROP_CUSTOMIMAGE) || !(flags & wxODCB_PAINTING_CONTROL)) &&
686 ( !p->m_valueBitmap || item == pCb->GetSelection() ) &&
687 ( item >= 0 || (flags & wxODCB_PAINTING_CONTROL) ) &&
688 !itemBitmap
689 )
690 {
691 pt.x += wxCC_CUSTOM_IMAGE_MARGIN1;
692 wxRect r(pt.x,pt.y,cis.x,cis.y);
693
694 if ( flags & wxODCB_PAINTING_CONTROL )
695 {
696 //r.width = cis.x;
697 r.height = wxPG_STD_CUST_IMAGE_HEIGHT(m_lineHeight);
698 }
699
700 paintdata.m_drawnWidth = r.width;
701
702 dc.SetPen(m_colPropFore);
703 if ( comValIndex >= 0 )
704 {
705 const wxPGCommonValue* cv = GetCommonValue(comValIndex);
706 wxPGCellRenderer* renderer = cv->GetRenderer();
707 r.width = rect.width;
708 renderer->Render( dc, r, this, p, m_selColumn, comValIndex, renderFlags );
709 return;
710 }
711 else if ( item >= 0 )
712 {
713 p->OnCustomPaint( dc, r, paintdata );
714 }
715 else
716 {
717 dc.DrawRectangle( r );
718 }
719
720 pt.x += paintdata.m_drawnWidth + wxCC_CUSTOM_IMAGE_MARGIN2 - 1;
721 }
722 else
723 {
724 // TODO: This aligns text so that it seems to be horizontally
725 // on the same line as property values. Not really
726 // sure if its needed, but seems to not cause any harm.
727 pt.x -= 1;
728
729 if ( item < 0 && (flags & wxODCB_PAINTING_CONTROL) )
730 item = pCb->GetSelection();
731
732 if ( choices.IsOk() && item >= 0 && comValIndex < 0 )
733 {
734 const wxPGChoiceEntry& cell = choices.Item(item);
735 wxPGCellRenderer* renderer = wxPGGlobalVars->m_defaultRenderer;
736 int imageOffset = renderer->PreDrawCell( dc, rect, cell, renderFlags );
737 if ( imageOffset )
738 imageOffset += wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2;
739 pt.x += imageOffset;
740 }
741 }
742
743 //
744 // Draw text
745 //
746
747 pt.y += (rect.height-m_fontHeight)/2 - 1;
748
749 pt.x += 1;
750
751 dc.DrawText( text, pt.x + wxPG_XBEFORETEXT, pt.y );
752 }
753 else
754 {
755 //
756 // MeasureItem call
757
758 p->OnCustomPaint( dc, rect, paintdata );
759 rect.height = paintdata.m_drawnHeight + 2;
760 rect.width = cis.x + wxCC_CUSTOM_IMAGE_MARGIN1 + wxCC_CUSTOM_IMAGE_MARGIN2 + 9;
761 }
762 }
763
764 bool wxPGChoiceEditor_SetCustomPaintWidth( wxPropertyGrid* propGrid, wxPGComboBox* cb, int cmnVal )
765 {
766 wxPGProperty* property = propGrid->GetSelectedProperty();
767 wxASSERT( property );
768
769 if ( cmnVal >= 0 )
770 {
771 // Yes, a common value is being selected
772 property->SetCommonValue( cmnVal );
773 wxSize imageSize = propGrid->GetCommonValue(cmnVal)->
774 GetRenderer()->GetImageSize(property, 1, cmnVal);
775 if ( imageSize.x ) imageSize.x += ODCB_CUST_PAINT_MARGIN;
776 cb->SetCustomPaintWidth( imageSize.x );
777 return false;
778 }
779 else
780 {
781 wxSize imageSize = propGrid->GetImageSize(property, -1);
782 if ( imageSize.x ) imageSize.x += ODCB_CUST_PAINT_MARGIN;
783 cb->SetCustomPaintWidth( imageSize.x );
784 return true;
785 }
786 }
787
788 // CreateControls calls this with CB_READONLY in extraStyle
789 wxWindow* wxPGChoiceEditor::CreateControlsBase( wxPropertyGrid* propGrid,
790 wxPGProperty* property,
791 const wxPoint& pos,
792 const wxSize& sz,
793 long extraStyle ) const
794 {
795 const wxPGChoices& choices = property->GetChoices();
796 wxString defString;
797 int index = property->GetChoiceSelection();
798
799 bool isUnspecified = property->IsValueUnspecified();
800
801 if ( !isUnspecified )
802 defString = property->GetDisplayedString();
803
804 wxArrayString labels = choices.GetLabels();
805
806 wxPGComboBox* cb;
807
808 wxPoint po(pos);
809 wxSize si(sz);
810 po.y += wxPG_CHOICEYADJUST;
811 si.y -= (wxPG_CHOICEYADJUST*2);
812
813 po.x += wxPG_CHOICEXADJUST;
814 si.x -= wxPG_CHOICEXADJUST;
815 wxWindow* ctrlParent = propGrid->GetPanel();
816
817 int odcbFlags = extraStyle | wxBORDER_NONE | wxTE_PROCESS_ENTER;
818
819 //
820 // If common value specified, use appropriate index
821 unsigned int cmnVals = property->GetDisplayedCommonValueCount();
822 if ( cmnVals )
823 {
824 if ( !isUnspecified )
825 {
826 int cmnVal = property->GetCommonValue();
827 if ( cmnVal >= 0 )
828 {
829 index = labels.size() + cmnVal;
830 }
831 }
832
833 unsigned int i;
834 for ( i=0; i<cmnVals; i++ )
835 labels.Add(propGrid->GetCommonValueLabel(i));
836 }
837
838 cb = new wxPGComboBox();
839 #ifdef __WXMSW__
840 cb->Hide();
841 #endif
842 cb->Create(ctrlParent,
843 wxPG_SUBID1,
844 wxString(),
845 po,
846 si,
847 labels,
848 odcbFlags);
849
850 cb->SetButtonPosition(si.y,0,wxRIGHT);
851 cb->SetTextIndent(wxPG_XBEFORETEXT-1);
852
853 wxPGChoiceEditor_SetCustomPaintWidth( propGrid, cb, property->GetCommonValue() );
854
855 if ( index >= 0 && index < (int)cb->GetCount() )
856 {
857 cb->SetSelection( index );
858 if ( defString.length() )
859 cb->SetText( defString );
860 }
861 else if ( !(extraStyle & wxCB_READONLY) && defString.length() )
862 cb->SetValue( defString );
863 else
864 cb->SetSelection( -1 );
865
866 // Connect event handling
867 wxWindowID id = cb->GetId();
868 propGrid->Connect(id, wxEVT_COMMAND_COMBOBOX_SELECTED,
869 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent));
870 propGrid->Connect(id, wxEVT_COMMAND_TEXT_UPDATED,
871 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent));
872 propGrid->Connect(id, wxEVT_COMMAND_TEXT_ENTER,
873 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent));
874
875 #ifdef __WXMSW__
876 cb->Show();
877 #endif
878
879 return (wxWindow*) cb;
880 }
881
882
883 void wxPGChoiceEditor::UpdateControl( wxPGProperty* property, wxWindow* ctrl ) const
884 {
885 wxASSERT( ctrl );
886 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
887 wxASSERT( cb->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)));
888 int ind = property->GetChoiceSelection();
889 cb->SetSelection(ind);
890 }
891
892 wxPGWindowList wxPGChoiceEditor::CreateControls( wxPropertyGrid* propGrid, wxPGProperty* property,
893 const wxPoint& pos, const wxSize& sz ) const
894 {
895 return CreateControlsBase(propGrid,property,pos,sz,wxCB_READONLY);
896 }
897
898
899 int wxPGChoiceEditor::InsertItem( wxWindow* ctrl, const wxString& label, int index ) const
900 {
901 wxASSERT( ctrl );
902 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
903 wxASSERT( cb->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)));
904
905 if (index < 0)
906 index = cb->GetCount();
907
908 return cb->Insert(label,index);
909 }
910
911
912 void wxPGChoiceEditor::DeleteItem( wxWindow* ctrl, int index ) const
913 {
914 wxASSERT( ctrl );
915 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
916 wxASSERT( cb->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)));
917
918 cb->Delete(index);
919 }
920
921 bool wxPGChoiceEditor::OnEvent( wxPropertyGrid* propGrid, wxPGProperty* property,
922 wxWindow* ctrl, wxEvent& event ) const
923 {
924 if ( event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED )
925 {
926 wxPGComboBox* cb = (wxPGComboBox*)ctrl;
927 int index = cb->GetSelection();
928 int cmnValIndex = -1;
929 int cmnVals = property->GetDisplayedCommonValueCount();
930 int items = cb->GetCount();
931
932 if ( index >= (items-cmnVals) )
933 {
934 // Yes, a common value is being selected
935 cmnValIndex = index - (items-cmnVals);
936 property->SetCommonValue( cmnValIndex );
937
938 // Truly set value to unspecified?
939 if ( propGrid->GetUnspecifiedCommonValue() == cmnValIndex )
940 {
941 if ( !property->IsValueUnspecified() )
942 propGrid->SetInternalFlag(wxPG_FL_VALUE_CHANGE_IN_EVENT);
943 property->SetValueToUnspecified();
944 if ( !cb->HasFlag(wxCB_READONLY) )
945 cb->GetTextCtrl()->SetValue(wxEmptyString);
946 return false;
947 }
948 }
949 return wxPGChoiceEditor_SetCustomPaintWidth( propGrid, cb, cmnValIndex );
950 }
951 return false;
952 }
953
954
955 bool wxPGChoiceEditor::GetValueFromControl( wxVariant& variant, wxPGProperty* property, wxWindow* ctrl ) const
956 {
957 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
958
959 int index = cb->GetSelection();
960
961 if ( index != property->GetChoiceSelection() ||
962 // Changing unspecified always causes event (returning
963 // true here should be enough to trigger it).
964 property->IsValueUnspecified()
965 )
966 {
967 return property->IntToValue( variant, index, 0 );
968 }
969 return false;
970 }
971
972
973 void wxPGChoiceEditor::SetControlStringValue( wxPGProperty* WXUNUSED(property), wxWindow* ctrl, const wxString& txt ) const
974 {
975 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
976 wxASSERT( cb );
977 cb->SetValue(txt);
978 }
979
980
981 void wxPGChoiceEditor::SetControlIntValue( wxPGProperty* WXUNUSED(property), wxWindow* ctrl, int value ) const
982 {
983 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
984 wxASSERT( cb );
985 cb->SetSelection(value);
986 }
987
988
989 void wxPGChoiceEditor::SetValueToUnspecified( wxPGProperty* WXUNUSED(property), wxWindow* ctrl ) const
990 {
991 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
992 cb->SetSelection(-1);
993 }
994
995
996 bool wxPGChoiceEditor::CanContainCustomImage() const
997 {
998 return true;
999 }
1000
1001
1002 wxPGChoiceEditor::~wxPGChoiceEditor() { }
1003
1004
1005 // -----------------------------------------------------------------------
1006 // wxPGComboBoxEditor
1007 // -----------------------------------------------------------------------
1008
1009
1010 WX_PG_IMPLEMENT_EDITOR_CLASS(ComboBox,wxPGComboBoxEditor,wxPGChoiceEditor)
1011
1012
1013 void wxPGComboBoxEditor::UpdateControl( wxPGProperty* property, wxWindow* ctrl ) const
1014 {
1015 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
1016 cb->SetValue(property->GetValueString(wxPG_EDITABLE_VALUE));
1017
1018 // TODO: If string matches any selection, then select that.
1019 }
1020
1021
1022 wxPGWindowList wxPGComboBoxEditor::CreateControls( wxPropertyGrid* propGrid,
1023 wxPGProperty* property,
1024 const wxPoint& pos,
1025 const wxSize& sz ) const
1026 {
1027 return CreateControlsBase(propGrid,property,pos,sz,0);
1028 }
1029
1030
1031 bool wxPGComboBoxEditor::OnEvent( wxPropertyGrid* propGrid,
1032 wxPGProperty* property,
1033 wxWindow* ctrl,
1034 wxEvent& event ) const
1035 {
1036 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*) NULL;
1037 wxWindow* textCtrl = (wxWindow*) NULL;
1038
1039 if ( ctrl )
1040 {
1041 cb = (wxOwnerDrawnComboBox*)ctrl;
1042 textCtrl = cb->GetTextCtrl();
1043 }
1044
1045 if ( wxPGTextCtrlEditor::OnTextCtrlEvent(propGrid,property,textCtrl,event) )
1046 return true;
1047
1048 return wxPGChoiceEditor::OnEvent(propGrid,property,ctrl,event);
1049 }
1050
1051
1052 bool wxPGComboBoxEditor::GetValueFromControl( wxVariant& variant, wxPGProperty* property, wxWindow* ctrl ) const
1053 {
1054 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
1055 wxString textVal = cb->GetValue();
1056
1057 if ( property->UsesAutoUnspecified() && !textVal.length() )
1058 {
1059 variant.MakeNull();
1060 return true;
1061 }
1062
1063 bool res = property->StringToValue(variant, textVal, wxPG_EDITABLE_VALUE);
1064
1065 // Changing unspecified always causes event (returning
1066 // true here should be enough to trigger it).
1067 if ( !res && variant.IsNull() )
1068 res = true;
1069
1070 return res;
1071 }
1072
1073
1074 void wxPGComboBoxEditor::OnFocus( wxPGProperty*, wxWindow* ctrl ) const
1075 {
1076 wxOwnerDrawnComboBox* cb = (wxOwnerDrawnComboBox*)ctrl;
1077 cb->GetTextCtrl()->SetSelection(-1,-1);
1078 }
1079
1080
1081 wxPGComboBoxEditor::~wxPGComboBoxEditor() { }
1082
1083
1084 // -----------------------------------------------------------------------
1085 // wxPGChoiceAndButtonEditor
1086 // -----------------------------------------------------------------------
1087
1088
1089 // This simpler implement_editor macro doesn't define class body.
1090 WX_PG_IMPLEMENT_EDITOR_CLASS(ChoiceAndButton,wxPGChoiceAndButtonEditor,wxPGChoiceEditor)
1091
1092
1093 wxPGWindowList wxPGChoiceAndButtonEditor::CreateControls( wxPropertyGrid* propGrid,
1094 wxPGProperty* property,
1095 const wxPoint& pos,
1096 const wxSize& sz ) const
1097 {
1098 // Use one two units smaller to match size of the combo's dropbutton.
1099 // (normally a bigger button is used because it looks better)
1100 int bt_wid = sz.y;
1101 bt_wid -= 2;
1102 wxSize bt_sz(bt_wid,bt_wid);
1103
1104 // Position of button.
1105 wxPoint bt_pos(pos.x+sz.x-bt_sz.x,pos.y);
1106 #ifdef __WXMAC__
1107 bt_pos.y -= 1;
1108 #else
1109 bt_pos.y += 1;
1110 #endif
1111
1112 wxWindow* bt = propGrid->GenerateEditorButton( bt_pos, bt_sz );
1113
1114 // Size of choice.
1115 wxSize ch_sz(sz.x-bt->GetSize().x,sz.y);
1116
1117 #ifdef __WXMAC__
1118 ch_sz.x -= wxPG_TEXTCTRL_AND_BUTTON_SPACING;
1119 #endif
1120
1121 wxWindow* ch = wxPGEditor_Choice->CreateControls(propGrid,property,
1122 pos,ch_sz).m_primary;
1123
1124 #ifdef __WXMSW__
1125 bt->Show();
1126 #endif
1127
1128 return wxPGWindowList(ch, bt);
1129 }
1130
1131
1132 wxPGChoiceAndButtonEditor::~wxPGChoiceAndButtonEditor() { }
1133
1134
1135 // -----------------------------------------------------------------------
1136 // wxPGTextCtrlAndButtonEditor
1137 // -----------------------------------------------------------------------
1138
1139
1140 // This simpler implement_editor macro doesn't define class body.
1141 WX_PG_IMPLEMENT_EDITOR_CLASS(TextCtrlAndButton,wxPGTextCtrlAndButtonEditor,wxPGTextCtrlEditor)
1142
1143
1144 wxPGWindowList wxPGTextCtrlAndButtonEditor::CreateControls( wxPropertyGrid* propGrid,
1145 wxPGProperty* property,
1146 const wxPoint& pos,
1147 const wxSize& sz ) const
1148 {
1149 wxWindow* wnd2;
1150 wxWindow* wnd = propGrid->GenerateEditorTextCtrlAndButton( pos, sz, &wnd2,
1151 property->GetFlags() & wxPG_PROP_NOEDITOR, property);
1152
1153 return wxPGWindowList(wnd, wnd2);
1154 }
1155
1156
1157 wxPGTextCtrlAndButtonEditor::~wxPGTextCtrlAndButtonEditor() { }
1158
1159
1160 // -----------------------------------------------------------------------
1161 // wxPGCheckBoxEditor
1162 // -----------------------------------------------------------------------
1163
1164 #if wxPG_INCLUDE_CHECKBOX
1165
1166 WX_PG_IMPLEMENT_EDITOR_CLASS(CheckBox,wxPGCheckBoxEditor,wxPGEditor)
1167
1168
1169 // state argument: 0x01 = set if checked
1170 // 0x02 = set if rectangle should be bold
1171 static void DrawSimpleCheckBox( wxDC& dc, const wxRect& rect, int box_hei, int state, const wxColour& linecol )
1172 {
1173
1174 // Box rectangle.
1175 wxRect r(rect.x+wxPG_XBEFORETEXT,rect.y+((rect.height-box_hei)/2),box_hei,box_hei);
1176
1177 // Draw check mark first because it is likely to overdraw the
1178 // surrounding rectangle.
1179 if ( state & 1 )
1180 {
1181 wxRect r2(r.x+wxPG_CHECKMARK_XADJ,
1182 r.y+wxPG_CHECKMARK_YADJ,
1183 r.width+wxPG_CHECKMARK_WADJ,
1184 r.height+wxPG_CHECKMARK_HADJ);
1185 #if wxPG_CHECKMARK_DEFLATE
1186 r2.Deflate(wxPG_CHECKMARK_DEFLATE);
1187 #endif
1188 dc.DrawCheckMark(r2);
1189
1190 // This would draw a simple cross check mark.
1191 // dc.DrawLine(r.x,r.y,r.x+r.width-1,r.y+r.height-1);
1192 // dc.DrawLine(r.x,r.y+r.height-1,r.x+r.width-1,r.y);
1193
1194 }
1195
1196 if ( !(state & 2) )
1197 {
1198 // Pen for thin rectangle.
1199 dc.SetPen(linecol);
1200 }
1201 else
1202 {
1203 // Pen for bold rectangle.
1204 wxPen linepen(linecol,2,wxSOLID);
1205 linepen.SetJoin(wxJOIN_MITER); // This prevents round edges.
1206 dc.SetPen(linepen);
1207 r.x++;
1208 r.y++;
1209 r.width--;
1210 r.height--;
1211 }
1212
1213 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1214
1215 dc.DrawRectangle(r);
1216 dc.SetPen(*wxTRANSPARENT_PEN);
1217 }
1218
1219 //
1220 // Real simple custom-drawn checkbox-without-label class.
1221 //
1222 class wxSimpleCheckBox : public wxControl
1223 {
1224 public:
1225
1226 void SetValue( int value );
1227
1228 wxSimpleCheckBox( wxWindow* parent,
1229 wxWindowID id,
1230 const wxPoint& pos = wxDefaultPosition,
1231 const wxSize& size = wxDefaultSize )
1232 : wxControl(parent,id,pos,size,wxBORDER_NONE|wxWANTS_CHARS)
1233 {
1234 // Due to SetOwnFont stuff necessary for GTK+ 1.2, we need to have this
1235 SetFont( parent->GetFont() );
1236
1237 m_state = 0;
1238 wxPropertyGrid* pg = (wxPropertyGrid*) parent->GetParent();
1239 wxASSERT( pg->IsKindOf(CLASSINFO(wxPropertyGrid)) );
1240 m_boxHeight = pg->GetFontHeight();
1241 SetBackgroundStyle( wxBG_STYLE_COLOUR );
1242 }
1243
1244 virtual ~wxSimpleCheckBox();
1245
1246 virtual bool ProcessEvent(wxEvent& event);
1247
1248 int m_state;
1249 int m_boxHeight;
1250
1251 static wxBitmap* ms_doubleBuffer;
1252
1253 };
1254
1255 wxSimpleCheckBox::~wxSimpleCheckBox()
1256 {
1257 delete ms_doubleBuffer;
1258 ms_doubleBuffer = NULL;
1259 }
1260
1261
1262 wxBitmap* wxSimpleCheckBox::ms_doubleBuffer = (wxBitmap*) NULL;
1263
1264 // value = 2 means toggle (sorry, too lazy to do constants)
1265 void wxSimpleCheckBox::SetValue( int value )
1266 {
1267 if ( value > 1 )
1268 {
1269 m_state++;
1270 if ( m_state > 1 ) m_state = 0;
1271 }
1272 else
1273 {
1274 m_state = value;
1275 }
1276 Refresh();
1277
1278 wxCommandEvent evt(wxEVT_COMMAND_CHECKBOX_CLICKED,GetParent()->GetId());
1279
1280 wxPropertyGrid* propGrid = (wxPropertyGrid*) GetParent()->GetParent();
1281 wxASSERT( propGrid->IsKindOf(CLASSINFO(wxPropertyGrid)) );
1282 propGrid->OnCustomEditorEvent(evt);
1283 }
1284
1285
1286 bool wxSimpleCheckBox::ProcessEvent(wxEvent& event)
1287 {
1288 wxPropertyGrid* propGrid = (wxPropertyGrid*) GetParent()->GetParent();
1289 wxASSERT( propGrid->IsKindOf(CLASSINFO(wxPropertyGrid)) );
1290
1291 if ( ( (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK)
1292 && ((wxMouseEvent&)event).m_x > (wxPG_XBEFORETEXT-2)
1293 && ((wxMouseEvent&)event).m_x <= (wxPG_XBEFORETEXT-2+m_boxHeight) )
1294 )
1295 {
1296 SetValue(2);
1297 return true;
1298 }
1299 else if ( event.GetEventType() == wxEVT_PAINT )
1300 {
1301 wxSize clientSize = GetClientSize();
1302 wxPaintDC dc(this);
1303
1304 /*
1305 // Buffered paint DC doesn't seem to do much good
1306 if ( !ms_doubleBuffer ||
1307 clientSize.x > ms_doubleBuffer->GetWidth() ||
1308 clientSize.y > ms_doubleBuffer->GetHeight() )
1309 {
1310 delete ms_doubleBuffer;
1311 ms_doubleBuffer = new wxBitmap(clientSize.x+25,clientSize.y+25);
1312 }
1313
1314 wxBufferedPaintDC dc(this,*ms_doubleBuffer);
1315 */
1316
1317 wxRect rect(0,0,clientSize.x,clientSize.y);
1318 //rect.x -= 1;
1319 rect.y += 1;
1320 rect.width += 1;
1321
1322 m_boxHeight = propGrid->GetFontHeight();
1323
1324 wxColour bgcol = GetBackgroundColour();
1325 dc.SetBrush( bgcol );
1326 dc.SetPen( bgcol );
1327 dc.DrawRectangle( rect );
1328
1329 wxColour txcol = GetForegroundColour();
1330
1331 int state = m_state;
1332 if ( m_font.GetWeight() == wxBOLD )
1333 state |= 2;
1334
1335 DrawSimpleCheckBox(dc,rect,m_boxHeight,state,txcol);
1336
1337 // If focused, indicate it somehow.
1338 /*
1339 if ( wxWindow::FindFocus() == this )
1340 {
1341 rect.x += 1;
1342 rect.width -= 1;
1343
1344 wxPGDrawFocusRect(dc,rect);
1345 }
1346 */
1347
1348 return true;
1349 }
1350 else if ( event.GetEventType() == wxEVT_SIZE ||
1351 event.GetEventType() == wxEVT_SET_FOCUS ||
1352 event.GetEventType() == wxEVT_KILL_FOCUS
1353 )
1354 {
1355 Refresh();
1356 }
1357 else if ( event.GetEventType() == wxEVT_KEY_DOWN )
1358 {
1359 wxKeyEvent& keyEv = (wxKeyEvent&) event;
1360
1361 if ( keyEv.GetKeyCode() == WXK_SPACE )
1362 {
1363 SetValue(2);
1364 return true;
1365 }
1366 }
1367 return wxControl::ProcessEvent(event);
1368 }
1369
1370
1371 wxPGWindowList wxPGCheckBoxEditor::CreateControls( wxPropertyGrid* propGrid,
1372 wxPGProperty* property,
1373 const wxPoint& pos,
1374 const wxSize& size ) const
1375 {
1376 wxPoint pt = pos;
1377 pt.x -= wxPG_XBEFOREWIDGET;
1378 wxSize sz = size;
1379 sz.x = propGrid->GetFontHeight() + (wxPG_XBEFOREWIDGET*2) + 4;
1380
1381 wxSimpleCheckBox* cb = new wxSimpleCheckBox(propGrid->GetPanel(),wxPG_SUBID1,pt,sz);
1382
1383 cb->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
1384
1385 cb->Connect( wxPG_SUBID1, wxEVT_LEFT_DOWN,
1386 (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction)
1387 &wxPropertyGrid::OnCustomEditorEvent, NULL, propGrid );
1388
1389 cb->Connect( wxPG_SUBID1, wxEVT_LEFT_DCLICK,
1390 (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction)
1391 &wxPropertyGrid::OnCustomEditorEvent, NULL, propGrid );
1392
1393 if ( property->GetChoiceSelection() > 0 &&
1394 !property->IsValueUnspecified() )
1395 cb->m_state = 1;
1396
1397 // If mouse cursor was on the item, toggle the value now.
1398 if ( propGrid->GetInternalFlags() & wxPG_FL_ACTIVATION_BY_CLICK )
1399 {
1400 wxPoint pt = cb->ScreenToClient(::wxGetMousePosition());
1401 if ( pt.x <= (wxPG_XBEFORETEXT-2+cb->m_boxHeight) )
1402 {
1403 cb->m_state++;
1404
1405 if ( cb->m_state > 1 )
1406 cb->m_state = 0;
1407
1408 // Makes sure wxPG_EVT_CHANGING etc. is sent for this initial click
1409 propGrid->ChangePropertyValue(property, wxPGVariant_Bool(cb->m_state));
1410 }
1411 }
1412
1413 propGrid->SetInternalFlag( wxPG_FL_FIXED_WIDTH_EDITOR );
1414
1415 return cb;
1416 }
1417
1418 /*
1419 class wxPGCheckBoxRenderer : public wxPGDefaultRenderer
1420 {
1421 public:
1422
1423 virtual void Render( wxDC& dc, const wxRect& rect,
1424 const wxPropertyGrid* WXUNUSED(propertyGrid), wxPGProperty* property,
1425 int WXUNUSED(column), int WXUNUSED(item), int WXUNUSED(flags) ) const
1426 {
1427 int state = 0;
1428 if ( !(property->GetFlags() & wxPG_PROP_UNSPECIFIED) )
1429 {
1430 state = ((wxPGProperty*)property)->GetChoiceInfo((wxPGChoiceInfo*)NULL);
1431 if ( dc.GetFont().GetWeight() == wxBOLD ) state |= 2;
1432 }
1433 DrawSimpleCheckBox(dc,rect,dc.GetCharHeight(),state,dc.GetTextForeground());
1434 }
1435
1436 protected:
1437 };
1438
1439 wxPGCheckBoxRenderer g_wxPGCheckBoxRenderer;
1440
1441 wxPGCellRenderer* wxPGCheckBoxEditor::GetCellRenderer() const
1442 {
1443 return &g_wxPGCheckBoxRenderer;
1444 }
1445 */
1446
1447 void wxPGCheckBoxEditor::DrawValue( wxDC& dc, const wxRect& rect, wxPGProperty* property, const wxString& WXUNUSED(text) ) const
1448 {
1449 int state = 0;
1450 if ( !property->IsValueUnspecified() )
1451 {
1452 state = property->GetChoiceSelection();
1453 if ( dc.GetFont().GetWeight() == wxBOLD ) state |= 2;
1454 }
1455 DrawSimpleCheckBox(dc,rect,dc.GetCharHeight(),state,dc.GetTextForeground());
1456 }
1457
1458 void wxPGCheckBoxEditor::UpdateControl( wxPGProperty* property, wxWindow* ctrl ) const
1459 {
1460 wxASSERT( ctrl );
1461 ((wxSimpleCheckBox*)ctrl)->m_state = property->GetChoiceSelection();
1462 ctrl->Refresh();
1463 }
1464
1465
1466 bool wxPGCheckBoxEditor::OnEvent( wxPropertyGrid* WXUNUSED(propGrid), wxPGProperty* WXUNUSED(property),
1467 wxWindow* WXUNUSED(ctrl), wxEvent& event ) const
1468 {
1469 if ( event.GetEventType() == wxEVT_COMMAND_CHECKBOX_CLICKED )
1470 {
1471 return true;
1472 }
1473 return false;
1474 }
1475
1476
1477 bool wxPGCheckBoxEditor::GetValueFromControl( wxVariant& variant, wxPGProperty* property, wxWindow* ctrl ) const
1478 {
1479 wxSimpleCheckBox* cb = (wxSimpleCheckBox*)ctrl;
1480
1481 int index = cb->m_state;
1482
1483 if ( index != property->GetChoiceSelection() ||
1484 // Changing unspecified always causes event (returning
1485 // true here should be enough to trigger it).
1486 property->IsValueUnspecified()
1487 )
1488 {
1489 return property->IntToValue(variant, index, 0);
1490 }
1491 return false;
1492 }
1493
1494
1495 void wxPGCheckBoxEditor::SetControlIntValue( wxPGProperty* WXUNUSED(property), wxWindow* ctrl, int value ) const
1496 {
1497 if ( value != 0 ) value = 1;
1498 ((wxSimpleCheckBox*)ctrl)->m_state = value;
1499 ctrl->Refresh();
1500 }
1501
1502
1503 void wxPGCheckBoxEditor::SetValueToUnspecified( wxPGProperty* WXUNUSED(property), wxWindow* ctrl ) const
1504 {
1505 ((wxSimpleCheckBox*)ctrl)->m_state = 0;
1506 ctrl->Refresh();
1507 }
1508
1509
1510 wxPGCheckBoxEditor::~wxPGCheckBoxEditor() { }
1511
1512
1513 #endif // wxPG_INCLUDE_CHECKBOX
1514
1515 // -----------------------------------------------------------------------
1516
1517 wxWindow* wxPropertyGrid::GetEditorControl() const
1518 {
1519 wxWindow* ctrl = m_wndEditor;
1520
1521 if ( !ctrl )
1522 return ctrl;
1523
1524 return ctrl;
1525 }
1526
1527 // -----------------------------------------------------------------------
1528
1529 void wxPropertyGrid::CorrectEditorWidgetSizeX()
1530 {
1531 if ( m_selColumn == -1 )
1532 return;
1533
1534 int secWid = 0;
1535 int newSplitterx = m_pState->DoGetSplitterPosition(m_selColumn-1);
1536 int newWidth = newSplitterx + m_pState->m_colWidths[m_selColumn];
1537
1538 if ( m_wndEditor2 )
1539 {
1540 // if width change occurred, move secondary wnd by that amount
1541 wxRect r = m_wndEditor2->GetRect();
1542 secWid = r.width;
1543 r.x = newWidth - secWid;
1544
1545 m_wndEditor2->SetSize( r );
1546
1547 // if primary is textctrl, then we have to add some extra space
1548 #ifdef __WXMAC__
1549 if ( m_wndEditor )
1550 #else
1551 if ( m_wndEditor && m_wndEditor->IsKindOf(CLASSINFO(wxTextCtrl)) )
1552 #endif
1553 secWid += wxPG_TEXTCTRL_AND_BUTTON_SPACING;
1554 }
1555
1556 if ( m_wndEditor )
1557 {
1558 wxRect r = m_wndEditor->GetRect();
1559
1560 r.x = newSplitterx+m_ctrlXAdjust;
1561
1562 if ( !(m_iFlags & wxPG_FL_FIXED_WIDTH_EDITOR) )
1563 r.width = newWidth - r.x - secWid;
1564
1565 m_wndEditor->SetSize(r);
1566 }
1567
1568 if ( m_wndEditor2 )
1569 m_wndEditor2->Refresh();
1570 }
1571
1572 // -----------------------------------------------------------------------
1573
1574 void wxPropertyGrid::CorrectEditorWidgetPosY()
1575 {
1576 if ( m_selected && (m_wndEditor || m_wndEditor2) )
1577 {
1578 wxRect r = GetEditorWidgetRect(m_selected, m_selColumn);
1579
1580 if ( m_wndEditor )
1581 {
1582 wxPoint pos = m_wndEditor->GetPosition();
1583
1584 // Calculate y offset
1585 int offset = pos.y % m_lineHeight;
1586
1587 m_wndEditor->Move(pos.x, r.y + offset);
1588 }
1589
1590 if ( m_wndEditor2 )
1591 {
1592 wxPoint pos = m_wndEditor2->GetPosition();
1593
1594 m_wndEditor2->Move(pos.x, r.y);
1595 }
1596 }
1597 }
1598
1599 // -----------------------------------------------------------------------
1600
1601 // Fixes position of wxTextCtrl-like control (wxSpinCtrl usually
1602 // fits into that category as well).
1603 void wxPropertyGrid::FixPosForTextCtrl( wxWindow* ctrl, const wxPoint& offset )
1604 {
1605 // Center the control vertically
1606 wxRect finalPos = ctrl->GetRect();
1607 int y_adj = (m_lineHeight - finalPos.height)/2 + wxPG_TEXTCTRLYADJUST;
1608
1609 // Prevent over-sized control
1610 int sz_dec = (y_adj + finalPos.height) - m_lineHeight;
1611 if ( sz_dec < 0 ) sz_dec = 0;
1612
1613 finalPos.y += y_adj;
1614 finalPos.height -= (y_adj+sz_dec);
1615
1616 const int textCtrlXAdjust = wxPG_TEXTCTRLXADJUST;
1617
1618 finalPos.x += textCtrlXAdjust;
1619 finalPos.width -= textCtrlXAdjust;
1620
1621 finalPos.x += offset.x;
1622 finalPos.y += offset.y;
1623
1624 ctrl->SetSize(finalPos);
1625 }
1626
1627 // -----------------------------------------------------------------------
1628
1629 wxWindow* wxPropertyGrid::GenerateEditorTextCtrl( const wxPoint& pos,
1630 const wxSize& sz,
1631 const wxString& value,
1632 wxWindow* secondary,
1633 int extraStyle,
1634 int maxLen )
1635 {
1636 wxWindowID id = wxPG_SUBID1;
1637 wxPGProperty* selected = m_selected;
1638 wxASSERT(selected);
1639
1640 int tcFlags = wxTE_PROCESS_ENTER | extraStyle;
1641
1642 if ( selected->HasFlag(wxPG_PROP_READONLY) )
1643 tcFlags |= wxTE_READONLY;
1644
1645 wxPoint p(pos.x,pos.y);
1646 wxSize s(sz.x,sz.y);
1647
1648 // Need to reduce width of text control on Mac
1649 #if defined(__WXMAC__)
1650 s.x -= 8;
1651 #endif
1652
1653 // Take button into acccount
1654 if ( secondary )
1655 {
1656 s.x -= (secondary->GetSize().x + wxPG_TEXTCTRL_AND_BUTTON_SPACING);
1657 m_iFlags &= ~(wxPG_FL_PRIMARY_FILLS_ENTIRE);
1658 }
1659
1660 // If the height is significantly higher, then use border, and fill the rect exactly.
1661 bool hasSpecialSize = false;
1662
1663 if ( (sz.y - m_lineHeight) > 5 )
1664 hasSpecialSize = true;
1665
1666 wxWindow* ctrlParent = GetPanel();
1667
1668 if ( !hasSpecialSize )
1669 tcFlags |= wxBORDER_NONE;
1670
1671 wxTextCtrl* tc = new wxTextCtrl();
1672
1673 #if defined(__WXMSW__)
1674 tc->Hide();
1675 #endif
1676 SetupTextCtrlValue(value);
1677 tc->Create(ctrlParent,id,value, p, s,tcFlags);
1678
1679 wxWindow* ed = tc;
1680
1681 // Center the control vertically
1682 if ( !hasSpecialSize )
1683 FixPosForTextCtrl(ed);
1684
1685 #ifdef __WXMSW__
1686 ed->Show();
1687 if ( secondary )
1688 secondary->Show();
1689 #endif
1690
1691 // Set maximum length
1692 if ( maxLen > 0 )
1693 tc->SetMaxLength( maxLen );
1694
1695 // Connect event handling
1696 id = ed->GetId();
1697 this->Connect(id, wxEVT_COMMAND_TEXT_UPDATED,
1698 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent));
1699 this->Connect(id, wxEVT_COMMAND_TEXT_ENTER,
1700 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent));
1701
1702 return (wxWindow*) ed;
1703 }
1704
1705 // -----------------------------------------------------------------------
1706
1707 wxWindow* wxPropertyGrid::GenerateEditorButton( const wxPoint& pos, const wxSize& sz )
1708 {
1709 wxWindowID id = wxPG_SUBID2;
1710 wxPGProperty* selected = m_selected;
1711 wxASSERT(selected);
1712
1713 #ifdef __WXMAC__
1714 // Decorations are chunky on Mac, and we can't make the button square, so
1715 // do things a bit differently on this platform.
1716
1717 wxPoint p(pos.x+sz.x,
1718 pos.y+wxPG_BUTTON_SIZEDEC-wxPG_NAT_BUTTON_BORDER_Y);
1719 wxSize s(25, -1);
1720
1721 wxButton* but = new wxButton();
1722 but->Create(GetPanel(),id,wxS("..."),p,s,wxWANTS_CHARS);
1723
1724 // Now that we know the size, move to the correct position
1725 p.x = pos.x + sz.x - but->GetSize().x - 2;
1726 but->Move(p);
1727
1728 #else
1729 wxSize s(sz.y-(wxPG_BUTTON_SIZEDEC*2)+(wxPG_NAT_BUTTON_BORDER_Y*2),
1730 sz.y-(wxPG_BUTTON_SIZEDEC*2)+(wxPG_NAT_BUTTON_BORDER_Y*2));
1731
1732 // Reduce button width to lineheight
1733 if ( s.x > m_lineHeight )
1734 s.x = m_lineHeight;
1735
1736 #ifdef __WXGTK__
1737 // On wxGTK, take fixed button margins into account
1738 if ( s.x < 25 )
1739 s.x = 25;
1740 #endif
1741
1742 wxPoint p(pos.x+sz.x-s.x,
1743 pos.y+wxPG_BUTTON_SIZEDEC-wxPG_NAT_BUTTON_BORDER_Y);
1744
1745 wxButton* but = new wxButton();
1746 #ifdef __WXMSW__
1747 but->Hide();
1748 #endif
1749 but->Create(GetPanel(),id,wxS("..."),p,s,wxWANTS_CHARS);
1750
1751 #ifdef __WXGTK__
1752 wxFont font = GetFont();
1753 font.SetPointSize(font.GetPointSize()-2);
1754 but->SetFont(font);
1755 #else
1756 but->SetFont(GetFont());
1757 #endif
1758 #endif
1759
1760 if ( selected->HasFlag(wxPG_PROP_READONLY) )
1761 but->Disable();
1762
1763 // Connect event handling
1764 id = but->GetId();
1765 this->Connect(id, wxEVT_COMMAND_BUTTON_CLICKED,
1766 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent));
1767
1768 return but;
1769 }
1770
1771 // -----------------------------------------------------------------------
1772
1773 wxWindow* wxPropertyGrid::GenerateEditorTextCtrlAndButton( const wxPoint& pos,
1774 const wxSize& sz,
1775 wxWindow** psecondary,
1776 int limitedEditing,
1777 wxPGProperty* property )
1778 {
1779 wxButton* but = (wxButton*)GenerateEditorButton(pos,sz);
1780 *psecondary = (wxWindow*)but;
1781
1782 if ( limitedEditing )
1783 {
1784 #ifdef __WXMSW__
1785 // There is button Show in GenerateEditorTextCtrl as well
1786 but->Show();
1787 #endif
1788 return (wxWindow*) NULL;
1789 }
1790
1791 wxString text;
1792
1793 if ( !property->IsValueUnspecified() )
1794 text = property->GetValueString(property->HasFlag(wxPG_PROP_READONLY)?0:wxPG_EDITABLE_VALUE);
1795
1796 return GenerateEditorTextCtrl(pos,sz,text,but,property->m_maxLen);
1797 }
1798
1799 // -----------------------------------------------------------------------
1800
1801 wxTextCtrl* wxPropertyGrid::GetEditorTextCtrl() const
1802 {
1803 wxWindow* wnd = GetEditorControl();
1804
1805 if ( !wnd )
1806 return NULL;
1807
1808 if ( wnd->IsKindOf(CLASSINFO(wxTextCtrl)) )
1809 return wxStaticCast(wnd, wxTextCtrl);
1810
1811 if ( wnd->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)) )
1812 {
1813 wxOwnerDrawnComboBox* cb = wxStaticCast(wnd, wxOwnerDrawnComboBox);
1814 return cb->GetTextCtrl();
1815 }
1816
1817 return NULL;
1818 }
1819
1820 // -----------------------------------------------------------------------
1821
1822 wxPGEditor* wxPropertyGridInterface::GetEditorByName( const wxString& editorName )
1823 {
1824 wxPGHashMapS2P::const_iterator it;
1825
1826 it = wxPGGlobalVars->m_mapEditorClasses.find(editorName);
1827 if ( it == wxPGGlobalVars->m_mapEditorClasses.end() )
1828 return NULL;
1829 return (wxPGEditor*) it->second;
1830 }
1831
1832 // -----------------------------------------------------------------------
1833 // wxPGEditorDialogAdapter
1834 // -----------------------------------------------------------------------
1835
1836 IMPLEMENT_ABSTRACT_CLASS(wxPGEditorDialogAdapter, wxObject)
1837
1838 bool wxPGEditorDialogAdapter::ShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property )
1839 {
1840 if ( !propGrid->EditorValidate() )
1841 return false;
1842
1843 bool res = DoShowDialog( propGrid, property );
1844
1845 if ( res )
1846 {
1847 propGrid->ValueChangeInEvent( m_value );
1848 return true;
1849 }
1850
1851 return false;
1852 }
1853
1854 // -----------------------------------------------------------------------
1855 // wxPGMultiButton
1856 // -----------------------------------------------------------------------
1857
1858 wxPGMultiButton::wxPGMultiButton( wxPropertyGrid* pg, const wxSize& sz )
1859 : wxWindow( pg->GetPanel(), wxPG_SUBID2, wxPoint(-100,-100), wxSize(0, sz.y) ),
1860 m_fullEditorSize(sz), m_buttonsWidth(0)
1861 {
1862 SetBackgroundColour(pg->GetCellBackgroundColour());
1863 }
1864
1865 void wxPGMultiButton::Finalize( wxPropertyGrid* propGrid, const wxPoint& pos )
1866 {
1867 Move( pos.x + m_fullEditorSize.x - m_buttonsWidth, pos.y );
1868
1869 // Connect event handling
1870 for ( int i=0; i<GetCount(); i++ )
1871 {
1872 wxWindowID id = GetButtonId(i);
1873 propGrid->Connect(id, wxEVT_COMMAND_BUTTON_CLICKED,
1874 wxCommandEventHandler(wxPropertyGrid::OnCustomEditorEvent));
1875 }
1876 }
1877
1878 int wxPGMultiButton::GenId( int id ) const
1879 {
1880 if ( id < -1 )
1881 {
1882 if ( m_buttons.size() )
1883 id = GetButton(m_buttons.size()-1)->GetId() + 1;
1884 else
1885 id = wxPG_SUBID2;
1886 }
1887 return id;
1888 }
1889
1890 #if wxUSE_BMPBUTTON
1891 void wxPGMultiButton::Add( const wxBitmap& bitmap, int id )
1892 {
1893 id = GenId(id);
1894 wxSize sz = GetSize();
1895 wxButton* button = new wxBitmapButton( this, id, bitmap, wxPoint(sz.x, 0), wxSize(sz.y, sz.y) );
1896 m_buttons.push_back(button);
1897 int bw = button->GetSize().x;
1898 SetSize(wxSize(sz.x+bw,sz.y));
1899 m_buttonsWidth += bw;
1900 }
1901 #endif
1902
1903 void wxPGMultiButton::Add( const wxString& label, int id )
1904 {
1905 id = GenId(id);
1906 wxSize sz = GetSize();
1907 wxButton* button = new wxButton( this, id, label, wxPoint(sz.x, 0), wxSize(sz.y, sz.y) );
1908 m_buttons.push_back(button);
1909 int bw = button->GetSize().x;
1910 SetSize(wxSize(sz.x+bw,sz.y));
1911 m_buttonsWidth += bw;
1912 }
1913
1914 // -----------------------------------------------------------------------
1915
1916 #endif // wxUSE_PROPGRID