]> git.saurik.com Git - wxWidgets.git/blame - utils/dialoged/src/symbtabl.cpp
don't crash when invalid colour is set as fg/bg colour
[wxWidgets.git] / utils / dialoged / src / symbtabl.cpp
CommitLineData
03f68f12
JS
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
34wxResourceSymbolTable::wxResourceSymbolTable():
f6bcfd97 35m_hashTable(wxKEY_STRING)
03f68f12
JS
36{
37}
38
39wxResourceSymbolTable::~wxResourceSymbolTable()
40{
41 Clear();
42}
43
44// Operations
45
46bool wxResourceSymbolTable::ReadIncludeFile(const wxString& filename)
47{
48 wxFile file;
5de76427
JS
49 if (!wxFileExists(filename))
50 return FALSE;
f6bcfd97 51
03f68f12
JS
52 if (!file.Open(filename, wxFile::read))
53 return FALSE;
f6bcfd97 54
03f68f12
JS
55 off_t len = file.Length();
56 if (len == -1)
57 return FALSE;
f6bcfd97 58
5de76427
JS
59 Clear();
60 AddStandardSymbols();
f6bcfd97 61
03f68f12
JS
62 wxString str;
63 char* p = str.GetWriteBuf(len + 1);
f6bcfd97 64
5de76427 65 if (file.Read(p, len) == wxFile::fd_invalid)
03f68f12
JS
66 {
67 str.UngetWriteBuf();
68 return FALSE;
69 }
70 str.UngetWriteBuf();
f6bcfd97 71
03f68f12 72 // Look for #define occurrences
fd71308f 73 int pos = str.Find("#define");
03f68f12
JS
74 while (pos != -1)
75 {
76 size_t len = str.Length();
f6bcfd97 77
03f68f12 78 size_t i = pos + 8;
f6bcfd97 79
03f68f12
JS
80 // Eat whitespace until symbol
81 while ((str[i] == ' ' || str[i] == '\t') && (i < len))
82 i ++;
f6bcfd97 83
03f68f12 84 size_t start = i;
f6bcfd97 85
03f68f12
JS
86 // Eat symbol
87 while (str[i] != ' ' && str[i] != '\t' && (i < len))
88 i ++;
89 size_t end = i-1;
f6bcfd97 90
03f68f12 91 wxString symbol(str.Mid(start, (end - start + 1)));
f6bcfd97 92
03f68f12
JS
93 // Eat whitespace until number
94 while ((str[i] == ' ' || str[i] == '\t') && (i < len))
95 i ++;
f6bcfd97 96
03f68f12 97 size_t startNum = i;
f6bcfd97 98
03f68f12
JS
99 // Eat number
100 while (str[i] != ' ' && str[i] != '\t' && str[i] != '\n' && (i < len))
101 i ++;
f6bcfd97 102
03f68f12 103 size_t endNum = i-1;
f6bcfd97 104
03f68f12 105 wxString numStr(str.Mid(startNum, (endNum - startNum + 1)));
f6bcfd97 106
5de76427 107 int id = atol(numStr);
f6bcfd97 108
03f68f12 109 AddSymbol(symbol, id);
f6bcfd97 110
03f68f12
JS
111 str = str.Right(len - i);
112 pos = str.Find("#define");
113 }
f6bcfd97 114
03f68f12
JS
115 return TRUE;
116}
117
118bool wxResourceSymbolTable::WriteIncludeFile(const wxString& filename)
119{
120 wxFile file;
121 if (!file.Open(filename, wxFile::write))
122 return FALSE;
f6bcfd97 123
bbcdf8bc
JS
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);
f6bcfd97 128
bbcdf8bc 129 file.Write(line, line.Length());
f6bcfd97 130
03f68f12 131 m_hashTable.BeginFind();
f6bcfd97 132
03f68f12
JS
133 wxNode* node = m_hashTable.Next();
134 while (node)
135 {
d4fce50a 136 const char* str = node->GetKeyString();
5de76427 137 int id = (int) node->Data() ;
f6bcfd97 138
5de76427
JS
139 if (!IsStandardSymbol(str))
140 {
141 wxString line;
142 line.Printf("#define %s %ld\n", str, id);
f6bcfd97 143
5de76427
JS
144 file.Write(line, line.Length());
145 }
f6bcfd97 146
03f68f12
JS
147 node = m_hashTable.Next();
148 }
149 return TRUE;
150}
151
152void wxResourceSymbolTable::Clear()
153{
154 m_hashTable.Clear();
155}
156
5de76427 157bool wxResourceSymbolTable::AddSymbol(const wxString& symbol, int id)
03f68f12
JS
158{
159 m_hashTable.Put(symbol, (wxObject*) id);
160 return TRUE;
161}
162
5de76427
JS
163bool wxResourceSymbolTable::RemoveSymbol(const wxString& symbol)
164{
165 m_hashTable.Delete(symbol);
166 return TRUE;
167}
168
169bool wxResourceSymbolTable::RemoveSymbol(int id)
170{
171 wxString symbol(GetSymbolForId(id));
172 m_hashTable.Delete(symbol);
173 return TRUE;
174}
175
03f68f12 176// Accessors
5de76427 177wxString wxResourceSymbolTable::GetSymbolForId(int id)
03f68f12
JS
178{
179 m_hashTable.BeginFind();
f6bcfd97 180
03f68f12
JS
181 wxNode* node = m_hashTable.Next();
182 while (node)
183 {
d4fce50a 184 const char* str = node->GetKeyString();
5de76427 185 if (str && ( ((int) node->Data()) == id) )
03f68f12 186 return wxString(str);
f6bcfd97 187
03f68f12
JS
188 node = m_hashTable.Next();
189 }
190 return wxString("");
191}
192
5de76427 193int wxResourceSymbolTable::GetIdForSymbol(const wxString& symbol)
03f68f12 194{
5de76427 195 return (int) m_hashTable.Get(symbol);
03f68f12
JS
196}
197
198bool wxResourceSymbolTable::SymbolExists(const wxString& symbol) const
199{
200 return (m_hashTable.Get(symbol) != NULL);
201}
202
5de76427
JS
203bool wxResourceSymbolTable::IdExists(int id)
204{
205 m_hashTable.BeginFind();
f6bcfd97 206
5de76427
JS
207 wxNode* node = m_hashTable.Next();
208 while (node)
209 {
210 if ( (((int) node->Data()) == id) )
211 return TRUE;
f6bcfd97 212
5de76427
JS
213 node = m_hashTable.Next();
214 }
215 return FALSE;
216}
217
218int wxResourceSymbolTable::FindHighestId()
219{
220 int highest = 0;
f6bcfd97 221
5de76427 222 m_hashTable.BeginFind();
f6bcfd97 223
5de76427
JS
224 wxNode* node = m_hashTable.Next();
225 while (node)
226 {
227 int id = ((int) node->Data());
228 if (id > highest)
229 highest = id;
f6bcfd97 230
5de76427
JS
231 node = m_hashTable.Next();
232 }
f6bcfd97 233
dfad0599
JS
234 // Make sure we don't clash with future standard wxWindows ids
235 if (highest <= wxID_HIGHEST)
236 highest = wxID_HIGHEST + 1;
5de76427
JS
237 return highest;
238}
239
240/*
f6bcfd97
BP
241* A table of the standard identifiers
242*/
5de76427
JS
243
244struct wxStandardSymbolStruct
245{
246 char* m_name;
247 int m_id;
248};
249
250static wxStandardSymbolStruct sg_StandardSymbols[] =
251{
dfad0599
JS
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 },
f6bcfd97 259
dfad0599
JS
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 },
f6bcfd97 277
dfad0599
JS
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 },
f6bcfd97 284
dfad0599
JS
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 }
f6bcfd97 294
5de76427
JS
295};
296
297static int sg_StandardSymbolSize = (sizeof(sg_StandardSymbols)/sizeof(wxStandardSymbolStruct));
298
299void 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
308bool 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
319bool wxResourceSymbolTable::FillComboBox(wxComboBox* comboBox)
320{
321 m_hashTable.BeginFind();
f6bcfd97 322
5de76427
JS
323 wxNode* node = m_hashTable.Next();
324 while (node)
325 {
d4fce50a 326 const char* str = node->GetKeyString();
f6bcfd97 327
5de76427
JS
328 comboBox->Append(str);
329 node = m_hashTable.Next();
330 }
331 return TRUE;
332}
333