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