]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xforms_helpers.C
Fix crash when using dialog with keyboard:
[lyx.git] / src / frontends / xforms / xforms_helpers.C
1 /** Collection of some useful xform helper functions
2  */
3
4 #include <config.h>
5
6 #include FORMS_H_LOCATION
7
8 #include <fstream> // ofstream
9 #include <vector>
10
11 #ifdef __GNUG_
12 #pragma implementation
13 #endif
14  
15 #include "xforms_helpers.h"
16 #include "lyxlex.h"
17 #include "support/FileInfo.h"
18 #include "support/filetools.h"
19 #include "gettext.h"
20
21 using std::ofstream;
22 using std::pair;
23 using std::vector;
24
25 // Set an FL_OBJECT to activated or deactivated
26 void setEnabled(FL_OBJECT * ob, bool enable)
27 {
28         if (enable) {
29                 fl_activate_object(ob);
30                 fl_set_object_lcol(ob, FL_BLACK);
31         } else {
32                 fl_deactivate_object(ob);
33                 fl_set_object_lcol(ob, FL_INACTIVE);
34         }
35 }
36
37         
38 // Take a string and add breaks so that it fits into a desired label width, w
39 string formatted(string const & sin, int w, int size, int style)
40 {
41         // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
42
43         string sout;
44         if (sin.empty()) return sout;
45
46         // break sin up into a vector of individual words
47         vector<string> sentence;
48         string word;
49         for (string::const_iterator sit = sin.begin();
50              sit != sin.end(); ++sit) {
51                 if ((*sit) == ' ' || (*sit) == '\n') {
52                         if (!word.empty()) {
53                                 sentence.push_back(word);
54                                 word.erase();
55                         }
56                         if ((*sit) == '\n') word += '\n';
57                         
58                 } else {
59                         word += (*sit);
60                 }
61         }
62
63         // Flush remaining contents of word
64         if (!word.empty() ) sentence.push_back(word);
65
66         string line;
67         string line_plus_word;
68         for (vector<string>::const_iterator vit = sentence.begin();
69              vit != sentence.end(); ++vit) {
70                 string word(*vit);
71
72                 char c = word[0];
73                 if (c == '\n') {
74                         sout += line + '\n';
75                         word.erase(0,1);
76                         line_plus_word.erase();
77                         line.erase();
78                 }
79
80                 if (!line_plus_word.empty() ) line_plus_word += ' ';
81                 line_plus_word += word;
82
83                 int const length = fl_get_string_width(style, size,
84                                                        line_plus_word.c_str(),
85                                                        int(line_plus_word.length()));
86                 if (length >= w) {
87                         sout += line + '\n';
88                         line_plus_word = word;
89                 }
90
91                 line = line_plus_word;
92         }
93         // Flush remaining contents of line
94         if (!line.empty()) {
95                 sout += line;
96         }
97
98         if (sout[sout.length() - 1] == '\n')
99                 sout.erase(sout.length() - 1);
100
101         return sout;
102 }
103
104
105 namespace {
106
107 // sorted by hand to prevent LyXLex from complaining on read().
108 keyword_item xformTags[] = {
109         { "\\gui_background", FL_COL1 },
110         { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
111         { "\\gui_buttonleft", FL_LEFT_BCOL },
112         { "\\gui_buttonright", FL_RIGHT_BCOL },
113         { "\\gui_buttontop", FL_TOP_BCOL },
114         { "\\gui_inactive", FL_INACTIVE },
115         { "\\gui_push_button", FL_YELLOW },
116         { "\\gui_selected", FL_MCOL },  
117         { "\\gui_text", FL_BLACK }
118 };
119
120
121 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
122
123 } // namespace anon
124
125
126 bool XformsColor::read(string const & filename)
127 {
128         LyXLex lexrc(xformTags, xformCount);
129         if (!lexrc.setFile(filename))
130                 return false;
131
132         while (lexrc.isOK()) {
133                 int const le = lexrc.lex();
134
135                 switch (le) {
136                 case LyXLex::LEX_UNDEF:
137                         lexrc.printError("Unknown tag `$$Token'");
138                         continue; 
139                 case LyXLex::LEX_FEOF:
140                         continue;
141                 default: break;
142                 }
143
144                 RGBColor col;
145
146                 if (!lexrc.next()) break;
147                 col.r = lexrc.getInteger();
148
149                 if (!lexrc.next()) break;
150                 col.g = lexrc.getInteger();
151
152                 if (!lexrc.next()) break;
153                 col.b = lexrc.getInteger();
154
155                 fl_mapcolor(le, col.r, col.g, col.b);
156         }
157                 
158         return true;
159 }
160
161
162 bool XformsColor::write(string const & filename)
163 {
164         ofstream os(filename.c_str());
165         if (!os)
166                 return false;
167
168         os << "### This file is part of\n"
169            << "### ========================================================\n"
170            << "###          LyX, The Document Processor\n"
171            << "###\n"
172            << "###          Copyright 1995 Matthias Ettrich\n"
173            << "###          Copyright 1995-2001 The LyX Team.\n"
174            << "###\n"
175            << "### ========================================================\n"
176            << "\n"
177            << "# This file is written by LyX, if you want to make your own\n"
178            << "# modifications you should do them from inside LyX and save\n"
179            << "\n";
180
181         for (int i = 0; i < xformCount; ++i) {
182                 string const tag  = xformTags[i].tag;
183                 int const colorID = xformTags[i].code;
184                 RGBColor color;
185
186                 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
187
188                 os << tag << " "
189                    << color.r << " " << color.g << " " << color.b << "\n";
190         }
191
192         return true;
193 }
194
195
196 string  RWInfo::error_message;
197
198 bool RWInfo::WriteableDir(string const & name)
199 {
200         error_message.erase();
201
202         if (!AbsolutePath(name)) {
203                 error_message = N_("The absolute path is required.");
204                 return false;
205         }
206
207         FileInfo const tp(name);
208         if (!tp.isDir()) {
209                 error_message = N_("Directory does not exist.");
210                 return false;
211         }
212
213         if (!tp.writable()) {
214                 error_message = N_("Cannot write to this directory.");
215                 return false;
216         }
217
218         return true;
219 }
220
221
222 bool RWInfo::ReadableDir(string const & name)
223 {
224         error_message.erase();
225
226         if (!AbsolutePath(name)) {
227                 error_message = N_("The absolute path is required.");
228                 return false;
229         }
230
231         FileInfo const tp(name);
232         if (!tp.isDir()) {
233                 error_message = N_("Directory does not exist.");
234                 return false;
235         }
236
237         if (!tp.readable()) {
238                 error_message = N_("Cannot read this directory.");
239                 return false;
240         }
241
242         return true;
243 }
244
245
246 bool RWInfo::WriteableFile(string const & name)
247 {
248         // A writeable file is either:
249         // * An existing file to which we have write access, or
250         // * A file that doesn't yet exist but that would exist in a writeable
251         //   directory.
252
253         error_message.erase();
254
255         if (name.empty()) {
256                 error_message = N_("No file input.");
257                 return false;
258         }
259
260         string const dir = OnlyPath(name);
261         if (!AbsolutePath(dir)) {
262                 error_message = N_("The absolute path is required.");
263                 return false;
264         }
265
266         FileInfo d(name);
267         if (!d.isDir()) {
268                 d.newFile(dir);
269         }
270
271         if (!d.isDir()) {
272                 error_message = N_("Directory does not exist.");
273                 return false;
274         }
275         
276         if (!d.writable()) {
277                 error_message = N_("Cannot write to this directory.");
278                 return false;
279         }
280
281         FileInfo f(name);
282         if (dir == name || f.isDir()) {
283                 error_message = N_("A file is required, not a directory.");
284                 return false;
285         }
286
287         if (f.exist() && !f.writable()) {
288                 error_message = N_("Cannot write to this file.");
289                 return false;
290         }
291         
292         return true;
293 }
294
295
296 bool RWInfo::ReadableFile(string const & name)
297 {
298         error_message.erase();
299
300         if (name.empty()) {
301                 error_message = N_("No file input.");
302                 return false;
303         }
304
305         string const dir = OnlyPath(name);
306         if (!AbsolutePath(dir)) {
307                 error_message = N_("The absolute path is required.");
308                 return false;
309         }
310
311         FileInfo d(name);
312         if (!d.isDir()) {
313                 d.newFile(dir);
314         }
315
316         if (!d.isDir()) {
317                 error_message = N_("Directory does not exist.");
318                 return false;
319         }
320         
321         if (!d.readable()) {
322                 error_message = N_("Cannot read from this directory.");
323                 return false;
324         }
325
326         FileInfo f(name);
327         if (dir == name || f.isDir()) {
328                 error_message = N_("A file is required, not a directory.");
329                 return false;
330         }
331
332         if (!f.exist()) {
333                 error_message = N_("File does not exist.");
334                 return false;
335         }
336         
337         if (!f.readable()) {
338                 error_message = N_("Cannot read from this file.");
339                 return false;
340         }
341
342         return true;
343 }