1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup spfile
22  */
23 
24 #include <errno.h>
25 #include <math.h>
26 #include <string.h>
27 
28 #include "BLI_blenlib.h"
29 #include "BLI_fileops_types.h"
30 #include "BLI_math.h"
31 #include "BLI_utildefines.h"
32 
33 #ifdef WIN32
34 #  include "BLI_winstuff.h"
35 #endif
36 
37 #include "BIF_glutil.h"
38 
39 #include "BKE_context.h"
40 #include "BKE_main.h"
41 
42 #include "BLO_readfile.h"
43 
44 #include "BLT_translation.h"
45 
46 #include "BLF_api.h"
47 
48 #include "IMB_imbuf_types.h"
49 
50 #include "DNA_userdef_types.h"
51 #include "DNA_windowmanager_types.h"
52 
53 #include "RNA_access.h"
54 
55 #include "ED_fileselect.h"
56 #include "ED_screen.h"
57 
58 #include "UI_interface.h"
59 #include "UI_interface_icons.h"
60 #include "UI_resources.h"
61 #include "UI_view2d.h"
62 
63 #include "WM_api.h"
64 #include "WM_types.h"
65 
66 #include "GPU_immediate.h"
67 #include "GPU_immediate_util.h"
68 #include "GPU_state.h"
69 
70 #include "filelist.h"
71 
72 #include "file_intern.h" /* own include */
73 
ED_file_path_button(bScreen * screen,const SpaceFile * sfile,FileSelectParams * params,uiBlock * block)74 void ED_file_path_button(bScreen *screen,
75                          const SpaceFile *sfile,
76                          FileSelectParams *params,
77                          uiBlock *block)
78 {
79   PointerRNA params_rna_ptr;
80   uiBut *but;
81 
82   RNA_pointer_create(&screen->id, &RNA_FileSelectParams, params, &params_rna_ptr);
83 
84   /* callbacks for operator check functions */
85   UI_block_func_set(block, file_draw_check_cb, NULL, NULL);
86 
87   but = uiDefButR(block,
88                   UI_BTYPE_TEXT,
89                   -1,
90                   "",
91                   0,
92                   0,
93                   UI_UNIT_X * 10,
94                   UI_UNIT_Y,
95                   &params_rna_ptr,
96                   "directory",
97                   0,
98                   0.0f,
99                   (float)FILE_MAX,
100                   0.0f,
101                   0.0f,
102                   TIP_("File path"));
103 
104   BLI_assert(!UI_but_flag_is_set(but, UI_BUT_UNDO));
105   BLI_assert(!UI_but_is_utf8(but));
106 
107   UI_but_func_complete_set(but, autocomplete_directory, NULL);
108   UI_but_funcN_set(but, file_directory_enter_handle, NULL, but);
109 
110   /* TODO, directory editing is non-functional while a library is loaded
111    * until this is properly supported just disable it. */
112   if (sfile && sfile->files && filelist_lib(sfile->files)) {
113     UI_but_flag_enable(but, UI_BUT_DISABLED);
114   }
115 
116   /* clear func */
117   UI_block_func_set(block, NULL, NULL, NULL);
118 }
119 
120 /* Dummy helper - we need dynamic tooltips here. */
file_draw_tooltip_func(bContext * UNUSED (C),void * argN,const char * UNUSED (tip))121 static char *file_draw_tooltip_func(bContext *UNUSED(C), void *argN, const char *UNUSED(tip))
122 {
123   char *dyn_tooltip = argN;
124   return BLI_strdup(dyn_tooltip);
125 }
126 
draw_tile(int sx,int sy,int width,int height,int colorid,int shade)127 static void draw_tile(int sx, int sy, int width, int height, int colorid, int shade)
128 {
129   float color[4];
130   UI_GetThemeColorShade4fv(colorid, shade, color);
131   UI_draw_roundbox_corner_set(UI_CNR_ALL);
132   UI_draw_roundbox_aa(
133       true, (float)sx, (float)(sy - height), (float)(sx + width), (float)sy, 5.0f, color);
134 }
135 
file_draw_icon(uiBlock * block,const char * path,int sx,int sy,int icon,int width,int height,bool drag,bool dimmed)136 static void file_draw_icon(uiBlock *block,
137                            const char *path,
138                            int sx,
139                            int sy,
140                            int icon,
141                            int width,
142                            int height,
143                            bool drag,
144                            bool dimmed)
145 {
146   uiBut *but;
147   int x, y;
148 
149   x = sx;
150   y = sy - height;
151 
152   /* For uiDefIconBut(), if a1==1.0 then a2 is alpha 0.0 - 1.0 */
153   const float a1 = dimmed ? 1.0f : 0.0f;
154   const float a2 = dimmed ? 0.3f : 0.0f;
155   but = uiDefIconBut(
156       block, UI_BTYPE_LABEL, 0, icon, x, y, width, height, NULL, 0.0f, 0.0f, a1, a2, NULL);
157   UI_but_func_tooltip_set(but, file_draw_tooltip_func, BLI_strdup(path));
158 
159   if (drag) {
160     /* path is no more static, cannot give it directly to but... */
161     UI_but_drag_set_path(but, BLI_strdup(path), true);
162   }
163 }
164 
file_draw_string(int sx,int sy,const char * string,float width,int height,eFontStyle_Align align,const uchar col[4])165 static void file_draw_string(int sx,
166                              int sy,
167                              const char *string,
168                              float width,
169                              int height,
170                              eFontStyle_Align align,
171                              const uchar col[4])
172 {
173   uiFontStyle fs;
174   rcti rect;
175   char fname[FILE_MAXFILE];
176 
177   if (string[0] == '\0' || width < 1) {
178     return;
179   }
180 
181   const uiStyle *style = UI_style_get();
182   fs = style->widgetlabel;
183 
184   BLI_strncpy(fname, string, FILE_MAXFILE);
185   UI_text_clip_middle_ex(&fs, fname, width, UI_DPI_ICON_SIZE, sizeof(fname), '\0');
186 
187   /* no text clipping needed, UI_fontstyle_draw does it but is a bit too strict
188    * (for buttons it works) */
189   rect.xmin = sx;
190   rect.xmax = sx + round_fl_to_int(width);
191   rect.ymin = sy - height;
192   rect.ymax = sy;
193 
194   UI_fontstyle_draw(&fs,
195                     &rect,
196                     fname,
197                     col,
198                     &(struct uiFontStyleDraw_Params){
199                         .align = align,
200                     });
201 }
202 
file_calc_previews(const bContext * C,ARegion * region)203 void file_calc_previews(const bContext *C, ARegion *region)
204 {
205   SpaceFile *sfile = CTX_wm_space_file(C);
206   View2D *v2d = &region->v2d;
207 
208   ED_fileselect_init_layout(sfile, region);
209   UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height);
210 }
211 
file_draw_preview(uiBlock * block,const char * path,int sx,int sy,const float icon_aspect,ImBuf * imb,const int icon,FileLayout * layout,const bool is_icon,const int typeflags,const bool drag,const bool dimmed,const bool is_link)212 static void file_draw_preview(uiBlock *block,
213                               const char *path,
214                               int sx,
215                               int sy,
216                               const float icon_aspect,
217                               ImBuf *imb,
218                               const int icon,
219                               FileLayout *layout,
220                               const bool is_icon,
221                               const int typeflags,
222                               const bool drag,
223                               const bool dimmed,
224                               const bool is_link)
225 {
226   uiBut *but;
227   float fx, fy;
228   float dx, dy;
229   int xco, yco;
230   float ui_imbx, ui_imby;
231   float scaledx, scaledy;
232   float scale;
233   int ex, ey;
234   bool show_outline = !is_icon &&
235                       (typeflags & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE | FILE_TYPE_BLENDER));
236 
237   BLI_assert(imb != NULL);
238 
239   ui_imbx = imb->x * UI_DPI_FAC;
240   ui_imby = imb->y * UI_DPI_FAC;
241   /* Unlike thumbnails, icons are not scaled up. */
242   if (((ui_imbx > layout->prv_w) || (ui_imby > layout->prv_h)) ||
243       (!is_icon && ((ui_imbx < layout->prv_w) || (ui_imby < layout->prv_h)))) {
244     if (imb->x > imb->y) {
245       scaledx = (float)layout->prv_w;
246       scaledy = ((float)imb->y / (float)imb->x) * layout->prv_w;
247       scale = scaledx / imb->x;
248     }
249     else {
250       scaledy = (float)layout->prv_h;
251       scaledx = ((float)imb->x / (float)imb->y) * layout->prv_h;
252       scale = scaledy / imb->y;
253     }
254   }
255   else {
256     scaledx = ui_imbx;
257     scaledy = ui_imby;
258     scale = UI_DPI_FAC;
259   }
260 
261   ex = (int)scaledx;
262   ey = (int)scaledy;
263   fx = ((float)layout->prv_w - (float)ex) / 2.0f;
264   fy = ((float)layout->prv_h - (float)ey) / 2.0f;
265   dx = (fx + 0.5f + layout->prv_border_x);
266   dy = (fy + 0.5f - layout->prv_border_y);
267   xco = sx + (int)dx;
268   yco = sy - layout->prv_h + (int)dy;
269 
270   GPU_blend(GPU_BLEND_ALPHA);
271 
272   /* the large image */
273 
274   float col[4] = {1.0f, 1.0f, 1.0f, 1.0f};
275   if (is_icon) {
276     if (typeflags & FILE_TYPE_DIR) {
277       UI_GetThemeColor4fv(TH_ICON_FOLDER, col);
278     }
279     else {
280       UI_GetThemeColor4fv(TH_TEXT, col);
281     }
282   }
283   else if (typeflags & FILE_TYPE_FTFONT) {
284     UI_GetThemeColor4fv(TH_TEXT, col);
285   }
286 
287   if (dimmed) {
288     col[3] *= 0.3f;
289   }
290 
291   if (!is_icon && typeflags & FILE_TYPE_BLENDERLIB) {
292     /* Datablock preview images use premultiplied alpha. */
293     GPU_blend(GPU_BLEND_ALPHA_PREMULT);
294   }
295 
296   IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
297   immDrawPixelsTexScaled(&state,
298                          (float)xco,
299                          (float)yco,
300                          imb->x,
301                          imb->y,
302                          GPU_RGBA8,
303                          false,
304                          imb->rect,
305                          scale,
306                          scale,
307                          1.0f,
308                          1.0f,
309                          col);
310 
311   GPU_blend(GPU_BLEND_ALPHA);
312 
313   if (icon && is_icon) {
314     /* Small icon in the middle of large image, scaled to fit container and UI scale */
315     float icon_x, icon_y;
316     const float icon_size = 16.0f / icon_aspect * U.dpi_fac;
317     float icon_opacity = 0.3f;
318     uchar icon_color[4] = {0, 0, 0, 255};
319     float bgcolor[4];
320     UI_GetThemeColor4fv(TH_ICON_FOLDER, bgcolor);
321     if (rgb_to_grayscale(bgcolor) < 0.5f) {
322       icon_color[0] = 255;
323       icon_color[1] = 255;
324       icon_color[2] = 255;
325     }
326     icon_x = xco + (ex / 2.0f) - (icon_size / 2.0f);
327     icon_y = yco + (ey / 2.0f) - (icon_size * ((typeflags & FILE_TYPE_DIR) ? 0.78f : 0.75f));
328     UI_icon_draw_ex(
329         icon_x, icon_y, icon, icon_aspect / U.dpi_fac, icon_opacity, 0.0f, icon_color, false);
330   }
331 
332   if (is_link) {
333     /* Arrow icon to indicate it is a shortcut, link, or alias. */
334     float icon_x, icon_y;
335     icon_x = xco + (2.0f * UI_DPI_FAC);
336     icon_y = yco + (2.0f * UI_DPI_FAC);
337     const int arrow = ICON_LOOP_FORWARDS;
338     if (!is_icon) {
339       /* Arrow at very bottom-left if preview style. */
340       const uchar dark[4] = {0, 0, 0, 255};
341       const uchar light[4] = {255, 255, 255, 255};
342       UI_icon_draw_ex(icon_x + 1, icon_y - 1, arrow, 1.0f / U.dpi_fac, 0.2f, 0.0f, dark, false);
343       UI_icon_draw_ex(icon_x, icon_y, arrow, 1.0f / U.dpi_fac, 0.6f, 0.0f, light, false);
344     }
345     else {
346       /* Link to folder or non-previewed file. */
347       uchar icon_color[4];
348       UI_GetThemeColor4ubv(TH_BACK, icon_color);
349       icon_x = xco + ((typeflags & FILE_TYPE_DIR) ? 0.14f : 0.23f) * scaledx;
350       icon_y = yco + ((typeflags & FILE_TYPE_DIR) ? 0.24f : 0.14f) * scaledy;
351       UI_icon_draw_ex(
352           icon_x, icon_y, arrow, icon_aspect / U.dpi_fac * 1.8, 0.3f, 0.0f, icon_color, false);
353     }
354   }
355   else if (icon && !is_icon && !(typeflags & FILE_TYPE_FTFONT)) {
356     /* Smaller, fainter icon at bottom-left for preview image thumbnail, but not for fonts. */
357     float icon_x, icon_y;
358     const uchar dark[4] = {0, 0, 0, 255};
359     const uchar light[4] = {255, 255, 255, 255};
360     icon_x = xco + (2.0f * UI_DPI_FAC);
361     icon_y = yco + (2.0f * UI_DPI_FAC);
362     UI_icon_draw_ex(icon_x + 1, icon_y - 1, icon, 1.0f / U.dpi_fac, 0.2f, 0.0f, dark, false);
363     UI_icon_draw_ex(icon_x, icon_y, icon, 1.0f / U.dpi_fac, 0.6f, 0.0f, light, false);
364   }
365 
366   /* Contrasting outline around some preview types. */
367   if (show_outline) {
368     GPUVertFormat *format = immVertexFormat();
369     uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
370     immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
371     float border_color[4] = {1.0f, 1.0f, 1.0f, 0.4f};
372     float bgcolor[4];
373     UI_GetThemeColor4fv(TH_BACK, bgcolor);
374     if (rgb_to_grayscale(bgcolor) > 0.5f) {
375       border_color[0] = 0.0f;
376       border_color[1] = 0.0f;
377       border_color[2] = 0.0f;
378     }
379     immUniformColor4fv(border_color);
380     imm_draw_box_wire_2d(pos, (float)xco, (float)yco, (float)(xco + ex), (float)(yco + ey));
381     immUnbindProgram();
382   }
383 
384   but = uiDefBut(block, UI_BTYPE_LABEL, 0, "", xco, yco, ex, ey, NULL, 0.0, 0.0, 0, 0, NULL);
385 
386   /* dragregion */
387   if (drag) {
388     /* path is no more static, cannot give it directly to but... */
389     UI_but_drag_set_image(but, BLI_strdup(path), icon, imb, scale, true);
390   }
391 
392   GPU_blend(GPU_BLEND_NONE);
393 }
394 
renamebutton_cb(bContext * C,void * UNUSED (arg1),char * oldname)395 static void renamebutton_cb(bContext *C, void *UNUSED(arg1), char *oldname)
396 {
397   char newname[FILE_MAX + 12];
398   char orgname[FILE_MAX + 12];
399   char filename[FILE_MAX + 12];
400   wmWindowManager *wm = CTX_wm_manager(C);
401   SpaceFile *sfile = (SpaceFile *)CTX_wm_space_data(C);
402   ARegion *region = CTX_wm_region(C);
403 
404   BLI_join_dirfile(orgname, sizeof(orgname), sfile->params->dir, oldname);
405   BLI_strncpy(filename, sfile->params->renamefile, sizeof(filename));
406   BLI_filename_make_safe(filename);
407   BLI_join_dirfile(newname, sizeof(newname), sfile->params->dir, filename);
408 
409   if (!STREQ(orgname, newname)) {
410     if (!BLI_exists(newname)) {
411       errno = 0;
412       if ((BLI_rename(orgname, newname) != 0) || !BLI_exists(newname)) {
413         WM_reportf(RPT_ERROR, "Could not rename: %s", errno ? strerror(errno) : "unknown error");
414         WM_report_banner_show();
415       }
416       else {
417         /* If rename is successful, scroll to newly renamed entry. */
418         BLI_strncpy(sfile->params->renamefile, filename, sizeof(sfile->params->renamefile));
419         sfile->params->rename_flag = FILE_PARAMS_RENAME_POSTSCROLL_PENDING;
420 
421         if (sfile->smoothscroll_timer != NULL) {
422           WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), sfile->smoothscroll_timer);
423         }
424         sfile->smoothscroll_timer = WM_event_add_timer(wm, CTX_wm_window(C), TIMER1, 1.0 / 1000.0);
425         sfile->scroll_offset = 0;
426       }
427 
428       /* to make sure we show what is on disk */
429       ED_fileselect_clear(wm, CTX_data_scene(C), sfile);
430     }
431 
432     ED_region_tag_redraw(region);
433   }
434 }
435 
draw_background(FileLayout * layout,View2D * v2d)436 static void draw_background(FileLayout *layout, View2D *v2d)
437 {
438   const int item_height = layout->tile_h + (2 * layout->tile_border_y);
439   int i;
440   int sy;
441 
442   uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
443   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
444   float col_alternating[4];
445   UI_GetThemeColor4fv(TH_ROW_ALTERNATE, col_alternating);
446   immUniformThemeColorBlend(TH_BACK, TH_ROW_ALTERNATE, col_alternating[3]);
447 
448   /* alternating flat shade background */
449   for (i = 2; (i <= layout->rows + 1); i += 2) {
450     sy = (int)v2d->cur.ymax - layout->offset_top - i * item_height - layout->tile_border_y;
451 
452     /* Offsett pattern slightly to add scroll effect. */
453     sy += round_fl_to_int(item_height * (v2d->tot.ymax - v2d->cur.ymax) / item_height);
454 
455     immRectf(pos,
456              v2d->cur.xmin,
457              (float)sy,
458              v2d->cur.xmax,
459              (float)(sy + layout->tile_h + 2 * layout->tile_border_y));
460   }
461 
462   immUnbindProgram();
463 }
464 
draw_dividers(FileLayout * layout,View2D * v2d)465 static void draw_dividers(FileLayout *layout, View2D *v2d)
466 {
467   /* vertical column dividers */
468 
469   const int step = (layout->tile_w + 2 * layout->tile_border_x);
470 
471   uint vertex_len = 0;
472   int sx = (int)v2d->tot.xmin;
473   while (sx < v2d->cur.xmax) {
474     sx += step;
475     vertex_len += 4; /* vertex_count = 2 points per line * 2 lines per divider */
476   }
477 
478   if (vertex_len > 0) {
479     int v1[2], v2[2];
480     uchar col_hi[3], col_lo[3];
481 
482     UI_GetThemeColorShade3ubv(TH_BACK, 30, col_hi);
483     UI_GetThemeColorShade3ubv(TH_BACK, -30, col_lo);
484 
485     v1[1] = v2d->cur.ymax - layout->tile_border_y;
486     v2[1] = v2d->cur.ymin;
487 
488     GPUVertFormat *format = immVertexFormat();
489     uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
490     uint color = GPU_vertformat_attr_add(
491         format, "color", GPU_COMP_U8, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
492 
493     immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
494     immBegin(GPU_PRIM_LINES, vertex_len);
495 
496     sx = (int)v2d->tot.xmin;
497     while (sx < v2d->cur.xmax) {
498       sx += step;
499 
500       v1[0] = v2[0] = sx;
501       immAttrSkip(color);
502       immVertex2iv(pos, v1);
503       immAttr3ubv(color, col_lo);
504       immVertex2iv(pos, v2);
505 
506       v1[0] = v2[0] = sx + 1;
507       immAttrSkip(color);
508       immVertex2iv(pos, v1);
509       immAttr3ubv(color, col_hi);
510       immVertex2iv(pos, v2);
511     }
512 
513     immEnd();
514     immUnbindProgram();
515   }
516 }
517 
draw_columnheader_background(const FileLayout * layout,const View2D * v2d)518 static void draw_columnheader_background(const FileLayout *layout, const View2D *v2d)
519 {
520   uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
521 
522   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
523   immUniformThemeColorShade(TH_BACK, 11);
524 
525   immRectf(pos,
526            v2d->cur.xmin,
527            v2d->cur.ymax - layout->attribute_column_header_h,
528            v2d->cur.xmax,
529            v2d->cur.ymax);
530 
531   immUnbindProgram();
532 }
533 
draw_columnheader_columns(const FileSelectParams * params,FileLayout * layout,const View2D * v2d,const uchar text_col[4])534 static void draw_columnheader_columns(const FileSelectParams *params,
535                                       FileLayout *layout,
536                                       const View2D *v2d,
537                                       const uchar text_col[4])
538 {
539   const float divider_pad = 0.2 * layout->attribute_column_header_h;
540   int sx = v2d->cur.xmin, sy = v2d->cur.ymax;
541 
542   for (FileAttributeColumnType column_type = 0; column_type < ATTRIBUTE_COLUMN_MAX;
543        column_type++) {
544     if (!file_attribute_column_type_enabled(params, column_type)) {
545       continue;
546     }
547     const FileAttributeColumn *column = &layout->attribute_columns[column_type];
548 
549     /* Active sort type triangle */
550     if (params->sort == column->sort_type) {
551       float tri_color[4];
552 
553       rgba_uchar_to_float(tri_color, text_col);
554       UI_draw_icon_tri(sx + column->width - (0.3f * U.widget_unit) -
555                            ATTRIBUTE_COLUMN_PADDING / 2.0f,
556                        sy + (0.1f * U.widget_unit) - (layout->attribute_column_header_h / 2),
557                        (params->flag & FILE_SORT_INVERT) ? 't' : 'v',
558                        tri_color);
559     }
560 
561     file_draw_string(sx + ATTRIBUTE_COLUMN_PADDING,
562                      sy - layout->tile_border_y,
563                      IFACE_(column->name),
564                      column->width - 2 * ATTRIBUTE_COLUMN_PADDING,
565                      layout->attribute_column_header_h - layout->tile_border_y,
566                      UI_STYLE_TEXT_LEFT,
567                      text_col);
568 
569     /* Separator line */
570     if (column_type != COLUMN_NAME) {
571       uint pos = GPU_vertformat_attr_add(
572           immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
573 
574       immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
575       immUniformThemeColorShade(TH_BACK, -10);
576       immBegin(GPU_PRIM_LINES, 2);
577       immVertex2f(pos, sx - 1, sy - divider_pad);
578       immVertex2f(pos, sx - 1, sy - layout->attribute_column_header_h + divider_pad);
579       immEnd();
580       immUnbindProgram();
581     }
582 
583     sx += column->width;
584   }
585 
586   /* Vertical separator lines line */
587   {
588     uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
589     immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
590     immUniformThemeColorShade(TH_BACK, -10);
591     immBegin(GPU_PRIM_LINES, 4);
592     immVertex2f(pos, v2d->cur.xmin, sy);
593     immVertex2f(pos, v2d->cur.xmax, sy);
594     immVertex2f(pos, v2d->cur.xmin, sy - layout->attribute_column_header_h);
595     immVertex2f(pos, v2d->cur.xmax, sy - layout->attribute_column_header_h);
596     immEnd();
597     immUnbindProgram();
598   }
599 }
600 
601 /**
602  * Updates the stat string stored in file->entry if necessary.
603  */
filelist_get_details_column_string(FileAttributeColumnType column,const FileDirEntry * file,const bool small_size,const bool update_stat_strings)604 static const char *filelist_get_details_column_string(FileAttributeColumnType column,
605                                                       const FileDirEntry *file,
606                                                       const bool small_size,
607                                                       const bool update_stat_strings)
608 {
609   switch (column) {
610     case COLUMN_DATETIME:
611       if (!(file->typeflag & FILE_TYPE_BLENDERLIB) && !FILENAME_IS_CURRPAR(file->relpath)) {
612         if ((file->entry->datetime_str[0] == '\0') || update_stat_strings) {
613           char date[FILELIST_DIRENTRY_DATE_LEN], time[FILELIST_DIRENTRY_TIME_LEN];
614           bool is_today, is_yesterday;
615 
616           BLI_filelist_entry_datetime_to_string(
617               NULL, file->entry->time, small_size, time, date, &is_today, &is_yesterday);
618 
619           if (is_today || is_yesterday) {
620             BLI_strncpy(date, is_today ? N_("Today") : N_("Yesterday"), sizeof(date));
621           }
622           BLI_snprintf(
623               file->entry->datetime_str, sizeof(file->entry->datetime_str), "%s %s", date, time);
624         }
625 
626         return file->entry->datetime_str;
627       }
628       break;
629     case COLUMN_SIZE:
630       if ((file->typeflag & (FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) ||
631           !(file->typeflag & (FILE_TYPE_DIR | FILE_TYPE_BLENDERLIB))) {
632         if ((file->entry->size_str[0] == '\0') || update_stat_strings) {
633           BLI_filelist_entry_size_to_string(
634               NULL, file->entry->size, small_size, file->entry->size_str);
635         }
636 
637         return file->entry->size_str;
638       }
639       break;
640     default:
641       break;
642   }
643 
644   return NULL;
645 }
646 
draw_details_columns(const FileSelectParams * params,const FileLayout * layout,const FileDirEntry * file,const int pos_x,const int pos_y,const uchar text_col[4])647 static void draw_details_columns(const FileSelectParams *params,
648                                  const FileLayout *layout,
649                                  const FileDirEntry *file,
650                                  const int pos_x,
651                                  const int pos_y,
652                                  const uchar text_col[4])
653 {
654   const bool small_size = SMALL_SIZE_CHECK(params->thumbnail_size);
655   const bool update_stat_strings = small_size != SMALL_SIZE_CHECK(layout->curr_size);
656   int sx = pos_x - layout->tile_border_x - (UI_UNIT_X * 0.1f), sy = pos_y;
657 
658   for (FileAttributeColumnType column_type = 0; column_type < ATTRIBUTE_COLUMN_MAX;
659        column_type++) {
660     const FileAttributeColumn *column = &layout->attribute_columns[column_type];
661 
662     /* Name column is not a detail column (should already be drawn), always skip here. */
663     if (column_type == COLUMN_NAME) {
664       sx += column->width;
665       continue;
666     }
667     if (!file_attribute_column_type_enabled(params, column_type)) {
668       continue;
669     }
670 
671     const char *str = filelist_get_details_column_string(
672         column_type, file, small_size, update_stat_strings);
673 
674     if (str) {
675       file_draw_string(sx + ATTRIBUTE_COLUMN_PADDING,
676                        sy - layout->tile_border_y,
677                        IFACE_(str),
678                        column->width - 2 * ATTRIBUTE_COLUMN_PADDING,
679                        layout->tile_h,
680                        column->text_align,
681                        text_col);
682     }
683 
684     sx += column->width;
685   }
686 }
687 
file_draw_list(const bContext * C,ARegion * region)688 void file_draw_list(const bContext *C, ARegion *region)
689 {
690   SpaceFile *sfile = CTX_wm_space_file(C);
691   FileSelectParams *params = ED_fileselect_get_params(sfile);
692   FileLayout *layout = ED_fileselect_get_layout(sfile, region);
693   View2D *v2d = &region->v2d;
694   struct FileList *files = sfile->files;
695   struct FileDirEntry *file;
696   const char *root = filelist_dir(files);
697   ImBuf *imb;
698   uiBlock *block = UI_block_begin(C, region, __func__, UI_EMBOSS);
699   int numfiles;
700   int numfiles_layout;
701   int sx, sy;
702   int offset;
703   int textwidth, textheight;
704   int i;
705   bool is_icon;
706   eFontStyle_Align align;
707   bool do_drag;
708   uchar text_col[4];
709   const bool draw_columnheader = (params->display == FILE_VERTICALDISPLAY);
710   const float thumb_icon_aspect = MIN2(64.0f / (float)(params->thumbnail_size), 1.0f);
711 
712   numfiles = filelist_files_ensure(files);
713 
714   if (params->display != FILE_IMGDISPLAY) {
715     draw_background(layout, v2d);
716     draw_dividers(layout, v2d);
717   }
718 
719   offset = ED_fileselect_layout_offset(
720       layout, (int)region->v2d.cur.xmin, (int)-region->v2d.cur.ymax);
721   if (offset < 0) {
722     offset = 0;
723   }
724 
725   numfiles_layout = ED_fileselect_layout_numfiles(layout, region);
726 
727   /* adjust, so the next row is already drawn when scrolling */
728   if (layout->flag & FILE_LAYOUT_HOR) {
729     numfiles_layout += layout->rows;
730   }
731   else {
732     numfiles_layout += layout->flow_columns;
733   }
734 
735   filelist_file_cache_slidingwindow_set(files, numfiles_layout);
736 
737   textwidth = (FILE_IMGDISPLAY == params->display) ?
738                   layout->tile_w :
739                   round_fl_to_int(layout->attribute_columns[COLUMN_NAME].width);
740   textheight = (int)(layout->textheight * 3.0 / 2.0 + 0.5);
741 
742   align = (FILE_IMGDISPLAY == params->display) ? UI_STYLE_TEXT_CENTER : UI_STYLE_TEXT_LEFT;
743 
744   if (numfiles > 0) {
745     const bool success = filelist_file_cache_block(
746         files, min_ii(offset + (numfiles_layout / 2), numfiles - 1));
747     BLI_assert(success);
748     UNUSED_VARS_NDEBUG(success);
749 
750     filelist_cache_previews_update(files);
751 
752     /* Handle preview timer here,
753      * since it's filelist_file_cache_block() and filelist_cache_previews_update()
754      * which controls previews task. */
755     {
756       const bool previews_running = filelist_cache_previews_running(files);
757       //          printf("%s: preview task: %d\n", __func__, previews_running);
758       if (previews_running && !sfile->previews_timer) {
759         sfile->previews_timer = WM_event_add_timer_notifier(
760             CTX_wm_manager(C), CTX_wm_window(C), NC_SPACE | ND_SPACE_FILE_PREVIEW, 0.01);
761       }
762       if (!previews_running && sfile->previews_timer) {
763         /* Preview is not running, no need to keep generating update events! */
764         //              printf("%s: Inactive preview task, sleeping!\n", __func__);
765         WM_event_remove_timer_notifier(CTX_wm_manager(C), CTX_wm_window(C), sfile->previews_timer);
766         sfile->previews_timer = NULL;
767       }
768     }
769   }
770 
771   BLF_batch_draw_begin();
772 
773   UI_GetThemeColor4ubv(TH_TEXT, text_col);
774 
775   for (i = offset; (i < numfiles) && (i < offset + numfiles_layout); i++) {
776     uint file_selflag;
777     char path[FILE_MAX_LIBEXTRA];
778     int padx = 0.1f * UI_UNIT_X;
779     int icon_ofs = 0;
780 
781     ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
782     sx += (int)(v2d->tot.xmin + padx);
783     sy = (int)(v2d->tot.ymax - sy);
784 
785     file = filelist_file(files, i);
786     file_selflag = filelist_entry_select_get(sfile->files, file, CHECK_ALL);
787 
788     BLI_join_dirfile(path, sizeof(path), root, file->relpath);
789 
790     if (!(file_selflag & FILE_SEL_EDITING)) {
791       if ((params->highlight_file == i) || (file_selflag & FILE_SEL_HIGHLIGHTED) ||
792           (file_selflag & FILE_SEL_SELECTED)) {
793         int colorid = (file_selflag & FILE_SEL_SELECTED) ? TH_HILITE : TH_BACK;
794         int shade = (params->highlight_file == i) || (file_selflag & FILE_SEL_HIGHLIGHTED) ? 35 :
795                                                                                              0;
796         const short width = ELEM(params->display, FILE_VERTICALDISPLAY, FILE_HORIZONTALDISPLAY) ?
797                                 layout->tile_w - (2 * padx) :
798                                 layout->tile_w;
799 
800         BLI_assert(i == 0 || !FILENAME_IS_CURRPAR(file->relpath));
801 
802         draw_tile(
803             sx, sy - 1, width, sfile->layout->tile_h + layout->tile_border_y, colorid, shade);
804       }
805     }
806     UI_draw_roundbox_corner_set(UI_CNR_NONE);
807 
808     /* don't drag parent or refresh items */
809     do_drag = !(FILENAME_IS_CURRPAR(file->relpath));
810     const bool is_hidden = (file->attributes & FILE_ATTR_HIDDEN);
811     const bool is_link = (file->attributes & FILE_ATTR_ANY_LINK);
812 
813     if (FILE_IMGDISPLAY == params->display) {
814       const int icon = filelist_geticon(files, i, false);
815       is_icon = 0;
816       imb = filelist_getimage(files, i);
817       if (!imb) {
818         imb = filelist_geticon_image(files, i);
819         is_icon = 1;
820       }
821 
822       file_draw_preview(block,
823                         path,
824                         sx,
825                         sy,
826                         thumb_icon_aspect,
827                         imb,
828                         icon,
829                         layout,
830                         is_icon,
831                         file->typeflag,
832                         do_drag,
833                         is_hidden,
834                         is_link);
835     }
836     else {
837       file_draw_icon(block,
838                      path,
839                      sx,
840                      sy - layout->tile_border_y,
841                      filelist_geticon(files, i, true),
842                      ICON_DEFAULT_WIDTH_SCALE,
843                      ICON_DEFAULT_HEIGHT_SCALE,
844                      do_drag,
845                      is_hidden);
846       icon_ofs += ICON_DEFAULT_WIDTH_SCALE + 0.2f * UI_UNIT_X;
847     }
848 
849     if (file_selflag & FILE_SEL_EDITING) {
850       uiBut *but;
851       const short width = (params->display == FILE_IMGDISPLAY) ?
852                               textwidth :
853                               layout->attribute_columns[COLUMN_NAME].width -
854                                   ATTRIBUTE_COLUMN_PADDING;
855 
856       but = uiDefBut(block,
857                      UI_BTYPE_TEXT,
858                      1,
859                      "",
860                      sx + icon_ofs,
861                      sy - layout->tile_h - 0.15f * UI_UNIT_X,
862                      width - icon_ofs,
863                      textheight,
864                      sfile->params->renamefile,
865                      1.0f,
866                      (float)sizeof(sfile->params->renamefile),
867                      0,
868                      0,
869                      "");
870       UI_but_func_rename_set(but, renamebutton_cb, file);
871       UI_but_flag_enable(but, UI_BUT_NO_UTF8); /* allow non utf8 names */
872       UI_but_flag_disable(but, UI_BUT_UNDO);
873       if (false == UI_but_active_only(C, region, block, but)) {
874         file_selflag = filelist_entry_select_set(
875             sfile->files, file, FILE_SEL_REMOVE, FILE_SEL_EDITING, CHECK_ALL);
876       }
877     }
878 
879     /* file_selflag might have been modified by branch above. */
880     if ((file_selflag & FILE_SEL_EDITING) == 0) {
881       const int txpos = (params->display == FILE_IMGDISPLAY) ? sx : sx + 1 + icon_ofs;
882       const int typos = (params->display == FILE_IMGDISPLAY) ?
883                             sy - layout->tile_h + layout->textheight :
884                             sy - layout->tile_border_y;
885       const int twidth = (params->display == FILE_IMGDISPLAY) ?
886                              textwidth :
887                              textwidth - 1 - icon_ofs - padx - layout->tile_border_x;
888       file_draw_string(txpos, typos, file->name, (float)twidth, textheight, align, text_col);
889     }
890 
891     if (params->display != FILE_IMGDISPLAY) {
892       draw_details_columns(params, layout, file, sx, sy, text_col);
893     }
894   }
895 
896   BLF_batch_draw_end();
897 
898   UI_block_end(C, block);
899   UI_block_draw(C, block);
900 
901   /* Draw last, on top of file list. */
902   if (draw_columnheader) {
903     draw_columnheader_background(layout, v2d);
904     draw_columnheader_columns(params, layout, v2d, text_col);
905   }
906 
907   layout->curr_size = params->thumbnail_size;
908 }
909