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
21 #include "support/LAssert.h"
27 // Set an FL_OBJECT to activated or deactivated
28 void setEnabled(FL_OBJECT * ob, bool enable)
31 fl_activate_object(ob);
32 fl_set_object_lcol(ob, FL_BLACK);
34 fl_deactivate_object(ob);
35 fl_set_object_lcol(ob, FL_INACTIVE);
40 // Given an fl_choice, create a vector of its entries
41 vector<string> const getVectorFromChoice(FL_OBJECT * ob)
44 if (!ob || ob->objclass != FL_CHOICE)
47 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
48 string const text = fl_get_choice_item_text(ob, i+1);
49 vec.push_back(strip(frontStrip(text)));
56 // Given an fl_browser, create a vector of its entries
57 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
60 if (!ob || ob->objclass != FL_BROWSER)
63 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
64 string const text = fl_get_browser_line(ob, i+1);
65 vec.push_back(strip(frontStrip(text)));
72 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
75 lyx::Assert(input && input->objclass == FL_INPUT &&
76 choice && choice->objclass == FL_CHOICE);
80 string len = strip(frontStrip(fl_get_input(input)));
84 string const units = strip(frontStrip(fl_get_choice_text(choice)));
90 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
94 lyx::Assert(input && input->objclass == FL_INPUT &&
95 choice && choice->objclass == FL_CHOICE);
97 // The unit is presumed to begin at the first char a-z
98 string const tmp = lowercase(strip(frontStrip(str)));
100 string::const_iterator p = tmp.begin();
101 for (; p != tmp.end(); ++p) {
102 if (*p >= 'a' && *p <= 'z')
107 int unitpos = 1; // xforms has Fortran-style indexing
109 if (p == tmp.end()) {
114 string tmplen = string(tmp.begin(), p);
115 if (isStrDbl(tmplen))
117 string unit = string(p+1, tmp.end());
119 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
120 string const text = fl_get_choice_item_text(choice,i+1);
121 if (unit == lowercase(strip(frontStrip(text)))) {
128 fl_set_input(input, len.c_str());
129 fl_set_choice(choice, unitpos);
132 // Take a string and add breaks so that it fits into a desired label width, w
133 string formatted(string const & sin, int w, int size, int style)
135 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
138 if (sin.empty()) return sout;
140 // breaks in up into a vector of individual words
141 vector<string> sentence;
143 for (string::const_iterator sit = sin.begin();
144 sit != sin.end(); ++sit) {
145 if ((*sit) == ' ' || (*sit) == '\n') {
147 sentence.push_back(word);
150 if ((*sit) == '\n') word += '\n';
157 // Flush remaining contents of word
158 if (!word.empty() ) sentence.push_back(word);
161 string line_plus_word;
162 for (vector<string>::const_iterator vit = sentence.begin();
163 vit != sentence.end(); ++vit) {
170 line_plus_word.erase();
174 if (!line_plus_word.empty() ) line_plus_word += ' ';
175 line_plus_word += word;
177 int const length = fl_get_string_width(style, size,
178 line_plus_word.c_str(),
179 int(line_plus_word.length()));
182 line_plus_word = word;
185 line = line_plus_word;
187 // Flush remaining contents of line
192 if (sout[sout.length() - 1] == '\n')
193 sout.erase(sout.length() - 1);
201 // sorted by hand to prevent LyXLex from complaining on read().
202 keyword_item xformTags[] = {
203 { "\\gui_background", FL_COL1 },
204 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
205 { "\\gui_buttonleft", FL_LEFT_BCOL },
206 { "\\gui_buttonright", FL_RIGHT_BCOL },
207 { "\\gui_buttontop", FL_TOP_BCOL },
208 { "\\gui_inactive", FL_INACTIVE },
209 { "\\gui_push_button", FL_YELLOW },
210 { "\\gui_selected", FL_MCOL },
211 { "\\gui_text", FL_BLACK }
215 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
220 bool XformsColor::read(string const & filename)
222 LyXLex lexrc(xformTags, xformCount);
223 if (!lexrc.setFile(filename))
226 while (lexrc.isOK()) {
227 int const le = lexrc.lex();
230 case LyXLex::LEX_UNDEF:
231 lexrc.printError("Unknown tag `$$Token'");
233 case LyXLex::LEX_FEOF:
240 if (!lexrc.next()) break;
241 col.r = lexrc.getInteger();
243 if (!lexrc.next()) break;
244 col.g = lexrc.getInteger();
246 if (!lexrc.next()) break;
247 col.b = lexrc.getInteger();
249 fl_mapcolor(le, col.r, col.g, col.b);
256 bool XformsColor::write(string const & filename)
258 ofstream os(filename.c_str());
262 os << "### This file is part of\n"
263 << "### ========================================================\n"
264 << "### LyX, The Document Processor\n"
266 << "### Copyright 1995 Matthias Ettrich\n"
267 << "### Copyright 1995-2001 The LyX Team.\n"
269 << "### ========================================================\n"
271 << "# This file is written by LyX, if you want to make your own\n"
272 << "# modifications you should do them from inside LyX and save\n"
275 for (int i = 0; i < xformCount; ++i) {
276 string const tag = xformTags[i].tag;
277 int const colorID = xformTags[i].code;
280 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
283 << color.r << " " << color.g << " " << color.b << "\n";
290 string RWInfo::error_message;
292 bool RWInfo::WriteableDir(string const & name)
294 error_message.erase();
296 if (!AbsolutePath(name)) {
297 error_message = N_("The absolute path is required.");
301 FileInfo const tp(name);
303 error_message = N_("Directory does not exist.");
307 if (!tp.writable()) {
308 error_message = N_("Cannot write to this directory.");
316 bool RWInfo::ReadableDir(string const & name)
318 error_message.erase();
320 if (!AbsolutePath(name)) {
321 error_message = N_("The absolute path is required.");
325 FileInfo const tp(name);
327 error_message = N_("Directory does not exist.");
331 if (!tp.readable()) {
332 error_message = N_("Cannot read this directory.");
340 bool RWInfo::WriteableFile(string const & name)
342 // A writeable file is either:
343 // * An existing file to which we have write access, or
344 // * A file that doesn't yet exist but that would exist in a writeable
347 error_message.erase();
350 error_message = N_("No file input.");
354 string const dir = OnlyPath(name);
355 if (!AbsolutePath(dir)) {
356 error_message = N_("The absolute path is required.");
366 error_message = N_("Directory does not exist.");
371 error_message = N_("Cannot write to this directory.");
376 if (dir == name || f.isDir()) {
377 error_message = N_("A file is required, not a directory.");
381 if (f.exist() && !f.writable()) {
382 error_message = N_("Cannot write to this file.");
390 bool RWInfo::ReadableFile(string const & name)
392 error_message.erase();
395 error_message = N_("No file input.");
399 string const dir = OnlyPath(name);
400 if (!AbsolutePath(dir)) {
401 error_message = N_("The absolute path is required.");
411 error_message = N_("Directory does not exist.");
416 error_message = N_("Cannot read from this directory.");
421 if (dir == name || f.isDir()) {
422 error_message = N_("A file is required, not a directory.");
427 error_message = N_("File does not exist.");
432 error_message = N_("Cannot read from this file.");