2 * \file xforms_helpers.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS
14 #include "xforms_helpers.h"
18 #include "lyxlength.h"
19 #include "lyxgluelength.h"
21 #include "support/LAssert.h"
22 #include "support/FileInfo.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h" // frontStrip, strip
30 #include FORMS_H_LOCATION
36 bool isActive(FL_OBJECT * ob)
38 return ob && ob->active > 0;
42 // A wrapper for the xforms routine, but this one accepts uint args
43 unsigned long fl_getmcolor(int i,
44 unsigned int * r, unsigned int * g, unsigned int * b)
47 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
55 // Set an FL_OBJECT to activated or deactivated
56 void setEnabled(FL_OBJECT * ob, bool enable)
59 fl_activate_object(ob);
60 fl_set_object_lcol(ob, FL_LCOL);
62 fl_deactivate_object(ob);
63 fl_set_object_lcol(ob, FL_INACTIVE);
68 // Given an fl_choice or an fl_browser, create a vector of its entries
69 vector<string> const getVector(FL_OBJECT * ob)
73 switch (ob->objclass) {
75 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
76 string const text = fl_get_choice_item_text(ob, i+1);
77 vec.push_back(trim(text));
81 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
82 string const text = fl_get_browser_line(ob, i+1);
83 vec.push_back(trim(text));
95 string const getString(FL_OBJECT * ob, int line)
97 // Negative line value does not make sense.
98 lyx::Assert(line >= 0);
100 char const * tmp = 0;
101 switch (ob->objclass) {
103 tmp = fl_get_input(ob);
108 line = fl_get_browser(ob);
110 if (line >= 1 && line <= fl_get_browser_maxline(ob))
111 tmp = fl_get_browser_line(ob, line);
116 line = fl_get_choice(ob);
118 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
119 tmp = fl_get_choice_item_text(ob, line);
126 return tmp ? trim(tmp) : string();
129 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
132 lyx::Assert(input && input->objclass == FL_INPUT &&
133 choice && choice->objclass == FL_CHOICE);
135 string const length = trim(fl_get_input(input));
139 // don't return unit-from-choice if the input(field) contains a unit
140 if (isValidGlueLength(length))
143 string unit = trim(fl_get_choice_text(choice));
144 unit = subst(unit, "%%", "%");
146 return length + unit;
150 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
152 string const & default_unit)
154 // use input field only for gluelengths
155 if (!isValidLength(str) && !isStrDbl(str)) {
156 fl_set_input(input, str.c_str());
157 // we assume that "default_unit" is in the choice as "we"
158 // have control over that!
159 // No need to check for its presence in the choice, therefore.
160 fl_set_choice_text(choice, default_unit.c_str());
162 updateWidgetsFromLength(input, choice,
163 LyXLength(str), default_unit);
168 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
169 LyXLength const & len,
170 string const & default_unit)
173 lyx::Assert(input && input->objclass == FL_INPUT &&
174 choice && choice->objclass == FL_CHOICE);
177 fl_set_input(input, "");
178 fl_set_choice_text(choice, default_unit.c_str());
180 ostringstream buffer;
181 buffer << len.value();
182 fl_set_input(input, buffer.str().c_str());
184 // Set the choice to the desired unit, if present in the choice.
185 // Else set the choice to the default unit.
186 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
188 vector<string> const vec = getVector(choice);
189 vector<string>::const_iterator it =
190 std::find(vec.begin(), vec.end(), unit);
191 if (it != vec.end()) {
192 fl_set_choice_text(choice, unit.c_str());
194 fl_set_choice_text(choice, default_unit.c_str());
200 // Take a string and add breaks so that it fits into a desired label width, w
201 string formatted(string const & sin, int w, int size, int style)
204 if (sin.empty()) return sout;
206 string::size_type curpos = 0;
209 string::size_type const nxtpos1 = sin.find(' ', curpos);
210 string::size_type const nxtpos2 = sin.find('\n', curpos);
211 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
213 string const word = nxtpos == string::npos ?
214 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
216 bool const newline = (nxtpos2 != string::npos &&
219 string const line_plus_word =
220 line.empty() ? word : line + ' ' + word;
223 fl_get_string_width(style, size,
224 line_plus_word.c_str(),
225 int(line_plus_word.length()));
236 } else if (newline) {
237 sout += line_plus_word + '\n';
246 if (nxtpos == string::npos) {
259 void setCursorColor(int color)
261 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
262 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
263 fl_set_cursor_color(XC_watch, color, FL_WHITE);
264 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
270 // sorted by hand to prevent LyXLex from complaining on read().
271 keyword_item xformTags[] = {
272 { "\\gui_background", FL_COL1 },
273 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
274 { "\\gui_buttonleft", FL_LEFT_BCOL },
275 { "\\gui_buttonright", FL_RIGHT_BCOL },
276 { "\\gui_buttontop", FL_TOP_BCOL },
277 { "\\gui_inactive", FL_INACTIVE },
278 { "\\gui_pointer", FL_FREE_COL16 },
279 { "\\gui_push_button", FL_YELLOW },
280 { "\\gui_selected", FL_MCOL },
281 { "\\gui_text", FL_BLACK }
285 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
290 bool XformsColor::read(string const & filename)
292 LyXLex lexrc(xformTags, xformCount);
293 if (!lexrc.setFile(filename))
296 while (lexrc.isOK()) {
297 int const le = lexrc.lex();
300 case LyXLex::LEX_UNDEF:
301 lexrc.printError("Unknown tag `$$Token'");
303 case LyXLex::LEX_FEOF:
308 string const tag = lexrc.getString();
312 if (!lexrc.next()) break;
313 col.r = lexrc.getInteger();
315 if (!lexrc.next()) break;
316 col.g = lexrc.getInteger();
318 if (!lexrc.next()) break;
319 col.b = lexrc.getInteger();
321 fl_mapcolor(le, col.r, col.g, col.b);
323 if (tag == "\\gui_pointer") {
324 setCursorColor(FL_FREE_COL16);
332 bool XformsColor::write(string const & filename)
334 ofstream os(filename.c_str());
339 << "### file " << filename << "\n\n"
340 << "### This file is written by LyX, if you want to make your own\n"
341 << "### modifications you should do them from inside LyX and save\n"
344 for (int i = 0; i < xformCount; ++i) {
345 string const tag = xformTags[i].tag;
346 int const colorID = xformTags[i].code;
349 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
352 << color.r << ' ' << color.g << ' ' << color.b << '\n';
359 string RWInfo::error_message;
361 bool RWInfo::WriteableDir(string const & name)
363 error_message.erase();
365 if (!AbsolutePath(name)) {
366 error_message = _("The absolute path is required.");
370 FileInfo const tp(name);
371 if (!tp.isOK() || !tp.isDir()) {
372 error_message = _("Directory does not exist.");
376 if (!tp.writable()) {
377 error_message = _("Cannot write to this directory.");
385 bool RWInfo::ReadableDir(string const & name)
387 error_message.erase();
389 if (!AbsolutePath(name)) {
390 error_message = _("The absolute path is required.");
394 FileInfo const tp(name);
395 if (!tp.isOK() || !tp.isDir()) {
396 error_message = _("Directory does not exist.");
400 if (!tp.readable()) {
401 error_message = _("Cannot read this directory.");
409 bool RWInfo::WriteableFile(string const & name)
411 // A writeable file is either:
412 // * An existing file to which we have write access, or
413 // * A file that doesn't yet exist but that would exist in a writeable
416 error_message.erase();
419 error_message = _("No file input.");
423 string const dir = OnlyPath(name);
424 if (!AbsolutePath(dir)) {
425 error_message = _("The absolute path is required.");
431 if (!d.isOK() || !d.isDir()) {
435 if (!d.isOK() || !d.isDir()) {
436 error_message = _("Directory does not exist.");
441 error_message = _("Cannot write to this directory.");
446 if (dir == name || (f.isOK() && f.isDir())) {
447 error_message = _("A file is required, not a directory.");
451 if (f.isOK() && f.exist() && !f.writable()) {
452 error_message = _("Cannot write to this file.");
460 bool RWInfo::ReadableFile(string const & name)
462 error_message.erase();
465 error_message = _("No file input.");
469 string const dir = OnlyPath(name);
470 if (!AbsolutePath(dir)) {
471 error_message = _("The absolute path is required.");
477 if (!d.isOK() && !d.isDir()) {
481 if (!d.isOK() || !d.isDir()) {
482 error_message = _("Directory does not exist.");
487 error_message = _("Cannot read from this directory.");
492 if (dir == name || (f.isOK() && f.isDir())) {
493 error_message = _("A file is required, not a directory.");
498 error_message = _("File does not exist.");
503 error_message = _("Cannot read from this file.");