initial import
[picodrive.git] / platform / win32 / GenaDrive / FileMenu.cpp
1 \r
2 #include "app.h"\r
3 #include "FileMenu.h"\r
4 \r
5 class FileMenu FileMenu;\r
6 \r
7 FileMenu::FileMenu()\r
8 {\r
9   memset(this,0,sizeof(*this));\r
10 }\r
11 \r
12 int FileMenu::init()\r
13 {\r
14   memset(this,0,sizeof(*this));\r
15   strcpy(currentPath,HOME "roms");\r
16 \r
17   return 0;\r
18 }\r
19 \r
20 int FileMenu::scan()\r
21 {\r
22   char path[260];\r
23 \r
24   memset(path,0,sizeof(path));\r
25 \r
26   // Scan for all the roms in the current directory:\r
27   nameReset();\r
28 \r
29   sprintf(path,"%.240s\\*.bin", currentPath); nameFind(path);\r
30   sprintf(path,"%.240s\\*.smd", currentPath); nameFind(path);\r
31   sprintf(path,"%.240s\\*.zip",currentPath); nameFind(path);\r
32 \r
33   return 0;\r
34 }\r
35 \r
36 void FileMenu::exit()\r
37 {\r
38   free(nameList);\r
39   memset(this,0,sizeof(*this));\r
40 }\r
41 \r
42 int FileMenu::render()\r
43 {\r
44   int x=0,y=0;\r
45   int pos=0,index=0;\r
46   WCHAR text[64];\r
47   int height=24;\r
48 \r
49   memset(text,0,sizeof(text));\r
50 \r
51   x=120; y=224;\r
52   y-=(choiceFocus*height)>>8;\r
53 \r
54   while (pos<nameSize)\r
55   {\r
56     char *name=NULL;\r
57 \r
58     name=nameList+pos;\r
59 \r
60     if (y>-height && y<MainHeight)\r
61     {\r
62       unsigned int colour=0xffffff;\r
63 \r
64       // If this line is visible:\r
65       wsprintfW(text,L"%.42S",name);\r
66       if (index==(choiceFocus>>8)) colour=0x00ff40;\r
67 \r
68       FontSetColour(colour);\r
69       FontText(text,x,y);\r
70     }\r
71         \r
72     y+=height;\r
73     pos+=strlen(name)+1; // Skip to next string\r
74     index++;\r
75   }\r
76 \r
77   return 0;\r
78 }\r
79 \r
80 int FileMenu::scroll(int amount)\r
81 {\r
82   int max=0;\r
83 \r
84   choiceFocus+=amount;\r
85 \r
86   max=nameCount<<8;\r
87   if (choiceFocus<0) choiceFocus=0;\r
88   if (choiceFocus>=max) choiceFocus=max-1;\r
89 \r
90   return 0;\r
91 }\r
92 \r
93 // Get the currently highlighted filename\r
94 int FileMenu::getFilePath(char *path)\r
95 {\r
96   int focus=0;\r
97   int pos=0;\r
98   char *name=NULL;\r
99 \r
100   // Find where the user is focused\r
101   focus=choiceFocus>>8;\r
102   pos=nameOffset(focus); if (pos<0) return 1;\r
103 \r
104   name=nameList+pos;\r
105 \r
106   // Return path and name:\r
107   sprintf(path,"%.128s\\%.128s",currentPath,name);\r
108   return 0;\r
109 }\r
110 \r
111 // ----------------------------------------------------------------------\r
112 int FileMenu::nameReset()\r
113 {\r
114   free(nameList); nameList=NULL;\r
115   nameSize=nameMax=nameCount=0;\r
116 \r
117   return 0;\r
118 }\r
119 \r
120 int FileMenu::nameFind(char *path)\r
121 {\r
122   HANDLE find=NULL;\r
123   WIN32_FIND_DATA wfd;\r
124  \r
125   memset(&wfd,0,sizeof(wfd));\r
126 \r
127   find=FindFirstFile(path,&wfd);\r
128   if (find==INVALID_HANDLE_VALUE) return 1;\r
129   \r
130   for (;;)\r
131   {\r
132     nameAdd(wfd.cFileName); // Add the name to the list\r
133 \r
134     if (FindNextFile(find,&wfd)==0) break;\r
135   }\r
136 \r
137   FindClose(find);\r
138   return 0;\r
139 }\r
140 \r
141 int FileMenu::nameAdd(char *entry)\r
142 {\r
143   int len=0;\r
144 \r
145   len=strlen(entry);\r
146   // Check we have room for this entry:\r
147   if (nameSize+len+1>nameMax) nameSizeUp();\r
148   if (nameSize+len+1>nameMax) return 1;\r
149 \r
150   // Add entry with zero at the end:\r
151   memcpy(nameList+nameSize,entry,len);\r
152   nameSize+=len+1;\r
153   nameCount++;\r
154 \r
155   return 0;\r
156 }\r
157 \r
158 int FileMenu::nameSizeUp()\r
159 {\r
160 \r
161   void *mem=NULL;\r
162   int add=256;\r
163 \r
164   // Allocate more memory for the list:\r
165   mem=realloc(nameList,nameMax+add); if (mem==NULL) return 1;\r
166 \r
167   nameList=(char *)mem;\r
168   memset(nameList+nameMax,0,add); // Blank new memory\r
169   nameMax+=add;\r
170   return 0;\r
171 }\r
172 \r
173 int FileMenu::nameOffset(int index)\r
174 {\r
175   int pos=0,i=0;\r
176 \r
177   while (pos<nameSize)\r
178   {\r
179     char *name=nameList+pos;\r
180 \r
181     if (i==index) return pos;\r
182 \r
183     pos+=strlen(name)+1; // Skip to next string\r
184     i++;\r
185   }\r
186 \r
187   return -1; // Unknown index\r
188 }\r