1 /** Collection of some useful xform helper functions
6 #include FORMS_H_LOCATION
8 #include <fstream> // ofstream
12 #pragma implementation
15 #include "xforms_helpers.h"
17 #include "support/FileInfo.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h" // frontStrip, strip
26 // Set an FL_OBJECT to activated or deactivated
27 void setEnabled(FL_OBJECT * ob, bool enable)
30 fl_activate_object(ob);
31 fl_set_object_lcol(ob, FL_BLACK);
33 fl_deactivate_object(ob);
34 fl_set_object_lcol(ob, FL_INACTIVE);
39 // Given an fl_choice, create a vector of its entries
40 vector<string> const getVectorFromChoice(FL_OBJECT * ob)
43 if (!ob || ob->objclass != FL_CHOICE)
46 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
47 string const text = fl_get_choice_item_text(ob, i+1);
48 vec.push_back(strip(frontStrip(text)));
55 // Given an fl_browser, create a vector of its entries
56 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
59 if (!ob || ob->objclass != FL_BROWSER)
62 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
63 string const text = fl_get_browser_line(ob, i+1);
64 vec.push_back(strip(frontStrip(text)));
71 // Take a string and add breaks so that it fits into a desired label width, w
72 string formatted(string const & sin, int w, int size, int style)
74 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
77 if (sin.empty()) return sout;
79 // breaks in up into a vector of individual words
80 vector<string> sentence;
82 for (string::const_iterator sit = sin.begin();
83 sit != sin.end(); ++sit) {
84 if ((*sit) == ' ' || (*sit) == '\n') {
86 sentence.push_back(word);
89 if ((*sit) == '\n') word += '\n';
96 // Flush remaining contents of word
97 if (!word.empty() ) sentence.push_back(word);
100 string line_plus_word;
101 for (vector<string>::const_iterator vit = sentence.begin();
102 vit != sentence.end(); ++vit) {
109 line_plus_word.erase();
113 if (!line_plus_word.empty() ) line_plus_word += ' ';
114 line_plus_word += word;
116 int const length = fl_get_string_width(style, size,
117 line_plus_word.c_str(),
118 int(line_plus_word.length()));
121 line_plus_word = word;
124 line = line_plus_word;
126 // Flush remaining contents of line
131 if (sout[sout.length() - 1] == '\n')
132 sout.erase(sout.length() - 1);
140 // sorted by hand to prevent LyXLex from complaining on read().
141 keyword_item xformTags[] = {
142 { "\\gui_background", FL_COL1 },
143 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
144 { "\\gui_buttonleft", FL_LEFT_BCOL },
145 { "\\gui_buttonright", FL_RIGHT_BCOL },
146 { "\\gui_buttontop", FL_TOP_BCOL },
147 { "\\gui_inactive", FL_INACTIVE },
148 { "\\gui_push_button", FL_YELLOW },
149 { "\\gui_selected", FL_MCOL },
150 { "\\gui_text", FL_BLACK }
154 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
159 bool XformsColor::read(string const & filename)
161 LyXLex lexrc(xformTags, xformCount);
162 if (!lexrc.setFile(filename))
165 while (lexrc.isOK()) {
166 int const le = lexrc.lex();
169 case LyXLex::LEX_UNDEF:
170 lexrc.printError("Unknown tag `$$Token'");
172 case LyXLex::LEX_FEOF:
179 if (!lexrc.next()) break;
180 col.r = lexrc.getInteger();
182 if (!lexrc.next()) break;
183 col.g = lexrc.getInteger();
185 if (!lexrc.next()) break;
186 col.b = lexrc.getInteger();
188 fl_mapcolor(le, col.r, col.g, col.b);
195 bool XformsColor::write(string const & filename)
197 ofstream os(filename.c_str());
201 os << "### This file is part of\n"
202 << "### ========================================================\n"
203 << "### LyX, The Document Processor\n"
205 << "### Copyright 1995 Matthias Ettrich\n"
206 << "### Copyright 1995-2001 The LyX Team.\n"
208 << "### ========================================================\n"
210 << "# This file is written by LyX, if you want to make your own\n"
211 << "# modifications you should do them from inside LyX and save\n"
214 for (int i = 0; i < xformCount; ++i) {
215 string const tag = xformTags[i].tag;
216 int const colorID = xformTags[i].code;
219 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
222 << color.r << " " << color.g << " " << color.b << "\n";
229 string RWInfo::error_message;
231 bool RWInfo::WriteableDir(string const & name)
233 error_message.erase();
235 if (!AbsolutePath(name)) {
236 error_message = N_("The absolute path is required.");
240 FileInfo const tp(name);
242 error_message = N_("Directory does not exist.");
246 if (!tp.writable()) {
247 error_message = N_("Cannot write to this directory.");
255 bool RWInfo::ReadableDir(string const & name)
257 error_message.erase();
259 if (!AbsolutePath(name)) {
260 error_message = N_("The absolute path is required.");
264 FileInfo const tp(name);
266 error_message = N_("Directory does not exist.");
270 if (!tp.readable()) {
271 error_message = N_("Cannot read this directory.");
279 bool RWInfo::WriteableFile(string const & name)
281 // A writeable file is either:
282 // * An existing file to which we have write access, or
283 // * A file that doesn't yet exist but that would exist in a writeable
286 error_message.erase();
289 error_message = N_("No file input.");
293 string const dir = OnlyPath(name);
294 if (!AbsolutePath(dir)) {
295 error_message = N_("The absolute path is required.");
305 error_message = N_("Directory does not exist.");
310 error_message = N_("Cannot write to this directory.");
315 if (dir == name || f.isDir()) {
316 error_message = N_("A file is required, not a directory.");
320 if (f.exist() && !f.writable()) {
321 error_message = N_("Cannot write to this file.");
329 bool RWInfo::ReadableFile(string const & name)
331 error_message.erase();
334 error_message = N_("No file input.");
338 string const dir = OnlyPath(name);
339 if (!AbsolutePath(dir)) {
340 error_message = N_("The absolute path is required.");
350 error_message = N_("Directory does not exist.");
355 error_message = N_("Cannot read from this directory.");
360 if (dir == name || f.isDir()) {
361 error_message = N_("A file is required, not a directory.");
366 error_message = N_("File does not exist.");
371 error_message = N_("Cannot read from this file.");