Sculpt Branch:
[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                 if ( strcmp(file->relname, "..") == 0) {
295                                 return  ICON_FILE_PARENT;
296                 }
297                 return ICON_FILE_FOLDER;
298         }
299         else if (file->flags & BLENDERFILE)
300                 return ICON_FILE_BLEND;
301         else if (file->flags & IMAGEFILE)
302                 return ICON_FILE_IMAGE;
303         else if (file->flags & MOVIEFILE)
304                 return ICON_FILE_MOVIE;
305         else if (file->flags & PYSCRIPTFILE)
306                 return ICON_FILE_SCRIPT;
307         else if (file->flags & PYSCRIPTFILE)
308                 return ICON_FILE_SCRIPT;
309         else if (file->flags & SOUNDFILE) 
310                 return ICON_FILE_SOUND;
311         else if (file->flags & FTFONTFILE) 
312                 return ICON_FILE_FONT;
313         else if (file->flags & BTXFILE) 
314                 return ICON_FILE_BLANK;
315         else
316                 return ICON_FILE_BLANK;
317 }
318
319 static void file_draw_icon(int sx, int sy, int icon, int width, int height)
320 {
321         float x,y;
322         float alpha=1.0f;
323         
324         x = (float)(sx);
325         y = (float)(sy-height);
326         
327         if (icon == ICON_FILE_BLANK) alpha = 0.375f;
328         
329         glEnable(GL_BLEND);
330         
331         UI_icon_draw_aspect(x, y, icon, 1.f, alpha);
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(int sx, int sy, ImBuf *imb, FileLayout *layout, short dropshadow)
364 {
365         if (imb) {
366                         float fx, fy;
367                         float dx, dy;
368                         int xco, yco;
369                         float scaledx, scaledy;
370                         float scale;
371                         int ex, ey;
372
373                         if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
374                                 if (imb->x > imb->y) {
375                                         scaledx = (float)layout->prv_w;
376                                         scaledy =  ( (float)imb->y/(float)imb->x )*layout->prv_w;
377                                         scale = scaledx/imb->x;
378                                 }
379                                 else {
380                                         scaledy = (float)layout->prv_h;
381                                         scaledx =  ( (float)imb->x/(float)imb->y )*layout->prv_h;
382                                         scale = scaledy/imb->y;
383                                 }
384                         } else {
385                                 scaledx = (float)imb->x;
386                                 scaledy = (float)imb->y;
387                                 scale = 1.0;
388                         }
389                         ex = (int)scaledx;
390                         ey = (int)scaledy;
391                         fx = ((float)layout->prv_w - (float)ex)/2.0f;
392                         fy = ((float)layout->prv_h - (float)ey)/2.0f;
393                         dx = (fx + 0.5f + layout->prv_border_x);
394                         dy = (fy + 0.5f - layout->prv_border_y);
395                         xco = (float)sx + dx;
396                         yco = (float)sy - layout->prv_h + dy;
397
398                         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
399                         
400                         /* shadow */
401                         if (dropshadow)
402                                 uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
403                         
404                         glEnable(GL_BLEND);
405                         
406                         /* the image */
407                         glColor4f(1.0, 1.0, 1.0, 1.0);
408                         glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
409                         
410                         /* border */
411                         if (dropshadow) {
412                                 glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
413                                 fdrawbox(xco, yco, xco + ex, yco + ey);
414                         }
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                         filelist_free(sfile->files);
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         int numfiles;
491         int numfiles_layout;
492         int colorid = 0;
493         int sx, sy;
494         int offset;
495         int i;
496         float sw, spos;
497         short is_icon;
498
499         numfiles = filelist_numfiles(files);
500         
501         if (params->display != FILE_IMGDISPLAY) {
502
503                 draw_background(layout, v2d);
504         
505                 draw_dividers(layout, v2d);
506         }
507
508         offset = ED_fileselect_layout_offset(layout, ar->v2d.cur.xmin, -ar->v2d.cur.ymax);
509         if (offset<0) offset=0;
510
511         numfiles_layout = ED_fileselect_layout_numfiles(layout, ar);
512
513         for (i=offset; (i < numfiles) && (i<offset+numfiles_layout); ++i)
514         {
515                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
516                 sx += v2d->tot.xmin+2;
517                 sy = v2d->tot.ymax - sy;
518
519                 file = filelist_file(files, i); 
520                 
521                 if (!(file->flags & EDITING)) {
522                         if (params->active_file == i) {
523                                 if (file->flags & ACTIVE) colorid= TH_HILITE;
524                                 else colorid = TH_BACK;
525                                 draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
526                         } else if (file->flags & ACTIVE) {
527                                 colorid = TH_HILITE;
528                                 draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
529                         } 
530                 }
531
532                 spos = sx;
533
534                 if ( FILE_IMGDISPLAY == params->display ) {
535                         if ( (file->flags & IMAGEFILE) /* || (file->flags & MOVIEFILE) */) {                    
536                                 filelist_loadimage(files, i);
537                         }
538                         is_icon = 0;
539                         imb = filelist_getimage(files, i);
540                         if (!imb) {
541                                 imb = filelist_geticon(files,i);
542                                 is_icon = 1;
543                         }
544                         
545                         file_draw_preview(sx, sy, imb, layout, !is_icon && (file->flags & IMAGEFILE));
546
547                 } else {
548                                 file_draw_icon(spos, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
549                                 spos += ICON_DEFAULT_WIDTH + 4;
550                 }
551
552                 UI_ThemeColor4(TH_TEXT);
553
554                 sw = file_string_width(file->relname);
555                 if (file->flags & EDITING) {
556                         int but_width = (FILE_IMGDISPLAY == params->display) ? layout->tile_w : layout->column_widths[COLUMN_NAME];
557                         uiBlock *block = uiBeginBlock(C, ar, "FileName", UI_EMBOSS);
558                         uiBut *but = uiDefBut(block, TEX, 1, "", spos, sy-layout->tile_h-3, 
559                                 but_width, layout->textheight*2, file->relname, 1.0f, (float)FILE_MAX,0,0,"");
560                         uiButSetRenameFunc(but, renamebutton_cb, file);
561                         if ( 0 == uiButActiveOnly(C, block, but)) {
562                                 file->flags &= ~EDITING;
563                         }
564                         uiEndBlock(C, block);
565                         uiDrawBlock(C, block);
566                 } else {
567                         float name_width = (FILE_IMGDISPLAY == params->display) ? layout->tile_w : sw;
568                         file_draw_string(spos, sy, file->relname, name_width, layout->tile_h, FILE_SHORTEN_END);
569                 }
570
571                 uiSetRoundBox(0);
572
573                 if (params->display == FILE_SHORTDISPLAY) {
574                         spos += layout->column_widths[COLUMN_NAME] + 12;
575                         if (!(file->type & S_IFDIR)) {
576                                 sw = file_string_width(file->size);
577                                 file_draw_string(spos, sy, file->size, sw+1, layout->tile_h, FILE_SHORTEN_END); 
578                                 spos += layout->column_widths[COLUMN_SIZE] + 12;
579                         }
580                 } else if (params->display == FILE_LONGDISPLAY) {
581                         spos += layout->column_widths[COLUMN_NAME] + 12;
582
583 #ifndef WIN32
584                         /* rwx rwx rwx */
585                         sw = file_string_width(file->mode1);
586                         file_draw_string(spos, sy, file->mode1, sw, layout->tile_h, FILE_SHORTEN_END); 
587                         spos += layout->column_widths[COLUMN_MODE1] + 12;
588
589                         sw = file_string_width(file->mode2);
590                         file_draw_string(spos, sy, file->mode2, sw, layout->tile_h, FILE_SHORTEN_END);
591                         spos += layout->column_widths[COLUMN_MODE2] + 12;
592
593                         sw = file_string_width(file->mode3);
594                         file_draw_string(spos, sy, file->mode3, sw, layout->tile_h, FILE_SHORTEN_END);
595                         spos += layout->column_widths[COLUMN_MODE3] + 12;
596
597                         sw = file_string_width(file->owner);
598                         file_draw_string(spos, sy, file->owner, sw, layout->tile_h, FILE_SHORTEN_END);
599                         spos += layout->column_widths[COLUMN_OWNER] + 12;
600 #endif
601
602                         sw = file_string_width(file->date);
603                         file_draw_string(spos, sy, file->date, sw, layout->tile_h, FILE_SHORTEN_END);
604                         spos += layout->column_widths[COLUMN_DATE] + 12;
605
606                         sw = file_string_width(file->time);
607                         file_draw_string(spos, sy, file->time, sw, layout->tile_h, FILE_SHORTEN_END); 
608                         spos += layout->column_widths[COLUMN_TIME] + 12;
609
610                         if (!(file->type & S_IFDIR)) {
611                                 sw = file_string_width(file->size);
612                                 file_draw_string(spos, sy, file->size, sw, layout->tile_h, FILE_SHORTEN_END);
613                                 spos += layout->column_widths[COLUMN_SIZE] + 12;
614                         }
615                 }
616         }
617
618         if (!sfile->loadimage_timer)
619                 sfile->loadimage_timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER1, 1.0/30.0);      /* max 30 frames/sec. */
620 }
621
622