ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/RenegadeMapSelector/RenegadeMapSelector.cpp
Revision: 74
Committed: 2003-03-13T13:05:06-08:00 (22 years, 3 months ago) by douglas
Original Path: trunk/RenegadeMapSelector/RenegadeMapSelector.cpp
File size: 10902 byte(s)
Log Message:
Implemented up and down buttons!

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 73 inline void munge(void) { debug = true; }
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 73 #ifdef _WIN32
69 douglas 63 int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
70    
71     int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
72     lpCmdLine, int nShowCmd)
73     {
74     int argc;
75     unsigned short** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
76 douglas 73 #else
77     int main(int argc, char* argv[])
78     {
79     #endif
80 douglas 63
81 douglas 68 string error;
82 douglas 67 string file = "svrcfg_cnc.ini";
83    
84 douglas 66 program = fix(argv[0]);
85 douglas 63
86     for (int index = 1; index < argc; index++)
87     {
88 douglas 66 string arg = fix(argv[index]);
89 douglas 63
90     if (arg == "-D")
91     {
92 douglas 67 munge();
93 douglas 63 }
94 douglas 67 else if (arg == "-file")
95     {
96     if (++index < argc)
97     {
98     file = fix(argv[index]);
99     }
100     else
101     {
102     error = "The argument -file must be followed by a filename.";
103    
104 douglas 73 #ifdef _WIN32
105 douglas 67 MessageBox(NULL, error.c_str(), "Bad Arguments", MB_ICONERROR);
106 douglas 73 #endif
107 douglas 67 return 1;
108     }
109     }
110 douglas 63 }
111    
112 douglas 73 #ifdef _WIN32
113 douglas 67 GlobalFree(argv);
114 douglas 73 #endif
115 douglas 63
116 douglas 67 if (debug) cerr << "file = " << file << "\n";
117    
118     config = new RenegadeConfig(file);
119 douglas 68 if (!config->load())
120     {
121     error = "Could not open " + file + ".";
122 douglas 67
123 douglas 73 #ifdef _WIN32
124 douglas 68 MessageBox(NULL, error.c_str(), "Bad File", MB_ICONERROR);
125 douglas 73 #endif
126 douglas 68 return 1;
127     }
128    
129 douglas 73 #ifdef _WIN32
130 douglas 63 DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAP_SELECTOR), NULL, selector);
131 douglas 73 #endif
132 douglas 63
133 douglas 67 delete config;
134    
135 douglas 73 #ifdef _WIN32
136 douglas 70 if (debug)
137     {
138     cout << "Press enter key to exit . . .";
139     cin.get();
140     }
141 douglas 73 #endif
142 douglas 68
143 douglas 63 return 0;
144     }
145    
146 douglas 73 #ifdef _WIN32
147 douglas 63 int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
148     {
149     switch (uMsg)
150     {
151     case WM_INITDIALOG:
152     {
153     RECT rc, rcDlg, rcDesktop;
154    
155     GetWindowRect(GetDesktopWindow(), &rcDesktop);
156     GetWindowRect(hwndDlg, &rcDlg);
157     CopyRect(&rc, &rcDesktop);
158    
159     OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
160     OffsetRect(&rc, -rc.left, -rc.top);
161     OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
162    
163     SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2),
164     rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE);
165     }
166     {
167     char* directory = new char[MAX_PATH + 1];
168     GetCurrentDirectory(MAX_PATH + 1, directory);
169 douglas 66 if (debug) cerr << "directory = " << directory << "\n";
170 douglas 63
171     char* data = new char[MAX_PATH + 1];
172     sprintf(data, "%s\\data\\*.mix", directory);
173     delete [] directory;
174 douglas 66 if (debug) cerr << "data = " << data << "\n";
175 douglas 63
176     DlgDirList(hwndDlg, data, IDC_AVAILABLE, 0, DDL_ARCHIVE);
177 douglas 68 SetCurrentDirectory("..");
178 douglas 63
179     delete [] data;
180     }
181 douglas 69 {
182     vector<string> maps = config->getMaps();
183    
184     for (unsigned index = 0; index < maps.size(); index++)
185     {
186     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0,
187     long(maps[index].c_str()));
188     }
189     }
190 douglas 63 break;
191     case WM_COMMAND:
192     switch (LOWORD(wParam))
193     {
194     case IDOK:
195 douglas 69 if (GetListBoxInfo(GetDlgItem(hwndDlg, IDC_SELECTED)) == 0)
196     {
197     MessageBox(hwndDlg, "You need at least one map.", "No Maps",
198     MB_ICONEXCLAMATION);
199    
200     return false;
201     }
202     else
203     {
204     vector<string> maps;
205    
206     for (unsigned index = 0; index < GetListBoxInfo(GetDlgItem(
207     hwndDlg, IDC_SELECTED)); index++)
208     {
209     char* name = new char[SendMessage(GetDlgItem(hwndDlg,
210     IDC_SELECTED), LB_GETTEXTLEN, index, 0) + 1];
211    
212     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
213     index, long(name));
214     maps.push_back(name);
215    
216     delete [] name;
217     }
218    
219     config->setMaps(maps);
220     }
221 douglas 67 config->save();
222 douglas 63 case IDCANCEL:
223     EndDialog(hwndDlg, wParam);
224     return true;
225 douglas 69 case IDC_ADD_MAP:
226 douglas 70 {
227     unsigned count = SendMessage(GetDlgItem(hwndDlg,
228     IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0);
229     unsigned* additions = new unsigned[count];
230    
231     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELITEMS,
232     count, long(additions));
233    
234     if (debug) cerr << "additions = {\n";
235     for (unsigned index = 0; index < count; index++)
236     {
237     if (debug) cerr << " [" << index << "] = " <<
238     additions[index] << "\n";
239    
240     char* name = new char[SendMessage(GetDlgItem(hwndDlg,
241     IDC_AVAILABLE), LB_GETTEXTLEN, additions[index], 0) + 1
242     ];
243    
244     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXT,
245     additions[index], long(name));
246     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
247     LB_ADDSTRING, 0, long(name));
248    
249     delete [] name;
250     }
251     if (debug) cerr << "}\n";
252    
253     delete [] additions;
254    
255     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_SETSEL,
256     false, -1);
257     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false);
258     }
259 douglas 69 break;
260     case IDC_REMOVE_MAP:
261 douglas 70 {
262     unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
263     LB_GETSELCOUNT, 0, 0);
264     unsigned* removals = new unsigned[count];
265    
266     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
267     count, long(removals));
268    
269     if (debug) cerr << "removals = {\n";
270     for (unsigned index = count; index > 0; index--)
271     {
272     if (debug) cerr << " [" << index - 1 << "] = " <<
273     removals[index - 1] << "\n";
274    
275     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
276     LB_DELETESTRING, removals[index - 1], 0);
277     }
278     if (debug) cerr << "}\n";
279    
280     delete [] removals;
281    
282     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false);
283     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
284     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
285     }
286 douglas 69 break;
287     case IDC_UP_MAP:
288 douglas 74 {
289     unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
290     LB_GETSELCOUNT, 0, 0);
291     unsigned* ups = new unsigned[count];
292    
293     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
294     count, long(ups));
295    
296     unsigned insert = ups[0] - 1;
297    
298     if (debug) cerr << "ups = {\n";
299     for (unsigned index = 0; index < count; index++)
300     {
301     if (debug) cerr << " [" << index << "] = " << ups[index]
302     << "\n";
303    
304     char* up = new char[SendMessage(GetDlgItem(hwndDlg,
305     IDC_SELECTED), LB_GETTEXTLEN, ups[index], 0) + 1];
306    
307     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
308     ups[index], long(up));
309     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
310     LB_DELETESTRING, ups[index], 0);
311     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
312     LB_INSERTSTRING, insert, long(up));
313     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL,
314     true, insert++);
315     }
316     if (debug) cerr << "}\n";
317    
318     delete [] ups;
319    
320     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
321     LB_GETSEL, 0, 0) == 0)
322     {
323     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
324     }
325     else
326     {
327     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
328     }
329    
330     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
331     LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
332     ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
333     {
334     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
335     }
336     }
337 douglas 69 break;
338     case IDC_DOWN_MAP:
339 douglas 74 {
340     unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
341     LB_GETSELCOUNT, 0, 0);
342     unsigned* downs = new unsigned[count];
343    
344     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
345     count, long(downs));
346    
347     unsigned insert = downs[count - 1] + 1;
348    
349     if (debug) cerr << "downs = {\n";
350     for (unsigned index = count; index > 0; index--)
351     {
352     if (debug) cerr << " [" << index << "] = " << downs[index
353     - 1] << "\n";
354    
355     char* down = new char[SendMessage(GetDlgItem(hwndDlg,
356     IDC_SELECTED), LB_GETTEXTLEN, downs[index - 1], 0) +
357     1];
358    
359     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
360     downs[index - 1], long(down));
361     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
362     LB_DELETESTRING, downs[index - 1], 0);
363     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
364     LB_INSERTSTRING, insert, long(down));
365     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL,
366     true, insert--);
367     }
368     if (debug) cerr << "}\n";
369    
370     delete [] downs;
371    
372     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
373     LB_GETSEL, 0, 0) == 0)
374     {
375     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
376     }
377    
378     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
379     LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
380     ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
381     {
382     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
383     }
384     else
385     {
386     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
387     }
388     }
389 douglas 69 break;
390     case IDC_AVAILABLE:
391     switch (HIWORD(wParam))
392     {
393     case LBN_SELCHANGE:
394     if (SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE),
395     LB_GETSELCOUNT, 0, 0) > 0)
396     {
397     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), true);
398     }
399     else
400     {
401     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false);
402     }
403     break;
404     }
405     break;
406     case IDC_SELECTED:
407     switch (HIWORD(wParam))
408     {
409     case LBN_SELCHANGE:
410     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
411     LB_GETSELCOUNT, 0, 0) > 0)
412     {
413     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), true);
414    
415     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
416     LB_GETSEL, 0, 0) == 0)
417     {
418     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
419     }
420     else
421     {
422     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
423     }
424    
425     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
426     LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
427     ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
428     {
429     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
430     }
431     else
432     {
433     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
434     }
435     }
436     else
437     {
438     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false);
439     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
440     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
441     }
442     break;
443     }
444     break;
445 douglas 63 }
446     break;
447     }
448    
449     return false;
450     }
451 douglas 73 #endif