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