~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/ComboBoxBackend.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ComboBoxBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using Xwt.Backends;
 
28
using Xwt.Engine;
 
29
 
 
30
namespace Xwt.GtkBackend
 
31
{
 
32
        public class ComboBoxBackend: WidgetBackend, IComboBoxBackend, ICellRendererTarget
 
33
        {
 
34
                public ComboBoxBackend ()
 
35
                {
 
36
                }
 
37
 
 
38
                public override void Initialize ()
 
39
                {
 
40
                        Widget = (Gtk.ComboBox) CreateWidget ();
 
41
                        var cr = new Gtk.CellRendererText ();
 
42
                        Widget.PackStart (cr, false);
 
43
                        Widget.AddAttribute (cr, "text", 0);
 
44
                        Widget.Show ();
 
45
                        Widget.RowSeparatorFunc = IsRowSeparator;
 
46
                }
 
47
                
 
48
                protected virtual Gtk.Widget CreateWidget ()
 
49
                {
 
50
                        return new Gtk.ComboBox ();
 
51
                }
 
52
                
 
53
                protected new Gtk.ComboBox Widget {
 
54
                        get { return (Gtk.ComboBox)base.Widget; }
 
55
                        set { base.Widget = value; }
 
56
                }
 
57
                
 
58
                protected new IComboBoxEventSink EventSink {
 
59
                        get { return (IComboBoxEventSink)base.EventSink; }
 
60
                }
 
61
                
 
62
                bool IsRowSeparator (Gtk.TreeModel model, Gtk.TreeIter iter)
 
63
                {
 
64
                        Gtk.TreePath path = model.GetPath (iter);
 
65
                        bool res = false;
 
66
                        Toolkit.Invoke (delegate {
 
67
                                res = EventSink.RowIsSeparator (path.Indices[0]);
 
68
                        });
 
69
                        return res;
 
70
                }
 
71
                
 
72
                public override void EnableEvent (object eventId)
 
73
                {
 
74
                        base.EnableEvent (eventId);
 
75
                        if (eventId is ComboBoxEvent) {
 
76
                                if ((ComboBoxEvent)eventId == ComboBoxEvent.SelectionChanged)
 
77
                                        Widget.Changed += HandleChanged;
 
78
                        }
 
79
                }
 
80
                
 
81
                public override void DisableEvent (object eventId)
 
82
                {
 
83
                        base.DisableEvent (eventId);
 
84
                        if (eventId is ComboBoxEvent) {
 
85
                                if ((ComboBoxEvent)eventId == ComboBoxEvent.SelectionChanged)
 
86
                                        Widget.Changed -= HandleChanged;
 
87
                        }
 
88
                }
 
89
 
 
90
                void HandleChanged (object sender, EventArgs e)
 
91
                {
 
92
                        Toolkit.Invoke (delegate {
 
93
                                EventSink.OnSelectionChanged ();
 
94
                        });
 
95
                }
 
96
 
 
97
                #region IComboBoxBackend implementation
 
98
                public void SetViews (CellViewCollection views)
 
99
                {
 
100
                        Widget.Clear ();
 
101
                        foreach (var v in views)
 
102
                                CellUtil.CreateCellRenderer (this, null, v);
 
103
                }
 
104
 
 
105
                public void SetSource (IListDataSource source, IBackend sourceBackend)
 
106
                {
 
107
                        ListStoreBackend b = sourceBackend as ListStoreBackend;
 
108
                        if (b == null) {
 
109
                                CustomListModel model = new CustomListModel (source, Widget);
 
110
                                Widget.Model = model.Store;
 
111
                        } else
 
112
                                Widget.Model = b.Store;
 
113
                }
 
114
 
 
115
                public int SelectedRow {
 
116
                        get {
 
117
                                return Widget.Active;
 
118
                        }
 
119
                        set {
 
120
                                Widget.Active = value;
 
121
                        }
 
122
                }
 
123
                #endregion
 
124
 
 
125
                #region ICellRendererTarget implementation
 
126
                public void PackStart (object target, Gtk.CellRenderer cr, bool expand)
 
127
                {
 
128
                        Widget.PackStart (cr, expand);
 
129
                }
 
130
 
 
131
                public void PackEnd (object target, Gtk.CellRenderer cr, bool expand)
 
132
                {
 
133
                        Widget.PackEnd (cr, expand);
 
134
                }
 
135
 
 
136
                public void AddAttribute (object target, Gtk.CellRenderer cr, string field, int column)
 
137
                {
 
138
                        Widget.AddAttribute (cr, field, column);
 
139
                }
 
140
                #endregion
 
141
        }
 
142
}
 
143