file chooser

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@408 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
NishiOwO
2025-10-18 14:17:26 +00:00
parent eff6ef3fa0
commit 266c2f5a16
3 changed files with 112 additions and 1 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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,