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 const 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);
96 fl_set_input(input, "");
97 fl_set_choice(choice, 1);
101 // The unit is presumed to begin at the first char a-z
102 string const tmp = lowercase(strip(frontStrip(str)));
104 string::const_iterator p = tmp.begin();
105 for (; p != tmp.end(); ++p) {
106 if (*p >= 'a' && *p <= 'z')
111 int unitpos = 1; // xforms has Fortran-style indexing
113 if (p == tmp.end()) {
118 string tmplen = string(tmp.begin(), p);
119 if (isStrDbl(tmplen))
121 string unit = string(p+1, tmp.end());
123 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
124 string const text = fl_get_choice_item_text(choice,i+1);
125 if (unit == lowercase(strip(frontStrip(text)))) {
132 fl_set_input(input, len.c_str());
133 fl_set_choice(choice, unitpos);
136 // Take a string and add breaks so that it fits into a desired label width, w
137 string formatted(string const & sin, int w, int size, int style)
139 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
142 if (sin.empty()) return sout;
144 // breaks in up into a vector of individual words
145 vector<string> sentence;
147 for (string::const_iterator sit = sin.begin();
148 sit != sin.end(); ++sit) {
149 if ((*sit) == ' ' || (*sit) == '\n') {
151 sentence.push_back(word);
154 if ((*sit) == '\n') word += '\n';
161 // Flush remaining contents of word
162 if (!word.empty() ) sentence.push_back(word);
165 string line_plus_word;
166 for (vector<string>::const_iterator vit = sentence.begin();
167 vit != sentence.end(); ++vit) {
174 line_plus_word.erase();
178 if (!line_plus_word.empty() ) line_plus_word += ' ';
179 line_plus_word += word;
181 int const length = fl_get_string_width(style, size,
182 line_plus_word.c_str(),
183 int(line_plus_word.length()));
186 line_plus_word = word;
189 line = line_plus_word;
191 // Flush remaining contents of line
196 if (sout[sout.length() - 1] == '\n')
197 sout.erase(sout.length() - 1);
205 // sorted by hand to prevent LyXLex from complaining on read().
206 keyword_item xformTags[] = {
207 { "\\gui_background", FL_COL1 },
208 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
209 { "\\gui_buttonleft", FL_LEFT_BCOL },
210 { "\\gui_buttonright", FL_RIGHT_BCOL },
211 { "\\gui_buttontop", FL_TOP_BCOL },
212 { "\\gui_inactive", FL_INACTIVE },
213 { "\\gui_push_button", FL_YELLOW },
214 { "\\gui_selected", FL_MCOL },
215 { "\\gui_text", FL_BLACK }
219 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
224 bool XformsColor::read(string const & filename)
226 LyXLex lexrc(xformTags, xformCount);
227 if (!lexrc.setFile(filename))
230 while (lexrc.isOK()) {
231 int const le = lexrc.lex();
234 case LyXLex::LEX_UNDEF:
235 lexrc.printError("Unknown tag `$$Token'");
237 case LyXLex::LEX_FEOF:
244 if (!lexrc.next()) break;
245 col.r = lexrc.getInteger();
247 if (!lexrc.next()) break;
248 col.g = lexrc.getInteger();
250 if (!lexrc.next()) break;
251 col.b = lexrc.getInteger();
253 fl_mapcolor(le, col.r, col.g, col.b);
260 bool XformsColor::write(string const & filename)
262 ofstream os(filename.c_str());
266 os << "### This file is part of\n"
267 << "### ========================================================\n"
268 << "### LyX, The Document Processor\n"
270 << "### Copyright 1995 Matthias Ettrich\n"
271 << "### Copyright 1995-2001 The LyX Team.\n"
273 << "### ========================================================\n"
275 << "# This file is written by LyX, if you want to make your own\n"
276 << "# modifications you should do them from inside LyX and save\n"
279 for (int i = 0; i < xformCount; ++i) {
280 string const tag = xformTags[i].tag;
281 int const colorID = xformTags[i].code;
284 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
287 << color.r << " " << color.g << " " << color.b << "\n";
294 string RWInfo::error_message;
296 bool RWInfo::WriteableDir(string const & name)
298 error_message.erase();
300 if (!AbsolutePath(name)) {
301 error_message = N_("The absolute path is required.");
305 FileInfo const tp(name);
307 error_message = N_("Directory does not exist.");
311 if (!tp.writable()) {
312 error_message = N_("Cannot write to this directory.");
320 bool RWInfo::ReadableDir(string const & name)
322 error_message.erase();
324 if (!AbsolutePath(name)) {
325 error_message = N_("The absolute path is required.");
329 FileInfo const tp(name);
331 error_message = N_("Directory does not exist.");
335 if (!tp.readable()) {
336 error_message = N_("Cannot read this directory.");
344 bool RWInfo::WriteableFile(string const & name)
346 // A writeable file is either:
347 // * An existing file to which we have write access, or
348 // * A file that doesn't yet exist but that would exist in a writeable
351 error_message.erase();
354 error_message = N_("No file input.");
358 string const dir = OnlyPath(name);
359 if (!AbsolutePath(dir)) {
360 error_message = N_("The absolute path is required.");
370 error_message = N_("Directory does not exist.");
375 error_message = N_("Cannot write to this directory.");
380 if (dir == name || f.isDir()) {
381 error_message = N_("A file is required, not a directory.");
385 if (f.exist() && !f.writable()) {
386 error_message = N_("Cannot write to this file.");
394 bool RWInfo::ReadableFile(string const & name)
396 error_message.erase();
399 error_message = N_("No file input.");
403 string const dir = OnlyPath(name);
404 if (!AbsolutePath(dir)) {
405 error_message = N_("The absolute path is required.");
415 error_message = N_("Directory does not exist.");
420 error_message = N_("Cannot read from this directory.");
425 if (dir == name || f.isDir()) {
426 error_message = N_("A file is required, not a directory.");
431 error_message = N_("File does not exist.");
436 error_message = N_("Cannot read from this file.");