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 "lyxgluelength.h"
20 #include "support/FileInfo.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h" // frontStrip, strip
23 #include "support/tostr.h"
25 #include "lyx_forms.h"
28 #include <boost/assert.hpp>
32 using lyx::support::AbsolutePath;
33 using lyx::support::FileInfo;
34 using lyx::support::isStrDbl;
35 using lyx::support::OnlyPath;
36 using lyx::support::subst;
37 using lyx::support::trim;
46 bool isActive(FL_OBJECT * ob)
48 return ob && ob->active > 0;
52 std::pair<string, string> parse_shortcut(string const & str)
54 string::size_type i = str.find_first_of("&");
55 if (i == string::npos || i == str.length() - 1)
56 return make_pair(str, string());
60 string::value_type c = str[i + 1];
61 return make_pair(str.substr(0, i) + str.substr(i + 1),
66 // A wrapper for the xforms routine, but this one accepts uint args
67 unsigned long fl_getmcolor(int i,
68 unsigned int * r, unsigned int * g, unsigned int * b)
71 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
79 // Set an FL_OBJECT to activated or deactivated
80 void setEnabled(FL_OBJECT * ob, bool enable)
83 fl_activate_object(ob);
84 fl_set_object_lcol(ob, FL_LCOL);
86 fl_deactivate_object(ob);
87 fl_set_object_lcol(ob, FL_INACTIVE);
92 // Given an fl_choice or an fl_browser, create a vector of its entries
93 vector<string> const getVector(FL_OBJECT * ob)
97 switch (ob->objclass) {
99 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
100 string const text = fl_get_choice_item_text(ob, i+1);
101 vec.push_back(trim(text));
105 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
106 string const text = fl_get_browser_line(ob, i+1);
107 vec.push_back(trim(text));
119 string const getString(FL_OBJECT * ob, int line)
121 // Negative line value does not make sense.
122 BOOST_ASSERT(line >= 0);
124 char const * tmp = 0;
125 switch (ob->objclass) {
127 tmp = fl_get_input(ob);
132 line = fl_get_browser(ob);
134 if (line >= 1 && line <= fl_get_browser_maxline(ob))
135 tmp = fl_get_browser_line(ob, line);
140 line = fl_get_choice(ob);
142 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
143 tmp = fl_get_choice_item_text(ob, line);
147 tmp = fl_get_combox_text(ob);
154 return tmp ? trim(tmp) : string();
157 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
160 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
161 choice && choice->objclass == FL_CHOICE);
163 string const length = trim(fl_get_input(input));
167 // don't return unit-from-choice if the input(field) contains a unit
168 if (isValidGlueLength(length))
171 string unit = trim(fl_get_choice_text(choice));
172 unit = subst(unit, "%%", "%");
174 return length + unit;
178 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
180 string const & default_unit)
183 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
184 choice && choice->objclass == FL_CHOICE);
186 // use input field only for gluelengths
187 if (!isValidLength(str) && !isStrDbl(str)) {
188 fl_set_input(input, str.c_str());
189 // we assume that "default_unit" is in the choice as "we"
190 // have control over that!
191 // No need to check for its presence in the choice, therefore.
192 fl_set_choice_text(choice, default_unit.c_str());
194 updateWidgetsFromLength(input, choice,
195 LyXLength(str), default_unit);
200 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
201 LyXLength const & len,
202 string const & default_unit)
205 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
206 choice && choice->objclass == FL_CHOICE);
209 fl_set_input(input, "");
210 fl_set_choice_text(choice, default_unit.c_str());
212 fl_set_input(input, tostr(len.value()).c_str());
214 // Set the choice to the desired unit, if present in the choice.
215 // Else set the choice to the default unit.
216 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
218 vector<string> const vec = getVector(choice);
219 vector<string>::const_iterator it =
220 std::find(vec.begin(), vec.end(), unit);
221 if (it != vec.end()) {
222 fl_set_choice_text(choice, unit.c_str());
224 fl_set_choice_text(choice, default_unit.c_str());
230 // Take a string and add breaks so that it fits into a desired label width, w
231 string formatted(string const & sin, int w, int size, int style)
237 string::size_type curpos = 0;
240 string::size_type const nxtpos1 = sin.find(' ', curpos);
241 string::size_type const nxtpos2 = sin.find('\n', curpos);
242 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
244 string const word = nxtpos == string::npos ?
245 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
247 bool const newline = (nxtpos2 != string::npos &&
250 string const line_plus_word =
251 line.empty() ? word : line + ' ' + word;
254 fl_get_string_width(style, size,
255 line_plus_word.c_str(),
256 int(line_plus_word.length()));
267 } else if (newline) {
268 sout += line_plus_word + '\n';
277 if (nxtpos == string::npos) {
290 void setCursorColor(int color)
292 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
293 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
294 fl_set_cursor_color(XC_watch, color, FL_WHITE);
295 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
301 // sorted by hand to prevent LyXLex from complaining on read().
302 keyword_item xformTags[] = {
303 { "\\gui_background", FL_COL1 },
304 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
305 { "\\gui_buttonleft", FL_LEFT_BCOL },
306 { "\\gui_buttonright", FL_RIGHT_BCOL },
307 { "\\gui_buttontop", FL_TOP_BCOL },
308 { "\\gui_inactive", FL_INACTIVE },
309 { "\\gui_pointer", FL_FREE_COL16 },
310 { "\\gui_push_button", FL_YELLOW },
311 { "\\gui_selected", FL_MCOL },
312 { "\\gui_text", FL_BLACK }
316 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
321 bool XformsColor::read(string const & filename)
323 FileInfo const f(filename);
324 LyXLex lexrc(xformTags, xformCount);
325 if (f.readable() && !lexrc.setFile(filename)) {
326 lyxerr << "XformsColor::read(" << filename << ")\n"
327 << _("Failed to open file.") << std::endl;
331 while (lexrc.isOK()) {
332 int const le = lexrc.lex();
335 case LyXLex::LEX_UNDEF:
336 lexrc.printError("Unknown tag `$$Token'");
338 case LyXLex::LEX_FEOF:
343 string const tag = lexrc.getString();
347 if (!lexrc.next()) break;
348 col.r = lexrc.getInteger();
350 if (!lexrc.next()) break;
351 col.g = lexrc.getInteger();
353 if (!lexrc.next()) break;
354 col.b = lexrc.getInteger();
356 fl_mapcolor(le, col.r, col.g, col.b);
358 if (tag == "\\gui_pointer") {
359 setCursorColor(FL_FREE_COL16);
367 bool XformsColor::write(string const & filename)
369 ofstream os(filename.c_str());
371 lyxerr << "XformsColor::write(" << filename << ")\n"
372 << _("Failed to open file.") << std::endl;
377 << "### file " << filename << "\n\n"
378 << "### This file is written by LyX, if you want to make your own\n"
379 << "### modifications you should do them from inside LyX and save\n"
382 for (int i = 0; i < xformCount; ++i) {
383 string const tag = xformTags[i].tag;
384 int const colorID = xformTags[i].code;
387 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
390 << color.r << ' ' << color.g << ' ' << color.b << '\n';
397 string RWInfo::error_message;
399 bool RWInfo::WriteableDir(string const & name)
401 error_message.erase();
403 if (!AbsolutePath(name)) {
404 error_message = _("The absolute path is required.");
408 FileInfo const tp(name);
409 if (!tp.isOK() || !tp.isDir()) {
410 error_message = _("Directory does not exist.");
414 if (!tp.writable()) {
415 error_message = _("Cannot write to this directory.");
423 bool RWInfo::ReadableDir(string const & name)
425 error_message.erase();
427 if (!AbsolutePath(name)) {
428 error_message = _("The absolute path is required.");
432 FileInfo const tp(name);
433 if (!tp.isOK() || !tp.isDir()) {
434 error_message = _("Directory does not exist.");
438 if (!tp.readable()) {
439 error_message = _("Cannot read this directory.");
447 bool RWInfo::WriteableFile(string const & name)
449 // A writeable file is either:
450 // * An existing file to which we have write access, or
451 // * A file that doesn't yet exist but that would exist in a writeable
454 error_message.erase();
457 error_message = _("No file input.");
461 string const dir = OnlyPath(name);
462 if (!AbsolutePath(dir)) {
463 error_message = _("The absolute path is required.");
469 if (!d.isOK() || !d.isDir()) {
473 if (!d.isOK() || !d.isDir()) {
474 error_message = _("Directory does not exist.");
479 error_message = _("Cannot write to this directory.");
484 if (dir == name || (f.isOK() && f.isDir())) {
485 error_message = _("A file is required, not a directory.");
489 if (f.isOK() && f.exist() && !f.writable()) {
490 error_message = _("Cannot write to this file.");
498 bool RWInfo::ReadableFile(string const & name)
500 error_message.erase();
503 error_message = _("No file input.");
507 string const dir = OnlyPath(name);
508 if (!AbsolutePath(dir)) {
509 error_message = _("The absolute path is required.");
515 if (!d.isOK() && !d.isDir()) {
519 if (!d.isOK() || !d.isDir()) {
520 error_message = _("Directory does not exist.");
525 error_message = _("Cannot read from this directory.");
530 if (dir == name || (f.isOK() && f.isDir())) {
531 error_message = _("A file is required, not a directory.");
536 error_message = _("File does not exist.");
541 error_message = _("Cannot read from this file.");