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 #warning Why cant this be done by a one pass algo? (Lgb)
43 if (sin.empty()) return sout;
45 // break sin up into a vector of individual words
46 vector<string> sentence;
48 for (string::const_iterator sit = sin.begin();
49 sit != sin.end(); ++sit) {
50 if ((*sit) == ' ' || (*sit) == '\n') {
51 sentence.push_back(word);
53 if ((*sit) == '\n') word += '\n';
60 // Flush remaining contents of word
61 if (!word.empty() ) sentence.push_back(word);
64 string line_plus_word;
65 for (vector<string>::const_iterator vit = sentence.begin();
66 vit != sentence.end(); ++vit) {
72 word = word.substr(1);
73 line_plus_word.erase();
77 if (!line_plus_word.empty() ) line_plus_word += ' ';
78 line_plus_word += word;
80 int const length = fl_get_string_width(style, size,
81 line_plus_word.c_str(),
82 int(line_plus_word.length()));
85 line_plus_word = word;
88 line = line_plus_word;
90 // Flush remaining contents of line
95 if (sout[sout.length() - 1] == '\n')
96 sout.erase(sout.length() - 1);
104 // sorted by hand to prevent LyXLex from complaining on read().
105 keyword_item xformTags[] = {
106 { "\\gui_background", FL_COL1 },
107 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
108 { "\\gui_buttonleft", FL_LEFT_BCOL },
109 { "\\gui_buttonright", FL_RIGHT_BCOL },
110 { "\\gui_buttontop", FL_TOP_BCOL },
111 { "\\gui_inactive", FL_INACTIVE },
112 { "\\gui_push_button", FL_YELLOW },
113 { "\\gui_selected", FL_MCOL },
114 { "\\gui_text", FL_BLACK }
118 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
123 bool XformsColor::read(string const & filename)
125 LyXLex lexrc(xformTags, xformCount);
126 if (!lexrc.setFile(filename))
129 while (lexrc.IsOK()) {
130 int const le = lexrc.lex();
133 case LyXLex::LEX_UNDEF:
134 lexrc.printError("Unknown tag `$$Token'");
136 case LyXLex::LEX_FEOF:
143 if (!lexrc.next()) break;
144 col.r = lexrc.GetInteger();
146 if (!lexrc.next()) break;
147 col.g = lexrc.GetInteger();
149 if (!lexrc.next()) break;
150 col.b = lexrc.GetInteger();
152 fl_mapcolor(le, col.r, col.g, col.b);
159 bool XformsColor::write(string const & filename)
161 ofstream os(filename.c_str());
165 os << "### This file is part of\n"
166 << "### ========================================================\n"
167 << "### LyX, The Document Processor\n"
169 << "### Copyright 1995 Matthias Ettrich\n"
170 << "### Copyright 1995-2000 The LyX Team.\n"
172 << "### ========================================================\n"
174 << "# This file is written by LyX, if you want to make your own\n"
175 << "# modifications you should do them from inside LyX and save\n"
178 for (int i = 0; i < xformCount; ++i) {
179 string const tag = xformTags[i].tag;
180 int const colorID = xformTags[i].code;
183 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
186 << color.r << " " << color.g << " " << color.b << "\n";
193 string RWInfo::error_message;
195 bool RWInfo::WriteableDir(string const & name)
197 error_message.erase();
199 if (!AbsolutePath(name)) {
200 error_message = N_("The absolute path is required.");
204 FileInfo const tp(name);
206 error_message = N_("Directory does not exist.");
210 if (!tp.writable()) {
211 error_message = N_("Cannot write to this directory.");
219 bool RWInfo::ReadableDir(string const & name)
221 error_message.erase();
223 if (!AbsolutePath(name)) {
224 error_message = N_("The absolute path is required.");
228 FileInfo const tp(name);
230 error_message = N_("Directory does not exist.");
234 if (!tp.readable()) {
235 error_message = N_("Cannot read this directory.");
243 bool RWInfo::WriteableFile(string const & name)
245 // A writeable file is either:
246 // * An existing file to which we have write access, or
247 // * A file that doesn't yet exist but that would exist in a writeable
250 error_message.erase();
253 error_message = N_("No file input.");
257 string const dir = OnlyPath(name);
258 if (!AbsolutePath(dir)) {
259 error_message = N_("The absolute path is required.");
269 error_message = N_("Directory does not exist.");
274 error_message = N_("Cannot write to this directory.");
279 if (dir == name || f.isDir()) {
280 error_message = N_("A file is required, not a directory.");
284 if (f.exist() && !f.writable()) {
285 error_message = N_("Cannot write to this file.");
293 bool RWInfo::ReadableFile(string const & name)
295 error_message.erase();
298 error_message = N_("No file input.");
302 string const dir = OnlyPath(name);
303 if (!AbsolutePath(dir)) {
304 error_message = N_("The absolute path is required.");
314 error_message = N_("Directory does not exist.");
319 error_message = N_("Cannot read from this directory.");
324 if (dir == name || f.isDir()) {
325 error_message = N_("A file is required, not a directory.");
330 error_message = N_("File does not exist.");
335 error_message = N_("Cannot read from this file.");