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