From e73a2020dd1450a709d8da53af9132e08c811226 Mon Sep 17 00:00:00 2001 From: Andrea Weikert Date: Sun, 20 Mar 2011 11:16:59 +0000 Subject: [PATCH] == filebrowser == Code cleanup * Separate out selection flags from file type flags, was bothering me since forever ;) * Also renamed ACTIVEFILE to SELECTED_FILE to better reflect actual usage in code. * Fix crash introduced with last commit, better check for valid selection --- source/blender/blenlib/BLI_storage_types.h | 3 +- source/blender/editors/space_file/file_draw.c | 14 +++---- source/blender/editors/space_file/file_ops.c | 38 ++++++++++--------- source/blender/editors/space_file/filelist.c | 16 ++++---- source/blender/editors/space_file/filesel.c | 2 +- .../blender/editors/space_file/space_file.c | 2 +- source/blender/makesdna/DNA_space_types.h | 12 ++++-- 7 files changed, 48 insertions(+), 39 deletions(-) diff --git a/source/blender/blenlib/BLI_storage_types.h b/source/blender/blenlib/BLI_storage_types.h index 0b8746b4c50..07c0ceffeb5 100644 --- a/source/blender/blenlib/BLI_storage_types.h +++ b/source/blender/blenlib/BLI_storage_types.h @@ -54,7 +54,7 @@ struct direntry{ #else struct stat s; #endif - unsigned int flags; + unsigned int flags; char size[16]; char mode1[4]; char mode2[4]; @@ -66,6 +66,7 @@ struct direntry{ void *poin; int nr; struct ImBuf *image; + unsigned int selflag; /* selection flag */ }; struct dirlink diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c index 68651674bd5..c8f4dc26e90 100644 --- a/source/blender/editors/space_file/file_draw.c +++ b/source/blender/editors/space_file/file_draw.c @@ -514,10 +514,10 @@ void file_draw_list(const bContext *C, ARegion *ar) UI_ThemeColor4(TH_TEXT); - if (!(file->flags & EDITING)) { - if ((params->active_file == i) || (file->flags & HILITED_FILE) || (file->flags & ACTIVEFILE) ) { - int colorid = (file->flags & ACTIVEFILE) ? TH_HILITE : TH_BACK; - int shade = (params->active_file == i) || (file->flags & HILITED_FILE) ? 20 : 0; + if (!(file->selflag & EDITING_FILE)) { + if ((params->active_file == i) || (file->selflag & HILITED_FILE) || (file->selflag & SELECTED_FILE) ) { + int colorid = (file->selflag & SELECTED_FILE) ? TH_HILITE : TH_BACK; + int shade = (params->active_file == i) || (file->selflag & HILITED_FILE) ? 20 : 0; draw_tile(sx, sy-1, layout->tile_w+4, sfile->layout->tile_h+layout->tile_border_y, colorid, shade); } } @@ -539,17 +539,17 @@ void file_draw_list(const bContext *C, ARegion *ar) UI_ThemeColor4(TH_TEXT); - if (file->flags & EDITING) { + if (file->selflag & EDITING_FILE) { uiBut *but = uiDefBut(block, TEX, 1, "", sx , sy-layout->tile_h-3, textwidth, textheight, sfile->params->renameedit, 1.0f, (float)sizeof(sfile->params->renameedit),0,0,""); uiButSetRenameFunc(but, renamebutton_cb, file); uiButSetFlag(but, UI_BUT_NO_UTF8); /* allow non utf8 names */ if ( 0 == uiButActiveOnly(C, block, but)) { - file->flags &= ~EDITING; + file->selflag &= ~EDITING_FILE; } } - if (!(file->flags & EDITING)) { + if (!(file->selflag & EDITING_FILE)) { int tpos = (FILE_IMGDISPLAY == params->display) ? sy - layout->tile_h + layout->textheight : sy; file_draw_string(sx+1, tpos, file->relname, textwidth, textheight, align); } diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c index f36bb6f030d..f0d4b2df8d3 100644 --- a/source/blender/editors/space_file/file_ops.c +++ b/source/blender/editors/space_file/file_ops.c @@ -96,8 +96,8 @@ static void file_deselect_all(SpaceFile* sfile, unsigned int flag) for ( i=0; i < numfiles; ++i) { struct direntry* file = filelist_file(sfile->files, i); - if (file && (file->flags & flag)) { - file->flags &= ~flag; + if (file && (file->selflag & flag)) { + file->selflag &= ~flag; } } } @@ -151,7 +151,7 @@ static FileSelection file_selection_get(bContext* C, const rcti* rect, short fil int f= sel.last; while (f >= 0) { struct direntry* file = filelist_file(sfile->files, f); - if (file->flags & ACTIVEFILE) + if (file->selflag & SELECTED_FILE) break; f--; } @@ -213,15 +213,19 @@ static FileSelect file_select(bContext* C, const rcti* rect, FileSelType select, struct direntry *file; /* flag the files as selected in the filelist */ - filelist_select(sfile->files, &sel, select, ACTIVEFILE); + filelist_select(sfile->files, &sel, select, SELECTED_FILE); /* Don't act on multiple selected files */ if (sel.first != sel.last) select = 0; - /* Check last selection, if selected, act on the file or dir */ - file = filelist_file(sfile->files, sel.last); - if (file->flags & ACTIVEFILE) { - retval = file_select_do(C, sel.last); + /* Do we have a valid selection and are we actually selecting */ + 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)) { + retval = file_select_do(C, sel.last); + } } /* update operator for name change event */ @@ -328,7 +332,7 @@ static int file_select_invoke(bContext *C, wmOperator *op, wmEvent *event) return OPERATOR_CANCELLED; /* single select, deselect all selected first */ - if (!extend) file_deselect_all(sfile, ACTIVEFILE); + if (!extend) file_deselect_all(sfile, SELECTED_FILE); ret = file_select(C, &rect, extend ? FILE_SEL_TOGGLE : FILE_SEL_ADD, fill); if (FILE_SELECT_DIR == ret) @@ -369,8 +373,8 @@ static int file_select_all_exec(bContext *C, wmOperator *UNUSED(op)) /* if any file is selected, deselect all first */ for ( i=0; i < numfiles; ++i) { struct direntry* file = filelist_file(sfile->files, i); - if (file && (file->flags & ACTIVEFILE)) { - file->flags &= ~ACTIVEFILE; + if (file && (file->selflag & SELECTED_FILE)) { + file->selflag &= ~SELECTED_FILE; select = 0; ED_area_tag_redraw(sa); } @@ -380,7 +384,7 @@ static int file_select_all_exec(bContext *C, wmOperator *UNUSED(op)) for ( i=0; i < numfiles; ++i) { struct direntry* file = filelist_file(sfile->files, i); if(file && !S_ISDIR(file->type)) { - file->flags |= ACTIVEFILE; + file->selflag |= SELECTED_FILE; ED_area_tag_redraw(sa); } } @@ -627,7 +631,7 @@ void file_sfile_to_operator(wmOperator *op, SpaceFile *sfile, char *filepath) if(RNA_struct_find_property(op->ptr, "files")) { for (i=0; ifiles, i); - if(file->flags & ACTIVEFILE) { + 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); @@ -639,7 +643,7 @@ void file_sfile_to_operator(wmOperator *op, SpaceFile *sfile, char *filepath) if(RNA_struct_find_property(op->ptr, "dirs")) { for (i=0; ifiles, i); - if(file->flags & ACTIVEFILE) { + if(file->selflag & SELECTED_FILE) { if ((file->type & S_IFDIR)) { RNA_collection_add(op->ptr, "dirs", &itemptr); RNA_string_set(&itemptr, "name", file->relname); @@ -728,7 +732,7 @@ int file_exec(bContext *C, wmOperator *exec_op) for (i=0; ifiles); i++) { file = filelist_file(sfile->files, i); - if(file->flags & ACTIVEFILE) { + if(file->selflag & SELECTED_FILE) { active=1; } } @@ -893,7 +897,7 @@ static int file_smoothscroll_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent for (i=0; i < numfiles; ++i) { struct direntry *file = filelist_file(sfile->files, i); - if (file->flags & EDITING) { + if (file->selflag & EDITING_FILE) { edit_idx=i; break; } @@ -1296,7 +1300,7 @@ static int file_rename_exec(bContext *C, wmOperator *UNUSED(op)) int numfiles = filelist_numfiles(sfile->files); if ( (0<=idx) && (idxfiles, idx); - file->flags |= EDITING; + file->selflag |= EDITING_FILE; BLI_strncpy(sfile->params->renameedit, file->relname, FILE_MAXFILE); sfile->params->renamefile[0]= '\0'; } diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c index 3c76b815a80..649c2dadc0c 100644 --- a/source/blender/editors/space_file/filelist.c +++ b/source/blender/editors/space_file/filelist.c @@ -914,15 +914,15 @@ void filelist_swapselect(struct FileList* filelist) file= filelist->filelist; for(num=0; numnumfiles; num++, file++) { - if(file->flags & ACTIVEFILE) { + if(file->selflag & SELECTED_FILE) { act= 1; break; } } file= filelist->filelist+2; for(num=2; numnumfiles; num++, file++) { - if(act) file->flags &= ~ACTIVEFILE; - else file->flags |= ACTIVEFILE; + if(act) file->selflag &= ~SELECTED_FILE; + else file->selflag |= SELECTED_FILE; } } @@ -936,13 +936,13 @@ void filelist_select(struct FileList* filelist, FileSelection* sel, FileSelType switch (select) { case FILE_SEL_REMOVE: - file->flags &= ~flag; + file->selflag &= ~flag; break; case FILE_SEL_ADD: - file->flags |= flag; + file->selflag |= flag; break; case FILE_SEL_TOGGLE: - file->flags ^= flag; + file->selflag ^= flag; break; } } @@ -1183,10 +1183,10 @@ void filelist_from_main(struct FileList *filelist) #if 0 // XXXXX TODO show the selection status of the objects if(!filelist->has_func) { /* F4 DATA BROWSE */ if(idcode==ID_OB) { - if( ((Object *)id)->flag & SELECT) files->flags |= ACTIVEFILE; + if( ((Object *)id)->flag & SELECT) files->selflag |= SELECTED_FILE; } else if(idcode==ID_SCE) { - if( ((Scene *)id)->r.scemode & R_BG_RENDER) files->flags |= ACTIVEFILE; + if( ((Scene *)id)->r.scemode & R_BG_RENDER) files->selflag |= SELECTED_FILE; } } #endif diff --git a/source/blender/editors/space_file/filesel.c b/source/blender/editors/space_file/filesel.c index 305598e7a0b..22f15b76dd3 100644 --- a/source/blender/editors/space_file/filesel.c +++ b/source/blender/editors/space_file/filesel.c @@ -568,7 +568,7 @@ int file_select_match(struct SpaceFile *sfile, const char *pattern) for (i = 0; i < n; i++) { file = filelist_file(sfile->files, i); if (fnmatch(pattern, file->relname, 0) == 0) { - file->flags |= ACTIVEFILE; + file->selflag |= SELECTED_FILE; match = 1; } } diff --git a/source/blender/editors/space_file/space_file.c b/source/blender/editors/space_file/space_file.c index d4240a9ad53..ec0d9806bca 100644 --- a/source/blender/editors/space_file/space_file.c +++ b/source/blender/editors/space_file/space_file.c @@ -236,7 +236,7 @@ static void file_refresh(const bContext *C, ScrArea *UNUSED(sa)) if (idx >= 0) { struct direntry *file= filelist_file(sfile->files, idx); if (file) { - file->flags |= EDITING; + file->selflag |= EDITING_FILE; } } BLI_strncpy(sfile->params->renameedit, sfile->params->renamefile, sizeof(sfile->params->renameedit)); diff --git a/source/blender/makesdna/DNA_space_types.h b/source/blender/makesdna/DNA_space_types.h index e6d1ed59f24..8ad9485ab15 100644 --- a/source/blender/makesdna/DNA_space_types.h +++ b/source/blender/makesdna/DNA_space_types.h @@ -713,9 +713,7 @@ enum FileSortTypeE { #define FILE_BOOKMARKS 512 #define FILE_GROUP_INSTANCE 1024 -/* files in filesel list: 2=ACTIVE */ -#define EDITING (1<<0) -#define ACTIVEFILE (1<<1) +/* files in filesel list: file types */ #define BLENDERFILE (1<<2) #define BLENDERFILE_BACKUP (1<<3) #define IMAGEFILE (1<<4) @@ -729,7 +727,13 @@ enum FileSortTypeE { #define BTXFILE (1<<12) #define COLLADAFILE (1<<13) #define OPERATORFILE (1<<14) /* from filter_glob operator property */ -#define HILITED_FILE (1<<15) + + +/* Selection Flags in filesel: struct direntry, unsigned char selflag */ +#define ACTIVE_FILE (1<<1) +#define HILITED_FILE (1<<2) +#define SELECTED_FILE (1<<3) +#define EDITING_FILE (1<<4) /* SpaceImage->dt_uv */ #define SI_UVDT_OUTLINE 0