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