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"
29 using lyx::support::AbsolutePath;
30 using lyx::support::FileInfo;
31 using lyx::support::isStrDbl;
32 using lyx::support::OnlyPath;
33 using lyx::support::subst;
34 using lyx::support::trim;
42 bool isActive(FL_OBJECT * ob)
44 return ob && ob->active > 0;
48 std::pair<string, string> parse_shortcut(string const & str)
50 string::size_type i = str.find_first_of("&");
51 if (i == string::npos || i == str.length() - 1)
52 return make_pair(str, string());
56 string::value_type c = str[i + 1];
57 return make_pair(str.substr(0, i) + str.substr(i + 1),
62 // A wrapper for the xforms routine, but this one accepts uint args
63 unsigned long fl_getmcolor(int i,
64 unsigned int * r, unsigned int * g, unsigned int * b)
67 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
75 // Set an FL_OBJECT to activated or deactivated
76 void setEnabled(FL_OBJECT * ob, bool enable)
79 fl_activate_object(ob);
80 fl_set_object_lcol(ob, FL_LCOL);
82 fl_deactivate_object(ob);
83 fl_set_object_lcol(ob, FL_INACTIVE);
88 // Given an fl_choice or an fl_browser, create a vector of its entries
89 vector<string> const getVector(FL_OBJECT * ob)
93 switch (ob->objclass) {
95 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
96 string const text = fl_get_choice_item_text(ob, i+1);
97 vec.push_back(trim(text));
101 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
102 string const text = fl_get_browser_line(ob, i+1);
103 vec.push_back(trim(text));
115 string const getString(FL_OBJECT * ob, int line)
117 // Negative line value does not make sense.
118 BOOST_ASSERT(line >= 0);
120 char const * tmp = 0;
121 switch (ob->objclass) {
123 tmp = fl_get_input(ob);
128 line = fl_get_browser(ob);
130 if (line >= 1 && line <= fl_get_browser_maxline(ob))
131 tmp = fl_get_browser_line(ob, line);
136 line = fl_get_choice(ob);
138 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
139 tmp = fl_get_choice_item_text(ob, line);
143 tmp = fl_get_combox_text(ob);
150 return tmp ? trim(tmp) : string();
153 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
156 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
157 choice && choice->objclass == FL_CHOICE);
159 string const length = trim(fl_get_input(input));
163 // don't return unit-from-choice if the input(field) contains a unit
164 if (isValidGlueLength(length))
167 string unit = trim(fl_get_choice_text(choice));
168 unit = subst(unit, "%%", "%");
170 return length + unit;
174 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
176 string const & default_unit)
178 // use input field only for gluelengths
179 if (!isValidLength(str) && !isStrDbl(str)) {
180 fl_set_input(input, str.c_str());
181 // we assume that "default_unit" is in the choice as "we"
182 // have control over that!
183 // No need to check for its presence in the choice, therefore.
184 fl_set_choice_text(choice, default_unit.c_str());
186 updateWidgetsFromLength(input, choice,
187 LyXLength(str), default_unit);
192 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
193 LyXLength const & len,
194 string const & default_unit)
197 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
198 choice && choice->objclass == FL_CHOICE);
201 fl_set_input(input, "");
202 fl_set_choice_text(choice, default_unit.c_str());
204 fl_set_input(input, tostr(len.value()).c_str());
206 // Set the choice to the desired unit, if present in the choice.
207 // Else set the choice to the default unit.
208 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
210 vector<string> const vec = getVector(choice);
211 vector<string>::const_iterator it =
212 std::find(vec.begin(), vec.end(), unit);
213 if (it != vec.end()) {
214 fl_set_choice_text(choice, unit.c_str());
216 fl_set_choice_text(choice, default_unit.c_str());
222 // Take a string and add breaks so that it fits into a desired label width, w
223 string formatted(string const & sin, int w, int size, int style)
229 string::size_type curpos = 0;
232 string::size_type const nxtpos1 = sin.find(' ', curpos);
233 string::size_type const nxtpos2 = sin.find('\n', curpos);
234 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
236 string const word = nxtpos == string::npos ?
237 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
239 bool const newline = (nxtpos2 != string::npos &&
242 string const line_plus_word =
243 line.empty() ? word : line + ' ' + word;
246 fl_get_string_width(style, size,
247 line_plus_word.c_str(),
248 int(line_plus_word.length()));
259 } else if (newline) {
260 sout += line_plus_word + '\n';
269 if (nxtpos == string::npos) {
282 void setCursorColor(int color)
284 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
285 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
286 fl_set_cursor_color(XC_watch, color, FL_WHITE);
287 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
293 // sorted by hand to prevent LyXLex from complaining on read().
294 keyword_item xformTags[] = {
295 { "\\gui_background", FL_COL1 },
296 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
297 { "\\gui_buttonleft", FL_LEFT_BCOL },
298 { "\\gui_buttonright", FL_RIGHT_BCOL },
299 { "\\gui_buttontop", FL_TOP_BCOL },
300 { "\\gui_inactive", FL_INACTIVE },
301 { "\\gui_pointer", FL_FREE_COL16 },
302 { "\\gui_push_button", FL_YELLOW },
303 { "\\gui_selected", FL_MCOL },
304 { "\\gui_text", FL_BLACK }
308 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
313 bool XformsColor::read(string const & filename)
315 LyXLex lexrc(xformTags, xformCount);
316 if (!lexrc.setFile(filename))
319 while (lexrc.isOK()) {
320 int const le = lexrc.lex();
323 case LyXLex::LEX_UNDEF:
324 lexrc.printError("Unknown tag `$$Token'");
326 case LyXLex::LEX_FEOF:
331 string const tag = lexrc.getString();
335 if (!lexrc.next()) break;
336 col.r = lexrc.getInteger();
338 if (!lexrc.next()) break;
339 col.g = lexrc.getInteger();
341 if (!lexrc.next()) break;
342 col.b = lexrc.getInteger();
344 fl_mapcolor(le, col.r, col.g, col.b);
346 if (tag == "\\gui_pointer") {
347 setCursorColor(FL_FREE_COL16);
355 bool XformsColor::write(string const & filename)
357 ofstream os(filename.c_str());
362 << "### file " << filename << "\n\n"
363 << "### This file is written by LyX, if you want to make your own\n"
364 << "### modifications you should do them from inside LyX and save\n"
367 for (int i = 0; i < xformCount; ++i) {
368 string const tag = xformTags[i].tag;
369 int const colorID = xformTags[i].code;
372 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
375 << color.r << ' ' << color.g << ' ' << color.b << '\n';
382 string RWInfo::error_message;
384 bool RWInfo::WriteableDir(string const & name)
386 error_message.erase();
388 if (!AbsolutePath(name)) {
389 error_message = _("The absolute path is required.");
393 FileInfo const tp(name);
394 if (!tp.isOK() || !tp.isDir()) {
395 error_message = _("Directory does not exist.");
399 if (!tp.writable()) {
400 error_message = _("Cannot write to this directory.");
408 bool RWInfo::ReadableDir(string const & name)
410 error_message.erase();
412 if (!AbsolutePath(name)) {
413 error_message = _("The absolute path is required.");
417 FileInfo const tp(name);
418 if (!tp.isOK() || !tp.isDir()) {
419 error_message = _("Directory does not exist.");
423 if (!tp.readable()) {
424 error_message = _("Cannot read this directory.");
432 bool RWInfo::WriteableFile(string const & name)
434 // A writeable file is either:
435 // * An existing file to which we have write access, or
436 // * A file that doesn't yet exist but that would exist in a writeable
439 error_message.erase();
442 error_message = _("No file input.");
446 string const dir = OnlyPath(name);
447 if (!AbsolutePath(dir)) {
448 error_message = _("The absolute path is required.");
454 if (!d.isOK() || !d.isDir()) {
458 if (!d.isOK() || !d.isDir()) {
459 error_message = _("Directory does not exist.");
464 error_message = _("Cannot write to this directory.");
469 if (dir == name || (f.isOK() && f.isDir())) {
470 error_message = _("A file is required, not a directory.");
474 if (f.isOK() && f.exist() && !f.writable()) {
475 error_message = _("Cannot write to this file.");
483 bool RWInfo::ReadableFile(string const & name)
485 error_message.erase();
488 error_message = _("No file input.");
492 string const dir = OnlyPath(name);
493 if (!AbsolutePath(dir)) {
494 error_message = _("The absolute path is required.");
500 if (!d.isOK() && !d.isDir()) {
504 if (!d.isOK() || !d.isDir()) {
505 error_message = _("Directory does not exist.");
510 error_message = _("Cannot read from this directory.");
515 if (dir == name || (f.isOK() && f.isDir())) {
516 error_message = _("A file is required, not a directory.");
521 error_message = _("File does not exist.");
526 error_message = _("Cannot read from this file.");