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