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