NLA SoC: Merge from 2.5
[blender.git] / source / blender / editors / space_image / image_buttons.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
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_color_types.h"
33 #include "DNA_image_types.h"
34 #include "DNA_mesh_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_packedFile_types.h"
38 #include "DNA_node_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_userdef_types.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_arithb.h"
48 #include "BLI_editVert.h"
49 #include "BLI_rand.h"
50
51 #include "BKE_colortools.h"
52 #include "BKE_context.h"
53 #include "BKE_customdata.h"
54 #include "BKE_image.h"
55 #include "BKE_global.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_mesh.h"
59 #include "BKE_node.h"
60 #include "BKE_packedFile.h"
61 #include "BKE_screen.h"
62 #include "BKE_utildefines.h"
63
64 #include "RE_pipeline.h"
65
66 #include "IMB_imbuf.h"
67 #include "IMB_imbuf_types.h"
68
69 #include "ED_image.h"
70 #include "ED_mesh.h"
71 #include "ED_space_api.h"
72 #include "ED_screen.h"
73 #include "ED_uvedit.h"
74 #include "ED_util.h"
75
76 #include "BIF_gl.h"
77 #include "BIF_glutil.h"
78
79 #include "RNA_access.h"
80
81 #include "WM_api.h"
82 #include "WM_types.h"
83
84 #include "UI_interface.h"
85 #include "UI_resources.h"
86 #include "UI_view2d.h"
87
88 #include "image_intern.h"
89
90 #define B_REDR                          1
91 #define B_IMAGECHANGED          2
92 #define B_TRANS_IMAGE           3
93 #define B_CURSOR_IMAGE          4
94 #define B_NOP                           0
95 #define B_TWINANIM                      5
96 #define B_SIMAGETILE            6
97 #define B_IDNAME                        10
98 #define B_FACESEL_PAINT_TEST    11
99 #define B_SIMA_RECORD           12
100 #define B_SIMA_PLAY                     13
101 #define B_SIMARANGE                     14
102 #define B_SIMACURVES            15
103
104 #define B_SIMANOTHING           16
105 #define B_SIMABRUSHCHANGE       17      
106 #define B_SIMABRUSHBROWSE       18
107 #define B_SIMABRUSHLOCAL        19
108 #define B_SIMABRUSHDELETE       20
109 #define B_KEEPDATA                      21
110 #define B_SIMABTEXBROWSE        22
111 #define B_SIMABTEXDELETE        23
112 #define B_VPCOLSLI                      24
113 #define B_SIMACLONEBROWSE       25
114 #define B_SIMACLONEDELETE       26
115
116 /* XXX */
117 static int okee() {return 0;}
118 static int simaFaceDraw_Check() {return 0;}
119 static int simaUVSel_Check() {return 0;}
120 static int is_uv_tface_editing_allowed_silent() {return 0;}
121 /* XXX */
122
123 /* proto */
124 static void image_editvertex_buts(const bContext *C, uiBlock *block);
125 static void image_editcursor_buts(const bContext *C, View2D *v2d, uiBlock *block);
126
127
128 static void do_image_panel_events(bContext *C, void *arg, int event)
129 {
130         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
131         ARegion *ar= CTX_wm_region(C);
132         
133         switch(event) {
134                 case B_REDR:
135                         break;
136                 case B_SIMACURVES:
137                         curvemapping_do_ibuf(sima->cumap, ED_space_image_buffer(sima));
138                         break;
139                 case B_SIMARANGE:
140                         curvemapping_set_black_white(sima->cumap, NULL, NULL);
141                         curvemapping_do_ibuf(sima->cumap, ED_space_image_buffer(sima));
142                         break;
143                 case B_TRANS_IMAGE:
144                         image_editvertex_buts(C, NULL);
145                         break;
146                 case B_CURSOR_IMAGE:
147                         image_editcursor_buts(C, &ar->v2d, NULL);
148                         break;
149         }
150         /* all events now */
151         WM_event_add_notifier(C, NC_IMAGE, sima->image);
152 }
153
154
155
156 static void image_info(Image *ima, ImBuf *ibuf, char *str)
157 {
158         int ofs= 0;
159         
160         str[0]= 0;
161         
162         if(ima==NULL) return;
163         if(ibuf==NULL) {
164                 sprintf(str, "Can not get an image");
165                 return;
166         }
167         
168         if(ima->source==IMA_SRC_MOVIE) {
169                 ofs= sprintf(str, "Movie ");
170                 if(ima->anim) 
171                         ofs+= sprintf(str+ofs, "%d frs", IMB_anim_get_duration(ima->anim));
172         }
173         else
174                 ofs= sprintf(str, "Image ");
175         
176         ofs+= sprintf(str+ofs, ": size %d x %d,", ibuf->x, ibuf->y);
177         
178         if(ibuf->rect_float) {
179                 if(ibuf->channels!=4) {
180                         sprintf(str+ofs, "%d float channel(s)", ibuf->channels);
181                 }
182                 else if(ibuf->depth==32)
183                         strcat(str, " RGBA float");
184                 else
185                         strcat(str, " RGB float");
186         }
187         else {
188                 if(ibuf->depth==32)
189                         strcat(str, " RGBA byte");
190                 else
191                         strcat(str, " RGB byte");
192         }
193         if(ibuf->zbuf || ibuf->zbuf_float)
194                 strcat(str, " + Z");
195         
196 }
197
198 /* gets active viewer user */
199 struct ImageUser *ntree_get_active_iuser(bNodeTree *ntree)
200 {
201         bNode *node;
202         
203         if(ntree)
204                 for(node= ntree->nodes.first; node; node= node->next)
205                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
206                                 if(node->flag & NODE_DO_OUTPUT)
207                                         return node->storage;
208         return NULL;
209 }
210
211
212 /* ************ panel stuff ************* */
213
214 /* this function gets the values for cursor and vertex number buttons */
215 static void image_transform_but_attr(SpaceImage *sima, int *imx, int *imy, int *step, int *digits) /*, float *xcoord, float *ycoord)*/
216 {
217         ImBuf *ibuf= ED_space_image_buffer(sima);
218         if(ibuf) {
219                 *imx= ibuf->x;
220                 *imy= ibuf->y;
221         }
222         
223         if (sima->flag & SI_COORDFLOATS) {
224                 *step= 1;
225                 *digits= 3;
226         }
227         else {
228                 *step= 100;
229                 *digits= 2;
230         }
231 }
232
233
234 /* is used for both read and write... */
235 static void image_editvertex_buts(const bContext *C, uiBlock *block)
236 {
237         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
238         Object *obedit= CTX_data_edit_object(C);
239         static float ocent[2];
240         float cent[2]= {0.0, 0.0};
241         int imx= 256, imy= 256;
242         int nactive= 0, step, digits;
243         EditMesh *em;
244         EditFace *efa;
245         MTFace *tf;
246         
247         if(obedit==NULL || obedit->type!=OB_MESH) return;
248         
249         if( is_uv_tface_editing_allowed_silent()==0 ) return;
250         
251         image_transform_but_attr(sima, &imx, &imy, &step, &digits);
252         
253         em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
254         for (efa= em->faces.first; efa; efa= efa->next) {
255                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
256                 if (simaFaceDraw_Check(efa, tf)) {
257                         
258                         if (simaUVSel_Check(efa, tf, 0)) {
259                                 cent[0]+= tf->uv[0][0];
260                                 cent[1]+= tf->uv[0][1];
261                                 nactive++;
262                         }
263                         if (simaUVSel_Check(efa, tf, 1)) {
264                                 cent[0]+= tf->uv[1][0];
265                                 cent[1]+= tf->uv[1][1];
266                                 nactive++;
267                         }
268                         if (simaUVSel_Check(efa, tf, 2)) {
269                                 cent[0]+= tf->uv[2][0];
270                                 cent[1]+= tf->uv[2][1];
271                                 nactive++;
272                         }
273                         if (efa->v4 && simaUVSel_Check(efa, tf, 3)) {
274                                 cent[0]+= tf->uv[3][0];
275                                 cent[1]+= tf->uv[3][1];
276                                 nactive++;
277                         }
278                 }
279         }
280                 
281         if(block) {     // do the buttons
282                 if (nactive) {
283                         ocent[0]= cent[0]/nactive;
284                         ocent[1]= cent[1]/nactive;
285                         if (sima->flag & SI_COORDFLOATS) {
286                         } else {
287                                 ocent[0] *= imx;
288                                 ocent[1] *= imy;
289                         }
290                         
291                         //uiBlockBeginAlign(block);
292                         if(nactive==1) {
293                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:",       10, 10, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
294                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:",       165, 10, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
295                         }
296                         else {
297                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:",       10, 10, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
298                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:",       165, 10, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
299                         }
300                         //uiBlockEndAlign(block);
301                 }
302         }
303         else {  // apply event
304                 float delta[2];
305                 
306                 cent[0]= cent[0]/nactive;
307                 cent[1]= cent[1]/nactive;
308                         
309                 if (sima->flag & SI_COORDFLOATS) {
310                         delta[0]= ocent[0]-cent[0];
311                         delta[1]= ocent[1]-cent[1];
312                 }
313                 else {
314                         delta[0]= ocent[0]/imx - cent[0];
315                         delta[1]= ocent[1]/imy - cent[1];
316                 }
317
318                 for (efa= em->faces.first; efa; efa= efa->next) {
319                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
320                         if (simaFaceDraw_Check(efa, tf)) {
321                                 if (simaUVSel_Check(efa, tf, 0)) {
322                                         tf->uv[0][0]+= delta[0];
323                                         tf->uv[0][1]+= delta[1];
324                                 }
325                                 if (simaUVSel_Check(efa, tf, 1)) {
326                                         tf->uv[1][0]+= delta[0];
327                                         tf->uv[1][1]+= delta[1];
328                                 }
329                                 if (simaUVSel_Check(efa, tf, 2)) {
330                                         tf->uv[2][0]+= delta[0];
331                                         tf->uv[2][1]+= delta[1];
332                                 }
333                                 if (efa->v4 && simaUVSel_Check(efa, tf, 3)) {
334                                         tf->uv[3][0]+= delta[0];
335                                         tf->uv[3][1]+= delta[1];
336                                 }
337                         }
338                 }
339                 
340                 WM_event_add_notifier(C, NC_IMAGE, sima->image);
341         }
342
343         BKE_mesh_end_editmesh(obedit->data, em);
344 }
345
346
347 /* is used for both read and write... */
348 static void image_editcursor_buts(const bContext *C, View2D *v2d, uiBlock *block)
349 {
350         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
351         static float ocent[2];
352         int imx= 256, imy= 256;
353         int step, digits;
354         
355         if( is_uv_tface_editing_allowed_silent()==0 ) return;
356         
357         image_transform_but_attr(sima, &imx, &imy, &step, &digits);
358                 
359         if(block) {     // do the buttons
360                 ocent[0]= v2d->cursor[0];
361                 ocent[1]= v2d->cursor[1];
362                 if (sima->flag & SI_COORDFLOATS) {
363                 } else {
364                         ocent[0] *= imx;
365                         ocent[1] *= imy;
366                 }
367                 
368                 uiBlockBeginAlign(block);
369                 uiDefButF(block, NUM, B_CURSOR_IMAGE, "Cursor X:",      165, 120, 145, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
370                 uiDefButF(block, NUM, B_CURSOR_IMAGE, "Cursor Y:",      165, 100, 145, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
371                 uiBlockEndAlign(block);
372         }
373         else {  // apply event
374                 if (sima->flag & SI_COORDFLOATS) {
375                         v2d->cursor[0]= ocent[0];
376                         v2d->cursor[1]= ocent[1];
377                 }
378                 else {
379                         v2d->cursor[0]= ocent[0]/imx;
380                         v2d->cursor[1]= ocent[1]/imy;
381                 }
382                 WM_event_add_notifier(C, NC_IMAGE, sima->image);
383         }
384 }
385
386 #if 0
387 static void image_panel_view_properties(const bContext *C, Panel *pa)
388 {
389         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
390         ARegion *ar= CTX_wm_region(C);
391         Object *obedit= CTX_data_edit_object(C);
392         uiBlock *block;
393
394         block= uiLayoutFreeBlock(pa->layout);
395         uiBlockSetHandleFunc(block, do_image_panel_events, NULL);
396         
397         uiDefButBitI(block, TOG, SI_DRAW_TILE, B_REDR, "Repeat Image",  10,160,140,19, &sima->flag, 0, 0, 0, 0, "Repeat/Tile the image display");
398         uiDefButBitI(block, TOG, SI_COORDFLOATS, B_REDR, "Normalized Coords",   165,160,145,19, &sima->flag, 0, 0, 0, 0, "Display coords from 0.0 to 1.0 rather then in pixels");
399         
400         if (sima->image) {
401                 uiDefBut(block, LABEL, B_NOP, "Image Display:",         10,140,140,19, 0, 0, 0, 0, 0, "");
402                 uiBlockBeginAlign(block);
403                 uiDefButF(block, NUM, B_REDR, "AspX:", 10,120,140,19, &sima->image->aspx, 0.1, 5000.0, 100, 0, "X Display Aspect for this image, does not affect renderingm 0 disables.");
404                 uiDefButF(block, NUM, B_REDR, "AspY:", 10,100,140,19, &sima->image->aspy, 0.1, 5000.0, 100, 0, "X Display Aspect for this image, does not affect rendering 0 disables.");
405                 uiBlockEndAlign(block);
406         }
407         
408         if (obedit && obedit->type==OB_MESH) {
409                 Mesh *me= obedit->data;
410                 EditMesh *em= BKE_mesh_get_editmesh(me);
411                 
412                 if(EM_texFaceCheck(em)) {
413                         uiDefBut(block, LABEL, B_NOP, "Draw Type:",             10, 80,120,19, 0, 0, 0, 0, 0, "");
414                         uiBlockBeginAlign(block);
415                         uiDefButC(block,  ROW, B_REDR, "Outline",               10,60,58,19, &sima->dt_uv, 0.0, SI_UVDT_OUTLINE, 0, 0, "Outline Wire UV drawtype");
416                         uiDefButC(block,  ROW, B_REDR, "Dash",                  68, 60,58,19, &sima->dt_uv, 0.0, SI_UVDT_DASH, 0, 0, "Dashed Wire UV drawtype");
417                         uiDefButC(block,  ROW, B_REDR, "Black",                 126, 60,58,19, &sima->dt_uv, 0.0, SI_UVDT_BLACK, 0, 0, "Black Wire UV drawtype");
418                         uiDefButC(block,  ROW, B_REDR, "White",                 184,60,58,19, &sima->dt_uv, 0.0, SI_UVDT_WHITE, 0, 0, "White Wire UV drawtype");
419                         
420                         uiBlockEndAlign(block);
421                         uiDefButBitI(block, TOG, SI_SMOOTH_UV, B_REDR, "Smooth",        250,60,60,19,  &sima->flag, 0, 0, 0, 0, "Display smooth lines in the UV view");
422                         
423                         
424                         uiDefButBitI(block, TOG, ME_DRAWFACES, B_REDR, "Faces",         10,30,60,19,  &me->drawflag, 0, 0, 0, 0, "Displays all faces as shades in the 3d view and UV editor");
425                         uiDefButBitI(block, TOG, ME_DRAWEDGES, B_REDR, "Edges", 70, 30,60,19, &me->drawflag, 0, 0, 0, 0, "Displays selected edges using hilights in the 3d view and UV editor");
426                         
427                         uiDefButBitI(block, TOG, SI_DRAWSHADOW, B_REDR, "Final Shadow", 130, 30,110,19, &sima->flag, 0, 0, 0, 0, "Draw the final result from the objects modifiers");
428                         uiDefButBitI(block, TOG, SI_DRAW_OTHER, B_REDR, "Other Objs", 230, 30, 80, 19, &sima->flag, 0, 0, 0, 0, "Also draw all 3d view selected mesh objects that use this image");
429                         
430                         uiDefButBitI(block, TOG, SI_DRAW_STRETCH, B_REDR, "UV Stretch", 10,0,100,19,  &sima->flag, 0, 0, 0, 0, "Difference between UV's and the 3D coords (blue for low distortion, red is high)");
431                         if (sima->flag & SI_DRAW_STRETCH) {
432                                 uiBlockBeginAlign(block);
433                                 uiDefButC(block,  ROW, B_REDR, "Area",                  120,0,60,19, &sima->dt_uvstretch, 0.0, SI_UVDT_STRETCH_AREA, 0, 0, "Area distortion between UV's and 3D coords");
434                                 uiDefButC(block,  ROW, B_REDR, "Angle",         180,0,60,19, &sima->dt_uvstretch, 0.0, SI_UVDT_STRETCH_ANGLE, 0, 0, "Angle distortion between UV's and 3D coords");
435                                 uiBlockEndAlign(block);
436                         }
437                 }
438
439                 BKE_mesh_end_editmesh(me, em);
440         }
441         image_editcursor_buts(C, &ar->v2d, block);
442 }
443 #endif
444
445 void brush_buttons(const bContext *C, uiBlock *block, short fromsima,
446                                    int evt_nop, int evt_change,
447                                    int evt_browse, int evt_local,
448                                    int evt_del, int evt_keepdata,
449                                    int evt_texbrowse, int evt_texdel)
450 {
451 //      SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
452         ToolSettings *settings= CTX_data_tool_settings(C);
453         Brush *brush= settings->imapaint.brush;
454         ID *id;
455         int yco, xco, butw, but_idx;
456 //      short *menupoin = &(sima->menunr); // XXX : &(G.buts->menunr);
457         short do_project = settings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE ? 0:1;
458         
459         yco= 160;
460         
461         butw = fromsima ? 80 : 106;
462         
463         uiBlockBeginAlign(block);
464         but_idx = 0;
465         uiDefButS(block, ROW, evt_change, "Draw",               butw*(but_idx++),yco,butw,19, &settings->imapaint.tool, 7.0, PAINT_TOOL_DRAW, 0, 0, "Draw brush");
466         if (fromsima || do_project==0)
467                 uiDefButS(block, ROW, evt_change, "Soften",     butw*(but_idx++),       yco,butw,19, &settings->imapaint.tool, 7.0, PAINT_TOOL_SOFTEN, 0, 0, "Soften brush");
468         uiDefButS(block, ROW, evt_change, "Smear",              butw*(but_idx++),       yco,butw,19, &settings->imapaint.tool, 7.0, PAINT_TOOL_SMEAR, 0, 0, "Smear brush");
469         if (fromsima || do_project)
470                 uiDefButS(block, ROW, evt_change, "Clone",      butw*(but_idx++),       yco,butw,19, &settings->imapaint.tool, 7.0, PAINT_TOOL_CLONE, 0, 0, "Clone brush, use RMB to drag source image");
471         
472         uiBlockEndAlign(block);
473         yco -= 30;
474         
475         id= (ID*)settings->imapaint.brush;
476         xco= 200; // std_libbuttons(block, 0, yco, 0, NULL, evt_browse, ID_BR, 0, id, NULL, menupoin, 0, evt_local, evt_del, 0, evt_keepdata);
477         
478         if(brush && !brush->id.lib) {
479                 
480                 butw= 320-(xco+10);
481                 
482                 uiDefButS(block, MENU, evt_nop, "Mix %x0|Add %x1|Subtract %x2|Multiply %x3|Lighten %x4|Darken %x5|Erase Alpha %x6|Add Alpha %x7", xco+10,yco,butw,19, &brush->blend, 0, 0, 0, 0, "Blending method for applying brushes");
483                 
484                 uiBlockBeginAlign(block);
485                 uiDefButBitS(block, TOG|BIT, BRUSH_AIRBRUSH, evt_change, "Airbrush",    xco+10,yco-25,butw/2,19, &brush->flag, 0, 0, 0, 0, "Keep applying paint effect while holding mouse (spray)");
486                 uiDefButF(block, NUM, evt_nop, "", xco+10 + butw/2,yco-25,butw/2,19, &brush->rate, 0.01, 1.0, 0, 0, "Number of paints per second for Airbrush");
487                 uiBlockEndAlign(block);
488                 
489                 if (fromsima) {
490                         uiDefButBitS(block, TOG|BIT, BRUSH_TORUS, evt_change, "Wrap",   xco+10,yco-45,butw,19, &brush->flag, 0, 0, 0, 0, "Enables torus wrapping");
491                         yco -= 25;
492                 }
493                 else {
494                         yco -= 25;
495                         uiBlockBeginAlign(block);
496                         uiDefButBitS(block, TOGN|BIT, IMAGEPAINT_PROJECT_DISABLE, B_REDR, "Project Paint",      xco+10,yco-25,butw,19, &settings->imapaint.flag, 0, 0, 0, 0, "Use projection painting for improved consistency in the brush strokes");
497                         
498                         if ((settings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE)==0) {
499                                 /* Projection Painting */
500                                 
501                                 uiDefButBitS(block, TOGN|BIT, IMAGEPAINT_PROJECT_XRAY, B_NOP, "Occlude",        xco+10,yco-45,butw/2,19, &settings->imapaint.flag, 0, 0, 0, 0, "Only paint onto the faces directly under the brush (slower)");
502                                 uiDefButBitS(block, TOGN|BIT, IMAGEPAINT_PROJECT_BACKFACE, B_NOP, "Cull",       xco+10+butw/2,yco-45,butw/2,19, &settings->imapaint.flag, 0, 0, 0, 0, "Ignore faces pointing away from the view (faster)");
503                                 
504                                 uiDefButBitS(block, TOGN|BIT, IMAGEPAINT_PROJECT_FLAT, B_NOP, "Normal", xco+10,yco-65,butw/2,19, &settings->imapaint.flag, 0, 0, 0, 0, "Paint most on faces pointing towards the view");
505                                 uiDefButS(block, NUM, B_NOP, "", xco+10 +(butw/2),yco-65,butw/2,19, &settings->imapaint.normal_angle, 10.0, 90.0, 0, 0, "Paint most on faces pointing towards the view acording to this angle");
506                                 
507                                 uiDefButS(block, NUM, B_NOP, "Bleed: ", xco+10,yco-85,butw,19, &settings->imapaint.seam_bleed, 0.0, 8.0, 0, 0, "Extend paint beyond the faces UVs to reduce seams (in pixels, slower)");
508                                 uiBlockEndAlign(block);
509                                 
510                                 uiBlockBeginAlign(block);
511                                 uiDefButBitS(block, TOG|BIT, IMAGEPAINT_PROJECT_LAYER_MASK, B_NOP, "Stencil Layer",     xco+10,yco-110,butw-30,19, &settings->imapaint.flag, 0, 0, 0, 0, "Set the mask layer from the UV layer buttons");
512                                 uiDefButBitS(block, TOG|BIT, IMAGEPAINT_PROJECT_LAYER_MASK_INV, B_NOP, "Inv",   xco+10 + butw-30,yco-110,30,19, &settings->imapaint.flag, 0, 0, 0, 0, "Invert the mask");
513                                 uiBlockEndAlign(block);
514                                 
515                         }
516                         uiBlockEndAlign(block);
517                 }
518                 
519                 uiBlockBeginAlign(block);
520                 uiDefButF(block, COL, B_VPCOLSLI, "",                                   0,yco,200,19, brush->rgb, 0, 0, 0, 0, "");
521                 uiDefButF(block, NUMSLI, evt_nop, "Opacity ",           0,yco-20,180,19, &brush->alpha, 0.0, 1.0, 0, 0, "The amount of pressure on the brush");
522                 uiDefButBitS(block, TOG|BIT, BRUSH_ALPHA_PRESSURE, evt_nop, "P",        180,yco-20,20,19, &brush->flag, 0, 0, 0, 0, "Enables pressure sensitivity for tablets");
523                 uiDefButI(block, NUMSLI, evt_nop, "Size ",              0,yco-40,180,19, &brush->size, 1, 200, 0, 0, "The size of the brush");
524                 uiDefButBitS(block, TOG|BIT, BRUSH_SIZE_PRESSURE, evt_nop, "P", 180,yco-40,20,19, &brush->flag, 0, 0, 0, 0, "Enables pressure sensitivity for tablets");
525                 uiDefButF(block, NUMSLI, evt_nop, "Falloff ",           0,yco-60,180,19, &brush->innerradius, 0.0, 1.0, 0, 0, "The fall off radius of the brush");
526                 uiDefButBitS(block, TOG|BIT, BRUSH_RAD_PRESSURE, evt_nop, "P",  180,yco-60,20,19, &brush->flag, 0, 0, 0, 0, "Enables pressure sensitivity for tablets");
527                 uiDefButF(block, NUMSLI, evt_nop, "Spacing ",0,yco-80,180,19, &brush->spacing, 1.0, 100.0, 0, 0, "Repeating paint on %% of brush diameter");
528                 uiDefButBitS(block, TOG|BIT, BRUSH_SPACING_PRESSURE, evt_nop, "P",      180,yco-80,20,19, &brush->flag, 0, 0, 0, 0, "Enables pressure sensitivity for tablets");
529                 uiBlockEndAlign(block);
530                 
531                 yco -= 110;
532                 
533                 if(fromsima && settings->imapaint.tool == PAINT_TOOL_CLONE) {
534                         id= (ID*)brush->clone.image;
535                         xco= 200; // std_libbuttons(block, 0, yco, 0, NULL, B_SIMACLONEBROWSE, ID_IM, 0, id, 0, menupoin, 0, 0, B_SIMACLONEDELETE, 0, 0);
536                         if(id) {
537                                 butw= 320-(xco+5);
538                                 uiDefButF(block, NUMSLI, evt_change, "B ",xco+5,yco,butw,19, &brush->clone.alpha , 0.0, 1.0, 0, 0, "Opacity of clone image display");
539                         }
540                 }
541                 else {
542                         if (
543                                 (fromsima==0) && /* 3D View */
544                                 (settings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE)==0 && /* Projection Painting */
545                                 (settings->imapaint.tool == PAINT_TOOL_CLONE)
546                                 ) {
547                                 butw = 130;
548                                 uiDefButBitS(block, TOG|BIT, IMAGEPAINT_PROJECT_LAYER_CLONE, B_REDR, "Clone Layer",     0,yco,butw,20, &settings->imapaint.flag, 0, 0, 0, 0, "Use another UV layer as clone source, otherwise use 3D the cursor as the source");
549                         }
550                         else {
551                                 MTex *mtex= brush->mtex[brush->texact];
552                                 
553                                 id= (mtex)? (ID*)mtex->tex: NULL;
554                                 xco= 200; // std_libbuttons(block, 0, yco, 0, NULL, evt_texbrowse, ID_TE, 0, id, NULL, menupoin, 0, 0, evt_texdel, 0, 0);
555                                 /*uiDefButBitS(block, TOG|BIT, BRUSH_FIXED_TEX, evt_change, "Fixed",    xco+5,yco,butw,19, &brush->flag, 0, 0, 0, 0, "Keep texture origin in fixed position");*/
556                         }
557                 }
558         }
559         
560 #if 0
561         uiDefButBitS(block, TOG|BIT, IMAGEPAINT_DRAW_TOOL_DRAWING, B_SIMABRUSHCHANGE, "TD", 0,1,50,19, &settings->imapaint.flag.flag, 0, 0, 0, 0, "Enables brush shape while drawing");
562         uiDefButBitS(block, TOG|BIT, IMAGEPAINT_DRAW_TOOL, B_SIMABRUSHCHANGE, "TP", 50,1,50,19, &settings->imapaint.flag.flag, 0, 0, 0, 0, "Enables brush shape while not drawing");
563 #endif
564 }
565
566 static int image_panel_paint_poll(const bContext *C, PanelType *pt)
567 {
568         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
569         
570         return (sima->image && (sima->flag & SI_DRAWTOOL));
571 }
572
573 static void image_panel_paintcolor(const bContext *C, Panel *pa)
574 {
575         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
576         ToolSettings *settings= CTX_data_tool_settings(C);
577         Brush *brush= settings->imapaint.brush;
578         uiBlock *block;
579         static float hsv[3], old[3];    // used as temp mem for picker
580         static char hexcol[128];
581         
582         if(!sima->image || (sima->flag & SI_DRAWTOOL)==0)
583                 return;
584         
585         block= uiLayoutFreeBlock(pa->layout);
586         uiBlockSetHandleFunc(block, do_image_panel_events, NULL);
587
588         if(brush)
589                 uiBlockPickerButtons(block, brush->rgb, hsv, old, hexcol, 'f', B_REDR);
590 }
591
592 static void image_panel_paint(const bContext *C, Panel *pa)
593 {
594         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
595         uiBlock *block;
596         
597         if(!sima->image || (sima->flag & SI_DRAWTOOL)==0)
598                 return;
599         
600         block= uiLayoutFreeBlock(pa->layout);
601         uiBlockSetHandleFunc(block, do_image_panel_events, NULL);
602         
603         brush_buttons(C, block, 1, B_SIMANOTHING, B_SIMABRUSHCHANGE, B_SIMABRUSHBROWSE, B_SIMABRUSHLOCAL, B_SIMABRUSHDELETE, B_KEEPDATA, B_SIMABTEXBROWSE, B_SIMABTEXDELETE);
604 }
605
606 static void image_panel_curves_reset(bContext *C, void *cumap_v, void *ibuf_v)
607 {
608         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
609         CurveMapping *cumap = cumap_v;
610         int a;
611         
612         for(a=0; a<CM_TOT; a++)
613                 curvemap_reset(cumap->cm+a, &cumap->clipr);
614         
615         cumap->black[0]=cumap->black[1]=cumap->black[2]= 0.0f;
616         cumap->white[0]=cumap->white[1]=cumap->white[2]= 1.0f;
617         curvemapping_set_black_white(cumap, NULL, NULL);
618         
619         curvemapping_changed(cumap, 0);
620         curvemapping_do_ibuf(cumap, ibuf_v);
621         
622         WM_event_add_notifier(C, NC_IMAGE, sima->image);
623 }
624
625
626 static void image_panel_curves(const bContext *C, Panel *pa)
627 {
628         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
629         ImBuf *ibuf;
630         uiBlock *block;
631         uiBut *bt;
632         
633         /* and we check for spare */
634         ibuf= ED_space_image_buffer(sima);
635         
636         block= uiLayoutFreeBlock(pa->layout);
637         uiBlockSetHandleFunc(block, do_image_panel_events, NULL);
638         
639         if (ibuf) {
640                 rctf rect;
641                 
642                 if(sima->cumap==NULL)
643                         sima->cumap= curvemapping_add(4, 0.0f, 0.0f, 1.0f, 1.0f);
644                 
645                 rect.xmin= 110; rect.xmax= 310;
646                 rect.ymin= 10; rect.ymax= 200;
647                 curvemap_buttons(block, sima->cumap, 'c', B_SIMACURVES, B_REDR, &rect);
648                 
649                 /* curvemap min/max only works for RGBA */
650                 if(ibuf->channels==4) {
651                         bt=uiDefBut(block, BUT, B_SIMARANGE, "Reset",   10, 160, 90, 19, NULL, 0.0f, 0.0f, 0, 0, "Reset Black/White point and curves");
652                         uiButSetFunc(bt, image_panel_curves_reset, sima->cumap, ibuf);
653                 
654                         uiBlockBeginAlign(block);
655                         uiDefButF(block, NUM, B_SIMARANGE, "Min R:",    10, 120, 90, 19, sima->cumap->black, -1000.0f, 1000.0f, 10, 2, "Black level");
656                         uiDefButF(block, NUM, B_SIMARANGE, "Min G:",    10, 100, 90, 19, sima->cumap->black+1, -1000.0f, 1000.0f, 10, 2, "Black level");
657                         uiDefButF(block, NUM, B_SIMARANGE, "Min B:",    10, 80, 90, 19, sima->cumap->black+2, -1000.0f, 1000.0f, 10, 2, "Black level");
658                         
659                         uiBlockBeginAlign(block);
660                         uiDefButF(block, NUM, B_SIMARANGE, "Max R:",    10, 50, 90, 19, sima->cumap->white, -1000.0f, 1000.0f, 10, 2, "White level");
661                         uiDefButF(block, NUM, B_SIMARANGE, "Max G:",    10, 30, 90, 19, sima->cumap->white+1, -1000.0f, 1000.0f, 10, 2, "White level");
662                         uiDefButF(block, NUM, B_SIMARANGE, "Max B:",    10, 10, 90, 19, sima->cumap->white+2, -1000.0f, 1000.0f, 10, 2, "White level");
663                 }
664         }
665 }
666
667 #if 0
668 /* 0: disable preview 
669    otherwise refresh preview
670 */
671 void image_preview_event(int event)
672 {
673         int exec= 0;
674         
675         if(event==0) {
676                 G.scene->r.scemode &= ~R_COMP_CROP;
677                 exec= 1;
678         }
679         else {
680                 if(image_preview_active(curarea, NULL, NULL)) {
681                         G.scene->r.scemode |= R_COMP_CROP;
682                         exec= 1;
683                 }
684                 else
685                         G.scene->r.scemode &= ~R_COMP_CROP;
686         }
687         
688         if(exec && G.scene->nodetree) {
689                 /* should work when no node editor in screen..., so we execute right away */
690                 
691                 ntreeCompositTagGenerators(G.scene->nodetree);
692
693                 G.afbreek= 0;
694                 G.scene->nodetree->timecursor= set_timecursor;
695                 G.scene->nodetree->test_break= blender_test_break;
696                 
697                 BIF_store_spare();
698                 
699                 ntreeCompositExecTree(G.scene->nodetree, &G.scene->r, 1);       /* 1 is do_previews */
700                 
701                 G.scene->nodetree->timecursor= NULL;
702                 G.scene->nodetree->test_break= NULL;
703                 
704                 scrarea_do_windraw(curarea);
705                 waitcursor(0);
706                 
707                 WM_event_add_notifier(C, NC_IMAGE, ima_v);
708         }       
709 }
710
711
712 /* nothing drawn here, we use it to store values */
713 static void preview_cb(struct ScrArea *sa, struct uiBlock *block)
714 {
715         SpaceImage *sima= sa->spacedata.first;
716         rctf dispf;
717         rcti *disprect= &G.scene->r.disprect;
718         int winx= (G.scene->r.size*G.scene->r.xsch)/100;
719         int winy= (G.scene->r.size*G.scene->r.ysch)/100;
720         short mval[2];
721         
722         if(G.scene->r.mode & R_BORDER) {
723                 winx*= (G.scene->r.border.xmax - G.scene->r.border.xmin);
724                 winy*= (G.scene->r.border.ymax - G.scene->r.border.ymin);
725         }
726         
727         /* while dragging we need to update the rects, otherwise it doesn't end with correct one */
728
729         BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
730         ui_graphics_to_window_rct(sa->win, &dispf, disprect);
731         
732         /* correction for gla draw */
733         BLI_translate_rcti(disprect, -curarea->winrct.xmin, -curarea->winrct.ymin);
734         
735         calc_image_view(sima, 'p');
736 //      printf("winrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
737         /* map to image space coordinates */
738         mval[0]= disprect->xmin; mval[1]= disprect->ymin;
739         areamouseco_to_ipoco(v2d, mval, &dispf.xmin, &dispf.ymin);
740         mval[0]= disprect->xmax; mval[1]= disprect->ymax;
741         areamouseco_to_ipoco(v2d, mval, &dispf.xmax, &dispf.ymax);
742         
743         /* map to render coordinates */
744         disprect->xmin= dispf.xmin;
745         disprect->xmax= dispf.xmax;
746         disprect->ymin= dispf.ymin;
747         disprect->ymax= dispf.ymax;
748         
749         CLAMP(disprect->xmin, 0, winx);
750         CLAMP(disprect->xmax, 0, winx);
751         CLAMP(disprect->ymin, 0, winy);
752         CLAMP(disprect->ymax, 0, winy);
753 //      printf("drawrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
754
755 }
756
757 static int is_preview_allowed(ScrArea *cur)
758 {
759         SpaceImage *sima= cur->spacedata.first;
760         ScrArea *sa;
761
762         /* check if another areawindow has preview set */
763         for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
764                 if(sa!=cur && sa->spacetype==SPACE_IMAGE) {
765                         if(image_preview_active(sa, NULL, NULL))
766                            return 0;
767                 }
768         }
769         /* check image type */
770         if(sima->image==NULL || sima->image->type!=IMA_TYPE_COMPOSITE)
771                 return 0;
772         
773         return 1;
774 }
775
776
777 static void image_panel_preview(ScrArea *sa, short cntrl)       // IMAGE_HANDLER_PREVIEW
778 {
779         uiBlock *block;
780         SpaceImage *sima= sa->spacedata.first;
781         int ofsx, ofsy;
782         
783         if(is_preview_allowed(sa)==0) {
784                 rem_blockhandler(sa, IMAGE_HANDLER_PREVIEW);
785                 G.scene->r.scemode &= ~R_COMP_CROP;     /* quite weak */
786                 return;
787         }
788         
789         block= uiBeginBlock(C, ar, "image_panel_preview", UI_EMBOSS);
790         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
791         uiSetPanelHandler(IMAGE_HANDLER_PREVIEW);  // for close and esc
792         
793         ofsx= -150+(sa->winx/2)/sima->blockscale;
794         ofsy= -100+(sa->winy/2)/sima->blockscale;
795         if(uiNewPanel(C, ar, block, "Preview", "Image", ofsx, ofsy, 300, 200)==0) return;
796         
797         uiBlockSetDrawExtraFunc(block, preview_cb);
798         
799 }
800
801 static void image_panel_gpencil(short cntrl)    // IMAGE_HANDLER_GREASEPENCIL
802 {
803         uiBlock *block;
804         SpaceImage *sima;
805         
806         sima= curarea->spacedata.first;
807
808         block= uiBeginBlock(C, ar, "image_panel_gpencil", UI_EMBOSS);
809         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
810         uiSetPanelHandler(IMAGE_HANDLER_GREASEPENCIL);  // for close and esc
811         if (uiNewPanel(C, ar, block, "Grease Pencil", "SpaceImage", 100, 30, 318, 204)==0) return;
812         
813         /* allocate memory for gpd if drawing enabled (this must be done first or else we crash) */
814         if (sima->flag & SI_DISPGP) {
815                 if (sima->gpd == NULL)
816                         gpencil_data_setactive(curarea, gpencil_data_addnew());
817         }
818         
819         if (sima->flag & SI_DISPGP) {
820                 bGPdata *gpd= sima->gpd;
821                 short newheight;
822                 
823                 /* this is a variable height panel, newpanel doesnt force new size on existing panels */
824                 /* so first we make it default height */
825                 uiNewPanelHeight(block, 204);
826                 
827                 /* draw button for showing gpencil settings and drawings */
828                 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)");
829                 
830                 /* extend the panel if the contents won't fit */
831                 newheight= draw_gpencil_panel(block, gpd, curarea); 
832                 uiNewPanelHeight(block, newheight);
833         }
834         else {
835                 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");
836                 uiDefBut(block, LABEL, 1, " ",  160, 180, 150, 20, NULL, 0.0, 0.0, 0, 0, "");
837         }
838 }
839 #endif
840
841
842 /* ********************* callbacks for standard image buttons *************** */
843
844 /* called from fileselect or button */
845 static void load_image_cb(bContext *C, char *str, void *ima_pp_v, void *iuser_v)        
846 {
847         Image **ima_pp= (Image **)ima_pp_v;
848         Image *ima= NULL;
849         
850         ima= BKE_add_image_file(str, 0);
851         if(ima) {
852                 if(*ima_pp) {
853                         (*ima_pp)->id.us--;
854                 }
855                 *ima_pp= ima;
856                 
857                 BKE_image_signal(ima, iuser_v, IMA_SIGNAL_RELOAD);
858                 WM_event_add_notifier(C, NC_IMAGE, ima);
859                 
860                 /* button event gets lost when it goes via filewindow */
861 //              if(G.buts && G.buts->lockpoin) {
862 //                      Tex *tex= G.buts->lockpoin;
863 //                      if(GS(tex->id.name)==ID_TE) {
864 //                              BIF_preview_changed(ID_TE);
865 //                              allqueue(REDRAWBUTSSHADING, 0);
866 //                              allqueue(REDRAWVIEW3D, 0);
867 //                              allqueue(REDRAWOOPS, 0);
868 //                      }
869 //              }
870         }
871         
872         ED_undo_push(C, "Load image");
873 }
874
875 static char *layer_menu(RenderResult *rr, short *curlay)
876 {
877         RenderLayer *rl;
878         int len= 64 + 32*BLI_countlist(&rr->layers);
879         short a, nr= 0;
880         char *str= MEM_callocN(len, "menu layers");
881         
882         strcpy(str, "Layer %t");
883         a= strlen(str);
884         
885         /* compo result */
886         if(rr->rectf) {
887                 a+= sprintf(str+a, "|Composite %%x0");
888                 nr= 1;
889         }
890         for(rl= rr->layers.first; rl; rl= rl->next, nr++) {
891                 a+= sprintf(str+a, "|%s %%x%d", rl->name, nr);
892         }
893         
894         /* no curlay clip here, on render (redraws) the amount of layers can be 1 fir single-layer render */
895         
896         return str;
897 }
898
899 /* rl==NULL means composite result */
900 static char *pass_menu(RenderLayer *rl, short *curpass)
901 {
902         RenderPass *rpass;
903         int len= 64 + 32*(rl?BLI_countlist(&rl->passes):1);
904         short a, nr= 0;
905         char *str= MEM_callocN(len, "menu layers");
906         
907         strcpy(str, "Pass %t");
908         a= strlen(str);
909         
910         /* rendered results don't have a Combined pass */
911         if(rl==NULL || rl->rectf) {
912                 a+= sprintf(str+a, "|Combined %%x0");
913                 nr= 1;
914         }
915         
916         if(rl)
917                 for(rpass= rl->passes.first; rpass; rpass= rpass->next, nr++)
918                         a+= sprintf(str+a, "|%s %%x%d", rpass->name, nr);
919         
920         if(*curpass >= nr)
921                 *curpass= 0;
922         
923         return str;
924 }
925
926 static void set_frames_cb(bContext *C, void *ima_v, void *iuser_v)
927 {
928         Scene *scene= CTX_data_scene(C);
929         Image *ima= ima_v;
930         ImageUser *iuser= iuser_v;
931         
932         if(ima->anim) {
933                 iuser->frames = IMB_anim_get_duration(ima->anim);
934                 BKE_image_user_calc_imanr(iuser, scene->r.cfra, 0);
935         }
936 }
937
938 static void image_src_change_cb(bContext *C, void *ima_v, void *iuser_v)
939 {
940         BKE_image_signal(ima_v, iuser_v, IMA_SIGNAL_SRC_CHANGE);
941 }
942
943 /* buttons have 2 arg callbacks, filewindow has 3 args... so thats why the wrapper below */
944 static void image_browse_cb1(bContext *C, void *ima_pp_v, void *iuser_v)
945 {
946         Image **ima_pp= (Image **)ima_pp_v;
947         ImageUser *iuser= iuser_v;
948         
949         if(ima_pp) {
950                 Image *ima= *ima_pp;
951                 
952                 if(iuser->menunr== -2) {
953                         // XXX activate_databrowse_args(&ima->id, ID_IM, 0, &iuser->menunr, image_browse_cb1, ima_pp, iuser);
954                 } 
955                 else if (iuser->menunr>0) {
956                         Image *newima= (Image*) BLI_findlink(&CTX_data_main(C)->image, iuser->menunr-1);
957                         
958                         if (newima && newima!=ima) {
959                                 *ima_pp= newima;
960                                 id_us_plus(&newima->id);
961                                 if(ima) ima->id.us--;
962                                 
963                                 BKE_image_signal(newima, iuser, IMA_SIGNAL_USER_NEW_IMAGE);
964                                 
965                                 ED_undo_push(C, "Browse image");
966                         }
967                 }
968         }
969 }
970
971 static void image_browse_cb(bContext *C, void *ima_pp_v, void *iuser_v)
972 {
973         image_browse_cb1(C, ima_pp_v, iuser_v);
974 }
975
976 static void image_reload_cb(bContext *C, void *ima_v, void *iuser_v)
977 {
978         if(ima_v) {
979                 BKE_image_signal(ima_v, iuser_v, IMA_SIGNAL_RELOAD);
980         }
981 }
982
983 static void image_field_test(bContext *C, void *ima_v, void *iuser_v)
984 {
985         Image *ima= ima_v;
986         
987         if(ima) {
988                 ImBuf *ibuf= BKE_image_get_ibuf(ima, iuser_v);
989                 if(ibuf) {
990                         short nr= 0;
991                         if( !(ima->flag & IMA_FIELDS) && (ibuf->flags & IB_fields) ) nr= 1;
992                         if( (ima->flag & IMA_FIELDS) && !(ibuf->flags & IB_fields) ) nr= 1;
993                         if(nr) {
994                                 BKE_image_signal(ima, iuser_v, IMA_SIGNAL_FREE);
995                         }
996                 }
997         }
998 }
999
1000 static void image_unlink_cb(bContext *C, void *ima_pp_v, void *unused)
1001 {
1002         Image **ima_pp= (Image **)ima_pp_v;
1003         
1004         if(ima_pp && *ima_pp) {
1005                 Image *ima= *ima_pp;
1006                 /* (for time being, texturefaces are no users, conflict in design...) */
1007                 if(ima->id.us>1)
1008                         ima->id.us--;
1009                 *ima_pp= NULL;
1010         }
1011 }
1012
1013 static void image_load_fs_cb(bContext *C, void *ima_pp_v, void *iuser_v)
1014 {
1015         ScrArea *sa= CTX_wm_area(C);
1016 //      Image **ima_pp= (Image **)ima_pp_v;
1017         
1018         if(sa->spacetype==SPACE_IMAGE)
1019                 WM_operator_name_call(C, "IMAGE_OT_open", WM_OP_INVOKE_REGION_WIN, NULL);
1020         else
1021                 printf("not supported yet\n");
1022 }
1023
1024 /* 5 layer button callbacks... */
1025 static void image_multi_cb(bContext *C, void *rr_v, void *iuser_v) 
1026 {
1027         BKE_image_multilayer_index(rr_v, iuser_v); 
1028         WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
1029 }
1030 static void image_multi_inclay_cb(bContext *C, void *rr_v, void *iuser_v) 
1031 {
1032         RenderResult *rr= rr_v;
1033         ImageUser *iuser= iuser_v;
1034         int tot= BLI_countlist(&rr->layers) + (rr->rectf?1:0);  /* fake compo result layer */
1035
1036         if(iuser->layer<tot-1) {
1037                 iuser->layer++;
1038                 BKE_image_multilayer_index(rr, iuser); 
1039                 WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
1040         }
1041 }
1042 static void image_multi_declay_cb(bContext *C, void *rr_v, void *iuser_v) 
1043 {
1044         ImageUser *iuser= iuser_v;
1045
1046         if(iuser->layer>0) {
1047                 iuser->layer--;
1048                 BKE_image_multilayer_index(rr_v, iuser); 
1049                 WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
1050         }
1051 }
1052 static void image_multi_incpass_cb(bContext *C, void *rr_v, void *iuser_v) 
1053 {
1054         RenderResult *rr= rr_v;
1055         ImageUser *iuser= iuser_v;
1056         RenderLayer *rl= BLI_findlink(&rr->layers, iuser->layer);
1057
1058         if(rl) {
1059                 int tot= BLI_countlist(&rl->passes) + (rl->rectf?1:0);  /* builtin render result has no combined pass in list */
1060                 if(iuser->pass<tot-1) {
1061                         iuser->pass++;
1062                         BKE_image_multilayer_index(rr, iuser); 
1063                         WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
1064                 }
1065         }
1066 }
1067 static void image_multi_decpass_cb(bContext *C, void *rr_v, void *iuser_v) 
1068 {
1069         ImageUser *iuser= iuser_v;
1070
1071         if(iuser->pass>0) {
1072                 iuser->pass--;
1073                 BKE_image_multilayer_index(rr_v, iuser); 
1074                 WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
1075         }
1076 }
1077
1078 static void image_pack_cb(bContext *C, void *ima_v, void *iuser_v) 
1079 {
1080         if(ima_v) {
1081                 Image *ima= ima_v;
1082                 if(ima->source!=IMA_SRC_SEQUENCE && ima->source!=IMA_SRC_MOVIE) {
1083                         if (ima->packedfile) {
1084                                 if (G.fileflags & G_AUTOPACK) {
1085                                         if (okee("Disable AutoPack ?")) {
1086                                                 G.fileflags &= ~G_AUTOPACK;
1087                                         }
1088                                 }
1089                                 
1090                                 if ((G.fileflags & G_AUTOPACK) == 0) {
1091                                         unpackImage(NULL, ima, PF_ASK); /* XXX report errors */
1092                                         ED_undo_push(C, "Unpack image");
1093                                 }
1094                         } 
1095                         else {
1096                                 ImBuf *ibuf= BKE_image_get_ibuf(ima, iuser_v);
1097                                 if (ibuf && (ibuf->userflags & IB_BITMAPDIRTY)) {
1098                                         // XXX error("Can't pack painted image. Save image or use Repack as PNG.");
1099                                 } else {
1100                                         ima->packedfile = newPackedFile(NULL, ima->name); /* XXX report errors */
1101                                         ED_undo_push(C, "Pack image");
1102                                 }
1103                         }
1104                 }
1105         }
1106 }
1107
1108 static void image_load_cb(bContext *C, void *ima_pp_v, void *iuser_v)
1109 {
1110         if(ima_pp_v) {
1111                 Image *ima= *((Image **)ima_pp_v);
1112                 ImBuf *ibuf= BKE_image_get_ibuf(ima, iuser_v);
1113                 char str[FILE_MAX];
1114         
1115                 /* name in ima has been changed by button! */
1116                 BLI_strncpy(str, ima->name, FILE_MAX);
1117                 if(ibuf) BLI_strncpy(ima->name, ibuf->name, FILE_MAX);
1118                 
1119                 load_image_cb(C, str, ima_pp_v, iuser_v);
1120         }
1121 }
1122
1123 static void image_freecache_cb(bContext *C, void *ima_v, void *unused) 
1124 {
1125         Scene *scene= CTX_data_scene(C);
1126         BKE_image_free_anim_ibufs(ima_v, scene->r.cfra);
1127         WM_event_add_notifier(C, NC_IMAGE, ima_v);
1128 }
1129
1130 static void image_generated_change_cb(bContext *C, void *ima_v, void *iuser_v)
1131 {
1132         BKE_image_signal(ima_v, iuser_v, IMA_SIGNAL_FREE);
1133 }
1134
1135 static void image_user_change(bContext *C, void *iuser_v, void *unused)
1136 {
1137         Scene *scene= CTX_data_scene(C);
1138         BKE_image_user_calc_imanr(iuser_v, scene->r.cfra, 0);
1139 }
1140
1141 static void uiblock_layer_pass_buttons(uiBlock *block, RenderResult *rr, ImageUser *iuser, int event, int x, int y, int w)
1142 {
1143         uiBut *but;
1144         RenderLayer *rl= NULL;
1145         int wmenu1, wmenu2;
1146         char *strp;
1147
1148         /* layer menu is 1/3 larger than pass */
1149         wmenu1= (3*w)/5;
1150         wmenu2= (2*w)/5;
1151         
1152         /* menu buts */
1153         strp= layer_menu(rr, &iuser->layer);
1154         but= uiDefButS(block, MENU, event, strp,                                        x, y, wmenu1, 20, &iuser->layer, 0,0,0,0, "Select Layer");
1155         uiButSetFunc(but, image_multi_cb, rr, iuser);
1156         MEM_freeN(strp);
1157         
1158         rl= BLI_findlink(&rr->layers, iuser->layer - (rr->rectf?1:0)); /* fake compo layer, return NULL is meant to be */
1159         strp= pass_menu(rl, &iuser->pass);
1160         but= uiDefButS(block, MENU, event, strp,                                        x+wmenu1, y, wmenu2, 20, &iuser->pass, 0,0,0,0, "Select Pass");
1161         uiButSetFunc(but, image_multi_cb, rr, iuser);
1162         MEM_freeN(strp);        
1163 }
1164
1165 static void uiblock_layer_pass_arrow_buttons(uiBlock *block, RenderResult *rr, ImageUser *iuser, int imagechanged) 
1166 {
1167         uiBut *but;
1168         
1169         if(rr==NULL || iuser==NULL)
1170                 return;
1171         if(rr->layers.first==NULL) {
1172                 uiDefBut(block, LABEL, 0, "No Layers in Render Result,",        10, 107, 300, 20, NULL, 1, 0, 0, 0, "");
1173                 return;
1174         }
1175         
1176         uiBlockBeginAlign(block);
1177
1178         /* decrease, increase arrows */
1179         but= uiDefIconBut(block, BUT, imagechanged, ICON_TRIA_LEFT,     10,107,17,20, NULL, 0, 0, 0, 0, "Previous Layer");
1180         uiButSetFunc(but, image_multi_declay_cb, rr, iuser);
1181         but= uiDefIconBut(block, BUT, imagechanged, ICON_TRIA_RIGHT,    27,107,18,20, NULL, 0, 0, 0, 0, "Next Layer");
1182         uiButSetFunc(but, image_multi_inclay_cb, rr, iuser);
1183
1184         uiblock_layer_pass_buttons(block, rr, iuser, imagechanged, 45, 107, 230);
1185
1186         /* decrease, increase arrows */
1187         but= uiDefIconBut(block, BUT, imagechanged, ICON_TRIA_LEFT,     275,107,17,20, NULL, 0, 0, 0, 0, "Previous Pass");
1188         uiButSetFunc(but, image_multi_decpass_cb, rr, iuser);
1189         but= uiDefIconBut(block, BUT, imagechanged, ICON_TRIA_RIGHT,    292,107,18,20, NULL, 0, 0, 0, 0, "Next Pass");
1190         uiButSetFunc(but, image_multi_incpass_cb, rr, iuser);
1191
1192         uiBlockEndAlign(block);
1193                          
1194 }
1195
1196 // XXX HACK!
1197 static int packdummy=0;
1198
1199 /* The general Image panel with the loadsa callbacks! */
1200 void ED_image_uiblock_panel(const bContext *C, uiBlock *block, Image **ima_pp, ImageUser *iuser, 
1201                                                  short redraw, short imagechanged)
1202 {
1203         Scene *scene= CTX_data_scene(C);
1204         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1205         Image *ima= *ima_pp;
1206         uiBut *but;
1207         char str[128], *strp;
1208         
1209         /* different stuff when we show viewer */
1210         if(ima && ima->source==IMA_SRC_VIEWER) {
1211                 ImBuf *ibuf= BKE_image_get_ibuf(ima, iuser);
1212                 
1213                 image_info(ima, ibuf, str);
1214                 uiDefBut(block, LABEL, 0, ima->id.name+2,       10, 180, 300, 20, NULL, 1, 0, 0, 0, "");
1215                 uiDefBut(block, LABEL, 0, str,                          10, 160, 300, 20, NULL, 1, 0, 0, 0, "");
1216                 
1217                 if(ima->type==IMA_TYPE_COMPOSITE) {
1218                         iuser= ntree_get_active_iuser(scene->nodetree);
1219                         if(iuser) {
1220                                 uiBlockBeginAlign(block);
1221                                 uiDefIconTextBut(block, BUT, B_SIMA_RECORD, ICON_REC, "Record", 10,120,100,20, 0, 0, 0, 0, 0, "");
1222                                 uiDefIconTextBut(block, BUT, B_SIMA_PLAY, ICON_PLAY, "Play",    110,120,100,20, 0, 0, 0, 0, 0, "");
1223                                 but= uiDefBut(block, BUT, B_NOP, "Free Cache",  210,120,100,20, 0, 0, 0, 0, 0, "");
1224                                 uiButSetFunc(but, image_freecache_cb, ima, NULL);
1225                                 
1226                                 if(iuser->frames)
1227                                         sprintf(str, "(%d) Frames:", iuser->framenr);
1228                                 else strcpy(str, "Frames:");
1229                                 uiBlockBeginAlign(block);
1230                                 uiDefButI(block, NUM, imagechanged, str,                10, 90,150, 20, &iuser->frames, 0.0, MAXFRAMEF, 0, 0, "Sets the number of images of a movie to use");
1231                                 uiDefButI(block, NUM, imagechanged, "StartFr:", 160,90,150,20, &iuser->sfra, 1.0, MAXFRAMEF, 0, 0, "Sets the global starting frame of the movie");
1232                         }
1233                 }
1234                 else if(ima->type==IMA_TYPE_R_RESULT) {
1235                         /* browse layer/passes */
1236                         uiblock_layer_pass_arrow_buttons(block, RE_GetResult(RE_GetRender(scene->id.name)), iuser, imagechanged);
1237                 }
1238                 return;
1239         }
1240         
1241         /* the main ima source types */
1242         if(ima) {
1243 // XXX          uiSetButLock(ima->id.lib!=NULL, ERROR_LIBDATA_MESSAGE);
1244                 uiBlockBeginAlign(block);
1245                 uiBlockSetFunc(block, image_src_change_cb, ima, iuser);
1246                 uiDefButS(block, ROW, imagechanged, "Still",            10, 180, 60, 20, &ima->source, 0.0, IMA_SRC_FILE, 0, 0, "Single Image file");
1247                 uiDefButS(block, ROW, imagechanged, "Movie",            70, 180, 60, 20, &ima->source, 0.0, IMA_SRC_MOVIE, 0, 0, "Movie file");
1248                 uiDefButS(block, ROW, imagechanged, "Sequence", 130, 180, 90, 20, &ima->source, 0.0, IMA_SRC_SEQUENCE, 0, 0, "Multiple Image files, as a sequence");
1249                 uiDefButS(block, ROW, imagechanged, "Generated",        220, 180, 90, 20, &ima->source, 0.0, IMA_SRC_GENERATED, 0, 0, "Generated Image");
1250                 uiBlockSetFunc(block, NULL, NULL, NULL);
1251         }
1252         else
1253                 uiDefBut(block, LABEL, 0, " ",                                  10, 180, 300, 20, 0, 0, 0, 0, 0, "");   /* for align in panel */
1254                                  
1255          /* Browse */
1256          IMAnames_to_pupstring(&strp, NULL, NULL, &(CTX_data_main(C)->image), NULL, &iuser->menunr);
1257          
1258          uiBlockBeginAlign(block);
1259          but= uiDefButS(block, MENU, imagechanged, strp,                10,155,23,20, &iuser->menunr, 0, 0, 0, 0, "Selects an existing Image or Movie");
1260          uiButSetFunc(but, image_browse_cb, ima_pp, iuser);
1261          
1262          MEM_freeN(strp);
1263          
1264          /* name + options, or only load */
1265          if(ima) {
1266                  int drawpack= (ima->source!=IMA_SRC_SEQUENCE && ima->source!=IMA_SRC_MOVIE && ima->ok);
1267
1268                  but= uiDefBut(block, TEX, B_IDNAME, "IM:",             33, 155, 177, 20, ima->id.name+2, 0.0, 21.0, 0, 0, "Current Image Datablock name.");
1269                  uiButSetFunc(but, test_idbutton_cb, ima->id.name, NULL);
1270                  but= uiDefBut(block, BUT, imagechanged, "Reload",              210, 155, 60, 20, NULL, 0, 0, 0, 0, "Reloads Image or Movie");
1271                  uiButSetFunc(but, image_reload_cb, ima, iuser);
1272                  
1273                  but= uiDefIconBut(block, BUT, imagechanged, ICON_X,    270,155,20,20, 0, 0, 0, 0, 0, "Unlink Image block");
1274                  uiButSetFunc(but, image_unlink_cb, ima_pp, NULL);
1275                  sprintf(str, "%d", ima->id.us);
1276                  uiDefBut(block, BUT, B_NOP, str,                                       290,155,20,20, 0, 0, 0, 0, 0, "Only displays number of users of Image block");
1277                  
1278                  but= uiDefIconBut(block, BUT, imagechanged, ICON_FILESEL,      10, 135, 23, 20, 0, 0, 0, 0, 0, "Open Fileselect to load new Image");
1279                  uiButSetFunc(but, image_load_fs_cb, ima_pp, iuser);
1280                  but= uiDefBut(block, TEX, imagechanged, "",                            33,135,257+(drawpack?0:20),20, ima->name, 0.0, 239.0, 0, 0, "Image/Movie file name, change to load new");
1281                  uiButSetFunc(but, image_load_cb, ima_pp, iuser);
1282                  
1283                  if(drawpack) {
1284                          if (ima->packedfile) packdummy = 1;
1285                          else packdummy = 0;
1286                          but= uiDefIconButBitI(block, TOG, 1, redraw, ICON_PACKAGE, 290,135,20,20, &packdummy, 0, 0, 0, 0, "Toggles Packed status of this Image");
1287                          uiButSetFunc(but, image_pack_cb, ima, iuser);
1288                  }
1289                  
1290          }
1291          else {
1292                  but= uiDefBut(block, BUT, imagechanged, "Load",                33, 155, 100,20, NULL, 0, 0, 0, 0, "Load new Image of Movie");
1293                  uiButSetFunc(but, image_load_fs_cb, ima_pp, iuser);
1294          }
1295          uiBlockEndAlign(block);
1296          
1297          if(ima) {
1298                  ImBuf *ibuf= BKE_image_get_ibuf(ima, iuser);
1299                  
1300                  /* check for re-render, only buttons */
1301                  if(imagechanged==B_IMAGECHANGED) {
1302                          if(iuser->flag & IMA_ANIM_REFRESHED) {
1303                                  iuser->flag &= ~IMA_ANIM_REFRESHED;
1304                                  WM_event_add_notifier(C, NC_IMAGE, ima);
1305                          }
1306                  }
1307                  
1308                  /* multilayer? */
1309                  if(ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
1310                          uiblock_layer_pass_arrow_buttons(block, ima->rr, iuser, imagechanged);
1311                  }
1312                  else {
1313                          image_info(ima, ibuf, str);
1314                          uiDefBut(block, LABEL, 0, str,         10, 112, 300, 20, NULL, 1, 0, 0, 0, "");
1315                  }
1316                  
1317                  /* exception, let's do because we only use this panel 3 times in blender... but not real good code! */
1318                  if( (FACESEL_PAINT_TEST) && sima && &sima->iuser==iuser)
1319                          return;
1320                  /* left side default per-image options, right half the additional options */
1321                  
1322                  /* fields */
1323                  uiBlockBeginAlign(block);
1324                  but= uiDefButBitS(block, TOGBUT, IMA_FIELDS, imagechanged, "Fields",   10, 70, 65, 20, &ima->flag, 0, 0, 0, 0, "Click to enable use of fields in Image");
1325                  uiButSetFunc(but, image_field_test, ima, iuser);
1326                  uiDefButBitS(block, TOGBUT, IMA_STD_FIELD, B_NOP, "Odd",                       75, 70, 45, 20, &ima->flag, 0, 0, 0, 0, "Standard Field Toggle");
1327                  
1328                  uiBlockSetFunc(block, image_reload_cb, ima, iuser);
1329                  uiDefButBitS(block, TOGBUT, IMA_ANTIALI, B_NOP, "Anti",                        10, 50, 45, 20, &ima->flag, 0, 0, 0, 0, "Toggles Image anti-aliasing, only works with solid colors");
1330                  uiDefButBitS(block, TOGBUT, IMA_DO_PREMUL, imagechanged, "Premul",             55, 50, 65, 20, &ima->flag, 0, 0, 0, 0, "Toggles premultiplying alpha");
1331                  uiBlockEndAlign(block);
1332                  
1333                  if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
1334                          sprintf(str, "(%d) Frames:", iuser->framenr);
1335                          
1336                          uiBlockBeginAlign(block);
1337                          uiBlockSetFunc(block, image_user_change, iuser, NULL);
1338                          uiDefButBitS(block, TOG, IMA_ANIM_ALWAYS, B_NOP, "Auto Refresh",       120, 70, 190, 20, &iuser->flag, 0, 0, 0, 0, "Always refresh Image on frame changes");
1339                          
1340                          if(ima->anim) {
1341                                  uiDefButI(block, NUM, imagechanged, str,               120, 50,170, 20, &iuser->frames, 0.0, MAXFRAMEF, 0, 0, "Sets the number of images of a movie to use");
1342                                  but= uiDefBut(block, BUT, redraw, "<",         290, 50, 20, 20, 0, 0, 0, 0, 0, "Copies number of frames in movie file to Frames: button");
1343                                  uiButSetFunc(but, set_frames_cb, ima, iuser);
1344                          }
1345                          else 
1346                                  uiDefButI(block, NUM, imagechanged, str,               120, 50,190, 20, &iuser->frames, 0.0, MAXFRAMEF, 0, 0, "Sets the number of images of a movie to use");
1347                          
1348                          uiDefButI(block, NUM, imagechanged, "Offs:",   120,30,100,20, &iuser->offset, -MAXFRAMEF, MAXFRAMEF, 0, 0, "Offsets the number of the frame to use in the animation");
1349                          uiDefButS(block, NUM, imagechanged, "Fie/Ima:",        220,30,90,20, &iuser->fie_ima, 1.0, 200.0, 0, 0, "The number of fields per rendered frame (2 fields is 1 image)");
1350                          
1351                          uiDefButI(block, NUM, imagechanged, "StartFr:",        120,10,100,20, &iuser->sfra, 1.0, MAXFRAMEF, 0, 0, "Sets the global starting frame of the movie");
1352                          uiDefButS(block, TOG, imagechanged, "Cyclic",  220,10,90,20, &iuser->cycl, 0.0, 1.0, 0, 0, "Cycle the images in the movie");
1353                          
1354                          uiBlockSetFunc(block, NULL, iuser, NULL);
1355                  }
1356                  else if(ima->source==IMA_SRC_GENERATED) {
1357                          
1358                          uiBlockBeginAlign(block);
1359                          uiBlockSetFunc(block, image_generated_change_cb, ima, iuser);
1360                          uiDefButS(block, NUM, imagechanged, "SizeX:",  120,70,100,20, &ima->gen_x, 1.0, 5000.0, 0, 0, "Image size x");
1361                          uiDefButS(block, NUM, imagechanged, "SizeY:",  220,70,90,20, &ima->gen_y, 1.0, 5000.0, 0, 0, "Image size y");
1362                          uiDefButS(block, TOGBUT, imagechanged, "UV Test grid",120,50,190,20, &ima->gen_type, 0.0, 1.0, 0, 0, "");
1363                          uiBlockSetFunc(block, NULL, NULL, NULL);
1364                  }
1365          }
1366          uiBlockEndAlign(block);
1367 }       
1368
1369 void uiTemplateImageLayers(uiLayout *layout, bContext *C, Image *ima, ImageUser *iuser)
1370 {
1371         uiBlock *block= uiLayoutFreeBlock(layout);
1372         Scene *scene= CTX_data_scene(C);
1373         RenderResult *rr;
1374
1375         /* render layers and passes */
1376         if(ima && iuser) {
1377                 rr= BKE_image_get_renderresult(scene, ima);
1378
1379                 if(rr) {
1380                         uiBlockBeginAlign(block);
1381                         uiblock_layer_pass_buttons(block, rr, iuser, 0, 0, 0, 160);
1382                         uiBlockEndAlign(block);
1383                 }
1384         }
1385 }
1386
1387 static void image_panel_properties(const bContext *C, Panel *pa)
1388 {
1389         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1390         uiBlock *block;
1391         
1392         block= uiLayoutFreeBlock(pa->layout);
1393         uiBlockSetHandleFunc(block, do_image_panel_events, NULL);
1394
1395         /* note, it draws no bottom half in facemode, for vertex buttons */
1396         ED_image_uiblock_panel(C, block, &sima->image, &sima->iuser, B_REDR, B_REDR);
1397         image_editvertex_buts(C, block);
1398 }       
1399
1400 void image_buttons_register(ARegionType *art)
1401 {
1402         PanelType *pt;
1403
1404         pt= MEM_callocN(sizeof(PanelType), "spacetype image panel properties");
1405         strcpy(pt->idname, "IMAGE_PT_properties");
1406         strcpy(pt->label, "Image Properties");
1407         pt->draw= image_panel_properties;
1408         BLI_addtail(&art->paneltypes, pt);
1409
1410         pt= MEM_callocN(sizeof(PanelType), "spacetype image panel paint");
1411         strcpy(pt->idname, "IMAGE_PT_paint");
1412         strcpy(pt->label, "Paint");
1413         pt->draw= image_panel_paint;
1414         pt->poll= image_panel_paint_poll;
1415         BLI_addtail(&art->paneltypes, pt);
1416
1417         pt= MEM_callocN(sizeof(PanelType), "spacetype image panel paint color");
1418         strcpy(pt->idname, "IMAGE_PT_paint_color");
1419         strcpy(pt->label, "Paint Color");
1420         pt->draw= image_panel_paintcolor;
1421         pt->poll= image_panel_paint_poll;
1422         BLI_addtail(&art->paneltypes, pt);
1423
1424         pt= MEM_callocN(sizeof(PanelType), "spacetype image panel curves");
1425         strcpy(pt->idname, "IMAGE_PT_curves");
1426         strcpy(pt->label, "Curves");
1427         pt->draw= image_panel_curves;
1428         BLI_addtail(&art->paneltypes, pt);
1429 }
1430
1431 static int image_properties(bContext *C, wmOperator *op)
1432 {
1433         ScrArea *sa= CTX_wm_area(C);
1434         ARegion *ar= image_has_buttons_region(sa);
1435         
1436         if(ar) {
1437                 ar->flag ^= RGN_FLAG_HIDDEN;
1438                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
1439                 
1440                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
1441                 ED_area_tag_redraw(sa);
1442         }
1443         return OPERATOR_FINISHED;
1444 }
1445
1446 void IMAGE_OT_properties(wmOperatorType *ot)
1447 {
1448         ot->name= "Properties";
1449         ot->idname= "IMAGE_OT_properties";
1450         
1451         ot->exec= image_properties;
1452         ot->poll= ED_operator_image_active;
1453         
1454         /* flags */
1455         ot->flag= 0;
1456 }
1457
1458
1459