Merge branch 'blender2.7'
[blender.git] / source / blender / editors / space_file / file_draw.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup spfile
22  */
23
24
25 #include <math.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "BLI_blenlib.h"
30 #include "BLI_utildefines.h"
31 #include "BLI_math.h"
32
33 #ifdef WIN32
34 #  include "BLI_winstuff.h"
35 #endif
36
37 #include "BIF_glutil.h"
38
39 #include "BKE_context.h"
40 #include "BKE_main.h"
41
42 #include "BLO_readfile.h"
43
44 #include "BLT_translation.h"
45
46 #include "BLF_api.h"
47
48 #include "IMB_imbuf_types.h"
49
50 #include "DNA_userdef_types.h"
51 #include "DNA_windowmanager_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_api.h"
64 #include "WM_types.h"
65
66 #include "GPU_immediate.h"
67 #include "GPU_immediate_util.h"
68 #include "GPU_state.h"
69
70 #include "filelist.h"
71
72 #include "file_intern.h"    // own include
73
74 /* Dummy helper - we need dynamic tooltips here. */
75 static char *file_draw_tooltip_func(bContext *UNUSED(C), void *argN, const char *UNUSED(tip))
76 {
77         char *dyn_tooltip = argN;
78         return BLI_strdup(dyn_tooltip);
79 }
80
81 /* Note: This function uses pixelspace (0, 0, winx, winy), not view2d.
82  * The controls are laid out as follows:
83  *
84  * -------------------------------------------
85  * | Directory input               | execute |
86  * -------------------------------------------
87  * | Filename input        | + | - | cancel  |
88  * -------------------------------------------
89  *
90  * The input widgets will stretch to fill any excess space.
91  * When there isn't enough space for all controls to be shown, they are
92  * hidden in this order: x/-, execute/cancel, input widgets.
93  */
94 void file_draw_buttons(const bContext *C, ARegion *ar)
95 {
96         /* Button layout. */
97         const int max_x      = ar->winx - 10;
98         const int line1_y    = ar->winy - (IMASEL_BUTTONS_HEIGHT / 2 + IMASEL_BUTTONS_MARGIN);
99         const int line2_y    = line1_y - (IMASEL_BUTTONS_HEIGHT / 2 + IMASEL_BUTTONS_MARGIN);
100         const int input_minw = 20;
101         const int btn_h      = UI_UNIT_Y;
102         const int btn_fn_w   = UI_UNIT_X;
103         const int btn_minw   = 80;
104         const int btn_margin = 20;
105         const int separator  = 4;
106
107         /* Additional locals. */
108         char uiblockstr[32];
109         int loadbutton;
110         int fnumbuttons;
111         int min_x       = 10;
112         int chan_offs   = 0;
113         int available_w = max_x - min_x;
114         int line1_w     = available_w;
115         int line2_w     = available_w;
116
117         uiBut *but;
118         uiBlock *block;
119         SpaceFile *sfile  = CTX_wm_space_file(C);
120         FileSelectParams *params = ED_fileselect_get_params(sfile);
121         ARegion *artmp;
122         const bool is_browse_only = (sfile->op == NULL);
123
124         /* Initialize UI block. */
125         BLI_snprintf(uiblockstr, sizeof(uiblockstr), "win %p", (void *)ar);
126         block = UI_block_begin(C, ar, uiblockstr, UI_EMBOSS);
127
128         /* exception to make space for collapsed region icon */
129         for (artmp = CTX_wm_area(C)->regionbase.first; artmp; artmp = artmp->next) {
130                 if (artmp->regiontype == RGN_TYPE_TOOLS && artmp->flag & RGN_FLAG_HIDDEN) {
131                         chan_offs = 16;
132                         min_x += chan_offs;
133                         available_w -= chan_offs;
134                 }
135         }
136
137         /* Is there enough space for the execute / cancel buttons? */
138
139
140         if (is_browse_only) {
141                 loadbutton = 0;
142         }
143         else {
144                 const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
145                 loadbutton = UI_fontstyle_string_width(fstyle, params->title) + btn_margin;
146                 CLAMP_MIN(loadbutton, btn_minw);
147                 if (available_w <= loadbutton + separator + input_minw) {
148                         loadbutton = 0;
149                 }
150         }
151
152         if (loadbutton) {
153                 line1_w -= (loadbutton + separator);
154                 line2_w  = line1_w;
155         }
156
157         /* Is there enough space for file number increment/decrement buttons? */
158         fnumbuttons = 2 * btn_fn_w;
159         if (!loadbutton || line2_w <= fnumbuttons + separator + input_minw) {
160                 fnumbuttons = 0;
161         }
162         else {
163                 line2_w -= (fnumbuttons + separator);
164         }
165
166         /* Text input fields for directory and file. */
167         if (available_w > 0) {
168                 const struct FileDirEntry *file = sfile->files ? filelist_file(sfile->files, params->active_file) : NULL;
169                 int overwrite_alert = file_draw_check_exists(sfile);
170                 const bool is_active_dir = file && (file->typeflag & FILE_TYPE_FOLDER);
171
172                 /* callbacks for operator check functions */
173                 UI_block_func_set(block, file_draw_check_cb, NULL, NULL);
174
175                 but = uiDefBut(block, UI_BTYPE_TEXT, -1, "",
176                                min_x, line1_y, line1_w - chan_offs, btn_h,
177                                params->dir, 0.0, (float)FILE_MAX, 0, 0,
178                                TIP_("File path"));
179                 UI_but_func_complete_set(but, autocomplete_directory, NULL);
180                 UI_but_flag_enable(but, UI_BUT_NO_UTF8);
181                 UI_but_flag_disable(but, UI_BUT_UNDO);
182                 UI_but_funcN_set(but, file_directory_enter_handle, NULL, but);
183
184                 /* TODO, directory editing is non-functional while a library is loaded
185                  * until this is properly supported just disable it. */
186                 if (sfile->files && filelist_lib(sfile->files))
187                         UI_but_flag_enable(but, UI_BUT_DISABLED);
188
189                 if ((params->flag & FILE_DIRSEL_ONLY) == 0) {
190                         but = uiDefBut(block, UI_BTYPE_TEXT, -1, "",
191                                        min_x, line2_y, line2_w - chan_offs, btn_h,
192                                        is_active_dir ? (char *)"" : params->file,
193                                        0.0, (float)FILE_MAXFILE, 0, 0,
194                                        TIP_(overwrite_alert ? N_("File name, overwrite existing") : N_("File name")));
195                         UI_but_func_complete_set(but, autocomplete_file, NULL);
196                         UI_but_flag_enable(but, UI_BUT_NO_UTF8);
197                         UI_but_flag_disable(but, UI_BUT_UNDO);
198                         /* silly workaround calling NFunc to ensure this does not get called
199                          * immediate ui_apply_but_func but only after button deactivates */
200                         UI_but_funcN_set(but, file_filename_enter_handle, NULL, but);
201
202                         /* check if this overrides a file and if the operator option is used */
203                         if (overwrite_alert) {
204                                 UI_but_flag_enable(but, UI_BUT_REDALERT);
205                         }
206                 }
207
208                 /* clear func */
209                 UI_block_func_set(block, NULL, NULL, NULL);
210         }
211
212         /* Filename number increment / decrement buttons. */
213         if (fnumbuttons && (params->flag & FILE_DIRSEL_ONLY) == 0) {
214                 UI_block_align_begin(block);
215                 but = uiDefIconButO(block, UI_BTYPE_BUT, "FILE_OT_filenum", 0, ICON_REMOVE,
216                                     min_x + line2_w + separator - chan_offs, line2_y,
217                                     btn_fn_w, btn_h,
218                                     TIP_("Decrement the filename number"));
219                 RNA_int_set(UI_but_operator_ptr_get(but), "increment", -1);
220
221                 but = uiDefIconButO(block, UI_BTYPE_BUT, "FILE_OT_filenum", 0, ICON_ADD,
222                                     min_x + line2_w + separator + btn_fn_w - chan_offs, line2_y,
223                                     btn_fn_w, btn_h,
224                                     TIP_("Increment the filename number"));
225                 RNA_int_set(UI_but_operator_ptr_get(but), "increment", 1);
226                 UI_block_align_end(block);
227         }
228
229         /* Execute / cancel buttons. */
230         if (loadbutton) {
231                 const struct FileDirEntry *file = sfile->files ? filelist_file(sfile->files, params->active_file) : NULL;
232                 char const *str_exec;
233
234                 if (file && FILENAME_IS_PARENT(file->relpath)) {
235                         str_exec = IFACE_("Parent Directory");
236                 }
237                 else if (file && file->typeflag & FILE_TYPE_DIR) {
238                         str_exec = IFACE_("Open Directory");
239                 }
240                 else {
241                         str_exec = params->title;  /* params->title is already translated! */
242                 }
243
244                 uiDefButO(block, UI_BTYPE_BUT, "FILE_OT_execute", WM_OP_EXEC_REGION_WIN, str_exec,
245                           max_x - loadbutton, line1_y, loadbutton, btn_h, "");
246                 uiDefButO(block, UI_BTYPE_BUT, "FILE_OT_cancel", WM_OP_EXEC_REGION_WIN, IFACE_("Cancel"),
247                           max_x - loadbutton, line2_y, loadbutton, btn_h, "");
248         }
249
250         UI_block_end(C, block);
251         UI_block_draw(C, block);
252 }
253
254
255 static void draw_tile(int sx, int sy, int width, int height, int colorid, int shade)
256 {
257         float color[4];
258         UI_GetThemeColorShade4fv(colorid, shade, color);
259         UI_draw_roundbox_corner_set(UI_CNR_ALL);
260         UI_draw_roundbox_aa(true, (float)sx, (float)(sy - height), (float)(sx + width), (float)sy, 5.0f, color);
261 }
262
263
264 static void file_draw_icon(uiBlock *block, const char *path, int sx, int sy, int icon, int width, int height, bool drag)
265 {
266         uiBut *but;
267         int x, y;
268         // float alpha = 1.0f;
269
270         x = sx;
271         y = sy - height;
272
273         /*if (icon == ICON_FILE_BLANK) alpha = 0.375f;*/
274
275         but = uiDefIconBut(block, UI_BTYPE_LABEL, 0, icon, x, y, width, height, NULL, 0.0f, 0.0f, 0.0f, 0.0f, NULL);
276         UI_but_func_tooltip_set(but, file_draw_tooltip_func, BLI_strdup(path));
277
278         if (drag) {
279                 /* path is no more static, cannot give it directly to but... */
280                 UI_but_drag_set_path(but, BLI_strdup(path), true);
281         }
282 }
283
284
285 static void file_draw_string(
286         int sx, int sy, const char *string, float width, int height, eFontStyle_Align align,
287         const uchar col[4])
288 {
289         uiStyle *style;
290         uiFontStyle fs;
291         rcti rect;
292         char fname[FILE_MAXFILE];
293
294         if (string[0] == '\0') {
295                 return;
296         }
297
298         style = UI_style_get();
299         fs = style->widgetlabel;
300
301         BLI_strncpy(fname, string, FILE_MAXFILE);
302         UI_text_clip_middle_ex(&fs, fname, width, UI_DPI_ICON_SIZE, sizeof(fname), '\0');
303
304         /* no text clipping needed, UI_fontstyle_draw does it but is a bit too strict
305          * (for buttons it works) */
306         rect.xmin = sx;
307         rect.xmax = (int)(sx + ceil(width + 5.0f / UI_DPI_FAC));
308         rect.ymin = sy - height;
309         rect.ymax = sy;
310
311         UI_fontstyle_draw(
312                 &fs, &rect, fname, col,
313                 &(struct uiFontStyleDraw_Params) { .align = align, });
314 }
315
316 void file_calc_previews(const bContext *C, ARegion *ar)
317 {
318         SpaceFile *sfile = CTX_wm_space_file(C);
319         View2D *v2d = &ar->v2d;
320
321         ED_fileselect_init_layout(sfile, ar);
322         UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height);
323 }
324
325 static void file_draw_preview(
326         uiBlock *block, const char *path, int sx, int sy, const float icon_aspect,
327         ImBuf *imb, const int icon, FileLayout *layout, const bool is_icon, const int typeflags, const bool drag)
328 {
329         uiBut *but;
330         float fx, fy;
331         float dx, dy;
332         int xco, yco;
333         float ui_imbx, ui_imby;
334         float scaledx, scaledy;
335         float scale;
336         int ex, ey;
337         bool use_dropshadow = !is_icon && (typeflags & FILE_TYPE_IMAGE);
338         float col[4] = {1.0f, 1.0f, 1.0f, 1.0f};
339
340         BLI_assert(imb != NULL);
341
342         ui_imbx = imb->x * UI_DPI_FAC;
343         ui_imby = imb->y * UI_DPI_FAC;
344         /* Unlike thumbnails, icons are not scaled up. */
345         if (((ui_imbx > layout->prv_w) || (ui_imby > layout->prv_h)) ||
346             (!is_icon && ((ui_imbx < layout->prv_w) || (ui_imby < layout->prv_h))))
347         {
348                 if (imb->x > imb->y) {
349                         scaledx = (float)layout->prv_w;
350                         scaledy = ((float)imb->y / (float)imb->x) * layout->prv_w;
351                         scale = scaledx / imb->x;
352                 }
353                 else {
354                         scaledy = (float)layout->prv_h;
355                         scaledx = ((float)imb->x / (float)imb->y) * layout->prv_h;
356                         scale = scaledy / imb->y;
357                 }
358         }
359         else {
360                 scaledx = ui_imbx;
361                 scaledy = ui_imby;
362                 scale = UI_DPI_FAC;
363         }
364
365         ex = (int)scaledx;
366         ey = (int)scaledy;
367         fx = ((float)layout->prv_w - (float)ex) / 2.0f;
368         fy = ((float)layout->prv_h - (float)ey) / 2.0f;
369         dx = (fx + 0.5f + layout->prv_border_x);
370         dy = (fy + 0.5f - layout->prv_border_y);
371         xco = sx + (int)dx;
372         yco = sy - layout->prv_h + (int)dy;
373
374         /* shadow */
375         if (use_dropshadow) {
376                 UI_draw_box_shadow(220, (float)xco, (float)yco, (float)(xco + ex), (float)(yco + ey));
377         }
378
379         GPU_blend(true);
380
381         /* the image */
382         if (!is_icon && typeflags & FILE_TYPE_FTFONT) {
383                 UI_GetThemeColor4fv(TH_TEXT, col);
384         }
385
386         if (!is_icon && typeflags & FILE_TYPE_BLENDERLIB) {
387                 /* Datablock preview images use premultiplied alpha. */
388                 GPU_blend_set_func_separate(GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
389         }
390
391         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
392         immDrawPixelsTexScaled(&state, (float)xco, (float)yco, imb->x, imb->y, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, imb->rect,
393                                scale, scale, 1.0f, 1.0f, col);
394
395         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
396
397         if (icon) {
398                 UI_icon_draw_aspect((float)xco, (float)yco, icon, icon_aspect, 1.0f, NULL);
399         }
400
401         /* border */
402         if (use_dropshadow) {
403                 GPUVertFormat *format = immVertexFormat();
404                 uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
405
406                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
407                 immUniformColor4f(0.0f, 0.0f, 0.0f, 0.4f);
408                 imm_draw_box_wire_2d(pos, (float)xco, (float)yco, (float)(xco + ex), (float)(yco + ey));
409                 immUnbindProgram();
410         }
411
412         but = uiDefBut(block, UI_BTYPE_LABEL, 0, "", xco, yco, ex, ey, NULL, 0.0, 0.0, 0, 0, NULL);
413         UI_but_func_tooltip_set(but, file_draw_tooltip_func, BLI_strdup(path));
414
415         /* dragregion */
416         if (drag) {
417                 /* path is no more static, cannot give it directly to but... */
418                 UI_but_drag_set_image(but, BLI_strdup(path), icon, imb, scale, true);
419         }
420
421         GPU_blend(false);
422 }
423
424 static void renamebutton_cb(bContext *C, void *UNUSED(arg1), char *oldname)
425 {
426         Main *bmain = CTX_data_main(C);
427         char newname[FILE_MAX + 12];
428         char orgname[FILE_MAX + 12];
429         char filename[FILE_MAX + 12];
430         wmWindowManager *wm = CTX_wm_manager(C);
431         SpaceFile *sfile = (SpaceFile *)CTX_wm_space_data(C);
432         ScrArea *sa = CTX_wm_area(C);
433         ARegion *ar = CTX_wm_region(C);
434
435         const char *blendfile_path = BKE_main_blendfile_path(bmain);
436         BLI_make_file_string(blendfile_path, orgname, sfile->params->dir, oldname);
437         BLI_strncpy(filename, sfile->params->renameedit, sizeof(filename));
438         BLI_filename_make_safe(filename);
439         BLI_make_file_string(blendfile_path, newname, sfile->params->dir, filename);
440
441         if (!STREQ(orgname, newname)) {
442                 if (!BLI_exists(newname)) {
443                         errno = 0;
444                         if ((BLI_rename(orgname, newname) != 0) ||
445                             !BLI_exists(newname))
446                         {
447                                 WM_reportf(RPT_ERROR,
448                                            "Could not rename: %s",
449                                            errno ? strerror(errno) : "unknown error");
450                                 WM_report_banner_show();
451                         }
452
453                         /* to make sure we show what is on disk */
454                         ED_fileselect_clear(wm, sa, sfile);
455                 }
456
457                 ED_region_tag_redraw(ar);
458         }
459 }
460
461
462 static void draw_background(FileLayout *layout, View2D *v2d)
463 {
464         int i;
465         int sy;
466
467         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
468         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
469         immUniformThemeColorShade(TH_BACK, -7);
470
471         /* alternating flat shade background */
472         for (i = 0; (i <= layout->rows); i += 2) {
473                 sy = (int)v2d->cur.ymax - i * (layout->tile_h + 2 * layout->tile_border_y) - layout->tile_border_y;
474
475                 immRectf(pos, v2d->cur.xmin, (float)sy, v2d->cur.xmax, (float)(sy + layout->tile_h + 2 * layout->tile_border_y));
476         }
477
478         immUnbindProgram();
479 }
480
481 static void draw_dividers(FileLayout *layout, View2D *v2d)
482 {
483         /* vertical column dividers */
484
485         const int step = (layout->tile_w + 2 * layout->tile_border_x);
486
487         unsigned int vertex_len = 0;
488         int sx = (int)v2d->tot.xmin;
489         while (sx < v2d->cur.xmax) {
490                 sx += step;
491                 vertex_len += 4; /* vertex_count = 2 points per line * 2 lines per divider */
492         }
493
494         if (vertex_len > 0) {
495                 int v1[2], v2[2];
496                 unsigned char col_hi[3], col_lo[3];
497
498                 UI_GetThemeColorShade3ubv(TH_BACK,  30, col_hi);
499                 UI_GetThemeColorShade3ubv(TH_BACK, -30, col_lo);
500
501                 v1[1] = v2d->cur.ymax - layout->tile_border_y;
502                 v2[1] = v2d->cur.ymin;
503
504                 GPUVertFormat *format = immVertexFormat();
505                 uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
506                 uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
507
508                 immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
509                 immBegin(GPU_PRIM_LINES, vertex_len);
510
511                 sx = (int)v2d->tot.xmin;
512                 while (sx < v2d->cur.xmax) {
513                         sx += step;
514
515                         v1[0] = v2[0] = sx;
516                         immAttrSkip(color);
517                         immVertex2iv(pos, v1);
518                         immAttr3ubv(color, col_lo);
519                         immVertex2iv(pos, v2);
520
521                         v1[0] = v2[0] = sx + 1;
522                         immAttrSkip(color);
523                         immVertex2iv(pos, v1);
524                         immAttr3ubv(color, col_hi);
525                         immVertex2iv(pos, v2);
526                 }
527
528                 immEnd();
529                 immUnbindProgram();
530         }
531 }
532
533 void file_draw_list(const bContext *C, ARegion *ar)
534 {
535         SpaceFile *sfile = CTX_wm_space_file(C);
536         FileSelectParams *params = ED_fileselect_get_params(sfile);
537         FileLayout *layout = ED_fileselect_get_layout(sfile, ar);
538         View2D *v2d = &ar->v2d;
539         struct FileList *files = sfile->files;
540         struct FileDirEntry *file;
541         const char *root = filelist_dir(files);
542         ImBuf *imb;
543         uiBlock *block = UI_block_begin(C, ar, __func__, UI_EMBOSS);
544         int numfiles;
545         int numfiles_layout;
546         int sx, sy;
547         int offset;
548         int textwidth, textheight;
549         int i;
550         bool is_icon;
551         eFontStyle_Align align;
552         bool do_drag;
553         int column_space = 0.6f * UI_UNIT_X;
554         unsigned char text_col[4];
555         const bool small_size = SMALL_SIZE_CHECK(params->thumbnail_size);
556         const bool update_stat_strings = small_size != SMALL_SIZE_CHECK(layout->curr_size);
557         const float thumb_icon_aspect = sqrtf(64.0f / (float)(params->thumbnail_size));
558
559         numfiles = filelist_files_ensure(files);
560
561         if (params->display != FILE_IMGDISPLAY) {
562
563                 draw_background(layout, v2d);
564
565                 draw_dividers(layout, v2d);
566         }
567
568         offset = ED_fileselect_layout_offset(layout, (int)ar->v2d.cur.xmin, (int)-ar->v2d.cur.ymax);
569         if (offset < 0) offset = 0;
570
571         numfiles_layout = ED_fileselect_layout_numfiles(layout, ar);
572
573         /* adjust, so the next row is already drawn when scrolling */
574         if (layout->flag & FILE_LAYOUT_HOR) {
575                 numfiles_layout += layout->rows;
576         }
577         else {
578                 numfiles_layout += layout->columns;
579         }
580
581         filelist_file_cache_slidingwindow_set(files, numfiles_layout);
582
583         textwidth = (FILE_IMGDISPLAY == params->display) ? layout->tile_w : (int)layout->column_widths[COLUMN_NAME];
584         textheight = (int)(layout->textheight * 3.0 / 2.0 + 0.5);
585
586         align = (FILE_IMGDISPLAY == params->display) ? UI_STYLE_TEXT_CENTER : UI_STYLE_TEXT_LEFT;
587
588         if (numfiles > 0) {
589                 const bool success = filelist_file_cache_block(files, min_ii(offset + (numfiles_layout / 2), numfiles - 1));
590                 BLI_assert(success);
591                 UNUSED_VARS_NDEBUG(success);
592
593                 filelist_cache_previews_update(files);
594
595                 /* Handle preview timer here, since it's filelist_file_cache_block() and filelist_cache_previews_update()
596                  * which controls previews task. */
597                 {
598                         const bool previews_running = filelist_cache_previews_running(files);
599 //                      printf("%s: preview task: %d\n", __func__, previews_running);
600                         if (previews_running && !sfile->previews_timer) {
601                                 sfile->previews_timer = WM_event_add_timer_notifier(CTX_wm_manager(C), CTX_wm_window(C),
602                                                                                     NC_SPACE | ND_SPACE_FILE_PREVIEW, 0.01);
603                         }
604                         if (!previews_running && sfile->previews_timer) {
605                                 /* Preview is not running, no need to keep generating update events! */
606 //                              printf("%s: Inactive preview task, sleeping!\n", __func__);
607                                 WM_event_remove_timer_notifier(CTX_wm_manager(C), CTX_wm_window(C), sfile->previews_timer);
608                                 sfile->previews_timer = NULL;
609                         }
610                 }
611         }
612
613         BLF_batch_draw_begin();
614
615         for (i = offset; (i < numfiles) && (i < offset + numfiles_layout); i++) {
616                 unsigned int file_selflag;
617                 char path[FILE_MAX_LIBEXTRA];
618                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
619                 sx += (int)(v2d->tot.xmin + 0.1f * UI_UNIT_X);
620                 sy = (int)(v2d->tot.ymax - sy);
621
622                 file = filelist_file(files, i);
623                 file_selflag = filelist_entry_select_get(sfile->files, file, CHECK_ALL);
624
625                 BLI_join_dirfile(path, sizeof(path), root, file->relpath);
626
627                 if (!(file_selflag & FILE_SEL_EDITING)) {
628                         if ((params->highlight_file == i) || (file_selflag & FILE_SEL_HIGHLIGHTED) ||
629                             (file_selflag & FILE_SEL_SELECTED))
630                         {
631                                 int colorid = (file_selflag & FILE_SEL_SELECTED) ? TH_HILITE : TH_BACK;
632                                 int shade = (params->highlight_file == i) || (file_selflag & FILE_SEL_HIGHLIGHTED) ? 35 : 0;
633
634                                 BLI_assert(i == 0 || !FILENAME_IS_CURRPAR(file->relpath));
635
636                                 draw_tile(sx, sy - 1, layout->tile_w + 4, sfile->layout->tile_h + layout->tile_border_y, colorid, shade);
637                         }
638                 }
639                 UI_draw_roundbox_corner_set(UI_CNR_NONE);
640
641                 /* don't drag parent or refresh items */
642                 do_drag = !(FILENAME_IS_CURRPAR(file->relpath));
643
644                 if (FILE_IMGDISPLAY == params->display) {
645                         const int icon = filelist_geticon(files, i, false);
646                         is_icon = 0;
647                         imb = filelist_getimage(files, i);
648                         if (!imb) {
649                                 imb = filelist_geticon_image(files, i);
650                                 is_icon = 1;
651                         }
652
653                         file_draw_preview(block, path, sx, sy, thumb_icon_aspect,
654                                           imb, icon, layout, is_icon, file->typeflag, do_drag);
655                 }
656                 else {
657                         file_draw_icon(block, path, sx, sy - (UI_UNIT_Y / 6), filelist_geticon(files, i, true),
658                                        ICON_DEFAULT_WIDTH_SCALE, ICON_DEFAULT_HEIGHT_SCALE, do_drag);
659                         sx += ICON_DEFAULT_WIDTH_SCALE + 0.2f * UI_UNIT_X;
660                 }
661
662                 UI_GetThemeColor4ubv(TH_TEXT, text_col);
663
664                 if (file_selflag & FILE_SEL_EDITING) {
665                         uiBut *but;
666                         short width;
667
668                         if (params->display == FILE_SHORTDISPLAY) {
669                                 width = layout->tile_w - (ICON_DEFAULT_WIDTH_SCALE + 0.2f * UI_UNIT_X);
670                         }
671                         else if (params->display == FILE_LONGDISPLAY) {
672                                 width = layout->column_widths[COLUMN_NAME] + (column_space * 3.5f);
673                         }
674                         else {
675                                 BLI_assert(params->display == FILE_IMGDISPLAY);
676                                 width = textwidth;
677                         }
678
679                         but = uiDefBut(block, UI_BTYPE_TEXT, 1, "", sx, sy - layout->tile_h - 0.15f * UI_UNIT_X,
680                                        width, textheight, sfile->params->renameedit, 1.0f,
681                                        (float)sizeof(sfile->params->renameedit), 0, 0, "");
682                         UI_but_func_rename_set(but, renamebutton_cb, file);
683                         UI_but_flag_enable(but, UI_BUT_NO_UTF8); /* allow non utf8 names */
684                         UI_but_flag_disable(but, UI_BUT_UNDO);
685                         if (false == UI_but_active_only(C, ar, block, but)) {
686                                 file_selflag = filelist_entry_select_set(
687                                                    sfile->files, file, FILE_SEL_REMOVE, FILE_SEL_EDITING, CHECK_ALL);
688                         }
689                 }
690
691                 if (!(file_selflag& FILE_SEL_EDITING)) {
692                         int tpos = (FILE_IMGDISPLAY == params->display) ? sy - layout->tile_h + layout->textheight : sy;
693                         file_draw_string(sx + 1, tpos, file->name, (float)textwidth, textheight, align, text_col);
694                 }
695
696                 sx += (int)layout->column_widths[COLUMN_NAME] + column_space;
697                 if (params->display == FILE_SHORTDISPLAY) {
698                         if ((file->typeflag & (FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) ||
699                             !(file->typeflag & (FILE_TYPE_DIR | FILE_TYPE_BLENDERLIB)))
700                         {
701                                 if ((file->entry->size_str[0] == '\0') || update_stat_strings) {
702                                         BLI_filelist_entry_size_to_string(NULL, file->entry->size, small_size, file->entry->size_str);
703                                 }
704                                 file_draw_string(
705                                             sx, sy, file->entry->size_str, layout->column_widths[COLUMN_SIZE], layout->tile_h,
706                                             align, text_col);
707                         }
708                         sx += (int)layout->column_widths[COLUMN_SIZE] + column_space;
709                 }
710                 else if (params->display == FILE_LONGDISPLAY) {
711                         if (!(file->typeflag & FILE_TYPE_BLENDERLIB) && !FILENAME_IS_CURRPAR(file->relpath)) {
712                                 if ((file->entry->date_str[0] == '\0') || update_stat_strings) {
713                                         BLI_filelist_entry_datetime_to_string(
714                                                     NULL, file->entry->time, small_size, file->entry->time_str, file->entry->date_str);
715                                 }
716                                 file_draw_string(
717                                             sx, sy, file->entry->date_str, layout->column_widths[COLUMN_DATE], layout->tile_h,
718                                             align, text_col);
719                                 sx += (int)layout->column_widths[COLUMN_DATE] + column_space;
720                                 file_draw_string(
721                                             sx, sy, file->entry->time_str, layout->column_widths[COLUMN_TIME], layout->tile_h,
722                                             align, text_col);
723                                 sx += (int)layout->column_widths[COLUMN_TIME] + column_space;
724                         }
725                         else {
726                                 sx += (int)layout->column_widths[COLUMN_DATE] + column_space;
727                                 sx += (int)layout->column_widths[COLUMN_TIME] + column_space;
728                         }
729
730                         if ((file->typeflag & (FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) ||
731                             !(file->typeflag & (FILE_TYPE_DIR | FILE_TYPE_BLENDERLIB)))
732                         {
733                                 if ((file->entry->size_str[0] == '\0') || update_stat_strings) {
734                                         BLI_filelist_entry_size_to_string(NULL, file->entry->size, small_size, file->entry->size_str);
735                                 }
736                                 file_draw_string(
737                                             sx, sy, file->entry->size_str, layout->column_widths[COLUMN_SIZE], layout->tile_h,
738                                             align, text_col);
739                         }
740                         sx += (int)layout->column_widths[COLUMN_SIZE] + column_space;
741                 }
742         }
743
744         BLF_batch_draw_end();
745
746         UI_block_end(C, block);
747         UI_block_draw(C, block);
748
749         layout->curr_size = params->thumbnail_size;
750 }