From 16fcfdc693d583306b431349e99d18c6b822f1c3 Mon Sep 17 00:00:00 2001 From: Dave White Date: Fri, 18 Jun 2004 00:12:23 +0000 Subject: [PATCH] added basic clipboard functionality; fixed bug in generating random maps --- data/game.cfg | 7 ++++-- src/clipboard.cpp | 44 ++++++++++++++++++++++++++++++++++++ src/clipboard.hpp | 4 ++++ src/config.cpp | 7 +++--- src/mapgen.cpp | 16 +++++++++++++ src/race.cpp | 9 +++++++- src/widgets/textbox.cpp | 50 +++++++++++++++++++++++++++++------------ 7 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 src/clipboard.cpp create mode 100644 src/clipboard.hpp diff --git a/data/game.cfg b/data/game.cfg index f25dbca190b..f9f102398d3 100644 --- a/data/game.cfg +++ b/data/game.cfg @@ -7,6 +7,11 @@ {schedules.cfg} +[+units] + {names.cfg} +[/units] + + {multiplayer.cfg} {terrain.cfg} @@ -121,8 +126,6 @@ {units} {~units} - {names.cfg} - [race] name=elf num_traits=2 diff --git a/src/clipboard.cpp b/src/clipboard.cpp new file mode 100644 index 00000000000..e9f21cb9527 --- /dev/null +++ b/src/clipboard.cpp @@ -0,0 +1,44 @@ +#include "clipboard.hpp" + +#ifdef WIN32 +#include +#endif + +void copy_to_clipboard(const std::string& text) +{ + if(text.empty()) { + return; + } + +#ifdef WIN32 + if(!OpenClipboard(0)) { + return; + } + + EmptyClipboard(); + + const HGLOBAL clip_buffer = GlobalAlloc(GMEM_DDESHARE,text.size()+1); + char* const buffer = reinterpret_cast(GlobalLock(clip_buffer)); + strcpy(buffer,text.c_str()); + + GlobalUnlock(clip_buffer); + SetClipboardData(CF_TEXT,clip_buffer); + CloseClipboard(); +#endif +} + +std::string copy_from_clipboard() +{ +#ifdef WIN32 + if(OpenClipboard(NULL)) { + const HANDLE data = GetClipboardData(CF_TEXT); + char* const buffer = reinterpret_cast(GlobalLock(data)); + GlobalUnlock(data); + CloseClipboard(); + + return buffer; + } +#endif + + return ""; +} diff --git a/src/clipboard.hpp b/src/clipboard.hpp new file mode 100644 index 00000000000..ce10a5e5d9d --- /dev/null +++ b/src/clipboard.hpp @@ -0,0 +1,4 @@ +#include + +void copy_to_clipboard(const std::string& text); +std::string copy_from_clipboard(); \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp index eca361bd2f9..ab44dd7acf3 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1368,12 +1368,13 @@ bool not_id(char c) void do_interpolation(std::string& res, size_t npos, const string_map* m) { + std::cerr << "doing interpolation into '" << res << "': " << npos << "\n"; const std::string::iterator i = std::find(res.begin()+npos,res.end(),'$'); if(i == res.end() || i+1 == res.end()) { return; } - npos = i - res.begin() + 1; + npos = i - res.begin(); const std::string::iterator end = std::find_if(i+1,res.end(),not_id); @@ -1383,10 +1384,10 @@ void do_interpolation(std::string& res, size_t npos, const string_map* m) if(m != NULL) { const string_map::const_iterator itor = m->find(key); if(itor != m->end()) { - res.insert(npos-1,itor->second); + res.insert(npos,itor->second); } } else { - res.insert(npos-1,game_events::get_variable_const(key)); + res.insert(npos,game_events::get_variable_const(key)); } do_interpolation(res,npos,m); diff --git a/src/mapgen.cpp b/src/mapgen.cpp index baf005dee5a..2a996e57984 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -252,6 +252,8 @@ bool generate_river_internal(const height_map& heights, terrain_map& terrain, in terrain[i->x][i->y] = 'c'; } + std::cerr << "done generating river\n"; + return true; } @@ -533,16 +535,23 @@ std::string generate_name(const unit_race& name_generator, const std::string& id const std::vector& options = config::split(string_table[id]); if(options.empty() == false) { const size_t choice = rand()%options.size(); + std::cerr << "calling name generator...\n"; const std::string& name = name_generator.generate_name(unit_race::MALE); + std::cerr << "name generator returned '" << name << "'\n"; if(base_name != NULL) { *base_name = name; } + + std::cerr << "assigned base name..\n"; std::map table; if(additional_symbols == NULL) { additional_symbols = &table; } + std::cerr << "got additional symbols\n"; + (*additional_symbols)["name"] = name; + std::cerr << "interpolation variables into '" << options[choice] << "'\n"; return config::interpolate_variables_into_string(options[choice],additional_symbols); } @@ -670,6 +679,8 @@ std::string default_generate_map(size_t width, size_t height, size_t island_size naming = *names_info; } + std::cerr << "random map config: '" << cfg.write() << "'\n"; + std::cerr << "making dummy race for naming: '" << naming.write() << "'\n"; //make a dummy race for generating names unit_race name_generator(naming); @@ -720,7 +731,9 @@ std::string default_generate_map(size_t width, size_t height, size_t island_size if(river.empty() == false && labels != NULL) { std::string base_name; + std::cerr << "generating name for river...\n"; const std::string& name = generate_name(name_generator,"river_name",&base_name); + std::cerr << "named river '" << name << "'\n"; size_t name_frequency = 20; for(std::vector::const_iterator r = river.begin(); r != river.end(); ++r) { @@ -732,8 +745,11 @@ std::string default_generate_map(size_t width, size_t height, size_t island_size river_names.insert(std::pair(loc,base_name)); } + + std::cerr << "put down river name...\n"; } + std::cerr << "generating lake...\n"; std::set locs; const bool res = generate_lake(terrain,x,y,atoi(cfg["lake_size"].c_str()),locs); if(res && labels != NULL) { diff --git a/src/race.cpp b/src/race.cpp index f99cc7a2427..2d3dcc77ae9 100644 --- a/src/race.cpp +++ b/src/race.cpp @@ -28,6 +28,7 @@ markov_prefix_map markov_prefixes(const std::vector& items, size_t std::string markov_generate_name(const markov_prefix_map& prefixes, size_t chain_size, size_t max_len) { + std::cerr << "generating name with chain size of " << chain_size << " and " << prefixes.size() << " prefixes\n"; if(chain_size == 0) return ""; @@ -35,8 +36,10 @@ std::string markov_generate_name(const markov_prefix_map& prefixes, size_t chain while(res.size() < max_len) { const markov_prefix_map::const_iterator i = prefixes.find(prefix); - if(i == prefixes.end() || i->second.empty()) + if(i == prefixes.end() || i->second.empty()) { + std::cerr << "failed to find prefix for '" << prefix << "'\n"; return res; + } const char c = i->second[get_random()%i->second.size()]; if(c == 0) { @@ -104,6 +107,10 @@ unit_race::unit_race(const config& cfg) : name_(cfg["name"]), ntraits_(atoi(cfg[ next_[MALE] = markov_prefixes(names_[MALE],chain_size_); next_[FEMALE] = markov_prefixes(names_[FEMALE],chain_size_); + + if(next_[MALE].size() == 0) { + std::cerr << "empty prefix string: has " << names_[MALE].size() << " names: '" << cfg["male_names"] << "'\n"; + } } const std::string& unit_race::name() const { return name_; } diff --git a/src/widgets/textbox.cpp b/src/widgets/textbox.cpp index 22b396e60b8..635eb3f2f9a 100644 --- a/src/widgets/textbox.cpp +++ b/src/widgets/textbox.cpp @@ -11,6 +11,7 @@ See the COPYING file for more details. */ #include "textbox.hpp" +#include "../clipboard.hpp" #include "../font.hpp" #include "../show_dialog.hpp" #include "../video.hpp" @@ -20,6 +21,7 @@ #include #include +#include namespace gui { @@ -330,7 +332,21 @@ void textbox::handle_event(const SDL_Event& event) { bool changed = false; - if(location().x == 0 || editable_ == false) { + if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_c) { + std::cerr << "got press of 'c': " << int(event.key.keysym.mod) << " " << (event.key.keysym.mod&KMOD_CTRL) << " " + << size_t(selstart_) << "/" << size_t(selend_) << "/" << text_.size() << " " << (focus() ? "focus" : "no focus") << " " << location().x << "\n"; + } + + //if someone presses ctrl+c to copy text onto the clipboard + if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_c && (event.key.keysym.mod&KMOD_CTRL) != 0 + && size_t(selstart_) <= text_.size() && size_t(selend_) <= text_.size() && selstart_ != selend_) { + const size_t beg = minimum(size_t(selstart_),size_t(selend_)); + const size_t end = maximum(size_t(selstart_),size_t(selend_)); + + std::string selection(end - beg,'x'); + std::copy(text_.begin()+beg,text_.begin()+end,selection.begin()); + std::cerr << "copying text to clipboard: '" << selection << "'\n"; + copy_to_clipboard(selection); return; } @@ -340,18 +356,6 @@ void textbox::handle_event(const SDL_Event& event) grabmouse_ = false; } - //if we don't have the focus, then see if we gain the focus, - //otherwise return - if(focus() == false) { - if(event.type == SDL_MOUSEMOTION && - mousex >= location().x && mousey >= location().y && - mousex < location().x + location().w && mousey < location().y + location().h) { - events::focus_handler(this); - } - - return; - } - if( (grabmouse_ && (event.type == SDL_MOUSEMOTION)) || ( event.type == SDL_MOUSEBUTTONDOWN && (mousebuttons & SDL_BUTTON(1)) && ! (mousex < location().x || mousex > location().x + location().w || @@ -384,8 +388,26 @@ void textbox::handle_event(const SDL_Event& event) } else if (! (mousebuttons & SDL_BUTTON(1))) { grabmouse_ = false; } + + set_dirty(); } - + + if(editable_ == false) { + return; + } + + //if we don't have the focus, then see if we gain the focus, + //otherwise return + if(focus() == false) { + if(event.type == SDL_MOUSEMOTION && + mousex >= location().x && mousey >= location().y && + mousex < location().x + location().w && mousey < location().y + location().h) { + events::focus_handler(this); + } + + return; + } + if(event.type != SDL_KEYDOWN || focus() != true) { draw(); return;