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