Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ComboBoxBackend.cs « Xwt.GtkBackend « Xwt.Gtk - github.com/mono/xwt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f00511a4fb0e694729dd5bfdf1027b82f5ccc6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 
// ComboBoxBackend.cs
//  
// Author:
//       Lluis Sanchez <lluis@xamarin.com>
// 
// Copyright (c) 2011 Xamarin Inc
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Xwt.Backends;
using Gtk;
#if XWT_GTK3
using TreeModel = Gtk.ITreeModel;
#endif


namespace Xwt.GtkBackend
{
	public class ComboBoxBackend: WidgetBackend, IComboBoxBackend, ICellRendererTarget
	{
		public ComboBoxBackend ()
		{
		}

		public override void Initialize ()
		{
			//NeedsEventBox = false;  // TODO: needs fix: no events with or without event box
			Widget = (Gtk.ComboBox) CreateWidget ();
			if (Widget.Cells.Length == 0) {
				var cr = new Gtk.CellRendererText ();
				Widget.PackStart (cr, false);
				Widget.AddAttribute (cr, "text", 0);
			}
			Widget.Show ();
			Widget.RowSeparatorFunc = IsRowSeparator;
		}
		
		protected virtual Gtk.Widget CreateWidget ()
		{
			return new Gtk.ComboBox ();
		}
		
		protected new Gtk.ComboBox Widget {
			get { return (Gtk.ComboBox)base.Widget; }
			set { base.Widget = value; }
		}
		
		protected new IComboBoxEventSink EventSink {
			get { return (IComboBoxEventSink)base.EventSink; }
		}

		bool IsRowSeparator (TreeModel model, Gtk.TreeIter iter)
		{
			Gtk.TreePath path = model.GetPath (iter);
			bool res = false;
			ApplicationContext.InvokeUserCode (delegate {
				res = EventSink.RowIsSeparator (path.Indices[0]);
			});
			return res;
		}
		
		public override void EnableEvent (object eventId)
		{
			base.EnableEvent (eventId);
			if (eventId is ComboBoxEvent) {
				if ((ComboBoxEvent)eventId == ComboBoxEvent.SelectionChanged)
					Widget.Changed += HandleChanged;
			}
		}
		
		public override void DisableEvent (object eventId)
		{
			base.DisableEvent (eventId);
			if (eventId is ComboBoxEvent) {
				if ((ComboBoxEvent)eventId == ComboBoxEvent.SelectionChanged)
					Widget.Changed -= HandleChanged;
			}
		}

		void HandleChanged (object sender, EventArgs e)
		{
			ApplicationContext.InvokeUserCode (EventSink.OnSelectionChanged);
		}

		#region IComboBoxBackend implementation
		public void SetViews (CellViewCollection views)
		{
			Widget.Clear ();
			foreach (var v in views)
				CellUtil.CreateCellRenderer (ApplicationContext, Frontend, this, null, v);
		}

		public void SetSource (IListDataSource source, IBackend sourceBackend)
		{
			ListStoreBackend b = sourceBackend as ListStoreBackend;
			if (b == null) {
				CustomListModel model = new CustomListModel (source, Widget);
				Widget.Model = model.Store;
			} else
				Widget.Model = b.Store;
		}

		public int SelectedRow {
			get {
				return Widget.Active;
			}
			set {
				Widget.Active = value;
			}
		}
		#endregion

		#region ICellRendererTarget implementation
		public void PackStart (object target, Gtk.CellRenderer cr, bool expand)
		{
			Widget.PackStart (cr, expand);
		}	

		public void PackEnd (object target, Gtk.CellRenderer cr, bool expand)
		{
			Widget.PackEnd (cr, expand);
		}
			
		public void AddAttribute (object target, Gtk.CellRenderer cr, string field, int column)
		{
			Widget.AddAttribute (cr, field, column);
		}
		
		public void SetCellDataFunc (object target, Gtk.CellRenderer cr, Gtk.CellLayoutDataFunc dataFunc)
		{
			Widget.SetCellDataFunc (cr, dataFunc);
		}

		Rectangle ICellRendererTarget.GetCellBounds (object target, Gtk.CellRenderer cr, Gtk.TreeIter iter)
		{
			return new Rectangle ();
		}

		Rectangle ICellRendererTarget.GetCellBackgroundBounds (object target, Gtk.CellRenderer cr, Gtk.TreeIter iter)
		{
			return new Rectangle ();
		}

		public virtual void SetCurrentEventRow (string path)
		{
		}

		Gtk.Widget ICellRendererTarget.EventRootWidget {
			get { return Widget; }
		}
		TreeModel ICellRendererTarget.Model {
			get { return Widget.Model; }
		}

		Gtk.TreeIter ICellRendererTarget.PressedIter { get; set; }

		CellViewBackend ICellRendererTarget.PressedCell { get; set; }

		public bool GetCellPosition (Gtk.CellRenderer r, int ex, int ey, out int cx, out int cy, out Gtk.TreeIter it)
		{
			cx = cy = 0;
			it = Gtk.TreeIter.Zero;
			return false;
		}

		public void QueueDraw (object target, Gtk.TreeIter iter)
		{
		}

		public void QueueResize (object target, Gtk.TreeIter iter)
		{
		}

		#endregion
	}
}