~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/editor/string_reader.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Goulais
  • Date: 2004-08-09 10:26:00 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040809102600-lg2q9lfars0q1p42
Tags: 0.6.0-8
Applied patch from Andreas Jochens (Closes: #263992)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: string_reader.cxx,v 1.7 2002/09/14 19:06:34 torangan Exp $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
 
5
//
 
6
//  This program is free software; you can redistribute it and/or
 
7
//  modify it under the terms of the GNU General Public License
 
8
//  as published by the Free Software Foundation; either version 2
 
9
//  of the License, or (at your option) any later version.
 
10
//
 
11
//  This program is distributed in the hope that it will be useful,
 
12
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//  GNU General Public License for more details.
 
15
//
 
16
//  You should have received a copy of the GNU General Public License
 
17
//  along with this program; if not, write to the Free Software
 
18
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
#include <ClanLib/Core/System/system.h>
 
21
#include <ClanLib/Display/Input/inputbuffer.h>
 
22
#include <ClanLib/Display/Display/display.h>
 
23
#include <ClanLib/Display/Font/font.h>
 
24
#include "../console.hxx"
 
25
#include "../pingus_resource.hxx"
 
26
#include "string_reader.hxx"
 
27
 
 
28
StringReader::StringReader()
 
29
{
 
30
  strings = 0;
 
31
  font = PingusResource::load_font("Fonts/courier_small", "fonts");
 
32
}
 
33
 
 
34
StringReader::StringReader(const std::string & d, const std::string & def)
 
35
{
 
36
  strings = 0;
 
37
  description = d;
 
38
  default_string = def;  
 
39
  font =  PingusResource::load_font("Fonts/courier_small", "fonts");
 
40
}
 
41
 
 
42
StringReader::~StringReader()
 
43
{
 
44
}
 
45
 
 
46
void
 
47
StringReader::set_strings(std::list<std::string>* s)
 
48
{
 
49
  strings = s;
 
50
}
 
51
 
 
52
std::string
 
53
StringReader::read_string()
 
54
{
 
55
  finished = false;
 
56
  CL_InputBuffer keys;
 
57
  CL_Key key;
 
58
 
 
59
  CL_System::keep_alive();
 
60
 
 
61
  current_string = default_string;
 
62
 
 
63
  keys.clear();
 
64
 
 
65
  draw();
 
66
 
 
67
  finished = false;
 
68
  while (!finished) 
 
69
    {
 
70
      CL_System::keep_alive();
 
71
 
 
72
      if (keys.peek_key().state != CL_Key::NoKey)
 
73
        {
 
74
          key = keys.get_key();
 
75
  
 
76
          if (key.state == CL_Key::Pressed)
 
77
            {
 
78
              switch (key.id)
 
79
                {
 
80
                case CL_KEY_ENTER:
 
81
                  finished = true;
 
82
                  break;
 
83
                case CL_KEY_ESCAPE:
 
84
                  finished = true;
 
85
                  current_string = "";
 
86
                  break;
 
87
                case CL_KEY_DELETE:
 
88
                case CL_KEY_BACKSPACE:
 
89
                  if (!current_string.empty())
 
90
                    current_string = current_string.substr(0, current_string.size() - 1); 
 
91
                  break;
 
92
                case CL_KEY_TAB:
 
93
                case CL_KEY_SPACE:
 
94
                  complete_string();
 
95
                  break;
 
96
                default:
 
97
                  if (key.ascii > 0)
 
98
                    current_string += key.ascii;
 
99
                }
 
100
              draw();
 
101
            }
 
102
        }
 
103
    }
 
104
  keys.clear();
 
105
 
 
106
  return current_string;
 
107
}
 
108
 
 
109
void
 
110
StringReader::complete_string()
 
111
{
 
112
  int completions_counter = 0;
 
113
  std::string* completion;
 
114
 
 
115
  completions.clear();
 
116
    
 
117
  console << "\nCompletions:\n" <<   "~~~~~~~~~~~~" << std::endl;
 
118
 
 
119
  for(std::list<std::string>::iterator i = strings->begin(); i != strings->end(); ++i)
 
120
    {
 
121
      if (i->find(current_string) == 0)
 
122
        {
 
123
          console << *i << std::endl;
 
124
          ++completions_counter;
 
125
          completion = &(*i);
 
126
          completions.push_back(&(*i));
 
127
        }
 
128
    }
 
129
 
 
130
  if (completions_counter >= 1)
 
131
    {
 
132
      current_string = find_uniq(); 
 
133
    }
 
134
  // std::cout << "Searching finished" << std::endl;
 
135
}
 
136
 
 
137
std::string
 
138
StringReader::find_uniq()
 
139
{
 
140
  std::list<std::string*>::iterator i = completions.begin();
 
141
  std::string ret_string = **(completions.begin());
 
142
 
 
143
  while (i != completions.end())
 
144
    {
 
145
      ret_string = while_eq(ret_string, **i);
 
146
      ++i;
 
147
    }
 
148
 
 
149
  return ret_string;
 
150
}
 
151
 
 
152
std::string
 
153
StringReader::while_eq(const std::string& a, const std::string& b)
 
154
{
 
155
  std::string ret_string;
 
156
  
 
157
  for(std::string::size_type i = 0;
 
158
      i < a.size() && i < b.size() && a[i] == b[i];
 
159
      ++i)
 
160
    {
 
161
      ret_string += a[i];
 
162
    }
 
163
  
 
164
  return ret_string;
 
165
}
 
166
 
 
167
void
 
168
StringReader::draw()
 
169
{
 
170
  CL_Display::clear_display();
 
171
  font->print_left(20, 20, description.c_str());
 
172
  font->print_left(20, 40, current_string.c_str());
 
173
  Display::flip_display();
 
174
}
 
175
 
 
176
 
 
177
/* EOF */