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