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