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>
41 using support::AbsolutePath;
42 using support::FileInfo;
43 using support::isStrDbl;
44 using support::OnlyPath;
50 std::string const buildChoiceLengthString()
53 for (int i = 0; i != num_units; ++i) {
56 data += subst(unit_name_gui[i], "%", "%%");
62 bool isActive(FL_OBJECT * ob)
64 return ob && ob->active > 0;
68 std::pair<string, string> parse_shortcut(string const & str)
70 string::size_type i = str.find_first_of("&");
71 if (i == string::npos || i == str.length() - 1)
72 return make_pair(str, string());
76 string::value_type c = str[i + 1];
77 return make_pair(str.substr(0, i) + str.substr(i + 1),
82 // A wrapper for the xforms routine, but this one accepts uint args
83 unsigned long fl_getmcolor(int i,
84 unsigned int * r, unsigned int * g, unsigned int * b)
87 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
95 // Set an FL_OBJECT to activated or deactivated
96 void setEnabled(FL_OBJECT * ob, bool enable)
99 fl_activate_object(ob);
100 fl_set_object_lcol(ob, FL_LCOL);
102 fl_deactivate_object(ob);
103 fl_set_object_lcol(ob, FL_INACTIVE);
108 // Given an fl_choice or an fl_browser, create a vector of its entries
109 vector<string> const getVector(FL_OBJECT * ob)
113 switch (ob->objclass) {
115 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
116 string const text = fl_get_choice_item_text(ob, i+1);
117 vec.push_back(trim(text));
121 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
122 string const text = fl_get_browser_line(ob, i+1);
123 vec.push_back(trim(text));
135 string const getString(FL_OBJECT * ob, int line)
137 // Negative line value does not make sense.
138 BOOST_ASSERT(line >= 0);
140 char const * tmp = 0;
141 switch (ob->objclass) {
143 tmp = fl_get_input(ob);
148 line = fl_get_browser(ob);
150 if (line >= 1 && line <= fl_get_browser_maxline(ob))
151 tmp = fl_get_browser_line(ob, line);
156 line = fl_get_choice(ob);
158 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
159 tmp = fl_get_choice_item_text(ob, line);
163 tmp = fl_get_combox_text(ob);
170 return tmp ? trim(tmp) : string();
173 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
176 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
177 choice && choice->objclass == FL_CHOICE);
179 string const length = trim(fl_get_input(input));
183 // don't return unit-from-choice if the input(field) contains a unit
184 if (isValidGlueLength(length))
187 string unit = trim(fl_get_choice_text(choice));
188 unit = subst(unit, "%%", "%");
190 return length + unit;
194 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
196 string const & default_unit)
199 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
200 choice && choice->objclass == FL_CHOICE);
202 // use input field only for gluelengths
203 if (!isValidLength(str) && !isStrDbl(str)) {
204 fl_set_input(input, str.c_str());
205 // we assume that "default_unit" is in the choice as "we"
206 // have control over that!
207 // No need to check for its presence in the choice, therefore.
208 fl_set_choice_text(choice, default_unit.c_str());
210 updateWidgetsFromLength(input, choice,
211 LyXLength(str), default_unit);
216 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
217 LyXLength const & len,
218 string const & default_unit)
221 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
222 choice && choice->objclass == FL_CHOICE);
225 fl_set_input(input, "");
226 fl_set_choice_text(choice, default_unit.c_str());
228 fl_set_input(input, tostr(len.value()).c_str());
230 // Set the choice to the desired unit, if present in the choice.
231 // Else set the choice to the default unit.
232 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
234 vector<string> const vec = getVector(choice);
235 vector<string>::const_iterator it =
236 std::find(vec.begin(), vec.end(), unit);
237 if (it != vec.end()) {
238 fl_set_choice_text(choice, unit.c_str());
240 fl_set_choice_text(choice, default_unit.c_str());
246 // Take a string and add breaks so that it fits into a desired label width, w
247 string formatted(string const & sin, int w, int size, int style)
253 string::size_type curpos = 0;
256 string::size_type const nxtpos1 = sin.find(' ', curpos);
257 string::size_type const nxtpos2 = sin.find('\n', curpos);
258 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
260 string const word = nxtpos == string::npos ?
261 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
263 bool const newline = (nxtpos2 != string::npos &&
266 string const line_plus_word =
267 line.empty() ? word : line + ' ' + word;
270 fl_get_string_width(style, size,
271 line_plus_word.c_str(),
272 int(line_plus_word.length()));
283 } else if (newline) {
284 sout += line_plus_word + '\n';
293 if (nxtpos == string::npos) {
306 void setCursorColor(int color)
308 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
309 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
310 fl_set_cursor_color(XC_watch, color, FL_WHITE);
311 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
317 // sorted by hand to prevent LyXLex from complaining on read().
318 keyword_item xformTags[] = {
319 { "\\gui_background", FL_COL1 },
320 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
321 { "\\gui_buttonleft", FL_LEFT_BCOL },
322 { "\\gui_buttonright", FL_RIGHT_BCOL },
323 { "\\gui_buttontop", FL_TOP_BCOL },
324 { "\\gui_inactive", FL_INACTIVE },
325 { "\\gui_pointer", FL_FREE_COL16 },
326 { "\\gui_push_button", FL_YELLOW },
327 { "\\gui_selected", FL_MCOL },
328 { "\\gui_text", FL_BLACK }
332 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
337 bool XformsColor::read(string const & filename)
339 FileInfo const f(filename);
340 LyXLex lexrc(xformTags, xformCount);
341 if (f.readable() && !lexrc.setFile(filename)) {
342 lyxerr << "XformsColor::read(" << filename << ")\n"
343 << _("Failed to open file.") << std::endl;
347 while (lexrc.isOK()) {
348 int const le = lexrc.lex();
351 case LyXLex::LEX_UNDEF:
352 lexrc.printError("Unknown tag `$$Token'");
354 case LyXLex::LEX_FEOF:
359 string const tag = lexrc.getString();
363 if (!lexrc.next()) break;
364 col.r = lexrc.getInteger();
366 if (!lexrc.next()) break;
367 col.g = lexrc.getInteger();
369 if (!lexrc.next()) break;
370 col.b = lexrc.getInteger();
372 fl_mapcolor(le, col.r, col.g, col.b);
374 if (tag == "\\gui_pointer") {
375 setCursorColor(FL_FREE_COL16);
383 bool XformsColor::write(string const & filename)
385 ofstream os(filename.c_str());
387 lyxerr << "XformsColor::write(" << filename << ")\n"
388 << _("Failed to open file.") << std::endl;
393 << "### file " << filename << "\n\n"
394 << "### This file is written by LyX, if you want to make your own\n"
395 << "### modifications you should do them from inside LyX and save\n"
398 for (int i = 0; i < xformCount; ++i) {
399 string const tag = xformTags[i].tag;
400 int const colorID = xformTags[i].code;
403 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
406 << color.r << ' ' << color.g << ' ' << color.b << '\n';
413 string RWInfo::error_message;
415 bool RWInfo::WriteableDir(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.writable()) {
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 FileInfo const tp(name);
449 if (!tp.isOK() || !tp.isDir()) {
450 error_message = _("Directory does not exist.");
454 if (!tp.readable()) {
455 error_message = _("Cannot read this directory.");
463 bool RWInfo::WriteableFile(string const & name)
465 // A writeable file is either:
466 // * An existing file to which we have write access, or
467 // * A file that doesn't yet exist but that would exist in a writeable
470 error_message.erase();
473 error_message = _("No file input.");
477 string const dir = OnlyPath(name);
478 if (!AbsolutePath(dir)) {
479 error_message = _("The absolute path is required.");
485 if (!d.isOK() || !d.isDir()) {
489 if (!d.isOK() || !d.isDir()) {
490 error_message = _("Directory does not exist.");
495 error_message = _("Cannot write to this directory.");
500 if (dir == name || (f.isOK() && f.isDir())) {
501 error_message = _("A file is required, not a directory.");
505 if (f.isOK() && f.exist() && !f.writable()) {
506 error_message = _("Cannot write to this file.");
514 bool RWInfo::ReadableFile(string const & name)
516 error_message.erase();
519 error_message = _("No file input.");
523 string const dir = OnlyPath(name);
524 if (!AbsolutePath(dir)) {
525 error_message = _("The absolute path is required.");
531 if (!d.isOK() && !d.isDir()) {
535 if (!d.isOK() || !d.isDir()) {
536 error_message = _("Directory does not exist.");
541 error_message = _("Cannot read from this directory.");
546 if (dir == name || (f.isOK() && f.isDir())) {
547 error_message = _("A file is required, not a directory.");
552 error_message = _("File does not exist.");
557 error_message = _("Cannot read from this file.");
564 } // namespace frontend