2.5: File browser
[blender.git] / source / blender / editors / space_image / image_header.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_object_types.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_customdata_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_editVert.h"
44
45 #include "BKE_context.h"
46 #include "BKE_customdata.h"
47 #include "BKE_image.h"
48 #include "BKE_mesh.h"
49 #include "BKE_screen.h"
50 #include "BKE_utildefines.h"
51
52 #include "IMB_imbuf.h"
53 #include "IMB_imbuf_types.h"
54
55 #include "ED_image.h"
56 #include "ED_mesh.h"
57 #include "ED_screen.h"
58 #include "ED_transform.h"
59 #include "ED_types.h"
60 #include "ED_util.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 #include "BIF_gl.h"
66 #include "BIF_glutil.h"
67
68 #include "UI_interface.h"
69 #include "UI_resources.h"
70 #include "UI_view2d.h"
71
72 #include "RNA_access.h"
73
74 #include "RE_pipeline.h"
75
76 #include "image_intern.h"
77
78 /* ************************ header area region *********************** */
79
80 #define B_NOP                           -1
81 #define B_REDR                          1
82 #define B_SIMAGEPAINTTOOL       4
83 #define B_SIMA_USE_ALPHA        5
84 #define B_SIMA_SHOW_ALPHA       6
85 #define B_SIMA_SHOW_ZBUF        7
86 #define B_SIMA_RECORD           8
87 #define B_SIMA_PLAY                     9
88
89 #if 0
90 static void do_image_imagemenu(void *arg, int event)
91 {
92         /* events >=20 are registered bpython scripts */
93 #ifndef DISABLE_PYTHON
94         if (event >= 20) BPY_menu_do_python(PYMENU_IMAGE, event - 20);
95 #endif  
96 }
97
98 #ifndef DISABLE_PYTHON
99         {
100                 BPyMenu *pym;
101                 int i = 0;
102
103                 /* note that we acount for the N previous entries with i+20: */
104                 for (pym = BPyMenuTable[PYMENU_IMAGE]; pym; pym = pym->next, i++) {
105
106                         uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
107                                          NULL, 0.0, 0.0, 1, i+20, 
108                                          pym->tooltip?pym->tooltip:pym->filename);
109                 }
110         }
111 #endif
112 #endif
113
114 #if 0
115 #ifndef DISABLE_PYTHON
116 static void do_image_uvs_scriptsmenu(void *arg, int event)
117 {
118         BPY_menu_do_python(PYMENU_UV, event);
119
120         allqueue(REDRAWIMAGE, 0);
121 }
122
123 static void image_uvs_scriptsmenu (void *args_unused)
124 {
125         uiBlock *block;
126         BPyMenu *pym;
127         int i= 0;
128         short yco = 20, menuwidth = 120;
129         
130         block= uiNewBlock(&curarea->uiblocks, "image_uvs_scriptsmenu", UI_EMBOSSP);
131         uiBlockSetButmFunc(block, do_image_uvs_scriptsmenu, NULL);
132         
133         /* note that we acount for the N previous entries with i+20: */
134         for (pym = BPyMenuTable[PYMENU_UV]; pym; pym = pym->next, i++) {
135                 
136                 uiDefIconTextBut(block, BUTM, 1, ICON_PYTHON, pym->name, 0, yco-=20, menuwidth, 19, 
137                                                  NULL, 0.0, 0.0, 1, i, 
138                                                  pym->tooltip?pym->tooltip:pym->filename);
139         }
140         
141         uiBlockSetDirection(block, UI_RIGHT);
142         uiTextBoundsBlock(block, 60);
143         
144         return block;
145 }
146 #endif /* DISABLE_PYTHON */
147 #endif
148
149 #if 0
150 static void do_image_buttons(bContext *C, void *arg, int event)
151 {
152         switch(event) {
153                 case B_REDR:
154                         ED_area_tag_redraw(CTX_wm_area(C));
155                         break;
156         }
157
158         ToolSettings *settings= G.scene->toolsettings;
159         ID *id, *idtest;
160         int nr;
161
162         if(curarea->win==0) return;
163
164         if(event<=100) {
165                 if(event<=50) do_global_buttons2(event);
166                 else do_global_buttons(event);
167                 return;
168         }
169         
170         switch(event) {
171         case B_SIMABROWSE:      
172                 if(sima->imanr== -2) {
173                         if(G.qual & LR_CTRLKEY) {
174                                 activate_databrowse_imasel((ID *)sima->image, ID_IM, 0, B_SIMABROWSE,
175                                                                                         &sima->imanr, do_image_buttons);
176                         } else {
177                                 activate_databrowse((ID *)sima->image, ID_IM, 0, B_SIMABROWSE,
178                                                                                         &sima->imanr, do_image_buttons);
179                         }
180                         return;
181                 }
182                 if(sima->imanr < 0) break;
183         
184                 nr= 1;
185                 id= (ID *)sima->image;
186
187                 idtest= BLI_findlink(&G.main->image, sima->imanr-1);
188                 if(idtest==NULL) { /* no new */
189                         return;
190                 }
191         
192                 if(idtest!=id) {
193                         sima->image= (Image *)idtest;
194                         if(idtest->us==0) idtest->us= 1;
195                         BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
196                         allqueue(REDRAWIMAGE, 0);
197                 }
198                 /* also when image is the same: assign! 0==no tileflag: */
199                 image_changed(sima, (Image *)idtest);
200                 BIF_undo_push("Assign image UV");
201
202                 break;
203         case B_SIMAGETILE:
204                 image_set_tile(sima, 1);                /* 1: only tileflag */
205                 allqueue(REDRAWVIEW3D, 0);
206                 allqueue(REDRAWIMAGE, 0);
207                 break;
208         case B_SIMA3DVIEWDRAW:
209                 allqueue(REDRAWVIEW3D, 0);
210                 break;
211         case B_SIMA_REDR_IMA_3D:
212                 allqueue(REDRAWVIEW3D, 0);
213                 allqueue(REDRAWIMAGE, 0);
214                 break;
215
216         case B_SIMAPACKIMA:
217                 pack_image_sima();
218                 break;
219                 
220         case B_SIMA_REPACK:
221                 BKE_image_memorypack(sima->image);
222                 allqueue(REDRAWIMAGE, 0);
223                 break;
224                 
225         case B_SIMA_USE_ALPHA:
226                 sima->flag &= ~(SI_SHOW_ALPHA|SI_SHOW_ZBUF);
227                 scrarea_queue_winredraw(curarea);
228                 scrarea_queue_headredraw(curarea);
229                 break;
230         case B_SIMA_SHOW_ALPHA:
231                 sima->flag &= ~(SI_USE_ALPHA|SI_SHOW_ZBUF);
232                 scrarea_queue_winredraw(curarea);
233                 scrarea_queue_headredraw(curarea);
234                 break;
235         case B_SIMA_SHOW_ZBUF:
236                 sima->flag &= ~(SI_SHOW_ALPHA|SI_USE_ALPHA);
237                 scrarea_queue_winredraw(curarea);
238                 scrarea_queue_headredraw(curarea);
239                 break;
240         case B_SIMARELOAD:
241                 reload_image_sima();
242                 break;
243         case B_SIMAGELOAD:
244                 open_image_sima(0);
245                 break;
246         case B_SIMANAME:
247                 if(sima->image) {
248                         Image *ima;
249                         char str[FILE_MAXDIR+FILE_MAXFILE];
250                         
251                         /* name in ima has been changed by button! */
252                         BLI_strncpy(str, sima->image->name, sizeof(str));
253                         ima= BKE_add_image_file(str);
254                         if(ima) {
255                                 BKE_image_signal(ima, &sima->iuser, IMA_SIGNAL_RELOAD);
256                                 image_changed(sima, ima);
257                         }
258                         BIF_undo_push("Load image");
259                         allqueue(REDRAWIMAGE, 0);
260                 }
261                 break;
262         case B_SIMAMULTI:
263                 if(sima && sima->image) {
264                         BKE_image_multilayer_index(sima->image->rr, &sima->iuser);
265                         allqueue(REDRAWIMAGE, 0);
266                 }
267                 break;
268         case B_TRANS_IMAGE:
269                 image_editvertex_buts(NULL);
270                 break;
271         case B_CURSOR_IMAGE:
272                 image_editcursor_buts(NULL);
273                 break;
274                 
275         case B_TWINANIM:
276         {
277                 Image *ima;
278                 int nr;
279                 
280                 ima = sima->image;
281                 if (ima) {
282                         if(ima->flag & IMA_TWINANIM) {
283                                 nr= ima->xrep*ima->yrep;
284                                 if(ima->twsta>=nr) ima->twsta= 1;
285                                 if(ima->twend>=nr) ima->twend= nr-1;
286                                 if(ima->twsta>ima->twend) ima->twsta= 1;
287                         }
288
289                         allqueue(REDRAWIMAGE, 0);
290                         allqueue(REDRAWVIEW3D, 0);
291                 }
292                 break;
293         }       
294         case B_SIMACLONEBROWSE:
295                 if(settings->imapaint.brush) {
296                         Brush *brush= settings->imapaint.brush;
297                 
298                         if(sima->menunr== -2) {
299                                 if(G.qual & LR_CTRLKEY) {
300                                         activate_databrowse_imasel((ID *)brush->clone.image, ID_IM, 0, B_SIMACLONEBROWSE,
301                                                                                                 &sima->menunr, do_image_buttons);
302                                 } else {
303                                         activate_databrowse((ID *)brush->clone.image, ID_IM, 0, B_SIMACLONEBROWSE,
304                                                                                                 &sima->menunr, do_image_buttons);
305                                 }
306                                 break;
307                         }
308                         if(sima->menunr < 0) break;
309
310                         if(brush_clone_image_set_nr(brush, sima->menunr))
311                                 allqueue(REDRAWIMAGE, 0);
312                 }
313                 break;
314                 
315         case B_SIMACLONEDELETE:
316                 if (settings->imapaint.brush)
317                         if (brush_clone_image_delete(settings->imapaint.brush))
318                                 allqueue(REDRAWIMAGE, 0);
319                 break;
320                 
321         case B_SIMABRUSHCHANGE:
322                 allqueue(REDRAWIMAGE, 0);
323                 allqueue(REDRAWBUTSEDIT, 0);
324                 break;
325                 
326         case B_SIMACURVES:
327                 curvemapping_do_ibuf(sima->cumap, imagewindow_get_ibuf(sima));
328                 allqueue(REDRAWIMAGE, 0);
329                 break;
330                 
331         case B_SIMARANGE:
332                 curvemapping_set_black_white(sima->cumap, NULL, NULL);
333                 curvemapping_do_ibuf(sima->cumap, imagewindow_get_ibuf(sima));
334                 allqueue(REDRAWIMAGE, 0);
335                 break;
336                 
337         case B_SIMABRUSHBROWSE:
338                 if(sima->menunr==-2) {
339                         activate_databrowse((ID*)settings->imapaint.brush, ID_BR, 0, B_SIMABRUSHBROWSE, &sima->menunr, do_global_buttons);
340                         break;
341                 }
342                 else if(sima->menunr < 0) break;
343                 
344                 if(brush_set_nr(&settings->imapaint.brush, sima->menunr)) {
345                         BIF_undo_push("Browse Brush");
346                         allqueue(REDRAWBUTSEDIT, 0);
347                         allqueue(REDRAWIMAGE, 0);
348                 }
349                 break;
350         case B_SIMABRUSHDELETE:
351                 if(brush_delete(&settings->imapaint.brush)) {
352                         BIF_undo_push("Unlink Brush");
353                         allqueue(REDRAWIMAGE, 0);
354                         allqueue(REDRAWBUTSEDIT, 0);
355                 }
356                 break;
357         case B_KEEPDATA:
358                 brush_toggled_fake_user(settings->imapaint.brush);
359                 allqueue(REDRAWIMAGE, 0);
360                 allqueue(REDRAWBUTSEDIT, 0);
361                 break;
362         case B_SIMABRUSHLOCAL:
363                 if(settings->imapaint.brush && settings->imapaint.brush->id.lib) {
364                         if(okee("Make local")) {
365                                 make_local_brush(settings->imapaint.brush);
366                                 allqueue(REDRAWIMAGE, 0);
367                                 allqueue(REDRAWBUTSEDIT, 0);
368                         }
369                 }
370                 break;
371         case B_SIMABTEXBROWSE:
372                 if(settings->imapaint.brush) {
373                         Brush *brush= settings->imapaint.brush;
374                         
375                         if(sima->menunr==-2) {
376                                 MTex *mtex= brush->mtex[brush->texact];
377                                 ID *id= (ID*)((mtex)? mtex->tex: NULL);
378                                 if(G.qual & LR_CTRLKEY) {
379                                         activate_databrowse_imasel(id, ID_TE, 0, B_SIMABTEXBROWSE, &sima->menunr, do_image_buttons);
380                                 } else {
381                                         activate_databrowse(id, ID_TE, 0, B_SIMABTEXBROWSE, &sima->menunr, do_image_buttons);
382                                 }
383                                 break;
384                         }
385                         else if(sima->menunr < 0) break;
386                         
387                         if(brush_texture_set_nr(brush, sima->menunr)) {
388                                 BIF_undo_push("Browse Brush Texture");
389                                 allqueue(REDRAWBUTSSHADING, 0);
390                                 allqueue(REDRAWBUTSEDIT, 0);
391                                 allqueue(REDRAWIMAGE, 0);
392                         }
393                 }
394                 break;
395         case B_SIMABTEXDELETE:
396                 if(settings->imapaint.brush) {
397                         if (brush_texture_delete(settings->imapaint.brush)) {
398                                 BIF_undo_push("Unlink Brush Texture");
399                                 allqueue(REDRAWBUTSSHADING, 0);
400                                 allqueue(REDRAWBUTSEDIT, 0);
401                                 allqueue(REDRAWIMAGE, 0);
402                         }
403                 }
404                 break;
405         case B_SIMA_PLAY:
406                 play_anim(0);
407                 break;
408         case B_SIMA_RECORD:
409                 imagespace_composite_flipbook(curarea);
410                 break;
411         }
412 }
413 #endif
414
415 /********************** toolbox operator *********************/
416
417 static int toolbox_invoke(bContext *C, wmOperator *op, wmEvent *event)
418 {
419         SpaceImage *sima= CTX_wm_space_image(C);
420         Object *obedit= CTX_data_edit_object(C);
421         uiPopupMenu *pup;
422         uiLayout *layout;
423         int show_uvedit;
424
425         show_uvedit= ED_space_image_show_uvedit(sima, obedit);
426
427         pup= uiPupMenuBegin(C, "Toolbox", 0);
428         layout= uiPupMenuLayout(pup);
429
430         uiItemM(layout, C, NULL, 0, "IMAGE_MT_view");
431         if(show_uvedit) uiItemM(layout, C, NULL, 0, "IMAGE_MT_select");
432         uiItemM(layout, C, NULL, 0, "IMAGE_MT_image");
433         if(show_uvedit) uiItemM(layout, C, NULL, 0, "IMAGE_MT_uvs");
434
435         uiPupMenuEnd(C, pup);
436
437         return OPERATOR_CANCELLED;
438 }
439
440 void IMAGE_OT_toolbox(wmOperatorType *ot)
441 {
442         /* identifiers */
443         ot->name= "Toolbox";
444         ot->idname= "IMAGE_OT_toolbox";
445         
446         /* api callbacks */
447         ot->invoke= toolbox_invoke;
448         ot->poll= space_image_main_area_poll;
449 }
450