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)
42 #warning Why cant this be done by a one pass algo? (Lgb)
45 if (sin.empty()) return sout;
47 // break sin up into a vector of individual words
48 vector<string> sentence;
50 for (string::const_iterator sit = sin.begin();
51 sit != sin.end(); ++sit) {
52 if ((*sit) == ' ' || (*sit) == '\n') {
54 sentence.push_back(word);
57 if ((*sit) == '\n') word += '\n';
64 // Flush remaining contents of word
65 if (!word.empty() ) sentence.push_back(word);
68 string line_plus_word;
69 for (vector<string>::const_iterator vit = sentence.begin();
70 vit != sentence.end(); ++vit) {
77 line_plus_word.erase();
81 if (!line_plus_word.empty() ) line_plus_word += ' ';
82 line_plus_word += word;
84 int const length = fl_get_string_width(style, size,
85 line_plus_word.c_str(),
86 int(line_plus_word.length()));
89 line_plus_word = word;
92 line = line_plus_word;
94 // Flush remaining contents of line
99 if (sout[sout.length() - 1] == '\n')
100 sout.erase(sout.length() - 1);
108 // sorted by hand to prevent LyXLex from complaining on read().
109 keyword_item xformTags[] = {
110 { "\\gui_background", FL_COL1 },
111 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
112 { "\\gui_buttonleft", FL_LEFT_BCOL },
113 { "\\gui_buttonright", FL_RIGHT_BCOL },
114 { "\\gui_buttontop", FL_TOP_BCOL },
115 { "\\gui_inactive", FL_INACTIVE },
116 { "\\gui_push_button", FL_YELLOW },
117 { "\\gui_selected", FL_MCOL },
118 { "\\gui_text", FL_BLACK }
122 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
127 bool XformsColor::read(string const & filename)
129 LyXLex lexrc(xformTags, xformCount);
130 if (!lexrc.setFile(filename))
133 while (lexrc.isOK()) {
134 int const le = lexrc.lex();
137 case LyXLex::LEX_UNDEF:
138 lexrc.printError("Unknown tag `$$Token'");
140 case LyXLex::LEX_FEOF:
147 if (!lexrc.next()) break;
148 col.r = lexrc.getInteger();
150 if (!lexrc.next()) break;
151 col.g = lexrc.getInteger();
153 if (!lexrc.next()) break;
154 col.b = lexrc.getInteger();
156 fl_mapcolor(le, col.r, col.g, col.b);
163 bool XformsColor::write(string const & filename)
165 ofstream os(filename.c_str());
169 os << "### This file is part of\n"
170 << "### ========================================================\n"
171 << "### LyX, The Document Processor\n"
173 << "### Copyright 1995 Matthias Ettrich\n"
174 << "### Copyright 1995-2001 The LyX Team.\n"
176 << "### ========================================================\n"
178 << "# This file is written by LyX, if you want to make your own\n"
179 << "# modifications you should do them from inside LyX and save\n"
182 for (int i = 0; i < xformCount; ++i) {
183 string const tag = xformTags[i].tag;
184 int const colorID = xformTags[i].code;
187 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
190 << color.r << " " << color.g << " " << color.b << "\n";
197 string RWInfo::error_message;
199 bool RWInfo::WriteableDir(string const & name)
201 error_message.erase();
203 if (!AbsolutePath(name)) {
204 error_message = N_("The absolute path is required.");
208 FileInfo const tp(name);
210 error_message = N_("Directory does not exist.");
214 if (!tp.writable()) {
215 error_message = N_("Cannot write to this directory.");
223 bool RWInfo::ReadableDir(string const & name)
225 error_message.erase();
227 if (!AbsolutePath(name)) {
228 error_message = N_("The absolute path is required.");
232 FileInfo const tp(name);
234 error_message = N_("Directory does not exist.");
238 if (!tp.readable()) {
239 error_message = N_("Cannot read this directory.");
247 bool RWInfo::WriteableFile(string const & name)
249 // A writeable file is either:
250 // * An existing file to which we have write access, or
251 // * A file that doesn't yet exist but that would exist in a writeable
254 error_message.erase();
257 error_message = N_("No file input.");
261 string const dir = OnlyPath(name);
262 if (!AbsolutePath(dir)) {
263 error_message = N_("The absolute path is required.");
273 error_message = N_("Directory does not exist.");
278 error_message = N_("Cannot write to this directory.");
283 if (dir == name || f.isDir()) {
284 error_message = N_("A file is required, not a directory.");
288 if (f.exist() && !f.writable()) {
289 error_message = N_("Cannot write to this file.");
297 bool RWInfo::ReadableFile(string const & name)
299 error_message.erase();
302 error_message = N_("No file input.");
306 string const dir = OnlyPath(name);
307 if (!AbsolutePath(dir)) {
308 error_message = N_("The absolute path is required.");
318 error_message = N_("Directory does not exist.");
323 error_message = N_("Cannot read from this directory.");
328 if (dir == name || f.isDir()) {
329 error_message = N_("A file is required, not a directory.");
334 error_message = N_("File does not exist.");
339 error_message = N_("Cannot read from this file.");