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