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