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