ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/RenegadeMapSelector/RenegadeMapSelector.cpp
(Generate patch)

Comparing trunk/RenegadeMapSelector/RenegadeMapSelector.cpp (file contents):
Revision 69 by douglas, 2003-03-11T18:39:01-08:00 vs.
Revision 77 by douglas, 2003-03-13T22:55:21-08:00

# Line 4 | Line 4
4   //
5   // RenegadeMapSelector.cpp
6  
7 + #include "RenegadeMapSelector.h"
8 + #include "RenegadeConfig.h"
9 +
10   #ifdef _WIN32
11   #include <io.h>
12   #include <fcntl.h>
13   #include "resource.h"
14 + #else
15 + #include "gtk/gtk.h"
16   #endif
17  
13 #include "RenegadeMapSelector.h"
14 #include "RenegadeConfig.h"
15
18   bool debug = false;
19   string program;
20   RenegadeConfig* config;
21  
22   #ifndef _WIN32
23   inline string fix(const string& ansi) { return ansi; }
24 < inline void munge(void) { return; }
24 > inline void munge(void) { debug = true; }
25 >
26 > void message(const string& text, const string& title, GtkMessageType type);
27   #else
28   inline string fix(const wstring& wide)
29   {
# Line 65 | Line 69 | inline void munge(void)
69   }
70   #endif
71  
72 + #ifdef _WIN32
73   int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
74  
75   int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
# Line 72 | Line 77 | int WINAPI WinMain(HINSTANCE hInstance,
77   {
78          int argc;
79          unsigned short** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
80 + #else
81 + void selector(void);
82 +
83 + int main(int argc, char* argv[])
84 + {
85 +        gtk_init(&argc, &argv);
86 + #endif
87  
88          string error;
89          string file = "svrcfg_cnc.ini";
# Line 96 | Line 108 | int WINAPI WinMain(HINSTANCE hInstance,
108                          {
109                                  error = "The argument -file must be followed by a filename.";
110  
111 + #ifdef _WIN32
112                                  MessageBox(NULL, error.c_str(), "Bad Arguments", MB_ICONERROR);
113 + #else
114 +                                message(error, "Bad Arguments", GTK_MESSAGE_ERROR);
115 + #endif
116                                  return 1;
117                          }
118                  }
119          }
120  
121 + #ifdef _WIN32
122          GlobalFree(argv);
123 + #endif
124  
125          if (debug) cerr << "file = " << file << "\n";
126  
# Line 111 | Line 129 | int WINAPI WinMain(HINSTANCE hInstance,
129          {
130                  error = "Could not open " + file + ".";
131  
132 + #ifdef _WIN32
133                  MessageBox(NULL, error.c_str(), "Bad File", MB_ICONERROR);
134 + #else
135 +                message(error, "Bad File", GTK_MESSAGE_ERROR);
136 + #endif
137                  return 1;
138          }
139  
140 + #ifdef _WIN32
141          DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAP_SELECTOR), NULL, selector);
142 + #else
143 +        selector();
144 + #endif
145  
146          delete config;
147  
148 <        if (debug) cin.get();
148 > #ifdef _WIN32
149 >        if (debug)
150 >        {
151 >                cout << "Press enter key to exit . . .";
152 >                cin.get();
153 >        }
154 > #endif
155  
156          return 0;
157   }
158  
159 + #ifdef _WIN32
160   int CALLBACK selector(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
161   {
162          switch (uMsg)
163          {
164          case WM_INITDIALOG:
165                  {
133                        RECT rc, rcDlg, rcDesktop;
134
135                        GetWindowRect(GetDesktopWindow(), &rcDesktop);
136                        GetWindowRect(hwndDlg, &rcDlg);
137                        CopyRect(&rc, &rcDesktop);
138
139                        OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
140                        OffsetRect(&rc, -rc.left, -rc.top);
141                        OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
142
143                        SetWindowPos(hwndDlg, HWND_TOP, rcDesktop.left + (rc.right / 2),
144                                rcDesktop.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE);
145                }
146                {
166                          char* directory = new char[MAX_PATH + 1];
167                          GetCurrentDirectory(MAX_PATH + 1, directory);
168                          if (debug) cerr << "directory = " << directory << "\n";
# Line 203 | Line 222 | int CALLBACK selector(HWND hwndDlg, UINT
222                          EndDialog(hwndDlg, wParam);
223                          return true;
224                  case IDC_ADD_MAP:
225 +                        {
226 +                                unsigned count = SendMessage(GetDlgItem(hwndDlg,
227 +                                        IDC_AVAILABLE), LB_GETSELCOUNT, 0, 0);
228 +                                unsigned* additions = new unsigned[count];
229 +
230 +                                SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETSELITEMS,
231 +                                        count, long(additions));
232 +
233 +                                if (debug) cerr << "additions = {\n";
234 +                                for (unsigned index = 0; index < count; index++)
235 +                                {
236 +                                        if (debug) cerr << "   [" << index << "] = " <<
237 +                                                additions[index] << "\n";
238 +
239 +                                        char* name = new char[SendMessage(GetDlgItem(hwndDlg,
240 +                                                IDC_AVAILABLE), LB_GETTEXTLEN, additions[index], 0) + 1
241 +                                                ];
242 +
243 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_GETTEXT,
244 +                                                additions[index], long(name));
245 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
246 +                                                LB_ADDSTRING, 0, long(name));
247 +
248 +                                        delete [] name;
249 +                                }
250 +                                if (debug) cerr << "}\n";
251 +
252 +                                delete [] additions;
253 +
254 +                                SendMessage(GetDlgItem(hwndDlg, IDC_AVAILABLE), LB_SETSEL,
255 +                                        false, -1);
256 +                                EnableWindow(GetDlgItem(hwndDlg, IDC_ADD_MAP), false);
257 +                        }
258                          break;
259                  case IDC_REMOVE_MAP:
260 +                        {
261 +                                unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
262 +                                        LB_GETSELCOUNT, 0, 0);
263 +                                unsigned* removals = new unsigned[count];
264 +
265 +                                SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
266 +                                        count, long(removals));
267 +
268 +                                if (debug) cerr << "removals = {\n";
269 +                                for (unsigned index = count; index > 0; index--)
270 +                                {
271 +                                        if (debug) cerr << "   [" << index - 1 << "] = " <<
272 +                                                removals[index - 1] << "\n";
273 +
274 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
275 +                                                LB_DELETESTRING, removals[index - 1], 0);
276 +                                }
277 +                                if (debug) cerr << "}\n";
278 +
279 +                                delete [] removals;
280 +
281 +                                EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE_MAP), false);
282 +                                EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
283 +                                EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
284 +                        }
285                          break;
286                  case IDC_UP_MAP:
287 +                        {
288 +                                unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
289 +                                        LB_GETSELCOUNT, 0, 0);
290 +                                unsigned* ups = new unsigned[count];
291 +
292 +                                SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
293 +                                        count, long(ups));
294 +
295 +                                unsigned insert = ups[0] - 1;
296 +
297 +                                if (debug) cerr << "ups = {\n";
298 +                                for (unsigned index = 0; index < count; index++)
299 +                                {
300 +                                        if (debug) cerr << "   [" << index << "] = " << ups[index]
301 +                                                << "\n";
302 +
303 +                                        char* up = new char[SendMessage(GetDlgItem(hwndDlg,
304 +                                                IDC_SELECTED), LB_GETTEXTLEN, ups[index], 0) + 1];
305 +
306 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
307 +                                                ups[index], long(up));
308 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
309 +                                                LB_DELETESTRING, ups[index], 0);
310 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
311 +                                                LB_INSERTSTRING, insert, long(up));
312 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL,
313 +                                                true, insert++);
314 +                                }
315 +                                if (debug) cerr << "}\n";
316 +
317 +                                delete [] ups;
318 +
319 +                                if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
320 +                                        LB_GETSEL, 0, 0) == 0)
321 +                                {
322 +                                        EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
323 +                                }
324 +                                else
325 +                                {
326 +                                        EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), false);
327 +                                }
328 +
329 +                                if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
330 +                                        LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
331 +                                        ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
332 +                                {
333 +                                        EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
334 +                                }
335 +                        }
336                          break;
337                  case IDC_DOWN_MAP:
338 +                        {
339 +                                unsigned count = SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
340 +                                        LB_GETSELCOUNT, 0, 0);
341 +                                unsigned* downs = new unsigned[count];
342 +
343 +                                SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETSELITEMS,
344 +                                        count, long(downs));
345 +
346 +                                unsigned insert = downs[count - 1] + 1;
347 +
348 +                                if (debug) cerr << "downs = {\n";
349 +                                for (unsigned index = count; index > 0; index--)
350 +                                {
351 +                                        if (debug) cerr << "   [" << index << "] = " << downs[index
352 +                                                - 1] << "\n";
353 +
354 +                                        char* down = new char[SendMessage(GetDlgItem(hwndDlg,
355 +                                                IDC_SELECTED), LB_GETTEXTLEN, downs[index - 1], 0) +
356 +                                                1];
357 +
358 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_GETTEXT,
359 +                                                downs[index - 1], long(down));
360 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
361 +                                                LB_DELETESTRING, downs[index - 1], 0);
362 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
363 +                                                LB_INSERTSTRING, insert, long(down));
364 +                                        SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED), LB_SETSEL,
365 +                                                true, insert--);
366 +                                }
367 +                                if (debug) cerr << "}\n";
368 +
369 +                                delete [] downs;
370 +
371 +                                if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
372 +                                        LB_GETSEL, 0, 0) == 0)
373 +                                {
374 +                                        EnableWindow(GetDlgItem(hwndDlg, IDC_UP_MAP), true);
375 +                                }
376 +
377 +                                if (SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED),
378 +                                        LB_GETSEL, SendMessage(GetDlgItem(hwndDlg, IDC_SELECTED
379 +                                        ), LB_GETCOUNT, 0, 0) - 1, 0) == 0)
380 +                                {
381 +                                        EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), true);
382 +                                }
383 +                                else
384 +                                {
385 +                                        EnableWindow(GetDlgItem(hwndDlg, IDC_DOWN_MAP), false);
386 +                                }
387 +                        }
388                          break;
389                  case IDC_AVAILABLE:
390                          switch (HIWORD(wParam))
# Line 271 | Line 447 | int CALLBACK selector(HWND hwndDlg, UINT
447  
448          return false;
449   }
450 + #else
451 + static void destruct(GtkWidget* widget, gpointer data)
452 + {
453 +        gtk_main_quit();
454 + }
455 +
456 + static void done(GtkWidget* widget, gpointer data)
457 + {
458 +        if (true)
459 +        {
460 +                message("You need at least one map.", "No Maps",
461 +                        GTK_MESSAGE_WARNING);
462 +        }
463 +        else
464 +        {
465 +                //
466 +
467 +                config->save();
468 +
469 +                gtk_main_quit();
470 +        }
471 + }
472 +
473 + static void mapAdd(GtkWidget* widget, gpointer data)
474 + {
475 +        cout << "STUB: add\n\a" << flush;
476 + }
477 +
478 + static void mapRemove(GtkWidget* widget, gpointer data)
479 + {
480 +        cout << "STUB: remove\n\a" << flush;
481 + }
482 +
483 + static void mapUp(GtkWidget* widget, gpointer data)
484 + {
485 +        cout << "STUB: up\n\a" << flush;
486 + }
487 +
488 + static void mapDown(GtkWidget* widget, gpointer data)
489 + {
490 +        cout << "STUB: down\n\a" << flush;
491 + }
492 +
493 + void selector(void)
494 + {
495 +        GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
496 +
497 +        gtk_window_set_title(GTK_WINDOW(window), "Renegade Map Selector");
498 +        gtk_window_set_resizable(GTK_WINDOW(window), false);
499 +        g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(destruct),
500 +                NULL);
501 +
502 +        GtkWidget* vertical = gtk_vbox_new(false, 0);
503 +        GtkWidget* horizontal = gtk_hbox_new(false, 9);
504 +        GtkWidget* left = gtk_frame_new("Available Maps");
505 +
506 +        gtk_frame_set_label_align(GTK_FRAME(left), 0.5, 0.5);
507 +        gtk_container_set_border_width(GTK_CONTAINER(left), 6);
508 +
509 +        GtkWidget* center = gtk_vbox_new(true, 0);
510 +        GtkWidget* top = gtk_vbutton_box_new();
511 +
512 +        gtk_button_box_set_layout(GTK_BUTTON_BOX(top), GTK_BUTTONBOX_START);
513 +        gtk_box_set_spacing(GTK_BOX(top), 9);
514 +        gtk_container_set_border_width(GTK_CONTAINER(top), 6);
515 +
516 +        GtkWidget* add = gtk_button_new_with_mnemonic("_Add");
517 +
518 +        g_signal_connect(G_OBJECT(add), "clicked", G_CALLBACK(mapAdd), NULL);
519 +        gtk_widget_set_sensitive(add, false);
520 +
521 +        GtkWidget* remove = gtk_button_new_with_mnemonic("_Remove");
522 +
523 +        g_signal_connect(G_OBJECT(remove), "clicked", G_CALLBACK(mapRemove), NULL);
524 +        gtk_widget_set_sensitive(remove, false);
525 +
526 +        GtkWidget* middle = gtk_vbutton_box_new();
527 +
528 +        gtk_button_box_set_layout(GTK_BUTTON_BOX(middle), GTK_BUTTONBOX_END);
529 +        gtk_box_set_spacing(GTK_BOX(middle), 9);
530 +        gtk_container_set_border_width(GTK_CONTAINER(middle), 6);
531 +
532 +        GtkWidget* up = gtk_button_new_with_mnemonic("_Up");
533 +
534 +        g_signal_connect(G_OBJECT(up), "clicked", G_CALLBACK(mapUp), NULL);
535 +        gtk_widget_set_sensitive(up, false);
536 +
537 +        GtkWidget* down = gtk_button_new_with_mnemonic("_Down");
538 +
539 +        g_signal_connect(G_OBJECT(down), "clicked", G_CALLBACK(mapDown), NULL);
540 +        gtk_widget_set_sensitive(down, false);
541 +
542 +        GtkWidget* right = gtk_frame_new("Selected Maps");
543 +
544 +        gtk_frame_set_label_align(GTK_FRAME(right), 0.5, 0.5);
545 +        gtk_container_set_border_width(GTK_CONTAINER(right), 6);
546 +
547 +        GtkWidget* separator = gtk_hseparator_new();
548 +        GtkWidget* bottom = gtk_hbutton_box_new();
549 +
550 +        gtk_button_box_set_layout(GTK_BUTTON_BOX(bottom), GTK_BUTTONBOX_END);
551 +        gtk_box_set_spacing(GTK_BOX(bottom), 9);
552 +        gtk_container_set_border_width(GTK_CONTAINER(bottom), 6);
553 +
554 +        GtkWidget* ok = gtk_button_new_from_stock(GTK_STOCK_OK);
555 +
556 +        g_signal_connect(G_OBJECT(ok), "clicked", G_CALLBACK(done), NULL);
557 +
558 +        GtkWidget* cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
559 +
560 +        g_signal_connect(G_OBJECT(cancel), "clicked", G_CALLBACK(destruct), NULL);
561 +
562 +        // pack
563 +        gtk_container_add(GTK_CONTAINER(vertical), horizontal);
564 +        gtk_container_add(GTK_CONTAINER(horizontal), left);
565 +        gtk_container_add(GTK_CONTAINER(horizontal), center);
566 +        gtk_container_add(GTK_CONTAINER(center), top);
567 +        gtk_container_add(GTK_CONTAINER(top), add);
568 +        gtk_container_add(GTK_CONTAINER(top), remove);
569 +        gtk_container_add(GTK_CONTAINER(center), middle);
570 +        gtk_container_add(GTK_CONTAINER(middle), up);
571 +        gtk_container_add(GTK_CONTAINER(middle), down);
572 +        gtk_container_add(GTK_CONTAINER(horizontal), right);
573 +        gtk_container_add(GTK_CONTAINER(vertical), separator);
574 +        gtk_container_add(GTK_CONTAINER(bottom), ok);
575 +        gtk_container_add(GTK_CONTAINER(bottom), cancel);
576 +        gtk_container_add(GTK_CONTAINER(vertical), bottom);
577 +        gtk_container_add(GTK_CONTAINER(window), vertical);
578 +
579 +        // show
580 +        gtk_widget_show(left);
581 +        gtk_widget_show(add);
582 +        gtk_widget_show(remove);
583 +        gtk_widget_show(top);
584 +        gtk_widget_show(up);
585 +        gtk_widget_show(down);
586 +        gtk_widget_show(middle);
587 +        gtk_widget_show(center);
588 +        gtk_widget_show(right);
589 +        gtk_widget_show(horizontal);
590 +        gtk_widget_show(separator);
591 +        gtk_widget_show(ok);
592 +        gtk_widget_show(cancel);
593 +        gtk_widget_show(bottom);
594 +        gtk_widget_show(vertical);
595 +        gtk_widget_show(window);
596 +
597 +        gtk_main();
598 + }
599 +
600 + void message(const string& text, const string& title, GtkMessageType type)
601 + {
602 +        GtkWidget* box = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, type,
603 +                GTK_BUTTONS_OK, text.c_str());
604 +
605 +        gtk_window_set_title(GTK_WINDOW(box), title.c_str());
606 +
607 +        gtk_dialog_run(GTK_DIALOG(box));
608 +        gtk_widget_destroy(box);
609 + }
610 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines