Cleanup: remove moar ugly G.main usages...
[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_main.h"
41 #include "BKE_screen.h"
42 #include "BKE_report.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "ED_screen.h"
48 #include "UI_interface.h"
49
50 #include "wm_window.h"
51
52 #include "render_intern.h"
53
54 /*********************** utilities for finding areas *************************/
55
56 /* returns biggest area that is not uv/image editor. Note that it uses buttons */
57 /* window as the last possible alternative.                                                                        */
58 /* would use BKE_screen_find_big_area(...) but this is too specific            */
59 static ScrArea *biggest_non_image_area(bContext *C)
60 {
61         bScreen *sc = CTX_wm_screen(C);
62         ScrArea *sa, *big = NULL;
63         int size, maxsize = 0, bwmaxsize = 0;
64         short foundwin = 0;
65
66         for (sa = sc->areabase.first; sa; sa = sa->next) {
67                 if (sa->winx > 30 && sa->winy > 30) {
68                         size = sa->winx * sa->winy;
69                         if (!sa->full && sa->spacetype == SPACE_BUTS) {
70                                 if (foundwin == 0 && size > bwmaxsize) {
71                                         bwmaxsize = size;
72                                         big = sa;
73                                 }
74                         }
75                         else if (sa->spacetype != SPACE_IMAGE && size > maxsize) {
76                                 maxsize = size;
77                                 big = sa;
78                                 foundwin = 1;
79                         }
80                 }
81         }
82
83         return big;
84 }
85
86 static ScrArea *find_area_showing_r_result(bContext *C, Scene *scene, wmWindow **win)
87 {
88         wmWindowManager *wm = CTX_wm_manager(C);
89         ScrArea *sa = NULL;
90         SpaceImage *sima;
91
92         /* find an imagewindow showing render result */
93         for (*win = wm->windows.first; *win; *win = (*win)->next) {
94                 if ((*win)->screen->scene == scene) {
95                         for (sa = (*win)->screen->areabase.first; sa; sa = sa->next) {
96                                 if (sa->spacetype == SPACE_IMAGE) {
97                                         sima = sa->spacedata.first;
98                                         if (sima->image && sima->image->type == IMA_TYPE_R_RESULT)
99                                                 break;
100                                 }
101                         }
102                         if (sa)
103                                 break;
104                 }
105         }
106
107         return sa;
108 }
109
110 static ScrArea *find_area_image_empty(bContext *C)
111 {
112         bScreen *sc = CTX_wm_screen(C);
113         ScrArea *sa;
114         SpaceImage *sima;
115
116         /* find an imagewindow showing render result */
117         for (sa = sc->areabase.first; sa; sa = sa->next) {
118                 if (sa->spacetype == SPACE_IMAGE) {
119                         sima = sa->spacedata.first;
120                         if (!sima->image)
121                                 break;
122                 }
123         }
124
125         return sa;
126 }
127
128 /********************** open image editor for render *************************/
129
130 /* new window uses x,y to set position */
131 ScrArea *render_view_open(bContext *C, int mx, int my, ReportList *reports)
132 {
133         Main *bmain = CTX_data_main(C);
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(bmain, 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 (win->screen->temp) {
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 (wincur->screen->temp) {
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                         bScreen *sc = win->screen;
307                         if ((sc->temp && ((ScrArea *)sc->areabase.first)->spacetype == SPACE_IMAGE) ||
308                             (win == winshow && winshow != wincur))
309                         {
310                                 wm_window_raise(win);
311                                 return OPERATOR_FINISHED;
312                         }
313                 }
314
315                 /* determine if render already shows */
316                 if (sa) {
317                         /* but don't close it when rendering */
318                         if (G.is_rendering == false) {
319                                 SpaceImage *sima = sa->spacedata.first;
320
321                                 if (sima->flag & SI_PREVSPACE) {
322                                         sima->flag &= ~SI_PREVSPACE;
323
324                                         if (sima->flag & SI_FULLWINDOW) {
325                                                 sima->flag &= ~SI_FULLWINDOW;
326                                                 ED_screen_full_prevspace(C, sa);
327                                         }
328                                         else {
329                                                 ED_area_prevspace(C, sa);
330                                         }
331                                 }
332                         }
333                 }
334                 else {
335                         render_view_open(C, event->x, event->y, op->reports);
336                 }
337         }
338
339         return OPERATOR_FINISHED;
340 }
341
342 void RENDER_OT_view_show(struct wmOperatorType *ot)
343 {
344         /* identifiers */
345         ot->name = "Show/Hide Render View";
346         ot->description = "Toggle show render view";
347         ot->idname = "RENDER_OT_view_show";
348
349         /* api callbacks */
350         ot->invoke = render_view_show_invoke;
351         ot->poll = ED_operator_screenactive;
352 }
353