2.5
[blender-staging.git] / source / blender / editors / space_image / image_panels.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2009
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #if 0
29
30 /* ************ panel stuff ************* */
31
32 /* this function gets the values for cursor and vertex number buttons */
33 static void image_transform_but_attr(int *imx, int *imy, int *step, int *digits) /*, float *xcoord, float *ycoord)*/
34 {
35         ImBuf *ibuf= imagewindow_get_ibuf(G.sima);
36         if(ibuf) {
37                 *imx= ibuf->x;
38                 *imy= ibuf->y;
39         }
40         
41         if (G.sima->flag & SI_COORDFLOATS) {
42                 *step= 1;
43                 *digits= 3;
44         }
45         else {
46                 *step= 100;
47                 *digits= 2;
48         }
49 }
50
51
52 /* is used for both read and write... */
53 void image_editvertex_buts(uiBlock *block)
54 {
55         static float ocent[2];
56         float cent[2]= {0.0, 0.0};
57         int imx= 256, imy= 256;
58         int nactive= 0, step, digits;
59         EditMesh *em = G.editMesh;
60         EditFace *efa;
61         MTFace *tf;
62         
63         if( is_uv_tface_editing_allowed_silent()==0 ) return;
64         
65         image_transform_but_attr(&imx, &imy, &step, &digits);
66         
67         for (efa= em->faces.first; efa; efa= efa->next) {
68                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
69                 if (simaFaceDraw_Check(efa, tf)) {
70                         
71                         if (simaUVSel_Check(efa, tf, 0)) {
72                                 cent[0]+= tf->uv[0][0];
73                                 cent[1]+= tf->uv[0][1];
74                                 nactive++;
75                         }
76                         if (simaUVSel_Check(efa, tf, 1)) {
77                                 cent[0]+= tf->uv[1][0];
78                                 cent[1]+= tf->uv[1][1];
79                                 nactive++;
80                         }
81                         if (simaUVSel_Check(efa, tf, 2)) {
82                                 cent[0]+= tf->uv[2][0];
83                                 cent[1]+= tf->uv[2][1];
84                                 nactive++;
85                         }
86                         if (efa->v4 && simaUVSel_Check(efa, tf, 3)) {
87                                 cent[0]+= tf->uv[3][0];
88                                 cent[1]+= tf->uv[3][1];
89                                 nactive++;
90                         }
91                 }
92         }
93                 
94         if(block) {     // do the buttons
95                 if (nactive) {
96                         ocent[0]= cent[0]/nactive;
97                         ocent[1]= cent[1]/nactive;
98                         if (G.sima->flag & SI_COORDFLOATS) {
99                         } else {
100                                 ocent[0] *= imx;
101                                 ocent[1] *= imy;
102                         }
103                         
104                         //uiBlockBeginAlign(block);
105                         if(nactive==1) {
106                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:",       10, 10, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
107                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:",       165, 10, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
108                         }
109                         else {
110                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:",       10, 10, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
111                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:",       165, 10, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
112                         }
113                         //uiBlockEndAlign(block);
114                 }
115         }
116         else {  // apply event
117                 float delta[2];
118                 
119                 cent[0]= cent[0]/nactive;
120                 cent[1]= cent[1]/nactive;
121                         
122                 if (G.sima->flag & SI_COORDFLOATS) {
123                         delta[0]= ocent[0]-cent[0];
124                         delta[1]= ocent[1]-cent[1];
125                 }
126                 else {
127                         delta[0]= ocent[0]/imx - cent[0];
128                         delta[1]= ocent[1]/imy - cent[1];
129                 }
130
131                 for (efa= em->faces.first; efa; efa= efa->next) {
132                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
133                         if (simaFaceDraw_Check(efa, tf)) {
134                                 if (simaUVSel_Check(efa, tf, 0)) {
135                                         tf->uv[0][0]+= delta[0];
136                                         tf->uv[0][1]+= delta[1];
137                                 }
138                                 if (simaUVSel_Check(efa, tf, 1)) {
139                                         tf->uv[1][0]+= delta[0];
140                                         tf->uv[1][1]+= delta[1];
141                                 }
142                                 if (simaUVSel_Check(efa, tf, 2)) {
143                                         tf->uv[2][0]+= delta[0];
144                                         tf->uv[2][1]+= delta[1];
145                                 }
146                                 if (efa->v4 && simaUVSel_Check(efa, tf, 3)) {
147                                         tf->uv[3][0]+= delta[0];
148                                         tf->uv[3][1]+= delta[1];
149                                 }
150                         }
151                 }
152                         
153                 allqueue(REDRAWVIEW3D, 0);
154                 allqueue(REDRAWIMAGE, 0);
155         }
156 }
157
158
159 /* is used for both read and write... */
160 void image_editcursor_buts(uiBlock *block)
161 {
162         static float ocent[2];
163         int imx= 256, imy= 256;
164         int step, digits;
165         
166         if( is_uv_tface_editing_allowed_silent()==0 ) return;
167         
168         image_transform_but_attr(&imx, &imy, &step, &digits);
169                 
170         if(block) {     // do the buttons
171                 ocent[0]= G.v2d->cursor[0];
172                 ocent[1]= G.v2d->cursor[1];
173                 if (G.sima->flag & SI_COORDFLOATS) {
174                 } else {
175                         ocent[0] *= imx;
176                         ocent[1] *= imy;
177                 }
178                 
179                 uiBlockBeginAlign(block);
180                 uiDefButF(block, NUM, B_CURSOR_IMAGE, "Cursor X:",      165, 120, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
181                 uiDefButF(block, NUM, B_CURSOR_IMAGE, "Cursor Y:",      165, 100, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
182                 uiBlockEndAlign(block);
183         }
184         else {  // apply event
185                 if (G.sima->flag & SI_COORDFLOATS) {
186                         G.v2d->cursor[0]= ocent[0];
187                         G.v2d->cursor[1]= ocent[1];
188                 }
189                 else {
190                         G.v2d->cursor[0]= ocent[0]/imx;
191                         G.v2d->cursor[1]= ocent[1]/imy;
192                 }
193                 allqueue(REDRAWIMAGE, 0);
194         }
195 }
196
197
198 void image_info(Image *ima, ImBuf *ibuf, char *str)
199 {
200         int ofs= 0;
201         
202         str[0]= 0;
203         
204         if(ima==NULL) return;
205         if(ibuf==NULL) {
206                 sprintf(str, "Can not get an image");
207                 return;
208         }
209         
210         if(ima->source==IMA_SRC_MOVIE) {
211                 ofs= sprintf(str, "Movie ");
212                 if(ima->anim) 
213                         ofs+= sprintf(str+ofs, "%d frs", IMB_anim_get_duration(ima->anim));
214         }
215         else
216                 ofs= sprintf(str, "Image ");
217
218         ofs+= sprintf(str+ofs, ": size %d x %d,", ibuf->x, ibuf->y);
219         
220         if(ibuf->rect_float) {
221                 if(ibuf->channels!=4) {
222                         sprintf(str+ofs, "%d float channel(s)", ibuf->channels);
223                 }
224                 else if(ibuf->depth==32)
225                         strcat(str, " RGBA float");
226                 else
227                         strcat(str, " RGB float");
228         }
229         else {
230                 if(ibuf->depth==32)
231                         strcat(str, " RGBA byte");
232                 else
233                         strcat(str, " RGB byte");
234         }
235         if(ibuf->zbuf || ibuf->zbuf_float)
236                 strcat(str, " + Z");
237         
238 }
239
240 static void image_panel_properties(short cntrl) // IMAGE_HANDLER_PROPERTIES
241 {
242         uiBlock *block;
243         
244         block= uiNewBlock(&curarea->uiblocks, "image_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
245         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
246         uiSetPanelHandler(IMAGE_HANDLER_PROPERTIES);  // for close and esc
247         if(uiNewPanel(curarea, block, "Image Properties", "Image", 10, 10, 318, 204)==0)
248                 return;
249         
250         /* note, it draws no bottom half in facemode, for vertex buttons */
251         uiblock_image_panel(block, &G.sima->image, &G.sima->iuser, B_REDR, B_REDR);
252         image_editvertex_buts(block);
253 }       
254
255 static void image_panel_game_properties(short cntrl)    // IMAGE_HANDLER_GAME_PROPERTIES
256 {
257         ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
258         uiBlock *block;
259
260         block= uiNewBlock(&curarea->uiblocks, "image_panel_game_properties", UI_EMBOSS, UI_HELV, curarea->win);
261         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
262         uiSetPanelHandler(IMAGE_HANDLER_GAME_PROPERTIES);  // for close and esc
263         if(uiNewPanel(curarea, block, "Real-time Properties", "Image", 10, 10, 318, 204)==0)
264                 return;
265
266         if (ibuf) {
267                 char str[128];
268                 
269                 image_info(G.sima->image, ibuf, str);
270                 uiDefBut(block, LABEL, B_NOP, str,              10,180,300,19, 0, 0, 0, 0, 0, "");
271
272                 uiBlockBeginAlign(block);
273                 uiDefButBitS(block, TOG, IMA_TWINANIM, B_TWINANIM, "Anim", 10,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of animated texture");
274                 uiDefButS(block, NUM, B_TWINANIM, "Start:",             10,130,140,19, &G.sima->image->twsta, 0.0, 128.0, 0, 0, "Displays the start frame of an animated texture");
275                 uiDefButS(block, NUM, B_TWINANIM, "End:",               10,110,140,19, &G.sima->image->twend, 0.0, 128.0, 0, 0, "Displays the end frame of an animated texture");
276                 uiDefButS(block, NUM, B_NOP, "Speed",                           10,90,140,19, &G.sima->image->animspeed, 1.0, 100.0, 0, 0, "Displays Speed of the animation in frames per second");
277                 uiBlockEndAlign(block);
278
279                 uiBlockBeginAlign(block);
280                 uiDefButBitS(block, TOG, IMA_TILES, B_SIMAGETILE, "Tiles",      160,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of tilemode for faces (Shift LMB to pick the tile for selected faces)");
281                 uiDefButS(block, NUM, B_SIMA_REDR_IMA_3D, "X:",         160,130,70,19, &G.sima->image->xrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the X direction");
282                 uiDefButS(block, NUM, B_SIMA_REDR_IMA_3D, "Y:",         230,130,70,19, &G.sima->image->yrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the Y direction");
283                 uiBlockBeginAlign(block);
284
285                 uiBlockBeginAlign(block);
286                 uiDefButBitS(block, TOG, IMA_CLAMP_U, B_SIMA3DVIEWDRAW, "ClampX",       160,100,70,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Disable texture repeating horizontaly");
287                 uiDefButBitS(block, TOG, IMA_CLAMP_V, B_SIMA3DVIEWDRAW, "ClampY",       230,100,70,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Disable texture repeating vertically");
288                 uiBlockEndAlign(block);
289         }
290 }
291
292 static void image_panel_view_properties(short cntrl)    // IMAGE_HANDLER_VIEW_PROPERTIES
293 {
294         uiBlock *block;
295
296         block= uiNewBlock(&curarea->uiblocks, "image_view_properties", UI_EMBOSS, UI_HELV, curarea->win);
297         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
298         uiSetPanelHandler(IMAGE_HANDLER_VIEW_PROPERTIES);  // for close and esc
299         if(uiNewPanel(curarea, block, "View Properties", "Image", 10, 10, 318, 204)==0)
300                 return;
301         
302         
303         uiDefButBitI(block, TOG, SI_DRAW_TILE, B_REDR, "Repeat Image",  10,160,140,19, &G.sima->flag, 0, 0, 0, 0, "Repeat/Tile the image display");
304         uiDefButBitI(block, TOG, SI_COORDFLOATS, B_REDR, "Normalized Coords",   165,160,145,19, &G.sima->flag, 0, 0, 0, 0, "Display coords from 0.0 to 1.0 rather then in pixels");
305         
306         
307         if (G.sima->image) {
308                 uiDefBut(block, LABEL, B_NOP, "Image Display:",         10,140,140,19, 0, 0, 0, 0, 0, "");
309                 uiBlockBeginAlign(block);
310                 uiDefButF(block, NUM, B_REDR, "AspX:", 10,120,140,19, &G.sima->image->aspx, 0.1, 5000.0, 100, 0, "X Display Aspect for this image, does not affect renderingm 0 disables.");
311                 uiDefButF(block, NUM, B_REDR, "AspY:", 10,100,140,19, &G.sima->image->aspy, 0.1, 5000.0, 100, 0, "X Display Aspect for this image, does not affect rendering 0 disables.");
312                 uiBlockEndAlign(block);
313         }
314         
315         
316         if (EM_texFaceCheck()) {
317                 uiDefBut(block, LABEL, B_NOP, "Draw Type:",             10, 80,120,19, 0, 0, 0, 0, 0, "");
318                 uiBlockBeginAlign(block);
319                 uiDefButC(block,  ROW, B_REDR, "Outline",               10,60,58,19, &G.sima->dt_uv, 0.0, SI_UVDT_OUTLINE, 0, 0, "Outline Wire UV drawtype");
320                 uiDefButC(block,  ROW, B_REDR, "Dash",                  68, 60,58,19, &G.sima->dt_uv, 0.0, SI_UVDT_DASH, 0, 0, "Dashed Wire UV drawtype");
321                 uiDefButC(block,  ROW, B_REDR, "Black",                 126, 60,58,19, &G.sima->dt_uv, 0.0, SI_UVDT_BLACK, 0, 0, "Black Wire UV drawtype");
322                 uiDefButC(block,  ROW, B_REDR, "White",                 184,60,58,19, &G.sima->dt_uv, 0.0, SI_UVDT_WHITE, 0, 0, "White Wire UV drawtype");
323                 
324                 uiBlockEndAlign(block);
325                 uiDefButBitI(block, TOG, SI_SMOOTH_UV, B_REDR, "Smooth",        250,60,60,19,  &G.sima->flag, 0, 0, 0, 0, "Display smooth lines in the UV view");
326                 
327                 
328                 uiDefButBitI(block, TOG, G_DRAWFACES, B_REDR, "Faces",          10,30,60,19,  &G.f, 0, 0, 0, 0, "Displays all faces as shades in the 3d view and UV editor");
329                 uiDefButBitI(block, TOG, G_DRAWEDGES, B_REDR, "Edges", 70, 30,60,19, &G.f, 0, 0, 0, 0, "Displays selected edges using hilights in the 3d view and UV editor");
330                 
331                 uiDefButBitI(block, TOG, SI_DRAWSHADOW, B_REDR, "Final Shadow", 130, 30,110,19, &G.sima->flag, 0, 0, 0, 0, "Draw the final result from the objects modifiers");
332                 
333                 uiDefButBitI(block, TOG, SI_DRAW_STRETCH, B_REDR, "UV Stretch", 10,0,100,19,  &G.sima->flag, 0, 0, 0, 0, "Difference between UV's and the 3D coords (blue for low distortion, red is high)");
334                 if (G.sima->flag & SI_DRAW_STRETCH) {
335                         uiBlockBeginAlign(block);
336                         uiDefButC(block,  ROW, B_REDR, "Area",                  120,0,60,19, &G.sima->dt_uvstretch, 0.0, SI_UVDT_STRETCH_AREA, 0, 0, "Area distortion between UV's and 3D coords");
337                         uiDefButC(block,  ROW, B_REDR, "Angle",         180,0,60,19, &G.sima->dt_uvstretch, 0.0, SI_UVDT_STRETCH_ANGLE, 0, 0, "Angle distortion between UV's and 3D coords");
338                         uiBlockEndAlign(block);
339                 }
340                 
341         }
342         image_editcursor_buts(block);
343 }
344
345 static void image_panel_paint(short cntrl)      // IMAGE_HANDLER_PAINT
346 {
347         uiBlock *block;
348         if ((G.sima->image && (G.sima->flag & SI_DRAWTOOL))==0) {
349                 return;
350         }
351         
352         block= uiNewBlock(&curarea->uiblocks, "image_panel_paint", UI_EMBOSS, UI_HELV, curarea->win);
353         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
354         uiSetPanelHandler(IMAGE_HANDLER_PAINT);  // for close and esc
355         if(uiNewPanel(curarea, block, "Image Paint", "Image", 10, 230, 318, 204)==0)
356                 return;
357         
358         brush_buttons(block, 1, B_SIMANOTHING, B_SIMABRUSHCHANGE, B_SIMABRUSHBROWSE, B_SIMABRUSHLOCAL, B_SIMABRUSHDELETE, B_KEEPDATA, B_SIMABTEXBROWSE, B_SIMABTEXDELETE);
359 }
360
361 static void image_panel_curves_reset(void *cumap_v, void *ibuf_v)
362 {
363         CurveMapping *cumap = cumap_v;
364         int a;
365         
366         for(a=0; a<CM_TOT; a++)
367                 curvemap_reset(cumap->cm+a, &cumap->clipr);
368         
369         cumap->black[0]=cumap->black[1]=cumap->black[2]= 0.0f;
370         cumap->white[0]=cumap->white[1]=cumap->white[2]= 1.0f;
371         curvemapping_set_black_white(cumap, NULL, NULL);
372         
373         curvemapping_changed(cumap, 0);
374         curvemapping_do_ibuf(cumap, ibuf_v);
375         
376         allqueue(REDRAWIMAGE, 0);
377 }
378
379
380 static void image_panel_curves(short cntrl)     // IMAGE_HANDLER_CURVES
381 {
382         ImBuf *ibuf;
383         uiBlock *block;
384         uiBut *bt;
385         
386         /* and we check for spare */
387         ibuf= imagewindow_get_ibuf(G.sima);
388         
389         block= uiNewBlock(&curarea->uiblocks, "image_panel_curves", UI_EMBOSS, UI_HELV, curarea->win);
390         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
391         uiSetPanelHandler(IMAGE_HANDLER_CURVES);  // for close and esc
392         if(uiNewPanel(curarea, block, "Curves", "Image", 10, 450, 318, 204)==0)
393                 return;
394         
395         if (ibuf) {
396                 rctf rect;
397                 
398                 if(G.sima->cumap==NULL)
399                         G.sima->cumap= curvemapping_add(4, 0.0f, 0.0f, 1.0f, 1.0f);
400                 
401                 rect.xmin= 110; rect.xmax= 310;
402                 rect.ymin= 10; rect.ymax= 200;
403                 curvemap_buttons(block, G.sima->cumap, 'c', B_SIMACURVES, B_REDR, &rect);
404                 
405                 /* curvemap min/max only works for RGBA */
406                 if(ibuf->channels==4) {
407                         bt=uiDefBut(block, BUT, B_SIMARANGE, "Reset",   10, 160, 90, 19, NULL, 0.0f, 0.0f, 0, 0, "Reset Black/White point and curves");
408                         uiButSetFunc(bt, image_panel_curves_reset, G.sima->cumap, ibuf);
409                 
410                         uiBlockBeginAlign(block);
411                         uiDefButF(block, NUM, B_SIMARANGE, "Min R:",    10, 120, 90, 19, G.sima->cumap->black, -1000.0f, 1000.0f, 10, 2, "Black level");
412                         uiDefButF(block, NUM, B_SIMARANGE, "Min G:",    10, 100, 90, 19, G.sima->cumap->black+1, -1000.0f, 1000.0f, 10, 2, "Black level");
413                         uiDefButF(block, NUM, B_SIMARANGE, "Min B:",    10, 80, 90, 19, G.sima->cumap->black+2, -1000.0f, 1000.0f, 10, 2, "Black level");
414                         
415                         uiBlockBeginAlign(block);
416                         uiDefButF(block, NUM, B_SIMARANGE, "Max R:",    10, 50, 90, 19, G.sima->cumap->white, -1000.0f, 1000.0f, 10, 2, "White level");
417                         uiDefButF(block, NUM, B_SIMARANGE, "Max G:",    10, 30, 90, 19, G.sima->cumap->white+1, -1000.0f, 1000.0f, 10, 2, "White level");
418                         uiDefButF(block, NUM, B_SIMARANGE, "Max B:",    10, 10, 90, 19, G.sima->cumap->white+2, -1000.0f, 1000.0f, 10, 2, "White level");
419                 }
420         }
421 }
422 /* 0: disable preview 
423    otherwise refresh preview
424 */
425 void image_preview_event(int event)
426 {
427         int exec= 0;
428         
429
430         if(event==0) {
431                 G.scene->r.scemode &= ~R_COMP_CROP;
432                 exec= 1;
433         }
434         else {
435                 if(image_preview_active(curarea, NULL, NULL)) {
436                         G.scene->r.scemode |= R_COMP_CROP;
437                         exec= 1;
438                 }
439                 else
440                         G.scene->r.scemode &= ~R_COMP_CROP;
441         }
442         
443         if(exec && G.scene->nodetree) {
444                 /* should work when no node editor in screen..., so we execute right away */
445                 
446                 ntreeCompositTagGenerators(G.scene->nodetree);
447
448                 G.afbreek= 0;
449                 G.scene->nodetree->timecursor= set_timecursor;
450                 G.scene->nodetree->test_break= blender_test_break;
451                 
452                 BIF_store_spare();
453                 
454                 ntreeCompositExecTree(G.scene->nodetree, &G.scene->r, 1);       /* 1 is do_previews */
455                 
456                 G.scene->nodetree->timecursor= NULL;
457                 G.scene->nodetree->test_break= NULL;
458                 
459                 scrarea_do_windraw(curarea);
460                 waitcursor(0);
461                 
462                 allqueue(REDRAWNODE, 1);
463         }       
464 }
465
466
467 /* nothing drawn here, we use it to store values */
468 static void preview_cb(struct ScrArea *sa, struct uiBlock *block)
469 {
470         rctf dispf;
471         rcti *disprect= &G.scene->r.disprect;
472         int winx= (G.scene->r.size*G.scene->r.xsch)/100;
473         int winy= (G.scene->r.size*G.scene->r.ysch)/100;
474         short mval[2];
475         
476         if(G.scene->r.mode & R_BORDER) {
477                 winx*= (G.scene->r.border.xmax - G.scene->r.border.xmin);
478                 winy*= (G.scene->r.border.ymax - G.scene->r.border.ymin);
479         }
480         
481         /* while dragging we need to update the rects, otherwise it doesn't end with correct one */
482
483         BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
484         ui_graphics_to_window_rct(sa->win, &dispf, disprect);
485         
486         /* correction for gla draw */
487         BLI_translate_rcti(disprect, -curarea->winrct.xmin, -curarea->winrct.ymin);
488         
489         calc_image_view(G.sima, 'p');
490 //      printf("winrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
491         /* map to image space coordinates */
492         mval[0]= disprect->xmin; mval[1]= disprect->ymin;
493         areamouseco_to_ipoco(G.v2d, mval, &dispf.xmin, &dispf.ymin);
494         mval[0]= disprect->xmax; mval[1]= disprect->ymax;
495         areamouseco_to_ipoco(G.v2d, mval, &dispf.xmax, &dispf.ymax);
496         
497         /* map to render coordinates */
498         disprect->xmin= dispf.xmin;
499         disprect->xmax= dispf.xmax;
500         disprect->ymin= dispf.ymin;
501         disprect->ymax= dispf.ymax;
502         
503         CLAMP(disprect->xmin, 0, winx);
504         CLAMP(disprect->xmax, 0, winx);
505         CLAMP(disprect->ymin, 0, winy);
506         CLAMP(disprect->ymax, 0, winy);
507 //      printf("drawrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
508
509 }
510
511 static int is_preview_allowed(ScrArea *cur)
512 {
513         SpaceImage *sima= cur->spacedata.first;
514         ScrArea *sa;
515
516         /* check if another areawindow has preview set */
517         for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
518                 if(sa!=cur && sa->spacetype==SPACE_IMAGE) {
519                         if(image_preview_active(sa, NULL, NULL))
520                            return 0;
521                 }
522         }
523         /* check image type */
524         if(sima->image==NULL || sima->image->type!=IMA_TYPE_COMPOSITE)
525                 return 0;
526         
527         return 1;
528 }
529
530 static void image_panel_preview(ScrArea *sa, short cntrl)       // IMAGE_HANDLER_PREVIEW
531 {
532         uiBlock *block;
533         SpaceImage *sima= sa->spacedata.first;
534         int ofsx, ofsy;
535         
536         if(is_preview_allowed(sa)==0) {
537                 rem_blockhandler(sa, IMAGE_HANDLER_PREVIEW);
538                 G.scene->r.scemode &= ~R_COMP_CROP;     /* quite weak */
539                 return;
540         }
541         
542         block= uiNewBlock(&sa->uiblocks, "image_panel_preview", UI_EMBOSS, UI_HELV, sa->win);
543         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
544         uiSetPanelHandler(IMAGE_HANDLER_PREVIEW);  // for close and esc
545         
546         ofsx= -150+(sa->winx/2)/sima->blockscale;
547         ofsy= -100+(sa->winy/2)/sima->blockscale;
548         if(uiNewPanel(sa, block, "Preview", "Image", ofsx, ofsy, 300, 200)==0) return;
549         
550         uiBlockSetDrawExtraFunc(block, preview_cb);
551         
552 }
553
554 static void image_panel_gpencil(short cntrl)    // IMAGE_HANDLER_GREASEPENCIL
555 {
556         uiBlock *block;
557         SpaceImage *sima;
558         
559         sima= curarea->spacedata.first;
560
561         block= uiNewBlock(&curarea->uiblocks, "image_panel_gpencil", UI_EMBOSS, UI_HELV, curarea->win);
562         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
563         uiSetPanelHandler(IMAGE_HANDLER_GREASEPENCIL);  // for close and esc
564         if (uiNewPanel(curarea, block, "Grease Pencil", "SpaceImage", 100, 30, 318, 204)==0) return;
565         
566         /* allocate memory for gpd if drawing enabled (this must be done first or else we crash) */
567         if (sima->flag & SI_DISPGP) {
568                 if (sima->gpd == NULL)
569                         gpencil_data_setactive(curarea, gpencil_data_addnew());
570         }
571         
572         if (sima->flag & SI_DISPGP) {
573                 bGPdata *gpd= sima->gpd;
574                 short newheight;
575                 
576                 /* this is a variable height panel, newpanel doesnt force new size on existing panels */
577                 /* so first we make it default height */
578                 uiNewPanelHeight(block, 204);
579                 
580                 /* draw button for showing gpencil settings and drawings */
581                 uiDefButBitI(block, TOG, SI_DISPGP, B_REDR, "Use Grease Pencil", 10, 225, 150, 20, &sima->flag, 0, 0, 0, 0, "Display freehand annotations overlay over this Image/UV Editor (draw using Shift-LMB)");
582                 
583                 /* extend the panel if the contents won't fit */
584                 newheight= draw_gpencil_panel(block, gpd, curarea); 
585                 uiNewPanelHeight(block, newheight);
586         }
587         else {
588                 uiDefButBitI(block, TOG, SI_DISPGP, B_REDR, "Use Grease Pencil", 10, 225, 150, 20, &sima->flag, 0, 0, 0, 0, "Display freehand annotations overlay over this Image/UV Editor");
589                 uiDefBut(block, LABEL, 1, " ",  160, 180, 150, 20, NULL, 0.0, 0.0, 0, 0, "");
590         }
591 }
592
593 static void image_blockhandlers(ScrArea *sa)
594 {
595         SpaceImage *sima= sa->spacedata.first;
596         short a;
597
598         /* warning; blocks need to be freed each time, handlers dont remove  */
599         uiFreeBlocksWin(&sa->uiblocks, sa->win);
600         
601         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
602                 switch(sima->blockhandler[a]) {
603                 case IMAGE_HANDLER_PROPERTIES:
604                         image_panel_properties(sima->blockhandler[a+1]);
605                         break;
606                 case IMAGE_HANDLER_GAME_PROPERTIES:
607                         image_panel_game_properties(sima->blockhandler[a+1]);
608                         break;
609                 case IMAGE_HANDLER_VIEW_PROPERTIES:
610                         image_panel_view_properties(sima->blockhandler[a+1]);
611                         break;
612                 case IMAGE_HANDLER_PAINT:
613                         image_panel_paint(sima->blockhandler[a+1]);
614                         break;          
615                 case IMAGE_HANDLER_CURVES:
616                         image_panel_curves(sima->blockhandler[a+1]);
617                         break;          
618                 case IMAGE_HANDLER_PREVIEW:
619                         image_panel_preview(sa, sima->blockhandler[a+1]);
620                         break;  
621                 case IMAGE_HANDLER_GREASEPENCIL:
622                         image_panel_gpencil(sima->blockhandler[a+1]);
623                         break;
624                 }
625                 /* clear action value for event */
626                 sima->blockhandler[a+1]= 0;
627         }
628         uiDrawBlocksPanels(sa, 0);
629 }
630 #endif
631