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 <https://www.gnu.org/licenses/>.
18 */
19 #include "previewwindow.h"
20 #include "guiutils.h"
21 #include "imagearea.h"
22 #include "cursormanager.h"
23 #include "options.h"
24 #include "rtscalable.h"
25
26 #include "../rtengine/procparams.h"
27
PreviewWindow()28 PreviewWindow::PreviewWindow () : previewHandler(nullptr), mainCropWin(nullptr), imageArea(nullptr), imgX(0), imgY(0), imgW(0), imgH(0),
29 zoom(0.0), press_x(0), press_y(0), isMoving(false), needsUpdate(false), cursor_type(CSUndefined)
30
31 {
32 set_name("PreviewWindow");
33 get_style_context()->add_class("drawingarea");
34 rconn = signal_size_allocate().connect( sigc::mem_fun(*this, &PreviewWindow::on_resized) );
35 }
36
on_realize()37 void PreviewWindow::on_realize ()
38 {
39
40 Gtk::DrawingArea::on_realize ();
41 add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
42 }
43
getObservedFrameArea(int & x,int & y,int & w,int & h)44 void PreviewWindow::getObservedFrameArea (int& x, int& y, int& w, int& h)
45 {
46
47 if (mainCropWin) {
48 int cropX, cropY, cropW, cropH;
49 mainCropWin->getCropRectangle (cropX, cropY, cropW, cropH);
50 // translate it to screen coordinates
51 x = imgX + round(cropX * zoom);
52 y = imgY + round(cropY * zoom);
53 w = round(cropW * zoom);
54 h = round(cropH * zoom);
55 }
56 }
57
updatePreviewImage()58 void PreviewWindow::updatePreviewImage ()
59 {
60
61 int W = get_width(), H = get_height();
62 Glib::RefPtr<Gdk::Window> wind = get_window();
63
64 if( ! wind ) {
65 needsUpdate = true;
66 return;
67 }
68
69 backBuffer = Cairo::RefPtr<BackBuffer> ( new BackBuffer(W, H, Cairo::FORMAT_ARGB32) );
70 Cairo::RefPtr<Cairo::ImageSurface> surface = backBuffer->getSurface();
71 Glib::RefPtr<Gtk::StyleContext> style = get_style_context();
72 Cairo::RefPtr<Cairo::Context> cc = Cairo::Context::create(surface);
73 cc->set_source_rgba (0., 0., 0., 0.);
74 cc->set_operator (Cairo::OPERATOR_CLEAR);
75 cc->paint ();
76 cc->set_operator (Cairo::OPERATOR_OVER);
77 cc->set_antialias(Cairo::ANTIALIAS_NONE);
78 cc->set_line_join(Cairo::LINE_JOIN_MITER);
79
80 if (previewHandler) {
81 Glib::RefPtr<Gdk::Pixbuf> resPixbuf = previewHandler->getRoughImage (W, H, zoom);
82
83 if (resPixbuf) {
84 imgW = resPixbuf->get_width();
85 imgH = resPixbuf->get_height();
86 imgX = (W - imgW) / 2;
87 imgY = (H - imgH) / 2;
88 Gdk::Cairo::set_source_pixbuf(cc, resPixbuf, imgX, imgY);
89 cc->rectangle(imgX, imgY, imgW, imgH);
90 cc->fill();
91
92 if (previewHandler->getCropParams().enabled) {
93 rtengine::procparams::CropParams cparams = previewHandler->getCropParams();
94 switch (options.cropGuides) {
95 case Options::CROP_GUIDE_NONE:
96 cparams.guide = "None";
97 break;
98 case Options::CROP_GUIDE_FRAME:
99 cparams.guide = "Frame";
100 break;
101 default:
102 break;
103 }
104 drawCrop (cc, imgX, imgY, imgW, imgH, 0, 0, zoom, cparams, true, false);
105 }
106 }
107 }
108 }
109
setPreviewHandler(PreviewHandler * ph)110 void PreviewWindow::setPreviewHandler (PreviewHandler* ph)
111 {
112
113 previewHandler = ph;
114
115 if (previewHandler) {
116 previewHandler->addPreviewImageListener (this);
117 }
118 }
119
on_resized(Gtk::Allocation & req)120 void PreviewWindow::on_resized (Gtk::Allocation& req)
121 {
122
123 updatePreviewImage ();
124 queue_draw ();
125 }
126
on_draw(const::Cairo::RefPtr<Cairo::Context> & cr)127 bool PreviewWindow::on_draw(const ::Cairo::RefPtr< Cairo::Context> &cr)
128 {
129 const Glib::RefPtr<Gtk::StyleContext> style = get_style_context();
130 style->render_background(cr, 0, 0, get_width(), get_height());
131
132 if (!backBuffer) {
133 return true;
134 }
135
136 int bufferW, bufferH;
137 bufferW = backBuffer->getWidth();
138 bufferH = backBuffer->getHeight();
139
140 if (!mainCropWin && imageArea) {
141 mainCropWin = imageArea->getMainCropWindow ();
142
143 if (mainCropWin) {
144 mainCropWin->addCropWindowListener (this);
145 }
146 }
147
148 if ((get_width() != bufferW && get_height() != bufferH) || needsUpdate) {
149 needsUpdate = false;
150 updatePreviewImage ();
151 }
152
153 backBuffer->copySurface(cr, NULL);
154
155 if (mainCropWin && zoom > 0.0) {
156 int x, y, w, h;
157 getObservedFrameArea (x, y, w, h);
158 if (x>imgX || y>imgY || w < imgW || h < imgH) {
159 double s = RTScalable::getScale();
160 double rectX = x + 0.5;
161 double rectY = y + 0.5;
162 double rectW = std::min(w, (int)(imgW - (x - imgX) - 1));
163 double rectH = std::min(h, (int)(imgH - (y - imgY) - 1));
164
165 // draw a black "shadow" line
166 cr->set_source_rgba (0.0, 0.0, 0.0, 0.65);
167 cr->set_line_width (1. * s);
168 cr->set_line_join(Cairo::LINE_JOIN_MITER);
169 cr->rectangle (rectX + 1. * s, rectY + 1. * s, rectW - 2. * s, rectH - 2. * s);
170 cr->stroke ();
171
172 // draw a "frame" line. Color of frame line can be set in preferences
173 cr->set_source_rgba(options.navGuideBrush[0], options.navGuideBrush[1], options.navGuideBrush[2], options.navGuideBrush[3]); //( 1.0, 1.0, 1.0, 1.0);
174 cr->rectangle (rectX, rectY, rectW, rectH);
175 cr->stroke ();
176 }
177 }
178
179 style->render_frame (cr, 0, 0, get_width(), get_height());
180
181 return true;
182 }
183
previewImageChanged()184 void PreviewWindow::previewImageChanged ()
185 {
186
187 updatePreviewImage ();
188 queue_draw ();
189 }
190
setImageArea(ImageArea * ia)191 void PreviewWindow::setImageArea (ImageArea* ia)
192 {
193
194 imageArea = ia;
195 mainCropWin = ia->getMainCropWindow ();
196
197 if (mainCropWin) {
198 mainCropWin->addCropWindowListener (this);
199 }
200 }
201
cropPositionChanged(CropWindow * w)202 void PreviewWindow::cropPositionChanged(CropWindow* w)
203 {
204 queue_draw ();
205 }
206
cropWindowSizeChanged(CropWindow * w)207 void PreviewWindow::cropWindowSizeChanged(CropWindow* w)
208 {
209 queue_draw ();
210 }
211
cropZoomChanged(CropWindow * w)212 void PreviewWindow::cropZoomChanged(CropWindow* w)
213 {
214 queue_draw ();
215 }
216
initialImageArrived()217 void PreviewWindow::initialImageArrived()
218 {
219 }
220
on_motion_notify_event(GdkEventMotion * event)221 bool PreviewWindow::on_motion_notify_event (GdkEventMotion* event)
222 {
223
224 if (!mainCropWin) {
225 return true;
226 }
227
228 int x, y, w, h;
229 getObservedFrameArea (x, y, w, h);
230 if (x>imgX || y>imgY || w < imgW || h < imgH) {
231 bool inside = event->x > x - 6 && event->x < x + w - 1 + 6 && event->y > y - 6 && event->y < y + h - 1 + 6;
232
233 CursorShape newType;
234
235 if (isMoving) {
236 mainCropWin->remoteMove ((event->x - press_x) / zoom, (event->y - press_y) / zoom);
237 press_x = event->x;
238 press_y = event->y;
239 newType = CSHandClosed;
240 } else if (inside) {
241 newType = CSHandOpen;
242 } else {
243 newType = CSArrow;
244 }
245
246 if (newType != cursor_type) {
247 cursor_type = newType;
248 CursorManager::setWidgetCursor(get_window(), cursor_type);
249 }
250 }
251
252 return true;
253 }
254
on_button_press_event(GdkEventButton * event)255 bool PreviewWindow::on_button_press_event (GdkEventButton* event)
256 {
257
258 if (!mainCropWin) {
259 return true;
260 }
261
262 int x, y, w, h;
263 getObservedFrameArea (x, y, w, h);
264 if (x>imgX || y>imgY || w < imgW || h < imgH) {
265
266 if (!isMoving) {
267 isMoving = true;
268
269 press_x = event->x;
270 press_y = event->y;
271
272 if (cursor_type != CSHandClosed) {
273 cursor_type = CSHandClosed;
274 CursorManager::setWidgetCursor(get_window(), cursor_type);
275 }
276 }
277 }
278
279 return true;
280 }
281
on_button_release_event(GdkEventButton * event)282 bool PreviewWindow::on_button_release_event (GdkEventButton* event)
283 {
284
285 if (!mainCropWin) {
286 return true;
287 }
288
289 if (isMoving) {
290 isMoving = false;
291
292 if (cursor_type != CSArrow) {
293 cursor_type = CSArrow;
294 CursorManager::setWidgetCursor(get_window(), cursor_type);
295 }
296
297 mainCropWin->remoteMoveReady ();
298 }
299
300 return true;
301 }
302
get_request_mode_vfunc() const303 Gtk::SizeRequestMode PreviewWindow::get_request_mode_vfunc () const
304 {
305 return Gtk::SIZE_REQUEST_CONSTANT_SIZE;
306 }
307
get_preferred_height_vfunc(int & minimum_height,int & natural_height) const308 void PreviewWindow::get_preferred_height_vfunc (int &minimum_height, int &natural_height) const
309 {
310 minimum_height= 50 * RTScalable::getScale();
311 natural_height = 100 * RTScalable::getScale();
312 }
313
get_preferred_width_vfunc(int & minimum_width,int & natural_width) const314 void PreviewWindow::get_preferred_width_vfunc (int &minimum_width, int &natural_width) const
315 {
316 minimum_width = 80 * RTScalable::getScale();
317 natural_width = 120 * RTScalable::getScale();
318 }
319
get_preferred_height_for_width_vfunc(int width,int & minimum_height,int & natural_height) const320 void PreviewWindow::get_preferred_height_for_width_vfunc (int width, int &minimum_height, int &natural_height) const
321 {
322 get_preferred_height_vfunc(minimum_height, natural_height);
323 }
324
get_preferred_width_for_height_vfunc(int height,int & minimum_width,int & natural_width) const325 void PreviewWindow::get_preferred_width_for_height_vfunc (int height, int &minimum_width, int &natural_width) const
326 {
327 get_preferred_width_vfunc (minimum_width, natural_width);
328 }
329