1103f956f7816c4094e793f0f45ac7dd3a0acb99
[blender.git] / source / blender / editors / render / render_view.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/render/render_view.c
26  *  \ingroup edrend
27  */
28
29 #include <string.h>
30 #include <stddef.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_utildefines.h"
36
37 #include "DNA_scene_types.h"
38 #include "DNA_userdef_types.h"
39
40 #include "BKE_blender.h"
41 #include "BKE_context.h"
42 #include "BKE_image.h"
43 #include "BKE_global.h"
44 #include "BKE_main.h"
45 #include "BKE_node.h"
46 #include "BKE_report.h"
47 #include "BKE_screen.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "ED_screen.h"
53
54 #include "wm_window.h"
55
56 #include "render_intern.h"
57
58 /*********************** utilities for finding areas *************************/
59
60 /* returns biggest area that is not uv/image editor. Note that it uses buttons */
61 /* window as the last possible alternative.                                                                        */
62 /* would use BKE_screen_find_big_area(...) but this is too specific            */
63 static ScrArea *biggest_non_image_area(bContext *C)
64 {
65         bScreen *sc = CTX_wm_screen(C);
66         ScrArea *sa, *big = NULL;
67         int size, maxsize = 0, bwmaxsize = 0;
68         short foundwin = 0;
69
70         for (sa = sc->areabase.first; sa; sa = sa->next) {
71                 if (sa->winx > 30 && sa->winy > 30) {
72                         size = sa->winx * sa->winy;
73                         if (sa->spacetype == SPACE_BUTS) {
74                                 if (foundwin == 0 && size > bwmaxsize) {
75                                         bwmaxsize = size;
76                                         big = sa;
77                                 }
78                         }
79                         else if (sa->spacetype != SPACE_IMAGE && size > maxsize) {
80                                 maxsize = size;
81                                 big = sa;
82                                 foundwin = 1;
83                         }
84                 }
85         }
86
87         return big;
88 }
89
90 static ScrArea *find_area_showing_r_result(bContext *C, Scene *scene, wmWindow **win)
91 {
92         wmWindowManager *wm = CTX_wm_manager(C);
93         ScrArea *sa = NULL;
94         SpaceImage *sima;
95
96         /* find an imagewindow showing render result */
97         for (*win = wm->windows.first; *win; *win = (*win)->next) {
98                 if ((*win)->screen->scene == scene) {
99                         for (sa = (*win)->screen->areabase.first; sa; sa = sa->next) {
100                                 if (sa->spacetype == SPACE_IMAGE) {
101                                         sima = sa->spacedata.first;
102                                         if (sima->image && sima->image->type == IMA_TYPE_R_RESULT)
103                                                 break;
104                                 }
105                         }
106                         if (sa)
107                                 break;
108                 }
109         }
110         
111         return sa;
112 }
113
114 static ScrArea *find_area_image_empty(bContext *C)
115 {
116         bScreen *sc = CTX_wm_screen(C);
117         ScrArea *sa;
118         SpaceImage *sima;
119
120         /* find an imagewindow showing render result */
121         for (sa = sc->areabase.first; sa; sa = sa->next) {
122                 if (sa->spacetype == SPACE_IMAGE) {
123                         sima = sa->spacedata.first;
124                         if (!sima->image)
125                                 break;
126                 }
127         }
128
129         return sa;
130 }
131
132 /********************** open image editor for render *************************/
133
134 /* new window uses x,y to set position */
135 void render_view_open(bContext *C, int mx, int my)
136 {
137         wmWindow *win = CTX_wm_window(C);
138         Scene *scene = CTX_data_scene(C);
139         ScrArea *sa = NULL;
140         SpaceImage *sima;
141         int area_was_image = 0;
142
143         if (scene->r.displaymode == R_OUTPUT_NONE)
144                 return;
145         
146         if (scene->r.displaymode == R_OUTPUT_WINDOW) {
147                 rcti rect;
148                 int sizex, sizey;
149
150                 sizex = 10 + (scene->r.xsch * scene->r.size) / 100;
151                 sizey = 40 + (scene->r.ysch * scene->r.size) / 100;
152
153                 /* arbitrary... miniature image window views don't make much sense */
154                 if (sizex < 320) sizex = 320;
155                 if (sizey < 256) sizey = 256;
156
157                 /* some magic to calculate postition */
158                 /* pixelsize: mouse coords are in U.pixelsize units :/ */
159                 rect.xmin = (mx / U.pixelsize) + win->posx - sizex / 2;
160                 rect.ymin = (my / U.pixelsize) + win->posy - sizey / 2;
161                 rect.xmax = rect.xmin + sizex;
162                 rect.ymax = rect.ymin + sizey;
163
164                 /* changes context! */
165                 WM_window_open_temp(C, &rect, WM_WINDOW_RENDER);
166
167                 sa = CTX_wm_area(C);
168         }
169         else if (scene->r.displaymode == R_OUTPUT_SCREEN) {
170                 if (CTX_wm_area(C) && CTX_wm_area(C)->spacetype == SPACE_IMAGE)
171                         area_was_image = 1;
172
173                 /* this function returns with changed context */
174                 sa = ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_IMAGE);
175         }
176
177         if (!sa) {
178                 sa = find_area_showing_r_result(C, scene, &win);
179                 if (sa == NULL)
180                         sa = find_area_image_empty(C);
181                 
182                 /* if area found in other window, we make that one show in front */
183                 if (win && win != CTX_wm_window(C))
184                         wm_window_raise(win);
185
186                 if (sa == NULL) {
187                         /* find largest open non-image area */
188                         sa = biggest_non_image_area(C);
189                         if (sa) {
190                                 ED_area_newspace(C, sa, SPACE_IMAGE);
191                                 sima = sa->spacedata.first;
192
193                                 /* makes ESC go back to prev space */
194                                 sima->flag |= SI_PREVSPACE;
195                         }
196                         else {
197                                 /* use any area of decent size */
198                                 sa = BKE_screen_find_big_area(CTX_wm_screen(C), -1, 0);
199                                 if (sa->spacetype != SPACE_IMAGE) {
200                                         // XXX newspace(sa, SPACE_IMAGE);
201                                         sima = sa->spacedata.first;
202
203                                         /* makes ESC go back to prev space */
204                                         sima->flag |= SI_PREVSPACE;
205                                 }
206                         }
207                 }
208         }
209         sima = sa->spacedata.first;
210
211         /* get the correct image, and scale it */
212         sima->image = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
213
214
215         /* if we're rendering to full screen, set appropriate hints on image editor
216          * so it can restore properly on pressing esc */
217         if (sa->full) {
218                 sima->flag |= SI_FULLWINDOW;
219
220                 /* Tell the image editor to revert to previous space in space list on close
221                  * _only_ if it wasn't already an image editor when the render was invoked */
222                 if (area_was_image == 0)
223                         sima->flag |= SI_PREVSPACE;
224                 else {
225                         /* Leave it alone so the image editor will just go back from
226                          * full screen to the original tiled setup */
227                 }
228         }
229 }
230
231 /*************************** cancel render viewer **********************/
232
233 static int render_view_cancel_exec(bContext *C, wmOperator *UNUSED(op))
234 {
235         wmWindow *win = CTX_wm_window(C);
236         ScrArea *sa = CTX_wm_area(C);
237         SpaceImage *sima = sa->spacedata.first;
238
239         /* test if we have a temp screen in front */
240         if (CTX_wm_window(C)->screen->temp) {
241                 wm_window_lower(CTX_wm_window(C));
242                 return OPERATOR_FINISHED;
243         }
244         /* determine if render already shows */
245         else if (sima->flag & SI_PREVSPACE) {
246                 sima->flag &= ~SI_PREVSPACE;
247
248                 if (sima->flag & SI_FULLWINDOW) {
249                         sima->flag &= ~SI_FULLWINDOW;
250                         ED_screen_full_prevspace(C, sa);
251                 }
252                 else
253                         ED_area_prevspace(C, sa);
254
255                 return OPERATOR_FINISHED;
256         }
257         else if (sima->flag & SI_FULLWINDOW) {
258                 sima->flag &= ~SI_FULLWINDOW;
259                 ED_screen_full_toggle(C, win, sa);
260                 return OPERATOR_FINISHED;
261         }
262
263         return OPERATOR_PASS_THROUGH;
264 }
265
266 void RENDER_OT_view_cancel(struct wmOperatorType *ot)
267 {
268         /* identifiers */
269         ot->name = "Cancel Render View";
270         ot->description = "Cancel show render view";
271         ot->idname = "RENDER_OT_view_cancel";
272
273         /* api callbacks */
274         ot->exec = render_view_cancel_exec;
275         ot->poll = ED_operator_image_active;
276 }
277
278 /************************* show render viewer *****************/
279
280 static int render_view_show_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
281 {
282         wmWindow *wincur = CTX_wm_window(C);
283         
284         /* test if we have currently a temp screen active */
285         if (wincur->screen->temp) {
286                 wm_window_lower(wincur);
287         }
288         else {
289                 wmWindow *win, *winshow;
290                 ScrArea *sa = find_area_showing_r_result(C, CTX_data_scene(C), &winshow);
291                 
292                 /* is there another window on current scene showing result? */
293                 for (win = CTX_wm_manager(C)->windows.first; win; win = win->next) {
294                         bScreen *sc = win->screen;
295                         if ((sc->temp && ((ScrArea *)sc->areabase.first)->spacetype == SPACE_IMAGE) ||
296                             (win == winshow && winshow != wincur))
297                         {
298                                 wm_window_raise(win);
299                                 return OPERATOR_FINISHED;
300                         }
301                 }
302                 
303                 /* determine if render already shows */
304                 if (sa) {
305                         /* but don't close it when rendering */
306                         if (G.is_rendering == FALSE) {
307                                 SpaceImage *sima = sa->spacedata.first;
308
309                                 if (sima->flag & SI_PREVSPACE) {
310                                         sima->flag &= ~SI_PREVSPACE;
311
312                                         if (sima->flag & SI_FULLWINDOW) {
313                                                 sima->flag &= ~SI_FULLWINDOW;
314                                                 ED_screen_full_prevspace(C, sa);
315                                         }
316                                         else if (sima->next) {
317                                                 /* workaround for case of double prevspace, render window
318                                                  * with a file browser on top of it (same as in ED_area_prevspace) */
319                                                 if (sima->next->spacetype == SPACE_FILE && sima->next->next)
320                                                         ED_area_newspace(C, sa, sima->next->next->spacetype);
321                                                 else
322                                                         ED_area_newspace(C, sa, sima->next->spacetype);
323                                                 ED_area_tag_redraw(sa);
324                                         }
325                                 }
326                         }
327                 }
328                 else {
329                         render_view_open(C, event->x, event->y);
330                 }
331         }
332
333         return OPERATOR_FINISHED;
334 }
335
336 void RENDER_OT_view_show(struct wmOperatorType *ot)
337 {
338         /* identifiers */
339         ot->name = "Show/Hide Render View";
340         ot->description = "Toggle show render view";
341         ot->idname = "RENDER_OT_view_show";
342
343         /* api callbacks */
344         ot->invoke = render_view_show_invoke;
345         ot->poll = ED_operator_screenactive;
346 }
347