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