== file browser ==
[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., 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 <math.h>
30 #include <string.h>
31
32 #include "BLI_blenlib.h"
33 #include "BLI_utildefines.h"
34 #include "BLI_dynstr.h"
35 #include "BLI_storage_types.h"
36 #ifdef WIN32
37 #include "BLI_winstuff.h"
38 #endif
39
40 #include "BIF_gl.h"
41 #include "BIF_glutil.h"
42
43 #include "BKE_context.h"
44 #include "BKE_global.h"
45 #include "BKE_main.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 *UNUSED(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[32];
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", (void *)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                 int overwrite_alert= file_draw_check_exists(sfile);
179                 /* callbacks for operator check functions */
180                 uiBlockSetFunc(block, file_draw_check_cb, NULL, NULL);
181
182                 but = uiDefButTextO(block, TEX, "FILE_OT_directory", 0, "",
183                                  min_x, line1_y, line1_w-chan_offs, btn_h, 
184                                  params->dir, 0.0, (float)FILE_MAX-1, 0, 0, 
185                                  "File path.");
186                 uiButSetCompleteFunc(but, autocomplete_directory, NULL);
187                 uiButSetFlag(but, UI_BUT_NO_UTF8);
188
189                 but = uiDefBut(block, TEX, B_FS_FILENAME, "",
190                                  min_x, line2_y, line2_w-chan_offs, btn_h,
191                                  params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, 
192                                  overwrite_alert ?"File name, overwrite existing." : "File name.");
193                 uiButSetCompleteFunc(but, autocomplete_file, NULL);
194                 uiButSetFlag(but, UI_BUT_NO_UTF8);
195                 
196                 /* check if this overrides a file and if the operator option is used */
197                 if(overwrite_alert) {
198                         uiButSetFlag(but, UI_BUT_REDALERT);
199                 }
200                 
201                 /* clear func */
202                 uiBlockSetFunc(block, NULL, NULL, NULL);
203         }
204         
205         /* Filename number increment / decrement buttons. */
206         if (fnumbuttons) {
207                 uiBlockBeginAlign(block);
208                 but = uiDefIconButO(block, BUT, "FILE_OT_filenum", 0, ICON_ZOOMOUT,
209                                 min_x + line2_w + separator - chan_offs, line2_y, 
210                                 btn_fn_w, btn_h, 
211                                 "Decrement the filename number");    
212                 RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", -1); 
213         
214                 but = uiDefIconButO(block, BUT, "FILE_OT_filenum", 0, ICON_ZOOMIN, 
215                                 min_x + line2_w + separator + btn_fn_w - chan_offs, line2_y, 
216                                 btn_fn_w, btn_h, 
217                                 "Increment the filename number");    
218                 RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", 1); 
219                 uiBlockEndAlign(block);
220         }
221         
222         /* Execute / cancel buttons. */
223         if(loadbutton) {
224                 
225                 uiDefButO(block, BUT, "FILE_OT_execute", WM_OP_EXEC_REGION_WIN, params->title,
226                         max_x - loadbutton, line1_y, loadbutton, btn_h, 
227                         params->title);
228                 uiDefButO(block, BUT, "FILE_OT_cancel", WM_OP_EXEC_REGION_WIN, "Cancel",
229                         max_x - loadbutton, line2_y, loadbutton, btn_h, 
230                         "Cancel");
231         }
232         
233         uiEndBlock(C, block);
234         uiDrawBlock(C, block);
235 }
236
237
238 static void draw_tile(int sx, int sy, int width, int height, int colorid, int shade)
239 {       
240         UI_ThemeColorShade(colorid, shade);
241         uiSetRoundBox(15);      
242         uiRoundBox(sx, sy - height, sx + width, sy, 5);
243 }
244
245
246 static int get_file_icon(struct direntry *file)
247 {
248         if (file->type & S_IFDIR) {
249                 if ( strcmp(file->relname, "..") == 0) {
250                                 return  ICON_FILE_PARENT;
251                 }
252                 if(file->flags & BLENDERFILE) {
253                         return ICON_FILE_BLEND;
254                 }
255                 return ICON_FILE_FOLDER;
256         }
257         else if (file->flags & BLENDERFILE)
258                 return ICON_FILE_BLEND;
259         else if (file->flags & IMAGEFILE)
260                 return ICON_FILE_IMAGE;
261         else if (file->flags & MOVIEFILE)
262                 return ICON_FILE_MOVIE;
263         else if (file->flags & PYSCRIPTFILE)
264                 return ICON_FILE_SCRIPT;
265         else if (file->flags & PYSCRIPTFILE)
266                 return ICON_FILE_SCRIPT;
267         else if (file->flags & SOUNDFILE) 
268                 return ICON_FILE_SOUND;
269         else if (file->flags & FTFONTFILE) 
270                 return ICON_FILE_FONT;
271         else if (file->flags & BTXFILE) 
272                 return ICON_FILE_BLANK;
273         else if (file->flags & COLLADAFILE) 
274                 return ICON_FILE_BLANK;
275         else
276                 return ICON_FILE_BLANK;
277 }
278
279 static void file_draw_icon(uiBlock *block, char *path, int sx, int sy, int icon, int width, int height)
280 {
281         uiBut *but;
282         float x,y;
283         /*float alpha=1.0f;*/
284         
285         x = (float)(sx);
286         y = (float)(sy-height);
287         
288         /*if (icon == ICON_FILE_BLANK) alpha = 0.375f;*/
289                 
290         but= uiDefIconBut(block, LABEL, 0, icon, x, y, width, height, NULL, 0.0, 0.0, 0, 0, "");
291         uiButSetDragPath(but, path);
292 }
293
294
295 static void file_draw_string(int sx, int sy, const char* string, float width, int height, short align)
296 {
297         uiStyle *style= U.uistyles.first;
298         uiFontStyle fs = style->widgetlabel;
299         rcti rect;
300         char fname[FILE_MAXFILE];
301
302         fs.align = align;
303
304         BLI_strncpy(fname,string, FILE_MAXFILE);
305         file_shorten_string(fname, width+1.0, 0);
306
307         /* no text clipping needed, uiStyleFontDraw does it but is a bit too strict (for buttons it works) */
308         rect.xmin = sx;
309         rect.xmax = sx + ceil(width+4.0f);
310         rect.ymin = sy - height;
311         rect.ymax = sy;
312         
313         uiStyleFontDraw(&fs, &rect, fname);
314 }
315
316 void file_calc_previews(const bContext *C, ARegion *ar)
317 {
318         SpaceFile *sfile= CTX_wm_space_file(C);
319         View2D *v2d= &ar->v2d;
320         
321         ED_fileselect_init_layout(sfile, ar);
322         /* +SCROLL_HEIGHT is bad hack to work around issue in UI_view2d_totRect_set */
323         UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height+V2D_SCROLL_HEIGHT);
324 }
325
326 static void file_draw_preview(uiBlock *block, struct direntry *file, int sx, int sy, ImBuf *imb, FileLayout *layout, short dropshadow)
327 {
328         if (imb) {
329                 uiBut *but;
330                 float fx, fy;
331                 float dx, dy;
332                 int xco, yco;
333                 float scaledx, scaledy;
334                 float scale;
335                 int ex, ey;
336                 
337                 if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
338                         if (imb->x > imb->y) {
339                                 scaledx = (float)layout->prv_w;
340                                 scaledy =  ( (float)imb->y/(float)imb->x )*layout->prv_w;
341                                 scale = scaledx/imb->x;
342                         }
343                         else {
344                                 scaledy = (float)layout->prv_h;
345                                 scaledx =  ( (float)imb->x/(float)imb->y )*layout->prv_h;
346                                 scale = scaledy/imb->y;
347                         }
348                 } else {
349                         scaledx = (float)imb->x;
350                         scaledy = (float)imb->y;
351                         scale = 1.0;
352                 }
353                 ex = (int)scaledx;
354                 ey = (int)scaledy;
355                 fx = ((float)layout->prv_w - (float)ex)/2.0f;
356                 fy = ((float)layout->prv_h - (float)ey)/2.0f;
357                 dx = (fx + 0.5f + layout->prv_border_x);
358                 dy = (fy + 0.5f - layout->prv_border_y);
359                 xco = (float)sx + dx;
360                 yco = (float)sy - layout->prv_h + dy;
361                 
362                 glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
363                 
364                 /* shadow */
365                 if (dropshadow)
366                         uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
367                 
368                 glEnable(GL_BLEND);
369                 
370                 /* the image */
371                 glColor4f(1.0, 1.0, 1.0, 1.0);
372                 glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
373                 
374                 /* border */
375                 if (dropshadow) {
376                         glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
377                         fdrawbox(xco, yco, xco + ex, yco + ey);
378                 }
379                 
380                 /* dragregion */
381                 but= uiDefBut(block, LABEL, 0, "", xco, yco, ex, ey, NULL, 0.0, 0.0, 0, 0, "");
382                 uiButSetDragImage(but, file->path, get_file_icon(file), imb, scale);
383                 
384                 glDisable(GL_BLEND);
385                 imb = 0;
386         }
387 }
388
389 static void renamebutton_cb(bContext *C, void *UNUSED(arg1), char *oldname)
390 {
391         char newname[FILE_MAX+12];
392         char orgname[FILE_MAX+12];
393         char filename[FILE_MAX+12];
394         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
395         ARegion* ar = CTX_wm_region(C);
396
397 #if 0
398         struct direntry *file = (struct direntry *)arg1;
399 #endif
400
401         BLI_make_file_string(G.main->name, orgname, sfile->params->dir, oldname);
402         BLI_strncpy(filename, sfile->params->renameedit, sizeof(filename));
403         BLI_make_file_string(G.main->name, newname, sfile->params->dir, filename);
404
405         if( strcmp(orgname, newname) != 0 ) {
406                 if (!BLI_exists(newname)) {
407                         BLI_rename(orgname, newname);
408                         /* to make sure we show what is on disk */
409 #if 0           /* this is cleared anyway, no need */
410                         MEM_freeN(file->relname);
411                         file->relname= BLI_strdup(sfile->params->renameedit);
412 #endif
413                         ED_fileselect_clear(C, sfile);
414                 }
415
416                 ED_region_tag_redraw(ar);
417         }
418 }
419
420
421 static void draw_background(FileLayout *layout, View2D *v2d)
422 {
423         int i;
424         int sy;
425
426         /* alternating flat shade background */
427         for (i=0; (i <= layout->rows); i+=2)
428         {
429                 sy = v2d->cur.ymax - i*(layout->tile_h+2*layout->tile_border_y) - layout->tile_border_y;
430
431                 UI_ThemeColorShade(TH_BACK, -7);
432                 glRectf(v2d->cur.xmin, sy, v2d->cur.xmax, sy+layout->tile_h+2*layout->tile_border_y);
433                 
434         }
435 }
436
437 static void draw_dividers(FileLayout *layout, View2D *v2d)
438 {
439         int sx;
440
441         /* vertical column dividers */
442         sx = v2d->tot.xmin;
443         while (sx < v2d->cur.xmax) {
444                 sx += (layout->tile_w+2*layout->tile_border_x);
445                 
446                 UI_ThemeColorShade(TH_BACK, 30);
447                 sdrawline(sx+1,  v2d->cur.ymax - layout->tile_border_y ,  sx+1,  v2d->cur.ymin); 
448                 UI_ThemeColorShade(TH_BACK, -30);
449                 sdrawline(sx,  v2d->cur.ymax - layout->tile_border_y ,  sx,  v2d->cur.ymin); 
450         }
451 }
452
453 void file_draw_list(const bContext *C, ARegion *ar)
454 {
455         SpaceFile *sfile= CTX_wm_space_file(C);
456         FileSelectParams* params = ED_fileselect_get_params(sfile);
457         FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
458         View2D *v2d= &ar->v2d;
459         struct FileList* files = sfile->files;
460         struct direntry *file;
461         ImBuf *imb;
462         uiBlock *block = uiBeginBlock(C, ar, "FileNames", UI_EMBOSS);
463         int numfiles;
464         int numfiles_layout;
465         int colorid = 0;
466         int sx, sy;
467         int offset;
468         int textwidth, textheight;
469         int i;
470         short is_icon;
471         short align;
472
473
474         numfiles = filelist_numfiles(files);
475         
476         if (params->display != FILE_IMGDISPLAY) {
477
478                 draw_background(layout, v2d);
479         
480                 draw_dividers(layout, v2d);
481         }
482
483         offset = ED_fileselect_layout_offset(layout, 0, ar->v2d.cur.xmin, -ar->v2d.cur.ymax);
484         if (offset<0) offset=0;
485
486         numfiles_layout = ED_fileselect_layout_numfiles(layout, ar);
487
488         /* adjust, so the next row is already drawn when scrolling */
489         if (layout->flag & FILE_LAYOUT_HOR) {
490                 numfiles_layout += layout->rows;
491         } else {
492                 numfiles_layout += layout->columns;
493         }
494
495         textwidth =( FILE_IMGDISPLAY == params->display) ? layout->tile_w : layout->column_widths[COLUMN_NAME];
496         textheight = layout->textheight*3.0/2.0 + 0.5;
497
498         align = ( FILE_IMGDISPLAY == params->display) ? UI_STYLE_TEXT_CENTER : UI_STYLE_TEXT_LEFT;
499
500         for (i=offset; (i < numfiles) && (i<offset+numfiles_layout); ++i)
501         {
502                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
503                 sx += v2d->tot.xmin+2;
504                 sy = v2d->tot.ymax - sy;
505
506                 file = filelist_file(files, i); 
507                 
508                 UI_ThemeColor4(TH_TEXT);
509
510
511                 if (!(file->flags & EDITING)) {
512                         if (params->active_file == i) {
513                                 if (file->flags & ACTIVEFILE) colorid= TH_HILITE;
514                                 else colorid = TH_BACK;
515                                 draw_tile(sx, sy-1, layout->tile_w+4, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
516                         } else if (file->flags & ACTIVEFILE) {
517                                 colorid = TH_HILITE;
518                                 draw_tile(sx, sy-1, layout->tile_w+4, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
519                         } 
520                 }
521                 uiSetRoundBox(0);
522
523                 if ( FILE_IMGDISPLAY == params->display ) {
524                         is_icon = 0;
525                         imb = filelist_getimage(files, i);
526                         if (!imb) {
527                                 imb = filelist_geticon(files,i);
528                                 is_icon = 1;
529                         }
530                         
531                         file_draw_preview(block, file, sx, sy, imb, layout, !is_icon && (file->flags & IMAGEFILE));
532                 } else {
533                         file_draw_icon(block, file->path, sx, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
534                         sx += ICON_DEFAULT_WIDTH + 4;
535                 }
536
537                 UI_ThemeColor4(TH_TEXT);
538
539                 if (file->flags & EDITING) {
540                         uiBut *but = uiDefBut(block, TEX, 1, "", sx , sy-layout->tile_h-3, 
541                                 textwidth, textheight, sfile->params->renameedit, 1.0f, (float)sizeof(sfile->params->renameedit),0,0,"");
542                         uiButSetRenameFunc(but, renamebutton_cb, file);
543                         uiButSetFlag(but, UI_BUT_NO_UTF8); /* allow non utf8 names */
544                         if ( 0 == uiButActiveOnly(C, block, but)) {
545                                 file->flags &= ~EDITING;
546                         }
547                 }
548
549                 if (!(file->flags & EDITING))  {
550                         int tpos = (FILE_IMGDISPLAY == params->display) ? sy - layout->tile_h + layout->textheight : sy;
551                         file_draw_string(sx+1, tpos, file->relname, textwidth, textheight, align);
552                 }
553
554                 if (params->display == FILE_SHORTDISPLAY) {
555                         sx += layout->column_widths[COLUMN_NAME] + 12;
556                         if (!(file->type & S_IFDIR)) {
557                                 file_draw_string(sx, sy, file->size, layout->column_widths[COLUMN_SIZE], layout->tile_h, align);        
558                                 sx += layout->column_widths[COLUMN_SIZE] + 12;
559                         }
560                 } else if (params->display == FILE_LONGDISPLAY) {
561                         sx += layout->column_widths[COLUMN_NAME] + 12;
562
563 #ifndef WIN32
564                         /* rwx rwx rwx */
565                         file_draw_string(sx, sy, file->mode1, layout->column_widths[COLUMN_MODE1], layout->tile_h, align); 
566                         sx += layout->column_widths[COLUMN_MODE1] + 12;
567
568                         file_draw_string(sx, sy, file->mode2, layout->column_widths[COLUMN_MODE2], layout->tile_h, align);
569                         sx += layout->column_widths[COLUMN_MODE2] + 12;
570
571                         file_draw_string(sx, sy, file->mode3, layout->column_widths[COLUMN_MODE3], layout->tile_h, align);
572                         sx += layout->column_widths[COLUMN_MODE3] + 12;
573
574                         file_draw_string(sx, sy, file->owner, layout->column_widths[COLUMN_OWNER] , layout->tile_h, align);
575                         sx += layout->column_widths[COLUMN_OWNER] + 12;
576 #endif
577
578                         file_draw_string(sx, sy, file->date, layout->column_widths[COLUMN_DATE], layout->tile_h, align);
579                         sx += layout->column_widths[COLUMN_DATE] + 12;
580
581                         file_draw_string(sx, sy, file->time, layout->column_widths[COLUMN_TIME] , layout->tile_h, align); 
582                         sx += layout->column_widths[COLUMN_TIME] + 12;
583
584                         if (!(file->type & S_IFDIR)) {
585                                 file_draw_string(sx, sy, file->size, layout->column_widths[COLUMN_SIZE], layout->tile_h, align);
586                                 sx += layout->column_widths[COLUMN_SIZE] + 12;
587                         }
588                 }
589         }
590
591         uiEndBlock(C, block);
592         uiDrawBlock(C, block);
593
594 }
595