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"
25 // Set an FL_OBJECT to activated or deactivated
26 void setEnabled(FL_OBJECT * ob, bool enable)
29 fl_activate_object(ob);
30 fl_set_object_lcol(ob, FL_BLACK);
32 fl_deactivate_object(ob);
33 fl_set_object_lcol(ob, FL_INACTIVE);
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)
41 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
44 if (sin.empty()) return sout;
46 // break sin up into a vector of individual words
47 vector<string> sentence;
49 for (string::const_iterator sit = sin.begin();
50 sit != sin.end(); ++sit) {
51 if ((*sit) == ' ' || (*sit) == '\n') {
53 sentence.push_back(word);
56 if ((*sit) == '\n') word += '\n';
63 // Flush remaining contents of word
64 if (!word.empty() ) sentence.push_back(word);
67 string line_plus_word;
68 for (vector<string>::const_iterator vit = sentence.begin();
69 vit != sentence.end(); ++vit) {
76 line_plus_word.erase();
80 if (!line_plus_word.empty() ) line_plus_word += ' ';
81 line_plus_word += word;
83 int const length = fl_get_string_width(style, size,
84 line_plus_word.c_str(),
85 int(line_plus_word.length()));
88 line_plus_word = word;
91 line = line_plus_word;
93 // Flush remaining contents of line
98 if (sout[sout.length() - 1] == '\n')
99 sout.erase(sout.length() - 1);
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 }
121 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
126 bool XformsColor::read(string const & filename)
128 LyXLex lexrc(xformTags, xformCount);
129 if (!lexrc.setFile(filename))
132 while (lexrc.isOK()) {
133 int const le = lexrc.lex();
136 case LyXLex::LEX_UNDEF:
137 lexrc.printError("Unknown tag `$$Token'");
139 case LyXLex::LEX_FEOF:
146 if (!lexrc.next()) break;
147 col.r = lexrc.getInteger();
149 if (!lexrc.next()) break;
150 col.g = lexrc.getInteger();
152 if (!lexrc.next()) break;
153 col.b = lexrc.getInteger();
155 fl_mapcolor(le, col.r, col.g, col.b);
162 bool XformsColor::write(string const & filename)
164 ofstream os(filename.c_str());
168 os << "### This file is part of\n"
169 << "### ========================================================\n"
170 << "### LyX, The Document Processor\n"
172 << "### Copyright 1995 Matthias Ettrich\n"
173 << "### Copyright 1995-2001 The LyX Team.\n"
175 << "### ========================================================\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"
181 for (int i = 0; i < xformCount; ++i) {
182 string const tag = xformTags[i].tag;
183 int const colorID = xformTags[i].code;
186 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
189 << color.r << " " << color.g << " " << color.b << "\n";
196 string RWInfo::error_message;
198 bool RWInfo::WriteableDir(string const & name)
200 error_message.erase();
202 if (!AbsolutePath(name)) {
203 error_message = N_("The absolute path is required.");
207 FileInfo const tp(name);
209 error_message = N_("Directory does not exist.");
213 if (!tp.writable()) {
214 error_message = N_("Cannot write to this directory.");
222 bool RWInfo::ReadableDir(string const & name)
224 error_message.erase();
226 if (!AbsolutePath(name)) {
227 error_message = N_("The absolute path is required.");
231 FileInfo const tp(name);
233 error_message = N_("Directory does not exist.");
237 if (!tp.readable()) {
238 error_message = N_("Cannot read this directory.");
246 bool RWInfo::WriteableFile(string const & name)
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
253 error_message.erase();
256 error_message = N_("No file input.");
260 string const dir = OnlyPath(name);
261 if (!AbsolutePath(dir)) {
262 error_message = N_("The absolute path is required.");
272 error_message = N_("Directory does not exist.");
277 error_message = N_("Cannot write to this directory.");
282 if (dir == name || f.isDir()) {
283 error_message = N_("A file is required, not a directory.");
287 if (f.exist() && !f.writable()) {
288 error_message = N_("Cannot write to this file.");
296 bool RWInfo::ReadableFile(string const & name)
298 error_message.erase();
301 error_message = N_("No file input.");
305 string const dir = OnlyPath(name);
306 if (!AbsolutePath(dir)) {
307 error_message = N_("The absolute path is required.");
317 error_message = N_("Directory does not exist.");
322 error_message = N_("Cannot read from this directory.");
327 if (dir == name || f.isDir()) {
328 error_message = N_("A file is required, not a directory.");
333 error_message = N_("File does not exist.");
338 error_message = N_("Cannot read from this file.");