Merge branch 'master' into blender2.8
[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 "BLI_utildefines.h"
33
34 #include "DNA_scene_types.h"
35 #include "DNA_userdef_types.h"
36
37 #include "BKE_context.h"
38 #include "BKE_image.h"
39 #include "BKE_global.h"
40 #include "BKE_main.h"
41 #include "BKE_screen.h"
42 #include "BKE_report.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "ED_screen.h"
48 #include "UI_interface.h"
49
50 #include "wm_window.h"
51
52 #include "render_intern.h"
53
54 /*********************** utilities for finding areas *************************/
55
56 /* returns biggest area that is not uv/image editor. Note that it uses buttons */
57 /* window as the last possible alternative.                                                                        */
58 /* would use BKE_screen_find_big_area(...) but this is too specific            */
59 static ScrArea *biggest_non_image_area(bContext *C)
60 {
61         bScreen *sc = CTX_wm_screen(C);
62         ScrArea *sa, *big = NULL;
63         int size, maxsize = 0, bwmaxsize = 0;
64         short foundwin = 0;
65
66         for (sa = sc->areabase.first; sa; sa = sa->next) {
67                 if (sa->winx > 30 && sa->winy > 30) {
68                         size = sa->winx * sa->winy;
69                         if (!sa->full && sa->spacetype == SPACE_BUTS) {
70                                 if (foundwin == 0 && size > bwmaxsize) {
71                                         bwmaxsize = size;
72                                         big = sa;
73                                 }
74                         }
75                         else if (sa->spacetype != SPACE_IMAGE && size > maxsize) {
76                                 maxsize = size;
77                                 big = sa;
78                                 foundwin = 1;
79                         }
80                 }
81         }
82
83         return big;
84 }
85
86 static ScrArea *find_area_showing_r_result(bContext *C, Scene *scene, wmWindow **win)
87 {
88         wmWindowManager *wm = CTX_wm_manager(C);
89         ScrArea *sa = NULL;
90         SpaceImage *sima;
91
92         /* find an imagewindow showing render result */
93         for (*win = wm->windows.first; *win; *win = (*win)->next) {
94                 if (WM_window_get_active_scene(*win) == scene) {
95                         const bScreen *screen = WM_window_get_active_screen(*win);
96
97                         for (sa = screen->areabase.first; sa; sa = sa->next) {
98                                 if (sa->spacetype == SPACE_IMAGE) {
99                                         sima = sa->spacedata.first;
100                                         if (sima->image && sima->image->type == IMA_TYPE_R_RESULT)
101                                                 break;
102                                 }
103                         }
104                         if (sa)
105                                 break;
106                 }
107         }
108
109         return sa;
110 }
111
112 static ScrArea *find_area_image_empty(bContext *C)
113 {
114         bScreen *sc = CTX_wm_screen(C);
115         ScrArea *sa;
116         SpaceImage *sima;
117
118         /* find an imagewindow showing render result */
119         for (sa = sc->areabase.first; sa; sa = sa->next) {
120                 if (sa->spacetype == SPACE_IMAGE) {
121                         sima = sa->spacedata.first;
122                         if (!sima->image)
123                                 break;
124                 }
125         }
126
127         return sa;
128 }
129
130 /********************** open image editor for render *************************/
131
132 /* new window uses x,y to set position */
133 ScrArea *render_view_open(bContext *C, int mx, int my, ReportList *reports)
134 {
135         Main *bmain = CTX_data_main(C);
136         Scene *scene = CTX_data_scene(C);
137         wmWindow *win = NULL;
138         ScrArea *sa = NULL;
139         SpaceImage *sima;
140         bool area_was_image = false;
141
142         if (scene->r.displaymode == R_OUTPUT_NONE)
143                 return NULL;
144
145         if (scene->r.displaymode == R_OUTPUT_WINDOW) {
146                 int sizex = 30 * UI_DPI_FAC + (scene->r.xsch * scene->r.size) / 100;
147                 int sizey = 60 * UI_DPI_FAC + (scene->r.ysch * scene->r.size) / 100;
148
149                 /* arbitrary... miniature image window views don't make much sense */
150                 if (sizex < 320) sizex = 320;
151                 if (sizey < 256) sizey = 256;
152
153                 /* changes context! */
154                 if (WM_window_open_temp(C, mx, my, sizex, sizey, WM_WINDOW_RENDER) == NULL) {
155                         BKE_report(reports, RPT_ERROR, "Failed to open window!");
156                         return NULL;
157                 }
158
159                 sa = CTX_wm_area(C);
160         }
161         else if (scene->r.displaymode == R_OUTPUT_SCREEN) {
162                 sa = CTX_wm_area(C);
163
164                 /* if the active screen is already in fullscreen mode, skip this and
165                  * unset the area, so that the fullscreen area is just changed later */
166                 if (sa && sa->full) {
167                         sa = NULL;
168                 }
169                 else {
170                         if (sa && sa->spacetype == SPACE_IMAGE)
171                                 area_was_image = true;
172
173                         /* this function returns with changed context */
174                         sa = ED_screen_full_newspace(C, sa, SPACE_IMAGE);
175                 }
176         }
177
178         if (!sa) {
179                 sa = find_area_showing_r_result(C, scene, &win);
180                 if (sa == NULL)
181                         sa = find_area_image_empty(C);
182
183                 /* if area found in other window, we make that one show in front */
184                 if (win && win != CTX_wm_window(C))
185                         wm_window_raise(win);
186
187                 if (sa == NULL) {
188                         /* find largest open non-image area */
189                         sa = biggest_non_image_area(C);
190                         if (sa) {
191                                 ED_area_newspace(C, sa, SPACE_IMAGE, true);
192                                 sima = sa->spacedata.first;
193
194                                 /* makes ESC go back to prev space */
195                                 sima->flag |= SI_PREVSPACE;
196
197                                 /* we already had a fullscreen here -> mark new space as a stacked fullscreen */
198                                 if (sa->full) {
199                                         sa->flag |= (AREA_FLAG_STACKED_FULLSCREEN | AREA_FLAG_TEMP_TYPE);
200                                 }
201                         }
202                         else {
203                                 /* use any area of decent size */
204                                 sa = BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TYPE_ANY, 0);
205                                 if (sa->spacetype != SPACE_IMAGE) {
206                                         // XXX newspace(sa, SPACE_IMAGE);
207                                         sima = sa->spacedata.first;
208
209                                         /* makes ESC go back to prev space */
210                                         sima->flag |= SI_PREVSPACE;
211                                 }
212                         }
213                 }
214         }
215         sima = sa->spacedata.first;
216
217         /* get the correct image, and scale it */
218         sima->image = BKE_image_verify_viewer(bmain, IMA_TYPE_R_RESULT, "Render Result");
219
220
221         /* if we're rendering to full screen, set appropriate hints on image editor
222          * so it can restore properly on pressing esc */
223         if (sa->full) {
224                 sima->flag |= SI_FULLWINDOW;
225
226                 /* Tell the image editor to revert to previous space in space list on close
227                  * _only_ if it wasn't already an image editor when the render was invoked */
228                 if (area_was_image == 0)
229                         sima->flag |= SI_PREVSPACE;
230                 else {
231                         /* Leave it alone so the image editor will just go back from
232                          * full screen to the original tiled setup */
233                 }
234         }
235
236         return sa;
237 }
238
239 /*************************** cancel render viewer **********************/
240
241 static int render_view_cancel_exec(bContext *C, wmOperator *UNUSED(op))
242 {
243         wmWindow *win = CTX_wm_window(C);
244         ScrArea *sa = CTX_wm_area(C);
245         SpaceImage *sima = sa->spacedata.first;
246
247         /* ensure image editor fullscreen and area fullscreen states are in sync */
248         if ((sima->flag & SI_FULLWINDOW) && !sa->full) {
249                 sima->flag &= ~SI_FULLWINDOW;
250         }
251
252         /* test if we have a temp screen in front */
253         if (WM_window_is_temp_screen(win)) {
254                 wm_window_lower(win);
255                 return OPERATOR_FINISHED;
256         }
257         /* determine if render already shows */
258         else if (sima->flag & SI_PREVSPACE) {
259                 sima->flag &= ~SI_PREVSPACE;
260
261                 if (sima->flag & SI_FULLWINDOW) {
262                         sima->flag &= ~SI_FULLWINDOW;
263                         ED_screen_full_prevspace(C, sa);
264                 }
265                 else {
266                         ED_area_prevspace(C, sa);
267                 }
268
269                 return OPERATOR_FINISHED;
270         }
271         else if (sima->flag & SI_FULLWINDOW) {
272                 sima->flag &= ~SI_FULLWINDOW;
273                 ED_screen_state_toggle(C, win, sa, SCREENMAXIMIZED);
274                 return OPERATOR_FINISHED;
275         }
276
277         return OPERATOR_PASS_THROUGH;
278 }
279
280 void RENDER_OT_view_cancel(struct wmOperatorType *ot)
281 {
282         /* identifiers */
283         ot->name = "Cancel Render View";
284         ot->description = "Cancel show render view";
285         ot->idname = "RENDER_OT_view_cancel";
286
287         /* api callbacks */
288         ot->exec = render_view_cancel_exec;
289         ot->poll = ED_operator_image_active;
290 }
291
292 /************************* show render viewer *****************/
293
294 static int render_view_show_invoke(bContext *C, wmOperator *op, const wmEvent *event)
295 {
296         wmWindow *wincur = CTX_wm_window(C);
297
298         /* test if we have currently a temp screen active */
299         if (WM_window_is_temp_screen(wincur)) {
300                 wm_window_lower(wincur);
301         }
302         else {
303                 wmWindow *win, *winshow;
304                 ScrArea *sa = find_area_showing_r_result(C, CTX_data_scene(C), &winshow);
305
306                 /* is there another window on current scene showing result? */
307                 for (win = CTX_wm_manager(C)->windows.first; win; win = win->next) {
308                         const bScreen *sc = WM_window_get_active_screen(win);
309
310                         if ((WM_window_is_temp_screen(win) && ((ScrArea *)sc->areabase.first)->spacetype == SPACE_IMAGE) ||
311                             (win == winshow && winshow != wincur))
312                         {
313                                 wm_window_raise(win);
314                                 return OPERATOR_FINISHED;
315                         }
316                 }
317
318                 /* determine if render already shows */
319                 if (sa) {
320                         /* but don't close it when rendering */
321                         if (G.is_rendering == false) {
322                                 SpaceImage *sima = sa->spacedata.first;
323
324                                 if (sima->flag & SI_PREVSPACE) {
325                                         sima->flag &= ~SI_PREVSPACE;
326
327                                         if (sima->flag & SI_FULLWINDOW) {
328                                                 sima->flag &= ~SI_FULLWINDOW;
329                                                 ED_screen_full_prevspace(C, sa);
330                                         }
331                                         else {
332                                                 ED_area_prevspace(C, sa);
333                                         }
334                                 }
335                         }
336                 }
337                 else {
338                         render_view_open(C, event->x, event->y, op->reports);
339                 }
340         }
341
342         return OPERATOR_FINISHED;
343 }
344
345 void RENDER_OT_view_show(struct wmOperatorType *ot)
346 {
347         /* identifiers */
348         ot->name = "Show/Hide Render View";
349         ot->description = "Toggle show render view";
350         ot->idname = "RENDER_OT_view_show";
351
352         /* api callbacks */
353         ot->invoke = render_view_show_invoke;
354         ot->poll = ED_operator_screenactive;
355 }
356