NLA SoC: Merge from 2.5
[blender.git] / source / blender / editors / space_file / file_draw.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
31 #include "BLI_blenlib.h"
32 #include "BLI_dynstr.h"
33 #include "BLI_storage_types.h"
34 #ifdef WIN32
35 #include "BLI_winstuff.h"
36 #endif
37
38 #include "BIF_gl.h"
39 #include "BIF_glutil.h"
40
41 #include "BKE_colortools.h"
42 #include "BKE_context.h"
43 #include "BKE_screen.h"
44 #include "BKE_global.h"
45 #include "BKE_utildefines.h"
46
47 #include "BLF_api.h"
48
49 #include "DNA_space_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_userdef_types.h"
53 #include "DNA_windowmanager_types.h"
54
55 #include "ED_datafiles.h"
56
57 #include "IMB_imbuf_types.h"
58 #include "IMB_imbuf.h"
59  
60 #include "MEM_guardedalloc.h"
61
62 #include "PIL_time.h"
63
64 #include "RNA_access.h"
65
66 #include "ED_fileselect.h"
67 #include "ED_screen.h"
68
69 #include "UI_interface.h"
70 #include "UI_interface_icons.h"
71 #include "UI_resources.h"
72 #include "UI_view2d.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "fsmenu.h"
78 #include "filelist.h"
79
80 #include "file_intern.h"        // own include
81
82 /* ui geometry */
83 #define IMASEL_BUTTONS_HEIGHT 40
84 #define TILE_BORDER_X 8
85 #define TILE_BORDER_Y 8
86
87 /* button events */
88 enum {
89         B_REDR  = 0,
90         B_FS_EXEC,
91         B_FS_CANCEL,
92         B_FS_PARENT,
93 } eFile_ButEvents;
94
95
96 static void do_file_buttons(bContext *C, void *arg, int event)
97 {
98         switch(event) {
99                 case B_FS_EXEC:
100                         file_exec(C, NULL);     /* file_ops.c */
101                         break;
102                 case B_FS_CANCEL:
103                         file_cancel_exec(C, NULL); /* file_ops.c */
104                         break;
105                 case B_FS_PARENT:
106                         file_parent_exec(C, NULL); /* file_ops.c */
107                         break;
108         }
109 }
110
111 /* note; this function uses pixelspace (0, 0, winx, winy), not view2d */
112 void file_draw_buttons(const bContext *C, ARegion *ar)
113 {
114         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
115         FileSelectParams* params = ED_fileselect_get_params(sfile);
116         uiBlock *block;
117         int loadbutton;
118         char name[20];
119         float slen;
120         int filebuty1, filebuty2;
121
122         float xmin = 8;
123         float xmax = ar->winx - 10;
124
125         filebuty1= ar->winy - IMASEL_BUTTONS_HEIGHT - 12;
126         filebuty2= filebuty1 + IMASEL_BUTTONS_HEIGHT/2 + 4;
127
128         /* HEADER */
129         sprintf(name, "win %p", ar);
130         block = uiBeginBlock(C, ar, name, UI_EMBOSS);
131         uiBlockSetHandleFunc(block, do_file_buttons, NULL);
132
133         /* XXXX
134         uiSetButLock( filelist_gettype(simasel->files)==FILE_MAIN && simasel->returnfunc, NULL); 
135         */
136
137         /* space available for load/save buttons? */
138         slen = UI_GetStringWidth(sfile->params->title);
139         loadbutton= slen > 60 ? slen + 20 : MAX2(80, 20+UI_GetStringWidth(params->title));
140         if(ar->winx > loadbutton+20) {
141                 if(params->title[0]==0) {
142                         loadbutton= 0;
143                 }
144         }
145         else {
146                 loadbutton= 0;
147         }
148
149         uiDefBut(block, TEX, 0 /* XXX B_FS_FILENAME */,"",      xmin+2, filebuty1, xmax-xmin-loadbutton-4, 21, params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
150         uiDefBut(block, TEX, 0 /* XXX B_FS_DIRNAME */,"",       xmin+2, filebuty2, xmax-xmin-loadbutton-4, 21, params->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
151         
152         if(loadbutton) {
153                 uiDefBut(block, BUT, B_FS_EXEC, params->title,  xmax-loadbutton, filebuty2, loadbutton, 21, params->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
154                 uiDefBut(block, BUT, B_FS_CANCEL, "Cancel",             xmax-loadbutton, filebuty1, loadbutton, 21, params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
155         }
156
157         uiEndBlock(C, block);
158         uiDrawBlock(C, block);
159 }
160
161
162 static void draw_tile(short sx, short sy, short width, short height, int colorid, int shade)
163 {
164         /* TODO: BIF_ThemeColor seems to need this to show the color, not sure why? - elubie */
165         //glEnable(GL_BLEND);
166         //glColor4ub(0, 0, 0, 100);
167         //glDisable(GL_BLEND);
168         /* I think it was a missing glDisable() - ton */
169         
170         UI_ThemeColorShade(colorid, shade);
171         uiSetRoundBox(15);      
172         uiRoundBox(sx, sy - height, sx + width, sy, 6);
173 }
174
175 #define FILE_SHORTEN_END                                0
176 #define FILE_SHORTEN_FRONT                              1
177
178 static float shorten_string(char* string, float w, int flag)
179 {       
180         char temp[FILE_MAX];
181         short shortened = 0;
182         float sw = 0;
183         float pad = 0;
184
185         if (w <= 0) {
186                 *string = '\0';
187                 return 0.0;
188         }
189
190         sw = file_string_width(string);
191         if (flag == FILE_SHORTEN_FRONT) {
192                 char *s = string;
193                 BLI_strncpy(temp, "...", 4);
194                 pad = file_string_width(temp);
195                 while ((*s) && (sw+pad>w)) {
196                         s++;
197                         sw = file_string_width(s);
198                         shortened = 1;
199                 }
200                 if (shortened) {
201                         int slen = strlen(s);                   
202                         BLI_strncpy(temp+3, s, slen+1);
203                         temp[slen+4] = '\0';
204                         BLI_strncpy(string, temp, slen+4);
205                 }
206         } else {
207                 char *s = string;
208                 while (sw>w) {
209                         int slen = strlen(string);
210                         string[slen-1] = '\0';
211                         sw = file_string_width(s);
212                         shortened = 1;
213                 }
214
215                 if (shortened) {
216                         int slen = strlen(string);
217                         if (slen > 3) {
218                                 BLI_strncpy(string+slen-3, "...", 4);                           
219                         }
220                 }
221         }
222         
223         return sw;
224 }
225
226 static int get_file_icon(struct direntry *file)
227 {
228         if (file->type & S_IFDIR)
229                 return ICON_FILE_FOLDER;
230         else if (file->flags & BLENDERFILE)
231                 return ICON_FILE_BLEND;
232         else if (file->flags & IMAGEFILE)
233                 return ICON_FILE_IMAGE;
234         else if (file->flags & MOVIEFILE)
235                 return ICON_FILE_MOVIE;
236         else if (file->flags & PYSCRIPTFILE)
237                 return ICON_FILE_SCRIPT;
238         else if (file->flags & PYSCRIPTFILE)
239                 return ICON_FILE_SCRIPT;
240         else if (file->flags & SOUNDFILE) 
241                 return ICON_FILE_SOUND;
242         else if (file->flags & FTFONTFILE) 
243                 return ICON_FILE_FONT;
244         else
245                 return ICON_FILE_BLANK;
246 }
247
248 static void file_draw_icon(short sx, short sy, int icon, short width, short height)
249 {
250         float x,y;
251         int blend=0;
252         
253         x = (float)(sx);
254         y = (float)(sy-height);
255         
256         if (icon == ICON_FILE_BLANK) blend = -80;
257         
258         glEnable(GL_BLEND);
259         
260         UI_icon_draw_aspect_blended(x, y, icon, 1.f, blend);
261 }
262
263
264 static void file_draw_string(short sx, short sy, const char* string, float width, short height, int flag)
265 {
266         short soffs;
267         char fname[FILE_MAXFILE];
268         float sw;
269         float x,y;
270
271         BLI_strncpy(fname,string, FILE_MAXFILE);
272         sw = shorten_string(fname, width, flag );
273
274         soffs = (width - sw) / 2;
275         x = (float)(sx);
276         y = (float)(sy-height);
277
278         BLF_position(x, y, 0);
279         BLF_draw(fname);
280 }
281
282 void file_calc_previews(const bContext *C, ARegion *ar)
283 {
284         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
285         View2D *v2d= &ar->v2d;
286         
287         ED_fileselect_init_layout(sfile, ar);
288         UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height);
289 }
290
291 void file_draw_previews(const bContext *C, ARegion *ar)
292 {
293         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
294         FileSelectParams* params= ED_fileselect_get_params(sfile);
295         FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
296         View2D *v2d= &ar->v2d;
297         struct FileList* files = sfile->files;
298         int numfiles;
299         struct direntry *file;
300         short sx, sy;
301         ImBuf* imb=0;
302         int i;
303         int colorid = 0;
304         int offset;
305         int is_icon;
306
307         if (!files) return;
308
309         filelist_imgsize(files,sfile->layout->prv_w,sfile->layout->prv_h);
310         numfiles = filelist_numfiles(files);
311
312         sx = v2d->cur.xmin + layout->tile_border_x;
313         sy = v2d->cur.ymax - layout->tile_border_y;
314         
315         offset = ED_fileselect_layout_offset(layout, 0, 0);
316         if (offset<0) offset=0;
317         for (i=offset; (i < numfiles) && (i < (offset+(layout->rows+2)*layout->columns)); ++i)
318         {
319                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
320                 sx += v2d->tot.xmin+2;
321                 sy = v2d->tot.ymax - sy;
322                 file = filelist_file(files, i); 
323
324                 if (file->flags & ACTIVE) {
325                         colorid = TH_HILITE;
326                         draw_tile(sx - 1, sy, sfile->layout->tile_w + 1, sfile->layout->tile_h, colorid,0);
327                 } else if (params->active_file == i) {
328                         colorid = TH_ACTIVE;
329                         draw_tile(sx - 1, sy, sfile->layout->tile_w + 1, sfile->layout->tile_h, colorid,0);
330                 }
331                 
332                 if ( (file->flags & IMAGEFILE) /* || (file->flags & MOVIEFILE) */)
333                 {                       
334                         filelist_loadimage(files, i);
335                 }
336                 is_icon = 0;
337                 imb = filelist_getimage(files, i);
338                 if (!imb) {
339                         imb = filelist_geticon(files,i);
340                         is_icon = 1;
341                 }
342
343                 if (imb) {
344                         float fx, fy;
345                         float dx, dy;
346                         short xco, yco;
347                         float scaledx, scaledy;
348                         float scale;
349                         short ex, ey;
350
351                         if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
352                                 if (imb->x > imb->y) {
353                                         scaledx = (float)layout->prv_w;
354                                         scaledy =  ( (float)imb->y/(float)imb->x )*layout->prv_w;
355                                         scale = scaledx/imb->x;
356                                 }
357                                 else {
358                                         scaledy = (float)layout->prv_h;
359                                         scaledx =  ( (float)imb->x/(float)imb->y )*layout->prv_h;
360                                         scale = scaledy/imb->y;
361                                 }
362                         } else {
363                                 scaledx = (float)imb->x;
364                                 scaledy = (float)imb->y;
365                                 scale = 1.0;
366                         }
367                         ex = (short)scaledx;
368                         ey = (short)scaledy;
369                         fx = ((float)layout->prv_w - (float)ex)/2.0f;
370                         fy = ((float)layout->prv_h - (float)ey)/2.0f;
371                         dx = (fx + 0.5f + sfile->layout->prv_border_x);
372                         dy = (fy + 0.5f - sfile->layout->prv_border_y);
373                         xco = (float)sx + dx;
374                         yco = (float)sy - sfile->layout->prv_h + dy;
375
376                         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
377                         
378                         /* shadow */
379                         if (!is_icon && (file->flags & IMAGEFILE))
380                                 uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
381                         
382                         glEnable(GL_BLEND);
383                         
384                         /* the image */
385                         glColor4f(1.0, 1.0, 1.0, 1.0);
386                         glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
387                         
388                         /* border */
389                         if (!is_icon && (file->flags & IMAGEFILE)) {
390                                 glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
391                                 fdrawbox(xco, yco, xco + ex, yco + ey);
392                         }
393                         
394                         glDisable(GL_BLEND);
395                         imb = 0;
396                 }
397
398                 /* shadow */
399                 UI_ThemeColorShade(TH_BACK, -20);
400                 
401                 
402                 if (S_ISDIR(file->type)) {
403                         glColor4f(1.0f, 1.0f, 0.9f, 1.0f);
404                 }
405                 else if (file->flags & IMAGEFILE) {
406                         UI_ThemeColor(TH_SEQ_IMAGE);
407                 }
408                 else if (file->flags & MOVIEFILE) {
409                         UI_ThemeColor(TH_SEQ_MOVIE);
410                 }
411                 else if (file->flags & BLENDERFILE) {
412                         UI_ThemeColor(TH_SEQ_SCENE);
413                 }
414                 else {
415                         if (params->active_file == i) {
416                                 UI_ThemeColor(TH_GRID); /* grid used for active text */
417                         } else if (file->flags & ACTIVE) {
418                                 UI_ThemeColor(TH_TEXT_HI);                      
419                         } else {
420                                 UI_ThemeColor(TH_TEXT);
421                         }
422                 }
423
424                 file_draw_string(sx + layout->prv_border_x, sy+4, file->relname, layout->tile_w, layout->tile_h, FILE_SHORTEN_END);
425
426                 if (!sfile->loadimage_timer)
427                         sfile->loadimage_timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER1, 1.0/30.0);  /* max 30 frames/sec. */
428
429         }
430
431         uiSetRoundBox(0);
432 }
433
434
435 void file_draw_list(const bContext *C, ARegion *ar)
436 {
437         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
438         FileSelectParams* params = ED_fileselect_get_params(sfile);
439         FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
440         View2D *v2d= &ar->v2d;
441         struct FileList* files = sfile->files;
442         struct direntry *file;
443         int numfiles;
444         int colorid = 0;
445         short sx, sy;
446         int offset;
447         int i;
448         float sw, spos;
449
450         numfiles = filelist_numfiles(files);
451         
452         sx = ar->v2d.tot.xmin + layout->tile_border_x/2;
453         sy = ar->v2d.cur.ymax - layout->tile_border_y;
454
455         offset = ED_fileselect_layout_offset(layout, 0, 0);
456         if (offset<0) offset=0;
457
458         /* alternating flat shade background */
459         for (i=0; (i <= layout->rows); i+=2)
460         {
461                 sx = v2d->cur.xmin;
462                 sy = v2d->cur.ymax - i*(layout->tile_h+2*layout->tile_border_y) - layout->tile_border_y;
463
464                 UI_ThemeColorShade(TH_BACK, -7);
465                 glRectf(v2d->cur.xmin, sy, v2d->cur.xmax, sy+layout->tile_h+2*layout->tile_border_y);
466                 
467         }
468         
469         /* vertical column dividers */
470         sx = v2d->tot.xmin;
471         while (sx < ar->v2d.cur.xmax) {
472                 sx += (sfile->layout->tile_w+2*sfile->layout->tile_border_x);
473                 
474                 UI_ThemeColorShade(TH_BACK, 30);
475                 sdrawline(sx+1,  ar->v2d.cur.ymax - layout->tile_border_y ,  sx+1,  ar->v2d.cur.ymin); 
476                 UI_ThemeColorShade(TH_BACK, -30);
477                 sdrawline(sx,  ar->v2d.cur.ymax - layout->tile_border_y ,  sx,  ar->v2d.cur.ymin); 
478         }
479
480         sx = ar->v2d.cur.xmin + layout->tile_border_x;
481         sy = ar->v2d.cur.ymax - layout->tile_border_y;
482
483         for (i=offset; (i < numfiles); ++i)
484         {
485                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
486                 sx += v2d->tot.xmin+2;
487                 sy = v2d->tot.ymax - sy;
488
489                 file = filelist_file(files, i); 
490
491                 if (params->active_file == i) {
492                         if (file->flags & ACTIVE) colorid= TH_HILITE;
493                         else colorid = TH_BACK;
494                         draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
495                 } else if (file->flags & ACTIVE) {
496                         colorid = TH_HILITE;
497                         draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
498                 } 
499
500                 spos = sx;
501                 file_draw_icon(spos, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
502                 spos += ICON_DEFAULT_WIDTH + 4;
503                 
504                 UI_ThemeColor4(TH_TEXT);
505                 
506                 sw = file_string_width(file->relname);
507                 file_draw_string(spos, sy, file->relname, sw, layout->tile_h, FILE_SHORTEN_END);
508                 spos += layout->column_widths[COLUMN_NAME] + 12;
509                 if (params->display == FILE_SHOWSHORT) {
510                         if (!(file->type & S_IFDIR)) {
511                                 sw = file_string_width(file->size);
512                                 spos += layout->column_widths[COLUMN_SIZE] + 12 - sw;
513                                 file_draw_string(spos, sy, file->size, sw+1, layout->tile_h, FILE_SHORTEN_END); 
514                         }
515                 } else {
516 #if 0 // XXX TODO: add this for non-windows systems
517                         /* rwx rwx rwx */
518                         spos += 20;
519                         sw = UI_GetStringWidth(file->mode1);
520                         file_draw_string(spos, sy, file->mode1, sw, layout->tile_h); 
521                         
522                         spos += 30;
523                         sw = UI_GetStringWidth(file->mode2);
524                         file_draw_string(spos, sy, file->mode2, sw, layout->tile_h);
525
526                         spos += 30;
527                         sw = UI_GetStringWidth(file->mode3);
528                         file_draw_string(spos, sy, file->mode3, sw, layout->tile_h);
529                         
530                         spos += 30;
531                         sw = UI_GetStringWidth(file->owner);
532                         file_draw_string(spos, sy, file->owner, sw, layout->tile_h);
533 #endif
534
535                         
536                         sw = file_string_width(file->date);
537                         file_draw_string(spos, sy, file->date, sw, layout->tile_h, FILE_SHORTEN_END);
538                         spos += layout->column_widths[COLUMN_DATE] + 12;
539
540                         sw = file_string_width(file->time);
541                         file_draw_string(spos, sy, file->time, sw, layout->tile_h, FILE_SHORTEN_END); 
542                         spos += layout->column_widths[COLUMN_TIME] + 12;
543
544                         if (!(file->type & S_IFDIR)) {
545                                 sw = file_string_width(file->size);
546                                 spos += layout->column_widths[COLUMN_SIZE] + 12 - sw;
547                                 file_draw_string(spos, sy, file->size, sw, layout->tile_h, FILE_SHORTEN_END);
548                         }
549                 }
550         }
551 }
552
553