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