|
|
1.1 root 1: /*
2: Hatari - dlgFileSelect.c
3:
4: This file is distributed under the GNU Public License, version 2 or at
5: your option any later version. Read the file gpl.txt for details.
6:
7: A file selection dialog for the graphical user interface for Hatari.
8: */
1.1.1.7 root 9: const char DlgFileSelect_fileid[] = "Hatari dlgFileSelect.c : " __DATE__ " " __TIME__;
1.1 root 10:
11: #include <SDL.h>
12: #include <sys/stat.h>
13: #include <unistd.h>
14:
15: #include "main.h"
1.1.1.3 root 16: #include "scandir.h"
1.1 root 17: #include "sdlgui.h"
18: #include "file.h"
1.1.1.8 root 19: #include "paths.h"
1.1 root 20: #include "zip.h"
21:
22:
1.1.1.2 root 23: #define SGFS_NUMENTRIES 16 /* How many entries are displayed at once */
24:
25:
1.1.1.9 root 26: #define SGFSDLG_FILENAME 5
27: #define SGFSDLG_UPDIR 6
28: #define SGFSDLG_HOMEDIR 7
29: #define SGFSDLG_ROOTDIR 8
30: #define SGFSDLG_ENTRYFIRST 11
31: #define SGFSDLG_ENTRYLAST 26
32: #define SGFSDLG_SCROLLBAR 27
33: #define SGFSDLG_UP 28
34: #define SGFSDLG_DOWN 29
35: #define SGFSDLG_SHOWHIDDEN 30
36: #define SGFSDLG_OKAY 31
37: #define SGFSDLG_CANCEL 32
1.1 root 38:
1.1.1.9 root 39: #define SCROLLOUT_ABOVE 1
40: #define SCROLLOUT_UNDER 2
1.1 root 41:
42: #define DLGPATH_SIZE 62
43: static char dlgpath[DLGPATH_SIZE+1]; /* Path name in the dialog */
44:
45: #define DLGFNAME_SIZE 56
46: static char dlgfname[DLGFNAME_SIZE+1]; /* Name of the selected file in the dialog */
47:
48: #define DLGFILENAMES_SIZE 59
1.1.1.2 root 49: static char dlgfilenames[SGFS_NUMENTRIES][DLGFILENAMES_SIZE+1]; /* Visible file names in the dialog */
1.1 root 50:
51: /* The dialog data: */
52: static SGOBJ fsdlg[] =
53: {
54: { SGBOX, 0, 0, 0,0, 64,25, NULL },
55: { SGTEXT, 0, 0, 25,1, 13,1, "Choose a file" },
56: { SGTEXT, 0, 0, 1,2, 7,1, "Folder:" },
57: { SGTEXT, 0, 0, 1,3, DLGPATH_SIZE,1, dlgpath },
58: { SGTEXT, 0, 0, 1,4, 6,1, "File:" },
59: { SGTEXT, 0, 0, 7,4, DLGFNAME_SIZE,1, dlgfname },
1.1.1.3 root 60: { SGBUTTON, 0, 0, 51,1, 4,1, ".." },
61: { SGBUTTON, 0, 0, 56,1, 3,1, "~" },
1.1 root 62: { SGBUTTON, 0, 0, 60,1, 3,1, "/" },
63: { SGBOX, 0, 0, 1,6, 62,16, NULL },
64: { SGBOX, 0, 0, 62,7, 1,14, NULL },
65: { SGTEXT, SG_EXIT, 0, 2,6, DLGFILENAMES_SIZE,1, dlgfilenames[0] },
66: { SGTEXT, SG_EXIT, 0, 2,7, DLGFILENAMES_SIZE,1, dlgfilenames[1] },
67: { SGTEXT, SG_EXIT, 0, 2,8, DLGFILENAMES_SIZE,1, dlgfilenames[2] },
68: { SGTEXT, SG_EXIT, 0, 2,9, DLGFILENAMES_SIZE,1, dlgfilenames[3] },
69: { SGTEXT, SG_EXIT, 0, 2,10, DLGFILENAMES_SIZE,1, dlgfilenames[4] },
70: { SGTEXT, SG_EXIT, 0, 2,11, DLGFILENAMES_SIZE,1, dlgfilenames[5] },
71: { SGTEXT, SG_EXIT, 0, 2,12, DLGFILENAMES_SIZE,1, dlgfilenames[6] },
72: { SGTEXT, SG_EXIT, 0, 2,13, DLGFILENAMES_SIZE,1, dlgfilenames[7] },
73: { SGTEXT, SG_EXIT, 0, 2,14, DLGFILENAMES_SIZE,1, dlgfilenames[8] },
74: { SGTEXT, SG_EXIT, 0, 2,15, DLGFILENAMES_SIZE,1, dlgfilenames[9] },
75: { SGTEXT, SG_EXIT, 0, 2,16, DLGFILENAMES_SIZE,1, dlgfilenames[10] },
76: { SGTEXT, SG_EXIT, 0, 2,17, DLGFILENAMES_SIZE,1, dlgfilenames[11] },
77: { SGTEXT, SG_EXIT, 0, 2,18, DLGFILENAMES_SIZE,1, dlgfilenames[12] },
78: { SGTEXT, SG_EXIT, 0, 2,19, DLGFILENAMES_SIZE,1, dlgfilenames[13] },
79: { SGTEXT, SG_EXIT, 0, 2,20, DLGFILENAMES_SIZE,1, dlgfilenames[14] },
80: { SGTEXT, SG_EXIT, 0, 2,21, DLGFILENAMES_SIZE,1, dlgfilenames[15] },
1.1.1.9 root 81: { SGSCROLLBAR, SG_TOUCHEXIT, 0, 62, 7, 0, 0, NULL }, /* Scrollbar */
82: { SGBUTTON, SG_TOUCHEXIT, 0, 62, 6,1,1, "\x01" }, /* Arrow up */
83: { SGBUTTON, SG_TOUCHEXIT, 0, 62,21,1,1, "\x02" }, /* Arrow down */
1.1.1.5 root 84: { SGCHECKBOX, SG_EXIT, 0, 2,23, 18,1, "Show hidden files" },
1.1.1.4 root 85: { SGBUTTON, SG_DEFAULT, 0, 32,23, 8,1, "Okay" },
86: { SGBUTTON, SG_CANCEL, 0, 50,23, 8,1, "Cancel" },
1.1 root 87: { -1, 0, 0, 0,0, 0,0, NULL }
88: };
89:
90:
1.1.1.2 root 91: static int ypos; /* First entry number to be displayed */
1.1.1.5 root 92: static bool refreshentries; /* Do we have to update the file names in the dialog? */
1.1.1.2 root 93: static int entries; /* How many files are in the actual directory? */
1.1.1.9 root 94: static int oldMouseY = 0; /* Keep the latest Y mouse position for scrollbar move computing */
95: static int mouseClicked = 0; /* used to know if mouse if down for the first time or not */
96: static int mouseIsOut = 0; /* used to keep info that mouse if above or under the scrollbar when mousebutton is down */
97: static float scrollbar_Ypos = 0.0; /* scrollbar heigth */
98:
99: /* Convert file position (in file list) to scrollbar y position */
100: static void DlgFileSelect_Convert_ypos_to_scrollbar_Ypos(void);
101:
1.1.1.2 root 102:
1.1 root 103:
104: /*-----------------------------------------------------------------------*/
1.1.1.8 root 105: /**
106: * Update the file name strings in the dialog.
107: * Returns false if it failed, true on success.
108: */
1.1.1.5 root 109: static int DlgFileSelect_RefreshEntries(struct dirent **files, char *path, bool browsingzip)
1.1 root 110: {
111: int i;
112: char *tempstr = malloc(FILENAME_MAX);
113:
114: if (!tempstr)
115: {
116: perror("DlgFileSelect_RefreshEntries");
1.1.1.7 root 117: return false;
1.1 root 118: }
119:
120: /* Copy entries to dialog: */
1.1.1.4 root 121: for (i=0; i<SGFS_NUMENTRIES; i++)
1.1 root 122: {
1.1.1.4 root 123: if (i+ypos < entries)
1.1 root 124: {
125: struct stat filestat;
126: /* Prepare entries: */
127: strcpy(tempstr, " ");
128: strcat(tempstr, files[i+ypos]->d_name);
129: File_ShrinkName(dlgfilenames[i], tempstr, DLGFILENAMES_SIZE);
130: /* Mark folders: */
131: strcpy(tempstr, path);
132: strcat(tempstr, files[i+ypos]->d_name);
133:
1.1.1.4 root 134: if (browsingzip)
1.1 root 135: {
1.1.1.3 root 136: if (File_DoesFileNameEndWithSlash(tempstr))
1.1 root 137: dlgfilenames[i][0] = SGFOLDER; /* Mark folders */
138: }
139: else
140: {
141: if( stat(tempstr, &filestat)==0 && S_ISDIR(filestat.st_mode) )
142: dlgfilenames[i][0] = SGFOLDER; /* Mark folders */
1.1.1.7 root 143: if (ZIP_FileNameIsZIP(tempstr) && browsingzip == false)
1.1 root 144: dlgfilenames[i][0] = SGFOLDER; /* Mark .ZIP archives as folders */
145: }
146: }
147: else
148: dlgfilenames[i][0] = 0; /* Clear entry */
149: }
150:
151: free(tempstr);
1.1.1.7 root 152: return true;
1.1 root 153: }
154:
155:
156: /*-----------------------------------------------------------------------*/
1.1.1.8 root 157: /**
158: * Remove all hidden files (files with file names that begin with a dot) from
159: * the list.
160: */
1.1.1.3 root 161: static void DlgFileSelect_RemoveHiddenFiles(struct dirent **files)
162: {
163: int i;
164: int nActPos = -1;
165: int nOldEntries;
166:
167: nOldEntries = entries;
168:
169: /* Scan list for hidden files and remove them. */
170: for (i = 0; i < nOldEntries; i++)
171: {
172: /* Does file name start with a dot? -> hidden file! */
173: if (files[i]->d_name[0] == '.')
174: {
175: if (nActPos == -1)
176: nActPos = i;
177: /* Remove file from list: */
178: free(files[i]);
179: files[i] = NULL;
180: entries -= 1;
181: }
182: }
183:
184: /* Now close the gaps in the list: */
185: if (nActPos != -1)
186: {
187: for (i = nActPos; i < nOldEntries; i++)
188: {
189: if (files[i] != NULL)
190: {
191: /* Move entry to earlier position: */
192: files[nActPos] = files[i];
193: files[i] = NULL;
194: nActPos += 1;
195: }
196: }
197: }
198: }
199:
200:
201: /*-----------------------------------------------------------------------*/
1.1.1.8 root 202: /**
203: * Prepare to scroll up one entry.
204: */
1.1.1.2 root 205: static void DlgFileSelect_ScrollUp(void)
206: {
207: if (ypos > 0)
208: {
209: --ypos;
1.1.1.9 root 210: DlgFileSelect_Convert_ypos_to_scrollbar_Ypos();
1.1.1.7 root 211: refreshentries = true;
1.1.1.2 root 212: }
213: }
214:
215:
216: /*-----------------------------------------------------------------------*/
1.1.1.8 root 217: /**
218: * Prepare to scroll down one entry.
219: */
1.1.1.2 root 220: static void DlgFileSelect_ScrollDown(void)
221: {
222: if (ypos+SGFS_NUMENTRIES < entries)
223: {
224: ++ypos;
1.1.1.9 root 225: DlgFileSelect_Convert_ypos_to_scrollbar_Ypos();
1.1.1.7 root 226: refreshentries = true;
1.1.1.2 root 227: }
228: }
229:
1.1.1.9 root 230: /*-----------------------------------------------------------------------*/
231: /**
232: * Manage the scrollbar up or down.
233: */
234: static void DlgFileSelect_ManageScrollbar(void)
235: {
236: int b, x, y;
237: int scrollY, scrollYmin, scrollYmax, scrollH_half;
238: float scrollMove;
239:
240: b = SDL_GetMouseState(&x, &y);
241:
242: /* If mouse is down on the scrollbar for the first time */
243: if (fsdlg[SGFSDLG_SCROLLBAR].state & SG_MOUSEDOWN) {
244: if (mouseClicked == 0) {
245: mouseClicked = 1;
246: mouseIsOut = 0;
247: oldMouseY = y;
248: }
249: }
250: /* Mouse button is up on the scrollbar */
251: else {
252: mouseClicked = 0;
253: oldMouseY = y;
254: mouseIsOut = 0;
255: }
256:
257: /* If mouse Y position didn't change */
258: if (oldMouseY == y)
259: return;
260:
261: /* Compute scrollbar ymin and ymax values */
262:
263: scrollYmin = (fsdlg[SGFSDLG_SCROLLBAR].y + fsdlg[0].y) * sdlgui_fontheight;
264: scrollYmax = (fsdlg[SGFSDLG_DOWN].y + fsdlg[0].y) * sdlgui_fontheight;
265:
266: scrollY = fsdlg[SGFSDLG_SCROLLBAR].y * sdlgui_fontheight + fsdlg[SGFSDLG_SCROLLBAR].h + fsdlg[0].y * sdlgui_fontheight;
267: scrollH_half = scrollY + fsdlg[SGFSDLG_SCROLLBAR].w / 2;
268: scrollMove = (float)(y-oldMouseY)/sdlgui_fontheight;
269:
270: /* Verify if mouse is not above the scrollbar area */
271: if (y < scrollYmin) {
272: mouseIsOut = SCROLLOUT_ABOVE;
273: oldMouseY = y;
274: return;
275: }
276: if (mouseIsOut == SCROLLOUT_ABOVE && y < scrollH_half) {
277: oldMouseY = y;
278: return;
279: }
280:
281: /* Verify if mouse is not under the scrollbar area */
282: if (y > scrollYmax) {
283: mouseIsOut = SCROLLOUT_UNDER;
284: oldMouseY = y;
285: return;
286: }
287: if (mouseIsOut == SCROLLOUT_UNDER && y > scrollH_half) {
288: oldMouseY = y;
289: return;
290: }
291:
292: mouseIsOut = 0;
293:
294: scrollbar_Ypos += scrollMove;
295: oldMouseY = y;
296:
297: /* Verifiy if scrollbar is in correct inferior boundary */
298: if (scrollbar_Ypos < 0)
299: scrollbar_Ypos = 0.0;
300:
301: /* Verifiy if scrollbar is in correct superior boundary */
302: b = (int) (scrollbar_Ypos * ((float)entries/(float)(SGFS_NUMENTRIES-2)) + 0.5);
303: if (b+SGFS_NUMENTRIES >= entries) {
304: ypos = entries - SGFS_NUMENTRIES;
305: DlgFileSelect_Convert_ypos_to_scrollbar_Ypos();
306: }
307:
308: refreshentries = true;
309: }
1.1.1.2 root 310:
311: /*-----------------------------------------------------------------------*/
1.1.1.8 root 312: /**
313: * Handle SDL events.
314: */
1.1.1.2 root 315: static void DlgFileSelect_HandleSdlEvents(SDL_Event *pEvent)
316: {
1.1.1.4 root 317: int oldypos = ypos;
1.1.1.2 root 318: switch (pEvent->type)
319: {
320: case SDL_MOUSEBUTTONDOWN:
321: if (pEvent->button.button == SDL_BUTTON_WHEELUP)
322: DlgFileSelect_ScrollUp();
323: else if (pEvent->button.button == SDL_BUTTON_WHEELDOWN)
324: DlgFileSelect_ScrollDown();
325: break;
1.1.1.9 root 326: case SDL_KEYDOWN:
1.1.1.2 root 327: switch (pEvent->key.keysym.sym)
328: {
1.1.1.9 root 329: case SDLK_UP:
330: DlgFileSelect_ScrollUp();
331: break;
332: case SDLK_DOWN:
333: DlgFileSelect_ScrollDown();
334: break;
335: case SDLK_HOME:
336: ypos = 0;
337: DlgFileSelect_Convert_ypos_to_scrollbar_Ypos();
338: break;
339: case SDLK_END:
340: ypos = entries-SGFS_NUMENTRIES;
341: DlgFileSelect_Convert_ypos_to_scrollbar_Ypos();
342: break;
343: case SDLK_PAGEUP:
344: ypos -= SGFS_NUMENTRIES;
345: DlgFileSelect_Convert_ypos_to_scrollbar_Ypos();
346: break;
1.1.1.2 root 347: case SDLK_PAGEDOWN:
348: if (ypos+2*SGFS_NUMENTRIES < entries)
349: ypos += SGFS_NUMENTRIES;
350: else
351: ypos = entries-SGFS_NUMENTRIES;
1.1.1.9 root 352: DlgFileSelect_Convert_ypos_to_scrollbar_Ypos();
1.1.1.2 root 353: break;
1.1.1.4 root 354: default:
355: break;
1.1.1.2 root 356: }
357: break;
1.1.1.4 root 358: default:
359: break;
360: }
1.1.1.9 root 361:
362: if (ypos < 0) {
1.1.1.4 root 363: ypos = 0;
1.1.1.9 root 364: scrollbar_Ypos = 0.0;
365: }
366:
1.1.1.4 root 367: if (ypos != oldypos)
1.1.1.7 root 368: refreshentries = true;
1.1.1.4 root 369: }
370:
371:
372: /*-----------------------------------------------------------------------*/
1.1.1.8 root 373: /**
374: * Free file entries
375: */
1.1.1.4 root 376: static struct dirent **files_free(struct dirent **files)
377: {
378: int i;
379: if (files != NULL)
380: {
381: for(i=0; i<entries; i++)
382: {
383: free(files[i]);
384: }
385: free(files);
386: }
387: return NULL;
388: }
389:
390:
391: /*-----------------------------------------------------------------------*/
1.1.1.8 root 392: /**
393: * Copy to dst src+add if they are below maxlen and return true,
394: * otherwise return false
395: */
1.1.1.4 root 396: static int strcat_maxlen(char *dst, int maxlen, const char *src, const char *add)
397: {
398: int slen, alen;
399: slen = strlen(src);
400: alen = strlen(add);
401: if (slen + alen < maxlen)
402: {
403: strcpy(dst, src);
404: strcpy(dst+slen, add);
405: return 1;
406: }
407: return 0;
408: }
409:
410: /*-----------------------------------------------------------------------*/
1.1.1.8 root 411: /**
412: * Create and return suitable path into zip file
413: */
1.1.1.4 root 414: static char* zip_get_path(const char *zipdir, const char *zipfilename, int browsingzip)
415: {
416: if (browsingzip)
417: {
418: char *zippath;
419: zippath = malloc(strlen(zipdir) + strlen(zipfilename) + 1);
420: strcpy(zippath, zipdir);
421: strcat(zippath, zipfilename);
422: return zippath;
1.1.1.2 root 423: }
1.1.1.4 root 424: return strdup("");
1.1.1.2 root 425: }
426:
1.1.1.8 root 427: /**
428: * string for zip root needs to be empty, check and correct if needed
429: */
1.1.1.4 root 430: static void correct_zip_root(char *zippath)
431: {
432: if (zippath[0] == PATHSEP && !zippath[1])
433: {
434: zippath[0] = '\0';
435: }
436: }
1.1.1.2 root 437:
1.1.1.9 root 438: /**
439: * Convert Ypos to Y scrollbar position
440: */
441: static void DlgFileSelect_Convert_ypos_to_scrollbar_Ypos(void)
442: {
443: if (entries <= SGFS_NUMENTRIES)
444: scrollbar_Ypos = 0.0;
445: else
446: scrollbar_Ypos = (float)ypos / ((float)entries/(float)(SGFS_NUMENTRIES-2));
447: }
448:
1.1.1.2 root 449: /*-----------------------------------------------------------------------*/
1.1.1.8 root 450: /**
451: * Show and process a file selection dialog.
452: * Returns path/name user selected or NULL if user canceled
453: * input: zip_path = pointer's pointer to buffer to contain file path
454: * within a selected zip file, or NULL if browsing zip files is disallowed.
455: * bAllowNew: true if the user is allowed to insert new file names.
456: */
1.1.1.5 root 457: char* SDLGui_FileSelect(const char *path_and_name, char **zip_path, bool bAllowNew)
1.1 root 458: {
459: struct dirent **files = NULL;
460: char *pStringMem;
1.1.1.10! root 461: char *retpath = NULL;
1.1.1.8 root 462: const char *home;
463: char *path, *fname; /* The actual file and path names */
1.1.1.7 root 464: bool reloaddir = true; /* Do we have to reload the directory file list? */
1.1 root 465: int retbut;
1.1.1.10! root 466: bool bOldMouseVisibility;
1.1.1.9 root 467: int selection; /* The selection index */
1.1 root 468: char *zipfilename; /* Filename in zip file */
469: char *zipdir;
1.1.1.7 root 470: bool browsingzip = false; /* Are we browsing an archive? */
1.1 root 471: zip_dir *zipfiles = NULL;
1.1.1.2 root 472: SDL_Event sdlEvent;
1.1.1.9 root 473: int yScrolbar_size; /* Size of the vertical scrollbar */
1.1.1.2 root 474:
475: ypos = 0;
1.1.1.9 root 476: scrollbar_Ypos = 0.0;
1.1.1.7 root 477: refreshentries = true;
1.1.1.2 root 478: entries = 0;
1.1 root 479:
480: /* Allocate memory for the file and path name strings: */
481: pStringMem = malloc(4 * FILENAME_MAX);
482: path = pStringMem;
483: fname = pStringMem + FILENAME_MAX;
1.1.1.4 root 484: zipdir = pStringMem + 2 * FILENAME_MAX;
485: zipfilename = pStringMem + 3 * FILENAME_MAX;
1.1 root 486: zipfilename[0] = 0;
1.1.1.5 root 487: fname[0] = 0;
488: path[0] = 0;
1.1 root 489:
1.1.1.10! root 490: /* Save mouse state and enable cursor */
! 491: bOldMouseVisibility = SDL_ShowCursor(SDL_QUERY);
! 492: SDL_ShowCursor(SDL_ENABLE);
! 493:
1.1 root 494: SDLGui_CenterDlg(fsdlg);
495: if (bAllowNew)
496: {
497: fsdlg[SGFSDLG_FILENAME].type = SGEDITFIELD;
498: fsdlg[SGFSDLG_FILENAME].flags |= SG_EXIT;
499: }
500: else
501: {
502: fsdlg[SGFSDLG_FILENAME].type = SGTEXT;
503: fsdlg[SGFSDLG_FILENAME].flags &= ~SG_EXIT;
504: }
505:
506: /* Prepare the path and filename variables */
1.1.1.4 root 507: if (path_and_name && path_and_name[0])
1.1.1.2 root 508: {
1.1.1.4 root 509: strncpy(path, path_and_name, FILENAME_MAX);
510: path[FILENAME_MAX-1] = '\0';
511: }
1.1.1.5 root 512: if (!File_DirExists(path))
1.1.1.4 root 513: {
1.1.1.5 root 514: File_SplitPath(path, path, fname, NULL);
515: if (!(File_DirExists(path) || getcwd(path, FILENAME_MAX)))
1.1.1.4 root 516: {
1.1.1.5 root 517: perror("SDLGui_FileSelect: non-existing path and CWD failed");
1.1.1.10! root 518: goto clean_exit;
1.1.1.4 root 519: }
1.1.1.2 root 520: }
1.1.1.5 root 521:
1.1.1.2 root 522: File_MakeAbsoluteName(path);
523: File_MakeValidPathName(path);
1.1 root 524: File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
525: File_ShrinkName(dlgfname, fname, DLGFNAME_SIZE);
526:
527: do
528: {
529: if (reloaddir)
530: {
1.1.1.4 root 531: files = files_free(files);
1.1 root 532:
533: if (browsingzip)
534: {
535: files = ZIP_GetFilesDir(zipfiles, zipdir, &entries);
1.1.1.8 root 536: if(!files)
537: {
538: fprintf(stderr, "SDLGui_FileSelect: ZIP_GetFilesDir error!\n");
1.1.1.10! root 539: goto clean_exit;
1.1.1.8 root 540: }
1.1 root 541: }
542: else
543: {
544: /* Load directory entries: */
545: entries = scandir(path, &files, 0, alphasort);
546: }
1.1.1.3 root 547:
548: /* Remove hidden files from the list if necessary: */
549: if (!(fsdlg[SGFSDLG_SHOWHIDDEN].state & SG_SELECTED))
550: {
551: DlgFileSelect_RemoveHiddenFiles(files);
552: }
1.1 root 553:
554: if (entries < 0)
555: {
556: fprintf(stderr, "SDLGui_FileSelect: Path not found.\n");
1.1.1.10! root 557: goto clean_exit;
1.1 root 558: }
559:
1.1.1.4 root 560: /* reload always implies refresh */
1.1.1.7 root 561: reloaddir = false;
562: refreshentries = true;
1.1 root 563: }/* reloaddir */
564:
1.1.1.9 root 565: /* Refresh scrollbar size */
566: if (entries <= SGFS_NUMENTRIES)
567: yScrolbar_size = (SGFS_NUMENTRIES-2) * sdlgui_fontheight;
568: else
569: yScrolbar_size = (int)((SGFS_NUMENTRIES-2) / ((float)entries/(float)SGFS_NUMENTRIES) * sdlgui_fontheight);
570: fsdlg[SGFSDLG_SCROLLBAR].w = yScrolbar_size;
571:
572: /* Refresh scrolbar pos */
573: fsdlg[SGFSDLG_SCROLLBAR].h = (int) (scrollbar_Ypos * sdlgui_fontheight);
574: ypos = (int) (scrollbar_Ypos * ((float)entries/(float)(SGFS_NUMENTRIES-2)) + 0.5);
575:
1.1 root 576: /* Update the file name strings in the dialog? */
577: if (refreshentries)
578: {
1.1.1.2 root 579: if (!DlgFileSelect_RefreshEntries(files, path, browsingzip))
1.1 root 580: {
1.1.1.10! root 581: goto clean_exit;
1.1 root 582: }
1.1.1.7 root 583: refreshentries = false;
1.1 root 584: }
585:
586: /* Show dialog: */
1.1.1.2 root 587: retbut = SDLGui_DoDialog(fsdlg, &sdlEvent);
1.1 root 588:
589: /* Has the user clicked on a file or folder? */
1.1.1.9 root 590: if (retbut>=SGFSDLG_ENTRYFIRST && retbut<=SGFSDLG_ENTRYLAST && retbut-SGFSDLG_ENTRYFIRST+ypos<entries)
1.1 root 591: {
592: char *tempstr;
1.1.1.4 root 593:
1.1 root 594: tempstr = malloc(FILENAME_MAX);
595: if (!tempstr)
596: {
597: perror("Error while allocating temporary memory in SDLGui_FileSelect()");
1.1.1.10! root 598: goto clean_exit;
1.1 root 599: }
600:
1.1.1.7 root 601: if (browsingzip == true)
1.1 root 602: {
1.1.1.4 root 603: if (!strcat_maxlen(tempstr, FILENAME_MAX,
1.1.1.9 root 604: zipdir, files[retbut-SGFSDLG_ENTRYFIRST+ypos]->d_name))
1.1.1.4 root 605: {
606: fprintf(stderr, "SDLGui_FileSelect: Path name too long!\n");
1.1.1.10! root 607: goto clean_exit;
1.1.1.4 root 608: }
609: /* directory? */
1.1.1.3 root 610: if (File_DoesFileNameEndWithSlash(tempstr))
1.1 root 611: {
612: /* handle the ../ directory */
1.1.1.9 root 613: if (strcmp(files[retbut-SGFSDLG_ENTRYFIRST+ypos]->d_name, "../") == 0)
1.1 root 614: {
615: /* close the zip file */
1.1.1.4 root 616: if (strcmp(tempstr, "../") == 0)
1.1 root 617: {
618: /* free zip file entries */
619: ZIP_FreeZipDir(zipfiles);
620: zipfiles = NULL;
621: /* Copy the path name to the dialog */
622: File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
1.1.1.7 root 623: browsingzip = false;
1.1 root 624: }
625: else
626: {
1.1.1.4 root 627: /* remove "../" and previous dir from path */
628: File_PathShorten(tempstr, 2);
629: correct_zip_root(tempstr);
1.1 root 630: strcpy(zipdir, tempstr);
631: File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE);
632: }
633: }
634: else /* not the "../" directory */
635: {
636: strcpy(zipdir, tempstr);
637: File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE);
638: }
1.1.1.7 root 639: reloaddir = true;
1.1 root 640: /* Copy the path name to the dialog */
641: zipfilename[0] = '\0';
642: dlgfname[0] = 0;
643: ypos = 0;
1.1.1.9 root 644: scrollbar_Ypos = 0.0;
1.1 root 645: }
646: else
647: {
1.1.1.4 root 648: /* not dir, select a file in the zip */
1.1.1.9 root 649: selection = retbut-SGFSDLG_ENTRYFIRST+ypos;
1.1 root 650: strcpy(zipfilename, files[selection]->d_name);
651: File_ShrinkName(dlgfname, zipfilename, DLGFNAME_SIZE);
652: }
653:
1.1.1.4 root 654: }
655: else /* not browsingzip */
1.1 root 656: {
1.1.1.4 root 657: if (!strcat_maxlen(tempstr, FILENAME_MAX,
1.1.1.9 root 658: path, files[retbut-SGFSDLG_ENTRYFIRST+ypos]->d_name))
1.1 root 659: {
1.1.1.4 root 660: fprintf(stderr, "SDLGui_FileSelect: Path name too long!\n");
1.1.1.10! root 661: goto clean_exit;
1.1.1.4 root 662: }
1.1.1.5 root 663: if (File_DirExists(tempstr))
1.1.1.4 root 664: {
665: File_HandleDotDirs(tempstr);
666: File_AddSlashToEndFileName(tempstr);
667: /* Copy the path name to the dialog */
668: File_ShrinkName(dlgpath, tempstr, DLGPATH_SIZE);
1.1 root 669: strcpy(path, tempstr);
1.1.1.7 root 670: reloaddir = true;
1.1 root 671: dlgfname[0] = 0;
672: ypos = 0;
1.1.1.9 root 673: scrollbar_Ypos = 0.0;
1.1 root 674: }
675: else if (ZIP_FileNameIsZIP(tempstr) && zip_path != NULL)
676: {
677: /* open a zip file */
678: zipfiles = ZIP_GetFiles(tempstr);
1.1.1.7 root 679: if (zipfiles != NULL && browsingzip == false)
1.1 root 680: {
1.1.1.9 root 681: selection = retbut-SGFSDLG_ENTRYFIRST+ypos;
1.1 root 682: strcpy(fname, files[selection]->d_name);
683: File_ShrinkName(dlgfname, fname, DLGFNAME_SIZE);
1.1.1.7 root 684: browsingzip = true;
1.1.1.4 root 685: zipdir[0] = '\0'; /* zip root */
1.1 root 686: File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE);
1.1.1.7 root 687: reloaddir = true;
1.1 root 688: ypos = 0;
1.1.1.9 root 689: scrollbar_Ypos = 0.0;
1.1 root 690: }
691:
692: }
693: else
694: {
695: /* Select a file */
1.1.1.9 root 696: selection = retbut-SGFSDLG_ENTRYFIRST+ypos;
1.1 root 697: strcpy(fname, files[selection]->d_name);
698: File_ShrinkName(dlgfname, fname, DLGFNAME_SIZE);
699: }
700:
701: } /* not browsingzip */
702:
703: free(tempstr);
704: }
705: else /* Has the user clicked on another button? */
706: {
707: switch(retbut)
708: {
709: case SGFSDLG_UPDIR: /* Change path to parent directory */
1.1.1.4 root 710: if (browsingzip)
1.1 root 711: {
1.1.1.4 root 712: /* close the zip file? */
713: if (!zipdir[0])
1.1 root 714: {
715: /* free zip file entries */
716: ZIP_FreeZipDir(zipfiles);
1.1.1.7 root 717: browsingzip = false;
1.1 root 718: zipfiles = NULL;
719: File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
720: }
721: else
722: {
1.1.1.4 root 723: /* remove last dir from zipdir path */
724: File_PathShorten(zipdir, 1);
725: correct_zip_root(zipdir);
1.1 root 726: File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE);
727: zipfilename[0] = '\0';
728: }
729: } /* not a zip file: */
1.1.1.4 root 730: else
1.1 root 731: {
1.1.1.4 root 732: File_PathShorten(path, 1);
733: File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
1.1 root 734: }
1.1.1.7 root 735: reloaddir = true;
1.1 root 736: break;
1.1.1.3 root 737:
738: case SGFSDLG_HOMEDIR: /* Change to home directory */
1.1.1.8 root 739: home = Paths_GetUserHome();
740: if (home == NULL || !*home)
1.1.1.3 root 741: break;
742: if (browsingzip)
743: {
744: /* free zip file entries */
745: ZIP_FreeZipDir(zipfiles);
746: zipfiles = NULL;
1.1.1.7 root 747: browsingzip = false;
1.1.1.3 root 748: }
1.1.1.4 root 749: strcpy(path, home);
1.1.1.3 root 750: File_AddSlashToEndFileName(path);
1.1.1.4 root 751: File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
1.1.1.7 root 752: reloaddir = true;
1.1.1.3 root 753: break;
754:
1.1 root 755: case SGFSDLG_ROOTDIR: /* Change to root directory */
1.1.1.4 root 756: if (browsingzip)
1.1 root 757: {
758: /* free zip file entries */
759: ZIP_FreeZipDir(zipfiles);
760: zipfiles = NULL;
1.1.1.7 root 761: browsingzip = false;
1.1 root 762: }
1.1.1.7 root 763: path[0] = PATHSEP; path[1] = '\0';
1.1 root 764: strcpy(dlgpath, path);
1.1.1.7 root 765: reloaddir = true;
1.1 root 766: break;
767: case SGFSDLG_UP: /* Scroll up */
1.1.1.2 root 768: DlgFileSelect_ScrollUp();
769: SDL_Delay(10);
1.1 root 770: break;
771: case SGFSDLG_DOWN: /* Scroll down */
1.1.1.2 root 772: DlgFileSelect_ScrollDown();
773: SDL_Delay(10);
1.1 root 774: break;
1.1.1.9 root 775: case SGFSDLG_SCROLLBAR: /* Scrollbar selected */
776: DlgFileSelect_ManageScrollbar();
777: SDL_Delay(10);
778: break;
1.1 root 779: case SGFSDLG_FILENAME: /* User entered new filename */
780: strcpy(fname, dlgfname);
781: break;
1.1.1.3 root 782: case SGFSDLG_SHOWHIDDEN: /* Show/hide hidden files */
1.1.1.7 root 783: reloaddir = true;
1.1.1.3 root 784: ypos = 0;
1.1.1.9 root 785: scrollbar_Ypos = 0.0;
1.1.1.3 root 786: break;
1.1.1.2 root 787: case SDLGUI_UNKNOWNEVENT:
788: DlgFileSelect_HandleSdlEvents(&sdlEvent);
789: break;
1.1 root 790: } /* switch */
1.1.1.4 root 791:
792: if (reloaddir)
793: {
794: /* Remove old selection */
795: fname[0] = 0;
796: dlgfname[0] = 0;
797: ypos = 0;
1.1.1.9 root 798: scrollbar_Ypos = 0.0;
1.1.1.4 root 799: }
1.1 root 800: } /* other button code */
801:
802:
803: } /* do */
1.1.1.4 root 804: while (retbut!=SGFSDLG_OKAY && retbut!=SGFSDLG_CANCEL
805: && retbut!=SDLGUI_QUIT && retbut != SDLGUI_ERROR && !bQuitProgram);
1.1 root 806:
1.1.1.4 root 807: files_free(files);
1.1 root 808:
809: if (browsingzip)
810: {
811: /* free zip file entries */
812: ZIP_FreeZipDir(zipfiles);
813: zipfiles = NULL;
814: }
815:
1.1.1.4 root 816: if (retbut == SGFSDLG_OKAY)
1.1 root 817: {
1.1.1.4 root 818: if (zip_path)
819: *zip_path = zip_get_path(zipdir, zipfilename, browsingzip);
820: retpath = File_MakePath(path, fname, NULL);
821: }
822: else
823: retpath = NULL;
1.1.1.10! root 824: clean_exit:
! 825: SDL_ShowCursor(bOldMouseVisibility);
1.1.1.4 root 826: free(pStringMem);
827: return retpath;
828: }
829:
830:
831: /*-----------------------------------------------------------------------*/
1.1.1.8 root 832: /**
833: * Let user browse for a file, confname is used as default.
1.1.1.4 root 834: * If bAllowNew is true, user can select new files also.
835: *
836: * If no file is selected, or there's some problem with the file,
1.1.1.7 root 837: * return false and clear dlgname & confname.
838: * Otherwise return true, set dlgname & confname to the new file name
1.1.1.4 root 839: * (dlgname is shrinked & limited to maxlen and confname is assumed
840: * to have FILENAME_MAX amount of space).
841: */
1.1.1.5 root 842: bool SDLGui_FileConfSelect(char *dlgname, char *confname, int maxlen, bool bAllowNew)
1.1.1.4 root 843: {
844: char *selname;
845:
846: selname = SDLGui_FileSelect(confname, NULL, bAllowNew);
847: if (selname)
848: {
849: if (!File_DoesFileNameEndWithSlash(selname) &&
850: (bAllowNew || File_Exists(selname)))
1.1 root 851: {
1.1.1.4 root 852: strncpy(confname, selname, FILENAME_MAX);
853: confname[FILENAME_MAX-1] = '\0';
854: File_ShrinkName(dlgname, selname, maxlen);
1.1 root 855: }
856: else
1.1.1.4 root 857: {
858: dlgname[0] = confname[0] = 0;
859: }
860: free(selname);
1.1.1.7 root 861: return true;
1.1 root 862: }
1.1.1.7 root 863: return false;
1.1 root 864: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.