use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender.git] / source / blender / editors / space_file / file_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_file/file_draw.c
28  *  \ingroup spfile
29  */
30
31
32 #include <math.h>
33 #include <string.h>
34
35 #include "BLI_blenlib.h"
36 #include "BLI_utildefines.h"
37 #include "BLI_dynstr.h"
38 #ifdef WIN32
39 #include "BLI_winstuff.h"
40 #endif
41
42 #include "BIF_gl.h"
43 #include "BIF_glutil.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_main.h"
48
49 #include "BLF_api.h"
50 #include "BLF_translation.h"
51
52 #include "IMB_imbuf_types.h"
53  
54 #include "MEM_guardedalloc.h"
55
56 #include "DNA_userdef_types.h"
57
58 #include "RNA_access.h"
59
60 #include "ED_fileselect.h"
61 #include "ED_screen.h"
62
63 #include "UI_interface.h"
64 #include "UI_interface_icons.h"
65 #include "UI_resources.h"
66 #include "UI_view2d.h"
67
68 #include "WM_types.h"
69
70 #include "fsmenu.h"
71 #include "filelist.h"
72
73 #include "file_intern.h"        // own include
74
75 /* button events */
76 enum {
77         B_FS_DIRNAME,
78         B_FS_FILENAME
79 } /*eFile_ButEvents*/;
80
81
82 static void do_file_buttons(bContext *C, void *UNUSED(arg), int event)
83 {
84         switch(event) {
85                 case B_FS_FILENAME:
86                         file_filename_exec(C, NULL);
87                         break;
88                 case B_FS_DIRNAME:
89                         file_directory_exec(C, NULL);
90                         break;
91         }
92 }
93
94 /* Note: This function uses pixelspace (0, 0, winx, winy), not view2d. 
95  * The controls are laid out as follows:
96  *
97  * -------------------------------------------
98  * | Directory input               | execute |
99  * -------------------------------------------
100  * | Filename input        | + | - | cancel  |
101  * -------------------------------------------
102  *
103  * The input widgets will stretch to fill any excess space.
104  * When there isn't enough space for all controls to be shown, they are
105  * hidden in this order: x/-, execute/cancel, input widgets.
106  */
107 void file_draw_buttons(const bContext *C, ARegion *ar)
108 {
109         /* Button layout. */
110         const int max_x      = ar->winx - 10;
111         const int line1_y    = ar->winy - (IMASEL_BUTTONS_HEIGHT/2 + IMASEL_BUTTONS_MARGIN);
112         const int line2_y    = line1_y - (IMASEL_BUTTONS_HEIGHT/2 + IMASEL_BUTTONS_MARGIN);
113         const int input_minw = 20;
114         const int btn_h      = UI_UNIT_Y;
115         const int btn_fn_w   = UI_UNIT_X;
116         const int btn_minw   = 80;
117         const int btn_margin = 20;
118         const int separator  = 4;
119
120         /* Additional locals. */
121         char uiblockstr[32];
122         int loadbutton;
123         int fnumbuttons;
124         int min_x       = 10;
125         int chan_offs   = 0;
126         int available_w = max_x - min_x;
127         int line1_w     = available_w;
128         int line2_w     = available_w;
129         
130         uiBut*            but;
131         uiBlock*          block;
132         SpaceFile*        sfile  = CTX_wm_space_file(C);
133         FileSelectParams* params = ED_fileselect_get_params(sfile);
134         ARegion*                  artmp;
135         
136         /* Initialize UI block. */
137         BLI_snprintf(uiblockstr, sizeof(uiblockstr), "win %p", (void *)ar);
138         block = uiBeginBlock(C, ar, uiblockstr, UI_EMBOSS);
139         uiBlockSetHandleFunc(block, do_file_buttons, NULL);
140
141         /* exception to make space for collapsed region icon */
142         for (artmp=CTX_wm_area(C)->regionbase.first; artmp; artmp=artmp->next) {
143                 if (artmp->regiontype == RGN_TYPE_CHANNELS && artmp->flag & RGN_FLAG_HIDDEN) {
144                         chan_offs = 16;
145                         min_x += chan_offs;
146                         available_w -= chan_offs;
147                 }
148         }
149         
150         /* Is there enough space for the execute / cancel buttons? */
151         loadbutton = UI_GetStringWidth(sfile->params->title) + btn_margin;
152         if (loadbutton < btn_minw) {
153                 loadbutton = MAX2(btn_minw, 
154                                                   btn_margin + UI_GetStringWidth(params->title));
155         }
156         
157         if (available_w <= loadbutton + separator + input_minw 
158          || params->title[0] == 0) {
159                 loadbutton = 0;
160         } else {
161                 line1_w -= (loadbutton + separator);
162                 line2_w  = line1_w;
163         }
164
165         /* Is there enough space for file number increment/decrement buttons? */
166         fnumbuttons = 2 * btn_fn_w;
167         if (!loadbutton || line2_w <= fnumbuttons + separator + input_minw) {
168                 fnumbuttons = 0;
169         } else {
170                 line2_w -= (fnumbuttons + separator);
171         }
172         
173         /* Text input fields for directory and file. */
174         if (available_w > 0) {
175                 int overwrite_alert= file_draw_check_exists(sfile);
176                 /* callbacks for operator check functions */
177                 uiBlockSetFunc(block, file_draw_check_cb, NULL, NULL);
178
179                 but = uiDefButTextO(block, TEX, "FILE_OT_directory", 0, "",
180                                     min_x, line1_y, line1_w-chan_offs, btn_h,
181                                     params->dir, 0.0, (float)FILE_MAX, 0, 0,
182                                     TIP_("File path"));
183                 uiButSetCompleteFunc(but, autocomplete_directory, NULL);
184                 uiButSetFlag(but, UI_BUT_NO_UTF8);
185
186                 if((params->flag & FILE_DIRSEL_ONLY) == 0) {
187                         but = uiDefBut(block, TEX, B_FS_FILENAME, "",
188                                        min_x, line2_y, line2_w-chan_offs, btn_h,
189                                        params->file, 0.0, (float)FILE_MAXFILE, 0, 0,
190                                        TIP_(overwrite_alert ?N_("File name, overwrite existing") : N_("File name")));
191                         uiButSetCompleteFunc(but, autocomplete_file, NULL);
192                         uiButSetFlag(but, UI_BUT_NO_UTF8);
193
194                         /* check if this overrides a file and if the operator option is used */
195                         if(overwrite_alert) {
196                                 uiButSetFlag(but, UI_BUT_REDALERT);
197                         }
198                 }
199                 
200                 /* clear func */
201                 uiBlockSetFunc(block, NULL, NULL, NULL);
202         }
203         
204         /* Filename number increment / decrement buttons. */
205         if (fnumbuttons && (params->flag & FILE_DIRSEL_ONLY) == 0) {
206                 uiBlockBeginAlign(block);
207                 but = uiDefIconButO(block, BUT, "FILE_OT_filenum", 0, ICON_ZOOMOUT,
208                                     min_x + line2_w + separator - chan_offs, line2_y,
209                                     btn_fn_w, btn_h,
210                                     TIP_("Decrement the filename number"));
211                 RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", -1);
212
213                 but = uiDefIconButO(block, BUT, "FILE_OT_filenum", 0, ICON_ZOOMIN,
214                                     min_x + line2_w + separator + btn_fn_w - chan_offs, line2_y,
215                                     btn_fn_w, btn_h,
216                                     TIP_("Increment the filename number"));
217                 RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", 1);
218                 uiBlockEndAlign(block);
219         }
220         
221         /* Execute / cancel buttons. */
222         if(loadbutton) {
223                 
224                 uiDefButO(block, BUT, "FILE_OT_execute", WM_OP_EXEC_REGION_WIN, params->title,
225                         max_x - loadbutton, line1_y, loadbutton, btn_h, 
226                         params->title);
227                 uiDefButO(block, BUT, "FILE_OT_cancel", WM_OP_EXEC_REGION_WIN, IFACE_("Cancel"),
228                         max_x - loadbutton, line2_y, loadbutton, btn_h, 
229                         TIP_("Cancel"));
230         }
231         
232         uiEndBlock(C, block);
233         uiDrawBlock(C, block);
234 }
235
236
237 static void draw_tile(int sx, int sy, int width, int height, int colorid, int shade)
238 {
239         UI_ThemeColorShade(colorid, shade);
240         uiSetRoundBox(UI_CNR_ALL);
241         uiRoundBox((float)sx, (float)(sy - height), (float)(sx + width), (float)sy, 5.0f);
242 }
243
244
245 static int get_file_icon(struct direntry *file)
246 {
247         if (file->type & S_IFDIR) {
248                 if ( strcmp(file->relname, "..") == 0) {
249                                 return  ICON_FILE_PARENT;
250                 }
251                 if(file->flags & BLENDERFILE) {
252                         return ICON_FILE_BLEND;
253                 }
254                 return ICON_FILE_FOLDER;
255         }
256         else if (file->flags & BLENDERFILE)
257                 return ICON_FILE_BLEND;
258         else if (file->flags & BLENDERFILE_BACKUP)
259                 return ICON_FILE_BLEND;
260         else if (file->flags & IMAGEFILE)
261                 return ICON_FILE_IMAGE;
262         else if (file->flags & MOVIEFILE)
263                 return ICON_FILE_MOVIE;
264         else if (file->flags & PYSCRIPTFILE)
265                 return ICON_FILE_SCRIPT;
266         else if (file->flags & SOUNDFILE) 
267                 return ICON_FILE_SOUND;
268         else if (file->flags & FTFONTFILE) 
269                 return ICON_FILE_FONT;
270         else if (file->flags & BTXFILE) 
271                 return ICON_FILE_BLANK;
272         else if (file->flags & COLLADAFILE) 
273                 return ICON_FILE_BLANK;
274         else
275                 return ICON_FILE_BLANK;
276 }
277
278 static void file_draw_icon(uiBlock *block, char *path, int sx, int sy, int icon, int width, int height)
279 {
280         uiBut *but;
281         int x,y;
282         /*float alpha=1.0f;*/
283         
284         x = sx;
285         y = sy-height;
286         
287         /*if (icon == ICON_FILE_BLANK) alpha = 0.375f;*/
288
289         but= uiDefIconBut(block, LABEL, 0, icon, x, y, width, height, NULL, 0.0f, 0.0f, 0.0f, 0.0f, "");
290         uiButSetDragPath(but, path);
291 }
292
293
294 static void file_draw_string(int sx, int sy, const char* string, float width, int height, short align)
295 {
296         uiStyle *style= UI_GetStyle();
297         uiFontStyle fs = style->widgetlabel;
298         rcti rect;
299         char fname[FILE_MAXFILE];
300
301         fs.align = align;
302
303         BLI_strncpy(fname,string, FILE_MAXFILE);
304         file_shorten_string(fname, width + 1.0f, 0);
305
306         /* no text clipping needed, uiStyleFontDraw does it but is a bit too strict (for buttons it works) */
307         rect.xmin = sx;
308         rect.xmax = (int)(sx + ceil(width+4.0f));
309         rect.ymin = sy - height;
310         rect.ymax = sy;
311         
312         uiStyleFontDraw(&fs, &rect, fname);
313 }
314
315 void file_calc_previews(const bContext *C, ARegion *ar)
316 {
317         SpaceFile *sfile= CTX_wm_space_file(C);
318         View2D *v2d= &ar->v2d;
319         
320         ED_fileselect_init_layout(sfile, ar);
321         /* +SCROLL_HEIGHT is bad hack to work around issue in UI_view2d_totRect_set */
322         UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height+V2D_SCROLL_HEIGHT);
323 }
324
325 static void file_draw_preview(uiBlock *block, struct direntry *file, int sx, int sy, ImBuf *imb, FileLayout *layout, short dropshadow)
326 {
327         if (imb) {
328                 uiBut *but;
329                 float fx, fy;
330                 float dx, dy;
331                 int xco, yco;
332                 float scaledx, scaledy;
333                 float scale;
334                 int ex, ey;
335                 
336                 if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
337                         if (imb->x > imb->y) {
338                                 scaledx = (float)layout->prv_w;
339                                 scaledy =  ( (float)imb->y/(float)imb->x )*layout->prv_w;
340                                 scale = scaledx/imb->x;
341                         }
342                         else {
343                                 scaledy = (float)layout->prv_h;
344                                 scaledx =  ( (float)imb->x/(float)imb->y )*layout->prv_h;
345                                 scale = scaledy/imb->y;
346                         }
347                 } else {
348                         scaledx = (float)imb->x;
349                         scaledy = (float)imb->y;
350                         scale = 1.0;
351                 }
352                 ex = (int)scaledx;
353                 ey = (int)scaledy;
354                 fx = ((float)layout->prv_w - (float)ex)/2.0f;
355                 fy = ((float)layout->prv_h - (float)ey)/2.0f;
356                 dx = (fx + 0.5f + layout->prv_border_x);
357                 dy = (fy + 0.5f - layout->prv_border_y);
358                 xco = sx + (int)dx;
359                 yco = sy - layout->prv_h + (int)dy;
360                 
361                 glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
362                 
363                 /* shadow */
364                 if (dropshadow)
365                         uiDrawBoxShadow(220, (float)xco, (float)yco, (float)(xco + ex), (float)(yco + ey));
366
367                 glEnable(GL_BLEND);
368                 
369                 /* the image */
370                 glColor4f(1.0, 1.0, 1.0, 1.0);
371                 glaDrawPixelsTexScaled((float)xco, (float)yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
372                 
373                 /* border */
374                 if (dropshadow) {
375                         glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
376                         fdrawbox((float)xco, (float)yco, (float)(xco + ex), (float)(yco + ey));
377                 }
378                 
379                 /* dragregion */
380                 but= uiDefBut(block, LABEL, 0, "", xco, yco, ex, ey, NULL, 0.0, 0.0, 0, 0, "");
381                 uiButSetDragImage(but, file->path, get_file_icon(file), imb, scale);
382                 
383                 glDisable(GL_BLEND);
384                 imb = NULL;
385         }
386 }
387
388 static void renamebutton_cb(bContext *C, void *UNUSED(arg1), char *oldname)
389 {
390         char newname[FILE_MAX+12];
391         char orgname[FILE_MAX+12];
392         char filename[FILE_MAX+12];
393         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
394         ARegion* ar = CTX_wm_region(C);
395
396         BLI_make_file_string(G.main->name, orgname, sfile->params->dir, oldname);
397         BLI_strncpy(filename, sfile->params->renameedit, sizeof(filename));
398         BLI_make_file_string(G.main->name, newname, sfile->params->dir, filename);
399
400         if( strcmp(orgname, newname) != 0 ) {
401                 if (!BLI_exists(newname)) {
402                         BLI_rename(orgname, newname);
403                         /* to make sure we show what is on disk */
404                         ED_fileselect_clear(C, sfile);
405                 }
406
407                 ED_region_tag_redraw(ar);
408         }
409 }
410
411
412 static void draw_background(FileLayout *layout, View2D *v2d)
413 {
414         int i;
415         int sy;
416
417         /* alternating flat shade background */
418         for (i=0; (i <= layout->rows); i+=2)
419         {
420                 sy = (int)v2d->cur.ymax - i*(layout->tile_h+2*layout->tile_border_y) - layout->tile_border_y;
421
422                 UI_ThemeColorShade(TH_BACK, -7);
423                 glRectf(v2d->cur.xmin, (float)sy, v2d->cur.xmax, (float)(sy+layout->tile_h+2*layout->tile_border_y));
424                 
425         }
426 }
427
428 static void draw_dividers(FileLayout *layout, View2D *v2d)
429 {
430         int sx;
431
432         /* vertical column dividers */
433         sx = (int)v2d->tot.xmin;
434         while (sx < v2d->cur.xmax) {
435                 sx += (layout->tile_w+2*layout->tile_border_x);
436                 
437                 UI_ThemeColorShade(TH_BACK, 30);
438                 sdrawline(sx+1, (short)(v2d->cur.ymax - layout->tile_border_y),  sx+1,  (short)v2d->cur.ymin); 
439                 UI_ThemeColorShade(TH_BACK, -30);
440                 sdrawline(sx, (short)(v2d->cur.ymax - layout->tile_border_y),  sx,  (short)v2d->cur.ymin); 
441         }
442 }
443
444 void file_draw_list(const bContext *C, ARegion *ar)
445 {
446         SpaceFile *sfile= CTX_wm_space_file(C);
447         FileSelectParams* params = ED_fileselect_get_params(sfile);
448         FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
449         View2D *v2d= &ar->v2d;
450         struct FileList* files = sfile->files;
451         struct direntry *file;
452         ImBuf *imb;
453         uiBlock *block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
454         int numfiles;
455         int numfiles_layout;
456         int sx, sy;
457         int offset;
458         int textwidth, textheight;
459         int i;
460         short is_icon;
461         short align;
462
463
464         numfiles = filelist_numfiles(files);
465         
466         if (params->display != FILE_IMGDISPLAY) {
467
468                 draw_background(layout, v2d);
469         
470                 draw_dividers(layout, v2d);
471         }
472
473         offset = ED_fileselect_layout_offset(layout, (int)ar->v2d.cur.xmin, (int)-ar->v2d.cur.ymax);
474         if (offset<0) offset=0;
475
476         numfiles_layout = ED_fileselect_layout_numfiles(layout, ar);
477
478         /* adjust, so the next row is already drawn when scrolling */
479         if (layout->flag & FILE_LAYOUT_HOR) {
480                 numfiles_layout += layout->rows;
481         } else {
482                 numfiles_layout += layout->columns;
483         }
484
485         textwidth =( FILE_IMGDISPLAY == params->display) ? layout->tile_w : (int)layout->column_widths[COLUMN_NAME];
486         textheight = (int)(layout->textheight*3.0/2.0 + 0.5);
487
488         align = ( FILE_IMGDISPLAY == params->display) ? UI_STYLE_TEXT_CENTER : UI_STYLE_TEXT_LEFT;
489
490         for (i=offset; (i < numfiles) && (i<offset+numfiles_layout); ++i)
491         {
492                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
493                 sx += (int)(v2d->tot.xmin+2.0f);
494                 sy = (int)(v2d->tot.ymax - sy);
495
496                 file = filelist_file(files, i); 
497                 
498                 UI_ThemeColor4(TH_TEXT);
499
500
501                 if (!(file->selflag & EDITING_FILE)) {
502                         if  ((params->active_file == i) || (file->selflag & HILITED_FILE) || (file->selflag & SELECTED_FILE) ) {
503                                 int colorid = (file->selflag & SELECTED_FILE) ? TH_HILITE : TH_BACK;
504                                 int shade = (params->active_file == i) || (file->selflag & HILITED_FILE) ? 20 : 0;
505                                 draw_tile(sx, sy-1, layout->tile_w+4, sfile->layout->tile_h+layout->tile_border_y, colorid, shade);
506                         }
507                 }
508                 uiSetRoundBox(UI_CNR_NONE);
509
510                 if ( FILE_IMGDISPLAY == params->display ) {
511                         is_icon = 0;
512                         imb = filelist_getimage(files, i);
513                         if (!imb) {
514                                 imb = filelist_geticon(files,i);
515                                 is_icon = 1;
516                         }
517                         
518                         file_draw_preview(block, file, sx, sy, imb, layout, !is_icon && (file->flags & IMAGEFILE));
519                 } else {
520                         file_draw_icon(block, file->path, sx, sy-(UI_UNIT_Y / 6), get_file_icon(file), ICON_DEFAULT_WIDTH_SCALE, ICON_DEFAULT_HEIGHT_SCALE);
521                         sx += ICON_DEFAULT_WIDTH_SCALE + 4;
522                 }
523
524                 UI_ThemeColor4(TH_TEXT);
525
526                 if (file->selflag & EDITING_FILE) {
527                         uiBut *but = uiDefBut(block, TEX, 1, "", sx , sy-layout->tile_h-3, 
528                                 textwidth, textheight, sfile->params->renameedit, 1.0f, (float)sizeof(sfile->params->renameedit),0,0,"");
529                         uiButSetRenameFunc(but, renamebutton_cb, file);
530                         uiButSetFlag(but, UI_BUT_NO_UTF8); /* allow non utf8 names */
531                         if ( 0 == uiButActiveOnly(C, block, but)) {
532                                 file->selflag &= ~EDITING_FILE;
533                         }
534                 }
535
536                 if (!(file->selflag & EDITING_FILE))  {
537                         int tpos = (FILE_IMGDISPLAY == params->display) ? sy - layout->tile_h + layout->textheight : sy;
538                         file_draw_string(sx+1, tpos, file->relname, (float)textwidth, textheight, align);
539                 }
540
541                 if (params->display == FILE_SHORTDISPLAY) {
542                         sx += (int)layout->column_widths[COLUMN_NAME] + 12;
543                         if (!(file->type & S_IFDIR)) {
544                                 file_draw_string(sx, sy, file->size, layout->column_widths[COLUMN_SIZE], layout->tile_h, align);        
545                                 sx += (int)layout->column_widths[COLUMN_SIZE] + 12;
546                         }
547                 } else if (params->display == FILE_LONGDISPLAY) {
548                         sx += (int)layout->column_widths[COLUMN_NAME] + 12;
549
550 #ifndef WIN32
551                         /* rwx rwx rwx */
552                         file_draw_string(sx, sy, file->mode1, layout->column_widths[COLUMN_MODE1], layout->tile_h, align); 
553                         sx += layout->column_widths[COLUMN_MODE1] + 12;
554
555                         file_draw_string(sx, sy, file->mode2, layout->column_widths[COLUMN_MODE2], layout->tile_h, align);
556                         sx += layout->column_widths[COLUMN_MODE2] + 12;
557
558                         file_draw_string(sx, sy, file->mode3, layout->column_widths[COLUMN_MODE3], layout->tile_h, align);
559                         sx += layout->column_widths[COLUMN_MODE3] + 12;
560
561                         file_draw_string(sx, sy, file->owner, layout->column_widths[COLUMN_OWNER] , layout->tile_h, align);
562                         sx += layout->column_widths[COLUMN_OWNER] + 12;
563 #endif
564
565                         file_draw_string(sx, sy, file->date, layout->column_widths[COLUMN_DATE], layout->tile_h, align);
566                         sx += (int)layout->column_widths[COLUMN_DATE] + 12;
567
568                         file_draw_string(sx, sy, file->time, layout->column_widths[COLUMN_TIME] , layout->tile_h, align); 
569                         sx += (int)layout->column_widths[COLUMN_TIME] + 12;
570
571                         if (!(file->type & S_IFDIR)) {
572                                 file_draw_string(sx, sy, file->size, layout->column_widths[COLUMN_SIZE], layout->tile_h, align);
573                                 sx += (int)layout->column_widths[COLUMN_SIZE] + 12;
574                         }
575                 }
576         }
577
578         uiEndBlock(C, block);
579         uiDrawBlock(C, block);
580
581 }