Fix for crash in file selector using the 'Open' button - button was using old UI
[blender.git] / source / blender / editors / space_file / file_draw.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30
31 #include "BLI_blenlib.h"
32 #include "BLI_dynstr.h"
33 #include "BLI_storage_types.h"
34 #ifdef WIN32
35 #include "BLI_winstuff.h"
36 #endif
37
38 #include "BIF_gl.h"
39 #include "BIF_glutil.h"
40
41 #include "BKE_colortools.h"
42 #include "BKE_context.h"
43 #include "BKE_screen.h"
44 #include "BKE_global.h"
45 #include "BKE_utildefines.h"
46
47 #include "BLF_api.h"
48
49 #include "DNA_space_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_userdef_types.h"
53 #include "DNA_windowmanager_types.h"
54
55 #include "ED_datafiles.h"
56
57 #include "IMB_imbuf_types.h"
58 #include "IMB_imbuf.h"
59  
60 #include "MEM_guardedalloc.h"
61
62 #include "PIL_time.h"
63
64 #include "RNA_access.h"
65
66 #include "ED_fileselect.h"
67 #include "ED_screen.h"
68
69 #include "UI_interface.h"
70 #include "UI_interface_icons.h"
71 #include "UI_resources.h"
72 #include "UI_view2d.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "fsmenu.h"
78 #include "filelist.h"
79
80 #include "file_intern.h"        // own include
81
82 /* ui geometry */
83 #define IMASEL_BUTTONS_HEIGHT 40
84 #define IMASEL_BUTTONS_MARGIN 6
85 #define TILE_BORDER_X 8
86 #define TILE_BORDER_Y 8
87
88 /* button events */
89 enum {
90         B_REDR  = 0,
91         B_FS_EXEC,
92         B_FS_CANCEL,
93         B_FS_PARENT,
94         B_FS_DIRNAME,
95         B_FS_FILENAME
96 } eFile_ButEvents;
97
98
99 static void do_file_buttons(bContext *C, void *arg, int event)
100 {
101         switch(event) {
102                 case B_FS_PARENT:
103                         file_parent_exec(C, NULL); /* file_ops.c */
104                         break;
105                 case B_FS_FILENAME:
106                         file_filename_exec(C, NULL);
107                         break;
108                 case B_FS_DIRNAME:
109                         file_directory_exec(C, NULL);
110                         break;
111         }
112 }
113
114 /* Note: This function uses pixelspace (0, 0, winx, winy), not view2d. 
115  * The controls are laid out as follows:
116  *
117  * -------------------------------------------
118  * | Directory input               | execute |
119  * -------------------------------------------
120  * | Filename input        | + | - | cancel  |
121  * -------------------------------------------
122  *
123  * The input widgets will stretch to fill any excess space.
124  * When there isn't enough space for all controls to be shown, they are
125  * hidden in this order: x/-, execute/cancel, input widgets.
126  */
127 void file_draw_buttons(const bContext *C, ARegion *ar)
128 {
129         /* Button layout. */
130         const int max_x      = ar->winx - 10;
131         const int line1_y    = IMASEL_BUTTONS_HEIGHT/2 + IMASEL_BUTTONS_MARGIN*2;
132         const int line2_y    = IMASEL_BUTTONS_MARGIN;
133         const int input_minw = 20;
134         const int btn_h      = UI_UNIT_Y;
135         const int btn_fn_w   = UI_UNIT_X;
136         const int btn_minw   = 80;
137         const int btn_margin = 20;
138         const int separator  = 4;
139
140         /* Additional locals. */
141         char  name[20];
142         int loadbutton;
143         int fnumbuttons;
144         int min_x       = 10;
145         int available_w = max_x - min_x;
146         int line1_w     = available_w;
147         int line2_w     = available_w;
148         
149         uiBut*            but;
150         uiBlock*          block;
151         SpaceFile*        sfile  = CTX_wm_space_file(C);
152         FileSelectParams* params = ED_fileselect_get_params(sfile);
153         ARegion*                  artmp;
154         
155         /* Initialize UI block. */
156         sprintf(name, "win %p", ar);
157         block = uiBeginBlock(C, ar, name, UI_EMBOSS);
158         uiBlockSetHandleFunc(block, do_file_buttons, NULL);
159
160         /* exception to make space for collapsed region icon */
161         for (artmp=CTX_wm_area(C)->regionbase.first; artmp; artmp=artmp->next) {
162                 if (artmp->regiontype == RGN_TYPE_CHANNELS && artmp->flag & RGN_FLAG_HIDDEN) {
163                         min_x += 16;
164                         available_w -= 16;
165                 }
166         }
167         
168         /* Is there enough space for the execute / cancel buttons? */
169         loadbutton = UI_GetStringWidth(sfile->params->title) + btn_margin;
170         if (loadbutton < btn_minw) {
171                 loadbutton = MAX2(btn_minw, 
172                                   btn_margin + UI_GetStringWidth(params->title));
173         }
174         
175         if (available_w <= loadbutton + separator + input_minw 
176          || params->title[0] == 0) {
177                 loadbutton = 0;
178         } else {
179                 line1_w -= (loadbutton + separator);
180                 line2_w  = line1_w;
181         }
182
183         /* Is there enough space for file number increment/decrement buttons? */
184         fnumbuttons = 2 * btn_fn_w;
185         if (!loadbutton || line2_w <= fnumbuttons + separator + input_minw) {
186                 fnumbuttons = 0;
187         } else {
188                 line2_w -= (fnumbuttons + separator);
189         }
190         
191         /* Text input fields for directory and file. */
192         if (available_w > 0) {
193                 but = uiDefBut(block, TEX, B_FS_DIRNAME, "",
194                          min_x, line1_y, line1_w, btn_h, 
195                          params->dir, 0.0, (float)FILE_MAX-1, 0, 0, 
196                          "File path.");
197                 uiButSetCompleteFunc(but, autocomplete_directory, NULL);
198                 uiDefBut(block, TEX, B_FS_FILENAME, "",
199                          min_x, line2_y, line2_w, btn_h,
200                          params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, 
201                          "File name.");
202         }
203         
204         /* Filename number increment / decrement buttons. */
205         if (fnumbuttons) {
206                 uiBlockBeginAlign(block);
207                 but = uiDefIconButO(block, BUT, "FILE_OT_filenum", 0, ICON_ZOOMOUT,
208                         min_x + line2_w + separator, line2_y, 
209                         btn_fn_w, btn_h, 
210                         "Decrement the filename number.");    
211                 RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", -1); 
212         
213                 but = uiDefIconButO(block, BUT, "FILE_OT_filenum", 0, ICON_ZOOMIN, 
214                         min_x + line2_w + separator + btn_fn_w, line2_y, 
215                         btn_fn_w, btn_h, 
216                         "Increment the filename number.");    
217                 RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", 1); 
218                 uiBlockEndAlign(block);
219         }
220         
221         /* Execute / cancel buttons. */
222         if(loadbutton) {
223                 
224                 uiDefButO(block, BUT, "FILE_OT_execute", WM_OP_EXEC_REGION_WIN, params->title,
225                         max_x - loadbutton, line1_y, loadbutton, btn_h, 
226                         params->title);
227                 uiDefButO(block, BUT, "FILE_OT_cancel", WM_OP_EXEC_REGION_WIN, "Cancel",
228                         max_x - loadbutton, line2_y, loadbutton, btn_h, 
229                         "Cancel");
230         }
231         
232         uiEndBlock(C, block);
233         uiDrawBlock(C, block);
234 }
235
236
237 static void draw_tile(int sx, int sy, int width, int height, int colorid, int shade)
238 {       
239         UI_ThemeColorShade(colorid, shade);
240         uiSetRoundBox(15);      
241         uiRoundBox(sx, sy - height, sx + width, sy, 6);
242 }
243
244 #define FILE_SHORTEN_END                                0
245 #define FILE_SHORTEN_FRONT                              1
246
247 static float shorten_string(char* string, float w, int flag)
248 {       
249         char temp[FILE_MAX];
250         short shortened = 0;
251         float sw = 0;
252         float pad = 0;
253
254         if (w <= 0) {
255                 *string = '\0';
256                 return 0.0;
257         }
258
259         sw = file_string_width(string);
260         if (flag == FILE_SHORTEN_FRONT) {
261                 char *s = string;
262                 BLI_strncpy(temp, "...", 4);
263                 pad = file_string_width(temp);
264                 while ((*s) && (sw+pad>w)) {
265                         s++;
266                         sw = file_string_width(s);
267                         shortened = 1;
268                 }
269                 if (shortened) {
270                         int slen = strlen(s);                   
271                         BLI_strncpy(temp+3, s, slen+1);
272                         temp[slen+4] = '\0';
273                         BLI_strncpy(string, temp, slen+4);
274                 }
275         } else {
276                 char *s = string;
277                 while (sw>w) {
278                         int slen = strlen(string);
279                         string[slen-1] = '\0';
280                         sw = file_string_width(s);
281                         shortened = 1;
282                 }
283
284                 if (shortened) {
285                         int slen = strlen(string);
286                         if (slen > 3) {
287                                 BLI_strncpy(string+slen-3, "...", 4);                           
288                         }
289                 }
290         }
291         
292         return sw;
293 }
294
295 static int get_file_icon(struct direntry *file)
296 {
297         if (file->type & S_IFDIR) {
298                 if ( strcmp(file->relname, "..") == 0) {
299                                 return  ICON_FILE_PARENT;
300                 }
301                 return ICON_FILE_FOLDER;
302         }
303         else if (file->flags & BLENDERFILE)
304                 return ICON_FILE_BLEND;
305         else if (file->flags & IMAGEFILE)
306                 return ICON_FILE_IMAGE;
307         else if (file->flags & MOVIEFILE)
308                 return ICON_FILE_MOVIE;
309         else if (file->flags & PYSCRIPTFILE)
310                 return ICON_FILE_SCRIPT;
311         else if (file->flags & PYSCRIPTFILE)
312                 return ICON_FILE_SCRIPT;
313         else if (file->flags & SOUNDFILE) 
314                 return ICON_FILE_SOUND;
315         else if (file->flags & FTFONTFILE) 
316                 return ICON_FILE_FONT;
317         else if (file->flags & BTXFILE) 
318                 return ICON_FILE_BLANK;
319         else
320                 return ICON_FILE_BLANK;
321 }
322
323 static void file_draw_icon(int sx, int sy, int icon, int width, int height)
324 {
325         float x,y;
326         float alpha=1.0f;
327         
328         x = (float)(sx);
329         y = (float)(sy-height);
330         
331         if (icon == ICON_FILE_BLANK) alpha = 0.375f;
332         
333         glEnable(GL_BLEND);
334         
335         UI_icon_draw_aspect(x, y, icon, 1.f, alpha);
336 }
337
338
339 static void file_draw_string(int sx, int sy, const char* string, float width, int height, int flag)
340 {
341         int soffs;
342         char fname[FILE_MAXFILE];
343         float sw;
344         float x,y;
345
346         BLI_strncpy(fname,string, FILE_MAXFILE);
347         sw = shorten_string(fname, width, flag );
348
349         soffs = (width - sw) / 2;
350         x = (float)(sx);
351         y = (float)(sy-height);
352
353         BLF_position(x, y, 0);
354         BLF_draw(fname);
355 }
356
357 void file_calc_previews(const bContext *C, ARegion *ar)
358 {
359         SpaceFile *sfile= CTX_wm_space_file(C);
360         View2D *v2d= &ar->v2d;
361         
362         ED_fileselect_init_layout(sfile, ar);
363         /* +SCROLL_HEIGHT is bad hack to work around issue in UI_view2d_totRect_set */
364         UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height+V2D_SCROLL_HEIGHT);
365 }
366
367 static void file_draw_preview(int sx, int sy, ImBuf *imb, FileLayout *layout, short dropshadow)
368 {
369         if (imb) {
370                         float fx, fy;
371                         float dx, dy;
372                         int xco, yco;
373                         float scaledx, scaledy;
374                         float scale;
375                         int ex, ey;
376
377                         if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
378                                 if (imb->x > imb->y) {
379                                         scaledx = (float)layout->prv_w;
380                                         scaledy =  ( (float)imb->y/(float)imb->x )*layout->prv_w;
381                                         scale = scaledx/imb->x;
382                                 }
383                                 else {
384                                         scaledy = (float)layout->prv_h;
385                                         scaledx =  ( (float)imb->x/(float)imb->y )*layout->prv_h;
386                                         scale = scaledy/imb->y;
387                                 }
388                         } else {
389                                 scaledx = (float)imb->x;
390                                 scaledy = (float)imb->y;
391                                 scale = 1.0;
392                         }
393                         ex = (int)scaledx;
394                         ey = (int)scaledy;
395                         fx = ((float)layout->prv_w - (float)ex)/2.0f;
396                         fy = ((float)layout->prv_h - (float)ey)/2.0f;
397                         dx = (fx + 0.5f + layout->prv_border_x);
398                         dy = (fy + 0.5f - layout->prv_border_y);
399                         xco = (float)sx + dx;
400                         yco = (float)sy - layout->prv_h + dy;
401
402                         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
403                         
404                         /* shadow */
405                         if (dropshadow)
406                                 uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
407                         
408                         glEnable(GL_BLEND);
409                         
410                         /* the image */
411                         glColor4f(1.0, 1.0, 1.0, 1.0);
412                         glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
413                         
414                         /* border */
415                         if (dropshadow) {
416                                 glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
417                                 fdrawbox(xco, yco, xco + ex, yco + ey);
418                         }
419                         
420                         glDisable(GL_BLEND);
421                         imb = 0;
422                 }
423 }
424
425 static void renamebutton_cb(bContext *C, void *arg1, char *oldname)
426 {
427         char newname[FILE_MAX+12];
428         char orgname[FILE_MAX+12];
429         char filename[FILE_MAX+12];
430         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
431         ARegion* ar = CTX_wm_region(C);
432
433         struct direntry *file = (struct direntry *)arg1;
434
435         BLI_make_file_string(G.sce, orgname, sfile->params->dir, oldname);
436         BLI_strncpy(filename, file->relname, sizeof(filename));
437         BLI_make_file_string(G.sce, newname, sfile->params->dir, filename);
438
439         if( strcmp(orgname, newname) != 0 ) {
440                 if (!BLI_exists(newname)) {
441                         BLI_rename(orgname, newname);
442                         /* to make sure we show what is on disk */
443                         filelist_free(sfile->files);
444                 } else {
445                         BLI_strncpy(file->relname, oldname, strlen(oldname)+1);
446                 }
447                 
448                 ED_region_tag_redraw(ar);
449         }
450 }
451
452
453 static void draw_background(FileLayout *layout, View2D *v2d)
454 {
455         int i;
456         int sy;
457
458         /* alternating flat shade background */
459         for (i=0; (i <= layout->rows); i+=2)
460         {
461                 sy = v2d->cur.ymax - i*(layout->tile_h+2*layout->tile_border_y) - layout->tile_border_y;
462
463                 UI_ThemeColorShade(TH_BACK, -7);
464                 glRectf(v2d->cur.xmin, sy, v2d->cur.xmax, sy+layout->tile_h+2*layout->tile_border_y);
465                 
466         }
467 }
468
469 static void draw_dividers(FileLayout *layout, View2D *v2d)
470 {
471         int sx;
472
473         /* vertical column dividers */
474         sx = v2d->tot.xmin;
475         while (sx < v2d->cur.xmax) {
476                 sx += (layout->tile_w+2*layout->tile_border_x);
477                 
478                 UI_ThemeColorShade(TH_BACK, 30);
479                 sdrawline(sx+1,  v2d->cur.ymax - layout->tile_border_y ,  sx+1,  v2d->cur.ymin); 
480                 UI_ThemeColorShade(TH_BACK, -30);
481                 sdrawline(sx,  v2d->cur.ymax - layout->tile_border_y ,  sx,  v2d->cur.ymin); 
482         }
483 }
484
485 void file_draw_list(const bContext *C, ARegion *ar)
486 {
487         SpaceFile *sfile= CTX_wm_space_file(C);
488         FileSelectParams* params = ED_fileselect_get_params(sfile);
489         FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
490         View2D *v2d= &ar->v2d;
491         struct FileList* files = sfile->files;
492         struct direntry *file;
493         ImBuf *imb;
494         int numfiles;
495         int numfiles_layout;
496         int colorid = 0;
497         int sx, sy;
498         int offset;
499         int i;
500         float sw, spos;
501         short is_icon;
502
503         numfiles = filelist_numfiles(files);
504         
505         if (params->display != FILE_IMGDISPLAY) {
506
507                 draw_background(layout, v2d);
508         
509                 draw_dividers(layout, v2d);
510         }
511
512         offset = ED_fileselect_layout_offset(layout, ar->v2d.cur.xmin, -ar->v2d.cur.ymax);
513         if (offset<0) offset=0;
514
515         numfiles_layout = ED_fileselect_layout_numfiles(layout, ar);
516
517         for (i=offset; (i < numfiles) && (i<offset+numfiles_layout); ++i)
518         {
519                 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
520                 sx += v2d->tot.xmin+2;
521                 sy = v2d->tot.ymax - sy;
522
523                 file = filelist_file(files, i); 
524                 
525                 if (!(file->flags & EDITING)) {
526                         if (params->active_file == i) {
527                                 if (file->flags & ACTIVE) colorid= TH_HILITE;
528                                 else colorid = TH_BACK;
529                                 draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
530                         } else if (file->flags & ACTIVE) {
531                                 colorid = TH_HILITE;
532                                 draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
533                         } 
534                 }
535
536                 spos = sx;
537
538                 if ( FILE_IMGDISPLAY == params->display ) {
539                         if ( (file->flags & IMAGEFILE) /* || (file->flags & MOVIEFILE) */) {                    
540                                 filelist_loadimage(files, i);
541                         }
542                         is_icon = 0;
543                         imb = filelist_getimage(files, i);
544                         if (!imb) {
545                                 imb = filelist_geticon(files,i);
546                                 is_icon = 1;
547                         }
548                         
549                         file_draw_preview(sx, sy, imb, layout, !is_icon && (file->flags & IMAGEFILE));
550
551                 } else {
552                                 file_draw_icon(spos, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
553                                 spos += ICON_DEFAULT_WIDTH + 4;
554                 }
555
556                 UI_ThemeColor4(TH_TEXT);
557
558                 sw = file_string_width(file->relname);
559                 if (file->flags & EDITING) {
560                         int but_width = (FILE_IMGDISPLAY == params->display) ? layout->tile_w : layout->column_widths[COLUMN_NAME];
561                         uiBlock *block = uiBeginBlock(C, ar, "FileName", UI_EMBOSS);
562                         uiBut *but = uiDefBut(block, TEX, 1, "", spos, sy-layout->tile_h-3, 
563                                 but_width, layout->textheight*2, file->relname, 1.0f, (float)FILE_MAX,0,0,"");
564                         uiButSetRenameFunc(but, renamebutton_cb, file);
565                         if ( 0 == uiButActiveOnly(C, block, but)) {
566                                 file->flags &= ~EDITING;
567                         }
568                         uiEndBlock(C, block);
569                         uiDrawBlock(C, block);
570                 } else {
571                         float name_width = (FILE_IMGDISPLAY == params->display) ? layout->tile_w : sw;
572                         file_draw_string(spos, sy, file->relname, name_width, layout->tile_h, FILE_SHORTEN_END);
573                 }
574
575                 uiSetRoundBox(0);
576
577                 if (params->display == FILE_SHORTDISPLAY) {
578                         spos += layout->column_widths[COLUMN_NAME] + 12;
579                         if (!(file->type & S_IFDIR)) {
580                                 sw = file_string_width(file->size);
581                                 file_draw_string(spos, sy, file->size, sw+1, layout->tile_h, FILE_SHORTEN_END); 
582                                 spos += layout->column_widths[COLUMN_SIZE] + 12;
583                         }
584                 } else if (params->display == FILE_LONGDISPLAY) {
585                         spos += layout->column_widths[COLUMN_NAME] + 12;
586
587 #ifndef WIN32
588                         /* rwx rwx rwx */
589                         sw = file_string_width(file->mode1);
590                         file_draw_string(spos, sy, file->mode1, sw, layout->tile_h, FILE_SHORTEN_END); 
591                         spos += layout->column_widths[COLUMN_MODE1] + 12;
592
593                         sw = file_string_width(file->mode2);
594                         file_draw_string(spos, sy, file->mode2, sw, layout->tile_h, FILE_SHORTEN_END);
595                         spos += layout->column_widths[COLUMN_MODE2] + 12;
596
597                         sw = file_string_width(file->mode3);
598                         file_draw_string(spos, sy, file->mode3, sw, layout->tile_h, FILE_SHORTEN_END);
599                         spos += layout->column_widths[COLUMN_MODE3] + 12;
600
601                         sw = file_string_width(file->owner);
602                         file_draw_string(spos, sy, file->owner, sw, layout->tile_h, FILE_SHORTEN_END);
603                         spos += layout->column_widths[COLUMN_OWNER] + 12;
604 #endif
605
606                         sw = file_string_width(file->date);
607                         file_draw_string(spos, sy, file->date, sw, layout->tile_h, FILE_SHORTEN_END);
608                         spos += layout->column_widths[COLUMN_DATE] + 12;
609
610                         sw = file_string_width(file->time);
611                         file_draw_string(spos, sy, file->time, sw, layout->tile_h, FILE_SHORTEN_END); 
612                         spos += layout->column_widths[COLUMN_TIME] + 12;
613
614                         if (!(file->type & S_IFDIR)) {
615                                 sw = file_string_width(file->size);
616                                 file_draw_string(spos, sy, file->size, sw, layout->tile_h, FILE_SHORTEN_END);
617                                 spos += layout->column_widths[COLUMN_SIZE] + 12;
618                         }
619                 }
620         }
621
622         if (!sfile->loadimage_timer)
623                 sfile->loadimage_timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER1, 1.0/30.0);      /* max 30 frames/sec. */
624 }
625
626