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/filetools.h"
22 #include "support/lstrings.h" // frontStrip, strip
23 #include "support/convert.h"
24 #include "support/fs_extras.h"
26 #include "lyx_forms.h"
29 #include <boost/assert.hpp>
30 #include <boost/filesystem/operations.hpp>
40 namespace fs = boost::filesystem;
44 using support::AbsolutePath;
45 using support::isStrDbl;
46 using support::OnlyPath;
52 std::string const buildChoiceLengthString()
55 for (int i = 0; i != num_units; ++i) {
58 data += subst(unit_name_gui[i], "%", "%%");
64 bool isActive(FL_OBJECT * ob)
66 return ob && ob->active > 0;
70 std::pair<string, string> parse_shortcut(string const & str)
72 string::size_type i = str.find_first_of("&");
73 if (i == string::npos || i == str.length() - 1)
74 return make_pair(str, string());
78 string::value_type c = str[i + 1];
79 return make_pair(str.substr(0, i) + str.substr(i + 1),
84 // A wrapper for the xforms routine, but this one accepts uint args
85 unsigned long fl_getmcolor(int i,
86 unsigned int * r, unsigned int * g, unsigned int * b)
89 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
97 // Set an FL_OBJECT to activated or deactivated
98 void setEnabled(FL_OBJECT * ob, bool enable)
101 fl_activate_object(ob);
102 fl_set_object_lcol(ob, FL_LCOL);
104 fl_deactivate_object(ob);
105 fl_set_object_lcol(ob, FL_INACTIVE);
110 // Given an fl_choice or an fl_browser, create a vector of its entries
111 vector<string> const getVector(FL_OBJECT * ob)
115 switch (ob->objclass) {
117 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
118 string const text = fl_get_choice_item_text(ob, i+1);
119 vec.push_back(trim(text));
123 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
124 string const text = fl_get_browser_line(ob, i+1);
125 vec.push_back(trim(text));
137 string const getString(FL_OBJECT * ob, int line)
139 // Negative line value does not make sense.
140 BOOST_ASSERT(line >= 0);
142 char const * tmp = 0;
143 switch (ob->objclass) {
145 tmp = fl_get_input(ob);
150 line = fl_get_browser(ob);
152 if (line >= 1 && line <= fl_get_browser_maxline(ob))
153 tmp = fl_get_browser_line(ob, line);
158 line = fl_get_choice(ob);
160 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
161 tmp = fl_get_choice_item_text(ob, line);
165 tmp = fl_get_combox_text(ob);
172 return tmp ? trim(tmp) : string();
175 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
178 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
179 choice && choice->objclass == FL_CHOICE);
181 string const length = trim(fl_get_input(input));
185 // don't return unit-from-choice if the input(field) contains a unit
186 if (isValidGlueLength(length))
189 string unit = trim(fl_get_choice_text(choice));
190 unit = subst(unit, "%%", "%");
192 return length + unit;
196 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
198 string const & default_unit)
201 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
202 choice && choice->objclass == FL_CHOICE);
204 // use input field only for gluelengths
205 if (!isValidLength(str) && !isStrDbl(str)) {
206 fl_set_input(input, str.c_str());
207 // we assume that "default_unit" is in the choice as "we"
208 // have control over that!
209 // No need to check for its presence in the choice, therefore.
210 fl_set_choice_text(choice, default_unit.c_str());
212 updateWidgetsFromLength(input, choice,
213 LyXLength(str), default_unit);
218 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
219 LyXLength const & len,
220 string const & default_unit)
223 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
224 choice && choice->objclass == FL_CHOICE);
227 fl_set_input(input, "");
228 fl_set_choice_text(choice, default_unit.c_str());
230 fl_set_input(input, convert<string>(len.value()).c_str());
232 // Set the choice to the desired unit, if present in the choice.
233 // Else set the choice to the default unit.
234 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
236 vector<string> const vec = getVector(choice);
237 vector<string>::const_iterator it =
238 std::find(vec.begin(), vec.end(), unit);
239 if (it != vec.end()) {
240 fl_set_choice_text(choice, unit.c_str());
242 fl_set_choice_text(choice, default_unit.c_str());
248 // Take a string and add breaks so that it fits into a desired label width, w
249 string formatted(string const & sin, int w, int size, int style)
255 string::size_type curpos = 0;
258 string::size_type const nxtpos1 = sin.find(' ', curpos);
259 string::size_type const nxtpos2 = sin.find('\n', curpos);
260 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
262 string const word = nxtpos == string::npos ?
263 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
265 bool const newline = (nxtpos2 != string::npos &&
268 string const line_plus_word =
269 line.empty() ? word : line + ' ' + word;
272 fl_get_string_width(style, size,
273 line_plus_word.c_str(),
274 int(line_plus_word.length()));
285 } else if (newline) {
286 sout += line_plus_word + '\n';
295 if (nxtpos == string::npos) {
308 void setCursorColor(int color)
310 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
311 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
312 fl_set_cursor_color(XC_watch, color, FL_WHITE);
313 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
319 // sorted by hand to prevent LyXLex from complaining on read().
320 keyword_item xformTags[] = {
321 { "\\gui_background", FL_COL1 },
322 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
323 { "\\gui_buttonleft", FL_LEFT_BCOL },
324 { "\\gui_buttonright", FL_RIGHT_BCOL },
325 { "\\gui_buttontop", FL_TOP_BCOL },
326 { "\\gui_inactive", FL_INACTIVE },
327 { "\\gui_pointer", FL_FREE_COL16 },
328 { "\\gui_push_button", FL_YELLOW },
329 { "\\gui_selected", FL_MCOL },
330 { "\\gui_text", FL_BLACK }
334 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
339 bool XformsColor::read(string const & filename)
341 LyXLex lexrc(xformTags, xformCount);
342 if (fs::is_readable(filename) && !lexrc.setFile(filename)) {
343 lyxerr << "XformsColor::read(" << filename << ")\n"
344 << _("Failed to open file.") << std::endl;
348 while (lexrc.isOK()) {
349 int const le = lexrc.lex();
352 case LyXLex::LEX_UNDEF:
353 lexrc.printError("Unknown tag `$$Token'");
355 case LyXLex::LEX_FEOF:
360 string const tag = lexrc.getString();
364 if (!lexrc.next()) break;
365 col.r = lexrc.getInteger();
367 if (!lexrc.next()) break;
368 col.g = lexrc.getInteger();
370 if (!lexrc.next()) break;
371 col.b = lexrc.getInteger();
373 fl_mapcolor(le, col.r, col.g, col.b);
375 if (tag == "\\gui_pointer") {
376 setCursorColor(FL_FREE_COL16);
384 bool XformsColor::write(string const & filename)
386 ofstream os(filename.c_str());
388 lyxerr << "XformsColor::write(" << filename << ")\n"
389 << _("Failed to open file.") << std::endl;
394 << "### file " << filename << "\n\n"
395 << "### This file is written by LyX, if you want to make your own\n"
396 << "### modifications you should do them from inside LyX and save\n"
399 for (int i = 0; i < xformCount; ++i) {
400 string const tag = xformTags[i].tag;
401 int const colorID = xformTags[i].code;
404 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
407 << color.r << ' ' << color.g << ' ' << color.b << '\n';
414 string RWInfo::error_message;
416 bool RWInfo::WriteableDir(string const & name)
418 error_message.erase();
420 if (!AbsolutePath(name)) {
421 error_message = _("The absolute path is required.");
425 if (!fs::exists(name) || !fs::is_directory(name)) {
426 error_message = _("Directory does not exist.");
430 if (!fs::is_writable(name)) {
431 error_message = _("Cannot write to this directory.");
439 bool RWInfo::ReadableDir(string const & name)
441 error_message.erase();
443 if (!AbsolutePath(name)) {
444 error_message = _("The absolute path is required.");
448 if (!fs::exists(name) || !fs::is_directory(name)) {
449 error_message = _("Directory does not exist.");
453 if (!fs::is_readable(name)) {
454 error_message = _("Cannot read this directory.");
462 bool RWInfo::WriteableFile(string const & name)
464 // A writeable file is either:
465 // * An existing file to which we have write access, or
466 // * A file that doesn't yet exist but that would exist in a writeable
469 error_message.erase();
472 error_message = _("No file input.");
476 string const dir = OnlyPath(name);
477 if (!AbsolutePath(dir)) {
478 error_message = _("The absolute path is required.");
482 string checkFile = name;
484 if (!fs::exists(checkFile) || !fs::is_directory(checkFile)) {
488 if (!fs::exists(checkFile) || !fs::is_directory(checkFile)) {
489 error_message = _("Directory does not exists.");
493 if (!fs::is_writable(checkFile)) {
494 error_message = _("Cannot write to this directory.");
498 if (dir == name || (fs::exists(name) && fs::is_directory(name))) {
499 error_message = _("A file is required, not a directory.");
503 if (fs::exists(name) && !fs::is_writable(name)) {
504 error_message = _("Cannot write to this file.");
512 bool RWInfo::ReadableFile(string const & name)
514 error_message.erase();
517 error_message = _("No file input.");
521 string const dir = OnlyPath(name);
522 if (!AbsolutePath(dir)) {
523 error_message = _("The absolute path is required.");
527 string checkFile = name;
529 if (!fs::exists(checkFile) && !fs::is_directory(checkFile)) {
533 if (!fs::exists(checkFile) || !fs::is_directory(checkFile)) {
534 error_message = _("Directory does not exist.");
538 if (!fs::is_readable(checkFile)) {
539 error_message = _("Cannot read from this directory.");
543 if (dir == name || (fs::exists(name) && fs::is_directory(name))) {
544 error_message = _("A file is required, not a directory.");
548 if (!fs::exists(name)) {
549 error_message = _("File does not exist.");
553 if (!fs::is_readable(name)) {
554 error_message = _("Cannot read from this file.");
561 } // namespace frontend