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