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