diff --git a/include/Mw/Directory.h b/include/Mw/Directory.h index d724e9a..18beb77 100644 --- a/include/Mw/Directory.h +++ b/include/Mw/Directory.h @@ -45,6 +45,14 @@ MWDECL void MwDirectoryFreeEntry(MwDirectoryEntry* entry); */ MWDECL char* MwDirectoryCurrent(void); +/*! + * %brief Joins 2 paths + * %param a Path + * %param b Path + * %return Path + */ +MWDECL char* MwDirectoryJoin(char* a, char* b); + #ifdef __cplusplus } #endif diff --git a/src/directory.c b/src/directory.c index 76482bd..494ce2c 100644 --- a/src/directory.c +++ b/src/directory.c @@ -124,3 +124,85 @@ char* MwDirectoryCurrent(void) { return getcwd(NULL, 0); #endif } + +#ifdef _WIN32 +#define DIRSEP '\\' +#else +#define DIRSEP '/' +#endif +static void MwDirectoryJoinSingle(char* target, char* p) { + int i; + if(strcmp(p, ".") == 0) return; + + for(i = strlen(target) - 1; i >= 0; i--) { + if(target[i] == DIRSEP) { + target[i] = 0; + } else { + break; + } + } + + if(strcmp(p, "..") == 0) { + for(i = strlen(target) - 1; i >= 0; i--) { + if(target[i] != DIRSEP) { + target[i] = 0; + } else { + break; + } + } + } else { + char b[2]; + b[0] = DIRSEP; + b[1] = 0; + + strcat(target, b); + strcat(target, p); + } + + for(i = strlen(target) - 1; i >= 0; i--) { + if(target[i] == DIRSEP) { + target[i] = 0; + } else { + break; + } + } + + if(strchr(target, DIRSEP) == NULL) { + char b[2]; + b[0] = DIRSEP; + b[1] = 0; + + strcat(target, b); + } +} + +char* MwDirectoryJoin(char* a, char* b) { + char* p = malloc(strlen(a) + 1 + strlen(b) + 1); + char* bdup = MwStringDupliacte(b); + char* b2 = bdup; + int i; + + strcpy(p, a); + for(i = strlen(p) - 1; i >= 0; i--) { + if(p[i] == DIRSEP) { + p[i] = 0; + } else { + break; + } + } + while(b2 != NULL) { + char* current = b2; + + b2 = strchr(b2, DIRSEP); + if(b2 != NULL) { + b2[0] = 0; + } + + MwDirectoryJoinSingle(p, current); + + if(b2 != NULL) b2++; + } + free(bdup); + + return p; +} diff --git a/src/filechooser.c b/src/filechooser.c index 76cac42..d86f43f 100644 --- a/src/filechooser.c +++ b/src/filechooser.c @@ -75,7 +75,27 @@ static void files_activate(MwWidget handle, void* user, void* call) { (void)user; - scan(handle->parent, fc->sorted_entries[index - 1]->name); + if(fc->sorted_entries[index - 1]->type == MwDIRECTORY_DIRECTORY) { + char* p = MwDirectoryJoin(fc->path, fc->sorted_entries[index - 1]->name); + + scan(handle->parent, p); + + free(p); + } else { + okay(fc->okay, NULL, NULL); + } +} + +static void addr_up_activate(MwWidget handle, void* user, void* call) { + filechooser_t* fc = handle->parent->opaque; + char* p = MwDirectoryJoin(fc->path, ".."); + + (void)user; + (void)call; + + scan(handle->parent, p); + + free(p); } static void layout(MwWidget handle) { @@ -178,6 +198,7 @@ static void layout(MwWidget handle) { fc->addr_up = MwVaCreateWidget(MwButtonClass, "addr_up", handle, wx, wy, ww, wh, MwNpixmap, fc->up, NULL); + MwAddUserHandler(fc->addr_up, MwNactivateHandler, addr_up_activate, NULL); } else { MwVaApply(fc->addr_up, MwNx, wx,