summaryrefslogtreecommitdiff
path: root/platform/winrt/gsview/Selection.xaml.cs
blob: 1fe76fd1d882f7e8856f106d089ac8168f7b4e02 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace gsview
{
	/// <summary>
	/// Interaction logic for Selection.xaml
	/// </summary>
	/// 

	public enum SelectStatus_t
	{
		OK,
		CANCEL,
		SELECT,
		CLOSE,
		ZOOMIN,
		ZOOMOUT
	}

	public class SelectEventArgs : EventArgs
	{
		Point m_topleft, m_size;
		double m_zoomFactor;
		SelectStatus_t m_state;
		int m_page_num;
		Extract_Type_t m_type;

		public Point TopLeft
		{
			get { return m_topleft; }
		}

		public Point Size
		{
			get { return m_size; }
		}

		public double ZoomFactor
		{
			get { return m_zoomFactor; }
		}

		public int PageNum
		{
			get { return m_page_num; }
		}

		public SelectStatus_t State
		{
			get { return m_state; }
		}

		public Extract_Type_t Type
		{
			get { return m_type; }
		}

		public SelectEventArgs(Point start, Point size, double zoom, 
			SelectStatus_t state, int page, Extract_Type_t type)
		{
			m_topleft = start;
			m_size = size;
			m_zoomFactor = zoom;
			m_state = state;
			m_page_num = page;
			m_type = type;
		}
	}

	public partial class Selection : Window
	{
		private Point m_startPoint, m_topleft;
		private Point m_size;
		public SelectStatus_t m_curr_state;
		double m_zoom;
		double m_old_zoom;
		int m_page_num;
		private Rectangle m_rect;
		Extract_Type_t m_type;
		internal delegate void CallBackMain(object gsObject, SelectEventArgs info);
		internal event CallBackMain UpdateMain;

		public Selection(int page, double init_zoom, Extract_Type_t type)
		{
			InitializeComponent();
			this.Closing += new System.ComponentModel.CancelEventHandler(WindowClosing);
			m_page_num = page;
			m_zoom = init_zoom;
			m_curr_state = SelectStatus_t.OK;
			m_type = type;
			m_rect = null;
		}

		void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			var result = new SelectEventArgs(m_topleft, m_size, m_zoom,
				SelectStatus_t.CANCEL, m_page_num, m_type);
			UpdateMain(this, result);
		}

		private void ClickOK(object sender, RoutedEventArgs e)
		{
			if (m_curr_state != SelectStatus_t.OK)
				return;
			if (m_rect == null)
				Close();
			else
			{
				m_size.X = m_rect.Width;
				m_size.Y = m_rect.Height;
				m_topleft.Y = xaml_Image.Height - m_topleft.Y - m_size.Y;
				var result = new SelectEventArgs(m_topleft, m_size, m_zoom,
					SelectStatus_t.SELECT, m_page_num, m_type);
				UpdateMain(this, result);
			}
		}

		private void ClickExit(object sender, RoutedEventArgs e)
		{
			var result = new SelectEventArgs(m_topleft, m_size, m_zoom,
				SelectStatus_t.CANCEL, m_page_num, m_type);
			UpdateMain(this, result);
			Close();
		}

		private void ClickClear(object sender, RoutedEventArgs e)
		{
			if (m_rect != null)
			{
				xaml_Canvas.Children.Remove(m_rect);
				m_rect = null;
			}
		}

		private void ZoomIn(object sender, RoutedEventArgs e)
		{
			if (m_curr_state != SelectStatus_t.OK || m_zoom >= Constants.ZOOM_MAX)
				return;
			m_old_zoom = m_zoom;
			m_zoom = m_zoom + Constants.ZOOM_STEP;
			if (m_zoom > Constants.ZOOM_MAX)
			{
				m_zoom = Constants.ZOOM_MAX;
				return;
			}
			m_curr_state = SelectStatus_t.ZOOMIN;
			var result = new SelectEventArgs(m_startPoint, m_size, m_zoom,
				SelectStatus_t.ZOOMIN, m_page_num, m_type);
			UpdateMain(this, result);
		}

		private void ZoomOut(object sender, RoutedEventArgs e)
		{
			if (m_curr_state != SelectStatus_t.OK || m_zoom <= Constants.ZOOM_MIN)
				return;
			m_old_zoom = m_zoom;
			m_zoom = m_zoom - Constants.ZOOM_STEP;
			if (m_zoom < Constants.ZOOM_MIN)
			{
				m_zoom = Constants.ZOOM_MIN;
				return;
			}
			m_curr_state = SelectStatus_t.ZOOMOUT;
			var result = new SelectEventArgs(m_startPoint, m_size, m_zoom,
				SelectStatus_t.ZOOMOUT, m_page_num, m_type);
			UpdateMain(this, result);
		}

		/* Called when we have had a zoom change */
		public void UpdateRect()
		{
			if (m_rect != null)
			{
				double left = Canvas.GetLeft(m_rect);
				double top = Canvas.GetTop(m_rect);
				Canvas.SetLeft(m_rect, left * m_zoom / m_old_zoom);
				Canvas.SetTop(m_rect, top * m_zoom / m_old_zoom);
				m_rect.Width = m_rect.Width * m_zoom / m_old_zoom;
				m_rect.Height = m_rect.Height * m_zoom / m_old_zoom;
			}
		}

		private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
		{
			if (m_rect != null)
			{
				xaml_Canvas.Children.Remove(m_rect);
			}

			m_startPoint = e.GetPosition(xaml_Canvas);

			m_rect = new Rectangle
			{
				Stroke = Brushes.Red,
				StrokeThickness = 2
			};
			Canvas.SetLeft(m_rect, m_startPoint.X);
			Canvas.SetTop(m_rect, m_startPoint.X);
			xaml_Canvas.Children.Add(m_rect);
		}

		private void Canvas_MouseMove(object sender, MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Released || m_rect == null)
				return;

			var pos = e.GetPosition(xaml_Canvas);

			var x = Math.Min(pos.X, m_startPoint.X);
			var y = Math.Min(pos.Y, m_startPoint.Y);

			var w = Math.Max(pos.X, m_startPoint.X) - x;
			var h = Math.Max(pos.Y, m_startPoint.Y) - y;

			m_rect.Width = w;
			m_rect.Height = h;

			m_topleft.X = x;
			m_topleft.Y = y;
			Canvas.SetLeft(m_rect, x);
			Canvas.SetTop(m_rect, y);
		}
	}
}