2.5 file browser
authorAndrea Weikert <elubie@gmx.net>
Sun, 26 Jul 2009 18:52:27 +0000 (18:52 +0000)
committerAndrea Weikert <elubie@gmx.net>
Sun, 26 Jul 2009 18:52:27 +0000 (18:52 +0000)
* operator for create new directory activated (IKEY)
* operator for rename (works on files and directories so far) (CTRL+LMB)

Note: fail to rename is rather quiet, no message popup, just doesn't rename if it can't.
So far checked that (On Windows Vista) rename fails on system directories, which I think acceptable.

Note: I removed the code that (silently) deletes file if I rename file to an existing one. Considered harmful :)

release/ui/space_filebrowser.py
source/blender/blenlib/BLI_storage_types.h
source/blender/blenlib/intern/fileops.c
source/blender/editors/space_file/file_draw.c
source/blender/editors/space_file/file_intern.h
source/blender/editors/space_file/file_ops.c
source/blender/editors/space_file/filelist.c
source/blender/editors/space_file/filelist.h
source/blender/editors/space_file/space_file.c
source/blender/makesdna/DNA_space_types.h

index 9a235516c9d87ad7321111a71ee1770e3e3172e6..2d35616069cb492c08b427d884930c25a153608a 100644 (file)
@@ -48,6 +48,7 @@ class FILEBROWSER_MT_directory(bpy.types.Menu):
        def draw(self, context):
                layout = self.layout
 
+               layout.itemO("file.directory_new", text="New Directory", icon='ICON_NEWFOLDER')
                layout.itemO("file.refresh", text="Refresh", icon='ICON_FILE_REFRESH')
                layout.itemO("file.parent", text="Parent", icon='ICON_FILE_PARENT')
                
index cdc85d1be39486a83b42c15c839d606045a03fca..36d96f6075c777417c3e56c1a5fcb635d5871e37 100644 (file)
@@ -77,7 +77,6 @@ struct direntry{
 #define SELECT                 1
 #define HIDDEN                 1
 #define FIRST                  1
-#define ACTIVE                 2
 #define DESELECT               0
 #define NOT_YET                        0
 #define VISIBLE                        0
index 42fd75a543e9296cf98a7a1f9a2f68e4808e7ce7..b9c0dd65edea3a958bfa9d6d7c1e0adc4b19c4b5 100644 (file)
@@ -311,10 +311,10 @@ void BLI_recurdir_fileops(char *dirname) {
 int BLI_rename(char *from, char *to) {
        if (!BLI_exists(from)) return 0;
 
-       /* make sure the filenames are different (case insensitive) before removing */
-       if (BLI_exists(to) && BLI_strcasecmp(from, to))
-               if(BLI_delete(to, 0, 0)) return 1;
-               
+       /* refuse to rename if file already exists */
+       if (BLI_exists(to))
+               return 1; 
+
        return rename(from, to);
 }
 
@@ -391,8 +391,9 @@ void BLI_recurdir_fileops(char *dirname) {
 int BLI_rename(char *from, char *to) {
        if (!BLI_exists(from)) return 0;
        
-       if (BLI_exists(to))     if(BLI_delete(to, 0, 0)) return 1;
-               
+       /* refuse to rename if file already exists */
+       if (BLI_exists(to)) return 1;
+
        return rename(from, to);
 }
 
index 780b99c3cae0453d7ee4160100b23ca5f1ef49fa..f2906686db5da1e4e0cf883441e5589fb7d66825 100644 (file)
@@ -495,6 +495,25 @@ void file_draw_previews(const bContext *C, ARegion *ar)
        uiSetRoundBox(0);
 }
 
+static void renamebutton_cb(bContext *C, void *arg1, char *oldname)
+{
+       char newname[FILE_MAX+12];
+       char orgname[FILE_MAX+12];
+       char filename[FILE_MAX+12];
+       SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
+       struct direntry *file = (struct direntry *)arg1;
+
+       BLI_make_file_string(G.sce, orgname, sfile->params->dir, oldname);
+       BLI_strncpy(filename, file->relname, sizeof(filename));
+       BLI_make_file_string(G.sce, newname, sfile->params->dir, filename);
+
+       if( strcmp(orgname, newname) != 0 ) {
+               BLI_rename(orgname, newname);
+
+               /* to refresh the file list, does sorting again */
+               filelist_free(sfile->files);
+       }
+}
 
 void file_draw_list(const bContext *C, ARegion *ar)
 {
@@ -554,15 +573,17 @@ void file_draw_list(const bContext *C, ARegion *ar)
                sy = v2d->tot.ymax - sy;
 
                file = filelist_file(files, i); 
-
-               if (params->active_file == i) {
-                       if (file->flags & ACTIVE) colorid= TH_HILITE;
-                       else colorid = TH_BACK;
-                       draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
-               } else if (file->flags & ACTIVE) {
-                       colorid = TH_HILITE;
-                       draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
-               } 
+               
+               if (!(file->flags & EDITING)) {
+                       if (params->active_file == i) {
+                               if (file->flags & ACTIVE) colorid= TH_HILITE;
+                               else colorid = TH_BACK;
+                               draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
+                       } else if (file->flags & ACTIVE) {
+                               colorid = TH_HILITE;
+                               draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
+                       } 
+               }
 
                spos = sx;
                file_draw_icon(spos, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
@@ -571,7 +592,19 @@ void file_draw_list(const bContext *C, ARegion *ar)
                UI_ThemeColor4(TH_TEXT);
                
                sw = file_string_width(file->relname);
-               file_draw_string(spos, sy, file->relname, sw, layout->tile_h, FILE_SHORTEN_END);
+               if (file->flags & EDITING) {
+                       uiBlock *block = uiBeginBlock(C, ar, "FileName", UI_EMBOSS);
+                       uiBut *but = uiDefBut(block, TEX, 1, "", spos, sy-layout->tile_h-3, 
+                               layout->column_widths[COLUMN_NAME], layout->tile_h, file->relname, 1.0f, (float)FILE_MAX,0,0,"");
+                       uiButSetRenameFunc(but, renamebutton_cb, file);
+                       if ( 0 == uiButActiveOnly(C, block, but)) {
+                               file->flags &= ~EDITING;
+                       }
+                       uiEndBlock(C, block);
+                       uiDrawBlock(C, block);
+               } else {
+                       file_draw_string(spos, sy, file->relname, sw, layout->tile_h, FILE_SHORTEN_END);
+               }
                spos += layout->column_widths[COLUMN_NAME] + 12;
                if (params->display == FILE_SHOWSHORT) {
                        if (!(file->type & S_IFDIR)) {
index dce56e05d6b241c7097462c4c24ad56d5f4db9d7..9f1e4ad0e25eb5e1909c99ccdf113ca3b2a23a47 100644 (file)
@@ -70,6 +70,7 @@ void FILE_OT_refresh(struct wmOperatorType *ot);
 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot);
 void FILE_OT_filenum(struct wmOperatorType *ot);
 void FILE_OT_delete(struct wmOperatorType *ot);
+void FILE_OT_rename(struct wmOperatorType *ot);
 
 int file_exec(bContext *C, struct wmOperator *unused);
 int file_cancel_exec(bContext *C, struct wmOperator *unused);
index 0bc2e310810221081f80c4bd2d070a18444eb475..46bfa022914a1c92faaef1ab9f7b43fdb9155028 100644 (file)
@@ -870,6 +870,37 @@ void FILE_OT_filenum(struct wmOperatorType *ot)
        RNA_def_int(ot->srna, "increment", 1, 0, 100, "Increment", "", 0,100);
 }
 
+int file_rename_exec(bContext *C, wmOperator *op)
+{
+       ScrArea *sa= CTX_wm_area(C);
+       SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
+       
+       if(sfile->params) {
+               int idx = sfile->params->active_file;
+               int numfiles = filelist_numfiles(sfile->files);
+               if ( (0<=idx) && (idx<numfiles) ) {
+                       struct direntry *file= filelist_file(sfile->files, idx);
+                       file->flags |= EDITING;
+               }
+               ED_area_tag_redraw(sa);
+       }
+       
+       return OPERATOR_FINISHED;
+
+}
+
+void FILE_OT_rename(struct wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Rename File or Directory";
+       ot->idname= "FILE_OT_rename";
+       
+       /* api callbacks */
+       ot->exec= file_rename_exec;
+       ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
+
+}
+
 int file_delete_poll(bContext *C)
 {
        int poll = ED_operator_file_active(C);
index 6e442c654cad38bb40a3a1c80ab95a6f3d030c6d..60a37aac9bd7ee1cd2a2a4d1ff887978a570b423 100644 (file)
@@ -113,7 +113,6 @@ typedef struct FileList
        int numfiles;
        int numfiltered;
        char dir[FILE_MAX];
-       int has_func;
        short prv_w;
        short prv_h;
        short hide_dot;
@@ -698,7 +697,6 @@ struct direntry * filelist_file(struct FileList* filelist, int index)
        return &filelist->filelist[fidx];
 }
 
-
 int filelist_find(struct FileList* filelist, char *file)
 {
        int index = -1;
@@ -922,9 +920,5 @@ void filelist_sort(struct FileList* filelist, short sort)
                qsort(filelist->filelist, filelist->numfiles, sizeof(struct direntry), compare_extension);      
        }
 
-       file= filelist->filelist;
-       for(num=0; num<filelist->numfiles; num++, file++) {
-               file->flags &= ~HILITE;
-       }
        filelist_filter(filelist);
 }
index dd3c2c766c181b4cbb424cb2716e180fb22af1c0..785e40d145ce1df7ad5dc04b590c71596f32c95d 100644 (file)
@@ -54,7 +54,7 @@ void                          filelist_sort(struct FileList* filelist, short sort);
 int                                    filelist_numfiles(struct FileList* filelist);
 const char *           filelist_dir(struct FileList* filelist);
 void                           filelist_setdir(struct FileList* filelist, const char *dir);
-struct direntry *      filelist_file(struct FileList* filelist, int index);
+void                           filelist_end_edit(struct FileList* filelist, int index);
 void                           filelist_hidedot(struct FileList* filelist, short hide);
 void                           filelist_setfilter(struct FileList* filelist, unsigned int filter);
 void                           filelist_filter(struct FileList* filelist);
index 077ad65830cf4d697771661bc6a53b25497d9351..cdf65fc5af23079eb8a8379f4b51c9c6e4daf68b 100644 (file)
@@ -272,7 +272,7 @@ static void file_main_area_draw(const bContext *C, ARegion *ar)
        float col[3];
 
        /* Needed, because filelist is not initialized on loading */
-       if (!sfile->files)
+       if (!sfile->files || filelist_empty(sfile->files))
                file_refresh(C, NULL);
 
        layout = ED_fileselect_get_layout(sfile, ar);
@@ -346,6 +346,7 @@ void file_operatortypes(void)
        WM_operatortype_append(FILE_OT_filenum);
        WM_operatortype_append(FILE_OT_directory_new);
        WM_operatortype_append(FILE_OT_delete);
+       WM_operatortype_append(FILE_OT_rename);
 }
 
 /* NOTE: do not add .blend file reading on this level */
@@ -360,7 +361,7 @@ void file_keymap(struct wmWindowManager *wm)
        WM_keymap_add_item(keymap, "FILE_OT_hidedot", HKEY, KM_PRESS, 0, 0);
        WM_keymap_add_item(keymap, "FILE_OT_previous", BACKSPACEKEY, KM_PRESS, 0, 0);
        WM_keymap_add_item(keymap, "FILE_OT_next", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0);
-       /* WM_keymap_add_item(keymap, "FILE_OT_directory_new", IKEY, KM_PRESS, 0, 0); */ /* XXX needs button */
+       WM_keymap_add_item(keymap, "FILE_OT_directory_new", IKEY, KM_PRESS, 0, 0);  /* XXX needs button */
        WM_keymap_add_item(keymap, "FILE_OT_delete", XKEY, KM_PRESS, 0, 0);
 
        /* keys for main area */
@@ -368,6 +369,7 @@ void file_keymap(struct wmWindowManager *wm)
        WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_PRESS, 0, 0);
        WM_keymap_add_item(keymap, "FILE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
        WM_keymap_add_item(keymap, "FILE_OT_select_border", BKEY, KM_PRESS, 0, 0);
+       WM_keymap_add_item(keymap, "FILE_OT_rename", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
        WM_keymap_add_item(keymap, "FILE_OT_highlight", MOUSEMOVE, KM_ANY, 0, 0);
        WM_keymap_add_item(keymap, "FILE_OT_loadimages", TIMER1, KM_ANY, KM_ANY, 0);
        kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
index c61935c106f8ebf17a6969658c43eded67975e25..c113f627da36c9aefdcf86d42fd80848f2f8599e 100644 (file)
@@ -642,7 +642,8 @@ enum FileSortTypeE {
 #define FILE_BOOKMARKS         512
 
 /* files in filesel list: 2=ACTIVE  */
-#define HILITE                         1
+#define EDITING                                1
+#define ACTIVE                         2
 #define BLENDERFILE                    4
 #define PSXFILE                                8
 #define IMAGEFILE                      16