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