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"
16 #include "lyxgluelength.h"
19 #include "support/FileInfo.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h" // frontStrip, strip
22 #include "support/tostr.h"
24 #include "lyx_forms.h"
27 #include <boost/assert.hpp>
31 using lyx::support::AbsolutePath;
32 using lyx::support::FileInfo;
33 using lyx::support::isStrDbl;
34 using lyx::support::OnlyPath;
35 using lyx::support::subst;
36 using lyx::support::trim;
45 bool isActive(FL_OBJECT * ob)
47 return ob && ob->active > 0;
51 std::pair<string, string> parse_shortcut(string const & str)
53 string::size_type i = str.find_first_of("&");
54 if (i == string::npos || i == str.length() - 1)
55 return make_pair(str, string());
59 string::value_type c = str[i + 1];
60 return make_pair(str.substr(0, i) + str.substr(i + 1),
65 // A wrapper for the xforms routine, but this one accepts uint args
66 unsigned long fl_getmcolor(int i,
67 unsigned int * r, unsigned int * g, unsigned int * b)
70 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
78 // Set an FL_OBJECT to activated or deactivated
79 void setEnabled(FL_OBJECT * ob, bool enable)
82 fl_activate_object(ob);
83 fl_set_object_lcol(ob, FL_LCOL);
85 fl_deactivate_object(ob);
86 fl_set_object_lcol(ob, FL_INACTIVE);
91 // Given an fl_choice or an fl_browser, create a vector of its entries
92 vector<string> const getVector(FL_OBJECT * ob)
96 switch (ob->objclass) {
98 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
99 string const text = fl_get_choice_item_text(ob, i+1);
100 vec.push_back(trim(text));
104 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
105 string const text = fl_get_browser_line(ob, i+1);
106 vec.push_back(trim(text));
118 string const getString(FL_OBJECT * ob, int line)
120 // Negative line value does not make sense.
121 BOOST_ASSERT(line >= 0);
123 char const * tmp = 0;
124 switch (ob->objclass) {
126 tmp = fl_get_input(ob);
131 line = fl_get_browser(ob);
133 if (line >= 1 && line <= fl_get_browser_maxline(ob))
134 tmp = fl_get_browser_line(ob, line);
139 line = fl_get_choice(ob);
141 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
142 tmp = fl_get_choice_item_text(ob, line);
146 tmp = fl_get_combox_text(ob);
153 return tmp ? trim(tmp) : string();
156 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
159 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
160 choice && choice->objclass == FL_CHOICE);
162 string const length = trim(fl_get_input(input));
166 // don't return unit-from-choice if the input(field) contains a unit
167 if (isValidGlueLength(length))
170 string unit = trim(fl_get_choice_text(choice));
171 unit = subst(unit, "%%", "%");
173 return length + unit;
177 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
179 string const & default_unit)
182 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
183 choice && choice->objclass == FL_CHOICE);
185 // use input field only for gluelengths
186 if (!isValidLength(str) && !isStrDbl(str)) {
187 fl_set_input(input, str.c_str());
188 // we assume that "default_unit" is in the choice as "we"
189 // have control over that!
190 // No need to check for its presence in the choice, therefore.
191 fl_set_choice_text(choice, default_unit.c_str());
193 updateWidgetsFromLength(input, choice,
194 LyXLength(str), default_unit);
199 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
200 LyXLength const & len,
201 string const & default_unit)
204 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
205 choice && choice->objclass == FL_CHOICE);
208 fl_set_input(input, "");
209 fl_set_choice_text(choice, default_unit.c_str());
211 fl_set_input(input, tostr(len.value()).c_str());
213 // Set the choice to the desired unit, if present in the choice.
214 // Else set the choice to the default unit.
215 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
217 vector<string> const vec = getVector(choice);
218 vector<string>::const_iterator it =
219 std::find(vec.begin(), vec.end(), unit);
220 if (it != vec.end()) {
221 fl_set_choice_text(choice, unit.c_str());
223 fl_set_choice_text(choice, default_unit.c_str());
229 // Take a string and add breaks so that it fits into a desired label width, w
230 string formatted(string const & sin, int w, int size, int style)
236 string::size_type curpos = 0;
239 string::size_type const nxtpos1 = sin.find(' ', curpos);
240 string::size_type const nxtpos2 = sin.find('\n', curpos);
241 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
243 string const word = nxtpos == string::npos ?
244 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
246 bool const newline = (nxtpos2 != string::npos &&
249 string const line_plus_word =
250 line.empty() ? word : line + ' ' + word;
253 fl_get_string_width(style, size,
254 line_plus_word.c_str(),
255 int(line_plus_word.length()));
266 } else if (newline) {
267 sout += line_plus_word + '\n';
276 if (nxtpos == string::npos) {
289 void setCursorColor(int color)
291 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
292 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
293 fl_set_cursor_color(XC_watch, color, FL_WHITE);
294 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
300 // sorted by hand to prevent LyXLex from complaining on read().
301 keyword_item xformTags[] = {
302 { "\\gui_background", FL_COL1 },
303 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
304 { "\\gui_buttonleft", FL_LEFT_BCOL },
305 { "\\gui_buttonright", FL_RIGHT_BCOL },
306 { "\\gui_buttontop", FL_TOP_BCOL },
307 { "\\gui_inactive", FL_INACTIVE },
308 { "\\gui_pointer", FL_FREE_COL16 },
309 { "\\gui_push_button", FL_YELLOW },
310 { "\\gui_selected", FL_MCOL },
311 { "\\gui_text", FL_BLACK }
315 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
320 bool XformsColor::read(string const & filename)
322 LyXLex lexrc(xformTags, xformCount);
323 if (!lexrc.setFile(filename))
326 while (lexrc.isOK()) {
327 int const le = lexrc.lex();
330 case LyXLex::LEX_UNDEF:
331 lexrc.printError("Unknown tag `$$Token'");
333 case LyXLex::LEX_FEOF:
338 string const tag = lexrc.getString();
342 if (!lexrc.next()) break;
343 col.r = lexrc.getInteger();
345 if (!lexrc.next()) break;
346 col.g = lexrc.getInteger();
348 if (!lexrc.next()) break;
349 col.b = lexrc.getInteger();
351 fl_mapcolor(le, col.r, col.g, col.b);
353 if (tag == "\\gui_pointer") {
354 setCursorColor(FL_FREE_COL16);
362 bool XformsColor::write(string const & filename)
364 ofstream os(filename.c_str());
369 << "### file " << filename << "\n\n"
370 << "### This file is written by LyX, if you want to make your own\n"
371 << "### modifications you should do them from inside LyX and save\n"
374 for (int i = 0; i < xformCount; ++i) {
375 string const tag = xformTags[i].tag;
376 int const colorID = xformTags[i].code;
379 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
382 << color.r << ' ' << color.g << ' ' << color.b << '\n';
389 string RWInfo::error_message;
391 bool RWInfo::WriteableDir(string const & name)
393 error_message.erase();
395 if (!AbsolutePath(name)) {
396 error_message = _("The absolute path is required.");
400 FileInfo const tp(name);
401 if (!tp.isOK() || !tp.isDir()) {
402 error_message = _("Directory does not exist.");
406 if (!tp.writable()) {
407 error_message = _("Cannot write to this directory.");
415 bool RWInfo::ReadableDir(string const & name)
417 error_message.erase();
419 if (!AbsolutePath(name)) {
420 error_message = _("The absolute path is required.");
424 FileInfo const tp(name);
425 if (!tp.isOK() || !tp.isDir()) {
426 error_message = _("Directory does not exist.");
430 if (!tp.readable()) {
431 error_message = _("Cannot read this directory.");
439 bool RWInfo::WriteableFile(string const & name)
441 // A writeable file is either:
442 // * An existing file to which we have write access, or
443 // * A file that doesn't yet exist but that would exist in a writeable
446 error_message.erase();
449 error_message = _("No file input.");
453 string const dir = OnlyPath(name);
454 if (!AbsolutePath(dir)) {
455 error_message = _("The absolute path is required.");
461 if (!d.isOK() || !d.isDir()) {
465 if (!d.isOK() || !d.isDir()) {
466 error_message = _("Directory does not exist.");
471 error_message = _("Cannot write to this directory.");
476 if (dir == name || (f.isOK() && f.isDir())) {
477 error_message = _("A file is required, not a directory.");
481 if (f.isOK() && f.exist() && !f.writable()) {
482 error_message = _("Cannot write to this file.");
490 bool RWInfo::ReadableFile(string const & name)
492 error_message.erase();
495 error_message = _("No file input.");
499 string const dir = OnlyPath(name);
500 if (!AbsolutePath(dir)) {
501 error_message = _("The absolute path is required.");
507 if (!d.isOK() && !d.isDir()) {
511 if (!d.isOK() || !d.isDir()) {
512 error_message = _("Directory does not exist.");
517 error_message = _("Cannot read from this directory.");
522 if (dir == name || (f.isOK() && f.isDir())) {
523 error_message = _("A file is required, not a directory.");
528 error_message = _("File does not exist.");
533 error_message = _("Cannot read from this file.");