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