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

File Contents

# Content
1 // Renegade Map Selector
2 //
3 // Douglas Thrift
4 //
5 // MapSelector.cpp
6
7 #include "MapSelector.h"
8
9 #ifndef _WIN32
10
11 MapSelector::MapSelector()
12 {
13 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
14
15 gtk_window_set_title(GTK_WINDOW(window), "Renegade Map Selector");
16 gtk_window_set_resizable(GTK_WINDOW(window), false);
17 g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(destruct),
18 this);
19
20 GtkWidget* vertical = gtk_vbox_new(false, 0);
21 GtkWidget* horizontal = gtk_hbox_new(false, 9);
22 GtkWidget* left = gtk_frame_new("Available Maps");
23
24 gtk_frame_set_label_align(GTK_FRAME(left), 0.5, 0.5);
25 gtk_container_set_border_width(GTK_CONTAINER(left), 6);
26
27 GtkWidget* leftScroll = gtk_scrolled_window_new(NULL, NULL);
28
29 gtk_container_set_border_width(GTK_CONTAINER(leftScroll), 6);
30 gtk_widget_set_size_request(leftScroll, 150, 200);
31 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(leftScroll),
32 GTK_SHADOW_IN);
33
34 available = gtk_tree_view_new();
35
36 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(available), false);
37 setupAvailable();
38
39 GtkWidget* center = gtk_vbox_new(true, 0);
40 GtkWidget* top = gtk_vbutton_box_new();
41
42 gtk_button_box_set_layout(GTK_BUTTON_BOX(top), GTK_BUTTONBOX_START);
43 gtk_box_set_spacing(GTK_BOX(top), 9);
44 gtk_container_set_border_width(GTK_CONTAINER(top), 6);
45
46 add = gtk_button_new_with_mnemonic("_Add");
47
48 g_signal_connect(G_OBJECT(add), "clicked", G_CALLBACK(mapAdd), this);
49 gtk_widget_set_sensitive(add, false);
50
51 remove = gtk_button_new_with_mnemonic("_Remove");
52
53 g_signal_connect(G_OBJECT(remove), "clicked", G_CALLBACK(mapRemove), this);
54 gtk_widget_set_sensitive(remove, false);
55
56 GtkWidget* middle = gtk_vbutton_box_new();
57
58 gtk_button_box_set_layout(GTK_BUTTON_BOX(middle), GTK_BUTTONBOX_END);
59 gtk_box_set_spacing(GTK_BOX(middle), 9);
60 gtk_container_set_border_width(GTK_CONTAINER(middle), 6);
61
62 up = gtk_button_new_with_mnemonic("_Up");
63
64 g_signal_connect(G_OBJECT(up), "clicked", G_CALLBACK(mapUp), this);
65 gtk_widget_set_sensitive(up, false);
66
67 down = gtk_button_new_with_mnemonic("_Down");
68
69 g_signal_connect(G_OBJECT(down), "clicked", G_CALLBACK(mapDown), this);
70 gtk_widget_set_sensitive(down, false);
71
72 GtkWidget* right = gtk_frame_new("Selected Maps");
73
74 gtk_frame_set_label_align(GTK_FRAME(right), 0.5, 0.5);
75 gtk_container_set_border_width(GTK_CONTAINER(right), 6);
76
77 GtkWidget* rightScroll = gtk_scrolled_window_new(NULL, NULL);
78
79 gtk_container_set_border_width(GTK_CONTAINER(rightScroll), 6);
80 gtk_widget_set_size_request(rightScroll, 150, 200);
81 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(rightScroll),
82 GTK_SHADOW_IN);
83
84 selected = gtk_tree_view_new();
85
86 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(selected), false);
87 setupSelected();
88
89 GtkWidget* separator = gtk_hseparator_new();
90 GtkWidget* bottom = gtk_hbutton_box_new();
91
92 gtk_button_box_set_layout(GTK_BUTTON_BOX(bottom), GTK_BUTTONBOX_END);
93 gtk_box_set_spacing(GTK_BOX(bottom), 9);
94 gtk_container_set_border_width(GTK_CONTAINER(bottom), 6);
95
96 GtkWidget* ok = gtk_button_new_from_stock(GTK_STOCK_OK);
97
98 g_signal_connect(G_OBJECT(ok), "clicked", G_CALLBACK(done), this);
99 GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT);
100
101 GtkWidget* cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
102
103 g_signal_connect(G_OBJECT(cancel), "clicked", G_CALLBACK(destruct), this);
104
105 // pack
106 gtk_container_add(GTK_CONTAINER(vertical), horizontal);
107 gtk_container_add(GTK_CONTAINER(horizontal), left);
108 gtk_container_add(GTK_CONTAINER(left), leftScroll);
109 gtk_container_add(GTK_CONTAINER(leftScroll), available);
110 gtk_container_add(GTK_CONTAINER(horizontal), center);
111 gtk_container_add(GTK_CONTAINER(center), top);
112 gtk_container_add(GTK_CONTAINER(top), add);
113 gtk_container_add(GTK_CONTAINER(top), remove);
114 gtk_container_add(GTK_CONTAINER(center), middle);
115 gtk_container_add(GTK_CONTAINER(middle), up);
116 gtk_container_add(GTK_CONTAINER(middle), down);
117 gtk_container_add(GTK_CONTAINER(horizontal), right);
118 gtk_container_add(GTK_CONTAINER(right), rightScroll);
119 gtk_container_add(GTK_CONTAINER(rightScroll), selected);
120 gtk_container_add(GTK_CONTAINER(vertical), separator);
121 gtk_container_add(GTK_CONTAINER(bottom), ok);
122 gtk_container_add(GTK_CONTAINER(bottom), cancel);
123 gtk_container_add(GTK_CONTAINER(vertical), bottom);
124 gtk_container_add(GTK_CONTAINER(window), vertical);
125
126 // show
127 gtk_widget_show(available);
128 gtk_widget_show(leftScroll);
129 gtk_widget_show(left);
130 gtk_widget_show(add);
131 gtk_widget_show(remove);
132 gtk_widget_show(top);
133 gtk_widget_show(up);
134 gtk_widget_show(down);
135 gtk_widget_show(middle);
136 gtk_widget_show(center);
137 gtk_widget_show(selected);
138 gtk_widget_show(rightScroll);
139 gtk_widget_show(right);
140 gtk_widget_show(horizontal);
141 gtk_widget_show(separator);
142 gtk_widget_show(ok);
143 gtk_widget_show(cancel);
144 gtk_widget_show(bottom);
145 gtk_widget_show(vertical);
146 gtk_widget_show(window);
147
148 // stuff
149 gtk_widget_grab_default(ok);
150 gtk_widget_grab_focus(ok);
151
152 GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
153 available));
154
155 gtk_tree_selection_unselect_all(selection);
156
157 gtk_main();
158 }
159
160 void MapSelector::setupAvailable()
161 {
162 GtkListStore* store = gtk_list_store_new(1, G_TYPE_STRING);
163
164 char* directory = new char[MAXPATHLEN + 1];
165 getcwd(directory, MAXPATHLEN + 1);
166 if (debug) cerr << "directory = " << directory << "\n";
167
168 char* data = new char[strlen(directory) + 6];
169 sprintf(data, "%s/data", directory);
170 delete [] directory;
171 if (debug) cerr << "data = " << data << "\n";
172
173 DIR* dir = opendir(data);
174 delete [] data;
175
176 GtkTreeIter itor;
177 dirent* file;
178 while ((file = readdir(dir)) != NULL)
179 {
180 string name = file->d_name;
181
182 if (name.rfind(".mix") == name.length() - 4)
183 {
184 gtk_list_store_append(store, &itor);
185 gtk_list_store_set(store, &itor, 0, name.c_str(), -1);
186 }
187 }
188
189 closedir(dir);
190
191 gtk_tree_view_set_model(GTK_TREE_VIEW(available), GTK_TREE_MODEL(store));
192
193 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
194 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes(NULL,
195 renderer, "text", 0, NULL);
196
197 gtk_tree_view_append_column(GTK_TREE_VIEW(available), column);
198
199 GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
200 available));
201
202 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
203 g_signal_connect(G_OBJECT(selection), "changed", G_CALLBACK(changeAvailable
204 ), this);
205 }
206
207 void MapSelector::setupSelected()
208 {
209 GtkListStore* store = gtk_list_store_new(1, G_TYPE_STRING);
210
211 vector<string> maps = config->getMaps();
212
213 GtkTreeIter itor;
214 for (unsigned index = 0; index < maps.size(); index++)
215 {
216 gtk_list_store_append(store, &itor);
217 gtk_list_store_set(store, &itor, 0, maps[index].c_str(), -1);
218 }
219
220 gtk_tree_view_set_model(GTK_TREE_VIEW(selected), GTK_TREE_MODEL(store));
221
222 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
223 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes(NULL,
224 renderer, "text", 0, NULL);
225
226 gtk_tree_view_append_column(GTK_TREE_VIEW(selected), column);
227
228 GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
229 selected));
230
231 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
232 g_signal_connect(G_OBJECT(selection), "changed", G_CALLBACK(changeSelected),
233 this);
234 }
235
236 void destruct(GtkWidget* widget, MapSelector* data)
237 {
238 gtk_main_quit();
239 }
240
241 void done(GtkWidget* widget, MapSelector* data)
242 {
243 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(data->selected)
244 );
245
246 GtkTreeIter itor;
247 if (!gtk_tree_model_get_iter_first(store, &itor))
248 {
249 message(GTK_WINDOW(data->window), "You need at least one map.",
250 "No Maps", GTK_MESSAGE_INFO);
251 }
252 else
253 {
254 vector<string> maps;
255
256 do
257 {
258 char* name;
259
260 gtk_tree_model_get(store, &itor, 0, &name, -1);
261 maps.push_back(name);
262
263 g_free(name);
264 }
265 while (gtk_tree_model_iter_next(store, &itor));
266
267 config->setMaps(maps);
268
269 config->save();
270
271 gtk_main_quit();
272 }
273 }
274
275 void changeAvailable(GtkTreeSelection* selection, MapSelector* data)
276 {
277 if (gtk_tree_selection_count_selected_rows(selection) > 0)
278 {
279 gtk_widget_set_sensitive(data->add, true);
280 }
281 else
282 {
283 gtk_widget_set_sensitive(data->add, false);
284 }
285 }
286
287 void changeSelected(GtkTreeSelection* selection, MapSelector* data)
288 {
289 if (gtk_tree_selection_count_selected_rows(selection) > 0)
290 {
291 gtk_widget_set_sensitive(data->remove, true);
292
293 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(
294 data->selected));
295 GtkTreeIter first, last;
296
297 gtk_tree_model_get_iter_first(store, &first);
298 gtk_tree_model_iter_nth_child(store, &last, NULL,
299 gtk_tree_model_iter_n_children(store, NULL) - 1);
300
301 if (!gtk_tree_selection_iter_is_selected(selection, &first))
302 {
303 gtk_widget_set_sensitive(data->up, true);
304 }
305 else
306 {
307 gtk_widget_set_sensitive(data->up, false);
308 }
309
310 if (!gtk_tree_selection_iter_is_selected(selection, &last))
311 {
312 gtk_widget_set_sensitive(data->down, true);
313 }
314 else
315 {
316 gtk_widget_set_sensitive(data->down, false);
317 }
318 }
319 else
320 {
321 gtk_widget_set_sensitive(data->remove, false);
322 gtk_widget_set_sensitive(data->up, false);
323 gtk_widget_set_sensitive(data->down, false);
324 }
325 }
326
327 void mapAdd(GtkWidget* widget, MapSelector* data)
328 {
329 GtkTreeSelection* additions = gtk_tree_view_get_selection(GTK_TREE_VIEW(
330 data->available));
331
332 if (debug) cerr << "additions = {\n";
333
334 data->counter = 0;
335 gtk_tree_selection_selected_foreach(additions, mapAddEach, data);
336
337 if (debug) cerr << "}\n";
338
339 gtk_tree_selection_unselect_all(additions);
340 }
341
342 void mapAddEach(GtkTreeModel* model, GtkTreePath* path, GtkTreeIter* iter,
343 MapSelector* data)
344 {
345 if (debug)
346 {
347 char* number = gtk_tree_model_get_string_from_iter(model, iter);
348
349 cerr << " [" << data->counter++ << "] = " << number << "\n";
350
351 g_free(number);
352 }
353
354 char* addition;
355
356 gtk_tree_model_get(model, iter, 0, &addition, -1);
357
358 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(data->selected)
359 );
360 GtkTreeIter itor;
361
362 gtk_list_store_append(GTK_LIST_STORE(store), &itor);
363 gtk_list_store_set(GTK_LIST_STORE(store), &itor, 0, addition, -1);
364
365 g_free(addition);
366 }
367
368 void mapRemove(GtkWidget* widget, MapSelector* data)
369 {
370 GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
371 data->selected));
372 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(data->selected)
373 );
374 GList* removals = gtk_tree_selection_get_selected_rows(selection, &store);
375
376 if (debug) cerr << "removals = {\n";
377
378 removals = g_list_reverse(removals);
379 data->counter = g_list_length(removals) - 1;
380 g_list_foreach(removals, mapRemoveEach, data);
381
382 if (debug) cerr << "}\n";
383
384 g_list_free(removals);
385 gtk_tree_selection_unselect_all(selection);
386 }
387
388 void mapRemoveEach(GtkTreePath* path, MapSelector* data)
389 {
390 if (debug)
391 {
392 char* number = gtk_tree_path_to_string(path);
393
394 cerr << " [" << data->counter-- << "] = " << number << "\n";
395
396 g_free(number);
397 }
398
399 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(data->selected)
400 );
401 GtkTreeIter itor;
402
403 gtk_tree_model_get_iter(store, &itor, path);
404 gtk_list_store_remove(GTK_LIST_STORE(store), &itor);
405 }
406
407 void mapUp(GtkWidget* widget, MapSelector* data)
408 {
409 GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
410 data->selected));
411 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(data->selected)
412 );
413 GList* ups = gtk_tree_selection_get_selected_rows(selection, &store);
414
415 if (debug) cerr << "ups = {\n";
416
417 data->counter = 0;
418 data->insert = 0;
419 g_list_foreach(ups, mapUpEach, data);
420
421 if (debug) cerr << "}\n";
422
423 g_list_free(ups);
424 }
425
426 void mapUpEach(GtkTreePath* path, MapSelector* data)
427 {
428 if (debug)
429 {
430 char* number = gtk_tree_path_to_string(path);
431
432 cerr << " [" << data->counter++ << "] = " << number << "\n";
433
434 g_free(number);
435 }
436
437 if (data->insert == 0)
438 {
439 int* number = gtk_tree_path_get_indices(path);
440
441 data->insert = *number - 1;
442 }
443
444 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(data->selected)
445 );
446 GtkTreeIter itor;
447 char* up;
448
449 gtk_tree_model_get_iter(store, &itor, path);
450 gtk_tree_model_get(store, &itor, 0, &up, -1);
451 gtk_list_store_remove(GTK_LIST_STORE(store), &itor);
452 gtk_list_store_insert(GTK_LIST_STORE(store), &itor, data->insert++);
453 gtk_list_store_set(GTK_LIST_STORE(store), &itor, 0, up, -1);
454
455 GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
456 data->selected));
457
458 gtk_tree_selection_select_iter(selection, &itor);
459 g_free(up);
460 }
461
462 void mapDown(GtkWidget* widget, MapSelector* data)
463 {
464 GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
465 data->selected));
466 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(data->selected)
467 );
468 GList* downs = gtk_tree_selection_get_selected_rows(selection, &store);
469
470 if (debug) cerr << "downs = {\n";
471
472 downs = g_list_reverse(downs);
473 data->counter = g_list_length(downs) - 1;
474 data->insert = 0;
475 g_list_foreach(downs, mapDownEach, data);
476
477 if (debug) cerr << "}\n";
478
479 g_list_free(downs);
480 }
481
482 void mapDownEach(GtkTreePath* path, MapSelector* data)
483 {
484 if (debug)
485 {
486 char* number = gtk_tree_path_to_string(path);
487
488 cerr << " [" << data->counter-- << "] = " << number << "\n";
489
490 g_free(number);
491 }
492
493 if (data->insert == 0)
494 {
495 int* number = gtk_tree_path_get_indices(path);
496
497 data->insert = *number + 1;
498 }
499
500 GtkTreeModel* store = gtk_tree_view_get_model(GTK_TREE_VIEW(data->selected)
501 );
502 GtkTreeIter itor;
503 char* down;
504
505 gtk_tree_model_get_iter(store, &itor, path);
506 gtk_tree_model_get(store, &itor, 0, &down, -1);
507 gtk_list_store_remove(GTK_LIST_STORE(store), &itor);
508 gtk_list_store_insert(GTK_LIST_STORE(store), &itor, data->insert--);
509 gtk_list_store_set(GTK_LIST_STORE(store), &itor, 0, down, -1);
510
511 GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
512 data->selected));
513
514 gtk_tree_selection_select_iter(selection, &itor);
515 g_free(down);
516 }
517
518 #endif