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