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 namespace lyx::support;
37 bool isActive(FL_OBJECT * ob)
39 return ob && ob->active > 0;
43 std::pair<string, string> parse_shortcut(string const & str)
45 string::size_type i = str.find_first_of("&");
46 if (i == string::npos || i == str.length() - 1)
47 return make_pair(str, string());
51 string::value_type c = str[i + 1];
52 return make_pair(str.substr(0, i) + str.substr(i + 1),
57 // A wrapper for the xforms routine, but this one accepts uint args
58 unsigned long fl_getmcolor(int i,
59 unsigned int * r, unsigned int * g, unsigned int * b)
62 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
70 // Set an FL_OBJECT to activated or deactivated
71 void setEnabled(FL_OBJECT * ob, bool enable)
74 fl_activate_object(ob);
75 fl_set_object_lcol(ob, FL_LCOL);
77 fl_deactivate_object(ob);
78 fl_set_object_lcol(ob, FL_INACTIVE);
83 // Given an fl_choice or an fl_browser, create a vector of its entries
84 vector<string> const getVector(FL_OBJECT * ob)
88 switch (ob->objclass) {
90 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
91 string const text = fl_get_choice_item_text(ob, i+1);
92 vec.push_back(trim(text));
96 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
97 string const text = fl_get_browser_line(ob, i+1);
98 vec.push_back(trim(text));
110 string const getString(FL_OBJECT * ob, int line)
112 // Negative line value does not make sense.
113 BOOST_ASSERT(line >= 0);
115 char const * tmp = 0;
116 switch (ob->objclass) {
118 tmp = fl_get_input(ob);
123 line = fl_get_browser(ob);
125 if (line >= 1 && line <= fl_get_browser_maxline(ob))
126 tmp = fl_get_browser_line(ob, line);
131 line = fl_get_choice(ob);
133 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
134 tmp = fl_get_choice_item_text(ob, line);
138 tmp = fl_get_combox_text(ob);
145 return tmp ? trim(tmp) : string();
148 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
151 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
152 choice && choice->objclass == FL_CHOICE);
154 string const length = trim(fl_get_input(input));
158 // don't return unit-from-choice if the input(field) contains a unit
159 if (isValidGlueLength(length))
162 string unit = trim(fl_get_choice_text(choice));
163 unit = subst(unit, "%%", "%");
165 return length + unit;
169 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
171 string const & default_unit)
173 // use input field only for gluelengths
174 if (!isValidLength(str) && !isStrDbl(str)) {
175 fl_set_input(input, str.c_str());
176 // we assume that "default_unit" is in the choice as "we"
177 // have control over that!
178 // No need to check for its presence in the choice, therefore.
179 fl_set_choice_text(choice, default_unit.c_str());
181 updateWidgetsFromLength(input, choice,
182 LyXLength(str), default_unit);
187 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
188 LyXLength const & len,
189 string const & default_unit)
192 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
193 choice && choice->objclass == FL_CHOICE);
196 fl_set_input(input, "");
197 fl_set_choice_text(choice, default_unit.c_str());
199 fl_set_input(input, tostr(len.value()).c_str());
201 // Set the choice to the desired unit, if present in the choice.
202 // Else set the choice to the default unit.
203 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
205 vector<string> const vec = getVector(choice);
206 vector<string>::const_iterator it =
207 std::find(vec.begin(), vec.end(), unit);
208 if (it != vec.end()) {
209 fl_set_choice_text(choice, unit.c_str());
211 fl_set_choice_text(choice, default_unit.c_str());
217 // Take a string and add breaks so that it fits into a desired label width, w
218 string formatted(string const & sin, int w, int size, int style)
224 string::size_type curpos = 0;
227 string::size_type const nxtpos1 = sin.find(' ', curpos);
228 string::size_type const nxtpos2 = sin.find('\n', curpos);
229 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
231 string const word = nxtpos == string::npos ?
232 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
234 bool const newline = (nxtpos2 != string::npos &&
237 string const line_plus_word =
238 line.empty() ? word : line + ' ' + word;
241 fl_get_string_width(style, size,
242 line_plus_word.c_str(),
243 int(line_plus_word.length()));
254 } else if (newline) {
255 sout += line_plus_word + '\n';
264 if (nxtpos == string::npos) {
277 void setCursorColor(int color)
279 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
280 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
281 fl_set_cursor_color(XC_watch, color, FL_WHITE);
282 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
288 // sorted by hand to prevent LyXLex from complaining on read().
289 keyword_item xformTags[] = {
290 { "\\gui_background", FL_COL1 },
291 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
292 { "\\gui_buttonleft", FL_LEFT_BCOL },
293 { "\\gui_buttonright", FL_RIGHT_BCOL },
294 { "\\gui_buttontop", FL_TOP_BCOL },
295 { "\\gui_inactive", FL_INACTIVE },
296 { "\\gui_pointer", FL_FREE_COL16 },
297 { "\\gui_push_button", FL_YELLOW },
298 { "\\gui_selected", FL_MCOL },
299 { "\\gui_text", FL_BLACK }
303 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
308 bool XformsColor::read(string const & filename)
310 LyXLex lexrc(xformTags, xformCount);
311 if (!lexrc.setFile(filename))
314 while (lexrc.isOK()) {
315 int const le = lexrc.lex();
318 case LyXLex::LEX_UNDEF:
319 lexrc.printError("Unknown tag `$$Token'");
321 case LyXLex::LEX_FEOF:
326 string const tag = lexrc.getString();
330 if (!lexrc.next()) break;
331 col.r = lexrc.getInteger();
333 if (!lexrc.next()) break;
334 col.g = lexrc.getInteger();
336 if (!lexrc.next()) break;
337 col.b = lexrc.getInteger();
339 fl_mapcolor(le, col.r, col.g, col.b);
341 if (tag == "\\gui_pointer") {
342 setCursorColor(FL_FREE_COL16);
350 bool XformsColor::write(string const & filename)
352 ofstream os(filename.c_str());
357 << "### file " << filename << "\n\n"
358 << "### This file is written by LyX, if you want to make your own\n"
359 << "### modifications you should do them from inside LyX and save\n"
362 for (int i = 0; i < xformCount; ++i) {
363 string const tag = xformTags[i].tag;
364 int const colorID = xformTags[i].code;
367 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
370 << color.r << ' ' << color.g << ' ' << color.b << '\n';
377 string RWInfo::error_message;
379 bool RWInfo::WriteableDir(string const & name)
381 error_message.erase();
383 if (!AbsolutePath(name)) {
384 error_message = _("The absolute path is required.");
388 FileInfo const tp(name);
389 if (!tp.isOK() || !tp.isDir()) {
390 error_message = _("Directory does not exist.");
394 if (!tp.writable()) {
395 error_message = _("Cannot write to this directory.");
403 bool RWInfo::ReadableDir(string const & name)
405 error_message.erase();
407 if (!AbsolutePath(name)) {
408 error_message = _("The absolute path is required.");
412 FileInfo const tp(name);
413 if (!tp.isOK() || !tp.isDir()) {
414 error_message = _("Directory does not exist.");
418 if (!tp.readable()) {
419 error_message = _("Cannot read this directory.");
427 bool RWInfo::WriteableFile(string const & name)
429 // A writeable file is either:
430 // * An existing file to which we have write access, or
431 // * A file that doesn't yet exist but that would exist in a writeable
434 error_message.erase();
437 error_message = _("No file input.");
441 string const dir = OnlyPath(name);
442 if (!AbsolutePath(dir)) {
443 error_message = _("The absolute path is required.");
449 if (!d.isOK() || !d.isDir()) {
453 if (!d.isOK() || !d.isDir()) {
454 error_message = _("Directory does not exist.");
459 error_message = _("Cannot write to this directory.");
464 if (dir == name || (f.isOK() && f.isDir())) {
465 error_message = _("A file is required, not a directory.");
469 if (f.isOK() && f.exist() && !f.writable()) {
470 error_message = _("Cannot write to this file.");
478 bool RWInfo::ReadableFile(string const & name)
480 error_message.erase();
483 error_message = _("No file input.");
487 string const dir = OnlyPath(name);
488 if (!AbsolutePath(dir)) {
489 error_message = _("The absolute path is required.");
495 if (!d.isOK() && !d.isDir()) {
499 if (!d.isOK() || !d.isDir()) {
500 error_message = _("Directory does not exist.");
505 error_message = _("Cannot read from this directory.");
510 if (dir == name || (f.isOK() && f.isDir())) {
511 error_message = _("A file is required, not a directory.");
516 error_message = _("File does not exist.");
521 error_message = _("Cannot read from this file.");