ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/RenegadeMapSelector/RenegadeMapSelector.cpp
Revision: 70
Committed: 2003-03-11T19:48:27-08:00 (21 years, 1 month ago) by douglas
Original Path: trunk/RenegadeMapSelector/RenegadeMapSelector.cpp
File size: 7810 byte(s)
Log Message:
Almost everything works but the up and down buttons.
Still need to port to GTK for Linux.

File Contents

# User Rev Content
1 douglas 63 // Renegade Map Selector
2     //
3     // Douglas Thrift
4     //
5     // RenegadeMapSelector.cpp
6    
7 douglas 66 #ifdef _WIN32
8     #include <io.h>
9     #include <fcntl.h>
10 douglas 67 #include "resource.h"
11 douglas 66 #endif
12    
13 douglas 63 #include "RenegadeMapSelector.h"
14 douglas 67 #include "RenegadeConfig.h"
15 douglas 63
16     bool debug = false;
17     string program;
18 douglas 67 RenegadeConfig* config;
19 douglas 63
20 douglas 66 #ifndef _WIN32
21     inline string fix(const string& ansi) { return ansi; }
22 douglas 67 inline void munge(void) { return; }
23 douglas 66 #else
24     inline string fix(const wstring& wide)
25 douglas 63 {
26     char* buffer = new char[wide.length() + 1];
27    
28     WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, buffer, wide.length() + 1,
29     NULL, NULL);
30    
31     string ansi = buffer;
32     delete [] buffer;
33    
34     return ansi;
35     }
36    
37 douglas 67 inline void munge(void)
38 douglas 66 {
39 douglas 67 if (debug) return;
40    
41 douglas 66 AllocConsole();
42     SetConsoleTitle("Renegade Map Selector");
43    
44     int hin = _open_osfhandle(long(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT);
45     int hout = _open_osfhandle(long(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT);
46     int herr = _open_osfhandle(long(GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT);
47    
48     FILE* fin = _fdopen(hin, "r");
49     FILE* fout = _fdopen(hout, "w");
50     FILE* ferr = _fdopen(herr, "w");
51    
52     *stdin = *fin;
53     *stdout = *fout;
54     *stderr = *ferr;
55    
56     setvbuf(stdin, NULL, _IONBF, 0);
57     setvbuf(stdout, NULL, _IONBF, 0);
58     setvbuf(stderr, NULL, _IONBF, 0);
59    
60     cin.sync_with_stdio();
61     cout.sync_with_stdio();
62     cerr.sync_with_stdio();
63 douglas 67
64     debug = true;
65 douglas 66 }
66     #endif
67    
68 douglas 63 int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
69    
70     int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
71     lpCmdLine, int nShowCmd)
72     {
73     int argc;
74     unsigned short** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
75    
76 douglas 68 string error;
77 douglas 67 string file = "svrcfg_cnc.ini";
78    
79 douglas 66 program = fix(argv[0]);
80 douglas 63
81     for (int index = 1; index < argc; index++)
82     {
83 douglas 66 string arg = fix(argv[index]);
84 douglas 63
85     if (arg == "-D")
86     {
87 douglas 67 munge();
88 douglas 63 }
89 douglas 67 else if (arg == "-file")
90     {
91     if (++index < argc)
92     {
93     file = fix(argv[index]);
94     }
95     else
96     {
97     error = "The argument -file must be followed by a filename.";
98    
99     MessageBox(NULL, error.c_str(), "Bad Arguments", MB_ICONERROR);
100     return 1;
101     }
102     }
103 douglas 63 }
104    
105 douglas 67 GlobalFree(argv);
106 douglas 63
107 douglas 67 if (debug) cerr << "file = " << file << "\n";
108    
109     config = new RenegadeConfig(file);
110 douglas 68 if (!config->load())
111     {
112     error = "Could not open " + file + ".";
113 douglas 67
114 douglas 68 MessageBox(NULL, error.c_str(), "Bad File", MB_ICONERROR);
115     return 1;
116     }
117    
118 douglas 63 DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAP_SELECTOR), NULL, selector);
119    
120 douglas 67 delete config;
121    
122 douglas 70 if (debug)
123     {
124     cout << "Press enter key to exit . . .";
125     cin.get();
126     }
127 douglas 68
128 douglas 63 return 0;
129     }
130    
131     int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
132     {
133     switch (uMsg)
134     {
135     case WM_INITDIALOG:
136     {
137     RECT rc, rcDlg, rcDesktop;
138    
139     GetWindowRect(GetDesktopWindow(), &rcDesktop);
140     GetWindowRect(hwndDlg, &rcDlg);
141     CopyRect(&rc, &rcDesktop);
142    
143     OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
144     OffsetRect(&rc, -rc.left, -rc.top);
145     OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
146    
147     SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2),
148     rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE);
149     }
150     {
151     char* directory = new char[MAX_PATH + 1];
152     GetCurrentDirectory(MAX_PATH + 1, directory);
153 douglas 66 if (debug) cerr << "directory = " << directory << "\n";
154 douglas 63
155     char* data = new char[MAX_PATH + 1];
156     sprintf(data, "%s\\data\\*.mix", directory);
157     delete [] directory;
158 douglas 66 if (debug) cerr << "data = " << data << "\n";
159 douglas 63
160     DlgDirList(hwndDlg, data, IDC_AVAILABLE, 0, DDL_ARCHIVE);
161 douglas 68 SetCurrentDirectory("..");
162 douglas 63
163     delete [] data;
164     }
165 douglas 69 {
166     vector<string> maps = config->getMaps();
167    
168     for (unsigned index = 0; index < maps.size(); index++)
169     {
170     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0,
171     long(maps[index].c_str()));
172     }
173     }
174 douglas 63 break;
175     case WM_COMMAND:
176     switch (LOWORD(wParam))
177     {
178     case IDOK:
179 douglas 69 if (GetListBoxInfo(GetDlgItem(hwndDlg, IDC_SELECTED)) == 0)
180     {
181     MessageBox(hwndDlg, "You need at least one map.", "No Maps",
182     MB_ICONEXCLAMATION);
183    
184     return false;
185     }
186     else
187     {
188     vector<string> maps;
189    
190     for (unsigned index = 0; index < GetListBoxInfo(GetDlgItem(
191     hwndDlg, IDC_SELECTED)); index++)
192     {
193     char* name = new char[SendMessage(GetDlgItem(hwndDlg,
194     IDC_SELECTED), LB_GETTEXTLEN, index, 0) + 1];
195    
196     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
197     index, long(name));
198     maps.push_back(name);
199    
200     delete [] name;
201     }
202    
203     config->setMaps(maps);
204     }
205 douglas 67 config->save();
206 douglas 63 case IDCANCEL:
207     EndDialog(hwndDlg, wParam);
208     return true;
209 douglas 69 case IDC_ADD_MAP:
210 douglas 70 {
211     unsigned count = SendMessage(GetDlgItem(hwndDlg,
212     IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0);
213     unsigned* additions = new unsigned[count];
214    
215     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELITEMS,
216     count, long(additions));
217    
218     if (debug) cerr << "additions = {\n";
219     for (unsigned index = 0; index < count; index++)
220     {
221     if (debug) cerr << " [" << index << "] = " <<
222     additions[index] << "\n";
223    
224     char* name = new char[SendMessage(GetDlgItem(hwndDlg,
225     IDC_AVAILABLE), LB_GETTEXTLEN, additions[index], 0) + 1
226     ];
227    
228     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXT,
229     additions[index], long(name));
230     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
231     LB_ADDSTRING, 0, long(name));
232    
233     delete [] name;
234     }
235     if (debug) cerr << "}\n";
236    
237     delete [] additions;
238    
239     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_SETSEL,
240     false, -1);
241     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false);
242     }
243 douglas 69 break;
244     case IDC_REMOVE_MAP:
245 douglas 70 {
246     unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
247     LB_GETSELCOUNT, 0, 0);
248     unsigned* removals = new unsigned[count];
249    
250     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
251     count, long(removals));
252    
253     if (debug) cerr << "removals = {\n";
254     for (unsigned index = count; index > 0; index--)
255     {
256     if (debug) cerr << " [" << index - 1 << "] = " <<
257     removals[index - 1] << "\n";
258    
259     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
260     LB_DELETESTRING, removals[index - 1], 0);
261     }
262     if (debug) cerr << "}\n";
263    
264     delete [] removals;
265    
266     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false);
267     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
268     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
269     }
270 douglas 69 break;
271     case IDC_UP_MAP:
272 douglas 70 cout << "STUB: up\a\n";
273 douglas 69 break;
274     case IDC_DOWN_MAP:
275 douglas 70 cout << "STUB: down\a\n";
276 douglas 69 break;
277     case IDC_AVAILABLE:
278     switch (HIWORD(wParam))
279     {
280     case LBN_SELCHANGE:
281     if (SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE),
282     LB_GETSELCOUNT, 0, 0) > 0)
283     {
284     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), true);
285     }
286     else
287     {
288     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false);
289     }
290     break;
291     }
292     break;
293     case IDC_SELECTED:
294     switch (HIWORD(wParam))
295     {
296     case LBN_SELCHANGE:
297     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
298     LB_GETSELCOUNT, 0, 0) > 0)
299     {
300     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), true);
301    
302     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
303     LB_GETSEL, 0, 0) == 0)
304     {
305     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
306     }
307     else
308     {
309     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
310     }
311    
312     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
313     LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
314     ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
315     {
316     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
317     }
318     else
319     {
320     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
321     }
322     }
323     else
324     {
325     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false);
326     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
327     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
328     }
329     break;
330     }
331     break;
332 douglas 63 }
333     break;
334     }
335    
336     return false;
337     }