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