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