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