diff options
author | Andrea Weikert <elubie@gmx.net> | 2009-03-12 22:36:59 +0300 |
---|---|---|
committer | Andrea Weikert <elubie@gmx.net> | 2009-03-12 22:36:59 +0300 |
commit | 432171fd99933f3aa4495bbc6e4b9a2c1df135a6 (patch) | |
tree | 30bb067788bdd7fd2e8c1aae45e3686a135bb887 /source/blender/editors/space_file | |
parent | 8420089d8bc5ca07c52c0984d4f0c6757b56d5b7 (diff) |
2.5 filebrowser
- better encapsulation for fsmenu to make it easier to remove global later.
- cleaned up fsmenu code to use just one global
- on file_exec current directory gets added to recent.
- save last 10 recent files in .Bfs too.
Diffstat (limited to 'source/blender/editors/space_file')
-rw-r--r-- | source/blender/editors/space_file/file_draw.c | 9 | ||||
-rw-r--r-- | source/blender/editors/space_file/file_ops.c | 14 | ||||
-rw-r--r-- | source/blender/editors/space_file/fsmenu.c | 144 | ||||
-rw-r--r-- | source/blender/editors/space_file/fsmenu.h | 29 | ||||
-rw-r--r-- | source/blender/editors/space_file/space_file.c | 4 |
5 files changed, 121 insertions, 79 deletions
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c index be777c72125..8ef5e069c76 100644 --- a/source/blender/editors/space_file/file_draw.c +++ b/source/blender/editors/space_file/file_draw.c @@ -546,8 +546,9 @@ void file_draw_list(const bContext *C, ARegion *ar) static void file_draw_fsmenu_category(const bContext *C, ARegion *ar, FSMenuCategory category, const char* category_name, short *starty) { SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C); + struct FSMenu* fsmenu = fsmenu_get(); char bookmark[FILE_MAX]; - int nentries = fsmenu_get_nentries(category); + int nentries = fsmenu_get_nentries(fsmenu, category); int linestep = gFontsize*2.0f; short sx, sy, xpos, ypos; int bmwidth = ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*TILE_BORDER_X - ICON_DEFAULT_WIDTH - 4; @@ -564,7 +565,7 @@ static void file_draw_fsmenu_category(const bContext *C, ARegion *ar, FSMenuCate sy -= linestep; for (i=0; i< nentries && (sy > ar->v2d.cur.ymin) ;++i) { - char *fname = fsmenu_get_entry(category, i); + char *fname = fsmenu_get_entry(fsmenu, category, i); if (fname) { int sl; @@ -575,7 +576,7 @@ static void file_draw_fsmenu_category(const bContext *C, ARegion *ar, FSMenuCate bookmark[sl] = '\0'; sl--; } - if (fsmenu_is_selected(category, i) ) { + if (fsmenu_is_selected(fsmenu, category, i) ) { UI_ThemeColor(TH_HILITE); /* uiSetRoundBox(15); * uiRoundBox(sx, sy - linestep, sx + bmwidth, sy, 4.0f); */ @@ -592,7 +593,7 @@ static void file_draw_fsmenu_category(const bContext *C, ARegion *ar, FSMenuCate xpos += ICON_DEFAULT_WIDTH + 4; file_draw_string(xpos, ypos, bookmark, bmwidth, fontsize, FILE_SHORTEN_FRONT); sy -= linestep; - fsmenu_set_pos(category, i, xpos, ypos); + fsmenu_set_pos(fsmenu, category, i, xpos, ypos); } } diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c index 4725014a4b1..20e6b9b16a8 100644 --- a/source/blender/editors/space_file/file_ops.c +++ b/source/blender/editors/space_file/file_ops.c @@ -318,7 +318,7 @@ void FILE_OT_select_all(wmOperatorType *ot) static void set_active_bookmark(FileSelectParams* params, struct ARegion* ar, short x, short y) { - int nentries = fsmenu_get_nentries(FS_CATEGORY_BOOKMARKS); + int nentries = fsmenu_get_nentries(fsmenu_get(), FS_CATEGORY_BOOKMARKS); float fx, fy; short posy; @@ -335,16 +335,17 @@ static void set_active_bookmark(FileSelectParams* params, struct ARegion* ar, sh static int file_select_bookmark_category(SpaceFile* sfile, ARegion* ar, short x, short y, FSMenuCategory category) { - int nentries = fsmenu_get_nentries(category); + struct FSMenu* fsmenu = fsmenu_get(); + int nentries = fsmenu_get_nentries(fsmenu, category); int linestep = U.fontsize*2.0f; short xs, ys; int i; int selected = -1; for (i=0; i < nentries; ++i) { - fsmenu_get_pos(category, i, &xs, &ys); + fsmenu_get_pos(fsmenu, category, i, &xs, &ys); if ( (y<=ys) && (y>ys-linestep) ) { - fsmenu_select_entry(category, i); + fsmenu_select_entry(fsmenu, category, i); selected = i; break; } @@ -373,7 +374,7 @@ static void file_select_bookmark(SpaceFile* sfile, ARegion* ar, short x, short y } if (selected>=0) { - entry= fsmenu_get_entry(category, selected); + entry= fsmenu_get_entry(fsmenu_get(), category, selected); /* which string */ if (entry) { FileSelectParams* params = sfile->params; @@ -523,6 +524,9 @@ int file_exec(bContext *C, wmOperator *unused) strcat(name, sfile->params->file); RNA_string_set(op->ptr, "filename", name); + fsmenu_insert_entry(fsmenu_get(), FS_CATEGORY_RECENT, sfile->params->dir,0, 1); + BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs"); + fsmenu_write_file(fsmenu_get(), name); WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC); } diff --git a/source/blender/editors/space_file/fsmenu.c b/source/blender/editors/space_file/fsmenu.c index 980c014ae9c..6ef946c9697 100644 --- a/source/blender/editors/space_file/fsmenu.c +++ b/source/blender/editors/space_file/fsmenu.c @@ -62,83 +62,97 @@ struct _FSMenuEntry { short xs, ys; }; -static FSMenuEntry *fsmenu_system= 0; -static FSMenuEntry *fsmenu_bookmarks= 0; -static FSMenuEntry *fsmenu_recent= 0; +typedef struct FSMenu +{ + FSMenuEntry *fsmenu_system; + FSMenuEntry *fsmenu_bookmarks; + FSMenuEntry *fsmenu_recent; + + FSMenuCategory selected_category; + int selected_entry; + +} FSMenu; + +static FSMenu *g_fsmenu = NULL; -static FSMenuCategory selected_category= FS_CATEGORY_SYSTEM; -static int selected_entry= 0; +struct FSMenu* fsmenu_get(void) +{ + if (!g_fsmenu) { + g_fsmenu=MEM_callocN(sizeof(struct FSMenu), "fsmenu"); + } + return g_fsmenu; +} -void fsmenu_select_entry(FSMenuCategory category, int index) +void fsmenu_select_entry(struct FSMenu* fsmenu, FSMenuCategory category, int index) { - selected_category = category; - selected_entry = index; + fsmenu->selected_category = category; + fsmenu->selected_entry = index; } -int fsmenu_is_selected(FSMenuCategory category, int index) +int fsmenu_is_selected(struct FSMenu* fsmenu, FSMenuCategory category, int index) { - return (category==selected_category) && (index==selected_entry); + return (category==fsmenu->selected_category) && (index==fsmenu->selected_entry); } -static FSMenuEntry *fsmenu_get(FSMenuCategory category) +static FSMenuEntry *fsmenu_get_category(struct FSMenu* fsmenu, FSMenuCategory category) { - FSMenuEntry *fsmenu = NULL; + FSMenuEntry *fsms = NULL; switch(category) { case FS_CATEGORY_SYSTEM: - fsmenu = fsmenu_system; + fsms = fsmenu->fsmenu_system; break; case FS_CATEGORY_BOOKMARKS: - fsmenu = fsmenu_bookmarks; + fsms = fsmenu->fsmenu_bookmarks; break; case FS_CATEGORY_RECENT: - fsmenu = fsmenu_recent; + fsms = fsmenu->fsmenu_recent; break; } - return fsmenu; + return fsms; } -static void fsmenu_set(FSMenuCategory category, FSMenuEntry *fsmenu) +static void fsmenu_set_category(struct FSMenu* fsmenu, FSMenuCategory category, FSMenuEntry *fsms) { switch(category) { case FS_CATEGORY_SYSTEM: - fsmenu_system = fsmenu; + fsmenu->fsmenu_system = fsms; break; case FS_CATEGORY_BOOKMARKS: - fsmenu_bookmarks = fsmenu; + fsmenu->fsmenu_bookmarks = fsms; break; case FS_CATEGORY_RECENT: - fsmenu_recent = fsmenu; + fsmenu->fsmenu_recent = fsms; break; } } -int fsmenu_get_nentries(FSMenuCategory category) +int fsmenu_get_nentries(struct FSMenu* fsmenu, FSMenuCategory category) { FSMenuEntry *fsme; int count= 0; - for (fsme= fsmenu_get(category); fsme; fsme= fsme->next) + for (fsme= fsmenu_get_category(fsmenu, category); fsme; fsme= fsme->next) count++; return count; } -char *fsmenu_get_entry(FSMenuCategory category, int idx) +char *fsmenu_get_entry(struct FSMenu* fsmenu, FSMenuCategory category, int idx) { FSMenuEntry *fsme; - for (fsme= fsmenu_get(category); fsme && idx; fsme= fsme->next) + for (fsme= fsmenu_get_category(fsmenu, category); fsme && idx; fsme= fsme->next) idx--; return fsme?fsme->path:NULL; } -void fsmenu_set_pos( FSMenuCategory category, int idx, short xs, short ys) +void fsmenu_set_pos(struct FSMenu* fsmenu, FSMenuCategory category, int idx, short xs, short ys) { FSMenuEntry *fsme; - for (fsme= fsmenu_get(category); fsme && idx; fsme= fsme->next) + for (fsme= fsmenu_get_category(fsmenu, category); fsme && idx; fsme= fsme->next) idx--; if (fsme) { @@ -147,11 +161,11 @@ void fsmenu_set_pos( FSMenuCategory category, int idx, short xs, short ys) } } -int fsmenu_get_pos (FSMenuCategory category, int idx, short* xs, short* ys) +int fsmenu_get_pos (struct FSMenu* fsmenu, FSMenuCategory category, int idx, short* xs, short* ys) { FSMenuEntry *fsme; - for (fsme= fsmenu_get(category); fsme && idx; fsme= fsme->next) + for (fsme= fsmenu_get_category(fsmenu, category); fsme && idx; fsme= fsme->next) idx--; if (fsme) { @@ -164,14 +178,14 @@ int fsmenu_get_pos (FSMenuCategory category, int idx, short* xs, short* ys) } -void fsmenu_insert_entry(FSMenuCategory category, char *path, int sorted, short save) +void fsmenu_insert_entry(struct FSMenu* fsmenu, FSMenuCategory category, char *path, int sorted, short save) { FSMenuEntry *prev; FSMenuEntry *fsme; - FSMenuEntry *fsmenu; + FSMenuEntry *fsms; - fsmenu = fsmenu_get(category); - prev= fsme= fsmenu; + fsms = fsmenu_get_category(fsmenu, category); + prev= fsme= fsms; for (; fsme; prev= fsme, fsme= fsme->next) { if (fsme->path) { @@ -198,17 +212,17 @@ void fsmenu_insert_entry(FSMenuCategory category, char *path, int sorted, short fsme->next= prev->next; prev->next= fsme; } else { - fsme->next= fsmenu; - fsmenu_set(category, fsme); + fsme->next= fsms; + fsmenu_set_category(fsmenu, category, fsme); } } -void fsmenu_remove_entry(FSMenuCategory category, int idx) +void fsmenu_remove_entry(struct FSMenu* fsmenu, FSMenuCategory category, int idx) { FSMenuEntry *prev= NULL, *fsme= NULL; - FSMenuEntry *fsmenu = fsmenu_get(category); + FSMenuEntry *fsms = fsmenu_get_category(fsmenu, category); - for (fsme= fsmenu; fsme && idx; prev= fsme, fsme= fsme->next) + for (fsme= fsms; fsme && idx; prev= fsme, fsme= fsme->next) idx--; if (fsme) { @@ -221,8 +235,8 @@ void fsmenu_remove_entry(FSMenuCategory category, int idx) if (prev) { prev->next= fsme->next; } else { - fsmenu= fsme->next; - fsmenu_set(category, fsmenu); + fsms= fsme->next; + fsmenu_set_category(fsmenu, category, fsms); } /* free entry */ MEM_freeN(fsme->path); @@ -231,14 +245,22 @@ void fsmenu_remove_entry(FSMenuCategory category, int idx) } } -void fsmenu_write_file(const char *filename) +void fsmenu_write_file(struct FSMenu* fsmenu, const char *filename) { FSMenuEntry *fsme= NULL; + int count=FSMENU_RECENT_MAX; FILE *fp = fopen(filename, "w"); if (!fp) return; - - for (fsme= fsmenu_get(FS_CATEGORY_BOOKMARKS); fsme; fsme= fsme->next) { + + fprintf(fp, "[Bookmarks]\n"); + for (fsme= fsmenu_get_category(fsmenu, FS_CATEGORY_BOOKMARKS); fsme; fsme= fsme->next) { + if (fsme->path && fsme->save) { + fprintf(fp, "%s\n", fsme->path); + } + } + fprintf(fp, "[Recent]\n"); + for (fsme= fsmenu_get_category(fsmenu, FS_CATEGORY_RECENT); fsme && count; fsme= fsme->next, --count) { if (fsme->path && fsme->save) { fprintf(fp, "%s\n", fsme->path); } @@ -246,9 +268,10 @@ void fsmenu_write_file(const char *filename) fclose(fp); } -void fsmenu_read_file(const char *filename) +void fsmenu_read_file(struct FSMenu* fsmenu, const char *filename) { char line[256]; + FSMenuCategory category = FS_CATEGORY_BOOKMARKS; FILE *fp; #ifdef WIN32 @@ -268,15 +291,15 @@ void fsmenu_read_file(const char *filename) tmps[2]='\\'; tmps[3]=0; - fsmenu_insert_entry(FS_CATEGORY_SYSTEM, tmps, 1, 0); + fsmenu_insert_entry(fsmenu, FS_CATEGORY_SYSTEM, tmps, 1, 0); } } /* Adding Desktop and My Documents */ SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0); - fsmenu_insert_entry(FS_CATEGORY_BOOKMARKS, folder, 1, 0); + fsmenu_insert_entry(fsmenu,FS_CATEGORY_BOOKMARKS, folder, 1, 0); SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0); - fsmenu_insert_entry(FS_CATEGORY_BOOKMARKS, folder, 1, 0); + fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, folder, 1, 0); } #endif @@ -285,20 +308,26 @@ void fsmenu_read_file(const char *filename) while ( fgets ( line, 256, fp ) != NULL ) /* read a line */ { - int len = strlen(line); - if (len>0) { - if (line[len-1] == '\n') { - line[len-1] = '\0'; + if (strncmp(line, "[Bookmarks]", 11)==0){ + category = FS_CATEGORY_BOOKMARKS; + } else if (strncmp(line, "[Recent]", 8)==0){ + category = FS_CATEGORY_RECENT; + } else { + int len = strlen(line); + if (len>0) { + if (line[len-1] == '\n') { + line[len-1] = '\0'; + } + fsmenu_insert_entry(fsmenu, category, line, 0, 1); } - fsmenu_insert_entry(FS_CATEGORY_BOOKMARKS, line, 0, 1); } } fclose(fp); } -static void fsmenu_free_category(FSMenuCategory category) +static void fsmenu_free_category(struct FSMenu* fsmenu, FSMenuCategory category) { - FSMenuEntry *fsme= fsmenu_get(category); + FSMenuEntry *fsme= fsmenu_get_category(fsmenu, category); while (fsme) { FSMenuEntry *n= fsme->next; @@ -310,10 +339,11 @@ static void fsmenu_free_category(FSMenuCategory category) } } -void fsmenu_free(void) +void fsmenu_free(struct FSMenu* fsmenu) { - fsmenu_free_category(FS_CATEGORY_SYSTEM); - fsmenu_free_category(FS_CATEGORY_BOOKMARKS); - fsmenu_free_category(FS_CATEGORY_RECENT); + fsmenu_free_category(fsmenu, FS_CATEGORY_SYSTEM); + fsmenu_free_category(fsmenu, FS_CATEGORY_BOOKMARKS); + fsmenu_free_category(fsmenu, FS_CATEGORY_RECENT); + MEM_freeN(fsmenu); } diff --git a/source/blender/editors/space_file/fsmenu.h b/source/blender/editors/space_file/fsmenu.h index d3ec17e9ffd..c51c45b7dc4 100644 --- a/source/blender/editors/space_file/fsmenu.h +++ b/source/blender/editors/space_file/fsmenu.h @@ -31,47 +31,54 @@ #ifndef BSE_FSMENU_H #define BSE_FSMENU_H +/* XXX could become UserPref */ +#define FSMENU_RECENT_MAX 10 + typedef enum FSMenuCategory { FS_CATEGORY_SYSTEM, FS_CATEGORY_BOOKMARKS, FS_CATEGORY_RECENT } FSMenuCategory; +struct FSMenu; + +struct FSMenu* fsmenu_get (void); + /** Returns the number of entries in the Fileselect Menu */ -int fsmenu_get_nentries (FSMenuCategory category); +int fsmenu_get_nentries (struct FSMenu* fsmenu, FSMenuCategory category); /** Returns the fsmenu entry at @a index (or NULL if a bad index) * or a separator. */ -char* fsmenu_get_entry (FSMenuCategory category, int index); +char* fsmenu_get_entry (struct FSMenu* fsmenu, FSMenuCategory category, int index); -void fsmenu_select_entry (FSMenuCategory category, int index); +void fsmenu_select_entry (struct FSMenu* fsmenu, FSMenuCategory category, int index); -int fsmenu_is_selected (FSMenuCategory category, int index); +int fsmenu_is_selected (struct FSMenu* fsmenu, FSMenuCategory category, int index); /** Sets the position of the fsmenu entry at @a index */ -void fsmenu_set_pos (FSMenuCategory category, int index, short xs, short ys); +void fsmenu_set_pos (struct FSMenu* fsmenu, FSMenuCategory category, int index, short xs, short ys); /** Returns the position of the fsmenu entry at @a index. return value is 1 if successful, 0 otherwise */ -int fsmenu_get_pos (FSMenuCategory category, int index, short* xs, short* ys); +int fsmenu_get_pos (struct FSMenu* fsmenu, FSMenuCategory category, int index, short* xs, short* ys); /** Inserts a new fsmenu entry with the given @a path. * Duplicate entries are not added. * @param sorted Should entry be inserted in sorted order? */ -void fsmenu_insert_entry (FSMenuCategory category, char *path, int sorted, short save); +void fsmenu_insert_entry (struct FSMenu* fsmenu, FSMenuCategory category, char *path, int sorted, short save); /** Removes the fsmenu entry at the given @a index. */ -void fsmenu_remove_entry (FSMenuCategory category, int index); +void fsmenu_remove_entry (struct FSMenu* fsmenu, FSMenuCategory category, int index); /** saves the 'bookmarks' to the specified file */ -void fsmenu_write_file(const char *filename); +void fsmenu_write_file (struct FSMenu* fsmenu, const char *filename); /** reads the 'bookmarks' from the specified file */ -void fsmenu_read_file(const char *filename); +void fsmenu_read_file (struct FSMenu* fsmenu, const char *filename); /** Free's all the memory associated with the fsmenu */ -void fsmenu_free (void); +void fsmenu_free (struct FSMenu* fsmenu); #endif diff --git a/source/blender/editors/space_file/space_file.c b/source/blender/editors/space_file/space_file.c index d68cde85554..b19245cba06 100644 --- a/source/blender/editors/space_file/space_file.c +++ b/source/blender/editors/space_file/space_file.c @@ -431,13 +431,13 @@ void ED_file_init(void) { char name[FILE_MAX]; BLI_make_file_string("/", name, BLI_gethome(), ".Bfs"); - fsmenu_read_file(name); + fsmenu_read_file(fsmenu_get(), name); filelist_init_icons(); IMB_thumb_makedirs(); } void ED_file_exit(void) { - fsmenu_free(); + fsmenu_free(fsmenu_get()); filelist_free_icons(); } |