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') {
53 sentence.push_back(word);
55 if ((*sit) == '\n') word += '\n';
62 // Flush remaining contents of word
63 if (!word.empty() ) sentence.push_back(word);
66 string line_plus_word;
67 for (vector<string>::const_iterator vit = sentence.begin();
68 vit != sentence.end(); ++vit) {
74 word = word.substr(1);
75 line_plus_word.erase();
79 if (!line_plus_word.empty() ) line_plus_word += ' ';
80 line_plus_word += word;
82 int const length = fl_get_string_width(style, size,
83 line_plus_word.c_str(),
84 int(line_plus_word.length()));
87 line_plus_word = word;
90 line = line_plus_word;
92 // Flush remaining contents of line
97 if (sout[sout.length() - 1] == '\n')
98 sout.erase(sout.length() - 1);
106 // sorted by hand to prevent LyXLex from complaining on read().
107 keyword_item xformTags[] = {
108 { "\\gui_background", FL_COL1 },
109 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
110 { "\\gui_buttonleft", FL_LEFT_BCOL },
111 { "\\gui_buttonright", FL_RIGHT_BCOL },
112 { "\\gui_buttontop", FL_TOP_BCOL },
113 { "\\gui_inactive", FL_INACTIVE },
114 { "\\gui_push_button", FL_YELLOW },
115 { "\\gui_selected", FL_MCOL },
116 { "\\gui_text", FL_BLACK }
120 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
125 bool XformsColor::read(string const & filename)
127 LyXLex lexrc(xformTags, xformCount);
128 if (!lexrc.setFile(filename))
131 while (lexrc.IsOK()) {
132 int const le = lexrc.lex();
135 case LyXLex::LEX_UNDEF:
136 lexrc.printError("Unknown tag `$$Token'");
138 case LyXLex::LEX_FEOF:
145 if (!lexrc.next()) break;
146 col.r = lexrc.GetInteger();
148 if (!lexrc.next()) break;
149 col.g = lexrc.GetInteger();
151 if (!lexrc.next()) break;
152 col.b = lexrc.GetInteger();
154 fl_mapcolor(le, col.r, col.g, col.b);
161 bool XformsColor::write(string const & filename)
163 ofstream os(filename.c_str());
167 os << "### This file is part of\n"
168 << "### ========================================================\n"
169 << "### LyX, The Document Processor\n"
171 << "### Copyright 1995 Matthias Ettrich\n"
172 << "### Copyright 1995-2000 The LyX Team.\n"
174 << "### ========================================================\n"
176 << "# This file is written by LyX, if you want to make your own\n"
177 << "# modifications you should do them from inside LyX and save\n"
180 for (int i = 0; i < xformCount; ++i) {
181 string const tag = xformTags[i].tag;
182 int const colorID = xformTags[i].code;
185 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
188 << color.r << " " << color.g << " " << color.b << "\n";
195 string RWInfo::error_message;
197 bool RWInfo::WriteableDir(string const & name)
199 error_message.erase();
201 if (!AbsolutePath(name)) {
202 error_message = N_("The absolute path is required.");
206 FileInfo const tp(name);
208 error_message = N_("Directory does not exist.");
212 if (!tp.writable()) {
213 error_message = N_("Cannot write to this directory.");
221 bool RWInfo::ReadableDir(string const & name)
223 error_message.erase();
225 if (!AbsolutePath(name)) {
226 error_message = N_("The absolute path is required.");
230 FileInfo const tp(name);
232 error_message = N_("Directory does not exist.");
236 if (!tp.readable()) {
237 error_message = N_("Cannot read this directory.");
245 bool RWInfo::WriteableFile(string const & name)
247 // A writeable file is either:
248 // * An existing file to which we have write access, or
249 // * A file that doesn't yet exist but that would exist in a writeable
252 error_message.erase();
255 error_message = N_("No file input.");
259 string const dir = OnlyPath(name);
260 if (!AbsolutePath(dir)) {
261 error_message = N_("The absolute path is required.");
271 error_message = N_("Directory does not exist.");
276 error_message = N_("Cannot write to this directory.");
281 if (dir == name || f.isDir()) {
282 error_message = N_("A file is required, not a directory.");
286 if (f.exist() && !f.writable()) {
287 error_message = N_("Cannot write to this file.");
295 bool RWInfo::ReadableFile(string const & name)
297 error_message.erase();
300 error_message = N_("No file input.");
304 string const dir = OnlyPath(name);
305 if (!AbsolutePath(dir)) {
306 error_message = N_("The absolute path is required.");
316 error_message = N_("Directory does not exist.");
321 error_message = N_("Cannot read from this directory.");
326 if (dir == name || f.isDir()) {
327 error_message = N_("A file is required, not a directory.");
332 error_message = N_("File does not exist.");
337 error_message = N_("Cannot read from this file.");