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