1 /*
2 * This file is part of RawTherapee.
3 *
4 * Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
5 *
6 * RawTherapee is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * RawTherapee is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "previewwindow.h"
20 #include "guiutils.h"
21 #include "imagearea.h"
22 #include "cursormanager.h"
23
PreviewWindow()24 PreviewWindow::PreviewWindow () : previewHandler(nullptr), mainCropWin(nullptr), imageArea(nullptr), imgX(0), imgY(0), imgW(0), imgH(0),
25 zoom(0.0), press_x(0), press_y(0), isMoving(false), needsUpdate(false), cursor_type(CSUndefined)
26
27 {
28 set_name("PreviewWindow");
29 get_style_context()->add_class("drawingarea");
30 rconn = signal_size_allocate().connect( sigc::mem_fun(*this, &PreviewWindow::on_resized) );
31 }
32
on_realize()33 void PreviewWindow::on_realize ()
34 {
35
36 Gtk::DrawingArea::on_realize ();
37 add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
38 }
39
getObservedFrameArea(int & x,int & y,int & w,int & h)40 void PreviewWindow::getObservedFrameArea (int& x, int& y, int& w, int& h)
41 {
42
43 if (mainCropWin) {
44 int cropX, cropY, cropW, cropH;
45 mainCropWin->getCropRectangle (cropX, cropY, cropW, cropH);
46 // translate it to screen coordinates
47 x = imgX + round(cropX * zoom);
48 y = imgY + round(cropY * zoom);
49 w = round(cropW * zoom);
50 h = round(cropH * zoom);
51 }
52 }
53
updatePreviewImage()54 void PreviewWindow::updatePreviewImage ()
55 {
56
57 int W = get_width(), H = get_height();
58 Glib::RefPtr<Gdk::Window> wind = get_window();
59
60 if( ! wind ) {
61 needsUpdate = true;
62 return;
63 }
64
65 backBuffer = Cairo::RefPtr<BackBuffer> ( new BackBuffer(W, H, Cairo::FORMAT_ARGB32) );
66 Cairo::RefPtr<Cairo::ImageSurface> surface = backBuffer->getSurface();
67 Glib::RefPtr<Gtk::StyleContext> style = get_style_context();
68 Cairo::RefPtr<Cairo::Context> cc = Cairo::Context::create(surface);
69 cc->set_source_rgba (0., 0., 0., 0.);
70 cc->set_operator (Cairo::OPERATOR_CLEAR);
71 cc->paint ();
72 cc->set_operator (Cairo::OPERATOR_OVER);
73 cc->set_antialias(Cairo::ANTIALIAS_NONE);
74 cc->set_line_join(Cairo::LINE_JOIN_MITER);
75
76 if (previewHandler) {
77 Glib::RefPtr<Gdk::Pixbuf> resPixbuf = previewHandler->getRoughImage (W, H, zoom);
78
79 if (resPixbuf) {
80 imgW = resPixbuf->get_width();
81 imgH = resPixbuf->get_height();
82 imgX = (W - imgW) / 2;
83 imgY = (H - imgH) / 2;
84 Gdk::Cairo::set_source_pixbuf(cc, resPixbuf, imgX, imgY);
85 cc->rectangle(imgX, imgY, imgW, imgH);
86 cc->fill();
87
88 if (previewHandler->getCropParams().enabled) {
89 rtengine::CropParams cparams = previewHandler->getCropParams();
90 cparams.guide = "Frame";
91 drawCrop (cc, imgX, imgY, imgW, imgH, 0, 0, zoom, cparams, true, false);
92 }
93 }
94 }
95 }
96
setPreviewHandler(PreviewHandler * ph)97 void PreviewWindow::setPreviewHandler (PreviewHandler* ph)
98 {
99
100 previewHandler = ph;
101
102 if (previewHandler) {
103 previewHandler->addPreviewImageListener (this);
104 }
105 }
106
on_resized(Gtk::Allocation & req)107 void PreviewWindow::on_resized (Gtk::Allocation& req)
108 {
109
110 updatePreviewImage ();
111 queue_draw ();
112 }
113
on_draw(const::Cairo::RefPtr<Cairo::Context> & cr)114 bool PreviewWindow::on_draw(const ::Cairo::RefPtr< Cairo::Context> &cr)
115 {
116 const Glib::RefPtr<Gtk::StyleContext> style = get_style_context();
117 style->render_background(cr, 0, 0, get_width(), get_height());
118
119 if (!backBuffer) {
120 return true;
121 }
122
123 int bufferW, bufferH;
124 bufferW = backBuffer->getWidth();
125 bufferH = backBuffer->getHeight();
126
127 if (!mainCropWin && imageArea) {
128 mainCropWin = imageArea->getMainCropWindow ();
129
130 if (mainCropWin) {
131 mainCropWin->addCropWindowListener (this);
132 }
133 }
134
135 if ((get_width() != bufferW && get_height() != bufferH) || needsUpdate) {
136 needsUpdate = false;
137 updatePreviewImage ();
138 }
139
140 backBuffer->copySurface(cr, NULL);
141
142 if (mainCropWin && zoom > 0.0) {
143 int x, y, w, h;
144 getObservedFrameArea (x, y, w, h);
145 if (x>imgX || y>imgY || w < imgW || h < imgH) {
146 double s = RTScalable::getScale();
147 double rectX = x + 0.5;
148 double rectY = y + 0.5;
149 double rectW = std::min(w, (int)(imgW - (x - imgX) - 1));
150 double rectH = std::min(h, (int)(imgH - (y - imgY) - 1));
151
152 // draw a black "shadow" line
153 cr->set_source_rgba (0.0, 0.0, 0.0, 0.65);
154 cr->set_line_width (1. * s);
155 cr->set_line_join(Cairo::LINE_JOIN_MITER);
156 cr->rectangle (rectX + 1. * s, rectY + 1. * s, rectW - 2. * s, rectH - 2. * s);
157 cr->stroke ();
158
159 // draw a "frame" line. Color of frame line can be set in preferences
160 cr->set_source_rgba(options.navGuideBrush[0], options.navGuideBrush[1], options.navGuideBrush[2], options.navGuideBrush[3]); //( 1.0, 1.0, 1.0, 1.0);
161 cr->rectangle (rectX, rectY, rectW, rectH);
162 cr->stroke ();
163 }
164 }
165
166 style->render_frame (cr, 0, 0, get_width(), get_height());
167
168 return true;
169 }
170
previewImageChanged()171 void PreviewWindow::previewImageChanged ()
172 {
173
174 updatePreviewImage ();
175 queue_draw ();
176 }
177
setImageArea(ImageArea * ia)178 void PreviewWindow::setImageArea (ImageArea* ia)
179 {
180
181 imageArea = ia;
182 mainCropWin = ia->getMainCropWindow ();
183
184 if (mainCropWin) {
185 mainCropWin->addCropWindowListener (this);
186 }
187 }
188
cropPositionChanged(CropWindow * w)189 void PreviewWindow::cropPositionChanged(CropWindow* w)
190 {
191 queue_draw ();
192 }
193
cropWindowSizeChanged(CropWindow * w)194 void PreviewWindow::cropWindowSizeChanged(CropWindow* w)
195 {
196 queue_draw ();
197 }
198
cropZoomChanged(CropWindow * w)199 void PreviewWindow::cropZoomChanged(CropWindow* w)
200 {
201 queue_draw ();
202 }
203
initialImageArrived()204 void PreviewWindow::initialImageArrived()
205 {
206 }
207
on_motion_notify_event(GdkEventMotion * event)208 bool PreviewWindow::on_motion_notify_event (GdkEventMotion* event)
209 {
210
211 if (!mainCropWin) {
212 return true;
213 }
214
215 int x, y, w, h;
216 getObservedFrameArea (x, y, w, h);
217 if (x>imgX || y>imgY || w < imgW || h < imgH) {
218 bool inside = event->x > x - 6 && event->x < x + w - 1 + 6 && event->y > y - 6 && event->y < y + h - 1 + 6;
219
220 CursorShape newType = cursor_type;
221
222 if (isMoving) {
223 mainCropWin->remoteMove ((event->x - press_x) / zoom, (event->y - press_y) / zoom);
224 press_x = event->x;
225 press_y = event->y;
226 newType = CSHandClosed;
227 } else if (inside) {
228 newType = CSHandOpen;
229 } else {
230 newType = CSArrow;
231 }
232
233 if (newType != cursor_type) {
234 cursor_type = newType;
235 CursorManager::setWidgetCursor(get_window(), cursor_type);
236 }
237 }
238
239 return true;
240 }
241
on_button_press_event(GdkEventButton * event)242 bool PreviewWindow::on_button_press_event (GdkEventButton* event)
243 {
244
245 if (!mainCropWin) {
246 return true;
247 }
248
249 int x, y, w, h;
250 getObservedFrameArea (x, y, w, h);
251 if (x>imgX || y>imgY || w < imgW || h < imgH) {
252
253 if (!isMoving) {
254 isMoving = true;
255
256 press_x = event->x;
257 press_y = event->y;
258
259 if (cursor_type != CSHandClosed) {
260 cursor_type = CSHandClosed;
261 CursorManager::setWidgetCursor(get_window(), cursor_type);
262 }
263 }
264 }
265
266 return true;
267 }
268
on_button_release_event(GdkEventButton * event)269 bool PreviewWindow::on_button_release_event (GdkEventButton* event)
270 {
271
272 if (!mainCropWin) {
273 return true;
274 }
275
276 if (isMoving) {
277 isMoving = false;
278
279 if (cursor_type != CSArrow) {
280 cursor_type = CSArrow;
281 CursorManager::setWidgetCursor(get_window(), cursor_type);
282 }
283
284 mainCropWin->remoteMoveReady ();
285 }
286
287 return true;
288 }
289
get_request_mode_vfunc() const290 Gtk::SizeRequestMode PreviewWindow::get_request_mode_vfunc () const
291 {
292 return Gtk::SIZE_REQUEST_CONSTANT_SIZE;
293 }
294
get_preferred_height_vfunc(int & minimum_height,int & natural_height) const295 void PreviewWindow::get_preferred_height_vfunc (int &minimum_height, int &natural_height) const
296 {
297 minimum_height= 50 * RTScalable::getScale();
298 natural_height = 100 * RTScalable::getScale();
299 }
300
get_preferred_width_vfunc(int & minimum_width,int & natural_width) const301 void PreviewWindow::get_preferred_width_vfunc (int &minimum_width, int &natural_width) const
302 {
303 minimum_width = 80 * RTScalable::getScale();
304 natural_width = 120 * RTScalable::getScale();
305 }
306
get_preferred_height_for_width_vfunc(int width,int & minimum_height,int & natural_height) const307 void PreviewWindow::get_preferred_height_for_width_vfunc (int width, int &minimum_height, int &natural_height) const
308 {
309 get_preferred_height_vfunc(minimum_height, natural_height);
310 }
311
get_preferred_width_for_height_vfunc(int height,int & minimum_width,int & natural_width) const312 void PreviewWindow::get_preferred_width_for_height_vfunc (int height, int &minimum_width, int &natural_width) const
313 {
314 get_preferred_width_vfunc (minimum_width, natural_width);
315 }
316