]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/filelist.cpp
Major refactoring of GUI: Phase I
[virtualjaguar] / src / gui / filelist.cpp
1 //
2 // FileList class
3 //
4 // by James L. Hammons
5 //
6
7 #include "filelist.h"
8
9 #include <sys/types.h>                                                          // For MacOS <dirent.h> dependency
10 #include <dirent.h>
11 #include "settings.h"
12 #include "file.h"
13 #include "guimisc.h"
14
15 //Need 4 buttons, one scrollbar...
16 FileList::FileList(uint32 x, uint32 y, uint32 w, uint32 h): Window(x, y, w, h)
17 {
18         files = new ListBox(8, 8, w - 16, h - 32);
19         AddElement(files);
20         load = new Button(8, h - 16, " Load ");
21         AddElement(load);
22         load->SetNotificationElement(this);
23
24 #warning !!! FIX !!! Directory might not exist--this shouldn't cause VJ to crash!
25         DIR * dp = opendir(vjs.ROMPath);
26         dirent * de;
27
28         if (dp != NULL)
29         {
30                 while ((de = readdir(dp)) != NULL)
31                 {
32                         char * ext = strrchr(de->d_name, '.');
33
34                         if (ext != NULL)
35                                 if (strcasecmp(ext, ".zip") == 0 || strcasecmp(ext, ".j64") == 0
36                                         || strcasecmp(ext, ".abs") == 0 || strcasecmp(ext, ".jag") == 0
37                                         || strcasecmp(ext, ".rom") == 0)
38                                         files->AddItem(std::string(de->d_name));
39                 }
40
41                 closedir(dp);
42         }
43         else
44         {
45 //Give a diagnostic message here so that the (l)user can figure out what went wrong. !!! FIX !!!
46         }
47 }
48
49 void FileList::HandleKey(SDLKey key)
50 {
51         if (key == SDLK_RETURN)
52                 Notify(load);
53         else
54                 Window::HandleKey(key);
55 }
56
57 void FileList::HandleMouseMove(uint32 x, uint32 y)
58 {
59         Window::HandleMouseMove(x, y);
60 }
61
62 void FileList::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
63 {
64         Window::HandleMouseButton(x, y, mouseDown);
65 }
66
67 void FileList::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/)
68 {
69         Window::Draw(offsetX, offsetY);
70 }
71
72 extern Window * ResetJaguar(void);
73 #warning ICKY KLUDGE--FIX THIS MESS!!!
74 void FileList::Notify(Element * e)
75 {
76         if (e == load)
77         {
78                 char filename[MAX_PATH];
79                 strcpy(filename, vjs.ROMPath);
80
81                 if (strlen(filename) > 0)
82                         if (filename[strlen(filename) - 1] != '/')
83                                 strcat(filename, "/");
84
85                 strcat(filename, files->GetSelectedItem().c_str());
86
87 //              uint32 romSize = JaguarLoadROM(jaguar_mainRom, filename);
88 //              JaguarLoadCart(jaguar_mainRom, filename);
89                 if (JaguarLoadFile(filename))
90                 {
91                         SDL_Event event;
92                         event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
93                         SDL_PushEvent(&event);
94
95                         event.type = SDL_USEREVENT, event.user.code = MENU_ITEM_CHOSEN;
96                         event.user.data1 = (void *)ResetJaguar;
97                 SDL_PushEvent(&event);
98                 }
99                 else
100                 {
101                         SDL_Event event;
102                         event.type = SDL_USEREVENT, event.user.code = WINDOW_CLOSE;
103                         SDL_PushEvent(&event);
104
105                         // Handle the error, but don't run...
106                         // Tell the user that we couldn't run their file for some reason... !!! FIX !!!
107 //how to kludge: Make a function like ResetJaguar which creates the dialog window
108                 }
109         }
110         else
111                 Window::Notify(e);
112 }