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