]> git.saurik.com Git - wxWidgets.git/blob - utils/dialoged/src/symbtabl.cpp
reverted Julian's unintentional breakage of wxChoice
[wxWidgets.git] / utils / dialoged / src / symbtabl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: symbtabl.cpp
3 // Purpose: wxResourceSymbolTable
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "symbtabl.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/file.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "symbtabl.h"
33
34 wxResourceSymbolTable::wxResourceSymbolTable():
35 m_hashTable(wxKEY_STRING)
36 {
37 }
38
39 wxResourceSymbolTable::~wxResourceSymbolTable()
40 {
41 Clear();
42 }
43
44 // Operations
45
46 bool wxResourceSymbolTable::ReadIncludeFile(const wxString& filename)
47 {
48 wxFile file;
49 if (!wxFileExists(filename))
50 return FALSE;
51
52 if (!file.Open(filename, wxFile::read))
53 return FALSE;
54
55 off_t len = file.Length();
56 if (len == -1)
57 return FALSE;
58
59 Clear();
60 AddStandardSymbols();
61
62 wxString str;
63 char* p = str.GetWriteBuf(len + 1);
64
65 if (file.Read(p, len) == wxFile::fd_invalid)
66 {
67 str.UngetWriteBuf();
68 return FALSE;
69 }
70 str.UngetWriteBuf();
71
72 // Look for #define occurrences
73 int pos = str.Find("#define");
74 while (pos != -1)
75 {
76 size_t len = str.Length();
77
78 size_t i = pos + 8;
79
80 // Eat whitespace until symbol
81 while ((str[i] == ' ' || str[i] == '\t') && (i < len))
82 i ++;
83
84 size_t start = i;
85
86 // Eat symbol
87 while (str[i] != ' ' && str[i] != '\t' && (i < len))
88 i ++;
89 size_t end = i-1;
90
91 wxString symbol(str.Mid(start, (end - start + 1)));
92
93 // Eat whitespace until number
94 while ((str[i] == ' ' || str[i] == '\t') && (i < len))
95 i ++;
96
97 size_t startNum = i;
98
99 // Eat number
100 while (str[i] != ' ' && str[i] != '\t' && str[i] != '\n' && (i < len))
101 i ++;
102
103 size_t endNum = i-1;
104
105 wxString numStr(str.Mid(startNum, (endNum - startNum + 1)));
106
107 int id = atol(numStr);
108
109 AddSymbol(symbol, id);
110
111 str = str.Right(len - i);
112 pos = str.Find("#define");
113 }
114
115 return TRUE;
116 }
117
118 bool wxResourceSymbolTable::WriteIncludeFile(const wxString& filename)
119 {
120 wxFile file;
121 if (!file.Open(filename, wxFile::write))
122 return FALSE;
123
124 wxString fileOnly(wxFileNameFromPath(filename));
125 wxString line;
126 line.Printf("/*\n * %s\n * Window identifiers file written by Dialog Editor\n */\n\n",
127 (const char*) fileOnly);
128
129 file.Write(line, line.Length());
130
131 m_hashTable.BeginFind();
132
133 wxNode* node = m_hashTable.Next();
134 while (node)
135 {
136 const char* str = node->GetKeyString();
137 int id = (int) node->Data() ;
138
139 if (!IsStandardSymbol(str))
140 {
141 wxString line;
142 line.Printf("#define %s %ld\n", str, id);
143
144 file.Write(line, line.Length());
145 }
146
147 node = m_hashTable.Next();
148 }
149 return TRUE;
150 }
151
152 void wxResourceSymbolTable::Clear()
153 {
154 m_hashTable.Clear();
155 }
156
157 bool wxResourceSymbolTable::AddSymbol(const wxString& symbol, int id)
158 {
159 m_hashTable.Put(symbol, (wxObject*) id);
160 return TRUE;
161 }
162
163 bool wxResourceSymbolTable::RemoveSymbol(const wxString& symbol)
164 {
165 m_hashTable.Delete(symbol);
166 return TRUE;
167 }
168
169 bool wxResourceSymbolTable::RemoveSymbol(int id)
170 {
171 wxString symbol(GetSymbolForId(id));
172 m_hashTable.Delete(symbol);
173 return TRUE;
174 }
175
176 // Accessors
177 wxString wxResourceSymbolTable::GetSymbolForId(int id)
178 {
179 m_hashTable.BeginFind();
180
181 wxNode* node = m_hashTable.Next();
182 while (node)
183 {
184 const char* str = node->GetKeyString();
185 if (str && ( ((int) node->Data()) == id) )
186 return wxString(str);
187
188 node = m_hashTable.Next();
189 }
190 return wxString("");
191 }
192
193 int wxResourceSymbolTable::GetIdForSymbol(const wxString& symbol)
194 {
195 return (int) m_hashTable.Get(symbol);
196 }
197
198 bool wxResourceSymbolTable::SymbolExists(const wxString& symbol) const
199 {
200 return (m_hashTable.Get(symbol) != NULL);
201 }
202
203 bool wxResourceSymbolTable::IdExists(int id)
204 {
205 m_hashTable.BeginFind();
206
207 wxNode* node = m_hashTable.Next();
208 while (node)
209 {
210 if ( (((int) node->Data()) == id) )
211 return TRUE;
212
213 node = m_hashTable.Next();
214 }
215 return FALSE;
216 }
217
218 int wxResourceSymbolTable::FindHighestId()
219 {
220 int highest = 0;
221
222 m_hashTable.BeginFind();
223
224 wxNode* node = m_hashTable.Next();
225 while (node)
226 {
227 int id = ((int) node->Data());
228 if (id > highest)
229 highest = id;
230
231 node = m_hashTable.Next();
232 }
233
234 // Make sure we don't clash with future standard wxWindows ids
235 if (highest <= wxID_HIGHEST)
236 highest = wxID_HIGHEST + 1;
237 return highest;
238 }
239
240 /*
241 * A table of the standard identifiers
242 */
243
244 struct wxStandardSymbolStruct
245 {
246 char* m_name;
247 int m_id;
248 };
249
250 static wxStandardSymbolStruct sg_StandardSymbols[] =
251 {
252 { "wxID_OK", wxID_OK },
253 { "wxID_CANCEL", wxID_CANCEL },
254 { "wxID_APPLY", wxID_APPLY },
255 { "wxID_HELP", wxID_HELP },
256 { "wxID_STATIC", wxID_STATIC },
257 { "wxID_YES", wxID_YES },
258 { "wxID_NO", wxID_NO },
259
260 { "wxID_OPEN", wxID_OPEN },
261 { "wxID_CLOSE", wxID_CLOSE },
262 { "wxID_NEW", wxID_NEW },
263 { "wxID_SAVE", wxID_SAVE },
264 { "wxID_SAVEAS", wxID_SAVEAS },
265 { "wxID_REVERT", wxID_REVERT },
266 { "wxID_EXIT", wxID_EXIT },
267 { "wxID_UNDO", wxID_UNDO },
268 { "wxID_REDO", wxID_REDO },
269 { "wxID_PRINT", wxID_PRINT },
270 { "wxID_PRINT_SETUP", wxID_PRINT_SETUP },
271 { "wxID_PREVIEW", wxID_PREVIEW },
272 { "wxID_ABOUT", wxID_ABOUT },
273 { "wxID_HELP_CONTENTS", wxID_HELP_CONTENTS },
274 { "wxID_HELP_COMMANDS", wxID_HELP_COMMANDS },
275 { "wxID_HELP_PROCEDURES", wxID_HELP_PROCEDURES },
276 { "wxID_HELP_CONTEXT", wxID_HELP_CONTEXT },
277
278 { "wxID_CUT", wxID_CUT },
279 { "wxID_COPY", wxID_COPY },
280 { "wxID_PASTE", wxID_PASTE },
281 { "wxID_CLEAR", wxID_CLEAR },
282 { "wxID_FIND", wxID_FIND },
283 { "wxID_DUPLICATE", wxID_DUPLICATE },
284
285 { "wxID_FILE1", wxID_FILE1 },
286 { "wxID_FILE2", wxID_FILE2 },
287 { "wxID_FILE3", wxID_FILE3 },
288 { "wxID_FILE4", wxID_FILE4 },
289 { "wxID_FILE5", wxID_FILE5 },
290 { "wxID_FILE6", wxID_FILE6 },
291 { "wxID_FILE7", wxID_FILE7 },
292 { "wxID_FILE8", wxID_FILE8 },
293 { "wxID_FILE9", wxID_FILE9 }
294
295 };
296
297 static int sg_StandardSymbolSize = (sizeof(sg_StandardSymbols)/sizeof(wxStandardSymbolStruct));
298
299 void wxResourceSymbolTable::AddStandardSymbols()
300 {
301 int i;
302 for (i = 0; i < sg_StandardSymbolSize; i++)
303 {
304 AddSymbol(sg_StandardSymbols[i].m_name, sg_StandardSymbols[i].m_id);
305 }
306 }
307
308 bool wxResourceSymbolTable::IsStandardSymbol(const wxString& symbol) const
309 {
310 int i;
311 for (i = 0; i < sg_StandardSymbolSize; i++)
312 {
313 if (symbol == sg_StandardSymbols[i].m_name)
314 return TRUE;
315 }
316 return FALSE;
317 }
318
319 bool wxResourceSymbolTable::FillComboBox(wxComboBox* comboBox)
320 {
321 m_hashTable.BeginFind();
322
323 wxNode* node = m_hashTable.Next();
324 while (node)
325 {
326 const char* str = node->GetKeyString();
327
328 comboBox->Append(str);
329 node = m_hashTable.Next();
330 }
331 return TRUE;
332 }
333