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