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);
78 string length = strip(frontStrip(fl_get_input(input)));
82 string const units = strip(frontStrip(fl_get_choice_text(choice)));
84 return length + units;
88 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
92 lyx::Assert(input && input->objclass == FL_INPUT &&
93 choice && choice->objclass == FL_CHOICE);
95 // The unit is presumed to begin at the first char a-z
96 string const tmp = lowercase(strip(frontStrip(str)));
98 string::const_iterator p = tmp.begin();
99 for (; p != tmp.end(); ++p) {
100 if (*p >= 'a' && *p <= 'z')
105 int unitpos = 1; // xforms has Fortran-style indexing
107 if (p == tmp.end()) {
112 string tmplen = string(tmp.begin(), p);
113 if (isStrDbl(tmplen))
115 string unit = string(p+1, tmp.end());
117 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
118 string const text = fl_get_choice_item_text(choice,i+1);
119 if (unit == lowercase(strip(frontStrip(text)))) {
126 fl_set_input(input, len.c_str());
127 fl_set_choice(choice, unitpos);
130 // Take a string and add breaks so that it fits into a desired label width, w
131 string formatted(string const & sin, int w, int size, int style)
133 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
136 if (sin.empty()) return sout;
138 // breaks in up into a vector of individual words
139 vector<string> sentence;
141 for (string::const_iterator sit = sin.begin();
142 sit != sin.end(); ++sit) {
143 if ((*sit) == ' ' || (*sit) == '\n') {
145 sentence.push_back(word);
148 if ((*sit) == '\n') word += '\n';
155 // Flush remaining contents of word
156 if (!word.empty() ) sentence.push_back(word);
159 string line_plus_word;
160 for (vector<string>::const_iterator vit = sentence.begin();
161 vit != sentence.end(); ++vit) {
168 line_plus_word.erase();
172 if (!line_plus_word.empty() ) line_plus_word += ' ';
173 line_plus_word += word;
175 int const length = fl_get_string_width(style, size,
176 line_plus_word.c_str(),
177 int(line_plus_word.length()));
180 line_plus_word = word;
183 line = line_plus_word;
185 // Flush remaining contents of line
190 if (sout[sout.length() - 1] == '\n')
191 sout.erase(sout.length() - 1);
199 // sorted by hand to prevent LyXLex from complaining on read().
200 keyword_item xformTags[] = {
201 { "\\gui_background", FL_COL1 },
202 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
203 { "\\gui_buttonleft", FL_LEFT_BCOL },
204 { "\\gui_buttonright", FL_RIGHT_BCOL },
205 { "\\gui_buttontop", FL_TOP_BCOL },
206 { "\\gui_inactive", FL_INACTIVE },
207 { "\\gui_push_button", FL_YELLOW },
208 { "\\gui_selected", FL_MCOL },
209 { "\\gui_text", FL_BLACK }
213 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
218 bool XformsColor::read(string const & filename)
220 LyXLex lexrc(xformTags, xformCount);
221 if (!lexrc.setFile(filename))
224 while (lexrc.isOK()) {
225 int const le = lexrc.lex();
228 case LyXLex::LEX_UNDEF:
229 lexrc.printError("Unknown tag `$$Token'");
231 case LyXLex::LEX_FEOF:
238 if (!lexrc.next()) break;
239 col.r = lexrc.getInteger();
241 if (!lexrc.next()) break;
242 col.g = lexrc.getInteger();
244 if (!lexrc.next()) break;
245 col.b = lexrc.getInteger();
247 fl_mapcolor(le, col.r, col.g, col.b);
254 bool XformsColor::write(string const & filename)
256 ofstream os(filename.c_str());
260 os << "### This file is part of\n"
261 << "### ========================================================\n"
262 << "### LyX, The Document Processor\n"
264 << "### Copyright 1995 Matthias Ettrich\n"
265 << "### Copyright 1995-2001 The LyX Team.\n"
267 << "### ========================================================\n"
269 << "# This file is written by LyX, if you want to make your own\n"
270 << "# modifications you should do them from inside LyX and save\n"
273 for (int i = 0; i < xformCount; ++i) {
274 string const tag = xformTags[i].tag;
275 int const colorID = xformTags[i].code;
278 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
281 << color.r << " " << color.g << " " << color.b << "\n";
288 string RWInfo::error_message;
290 bool RWInfo::WriteableDir(string const & name)
292 error_message.erase();
294 if (!AbsolutePath(name)) {
295 error_message = N_("The absolute path is required.");
299 FileInfo const tp(name);
301 error_message = N_("Directory does not exist.");
305 if (!tp.writable()) {
306 error_message = N_("Cannot write to this directory.");
314 bool RWInfo::ReadableDir(string const & name)
316 error_message.erase();
318 if (!AbsolutePath(name)) {
319 error_message = N_("The absolute path is required.");
323 FileInfo const tp(name);
325 error_message = N_("Directory does not exist.");
329 if (!tp.readable()) {
330 error_message = N_("Cannot read this directory.");
338 bool RWInfo::WriteableFile(string const & name)
340 // A writeable file is either:
341 // * An existing file to which we have write access, or
342 // * A file that doesn't yet exist but that would exist in a writeable
345 error_message.erase();
348 error_message = N_("No file input.");
352 string const dir = OnlyPath(name);
353 if (!AbsolutePath(dir)) {
354 error_message = N_("The absolute path is required.");
364 error_message = N_("Directory does not exist.");
369 error_message = N_("Cannot write to this directory.");
374 if (dir == name || f.isDir()) {
375 error_message = N_("A file is required, not a directory.");
379 if (f.exist() && !f.writable()) {
380 error_message = N_("Cannot write to this file.");
388 bool RWInfo::ReadableFile(string const & name)
390 error_message.erase();
393 error_message = N_("No file input.");
397 string const dir = OnlyPath(name);
398 if (!AbsolutePath(dir)) {
399 error_message = N_("The absolute path is required.");
409 error_message = N_("Directory does not exist.");
414 error_message = N_("Cannot read from this directory.");
419 if (dir == name || f.isDir()) {
420 error_message = N_("A file is required, not a directory.");
425 error_message = N_("File does not exist.");
430 error_message = N_("Cannot read from this file.");