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

CustomCellRenderer.cs « Xwt.GtkBackend.CellViews « Xwt.Gtk - github.com/mono/xwt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 22670f386a832024b54530346af1dbb616ccf8ad (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
//
// CustomCellRenderer.cs
//
// Author:
//       Lluis Sanchez <lluis@xamarin.com>
//
// Copyright (c) 2013 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 Gtk;
using Xwt.CairoBackend;
using Xwt.Backends;
using Xwt.GtkBackend;

namespace Xwt.GtkBackend
{
	class CustomCellRenderer: CellViewBackend, ICanvasCellViewBackend
	{
		Rectangle cellArea;
		Rectangle backgroundArea;
		bool isSelected;
		bool hasFocus;
		bool isPrelit;
		TreeIter lastIter;
		bool shown;

		public override void Initialize (ICellViewFrontend cellView, ICellRendererTarget rendererTarget, object target)
		{
			base.Initialize (cellView, rendererTarget, target);
			CellRenderer = new CanvasRenderer {
				Parent = this,
				CellView = (ICanvasCellViewFrontend) cellView 
			};
		}

		internal void StartDrawing (Rectangle ba, Rectangle ca, Gtk.CellRendererState flags)
		{
			this.cellArea = ca;
			this.backgroundArea = ba;
			isSelected = (flags & CellRendererState.Selected) != 0;
			hasFocus = (flags & CellRendererState.Focused) != 0;
			isPrelit = (flags & CellRendererState.Prelit) != 0;

			// Gtk will rerender the cell on every status change, hence we can expect the values
			// set here to be valid until the geometry or any other status of the cell and tree changes.
			// Setting shown=true ensures that those values are always reused instead if queried
			// from the parent tree after the cell has been rendered.
			shown = true;
		}

		protected override void OnLoadData ()
		{
			if (!CurrentIter.Equals (lastIter)) {
				// if the current iter has changed all cached values from the last draw are invalid
				shown = false;
				lastIter = CurrentIter;
			}
			base.OnLoadData ();
		}

		internal void EndDrawing ()
		{
		}

		public override Rectangle CellBounds {
			get {
				if (shown)
					return cellArea;
				return base.CellBounds;
			}
		}

		public override Rectangle BackgroundBounds {
			get {
				if (shown)
					return backgroundArea;
				return base.BackgroundBounds;
			}
		}

		public override bool Selected {
			get {
				if (shown)
					return isSelected;
				return base.Selected;
			}
		}

		public override bool HasFocus {
			get {
				if (shown)
					return hasFocus;
				return base.HasFocus;
			}
		}

		public bool IsHighlighted {
			get {
				if (shown)
					return isPrelit;
				return false;
			}
		}
	}

	class CanvasRenderer: GtkCellRendererCustom
	{
		public ICanvasCellViewFrontend CellView;
		public CustomCellRenderer Parent;

		protected override void OnRender (Cairo.Context cr, Gtk.Widget widget, Gdk.Rectangle background_area, Gdk.Rectangle cell_area, CellRendererState flags)
		{
			int wx, wy, dx = 0, dy = 0;
			var tree = widget as Gtk.TreeView;
			// Tree coordinates must be converted to widget coordinates,
			// otherwise custom cell bounds will have an offset to the parent widget
			if (tree != null) {
				tree.ConvertBinWindowToWidgetCoords (cell_area.X, cell_area.Y, out wx, out wy);
				dx = wx - cell_area.X;
				dy = wy - cell_area.Y;
				cell_area.X += dx;
				background_area.X += dx;
				cell_area.Y += dy;
				background_area.Y += dy;
			}

			Parent.StartDrawing (new Rectangle (background_area.X, background_area.Y, background_area.Width, background_area.Height), new Rectangle (cell_area.X, cell_area.Y, cell_area.Width, cell_area.Height), flags);
			CellView.ApplicationContext.InvokeUserCode (delegate {
				CairoContextBackend ctx = new CairoContextBackend (Util.GetScaleFactor (widget));
				ctx.Context = cr;
				ctx.Context.Translate(-dx, -dy);
				using (ctx) {
					CellView.Draw (ctx, new Rectangle (cell_area.X, cell_area.Y, cell_area.Width, cell_area.Height));
				}
			});
			Parent.EndDrawing ();
		}

		protected override void OnGetSize (Gtk.Widget widget, ref Gdk.Rectangle cell_area, out int x_offset, out int y_offset, out int width, out int height)
		{
			Size size = new Size ();
			var widthConstraint = cell_area.Width > 0 ? SizeConstraint.WithSize(cell_area.Width) : SizeConstraint.Unconstrained;
			CellView.ApplicationContext.InvokeUserCode (delegate {
				size = CellView.GetRequiredSize (widthConstraint);
			});
			width = (int) size.Width;
			height = (int) size.Height;
			x_offset = y_offset = 0;
		}
	}
}