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.
13 #include "xforms_helpers.h"
17 #include "lengthcommon.h"
18 #include "lyxgluelength.h"
21 #include "support/FileInfo.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h" // frontStrip, strip
24 #include "support/tostr.h"
26 #include "lyx_forms.h"
29 #include <boost/assert.hpp>
33 using lyx::support::AbsolutePath;
34 using lyx::support::FileInfo;
35 using lyx::support::isStrDbl;
36 using lyx::support::OnlyPath;
37 using lyx::support::subst;
38 using lyx::support::trim;
47 std::string const buildChoiceLengthString()
50 for (int i = 0; i != num_units; ++i) {
53 data += subst(unit_name_gui[i], "%", "%%");
59 bool isActive(FL_OBJECT * ob)
61 return ob && ob->active > 0;
65 std::pair<string, string> parse_shortcut(string const & str)
67 string::size_type i = str.find_first_of("&");
68 if (i == string::npos || i == str.length() - 1)
69 return make_pair(str, string());
73 string::value_type c = str[i + 1];
74 return make_pair(str.substr(0, i) + str.substr(i + 1),
79 // A wrapper for the xforms routine, but this one accepts uint args
80 unsigned long fl_getmcolor(int i,
81 unsigned int * r, unsigned int * g, unsigned int * b)
84 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
92 // Set an FL_OBJECT to activated or deactivated
93 void setEnabled(FL_OBJECT * ob, bool enable)
96 fl_activate_object(ob);
97 fl_set_object_lcol(ob, FL_LCOL);
99 fl_deactivate_object(ob);
100 fl_set_object_lcol(ob, FL_INACTIVE);
105 // Given an fl_choice or an fl_browser, create a vector of its entries
106 vector<string> const getVector(FL_OBJECT * ob)
110 switch (ob->objclass) {
112 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
113 string const text = fl_get_choice_item_text(ob, i+1);
114 vec.push_back(trim(text));
118 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
119 string const text = fl_get_browser_line(ob, i+1);
120 vec.push_back(trim(text));
132 string const getString(FL_OBJECT * ob, int line)
134 // Negative line value does not make sense.
135 BOOST_ASSERT(line >= 0);
137 char const * tmp = 0;
138 switch (ob->objclass) {
140 tmp = fl_get_input(ob);
145 line = fl_get_browser(ob);
147 if (line >= 1 && line <= fl_get_browser_maxline(ob))
148 tmp = fl_get_browser_line(ob, line);
153 line = fl_get_choice(ob);
155 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
156 tmp = fl_get_choice_item_text(ob, line);
160 tmp = fl_get_combox_text(ob);
167 return tmp ? trim(tmp) : string();
170 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
173 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
174 choice && choice->objclass == FL_CHOICE);
176 string const length = trim(fl_get_input(input));
180 // don't return unit-from-choice if the input(field) contains a unit
181 if (isValidGlueLength(length))
184 string unit = trim(fl_get_choice_text(choice));
185 unit = subst(unit, "%%", "%");
187 return length + unit;
191 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
193 string const & default_unit)
196 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
197 choice && choice->objclass == FL_CHOICE);
199 // use input field only for gluelengths
200 if (!isValidLength(str) && !isStrDbl(str)) {
201 fl_set_input(input, str.c_str());
202 // we assume that "default_unit" is in the choice as "we"
203 // have control over that!
204 // No need to check for its presence in the choice, therefore.
205 fl_set_choice_text(choice, default_unit.c_str());
207 updateWidgetsFromLength(input, choice,
208 LyXLength(str), default_unit);
213 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
214 LyXLength const & len,
215 string const & default_unit)
218 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
219 choice && choice->objclass == FL_CHOICE);
222 fl_set_input(input, "");
223 fl_set_choice_text(choice, default_unit.c_str());
225 fl_set_input(input, tostr(len.value()).c_str());
227 // Set the choice to the desired unit, if present in the choice.
228 // Else set the choice to the default unit.
229 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
231 vector<string> const vec = getVector(choice);
232 vector<string>::const_iterator it =
233 std::find(vec.begin(), vec.end(), unit);
234 if (it != vec.end()) {
235 fl_set_choice_text(choice, unit.c_str());
237 fl_set_choice_text(choice, default_unit.c_str());
243 // Take a string and add breaks so that it fits into a desired label width, w
244 string formatted(string const & sin, int w, int size, int style)
250 string::size_type curpos = 0;
253 string::size_type const nxtpos1 = sin.find(' ', curpos);
254 string::size_type const nxtpos2 = sin.find('\n', curpos);
255 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
257 string const word = nxtpos == string::npos ?
258 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
260 bool const newline = (nxtpos2 != string::npos &&
263 string const line_plus_word =
264 line.empty() ? word : line + ' ' + word;
267 fl_get_string_width(style, size,
268 line_plus_word.c_str(),
269 int(line_plus_word.length()));
280 } else if (newline) {
281 sout += line_plus_word + '\n';
290 if (nxtpos == string::npos) {
303 void setCursorColor(int color)
305 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
306 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
307 fl_set_cursor_color(XC_watch, color, FL_WHITE);
308 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
314 // sorted by hand to prevent LyXLex from complaining on read().
315 keyword_item xformTags[] = {
316 { "\\gui_background", FL_COL1 },
317 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
318 { "\\gui_buttonleft", FL_LEFT_BCOL },
319 { "\\gui_buttonright", FL_RIGHT_BCOL },
320 { "\\gui_buttontop", FL_TOP_BCOL },
321 { "\\gui_inactive", FL_INACTIVE },
322 { "\\gui_pointer", FL_FREE_COL16 },
323 { "\\gui_push_button", FL_YELLOW },
324 { "\\gui_selected", FL_MCOL },
325 { "\\gui_text", FL_BLACK }
329 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
334 bool XformsColor::read(string const & filename)
336 FileInfo const f(filename);
337 LyXLex lexrc(xformTags, xformCount);
338 if (f.readable() && !lexrc.setFile(filename)) {
339 lyxerr << "XformsColor::read(" << filename << ")\n"
340 << _("Failed to open file.") << std::endl;
344 while (lexrc.isOK()) {
345 int const le = lexrc.lex();
348 case LyXLex::LEX_UNDEF:
349 lexrc.printError("Unknown tag `$$Token'");
351 case LyXLex::LEX_FEOF:
356 string const tag = lexrc.getString();
360 if (!lexrc.next()) break;
361 col.r = lexrc.getInteger();
363 if (!lexrc.next()) break;
364 col.g = lexrc.getInteger();
366 if (!lexrc.next()) break;
367 col.b = lexrc.getInteger();
369 fl_mapcolor(le, col.r, col.g, col.b);
371 if (tag == "\\gui_pointer") {
372 setCursorColor(FL_FREE_COL16);
380 bool XformsColor::write(string const & filename)
382 ofstream os(filename.c_str());
384 lyxerr << "XformsColor::write(" << filename << ")\n"
385 << _("Failed to open file.") << std::endl;
390 << "### file " << filename << "\n\n"
391 << "### This file is written by LyX, if you want to make your own\n"
392 << "### modifications you should do them from inside LyX and save\n"
395 for (int i = 0; i < xformCount; ++i) {
396 string const tag = xformTags[i].tag;
397 int const colorID = xformTags[i].code;
400 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
403 << color.r << ' ' << color.g << ' ' << color.b << '\n';
410 string RWInfo::error_message;
412 bool RWInfo::WriteableDir(string const & name)
414 error_message.erase();
416 if (!AbsolutePath(name)) {
417 error_message = _("The absolute path is required.");
421 FileInfo const tp(name);
422 if (!tp.isOK() || !tp.isDir()) {
423 error_message = _("Directory does not exist.");
427 if (!tp.writable()) {
428 error_message = _("Cannot write to this directory.");
436 bool RWInfo::ReadableDir(string const & name)
438 error_message.erase();
440 if (!AbsolutePath(name)) {
441 error_message = _("The absolute path is required.");
445 FileInfo const tp(name);
446 if (!tp.isOK() || !tp.isDir()) {
447 error_message = _("Directory does not exist.");
451 if (!tp.readable()) {
452 error_message = _("Cannot read this directory.");
460 bool RWInfo::WriteableFile(string const & name)
462 // A writeable file is either:
463 // * An existing file to which we have write access, or
464 // * A file that doesn't yet exist but that would exist in a writeable
467 error_message.erase();
470 error_message = _("No file input.");
474 string const dir = OnlyPath(name);
475 if (!AbsolutePath(dir)) {
476 error_message = _("The absolute path is required.");
482 if (!d.isOK() || !d.isDir()) {
486 if (!d.isOK() || !d.isDir()) {
487 error_message = _("Directory does not exist.");
492 error_message = _("Cannot write to this directory.");
497 if (dir == name || (f.isOK() && f.isDir())) {
498 error_message = _("A file is required, not a directory.");
502 if (f.isOK() && f.exist() && !f.writable()) {
503 error_message = _("Cannot write to this file.");
511 bool RWInfo::ReadableFile(string const & name)
513 error_message.erase();
516 error_message = _("No file input.");
520 string const dir = OnlyPath(name);
521 if (!AbsolutePath(dir)) {
522 error_message = _("The absolute path is required.");
528 if (!d.isOK() && !d.isDir()) {
532 if (!d.isOK() || !d.isDir()) {
533 error_message = _("Directory does not exist.");
538 error_message = _("Cannot read from this directory.");
543 if (dir == name || (f.isOK() && f.isDir())) {
544 error_message = _("A file is required, not a directory.");
549 error_message = _("File does not exist.");
554 error_message = _("Cannot read from this file.");