Annotation of hatari/src/gui-sdl/dlgFileSelect.c, revision 1.1.1.3

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.3 ! root        9: const char DlgFileSelect_rcsid[] = "Hatari $Id: dlgFileSelect.c,v 1.14 2006/08/01 20:18:28 thothy Exp $";
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"
                     19: #include "zip.h"
                     20: 
                     21: 
1.1.1.2   root       22: #define SGFS_NUMENTRIES   16            /* How many entries are displayed at once */
                     23: 
                     24: 
1.1       root       25: #define SGFSDLG_FILENAME  5
                     26: #define SGFSDLG_UPDIR     6
1.1.1.3 ! root       27: #define SGFSDLG_HOMEDIR   7
        !            28: #define SGFSDLG_ROOTDIR   8
        !            29: #define SGFSDLG_ENTRY1    11
        !            30: #define SGFSDLG_ENTRY16   26
        !            31: #define SGFSDLG_UP        27
        !            32: #define SGFSDLG_DOWN      28
        !            33: #define SGFSDLG_SHOWHIDDEN  29
        !            34: #define SGFSDLG_OKAY      30
        !            35: #define SGFSDLG_CANCEL    31
1.1       root       36: 
                     37: 
                     38: #define DLGPATH_SIZE 62
                     39: static char dlgpath[DLGPATH_SIZE+1];    /* Path name in the dialog */
                     40: 
                     41: #define DLGFNAME_SIZE 56
                     42: static char dlgfname[DLGFNAME_SIZE+1];  /* Name of the selected file in the dialog */
                     43: 
                     44: #define DLGFILENAMES_SIZE 59
1.1.1.2   root       45: static char dlgfilenames[SGFS_NUMENTRIES][DLGFILENAMES_SIZE+1];  /* Visible file names in the dialog */
1.1       root       46: 
                     47: /* The dialog data: */
                     48: static SGOBJ fsdlg[] =
                     49: {
                     50:        { SGBOX, 0, 0, 0,0, 64,25, NULL },
                     51:        { SGTEXT, 0, 0, 25,1, 13,1, "Choose a file" },
                     52:        { SGTEXT, 0, 0, 1,2, 7,1, "Folder:" },
                     53:        { SGTEXT, 0, 0, 1,3, DLGPATH_SIZE,1, dlgpath },
                     54:        { SGTEXT, 0, 0, 1,4, 6,1, "File:" },
                     55:        { SGTEXT, 0, 0, 7,4, DLGFNAME_SIZE,1, dlgfname },
1.1.1.3 ! root       56:        { SGBUTTON, 0, 0, 51,1, 4,1, ".." },
        !            57:        { SGBUTTON, 0, 0, 56,1, 3,1, "~" },
1.1       root       58:        { SGBUTTON, 0, 0, 60,1, 3,1, "/" },
                     59:        { SGBOX, 0, 0, 1,6, 62,16, NULL },
                     60:        { SGBOX, 0, 0, 62,7, 1,14, NULL },
                     61:        { SGTEXT, SG_EXIT, 0, 2,6, DLGFILENAMES_SIZE,1, dlgfilenames[0] },
                     62:        { SGTEXT, SG_EXIT, 0, 2,7, DLGFILENAMES_SIZE,1, dlgfilenames[1] },
                     63:        { SGTEXT, SG_EXIT, 0, 2,8, DLGFILENAMES_SIZE,1, dlgfilenames[2] },
                     64:        { SGTEXT, SG_EXIT, 0, 2,9, DLGFILENAMES_SIZE,1, dlgfilenames[3] },
                     65:        { SGTEXT, SG_EXIT, 0, 2,10, DLGFILENAMES_SIZE,1, dlgfilenames[4] },
                     66:        { SGTEXT, SG_EXIT, 0, 2,11, DLGFILENAMES_SIZE,1, dlgfilenames[5] },
                     67:        { SGTEXT, SG_EXIT, 0, 2,12, DLGFILENAMES_SIZE,1, dlgfilenames[6] },
                     68:        { SGTEXT, SG_EXIT, 0, 2,13, DLGFILENAMES_SIZE,1, dlgfilenames[7] },
                     69:        { SGTEXT, SG_EXIT, 0, 2,14, DLGFILENAMES_SIZE,1, dlgfilenames[8] },
                     70:        { SGTEXT, SG_EXIT, 0, 2,15, DLGFILENAMES_SIZE,1, dlgfilenames[9] },
                     71:        { SGTEXT, SG_EXIT, 0, 2,16, DLGFILENAMES_SIZE,1, dlgfilenames[10] },
                     72:        { SGTEXT, SG_EXIT, 0, 2,17, DLGFILENAMES_SIZE,1, dlgfilenames[11] },
                     73:        { SGTEXT, SG_EXIT, 0, 2,18, DLGFILENAMES_SIZE,1, dlgfilenames[12] },
                     74:        { SGTEXT, SG_EXIT, 0, 2,19, DLGFILENAMES_SIZE,1, dlgfilenames[13] },
                     75:        { SGTEXT, SG_EXIT, 0, 2,20, DLGFILENAMES_SIZE,1, dlgfilenames[14] },
                     76:        { SGTEXT, SG_EXIT, 0, 2,21, DLGFILENAMES_SIZE,1, dlgfilenames[15] },
                     77:        { SGBUTTON, SG_TOUCHEXIT, 0, 62,6, 1,1, "\x01" },           /* Arrow up */
                     78:        { SGBUTTON, SG_TOUCHEXIT, 0, 62,21, 1,1, "\x02" },          /* Arrow down */
1.1.1.3 ! root       79:        { SGCHECKBOX, SG_EXIT, SG_SELECTED, 2,23, 18,1, "Show hidden files" },
        !            80:        { SGBUTTON, 0, 0, 32,23, 8,1, "Okay" },
        !            81:        { SGBUTTON, 0, 0, 50,23, 8,1, "Cancel" },
1.1       root       82:        { -1, 0, 0, 0,0, 0,0, NULL }
                     83: };
                     84: 
                     85: 
1.1.1.2   root       86: static int ypos;                        /* First entry number to be displayed */
                     87: static BOOL refreshentries;             /* Do we have to update the file names in the dialog? */
                     88: static int entries;                     /* How many files are in the actual directory? */
                     89: 
1.1       root       90: 
                     91: /*-----------------------------------------------------------------------*/
                     92: /*
                     93:   Update the file name strings in the dialog.
                     94:   Returns FALSE if it failed, TRUE on success.
                     95: */
1.1.1.2   root       96: static int DlgFileSelect_RefreshEntries(struct dirent **files, char *path, BOOL browsingzip)
1.1       root       97: {
                     98:        int i;
                     99:        char *tempstr = malloc(FILENAME_MAX);
                    100: 
                    101:        if (!tempstr)
                    102:        {
                    103:                perror("DlgFileSelect_RefreshEntries");
                    104:                return FALSE;
                    105:        }
                    106: 
                    107:        /* Copy entries to dialog: */
1.1.1.2   root      108:        for(i=0; i<SGFS_NUMENTRIES; i++)
1.1       root      109:        {
                    110:                if( i+ypos < entries )
                    111:                {
                    112:                        struct stat filestat;
                    113:                        /* Prepare entries: */
                    114:                        strcpy(tempstr, "  ");
                    115:                        strcat(tempstr, files[i+ypos]->d_name);
                    116:                        File_ShrinkName(dlgfilenames[i], tempstr, DLGFILENAMES_SIZE);
                    117:                        /* Mark folders: */
                    118:                        strcpy(tempstr, path);
                    119:                        strcat(tempstr, files[i+ypos]->d_name);
                    120: 
                    121:                        if( browsingzip )
                    122:                        {
1.1.1.3 ! root      123:                                if (File_DoesFileNameEndWithSlash(tempstr))
1.1       root      124:                                        dlgfilenames[i][0] = SGFOLDER;    /* Mark folders */
                    125:                        }
                    126:                        else
                    127:                        {
                    128:                                if( stat(tempstr, &filestat)==0 && S_ISDIR(filestat.st_mode) )
                    129:                                        dlgfilenames[i][0] = SGFOLDER;    /* Mark folders */
                    130:                                if (ZIP_FileNameIsZIP(tempstr) && browsingzip == FALSE)
                    131:                                        dlgfilenames[i][0] = SGFOLDER;    /* Mark .ZIP archives as folders */
                    132:                        }
                    133:                }
                    134:                else
                    135:                        dlgfilenames[i][0] = 0;  /* Clear entry */
                    136:        }
                    137: 
                    138:        free(tempstr);
                    139:        return TRUE;
                    140: }
                    141: 
                    142: 
                    143: /*-----------------------------------------------------------------------*/
                    144: /*
1.1.1.3 ! root      145:   Remove all hidden files (files with file names that begin with a dot) from
        !           146:   the list.
        !           147: */
        !           148: static void DlgFileSelect_RemoveHiddenFiles(struct dirent **files)
        !           149: {
        !           150:        int i;
        !           151:        int nActPos = -1;
        !           152:        int nOldEntries;
        !           153: 
        !           154:        nOldEntries = entries;
        !           155: 
        !           156:        /* Scan list for hidden files and remove them. */
        !           157:        for (i = 0; i < nOldEntries; i++)
        !           158:        {
        !           159:                /* Does file name start with a dot? -> hidden file! */
        !           160:                if (files[i]->d_name[0] == '.')
        !           161:                {
        !           162:                        if (nActPos == -1)
        !           163:                                nActPos = i;
        !           164:                        /* Remove file from list: */
        !           165:                        free(files[i]);
        !           166:                        files[i] = NULL;
        !           167:                        entries -= 1;
        !           168:                }
        !           169:        }
        !           170: 
        !           171:        /* Now close the gaps in the list: */
        !           172:        if (nActPos != -1)
        !           173:        {
        !           174:                for (i = nActPos; i < nOldEntries; i++)
        !           175:                {
        !           176:                        if (files[i] != NULL)
        !           177:                        {
        !           178:                                /* Move entry to earlier position: */
        !           179:                                files[nActPos] = files[i];
        !           180:                                files[i] = NULL;
        !           181:                                nActPos += 1;
        !           182:                        }
        !           183:                }
        !           184:        }
        !           185: }
        !           186: 
        !           187: 
        !           188: /*-----------------------------------------------------------------------*/
        !           189: /*
1.1.1.2   root      190:   Prepare to scroll up one entry.
                    191: */
                    192: static void DlgFileSelect_ScrollUp(void)
                    193: {
                    194:        if (ypos > 0)
                    195:        {
                    196:                --ypos;
                    197:                refreshentries = TRUE;
                    198:        }
                    199: }
                    200: 
                    201: 
                    202: /*-----------------------------------------------------------------------*/
                    203: /*
                    204:   Prepare to scroll down one entry.
                    205: */
                    206: static void DlgFileSelect_ScrollDown(void)
                    207: {
                    208:        if (ypos+SGFS_NUMENTRIES < entries)
                    209:        {
                    210:                ++ypos;
                    211:                refreshentries = TRUE;
                    212:        }
                    213: }
                    214: 
                    215: 
                    216: /*-----------------------------------------------------------------------*/
                    217: /*
                    218:   Handle SDL events.
                    219: */
                    220: static void DlgFileSelect_HandleSdlEvents(SDL_Event *pEvent)
                    221: {
                    222:        switch (pEvent->type)
                    223:        {
                    224:         case SDL_MOUSEBUTTONDOWN:
                    225:                if (pEvent->button.button == SDL_BUTTON_WHEELUP)
                    226:                        DlgFileSelect_ScrollUp();
                    227:                else if (pEvent->button.button == SDL_BUTTON_WHEELDOWN)
                    228:                        DlgFileSelect_ScrollDown();
                    229:                break;
                    230:         case SDL_KEYDOWN:
                    231:                switch (pEvent->key.keysym.sym)
                    232:                {
                    233:                 case SDLK_UP:          DlgFileSelect_ScrollUp(); break;
                    234:                 case SDLK_DOWN:        DlgFileSelect_ScrollDown(); break;
                    235:                 case SDLK_HOME:        ypos = 0; refreshentries = TRUE; break;
                    236:                 case SDLK_END:         ypos = entries-SGFS_NUMENTRIES; refreshentries = TRUE; break;
                    237:                 case SDLK_PAGEUP:
                    238:                        if (ypos > SGFS_NUMENTRIES)
                    239:                                ypos -= SGFS_NUMENTRIES;
                    240:                        else
                    241:                                ypos = 0;
                    242:                        refreshentries = TRUE;
                    243:                        break;
                    244:                 case SDLK_PAGEDOWN:
                    245:                        if (ypos+2*SGFS_NUMENTRIES < entries)
                    246:                                ypos += SGFS_NUMENTRIES;
                    247:                        else
                    248:                                ypos = entries-SGFS_NUMENTRIES;
                    249:                        refreshentries = TRUE;
                    250:                        break;
                    251:                 default: break;
                    252:                }
                    253:                break;
                    254:        }
                    255: }
                    256: 
                    257: 
                    258: /*-----------------------------------------------------------------------*/
                    259: /*
1.1       root      260:   Show and process a file selection dialog.
                    261:   Returns TRUE if the use selected "okay", FALSE if "cancel".
                    262:   input: zip_path = pointer to buffer to contain file path within a selected
                    263:   zip file, or NULL if browsing zip files is disallowed.
                    264:   bAllowNew: TRUE if the user is allowed to insert new file names.
1.1.1.3 ! root      265: 
        !           266:   TODO: This function urgently needs refactoring... it's way too big!
1.1       root      267: */
                    268: int SDLGui_FileSelect(char *path_and_name, char *zip_path, BOOL bAllowNew)
                    269: {
                    270:        int i,n;
                    271:        struct dirent **files = NULL;
                    272:        char *pStringMem;
                    273:        char *path, *fname;                 /* The actual file and path names */
                    274:        BOOL reloaddir = TRUE;              /* Do we have to reload the directory file list? */
                    275:        int retbut;
                    276:        int oldcursorstate;
                    277:        int selection = -1;                 /* The actual selection, -1 if none selected */
                    278:        char *zipfilename;                  /* Filename in zip file */
                    279:        char *zipdir;
                    280:        BOOL browsingzip = FALSE;           /* Are we browsing an archive? */
                    281:        zip_dir *zipfiles = NULL;
1.1.1.2   root      282:        SDL_Event sdlEvent;
                    283:        struct stat filestat;
                    284: 
                    285:        ypos = 0;
                    286:        refreshentries = TRUE;
                    287:        entries = 0;
1.1       root      288: 
                    289:        /* Allocate memory for the file and path name strings: */
                    290:        pStringMem = malloc(4 * FILENAME_MAX);
                    291:        path = pStringMem;
                    292:        fname = pStringMem + FILENAME_MAX;
                    293:        zipfilename = pStringMem + 2 * FILENAME_MAX;
                    294:        zipdir = pStringMem + 3 * FILENAME_MAX;
                    295: 
                    296:        zipfilename[0] = 0;
                    297: 
                    298:        SDLGui_CenterDlg(fsdlg);
                    299:        if (bAllowNew)
                    300:        {
                    301:                fsdlg[SGFSDLG_FILENAME].type = SGEDITFIELD;
                    302:                fsdlg[SGFSDLG_FILENAME].flags |= SG_EXIT;
                    303:        }
                    304:        else
                    305:        {
                    306:                fsdlg[SGFSDLG_FILENAME].type = SGTEXT;
                    307:                fsdlg[SGFSDLG_FILENAME].flags &= ~SG_EXIT;
                    308:        }
                    309: 
                    310:        /* Prepare the path and filename variables */
1.1.1.2   root      311:        if (stat(path_and_name, &filestat) == 0 && S_ISDIR(filestat.st_mode))
                    312:        {
                    313:                /* assure that a directory name ends with a '/' */
                    314:                File_AddSlashToEndFileName(path_and_name);
                    315:        }
1.1       root      316:        File_splitpath(path_and_name, path, fname, NULL);
1.1.1.2   root      317:        File_MakeAbsoluteName(path);
                    318:        File_MakeValidPathName(path);
1.1       root      319:        File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
                    320:        File_ShrinkName(dlgfname, fname, DLGFNAME_SIZE);
                    321: 
                    322:        /* Save old mouse cursor state and enable cursor anyway */
                    323:        oldcursorstate = SDL_ShowCursor(SDL_QUERY);
                    324:        if (oldcursorstate == SDL_DISABLE)
                    325:                SDL_ShowCursor(SDL_ENABLE);
                    326: 
                    327:        do
                    328:        {
                    329:                if (reloaddir)
                    330:                {
                    331:                        if (strlen(path) >= FILENAME_MAX)
                    332:                        {
                    333:                                fprintf(stderr, "SDLGui_FileSelect: Path name too long!\n");
                    334:                                free(pStringMem);
                    335:                                return FALSE;
                    336:                        }
                    337: 
                    338:                        /* Free old allocated memory: */
                    339:                        if (files != NULL)
                    340:                        {
                    341:                                for(i=0; i<entries; i++)
                    342:                                {
                    343:                                        free(files[i]);
                    344:                                }
                    345:                                free(files);
                    346:                                files = NULL;
                    347:                        }
                    348: 
                    349:                        if (browsingzip)
                    350:                        {
                    351:                                files = ZIP_GetFilesDir(zipfiles, zipdir, &entries);
                    352:                        }
                    353:                        else
                    354:                        {
                    355:                                /* Load directory entries: */
                    356:                                entries = scandir(path, &files, 0, alphasort);
                    357:                        }
1.1.1.3 ! root      358:                        
        !           359:                        /* Remove hidden files from the list if necessary: */
        !           360:                        if (!(fsdlg[SGFSDLG_SHOWHIDDEN].state & SG_SELECTED))
        !           361:                        {
        !           362:                                DlgFileSelect_RemoveHiddenFiles(files);
        !           363:                        }
1.1       root      364: 
                    365:                        if (entries < 0)
                    366:                        {
                    367:                                fprintf(stderr, "SDLGui_FileSelect: Path not found.\n");
                    368:                                free(pStringMem);
                    369:                                return FALSE;
                    370:                        }
                    371: 
                    372:                        reloaddir = FALSE;
                    373:                        refreshentries = TRUE;
                    374:                }/* reloaddir */
                    375: 
                    376:                /* Update the file name strings in the dialog? */
                    377:                if (refreshentries)
                    378:                {
1.1.1.2   root      379:                        if (!DlgFileSelect_RefreshEntries(files, path, browsingzip))
1.1       root      380:                        {
                    381:                                free(pStringMem);
                    382:                                return FALSE;
                    383:                        }
                    384:                        refreshentries = FALSE;
                    385:                }
                    386: 
                    387:                /* Show dialog: */
1.1.1.2   root      388:                retbut = SDLGui_DoDialog(fsdlg, &sdlEvent);
1.1       root      389: 
                    390:                /* Has the user clicked on a file or folder? */
                    391:                if( retbut>=SGFSDLG_ENTRY1 && retbut<=SGFSDLG_ENTRY16 && retbut-SGFSDLG_ENTRY1+ypos<entries)
                    392:                {
                    393:                        char *tempstr;
                    394: 
                    395:                        tempstr = malloc(FILENAME_MAX);
                    396:                        if (!tempstr)
                    397:                        {
                    398:                                perror("Error while allocating temporary memory in SDLGui_FileSelect()");
                    399:                                free(pStringMem);
                    400:                                return FALSE;
                    401:                        }
                    402: 
                    403:                        if( browsingzip == TRUE )
                    404:                        {
                    405:                                strcpy(tempstr, zipdir);
                    406:                                strcat(tempstr, files[retbut-SGFSDLG_ENTRY1+ypos]->d_name);
1.1.1.3 ! root      407:                                if (File_DoesFileNameEndWithSlash(tempstr))
1.1       root      408:                                {
                    409:                                        /* handle the ../ directory */
                    410:                                        if(strcmp(files[retbut-SGFSDLG_ENTRY1+ypos]->d_name, "../") == 0)
                    411:                                        {
                    412:                                                /* close the zip file */
                    413:                                                if( strcmp(tempstr, "../") == 0 )
                    414:                                                {
                    415:                                                        reloaddir = refreshentries = TRUE;
                    416:                                                        /* free zip file entries */
                    417:                                                        ZIP_FreeZipDir(zipfiles);
                    418:                                                        zipfiles = NULL;
                    419:                                                        /* Copy the path name to the dialog */
                    420:                                                        File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
                    421:                                                        browsingzip = FALSE;
                    422:                                                }
                    423:                                                else
                    424:                                                {
                    425:                                                        i=strlen(tempstr)-1;
                    426:                                                        n=0;
                    427:                                                        while(i > 0 && n < 3)
1.1.1.3 ! root      428:                                                                if (tempstr[i--] == PATHSEP)
1.1       root      429:                                                                        n++;
1.1.1.3 ! root      430:                                                        if (tempstr[i+1] == PATHSEP)
1.1       root      431:                                                                tempstr[i+2] = '\0';
                    432:                                                        else
                    433:                                                                tempstr[0] = '\0';
                    434: 
                    435:                                                        strcpy(zipdir, tempstr);
                    436:                                                        File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE);
                    437:                                                }
                    438:                                        }
                    439:                                        else /* not the "../" directory */
                    440:                                        {
                    441:                                                strcpy(zipdir, tempstr);
                    442:                                                File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE);
                    443:                                        }
                    444:                                        reloaddir = TRUE;
                    445:                                        /* Copy the path name to the dialog */
                    446:                                        selection = -1;                /* Remove old selection */
                    447:                                        zipfilename[0] = '\0';
                    448:                                        dlgfname[0] = 0;
                    449:                                        ypos = 0;
                    450: 
                    451:                                }
                    452:                                else
                    453:                                {
                    454:                                        /* Select a file in the zip */
                    455:                                        selection = retbut-SGFSDLG_ENTRY1+ypos;
                    456:                                        strcpy(zipfilename, files[selection]->d_name);
                    457:                                        File_ShrinkName(dlgfname, zipfilename, DLGFNAME_SIZE);
                    458:                                }
                    459: 
                    460:                        } /* if browsingzip */
                    461:                        else
                    462:                        {
                    463:                                strcpy(tempstr, path);
                    464:                                strcat(tempstr, files[retbut-SGFSDLG_ENTRY1+ypos]->d_name);
                    465:                                if( stat(tempstr, &filestat)==0 && S_ISDIR(filestat.st_mode) )
                    466:                                {
                    467:                                        /* Set the new directory */
                    468:                                        strcpy(path, tempstr);
                    469:                                        if( strlen(path)>=3 )
                    470:                                        {
1.1.1.3 ! root      471:                                                if (path[strlen(path)-2] == PATHSEP && path[strlen(path)-1]=='.')
1.1       root      472:                                                        path[strlen(path)-2] = 0;  /* Strip a single dot at the end of the path name */
1.1.1.3 ! root      473:                                                if (path[strlen(path)-3] == PATHSEP && path[strlen(path)-2]=='.' && path[strlen(path)-1]=='.')
1.1       root      474:                                                {
                    475:                                                        /* Handle the ".." folder */
                    476:                                                        char *ptr;
                    477:                                                        if( strlen(path)==3 )
                    478:                                                                path[1] = 0;
                    479:                                                        else
                    480:                                                        {
                    481:                                                                path[strlen(path)-3] = 0;
1.1.1.3 ! root      482:                                                                ptr = strrchr(path, PATHSEP);
1.1       root      483:                                                                if(ptr)
                    484:                                                                        *(ptr+1) = 0;
                    485:                                                        }
                    486:                                                }
                    487:                                        }
                    488:                                        File_AddSlashToEndFileName(path);
                    489:                                        reloaddir = TRUE;
                    490:                                        /* Copy the path name to the dialog */
                    491:                                        File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
                    492:                                        selection = -1;                /* Remove old selection */
                    493:                                        dlgfname[0] = 0;
                    494:                                        ypos = 0;
                    495:                                }
                    496:                                else if (ZIP_FileNameIsZIP(tempstr) && zip_path != NULL)
                    497:                                {
                    498:                                        /* open a zip file */
                    499:                                        zipfiles = ZIP_GetFiles(tempstr);
                    500:                                        if( zipfiles != NULL && browsingzip == FALSE )
                    501:                                        {
                    502:                                                selection = retbut-SGFSDLG_ENTRY1+ypos;
                    503:                                                strcpy(fname, files[selection]->d_name);
                    504:                                                File_ShrinkName(dlgfname, fname, DLGFNAME_SIZE);
                    505:                                                browsingzip=TRUE;
                    506:                                                strcpy(zipdir, "");
                    507:                                                File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE);
                    508:                                                reloaddir = refreshentries = TRUE;
                    509:                                                ypos = 0;
                    510:                                        }
                    511: 
                    512:                                }
                    513:                                else
                    514:                                {
                    515:                                        /* Select a file */
                    516:                                        selection = retbut-SGFSDLG_ENTRY1+ypos;
                    517:                                        strcpy(fname, files[selection]->d_name);
                    518:                                        File_ShrinkName(dlgfname, fname, DLGFNAME_SIZE);
                    519:                                }
                    520: 
                    521:                        } /* not browsingzip */
                    522: 
                    523:                        free(tempstr);
                    524:                }
                    525:                else    /* Has the user clicked on another button? */
                    526:                {
                    527:                        switch(retbut)
                    528:                        {
                    529:                        case SGFSDLG_UPDIR:                 /* Change path to parent directory */
                    530: 
                    531:                                if( browsingzip )
                    532:                                {
                    533:                                        /* close the zip file */
                    534:                                        if( strcmp(zipdir, "") == 0 )
                    535:                                        {
                    536:                                                reloaddir = refreshentries = TRUE;
                    537:                                                /* free zip file entries */
                    538:                                                ZIP_FreeZipDir(zipfiles);
                    539:                                                zipfiles = NULL;
                    540:                                                /* Copy the path name to the dialog */
                    541:                                                File_ShrinkName(dlgpath, path, DLGPATH_SIZE);
                    542:                                                browsingzip = FALSE;
                    543:                                                reloaddir = TRUE;
                    544:                                                selection = -1;         /* Remove old selection */
                    545:                                                fname[0] = 0;
                    546:                                                dlgfname[0] = 0;
                    547:                                                ypos = 0;
                    548:                                        }
                    549:                                        else
                    550:                                        {
                    551:                                                i=strlen(zipdir)-1;
                    552:                                                n=0;
                    553:                                                while(i > 0 && n < 2)
1.1.1.3 ! root      554:                                                        if (zipdir[i--] == PATHSEP)
1.1       root      555:                                                                n++;
1.1.1.3 ! root      556:                                                if (zipdir[i+1] == PATHSEP)
1.1       root      557:                                                        zipdir[i+2] = '\0';
                    558:                                                else
                    559:                                                        zipdir[0] = '\0';
                    560: 
                    561:                                                File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE);
                    562:                                                reloaddir = TRUE;
                    563:                                                selection = -1;         /* Remove old selection */
                    564:                                                zipfilename[0] = '\0';
                    565:                                                dlgfname[0] = 0;
                    566:                                                ypos = 0;
                    567:                                        }
                    568:                                }  /* not a zip file: */
                    569:                                else if( strlen(path)>2 )
                    570:                                {
                    571:                                        char *ptr;
                    572:                                        File_CleanFileName(path);
1.1.1.3 ! root      573:                                        ptr = strrchr(path, PATHSEP);
1.1       root      574:                                        if(ptr)
                    575:                                                *(ptr+1) = 0;
                    576:                                        File_AddSlashToEndFileName(path);
                    577:                                        reloaddir = TRUE;
                    578:                                        File_ShrinkName(dlgpath, path, DLGPATH_SIZE);  /* Copy the path name to the dialog */
                    579:                                        selection = -1;             /* Remove old selection */
                    580:                                        fname[0] = 0;
                    581:                                        dlgfname[0] = 0;
                    582:                                        ypos = 0;
                    583:                                }
                    584:                                break;
1.1.1.3 ! root      585: 
        !           586:                        case SGFSDLG_HOMEDIR:               /* Change to home directory */
        !           587:                                if (getenv("HOME") == NULL)
        !           588:                                        break;
        !           589:                                if (browsingzip)
        !           590:                                {
        !           591:                                        /* free zip file entries */
        !           592:                                        ZIP_FreeZipDir(zipfiles);
        !           593:                                        zipfiles = NULL;
        !           594:                                        browsingzip = FALSE;
        !           595:                                }
        !           596:                                strcpy(path, getenv("HOME"));
        !           597:                                File_AddSlashToEndFileName(path);
        !           598:                                reloaddir = TRUE;
        !           599:                                strcpy(dlgpath, path);
        !           600:                                selection = -1;                 /* Remove old selection */
        !           601:                                fname[0] = 0;
        !           602:                                dlgfname[0] = 0;
        !           603:                                ypos = 0;
        !           604:                                break;
        !           605: 
1.1       root      606:                        case SGFSDLG_ROOTDIR:               /* Change to root directory */
                    607:                                if( browsingzip )
                    608:                                {
                    609:                                        /* free zip file entries */
                    610:                                        ZIP_FreeZipDir(zipfiles);
                    611:                                        zipfiles = NULL;
                    612:                                        browsingzip = FALSE;
                    613:                                }
                    614: 
                    615:                                strcpy(path, "/");
                    616:                                reloaddir = TRUE;
                    617:                                strcpy(dlgpath, path);
                    618:                                selection = -1;                 /* Remove old selection */
                    619:                                fname[0] = 0;
                    620:                                dlgfname[0] = 0;
                    621:                                ypos = 0;
                    622:                                break;
                    623:                        case SGFSDLG_UP:                    /* Scroll up */
1.1.1.2   root      624:                                DlgFileSelect_ScrollUp();
                    625:                                SDL_Delay(10);
1.1       root      626:                                break;
                    627:                        case SGFSDLG_DOWN:                  /* Scroll down */
1.1.1.2   root      628:                                DlgFileSelect_ScrollDown();
                    629:                                SDL_Delay(10);
1.1       root      630:                                break;
                    631:                        case SGFSDLG_FILENAME:              /* User entered new filename */
                    632:                                strcpy(fname, dlgfname);
                    633:                                break;
1.1.1.3 ! root      634:                        case SGFSDLG_SHOWHIDDEN:            /* Show/hide hidden files */
        !           635:                                reloaddir = TRUE;
        !           636:                                ypos = 0;
        !           637:                                break;
1.1.1.2   root      638:                        case SDLGUI_UNKNOWNEVENT:
                    639:                                DlgFileSelect_HandleSdlEvents(&sdlEvent);
                    640:                                break;
1.1       root      641:                        } /* switch */
                    642:                } /* other button code */
                    643: 
                    644: 
                    645:        } /* do */
1.1.1.2   root      646:        while (retbut!=SGFSDLG_OKAY && retbut!=SGFSDLG_CANCEL && retbut!=SDLGUI_QUIT && !bQuitProgram);
1.1       root      647: 
                    648:        if (oldcursorstate == SDL_DISABLE)
                    649:                SDL_ShowCursor(SDL_DISABLE);
                    650: 
                    651:        File_makepath(path_and_name, path, fname, NULL);
                    652: 
                    653:        /* Free old allocated memory: */
                    654:        if (files != NULL)
                    655:        {
                    656:                for(i=0; i<entries; i++)
                    657:                {
                    658:                        free(files[i]);
                    659:                }
                    660:                free(files);
                    661:                files = NULL;
                    662:        }
                    663: 
                    664:        if (browsingzip)
                    665:        {
                    666:                /* free zip file entries */
                    667:                ZIP_FreeZipDir(zipfiles);
                    668:                zipfiles = NULL;
                    669:        }
                    670: 
                    671:        if (zip_path != NULL)
                    672:        {
                    673:                if( browsingzip )
                    674:                {
                    675:                        strcpy(zip_path, zipdir);
                    676:                        strcat(zip_path, zipfilename);
                    677:                }
                    678:                else
                    679:                        zip_path[0] = '\0';
                    680:        }
                    681: 
                    682:        free(pStringMem);
                    683: 
                    684:        return(retbut == SGFSDLG_OKAY);
                    685: }
                    686: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.