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  * The controls are laid out as follows:
121  *
122  * -------------------------------------------
123  * | Directory input               | execute |
124  * -------------------------------------------
125  * | Filename input        | + | - | cancel  |
126  * -------------------------------------------
127  *
128  * The input widgets will stretch to fill any excess space.
129  * When there isn't enough space for all controls to be shown, they are
130  * hidden in this order: x/-, execute/cancel, input widgets.
131  */
132 void file_draw_buttons(const bContext *C, ARegion *ar)
133 {
134         /* Button layout. */
135         const short min_x      = 10;
136         const short max_x      = ar->winx - 10;
137         const short line2_y    = ar->winy - IMASEL_BUTTONS_HEIGHT - 12;
138         const short line1_y    = line2_y  + IMASEL_BUTTONS_HEIGHT/2 + 4;
139         const short input_minw = 20;
140         const short btn_h      = 21;
141         const short btn_fn_w   = 14;
142         const short btn_minw   = 80;
143         const short btn_margin = 20;
144         const short separator  = 4;
145
146         /* Additional locals. */
147         char  name[20];
148         short loadbutton;
149         short fnumbuttons;
150         short available_w = max_x - min_x;
151         short line1_w     = available_w;
152         short line2_w     = available_w;
153         
154         uiBut*            but;
155         uiBlock*          block;
156         SpaceFile*        sfile  = (SpaceFile*) CTX_wm_space_data(C);
157         FileSelectParams* params = ED_fileselect_get_params(sfile);
158         
159         /* Initialize UI block. */
160         sprintf(name, "win %p", ar);
161         block = uiBeginBlock(C, ar, name, UI_EMBOSS);
162         uiBlockSetHandleFunc(block, do_file_buttons, NULL);
163         
164         /* Is there enough space for the execute / cancel buttons? */
165         loadbutton = UI_GetStringWidth(sfile->params->title) + btn_margin;
166         if (loadbutton < btn_minw) {
167                 loadbutton = MAX2(btn_minw, 
168                                   btn_margin + UI_GetStringWidth(params->title));
169         }
170         
171         if (available_w <= loadbutton + separator + input_minw 
172          || params->title[0] == 0) {
173                 loadbutton = 0;
174         } else {
175                 line1_w -= (loadbutton + separator);
176                 line2_w  = line1_w;
177         }
178
179         /* Is there enough space for file number increment/decrement buttons? */
180         fnumbuttons = 2 * btn_fn_w;
181         if (!loadbutton || line2_w <= fnumbuttons + separator + input_minw) {
182                 fnumbuttons = 0;
183         } else {
184                 line2_w -= (fnumbuttons + separator);
185         }
186         
187         /* Text input fields for directory and file. */
188         if (available_w > 0) {
189                 uiDefBut(block, TEX, B_FS_DIRNAME, "",
190                          min_x, line1_y, line1_w, btn_h, 
191                          params->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, 
192                          "File path.");
193                 uiDefBut(block, TEX, B_FS_FILENAME, "",
194                          min_x, line2_y, line2_w, btn_h,
195                          params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, 
196                          "File name.");
197         }
198         
199         /* Filename number increment / decrement buttons. */
200         if (fnumbuttons) {
201                 uiBlockBeginAlign(block);
202                 but = uiDefButO(block, BUT, "FILE_OT_filenum", 0, "-",
203                         min_x + line2_w + separator, line2_y, 
204                         btn_fn_w, btn_h, 
205                         "Decrement the filename number.");    
206                 RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", -1); 
207         
208                 but = uiDefButO(block, BUT, "FILE_OT_filenum", 0, "+", 
209                         min_x + line2_w + separator + btn_fn_w, line2_y, 
210                         btn_fn_w, btn_h, 
211                         "Increment the filename number.");    
212                 RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", 1); 
213                 uiBlockEndAlign(block);
214         }
215         
216         /* Execute / cancel buttons. */
217         if(loadbutton) {
218                 uiDefBut(block, BUT, B_FS_EXEC, params->title, 
219                   max_x - loadbutton, line1_y, loadbutton, btn_h, 
220                   params->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, params->title);
221                 uiDefBut(block, BUT, B_FS_CANCEL, "Cancel", 
222                   max_x - loadbutton, line2_y, loadbutton, btn_h, 
223                   params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "Cancel.");
224         }
225         
226         uiEndBlock(C, block);
227         uiDrawBlock(C, block);
228 }
229
230
231 static void draw_tile(short sx, short sy, short width, short height, int colorid, int shade)
232 {       
233         UI_ThemeColorShade(colorid, shade);
234         uiSetRoundBox(15);      
235         uiRoundBox(sx, sy - height, sx + width, sy, 6);
236 }
237
238 #define FILE_SHORTEN_END                                0
239 #define FILE_SHORTEN_FRONT                              1
240
241 static float shorten_string(char* string, float w, int flag)
242 {       
243         char temp[FILE_MAX];
244         short shortened = 0;
245         float sw = 0;
246         float pad = 0;
247
248         if (w <= 0) {
249                 *string = '\0';
250                 return 0.0;
251         }
252
253         sw = file_string_width(string);
254         if (flag == FILE_SHORTEN_FRONT) {
255                 char *s = string;
256                 BLI_strncpy(temp, "...", 4);
257                 pad = file_string_width(temp);
258                 while ((*s) && (sw+pad>w)) {
259                         s++;
260                         sw = file_string_width(s);
261                         shortened = 1;
262                 }
263                 if (shortened) {
264                         int slen = strlen(s);                   
265                         BLI_strncpy(temp+3, s, slen+1);
266                         temp[slen+4] = '\0';
267                         BLI_strncpy(string, temp, slen+4);
268                 }
269         } else {
270                 char *s = string;
271                 while (sw>w) {
272                         int slen = strlen(string);
273                         string[slen-1] = '\0';
274                         sw = file_string_width(s);
275                         shortened = 1;
276                 }
277
278                 if (shortened) {
279                         int slen = strlen(string);
280                         if (slen > 3) {
281                                 BLI_strncpy(string+slen-3, "...", 4);                           
282                         }
283                 }
284         }
285         
286         return sw;
287 }
288
289 static int get_file_icon(struct direntry *file)
290 {
291         if (file->type & S_IFDIR)
292                 return ICON_FILE_FOLDER;
293         else if (file->flags & BLENDERFILE)
294                 return ICON_FILE_BLEND;
295         else if (file->flags & IMAGEFILE)
296                 return ICON_FILE_IMAGE;
297         else if (file->flags & MOVIEFILE)
298                 return ICON_FILE_MOVIE;
299         else if (file->flags & PYSCRIPTFILE)
300                 return ICON_FILE_SCRIPT;
301         else if (file->flags & PYSCRIPTFILE)
302                 return ICON_FILE_SCRIPT;
303         else if (file->flags & SOUNDFILE) 
304                 return ICON_FILE_SOUND;
305         else if (file->flags & FTFONTFILE) 
306                 return ICON_FILE_FONT;
307         else
308                 return ICON_FILE_BLANK;
309 }
310
311 static void file_draw_icon(short sx, short sy, int icon, short width, short height)
312 {
313         float x,y;
314         int blend=0;
315         
316         x = (float)(sx);
317         y = (float)(sy-height);
318         
319         if (icon == ICON_FILE_BLANK) blend = -80;
320         
321         glEnable(GL_BLEND);
322         
323         UI_icon_draw_aspect_blended(x, y, icon, 1.f, blend);
324 }
325
326
327 static void file_draw_string(short sx, short sy, const char* string, float width, short height, int flag)
328 {
329         short soffs;
330         char fname[FILE_MAXFILE];
331         float sw;
332         float x,y;
333
334         BLI_strncpy(fname,string, FILE_MAXFILE);
335         sw = shorten_string(fname, width, flag );
336
337         soffs = (width - sw) / 2;
338         x = (float)(sx);
339         y = (float)(sy-height);
340
341         BLF_position(x, y, 0);
342         BLF_draw(fname);
343 }
344
345 void file_calc_previews(const bContext *C, ARegion *ar)
346 {
347         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
348         View2D *v2d= &ar->v2d;
349         
350         ED_fileselect_init_layout(sfile, ar);
351         UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height);
352 }
353
354 void file_draw_previews(const bContext *C, ARegion *ar)
355 {
356         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
357         FileSelectParams* params= ED_fileselect_get_params(sfile);
358         FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
359         View2D *v2d= &ar->v2d;
360         struct FileList* files = sfile->files;
361         int numfiles;
362         struct direntry *file;
363         short sx, sy;
364         ImBuf* imb=0;
365         int i;
366         int colorid = 0;
367         int offset;
368         int is_icon;
369
370         if (!files) return;
371
372         filelist_imgsize(files,sfile->layout->prv_w,sfile->layout->prv_h);
373         numfiles = filelist_numfiles(files);
374
375         sx = v2d->cur.xmin + layout->tile_border_x;
376         sy = v2d->cur.ymax - layout->tile_border_y;
377         
378         offset = ED_fileselect_layout_offset(layout, 0, 0);
379         if (offset<0) offset=0;
380         for (i=offset; (i < numfiles) && (i < (offset+(layout->rows+2)*layout->columns)); ++i)
381         {
382                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
383                 sx += v2d->tot.xmin+2;
384                 sy = v2d->tot.ymax - sy;
385                 file = filelist_file(files, i); 
386
387                 if (file->flags & ACTIVE) {
388                         colorid = TH_HILITE;
389                         draw_tile(sx - 1, sy, sfile->layout->tile_w + 1, sfile->layout->tile_h, colorid,0);
390                 } else if (params->active_file == i) {
391                         colorid = TH_ACTIVE;
392                         draw_tile(sx - 1, sy, sfile->layout->tile_w + 1, sfile->layout->tile_h, colorid,0);
393                 }
394                 
395                 if ( (file->flags & IMAGEFILE) /* || (file->flags & MOVIEFILE) */)
396                 {                       
397                         filelist_loadimage(files, i);
398                 }
399                 is_icon = 0;
400                 imb = filelist_getimage(files, i);
401                 if (!imb) {
402                         imb = filelist_geticon(files,i);
403                         is_icon = 1;
404                 }
405
406                 if (imb) {
407                         float fx, fy;
408                         float dx, dy;
409                         short xco, yco;
410                         float scaledx, scaledy;
411                         float scale;
412                         short ex, ey;
413
414                         if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
415                                 if (imb->x > imb->y) {
416                                         scaledx = (float)layout->prv_w;
417                                         scaledy =  ( (float)imb->y/(float)imb->x )*layout->prv_w;
418                                         scale = scaledx/imb->x;
419                                 }
420                                 else {
421                                         scaledy = (float)layout->prv_h;
422                                         scaledx =  ( (float)imb->x/(float)imb->y )*layout->prv_h;
423                                         scale = scaledy/imb->y;
424                                 }
425                         } else {
426                                 scaledx = (float)imb->x;
427                                 scaledy = (float)imb->y;
428                                 scale = 1.0;
429                         }
430                         ex = (short)scaledx;
431                         ey = (short)scaledy;
432                         fx = ((float)layout->prv_w - (float)ex)/2.0f;
433                         fy = ((float)layout->prv_h - (float)ey)/2.0f;
434                         dx = (fx + 0.5f + sfile->layout->prv_border_x);
435                         dy = (fy + 0.5f - sfile->layout->prv_border_y);
436                         xco = (float)sx + dx;
437                         yco = (float)sy - sfile->layout->prv_h + dy;
438
439                         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
440                         
441                         /* shadow */
442                         if (!is_icon && (file->flags & IMAGEFILE))
443                                 uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
444                         
445                         glEnable(GL_BLEND);
446                         
447                         /* the image */
448                         glColor4f(1.0, 1.0, 1.0, 1.0);
449                         glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
450                         
451                         /* border */
452                         if (!is_icon && (file->flags & IMAGEFILE)) {
453                                 glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
454                                 fdrawbox(xco, yco, xco + ex, yco + ey);
455                         }
456                         
457                         glDisable(GL_BLEND);
458                         imb = 0;
459                 }
460
461                 /* shadow */
462                 UI_ThemeColorShade(TH_BACK, -20);
463                 
464                 
465                 if (S_ISDIR(file->type)) {
466                         glColor4f(1.0f, 1.0f, 0.9f, 1.0f);
467                 }
468                 else if (file->flags & IMAGEFILE) {
469                         UI_ThemeColor(TH_SEQ_IMAGE);
470                 }
471                 else if (file->flags & MOVIEFILE) {
472                         UI_ThemeColor(TH_SEQ_MOVIE);
473                 }
474                 else if (file->flags & BLENDERFILE) {
475                         UI_ThemeColor(TH_SEQ_SCENE);
476                 }
477                 else {
478                         if (params->active_file == i) {
479                                 UI_ThemeColor(TH_GRID); /* grid used for active text */
480                         } else if (file->flags & ACTIVE) {
481                                 UI_ThemeColor(TH_TEXT_HI);                      
482                         } else {
483                                 UI_ThemeColor(TH_TEXT);
484                         }
485                 }
486
487                 file_draw_string(sx + layout->prv_border_x, sy+4, file->relname, layout->tile_w, layout->tile_h, FILE_SHORTEN_END);
488
489                 if (!sfile->loadimage_timer)
490                         sfile->loadimage_timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER1, 1.0/30.0);  /* max 30 frames/sec. */
491
492         }
493
494         uiSetRoundBox(0);
495 }
496
497
498 void file_draw_list(const bContext *C, ARegion *ar)
499 {
500         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
501         FileSelectParams* params = ED_fileselect_get_params(sfile);
502         FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
503         View2D *v2d= &ar->v2d;
504         struct FileList* files = sfile->files;
505         struct direntry *file;
506         int numfiles;
507         int colorid = 0;
508         short sx, sy;
509         int offset;
510         int i;
511         float sw, spos;
512
513         numfiles = filelist_numfiles(files);
514         
515         sx = ar->v2d.tot.xmin + layout->tile_border_x/2;
516         sy = ar->v2d.cur.ymax - layout->tile_border_y;
517
518         offset = ED_fileselect_layout_offset(layout, 0, 0);
519         if (offset<0) offset=0;
520
521         /* alternating flat shade background */
522         for (i=0; (i <= layout->rows); i+=2)
523         {
524                 sx = v2d->cur.xmin;
525                 sy = v2d->cur.ymax - i*(layout->tile_h+2*layout->tile_border_y) - layout->tile_border_y;
526
527                 UI_ThemeColorShade(TH_BACK, -7);
528                 glRectf(v2d->cur.xmin, sy, v2d->cur.xmax, sy+layout->tile_h+2*layout->tile_border_y);
529                 
530         }
531         
532         /* vertical column dividers */
533         sx = v2d->tot.xmin;
534         while (sx < ar->v2d.cur.xmax) {
535                 sx += (sfile->layout->tile_w+2*sfile->layout->tile_border_x);
536                 
537                 UI_ThemeColorShade(TH_BACK, 30);
538                 sdrawline(sx+1,  ar->v2d.cur.ymax - layout->tile_border_y ,  sx+1,  ar->v2d.cur.ymin); 
539                 UI_ThemeColorShade(TH_BACK, -30);
540                 sdrawline(sx,  ar->v2d.cur.ymax - layout->tile_border_y ,  sx,  ar->v2d.cur.ymin); 
541         }
542
543         sx = ar->v2d.cur.xmin + layout->tile_border_x;
544         sy = ar->v2d.cur.ymax - layout->tile_border_y;
545
546         for (i=offset; (i < numfiles); ++i)
547         {
548                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
549                 sx += v2d->tot.xmin+2;
550                 sy = v2d->tot.ymax - sy;
551
552                 file = filelist_file(files, i); 
553
554                 if (params->active_file == i) {
555                         if (file->flags & ACTIVE) colorid= TH_HILITE;
556                         else colorid = TH_BACK;
557                         draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
558                 } else if (file->flags & ACTIVE) {
559                         colorid = TH_HILITE;
560                         draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
561                 } 
562
563                 spos = sx;
564                 file_draw_icon(spos, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
565                 spos += ICON_DEFAULT_WIDTH + 4;
566                 
567                 UI_ThemeColor4(TH_TEXT);
568                 
569                 sw = file_string_width(file->relname);
570                 file_draw_string(spos, sy, file->relname, sw, layout->tile_h, FILE_SHORTEN_END);
571                 spos += layout->column_widths[COLUMN_NAME] + 12;
572                 if (params->display == FILE_SHOWSHORT) {
573                         if (!(file->type & S_IFDIR)) {
574                                 sw = file_string_width(file->size);
575                                 spos += layout->column_widths[COLUMN_SIZE] + 12 - sw;
576                                 file_draw_string(spos, sy, file->size, sw+1, layout->tile_h, FILE_SHORTEN_END); 
577                         }
578                 } else {
579 #if 0 // XXX TODO: add this for non-windows systems
580                         /* rwx rwx rwx */
581                         spos += 20;
582                         sw = UI_GetStringWidth(file->mode1);
583                         file_draw_string(spos, sy, file->mode1, sw, layout->tile_h); 
584                         
585                         spos += 30;
586                         sw = UI_GetStringWidth(file->mode2);
587                         file_draw_string(spos, sy, file->mode2, sw, layout->tile_h);
588
589                         spos += 30;
590                         sw = UI_GetStringWidth(file->mode3);
591                         file_draw_string(spos, sy, file->mode3, sw, layout->tile_h);
592                         
593                         spos += 30;
594                         sw = UI_GetStringWidth(file->owner);
595                         file_draw_string(spos, sy, file->owner, sw, layout->tile_h);
596 #endif
597
598                         
599                         sw = file_string_width(file->date);
600                         file_draw_string(spos, sy, file->date, sw, layout->tile_h, FILE_SHORTEN_END);
601                         spos += layout->column_widths[COLUMN_DATE] + 12;
602
603                         sw = file_string_width(file->time);
604                         file_draw_string(spos, sy, file->time, sw, layout->tile_h, FILE_SHORTEN_END); 
605                         spos += layout->column_widths[COLUMN_TIME] + 12;
606
607                         if (!(file->type & S_IFDIR)) {
608                                 sw = file_string_width(file->size);
609                                 spos += layout->column_widths[COLUMN_SIZE] + 12 - sw;
610                                 file_draw_string(spos, sy, file->size, sw, layout->tile_h, FILE_SHORTEN_END);
611                         }
612                 }
613         }
614 }
615
616