== file browser == Code cleanup
authorAndrea Weikert <elubie@gmx.net>
Sun, 20 Mar 2011 15:15:05 +0000 (15:15 +0000)
committerAndrea Weikert <elubie@gmx.net>
Sun, 20 Mar 2011 15:15:05 +0000 (15:15 +0000)
* Remove direct access to file->selflag in file_ops.c

source/blender/editors/space_file/file_ops.c
source/blender/editors/space_file/filelist.c
source/blender/editors/space_file/filelist.h

index f0d4b2df8d3519ceba5b79b319c342283fd9216f..9005a6c17965e61ec779d906f3b4ef85b25a88fb 100644 (file)
@@ -91,15 +91,11 @@ static FileSelection find_file_mouse_rect(SpaceFile *sfile, struct ARegion* ar,
 
 static void file_deselect_all(SpaceFile* sfile, unsigned int flag)
 {
-       int numfiles = filelist_numfiles(sfile->files);
-       int i;
-
-       for ( i=0; i < numfiles; ++i) {
-               struct direntry* file = filelist_file(sfile->files, i);
-               if (file && (file->selflag & flag)) {
-                       file->selflag &= ~flag;
-               }
-       }
+       FileSelection sel;
+       sel.first = 0;
+       sel.last = filelist_numfiles(sfile->files)-1;
+       
+       filelist_select(sfile->files, &sel, FILE_SEL_REMOVE, flag, CHECK_ALL);
 }
 
 typedef enum FileSelect { 
@@ -150,8 +146,7 @@ static FileSelection file_selection_get(bContext* C, const rcti* rect, short fil
        if (fill && (sel.last >= 0) && (sel.last < numfiles) ) {
                int f= sel.last;
                while (f >= 0) {
-                       struct direntry* file = filelist_file(sfile->files, f);
-                       if (file->selflag & SELECTED_FILE)
+                       if ( filelist_is_selected(sfile->files, f, SELECTED_FILE, CHECK_ALL) )
                                break;
                        f--;
                }
@@ -210,10 +205,9 @@ static FileSelect file_select(bContext* C, const rcti* rect, FileSelType select,
        SpaceFile *sfile= CTX_wm_space_file(C);
        FileSelect retval = FILE_SELECT_NOTHING;
        FileSelection sel= file_selection_get(C, rect, fill); /* get the selection */
-       struct direntry *file;
        
        /* flag the files as selected in the filelist */
-       filelist_select(sfile->files, &sel, select, SELECTED_FILE);
+       filelist_select(sfile->files, &sel, select, SELECTED_FILE, CHECK_ALL);
        
        /* Don't act on multiple selected files */
        if (sel.first != sel.last) select = 0;
@@ -222,8 +216,7 @@ static FileSelect file_select(bContext* C, const rcti* rect, FileSelType select,
        if ( (sel.last >= 0) && ((select == FILE_SEL_ADD) || (select == FILE_SEL_TOGGLE)) )
        {
                /* Check last selection, if selected, act on the file or dir */
-               file = filelist_file(sfile->files, sel.last);
-               if (file && (file->selflag & SELECTED_FILE)) {
+               if (filelist_is_selected(sfile->files, sel.last, SELECTED_FILE, CHECK_ALL)) {
                        retval = file_select_do(C, sel.last);
                }
        }
@@ -258,7 +251,7 @@ static int file_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
                sel = file_selection_get(C, &rect, 0);
                if ( (sel.first != params->sel_first) || (sel.last != params->sel_last) ) {
                        file_deselect_all(sfile, HILITED_FILE);
-                       filelist_select(sfile->files, &sel, FILE_SEL_ADD, HILITED_FILE);
+                       filelist_select(sfile->files, &sel, FILE_SEL_ADD, HILITED_FILE, CHECK_ALL);
                        WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_PARAMS, NULL);
                }
                params->sel_first = sel.first; params->sel_last = sel.last;
@@ -366,29 +359,28 @@ static int file_select_all_exec(bContext *C, wmOperator *UNUSED(op))
 {
        ScrArea *sa= CTX_wm_area(C);
        SpaceFile *sfile= CTX_wm_space_file(C);
+       FileSelection sel;
        int numfiles = filelist_numfiles(sfile->files);
        int i;
-       int select = 1;
+       int is_selected = 0;
+    
+       sel.first = 0; 
+       sel.last = numfiles-1;
 
-       /* if any file is selected, deselect all first */
+       /* Is any file selected ? */
        for ( i=0; i < numfiles; ++i) {
-               struct direntry* file = filelist_file(sfile->files, i);
-               if (file && (file->selflag & SELECTED_FILE)) {
-                       file->selflag &= ~SELECTED_FILE;
-                       select = 0;
-                       ED_area_tag_redraw(sa);
+               if (filelist_is_selected(sfile->files, i, SELECTED_FILE, CHECK_ALL)) {
+                       is_selected = 1;
+                       break;
                }
        }
        /* select all only if previously no file was selected */
-       if (select) {
-               for ( i=0; i < numfiles; ++i) {
-                       struct direntry* file = filelist_file(sfile->files, i);
-                       if(file && !S_ISDIR(file->type)) {
-                               file->selflag |= SELECTED_FILE;
-                               ED_area_tag_redraw(sa);
-                       }
-               }
+       if (is_selected) {
+               filelist_select(sfile->files, &sel, FILE_SEL_REMOVE, SELECTED_FILE, CHECK_ALL);
+       } else {
+               filelist_select(sfile->files, &sel, FILE_SEL_ADD, SELECTED_FILE, CHECK_FILES);
        }
+       ED_area_tag_redraw(sa);
        return OPERATOR_FINISHED;
 }
 
@@ -630,27 +622,23 @@ void file_sfile_to_operator(wmOperator *op, SpaceFile *sfile, char *filepath)
                struct direntry *file;
                if(RNA_struct_find_property(op->ptr, "files")) {
                        for (i=0; i<numfiles; i++) {
-                               file = filelist_file(sfile->files, i);
-                               if(file->selflag & SELECTED_FILE) {
-                                       if ((file->type & S_IFDIR)==0) {
-                                               RNA_collection_add(op->ptr, "files", &itemptr);
-                                               RNA_string_set(&itemptr, "name", file->relname);
-                                       }
+                               if (filelist_is_selected(sfile->files, i, SELECTED_FILE, CHECK_FILES)) {
+                                       RNA_collection_add(op->ptr, "files", &itemptr);
+                                       RNA_string_set(&itemptr, "name", file->relname);
                                }
                        }
                }
                
                if(RNA_struct_find_property(op->ptr, "dirs")) {
                        for (i=0; i<numfiles; i++) {
-                               file = filelist_file(sfile->files, i);
-                               if(file->selflag & SELECTED_FILE) {
-                                       if ((file->type & S_IFDIR)) {
-                                               RNA_collection_add(op->ptr, "dirs", &itemptr);
-                                               RNA_string_set(&itemptr, "name", file->relname);
-                                       }
+                               if (filelist_is_selected(sfile->files, i, SELECTED_FILE, CHECK_DIRS)) {
+                                       RNA_collection_add(op->ptr, "dirs", &itemptr);
+                                       RNA_string_set(&itemptr, "name", file->relname);
                                }
                        }
                }
+
+
        }
 }
 
@@ -728,12 +716,11 @@ int file_exec(bContext *C, wmOperator *exec_op)
                 to prevent closing when doubleclicking on .. item */
                if (RNA_boolean_get(exec_op->ptr, "need_active")) {
                        int i, active=0;
-                       struct direntry *file;
                        
                        for (i=0; i<filelist_numfiles(sfile->files); i++) {
-                               file = filelist_file(sfile->files, i);
-                               if(file->selflag & SELECTED_FILE) {
+                               if(filelist_is_selected(sfile->files, i, SELECTED_FILE, CHECK_ALL)) {
                                        active=1;
+                                       break;
                                }
                        }
                        if (active == 0)
@@ -896,8 +883,7 @@ static int file_smoothscroll_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent
        /* check if we are editing a name */
        for (i=0; i < numfiles; ++i)
        {
-               struct direntry *file = filelist_file(sfile->files, i); 
-               if (file->selflag & EDITING_FILE) {
+               if (filelist_is_selected(sfile->files, i, EDITING_FILE, CHECK_ALL) ) {
                        edit_idx=i;
                        break;
                }
@@ -1300,7 +1286,7 @@ static int file_rename_exec(bContext *C, wmOperator *UNUSED(op))
                int numfiles = filelist_numfiles(sfile->files);
                if ( (0<=idx) && (idx<numfiles) ) {
                        struct direntry *file= filelist_file(sfile->files, idx);
-                       file->selflag |= EDITING_FILE;
+                       filelist_select_file(sfile->files, idx, FILE_SEL_ADD, EDITING_FILE, CHECK_ALL);
                        BLI_strncpy(sfile->params->renameedit, file->relname, FILE_MAXFILE);
                        sfile->params->renamefile[0]= '\0';
                }
index 649c2dadc0cc9bd6ea82e1a73b3067a698b9ac3e..1292cc68f65951503ae152e91342fb547c7bed2e 100644 (file)
@@ -906,34 +906,22 @@ void filelist_parent(struct FileList* filelist)
        filelist_readdir(filelist);
 }
 
-
-void filelist_swapselect(struct FileList* filelist)
+void filelist_select_file(struct FileList* filelist, int index, FileSelType select, unsigned int flag, FileCheckType check)
 {
-       struct direntry *file;
-       int num, act= 0;
-       
-       file= filelist->filelist;
-       for(num=0; num<filelist->numfiles; num++, file++) {
-               if(file->selflag & SELECTED_FILE) {
-                       act= 1;
-                       break;
+       struct direntry* file = filelist_file(filelist, index);
+       if (file != NULL) {     
+               int check_ok = 0; 
+               switch (check) {
+                       case CHECK_DIRS:
+                       case CHECK_ALL:
+                               check_ok = 1;
+                               break;
+                       case CHECK_FILES:
+                       default:
+                               check_ok = !S_ISDIR(file->type);
+                               break;
                }
-       }
-       file= filelist->filelist+2;
-       for(num=2; num<filelist->numfiles; num++, file++) {
-               if(act) file->selflag &= ~SELECTED_FILE;
-               else file->selflag |= SELECTED_FILE;
-       }
-}
-
-void filelist_select(struct FileList* filelist, FileSelection* sel, FileSelType select, unsigned int flag)
-{
-       /* select all valid files between first and last indicated */
-       if ( (sel->first >= 0) && (sel->first < filelist->numfiltered) && (sel->last >= 0) && (sel->last < filelist->numfiltered) ) {
-               int current_file;
-               for (current_file = sel->first; current_file <= sel->last; current_file++) {    
-                       struct direntry* file = filelist_file(filelist, current_file);
-                       
+               if (check_ok) {
                        switch (select) {
                                case FILE_SEL_REMOVE:
                                        file->selflag &= ~flag;
@@ -949,6 +937,33 @@ void filelist_select(struct FileList* filelist, FileSelection* sel, FileSelType
        }
 }
 
+void filelist_select(struct FileList* filelist, FileSelection* sel, FileSelType select, unsigned int flag, FileCheckType check)
+{
+       /* select all valid files between first and last indicated */
+       if ( (sel->first >= 0) && (sel->first < filelist->numfiltered) && (sel->last >= 0) && (sel->last < filelist->numfiltered) ) {
+               int current_file;
+               for (current_file = sel->first; current_file <= sel->last; current_file++) {    
+                       filelist_select_file(filelist, current_file, select, flag, check);
+               }
+       }
+}
+
+int    filelist_is_selected(struct FileList* filelist, int index, unsigned int flag, FileCheckType check)
+{
+       struct direntry* file = filelist_file(filelist, index);
+       if (!file) {
+               return 0;
+       }
+       switch (check) {
+               case CHECK_DIRS:
+                       return S_ISDIR(file->type) && (file->selflag & SELECTED_FILE);
+               case CHECK_FILES:
+                       return S_ISREG(file->type) && (file->selflag & SELECTED_FILE);
+               case CHECK_ALL:
+               default:
+                       return (file->selflag & SELECTED_FILE);
+       }
+}
 
 void filelist_sort(struct FileList* filelist, short sort)
 {
index 3e3b52ed17c29d8002cb88ca9929c8260864819b..c63e7f0b1cebb770cfd953d13a30f25a41a4238b 100644 (file)
@@ -58,6 +58,13 @@ typedef enum FileSelType {
        FILE_SEL_TOGGLE  = 2
 } FileSelType;
 
+typedef enum FileCheckType
+{
+       CHECK_DIRS = 1,
+       CHECK_FILES = 2,
+       CHECK_ALL = 3
+} FileCheckType;
+
 struct FileList *      filelist_new(short type);
 void                           filelist_init_icons(void);
 void                           filelist_free_icons(void);
@@ -68,12 +75,13 @@ 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_select(struct FileList* filelist, FileSelection* sel, FileSelType select, unsigned int flag);
+void                           filelist_select(struct FileList* filelist, FileSelection* sel, FileSelType select, unsigned int flag, FileCheckType check);
+void                           filelist_select_file(struct FileList* filelist, int index, FileSelType select, unsigned int flag, FileCheckType check);
+int                                    filelist_is_selected(struct FileList* filelist, int index, unsigned int flag, FileCheckType check);
 void                           filelist_hidedot(struct FileList* filelist, short hide);
 void                           filelist_setfilter(struct FileList* filelist, unsigned int filter);
 void                           filelist_setfilter_types(struct FileList* filelist, const char *filter_glob);
 void                           filelist_filter(struct FileList* filelist);
-void                           filelist_swapselect(struct FileList* filelist);
 void                           filelist_imgsize(struct FileList* filelist, short w, short h);
 struct ImBuf *         filelist_getimage(struct FileList* filelist, int index);
 struct ImBuf *         filelist_geticon(struct FileList* filelist, int index);