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