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"
27 #include <boost/assert.hpp>
31 using lyx::support::AbsolutePath;
32 using lyx::support::FileInfo;
33 using lyx::support::isStrDbl;
34 using lyx::support::OnlyPath;
35 using lyx::support::subst;
36 using lyx::support::trim;
44 bool isActive(FL_OBJECT * ob)
46 return ob && ob->active > 0;
50 std::pair<string, string> parse_shortcut(string const & str)
52 string::size_type i = str.find_first_of("&");
53 if (i == string::npos || i == str.length() - 1)
54 return make_pair(str, string());
58 string::value_type c = str[i + 1];
59 return make_pair(str.substr(0, i) + str.substr(i + 1),
64 // A wrapper for the xforms routine, but this one accepts uint args
65 unsigned long fl_getmcolor(int i,
66 unsigned int * r, unsigned int * g, unsigned int * b)
69 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
77 // Set an FL_OBJECT to activated or deactivated
78 void setEnabled(FL_OBJECT * ob, bool enable)
81 fl_activate_object(ob);
82 fl_set_object_lcol(ob, FL_LCOL);
84 fl_deactivate_object(ob);
85 fl_set_object_lcol(ob, FL_INACTIVE);
90 // Given an fl_choice or an fl_browser, create a vector of its entries
91 vector<string> const getVector(FL_OBJECT * ob)
95 switch (ob->objclass) {
97 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
98 string const text = fl_get_choice_item_text(ob, i+1);
99 vec.push_back(trim(text));
103 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
104 string const text = fl_get_browser_line(ob, i+1);
105 vec.push_back(trim(text));
117 string const getString(FL_OBJECT * ob, int line)
119 // Negative line value does not make sense.
120 BOOST_ASSERT(line >= 0);
122 char const * tmp = 0;
123 switch (ob->objclass) {
125 tmp = fl_get_input(ob);
130 line = fl_get_browser(ob);
132 if (line >= 1 && line <= fl_get_browser_maxline(ob))
133 tmp = fl_get_browser_line(ob, line);
138 line = fl_get_choice(ob);
140 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
141 tmp = fl_get_choice_item_text(ob, line);
145 tmp = fl_get_combox_text(ob);
152 return tmp ? trim(tmp) : string();
155 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
158 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
159 choice && choice->objclass == FL_CHOICE);
161 string const length = trim(fl_get_input(input));
165 // don't return unit-from-choice if the input(field) contains a unit
166 if (isValidGlueLength(length))
169 string unit = trim(fl_get_choice_text(choice));
170 unit = subst(unit, "%%", "%");
172 return length + unit;
176 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
178 string const & default_unit)
180 // use input field only for gluelengths
181 if (!isValidLength(str) && !isStrDbl(str)) {
182 fl_set_input(input, str.c_str());
183 // we assume that "default_unit" is in the choice as "we"
184 // have control over that!
185 // No need to check for its presence in the choice, therefore.
186 fl_set_choice_text(choice, default_unit.c_str());
188 updateWidgetsFromLength(input, choice,
189 LyXLength(str), default_unit);
194 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
195 LyXLength const & len,
196 string const & default_unit)
199 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
200 choice && choice->objclass == FL_CHOICE);
203 fl_set_input(input, "");
204 fl_set_choice_text(choice, default_unit.c_str());
206 fl_set_input(input, tostr(len.value()).c_str());
208 // Set the choice to the desired unit, if present in the choice.
209 // Else set the choice to the default unit.
210 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
212 vector<string> const vec = getVector(choice);
213 vector<string>::const_iterator it =
214 std::find(vec.begin(), vec.end(), unit);
215 if (it != vec.end()) {
216 fl_set_choice_text(choice, unit.c_str());
218 fl_set_choice_text(choice, default_unit.c_str());
224 // Take a string and add breaks so that it fits into a desired label width, w
225 string formatted(string const & sin, int w, int size, int style)
231 string::size_type curpos = 0;
234 string::size_type const nxtpos1 = sin.find(' ', curpos);
235 string::size_type const nxtpos2 = sin.find('\n', curpos);
236 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
238 string const word = nxtpos == string::npos ?
239 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
241 bool const newline = (nxtpos2 != string::npos &&
244 string const line_plus_word =
245 line.empty() ? word : line + ' ' + word;
248 fl_get_string_width(style, size,
249 line_plus_word.c_str(),
250 int(line_plus_word.length()));
261 } else if (newline) {
262 sout += line_plus_word + '\n';
271 if (nxtpos == string::npos) {
284 void setCursorColor(int color)
286 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
287 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
288 fl_set_cursor_color(XC_watch, color, FL_WHITE);
289 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
295 // sorted by hand to prevent LyXLex from complaining on read().
296 keyword_item xformTags[] = {
297 { "\\gui_background", FL_COL1 },
298 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
299 { "\\gui_buttonleft", FL_LEFT_BCOL },
300 { "\\gui_buttonright", FL_RIGHT_BCOL },
301 { "\\gui_buttontop", FL_TOP_BCOL },
302 { "\\gui_inactive", FL_INACTIVE },
303 { "\\gui_pointer", FL_FREE_COL16 },
304 { "\\gui_push_button", FL_YELLOW },
305 { "\\gui_selected", FL_MCOL },
306 { "\\gui_text", FL_BLACK }
310 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
315 bool XformsColor::read(string const & filename)
317 LyXLex lexrc(xformTags, xformCount);
318 if (!lexrc.setFile(filename))
321 while (lexrc.isOK()) {
322 int const le = lexrc.lex();
325 case LyXLex::LEX_UNDEF:
326 lexrc.printError("Unknown tag `$$Token'");
328 case LyXLex::LEX_FEOF:
333 string const tag = lexrc.getString();
337 if (!lexrc.next()) break;
338 col.r = lexrc.getInteger();
340 if (!lexrc.next()) break;
341 col.g = lexrc.getInteger();
343 if (!lexrc.next()) break;
344 col.b = lexrc.getInteger();
346 fl_mapcolor(le, col.r, col.g, col.b);
348 if (tag == "\\gui_pointer") {
349 setCursorColor(FL_FREE_COL16);
357 bool XformsColor::write(string const & filename)
359 ofstream os(filename.c_str());
364 << "### file " << filename << "\n\n"
365 << "### This file is written by LyX, if you want to make your own\n"
366 << "### modifications you should do them from inside LyX and save\n"
369 for (int i = 0; i < xformCount; ++i) {
370 string const tag = xformTags[i].tag;
371 int const colorID = xformTags[i].code;
374 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
377 << color.r << ' ' << color.g << ' ' << color.b << '\n';
384 string RWInfo::error_message;
386 bool RWInfo::WriteableDir(string const & name)
388 error_message.erase();
390 if (!AbsolutePath(name)) {
391 error_message = _("The absolute path is required.");
395 FileInfo const tp(name);
396 if (!tp.isOK() || !tp.isDir()) {
397 error_message = _("Directory does not exist.");
401 if (!tp.writable()) {
402 error_message = _("Cannot write to this directory.");
410 bool RWInfo::ReadableDir(string const & name)
412 error_message.erase();
414 if (!AbsolutePath(name)) {
415 error_message = _("The absolute path is required.");
419 FileInfo const tp(name);
420 if (!tp.isOK() || !tp.isDir()) {
421 error_message = _("Directory does not exist.");
425 if (!tp.readable()) {
426 error_message = _("Cannot read this directory.");
434 bool RWInfo::WriteableFile(string const & name)
436 // A writeable file is either:
437 // * An existing file to which we have write access, or
438 // * A file that doesn't yet exist but that would exist in a writeable
441 error_message.erase();
444 error_message = _("No file input.");
448 string const dir = OnlyPath(name);
449 if (!AbsolutePath(dir)) {
450 error_message = _("The absolute path is required.");
456 if (!d.isOK() || !d.isDir()) {
460 if (!d.isOK() || !d.isDir()) {
461 error_message = _("Directory does not exist.");
466 error_message = _("Cannot write to this directory.");
471 if (dir == name || (f.isOK() && f.isDir())) {
472 error_message = _("A file is required, not a directory.");
476 if (f.isOK() && f.exist() && !f.writable()) {
477 error_message = _("Cannot write to this file.");
485 bool RWInfo::ReadableFile(string const & name)
487 error_message.erase();
490 error_message = _("No file input.");
494 string const dir = OnlyPath(name);
495 if (!AbsolutePath(dir)) {
496 error_message = _("The absolute path is required.");
502 if (!d.isOK() && !d.isDir()) {
506 if (!d.isOK() || !d.isDir()) {
507 error_message = _("Directory does not exist.");
512 error_message = _("Cannot read from this directory.");
517 if (dir == name || (f.isOK() && f.isDir())) {
518 error_message = _("A file is required, not a directory.");
523 error_message = _("File does not exist.");
528 error_message = _("Cannot read from this file.");