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