2.6 Various code cleanup:
[blender.git] / source / blender / editors / space_image / image_buttons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_image/image_buttons.c
27  *  \ingroup spimage
28  */
29
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_meshdata_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_node_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_editVert.h"
45 #include "BLI_rand.h"
46 #include "BLI_utildefines.h"
47
48 #include "BKE_colortools.h"
49 #include "BKE_context.h"
50 #include "BKE_customdata.h"
51 #include "BKE_image.h"
52 #include "BKE_mesh.h"
53 #include "BKE_node.h"
54 #include "BKE_screen.h"
55
56 #include "RE_pipeline.h"
57
58 #include "IMB_imbuf.h"
59 #include "IMB_imbuf_types.h"
60
61 #include "ED_gpencil.h"
62 #include "ED_image.h"
63 #include "ED_screen.h"
64
65 #include "RNA_access.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "UI_interface.h"
71 #include "UI_resources.h"
72
73 #include "image_intern.h"
74
75 #define B_REDR                          1
76 #define B_IMAGECHANGED          2
77 #define B_NOP                           0
78 #define B_TWINANIM                      5
79 #define B_SIMAGETILE            6
80 #define B_IDNAME                        10
81 #define B_FACESEL_PAINT_TEST    11
82 #define B_SIMA_RECORD           12
83 #define B_SIMA_PLAY                     13
84
85 #define B_SIMANOTHING           16
86 #define B_SIMABRUSHCHANGE       17      
87 #define B_SIMABRUSHBROWSE       18
88 #define B_SIMABRUSHLOCAL        19
89 #define B_SIMABRUSHDELETE       20
90 #define B_KEEPDATA                      21
91 #define B_SIMABTEXBROWSE        22
92 #define B_SIMABTEXDELETE        23
93 #define B_VPCOLSLI                      24
94 #define B_SIMACLONEBROWSE       25
95 #define B_SIMACLONEDELETE       26
96
97 /* proto */
98
99 static void image_info(Scene *scene, ImageUser *iuser, Image *ima, ImBuf *ibuf, char *str)
100 {
101         int ofs= 0;
102
103         str[0]= 0;
104         
105         if(ima==NULL) return;
106
107         if(ibuf==NULL) {
108                 ofs+= sprintf(str, "Can't Load Image");
109         }
110         else {
111                 if(ima->source==IMA_SRC_MOVIE) {
112                         ofs+= sprintf(str, "Movie");
113                         if(ima->anim)
114                                 ofs+= sprintf(str+ofs, "%d frs", IMB_anim_get_duration(ima->anim, IMB_TC_RECORD_RUN));
115                 }
116                 else
117                         ofs+= sprintf(str, "Image");
118
119                 ofs+= sprintf(str+ofs, ": size %d x %d,", ibuf->x, ibuf->y);
120
121                 if(ibuf->rect_float) {
122                         if(ibuf->channels!=4) {
123                                 ofs+= sprintf(str+ofs, "%d float channel(s)", ibuf->channels);
124                         }
125                         else if(ibuf->planes == R_IMF_PLANES_RGBA)
126                                 ofs+= sprintf(str+ofs, " RGBA float");
127                         else
128                                 ofs+= sprintf(str+ofs, " RGB float");
129                 }
130                 else {
131                         if(ibuf->planes == R_IMF_PLANES_RGBA)
132                                 ofs+= sprintf(str+ofs, " RGBA byte");
133                         else
134                                 ofs+= sprintf(str+ofs, " RGB byte");
135                 }
136                 if(ibuf->zbuf || ibuf->zbuf_float)
137                         ofs+= sprintf(str+ofs, " + Z");
138
139                 if(ima->source==IMA_SRC_SEQUENCE) {
140                         char *file= BLI_last_slash(ibuf->name);
141                         if(file==NULL)  file= ibuf->name;
142                         else                    file++;
143                         ofs+= sprintf(str+ofs, ", %s", file);
144                 }
145         }
146
147         /* the frame number, even if we cant */
148         if(ima->source==IMA_SRC_SEQUENCE) {
149                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
150                 const int framenr= BKE_image_user_get_frame(iuser, CFRA, 0);
151                 ofs+= sprintf(str+ofs, ", Frame: %d", framenr);
152         }
153
154         (void)ofs;
155 }
156
157 /* gets active viewer user */
158 struct ImageUser *ntree_get_active_iuser(bNodeTree *ntree)
159 {
160         bNode *node;
161         
162         if(ntree)
163                 for(node= ntree->nodes.first; node; node= node->next)
164                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
165                                 if(node->flag & NODE_DO_OUTPUT)
166                                         return node->storage;
167         return NULL;
168 }
169
170
171 /* ************ panel stuff ************* */
172
173 /* is used for both read and write... */
174
175 static int image_panel_poll(const bContext *C, PanelType *UNUSED(pt))
176 {
177         SpaceImage *sima= CTX_wm_space_image(C);
178         ImBuf *ibuf;
179         void *lock;
180         int result;
181
182         ibuf= ED_space_image_acquire_buffer(sima, &lock);
183         result= ibuf && ibuf->rect_float;
184         ED_space_image_release_buffer(sima, lock);
185         
186         return result;
187 }
188
189 static void image_panel_curves(const bContext *C, Panel *pa)
190 {
191         bScreen *sc= CTX_wm_screen(C);
192         SpaceImage *sima= CTX_wm_space_image(C);
193         ImBuf *ibuf;
194         PointerRNA simaptr;
195         int levels;
196         void *lock;
197         
198         ibuf= ED_space_image_acquire_buffer(sima, &lock);
199         
200         if(ibuf) {
201                 if(sima->cumap==NULL)
202                         sima->cumap= curvemapping_add(4, 0.0f, 0.0f, 1.0f, 1.0f);
203
204                 /* curvemap black/white levels only works for RGBA */
205                 levels= (ibuf->channels==4);
206
207                 RNA_pointer_create(&sc->id, &RNA_SpaceImageEditor, sima, &simaptr);
208                 uiTemplateCurveMapping(pa->layout, &simaptr, "curve", 'c', levels, 0);
209         }
210
211         ED_space_image_release_buffer(sima, lock);
212 }
213
214 #if 0
215 /* 0: disable preview 
216    otherwise refresh preview
217  
218    XXX if you put this back, also check XXX in image_main_area_draw() */
219 */
220 void image_preview_event(int event)
221 {
222         int exec= 0;
223         
224         if(event==0) {
225                 G.scene->r.scemode &= ~R_COMP_CROP;
226                 exec= 1;
227         }
228         else {
229                 if(image_preview_active(curarea, NULL, NULL)) {
230                         G.scene->r.scemode |= R_COMP_CROP;
231                         exec= 1;
232                 }
233                 else
234                         G.scene->r.scemode &= ~R_COMP_CROP;
235         }
236         
237         if(exec && G.scene->nodetree) {
238                 /* should work when no node editor in screen..., so we execute right away */
239                 
240                 ntreeCompositTagGenerators(G.scene->nodetree);
241
242                 G.afbreek= 0;
243                 G.scene->nodetree->timecursor= set_timecursor;
244                 G.scene->nodetree->test_break= blender_test_break;
245                 
246                 BIF_store_spare();
247                 
248                 ntreeCompositExecTree(G.scene->nodetree, &G.scene->r, 1);       /* 1 is do_previews */
249                 
250                 G.scene->nodetree->timecursor= NULL;
251                 G.scene->nodetree->test_break= NULL;
252                 
253                 scrarea_do_windraw(curarea);
254                 waitcursor(0);
255                 
256                 WM_event_add_notifier(C, NC_IMAGE, ima_v);
257         }       
258 }
259
260
261 /* nothing drawn here, we use it to store values */
262 static void preview_cb(struct ScrArea *sa, struct uiBlock *block)
263 {
264         SpaceImage *sima= sa->spacedata.first;
265         rctf dispf;
266         rcti *disprect= &G.scene->r.disprect;
267         int winx= (G.scene->r.size*G.scene->r.xsch)/100;
268         int winy= (G.scene->r.size*G.scene->r.ysch)/100;
269         int mval[2];
270         
271         if(G.scene->r.mode & R_BORDER) {
272                 winx*= (G.scene->r.border.xmax - G.scene->r.border.xmin);
273                 winy*= (G.scene->r.border.ymax - G.scene->r.border.ymin);
274         }
275         
276         /* while dragging we need to update the rects, otherwise it doesn't end with correct one */
277
278         BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
279         ui_graphics_to_window_rct(sa->win, &dispf, disprect);
280         
281         /* correction for gla draw */
282         BLI_translate_rcti(disprect, -curarea->winrct.xmin, -curarea->winrct.ymin);
283         
284         calc_image_view(sima, 'p');
285 //      printf("winrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
286         /* map to image space coordinates */
287         mval[0]= disprect->xmin; mval[1]= disprect->ymin;
288         areamouseco_to_ipoco(v2d, mval, &dispf.xmin, &dispf.ymin);
289         mval[0]= disprect->xmax; mval[1]= disprect->ymax;
290         areamouseco_to_ipoco(v2d, mval, &dispf.xmax, &dispf.ymax);
291         
292         /* map to render coordinates */
293         disprect->xmin= dispf.xmin;
294         disprect->xmax= dispf.xmax;
295         disprect->ymin= dispf.ymin;
296         disprect->ymax= dispf.ymax;
297         
298         CLAMP(disprect->xmin, 0, winx);
299         CLAMP(disprect->xmax, 0, winx);
300         CLAMP(disprect->ymin, 0, winy);
301         CLAMP(disprect->ymax, 0, winy);
302 //      printf("drawrct %d %d %d %d\n", disprect->xmin, disprect->ymin,disprect->xmax, disprect->ymax);
303
304 }
305
306 static int is_preview_allowed(ScrArea *cur)
307 {
308         SpaceImage *sima= cur->spacedata.first;
309         ScrArea *sa;
310
311         /* check if another areawindow has preview set */
312         for(sa=G.curscreen->areabase.first; sa; sa= sa->next) {
313                 if(sa!=cur && sa->spacetype==SPACE_IMAGE) {
314                         if(image_preview_active(sa, NULL, NULL))
315                                 return 0;
316                 }
317         }
318         /* check image type */
319         if(sima->image==NULL || sima->image->type!=IMA_TYPE_COMPOSITE)
320                 return 0;
321         
322         return 1;
323 }
324
325
326 static void image_panel_preview(ScrArea *sa, short cntrl)       // IMAGE_HANDLER_PREVIEW
327 {
328         uiBlock *block;
329         SpaceImage *sima= sa->spacedata.first;
330         int ofsx, ofsy;
331         
332         if(is_preview_allowed(sa)==0) {
333                 rem_blockhandler(sa, IMAGE_HANDLER_PREVIEW);
334                 G.scene->r.scemode &= ~R_COMP_CROP;     /* quite weak */
335                 return;
336         }
337         
338         block= uiBeginBlock(C, ar, "image_panel_preview", UI_EMBOSS);
339         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
340         uiSetPanelHandler(IMAGE_HANDLER_PREVIEW);  // for close and esc
341         
342         ofsx= -150+(sa->winx/2)/sima->blockscale;
343         ofsy= -100+(sa->winy/2)/sima->blockscale;
344         if(uiNewPanel(C, ar, block, "Preview", "Image", ofsx, ofsy, 300, 200)==0) return;
345         
346         uiBlockSetDrawExtraFunc(block, preview_cb);
347         
348 }
349 #endif
350
351
352 /* ********************* callbacks for standard image buttons *************** */
353
354 static char *slot_menu(void)
355 {
356         char *str;
357         int a, slot;
358         
359         str= MEM_callocN(IMA_MAX_RENDER_SLOT*32, "menu slots");
360         
361         strcpy(str, "Slot %t");
362         a= strlen(str);
363
364         for(slot=0; slot<IMA_MAX_RENDER_SLOT; slot++)
365                 a += sprintf(str+a, "|Slot %d %%x%d", slot+1, slot);
366         
367         return str;
368 }
369
370 /* TODO, curlay should be removed? */
371 static char *layer_menu(RenderResult *rr, short *UNUSED(curlay))
372 {
373         RenderLayer *rl;
374         int len= 64 + 32*BLI_countlist(&rr->layers);
375         short a, nr= 0;
376         char *str= MEM_callocN(len, "menu layers");
377         
378         strcpy(str, "Layer %t");
379         a= strlen(str);
380         
381         /* compo result */
382         if(rr->rectf) {
383                 a+= sprintf(str+a, "|Composite %%x0");
384                 nr= 1;
385         }
386         for(rl= rr->layers.first; rl; rl= rl->next, nr++) {
387                 a+= sprintf(str+a, "|%s %%x%d", rl->name, nr);
388         }
389         
390         /* no curlay clip here, on render (redraws) the amount of layers can be 1 fir single-layer render */
391         
392         return str;
393 }
394
395 /* rl==NULL means composite result */
396 static char *pass_menu(RenderLayer *rl, short *curpass)
397 {
398         RenderPass *rpass;
399         int len= 64 + 32*(rl?BLI_countlist(&rl->passes):1);
400         short a, nr= 0;
401         char *str= MEM_callocN(len, "menu layers");
402         
403         strcpy(str, "Pass %t");
404         a= strlen(str);
405         
406         /* rendered results don't have a Combined pass */
407         if(rl==NULL || rl->rectf) {
408                 a+= sprintf(str+a, "|Combined %%x0");
409                 nr= 1;
410         }
411         
412         if(rl)
413                 for(rpass= rl->passes.first; rpass; rpass= rpass->next, nr++)
414                         a+= sprintf(str+a, "|%s %%x%d", rpass->name, nr);
415         
416         if(*curpass >= nr)
417                 *curpass= 0;
418         
419         return str;
420 }
421
422 static void set_frames_cb(bContext *C, void *ima_v, void *iuser_v)
423 {
424         Scene *scene= CTX_data_scene(C);
425         Image *ima= ima_v;
426         ImageUser *iuser= iuser_v;
427         
428         if(ima->anim) {
429                 iuser->frames = IMB_anim_get_duration(ima->anim, IMB_TC_RECORD_RUN);
430                 BKE_image_user_calc_frame(iuser, scene->r.cfra, 0);
431         }
432 }
433
434 /* 5 layer button callbacks... */
435 static void image_multi_cb(bContext *C, void *rr_v, void *iuser_v) 
436 {
437         ImageUser *iuser= iuser_v;
438
439         BKE_image_multilayer_index(rr_v, iuser); 
440         WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
441 }
442 static void image_multi_inclay_cb(bContext *C, void *rr_v, void *iuser_v) 
443 {
444         RenderResult *rr= rr_v;
445         ImageUser *iuser= iuser_v;
446         int tot= BLI_countlist(&rr->layers) + (rr->rectf?1:0);  /* fake compo result layer */
447
448         if(iuser->layer<tot-1) {
449                 iuser->layer++;
450                 BKE_image_multilayer_index(rr, iuser); 
451                 WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
452         }
453 }
454 static void image_multi_declay_cb(bContext *C, void *rr_v, void *iuser_v) 
455 {
456         ImageUser *iuser= iuser_v;
457
458         if(iuser->layer>0) {
459                 iuser->layer--;
460                 BKE_image_multilayer_index(rr_v, iuser); 
461                 WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
462         }
463 }
464 static void image_multi_incpass_cb(bContext *C, void *rr_v, void *iuser_v) 
465 {
466         RenderResult *rr= rr_v;
467         ImageUser *iuser= iuser_v;
468         RenderLayer *rl= BLI_findlink(&rr->layers, iuser->layer);
469
470         if(rl) {
471                 int tot= BLI_countlist(&rl->passes) + (rl->rectf?1:0);  /* builtin render result has no combined pass in list */
472                 if(iuser->pass<tot-1) {
473                         iuser->pass++;
474                         BKE_image_multilayer_index(rr, iuser); 
475                         WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
476                 }
477         }
478 }
479 static void image_multi_decpass_cb(bContext *C, void *rr_v, void *iuser_v) 
480 {
481         ImageUser *iuser= iuser_v;
482
483         if(iuser->pass>0) {
484                 iuser->pass--;
485                 BKE_image_multilayer_index(rr_v, iuser); 
486                 WM_event_add_notifier(C, NC_IMAGE|ND_DRAW, NULL);
487         }
488 }
489
490 #if 0
491 static void image_freecache_cb(bContext *C, void *ima_v, void *unused) 
492 {
493         Scene *scene= CTX_data_scene(C);
494         BKE_image_free_anim_ibufs(ima_v, scene->r.cfra);
495         WM_event_add_notifier(C, NC_IMAGE, ima_v);
496 }
497 #endif
498
499 #if 0
500 static void image_user_change(bContext *C, void *iuser_v, void *unused)
501 {
502         Scene *scene= CTX_data_scene(C);
503         BKE_image_user_calc_imanr(iuser_v, scene->r.cfra, 0);
504 }
505 #endif
506
507 static void uiblock_layer_pass_buttons(uiLayout *layout, RenderResult *rr, ImageUser *iuser, int w, short *render_slot)
508 {
509         uiBlock *block= uiLayoutGetBlock(layout);
510         uiBut *but;
511         RenderLayer *rl= NULL;
512         int wmenu1, wmenu2, wmenu3;
513         char *strp;
514
515         uiLayoutRow(layout, 1);
516
517         /* layer menu is 1/3 larger than pass */
518         wmenu1= (2*w)/5;
519         wmenu2= (3*w)/5;
520         wmenu3= (3*w)/6;
521         
522         /* menu buts */
523         if(render_slot) {
524                 strp= slot_menu();
525                 but= uiDefButS(block, MENU, 0, strp,                                    0, 0, wmenu1, UI_UNIT_Y, render_slot, 0,0,0,0, "Select Slot");
526                 uiButSetFunc(but, image_multi_cb, rr, iuser);
527                 MEM_freeN(strp);
528         }
529
530         if(rr) {
531                 strp= layer_menu(rr, &iuser->layer);
532                 but= uiDefButS(block, MENU, 0, strp,                                    0, 0, wmenu2, UI_UNIT_Y, &iuser->layer, 0,0,0,0, "Select Layer");
533                 uiButSetFunc(but, image_multi_cb, rr, iuser);
534                 MEM_freeN(strp);
535                 
536                 rl= BLI_findlink(&rr->layers, iuser->layer - (rr->rectf?1:0)); /* fake compo layer, return NULL is meant to be */
537                 strp= pass_menu(rl, &iuser->pass);
538                 but= uiDefButS(block, MENU, 0, strp,                                    0, 0, wmenu3, UI_UNIT_Y, &iuser->pass, 0,0,0,0, "Select Pass");
539                 uiButSetFunc(but, image_multi_cb, rr, iuser);
540                 MEM_freeN(strp);        
541         }
542 }
543
544 static void uiblock_layer_pass_arrow_buttons(uiLayout *layout, RenderResult *rr, ImageUser *iuser, short *render_slot)
545 {
546         uiBlock *block= uiLayoutGetBlock(layout);
547         uiLayout *row;
548         uiBut *but;
549         const float dpi_fac= UI_DPI_FAC;
550         
551         row= uiLayoutRow(layout, 1);
552
553         if(rr==NULL || iuser==NULL)
554                 return;
555         if(rr->layers.first==NULL) {
556                 uiItemL(row, "No Layers in Render Result", ICON_NONE);
557                 return;
558         }
559
560         /* decrease, increase arrows */
561         but= uiDefIconBut(block, BUT, 0, ICON_TRIA_LEFT,        0,0,17,20, NULL, 0, 0, 0, 0, "Previous Layer");
562         uiButSetFunc(but, image_multi_declay_cb, rr, iuser);
563         but= uiDefIconBut(block, BUT, 0, ICON_TRIA_RIGHT,       0,0,18,20, NULL, 0, 0, 0, 0, "Next Layer");
564         uiButSetFunc(but, image_multi_inclay_cb, rr, iuser);
565
566         uiblock_layer_pass_buttons(row, rr, iuser, 230 * dpi_fac, render_slot);
567
568         /* decrease, increase arrows */
569         but= uiDefIconBut(block, BUT, 0, ICON_TRIA_LEFT,        0,0,17,20, NULL, 0, 0, 0, 0, "Previous Pass");
570         uiButSetFunc(but, image_multi_decpass_cb, rr, iuser);
571         but= uiDefIconBut(block, BUT, 0, ICON_TRIA_RIGHT,       0,0,18,20, NULL, 0, 0, 0, 0, "Next Pass");
572         uiButSetFunc(but, image_multi_incpass_cb, rr, iuser);
573
574         uiBlockEndAlign(block);
575 }
576
577 // XXX HACK!
578 // static int packdummy=0;
579
580 typedef struct RNAUpdateCb {
581         PointerRNA ptr;
582         PropertyRNA *prop;
583         ImageUser *iuser;
584 } RNAUpdateCb;
585
586 static void rna_update_cb(bContext *C, void *arg_cb, void *UNUSED(arg))
587 {
588         RNAUpdateCb *cb= (RNAUpdateCb*)arg_cb;
589
590         /* ideally this would be done by RNA itself, but there we have
591            no image user available, so we just update this flag here */
592         cb->iuser->ok= 1;
593
594         /* we call update here on the pointer property, this way the
595            owner of the image pointer can still define it's own update
596            and notifier */
597         RNA_property_update(C, &cb->ptr, cb->prop);
598 }
599
600 void uiTemplateImage(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, PointerRNA *userptr, int compact)
601 {
602         PropertyRNA *prop;
603         PointerRNA imaptr;
604         RNAUpdateCb *cb;
605         Image *ima;
606         ImageUser *iuser;
607         ImBuf *ibuf;
608         Scene *scene= CTX_data_scene(C);
609         uiLayout *row, *split, *col;
610         uiBlock *block;
611         uiBut *but;
612         char str[128];
613         void *lock;
614
615         if(!ptr->data)
616                 return;
617
618         prop= RNA_struct_find_property(ptr, propname);
619         if(!prop) {
620                 printf("%s: property not found: %s.%s\n",
621                        __func__, RNA_struct_identifier(ptr->type), propname);
622                 return;
623         }
624
625         if(RNA_property_type(prop) != PROP_POINTER) {
626                 printf("%s: expected pointer property for %s.%s\n",
627                        __func__, RNA_struct_identifier(ptr->type), propname);
628                 return;
629         }
630
631         block= uiLayoutGetBlock(layout);
632
633         imaptr= RNA_property_pointer_get(ptr, prop);
634         ima= imaptr.data;
635         iuser= userptr->data;
636
637         cb= MEM_callocN(sizeof(RNAUpdateCb), "RNAUpdateCb");
638         cb->ptr= *ptr;
639         cb->prop= prop;
640         cb->iuser= iuser;
641
642         uiLayoutSetContextPointer(layout, "edit_image", &imaptr);
643
644         if(!compact)
645                 uiTemplateID(layout, C, ptr, propname, "IMAGE_OT_new", "IMAGE_OT_open", NULL);
646
647         if(ima) {
648                 uiBlockSetNFunc(block, rna_update_cb, MEM_dupallocN(cb), NULL);
649
650                 if(ima->source == IMA_SRC_VIEWER) {
651                         ibuf= BKE_image_acquire_ibuf(ima, iuser, &lock);
652                         image_info(scene, iuser, ima, ibuf, str);
653                         BKE_image_release_ibuf(ima, lock);
654
655                         uiItemL(layout, ima->id.name+2, ICON_NONE);
656                         uiItemL(layout, str, ICON_NONE);
657
658                         if(ima->type==IMA_TYPE_COMPOSITE) {
659                                 // XXX not working yet
660 #if 0
661                                 iuser= ntree_get_active_iuser(scene->nodetree);
662                                 if(iuser) {
663                                         uiBlockBeginAlign(block);
664                                         uiDefIconTextBut(block, BUT, B_SIMA_RECORD, ICON_REC, "Record", 10,120,100,20, 0, 0, 0, 0, 0, "");
665                                         uiDefIconTextBut(block, BUT, B_SIMA_PLAY, ICON_PLAY, "Play",    110,120,100,20, 0, 0, 0, 0, 0, "");
666                                         but= uiDefBut(block, BUT, B_NOP, "Free Cache",  210,120,100,20, 0, 0, 0, 0, 0, "");
667                                         uiButSetFunc(but, image_freecache_cb, ima, NULL);
668                                         
669                                         if(iuser->frames)
670                                                 sprintf(str, "(%d) Frames:", iuser->framenr);
671                                         else strcpy(str, "Frames:");
672                                         uiBlockBeginAlign(block);
673                                         uiDefButI(block, NUM, imagechanged, str,                10, 90,150, 20, &iuser->frames, 0.0, MAXFRAMEF, 0, 0, "Number of images of a movie to use");
674                                         uiDefButI(block, NUM, imagechanged, "StartFr:", 160,90,150,20, &iuser->sfra, 1.0, MAXFRAMEF, 0, 0, "Global starting frame of the movie");
675                                 }
676 #endif
677                         }
678                         else if(ima->type==IMA_TYPE_R_RESULT) {
679                                 /* browse layer/passes */
680                                 Render *re= RE_GetRender(scene->id.name);
681                                 RenderResult *rr= RE_AcquireResultRead(re);
682                                 uiblock_layer_pass_arrow_buttons(layout, rr, iuser, &ima->render_slot);
683                                 RE_ReleaseResult(re);
684                         }
685                 }
686                 else {
687                         uiItemR(layout, &imaptr, "source", 0, NULL, ICON_NONE);
688
689                         if(ima->source != IMA_SRC_GENERATED) {
690                                 row= uiLayoutRow(layout, 1);
691                                 if (ima->packedfile)
692                                         uiItemO(row, "", ICON_PACKAGE, "image.unpack");
693                                 else
694                                         uiItemO(row, "", ICON_UGLYPACKAGE, "image.pack");
695                                 
696                                 row= uiLayoutRow(row, 0);
697                                 uiLayoutSetEnabled(row, ima->packedfile==NULL);
698                                 uiItemR(row, &imaptr, "filepath", 0, "", ICON_NONE);
699                                 uiItemO(row, "", ICON_FILE_REFRESH, "image.reload");
700                         }
701
702                         // XXX what was this for?
703 #if 0
704                          /* check for re-render, only buttons */
705                         if(imagechanged==B_IMAGECHANGED) {
706                                 if(iuser->flag & IMA_ANIM_REFRESHED) {
707                                         iuser->flag &= ~IMA_ANIM_REFRESHED;
708                                         WM_event_add_notifier(C, NC_IMAGE, ima);
709                                 }
710                         }
711 #endif
712
713                         /* multilayer? */
714                         if(ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
715                                 uiblock_layer_pass_arrow_buttons(layout, ima->rr, iuser, NULL);
716                         }
717                         else if(ima->source != IMA_SRC_GENERATED) {
718                                 if(compact == 0) {
719                                         ibuf= BKE_image_acquire_ibuf(ima, iuser, &lock);
720                                         image_info(scene, iuser, ima, ibuf, str);
721                                         BKE_image_release_ibuf(ima, lock);
722                                         uiItemL(layout, str, ICON_NONE);
723                                 }
724                         }
725                         
726                         if(ima->source != IMA_SRC_GENERATED) {
727                                 if(compact == 0) { /* background image view doesnt need these */
728                                         uiItemS(layout);
729
730                                         split= uiLayoutSplit(layout, 0, 0);
731
732                                         col= uiLayoutColumn(split, 0);
733                                         uiItemR(col, &imaptr, "use_fields", 0, NULL, ICON_NONE);
734                                         row= uiLayoutRow(col, 0);
735                                         uiLayoutSetActive(row, RNA_boolean_get(&imaptr, "use_fields"));
736                                         uiItemR(row, &imaptr, "field_order", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
737                                         
738                                         uiItemR(split, &imaptr, "use_premultiply", 0, NULL, ICON_NONE);
739                                 }
740                         }
741
742                         if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
743                                 uiItemS(layout);
744                                 
745                                 split= uiLayoutSplit(layout, 0, 0);
746
747                                 col= uiLayoutColumn(split, 0);
748                                  
749                                 sprintf(str, "(%d) Frames", iuser->framenr);
750                                 uiItemR(col, userptr, "frame_duration", 0, str, ICON_NONE);
751                                 if(ima->anim) {
752                                         block= uiLayoutGetBlock(col);
753                                         but= uiDefBut(block, BUT, 0, "Match Movie Length", 0, 0, UI_UNIT_X*2, UI_UNIT_Y, NULL, 0, 0, 0, 0, "Set the number of frames to match the movie or sequence");
754                                         uiButSetFunc(but, set_frames_cb, ima, iuser);
755                                 }
756
757                                 uiItemR(col, userptr, "frame_start", 0, "Start", ICON_NONE);
758                                 uiItemR(col, userptr, "frame_offset", 0, NULL, ICON_NONE);
759
760                                 col= uiLayoutColumn(split, 0);
761                                 row= uiLayoutRow(col, 0);
762                                 uiLayoutSetActive(row, RNA_boolean_get(&imaptr, "use_fields"));
763                                 uiItemR(row, userptr, "fields_per_frame", 0, "Fields", ICON_NONE);
764                                 uiItemR(col, userptr, "use_auto_refresh", 0, NULL, ICON_NONE);
765                                 uiItemR(col, userptr, "use_cyclic", 0, NULL, ICON_NONE);
766                         }
767                         else if(ima->source==IMA_SRC_GENERATED) {
768                                 split= uiLayoutSplit(layout, 0, 0);
769
770                                 col= uiLayoutColumn(split, 1);
771                                 uiItemR(col, &imaptr, "generated_width", 0, "X", ICON_NONE);
772                                 uiItemR(col, &imaptr, "generated_height", 0, "Y", ICON_NONE);
773                                 uiItemR(col, &imaptr, "use_generated_float", 0, NULL, ICON_NONE);
774
775                                 uiItemR(split, &imaptr, "generated_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
776                         }
777
778                                         }
779
780                 uiBlockSetNFunc(block, NULL, NULL, NULL);
781         }
782
783         MEM_freeN(cb);
784 }
785
786 void uiTemplateImageSettings(uiLayout *layout, PointerRNA *imfptr)
787 {
788         ImageFormatData *imf= imfptr->data;
789         ID *id= imfptr->id.data;
790         const int depth_ok= BKE_imtype_valid_depths(imf->imtype);
791         /* some settings depend on this being a scene thats rendered */
792         const short is_render_out= (id && GS(id->name) == ID_SCE);
793
794         uiLayout *col, *row;
795
796         col= uiLayoutColumn(layout, 0);
797
798         uiItemR(col, imfptr, "file_format", 0, "", ICON_NONE);
799         
800         row= uiLayoutRow(col, 0);
801         uiItemR(row, imfptr, "color_mode", UI_ITEM_R_EXPAND, "Color", ICON_NONE);
802
803         /* only display depth setting if multiple depths can be used */
804         if((ELEM6(depth_ok,
805                   R_IMF_CHAN_DEPTH_1,
806                   R_IMF_CHAN_DEPTH_8,
807                   R_IMF_CHAN_DEPTH_12,
808                   R_IMF_CHAN_DEPTH_16,
809                   R_IMF_CHAN_DEPTH_24,
810                   R_IMF_CHAN_DEPTH_32)) == 0)
811         {
812                 row= uiLayoutRow(col, 0);
813                 uiItemR(row, imfptr, "color_depth", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
814         }
815
816         if (BKE_imtype_supports_quality(imf->imtype)) {
817                 uiItemR(col, imfptr, "quality", 0, NULL, ICON_NONE);
818         }
819
820         if (BKE_imtype_supports_compress(imf->imtype)) {
821                 uiItemR(col, imfptr, "compression", 0, NULL, ICON_NONE);
822         }
823
824         if (ELEM(imf->imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
825                 uiItemR(col, imfptr, "exr_codec", 0, NULL, ICON_NONE);
826         }
827         
828         row= uiLayoutRow(col, 0);
829         if (BKE_imtype_supports_zbuf(imf->imtype)) {
830                 uiItemR(row, imfptr, "use_zbuffer", 0, NULL, ICON_NONE);
831         }
832
833         if (is_render_out && (imf->imtype == R_IMF_IMTYPE_OPENEXR)) {
834                 uiItemR(row, imfptr, "use_preview", 0, NULL, ICON_NONE);
835         }
836
837         if (imf->imtype == R_IMF_IMTYPE_JP2) {
838                 row= uiLayoutRow(col, 0);
839                 uiItemR(row, imfptr, "use_jpeg2k_cinema_preset", 0, NULL, ICON_NONE);
840                 uiItemR(row, imfptr, "use_jpeg2k_cinema_48", 0, NULL, ICON_NONE);
841                 
842                 uiItemR(col, imfptr, "use_jpeg2k_ycc", 0, NULL, ICON_NONE);
843         }
844
845         if (imf->imtype == R_IMF_IMTYPE_CINEON) {
846 #if 1
847                 uiItemL(col, "Hard coded Non-Linear, Gamma:1.0", ICON_NONE);
848 #else
849                 uiItemR(col, imfptr, "use_cineon_log", 0, NULL, ICON_NONE);
850                 uiItemR(col, imfptr, "cineon_black", 0, NULL, ICON_NONE);
851                 uiItemR(col, imfptr, "cineon_white", 0, NULL, ICON_NONE);
852                 uiItemR(col, imfptr, "cineon_gamma", 0, NULL, ICON_NONE);
853 #endif
854         }
855 }
856
857 void uiTemplateImageLayers(uiLayout *layout, bContext *C, Image *ima, ImageUser *iuser)
858 {
859         Scene *scene= CTX_data_scene(C);
860         RenderResult *rr;
861
862         /* render layers and passes */
863         if(ima && iuser) {
864                 const float dpi_fac= UI_DPI_FAC;
865                 rr= BKE_image_acquire_renderresult(scene, ima);
866                 uiblock_layer_pass_buttons(layout, rr, iuser, 160 * dpi_fac, (ima->type==IMA_TYPE_R_RESULT)? &ima->render_slot: NULL);
867                 BKE_image_release_renderresult(scene, ima);
868         }
869 }
870
871 void image_buttons_register(ARegionType *art)
872 {
873         PanelType *pt;
874
875         pt= MEM_callocN(sizeof(PanelType), "spacetype image panel curves");
876         strcpy(pt->idname, "IMAGE_PT_curves");
877         strcpy(pt->label, "Curves");
878         pt->draw= image_panel_curves;
879         pt->poll= image_panel_poll;
880         pt->flag |= PNL_DEFAULT_CLOSED;
881         BLI_addtail(&art->paneltypes, pt);
882         
883         pt= MEM_callocN(sizeof(PanelType), "spacetype image panel gpencil");
884         strcpy(pt->idname, "IMAGE_PT_gpencil");
885         strcpy(pt->label, "Grease Pencil");
886         pt->draw= gpencil_panel_standard;
887         BLI_addtail(&art->paneltypes, pt);
888 }
889
890 static int image_properties(bContext *C, wmOperator *UNUSED(op))
891 {
892         ScrArea *sa= CTX_wm_area(C);
893         ARegion *ar= image_has_buttons_region(sa);
894         
895         if(ar)
896                 ED_region_toggle_hidden(C, ar);
897
898         return OPERATOR_FINISHED;
899 }
900
901 void IMAGE_OT_properties(wmOperatorType *ot)
902 {
903         ot->name= "Properties";
904         ot->idname= "IMAGE_OT_properties";
905         ot->description= "Toggle display properties panel";
906         
907         ot->exec= image_properties;
908         ot->poll= ED_operator_image_active;
909         
910         /* flags */
911         ot->flag= 0;
912 }
913
914 static int image_scopes(bContext *C, wmOperator *UNUSED(op))
915 {
916         ScrArea *sa= CTX_wm_area(C);
917         ARegion *ar= image_has_scope_region(sa);
918         
919         if(ar)
920                 ED_region_toggle_hidden(C, ar);
921         
922         return OPERATOR_FINISHED;
923 }
924
925 void IMAGE_OT_scopes(wmOperatorType *ot)
926 {
927         ot->name= "Scopes";
928         ot->idname= "IMAGE_OT_scopes";
929         ot->description= "Toggle display scopes panel";
930         
931         ot->exec= image_scopes;
932         ot->poll= ED_operator_image_active;
933         
934         /* flags */
935         ot->flag= 0;
936 }
937