ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/RenegadeMapSelector/RenegadeMapSelector.cpp
Revision: 368
Committed: 2008-08-23T02:44:00-07:00 (16 years, 9 months ago) by douglas
File size: 11650 byte(s)
Log Message:
Rearranged everything else.

File Contents

# User Rev Content
1 douglas 63 // Renegade Map Selector
2     //
3     // Douglas Thrift
4     //
5     // RenegadeMapSelector.cpp
6    
7 douglas 75 #include "RenegadeMapSelector.h"
8     #include "RenegadeConfig.h"
9    
10 douglas 66 #ifdef _WIN32
11 douglas 86
12 douglas 66 #include <io.h>
13     #include <fcntl.h>
14 douglas 67 #include "resource.h"
15 douglas 86
16 douglas 75 #else
17 douglas 86
18     #include "MapSelector.h"
19    
20 douglas 66 #endif
21    
22 douglas 63 bool debug = false;
23     string program;
24 douglas 67 RenegadeConfig* config;
25 douglas 63
26 douglas 66 #ifndef _WIN32
27 douglas 86
28 douglas 66 inline string fix(const string& ansi) { return ansi; }
29 douglas 73 inline void munge(void) { debug = true; }
30 douglas 77
31 douglas 66 #else
32 douglas 86
33 douglas 66 inline string fix(const wstring& wide)
34 douglas 63 {
35     char* buffer = new char[wide.length() + 1];
36    
37     WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, buffer, wide.length() + 1,
38     NULL, NULL);
39    
40     string ansi = buffer;
41     delete [] buffer;
42    
43     return ansi;
44     }
45    
46 douglas 67 inline void munge(void)
47 douglas 66 {
48 douglas 67 if (debug) return;
49    
50 douglas 66 AllocConsole();
51     SetConsoleTitle("Renegade Map Selector");
52    
53     int hin = _open_osfhandle(long(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT);
54     int hout = _open_osfhandle(long(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT);
55     int herr = _open_osfhandle(long(GetStdHandle(STD_ERROR_HANDLE)), _O_TEXT);
56    
57     FILE* fin = _fdopen(hin, "r");
58     FILE* fout = _fdopen(hout, "w");
59     FILE* ferr = _fdopen(herr, "w");
60    
61     *stdin = *fin;
62     *stdout = *fout;
63     *stderr = *ferr;
64    
65     setvbuf(stdin, NULL, _IONBF, 0);
66     setvbuf(stdout, NULL, _IONBF, 0);
67     setvbuf(stderr, NULL, _IONBF, 0);
68    
69     cin.sync_with_stdio();
70     cout.sync_with_stdio();
71     cerr.sync_with_stdio();
72 douglas 67
73     debug = true;
74 douglas 66 }
75 douglas 86
76 douglas 66 #endif
77    
78 douglas 73 #ifdef _WIN32
79 douglas 63 int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
80    
81     int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
82     lpCmdLine, int nShowCmd)
83     {
84     int argc;
85     unsigned short** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
86 douglas 73 #else
87 douglas 77 void selector(void);
88    
89 douglas 73 int main(int argc, char* argv[])
90     {
91 douglas 84 if (!gtk_init_check(&argc, &argv))
92     {
93     cerr << argv[0] << ": Cannot open display, need a clue-by-four?\n";
94    
95     return 1;
96     }
97 douglas 73 #endif
98 douglas 63
99 douglas 68 string error;
100 douglas 67 string file = "svrcfg_cnc.ini";
101    
102 douglas 66 program = fix(argv[0]);
103 douglas 63
104     for (int index = 1; index < argc; index++)
105     {
106 douglas 66 string arg = fix(argv[index]);
107 douglas 63
108     if (arg == "-D")
109     {
110 douglas 67 munge();
111 douglas 63 }
112 douglas 67 else if (arg == "-file")
113     {
114     if (++index < argc)
115     {
116     file = fix(argv[index]);
117     }
118     else
119     {
120     error = "The argument -file must be followed by a filename.";
121    
122 douglas 94 cerr << program << ": " << error << "\n";
123 douglas 73 #ifdef _WIN32
124 douglas 67 MessageBox(NULL, error.c_str(), "Bad Arguments", MB_ICONERROR);
125 douglas 77 #else
126 douglas 86 message(NULL, error, "Bad Arguments", GTK_MESSAGE_ERROR);
127 douglas 73 #endif
128 douglas 67 return 1;
129     }
130     }
131 douglas 89 else
132     {
133     error = "The argument " + arg + " is not a valid argument.";
134    
135 douglas 94 cerr << program << ": " << error << "\n";
136 douglas 89 #ifdef _WIN32
137 douglas 90 MessageBox(NULL, error.c_str(), "Unknown Arguments", MB_ICONWARNING
138     );
139 douglas 89 #else
140     message(NULL, error, "Unknown Arguments", GTK_MESSAGE_WARNING);
141     #endif
142     }
143 douglas 63 }
144    
145 douglas 73 #ifdef _WIN32
146 douglas 67 GlobalFree(argv);
147 douglas 73 #endif
148 douglas 63
149 douglas 67 if (debug) cerr << "file = " << file << "\n";
150    
151     config = new RenegadeConfig(file);
152 douglas 68 if (!config->load())
153     {
154     error = "Could not open " + file + ".";
155 douglas 67
156 douglas 94 cerr << program << ": " << error << "\n";
157 douglas 73 #ifdef _WIN32
158 douglas 68 MessageBox(NULL, error.c_str(), "Bad File", MB_ICONERROR);
159 douglas 77 #else
160 douglas 86 message(NULL, error, "Bad File", GTK_MESSAGE_ERROR);
161 douglas 73 #endif
162 douglas 68 return 1;
163     }
164    
165 douglas 73 #ifdef _WIN32
166 douglas 63 DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAP_SELECTOR), NULL, selector);
167 douglas 75 #else
168 douglas 86 MapSelector selector;
169 douglas 73 #endif
170 douglas 63
171 douglas 67 delete config;
172    
173 douglas 73 #ifdef _WIN32
174 douglas 70 if (debug)
175     {
176     cout << "Press enter key to exit . . .";
177     cin.get();
178     }
179 douglas 73 #endif
180 douglas 68
181 douglas 63 return 0;
182     }
183    
184 douglas 73 #ifdef _WIN32
185 douglas 86
186 douglas 63 int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
187     {
188     switch (uMsg)
189     {
190     case WM_INITDIALOG:
191     {
192 douglas 78 RECT rc, rcDlg, rcDesktop;
193    
194     GetWindowRect(GetDesktopWindow(), &rcDesktop);
195     GetWindowRect(hwndDlg, &rcDlg);
196     CopyRect(&rc, &rcDesktop);
197    
198     OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
199     OffsetRect(&rc, -rc.left, -rc.top);
200     OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
201    
202     SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2),
203     rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE);
204     }
205     {
206 douglas 63 char* directory = new char[MAX_PATH + 1];
207     GetCurrentDirectory(MAX_PATH + 1, directory);
208 douglas 66 if (debug) cerr << "directory = " << directory << "\n";
209 douglas 63
210 douglas 88 char* data = new char[strlen(directory) + 12];
211 douglas 63 sprintf(data, "%s\\data\\*.mix", directory);
212     delete [] directory;
213 douglas 66 if (debug) cerr << "data = " << data << "\n";
214 douglas 63
215     DlgDirList(hwndDlg, data, IDC_AVAILABLE, 0, DDL_ARCHIVE);
216 douglas 68 SetCurrentDirectory("..");
217 douglas 63
218     delete [] data;
219     }
220 douglas 69 {
221     vector<string> maps = config->getMaps();
222    
223     for (unsigned index = 0; index < maps.size(); index++)
224     {
225     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_ADDSTRING, 0,
226     long(maps[index].c_str()));
227     }
228     }
229 douglas 63 break;
230     case WM_COMMAND:
231     switch (LOWORD(wParam))
232     {
233     case IDOK:
234 douglas 69 if (GetListBoxInfo(GetDlgItem(hwndDlg, IDC_SELECTED)) == 0)
235     {
236     MessageBox(hwndDlg, "You need at least one map.", "No Maps",
237 douglas 90 MB_ICONINFORMATION);
238 douglas 69
239     return false;
240     }
241     else
242     {
243     vector<string> maps;
244    
245     for (unsigned index = 0; index < GetListBoxInfo(GetDlgItem(
246     hwndDlg, IDC_SELECTED)); index++)
247     {
248     char* name = new char[SendMessage(GetDlgItem(hwndDlg,
249     IDC_SELECTED), LB_GETTEXTLEN, index, 0) + 1];
250    
251     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
252     index, long(name));
253     maps.push_back(name);
254    
255     delete [] name;
256     }
257    
258     config->setMaps(maps);
259     }
260 douglas 67 config->save();
261 douglas 63 case IDCANCEL:
262     EndDialog(hwndDlg, wParam);
263     return true;
264 douglas 69 case IDC_ADD_MAP:
265 douglas 70 {
266     unsigned count = SendMessage(GetDlgItem(hwndDlg,
267     IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0);
268     unsigned* additions = new unsigned[count];
269    
270     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELITEMS,
271     count, long(additions));
272    
273     if (debug) cerr << "additions = {\n";
274     for (unsigned index = 0; index < count; index++)
275     {
276     if (debug) cerr << " [" << index << "] = " <<
277     additions[index] << "\n";
278    
279     char* name = new char[SendMessage(GetDlgItem(hwndDlg,
280     IDC_AVAILABLE), LB_GETTEXTLEN, additions[index], 0) + 1
281     ];
282    
283     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXT,
284     additions[index], long(name));
285     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
286     LB_ADDSTRING, 0, long(name));
287    
288     delete [] name;
289     }
290     if (debug) cerr << "}\n";
291    
292     delete [] additions;
293    
294     SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_SETSEL,
295     false, -1);
296     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false);
297     }
298 douglas 69 break;
299     case IDC_REMOVE_MAP:
300 douglas 70 {
301     unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
302     LB_GETSELCOUNT, 0, 0);
303     unsigned* removals = new unsigned[count];
304    
305     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
306     count, long(removals));
307    
308     if (debug) cerr << "removals = {\n";
309     for (unsigned index = count; index > 0; index--)
310     {
311     if (debug) cerr << " [" << index - 1 << "] = " <<
312     removals[index - 1] << "\n";
313    
314     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
315     LB_DELETESTRING, removals[index - 1], 0);
316     }
317     if (debug) cerr << "}\n";
318    
319     delete [] removals;
320    
321     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false);
322     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
323     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
324     }
325 douglas 69 break;
326     case IDC_UP_MAP:
327 douglas 74 {
328     unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
329     LB_GETSELCOUNT, 0, 0);
330     unsigned* ups = new unsigned[count];
331    
332     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
333     count, long(ups));
334    
335     unsigned insert = ups[0] - 1;
336    
337     if (debug) cerr << "ups = {\n";
338     for (unsigned index = 0; index < count; index++)
339     {
340     if (debug) cerr << " [" << index << "] = " << ups[index]
341     << "\n";
342    
343     char* up = new char[SendMessage(GetDlgItem(hwndDlg,
344     IDC_SELECTED), LB_GETTEXTLEN, ups[index], 0) + 1];
345    
346     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
347     ups[index], long(up));
348     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
349     LB_DELETESTRING, ups[index], 0);
350     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
351     LB_INSERTSTRING, insert, long(up));
352     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL,
353     true, insert++);
354     }
355     if (debug) cerr << "}\n";
356    
357     delete [] ups;
358    
359     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
360     LB_GETSEL, 0, 0) == 0)
361     {
362     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
363     }
364     else
365     {
366     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
367     }
368    
369     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
370     LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
371     ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
372     {
373     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
374     }
375     }
376 douglas 69 break;
377     case IDC_DOWN_MAP:
378 douglas 74 {
379     unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
380     LB_GETSELCOUNT, 0, 0);
381     unsigned* downs = new unsigned[count];
382    
383     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
384     count, long(downs));
385    
386     unsigned insert = downs[count - 1] + 1;
387    
388     if (debug) cerr << "downs = {\n";
389     for (unsigned index = count; index > 0; index--)
390     {
391 douglas 96 if (debug) cerr << " [" << index - 1 << "] = " << downs[
392     index - 1] << "\n";
393 douglas 74
394     char* down = new char[SendMessage(GetDlgItem(hwndDlg,
395     IDC_SELECTED), LB_GETTEXTLEN, downs[index - 1], 0) +
396     1];
397    
398     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
399     downs[index - 1], long(down));
400     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
401     LB_DELETESTRING, downs[index - 1], 0);
402     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
403     LB_INSERTSTRING, insert, long(down));
404     SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL,
405     true, insert--);
406     }
407     if (debug) cerr << "}\n";
408    
409     delete [] downs;
410    
411     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
412     LB_GETSEL, 0, 0) == 0)
413     {
414     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
415     }
416    
417     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
418     LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
419     ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
420     {
421     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
422     }
423     else
424     {
425     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
426     }
427     }
428 douglas 69 break;
429     case IDC_AVAILABLE:
430     switch (HIWORD(wParam))
431     {
432     case LBN_SELCHANGE:
433     if (SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE),
434     LB_GETSELCOUNT, 0, 0) > 0)
435     {
436     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), true);
437     }
438     else
439     {
440     EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false);
441     }
442     break;
443     }
444     break;
445     case IDC_SELECTED:
446     switch (HIWORD(wParam))
447     {
448     case LBN_SELCHANGE:
449     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
450     LB_GETSELCOUNT, 0, 0) > 0)
451     {
452     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), true);
453    
454     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
455     LB_GETSEL, 0, 0) == 0)
456     {
457     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
458     }
459     else
460     {
461     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
462     }
463    
464     if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
465     LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
466     ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
467     {
468     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
469     }
470     else
471     {
472     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
473     }
474     }
475     else
476     {
477     EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false);
478     EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
479     EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
480     }
481     break;
482     }
483     break;
484 douglas 63 }
485     break;
486     }
487    
488     return false;
489     }
490 douglas 77
491 douglas 73 #endif