Proper check for rawzor libraries.
[rawtherapee-fixes.git] / rtgui / previewwindow.cc
blob9c3d647dd234b3a50718b3546fa16b7378f26e8c
1 /*
2 * This file is part of RawTherapee.
4 * Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
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.
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.
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/>.
19 #include <previewwindow.h>
20 #include <guiutils.h>
21 #include <imagearea.h>
23 PreviewWindow::PreviewWindow () : previewHandler(NULL), mainCropWin(NULL), isMoving(false) {
25 rconn = signal_size_allocate().connect( sigc::mem_fun(*this, &PreviewWindow::on_resized) );
28 PreviewWindow::~PreviewWindow () {
30 delete cCropMoving;
31 #ifndef WIN32
32 delete cNormal;
33 #endif
36 void PreviewWindow::on_realize () {
38 Gtk::DrawingArea::on_realize ();
39 add_events(Gdk::EXPOSURE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::SCROLL_MASK);
40 cCropMoving = new Gdk::Cursor (Gdk::FLEUR);
41 #ifdef _WIN32
42 cNormal = new Gdk::Cursor (Gdk::LAST_CURSOR);
43 #else
44 cNormal = new Gdk::Cursor (Gdk::ARROW);
45 #endif
48 void PreviewWindow::getObservedFrameArea (int& x, int& y, int& w, int& h) {
50 if (mainCropWin) {
51 int cropX, cropY, cropW, cropH;
52 mainCropWin->getCropRectangle (cropX, cropY, cropW, cropH);
53 // translate it to screen coordinates
54 x = imgX + cropX*zoom;
55 y = imgY + cropY*zoom;
56 w = cropW * zoom;
57 h = cropH * zoom;
61 void PreviewWindow::updatePreviewImage () {
63 int W = get_width(), H = get_height();
64 backBuffer = Gdk::Pixmap::create (get_window(), W, H, -1);
65 backBuffer->draw_rectangle (get_style()->get_base_gc(Gtk::STATE_NORMAL), true, 0, 0, W, H);
66 if (previewHandler) {
67 Glib::RefPtr<Gdk::Pixbuf> resPixbuf = previewHandler->getRoughImage (W, H, zoom);
68 if (resPixbuf) {
69 imgW = resPixbuf->get_width();
70 imgH = resPixbuf->get_height();
71 imgX = (W-imgW)/2;
72 imgY = (H-imgH)/2;
73 backBuffer->draw_pixbuf (get_style()->get_base_gc(Gtk::STATE_NORMAL), resPixbuf, 0, 0, imgX, imgY, -1, -1, Gdk::RGB_DITHER_NONE, 0, 0);
74 Cairo::RefPtr<Cairo::Context> cr = backBuffer->create_cairo_context();
75 if (previewHandler->getCropParams().enabled)
76 drawCrop (cr, imgX, imgY, imgW, imgH, 0, 0, zoom, previewHandler->getCropParams());
81 void PreviewWindow::setPreviewHandler (PreviewHandler* ph) {
83 previewHandler = ph;
84 previewHandler->addPreviewImageListener (this);
87 void PreviewWindow::on_resized (Gtk::Allocation& req) {
89 updatePreviewImage ();
90 queue_draw ();
93 bool PreviewWindow::on_expose_event (GdkEventExpose* event) {
95 if (backBuffer) {
96 Glib::RefPtr<Gdk::Window> window = get_window();
98 int bufferW, bufferH;
99 backBuffer->get_size (bufferW, bufferH);
101 if (!mainCropWin) {
102 mainCropWin = imageArea->getMainCropWindow ();
103 if (mainCropWin)
104 mainCropWin->addCropWindowListener (this);
107 if (get_width()!=bufferW && get_height()!=bufferH)
108 updatePreviewImage ();
110 window->draw_drawable (get_style()->get_base_gc(Gtk::STATE_NORMAL), backBuffer, 0, 0, 0, 0, -1, -1);
112 if (mainCropWin) {
113 Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context();
114 int x, y, w, h;
115 getObservedFrameArea (x, y, w, h);
116 cr->set_source_rgb (1.0, 1.0, 1.0);
117 cr->set_line_width (3);
118 cr->rectangle (x-1.5, y-1.5, w+2, h+2);
119 cr->stroke ();
120 cr->set_source_rgb (1.0, 0.0, 0.0);
121 cr->set_line_width (1);
122 cr->rectangle (x-1.5, y-1.5, w+2, h+2);
123 cr->stroke ();
126 return true;
129 void PreviewWindow::previewImageChanged () {
131 updatePreviewImage ();
132 queue_draw ();
135 void PreviewWindow::setImageArea (ImageArea* ia) {
137 imageArea = ia;
138 mainCropWin = ia->getMainCropWindow ();
139 if (mainCropWin)
140 mainCropWin->addCropWindowListener (this);
143 void PreviewWindow::cropPositionChanged (CropWindow* w) {
145 queue_draw ();
148 void PreviewWindow::cropWindowSizeChanged (CropWindow* w) {
150 queue_draw ();
153 void PreviewWindow::cropZoomChanged (CropWindow* w) {
155 queue_draw ();
158 bool PreviewWindow::on_motion_notify_event (GdkEventMotion* event) {
160 if (!mainCropWin)
161 return true;
163 int x, y, w, h;
164 getObservedFrameArea (x, y, w, h);
165 bool inside = event->x > x-6 && event->x < x+w-1+6 && event->y > y-6 && event->y < y+h-1+6;
166 bool moreInside = event->x > x+6 && event->x < x+w-1-6 && event->y > y+6 && event->y < y+h-1-6;
168 if (isMoving)
169 mainCropWin->remoteMove ((event->x - press_x)/zoom, (event->y - press_y)/zoom);
170 else if (inside && !moreInside)
171 get_window()->set_cursor (*cCropMoving);
172 else
173 get_window()->set_cursor (*cNormal);
177 bool PreviewWindow::on_button_press_event (GdkEventButton* event) {
179 if (!mainCropWin)
180 return true;
182 int x, y, w, h;
183 getObservedFrameArea (x, y, w, h);
184 bool inside = event->x > x-6 && event->x < x+w-1+6 && event->y > y-6 && event->y < y+h-1+6;
185 bool moreInside = event->x > x+6 && event->x < x+w-1-6 && event->y > y+6 && event->y < y+h-1-6;
187 if (!isMoving) {
188 isMoving = true;
189 if (!inside || moreInside) {
190 mainCropWin->remoteMove ((event->x - (x+w/2))/zoom, (event->y - (y+h/2))/zoom);
191 press_x = x+w/2;
192 press_y = y+h/2;
194 else {
195 press_x = event->x;
196 press_y = event->y;
198 get_window()->set_cursor (*cCropMoving);
202 bool PreviewWindow::on_button_release_event (GdkEventButton* event) {
204 if (!mainCropWin)
205 return true;
207 if (isMoving) {
208 isMoving = false;
209 get_window()->set_cursor (*cNormal);
210 mainCropWin->remoteMoveReady ();