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;
45 bool isActive(FL_OBJECT * ob)
47 return ob && ob->active > 0;
51 std::pair<string, string> parse_shortcut(string const & str)
53 string::size_type i = str.find_first_of("&");
54 if (i == string::npos || i == str.length() - 1)
55 return make_pair(str, string());
59 string::value_type c = str[i + 1];
60 return make_pair(str.substr(0, i) + str.substr(i + 1),
65 // A wrapper for the xforms routine, but this one accepts uint args
66 unsigned long fl_getmcolor(int i,
67 unsigned int * r, unsigned int * g, unsigned int * b)
70 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
78 // Set an FL_OBJECT to activated or deactivated
79 void setEnabled(FL_OBJECT * ob, bool enable)
82 fl_activate_object(ob);
83 fl_set_object_lcol(ob, FL_LCOL);
85 fl_deactivate_object(ob);
86 fl_set_object_lcol(ob, FL_INACTIVE);
91 // Given an fl_choice or an fl_browser, create a vector of its entries
92 vector<string> const getVector(FL_OBJECT * ob)
96 switch (ob->objclass) {
98 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
99 string const text = fl_get_choice_item_text(ob, i+1);
100 vec.push_back(trim(text));
104 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
105 string const text = fl_get_browser_line(ob, i+1);
106 vec.push_back(trim(text));
118 string const getString(FL_OBJECT * ob, int line)
120 // Negative line value does not make sense.
121 BOOST_ASSERT(line >= 0);
123 char const * tmp = 0;
124 switch (ob->objclass) {
126 tmp = fl_get_input(ob);
131 line = fl_get_browser(ob);
133 if (line >= 1 && line <= fl_get_browser_maxline(ob))
134 tmp = fl_get_browser_line(ob, line);
139 line = fl_get_choice(ob);
141 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
142 tmp = fl_get_choice_item_text(ob, line);
146 tmp = fl_get_combox_text(ob);
153 return tmp ? trim(tmp) : string();
156 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
159 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
160 choice && choice->objclass == FL_CHOICE);
162 string const length = trim(fl_get_input(input));
166 // don't return unit-from-choice if the input(field) contains a unit
167 if (isValidGlueLength(length))
170 string unit = trim(fl_get_choice_text(choice));
171 unit = subst(unit, "%%", "%");
173 return length + unit;
177 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
179 string const & default_unit)
181 // use input field only for gluelengths
182 if (!isValidLength(str) && !isStrDbl(str)) {
183 fl_set_input(input, str.c_str());
184 // we assume that "default_unit" is in the choice as "we"
185 // have control over that!
186 // No need to check for its presence in the choice, therefore.
187 fl_set_choice_text(choice, default_unit.c_str());
189 updateWidgetsFromLength(input, choice,
190 LyXLength(str), default_unit);
195 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
196 LyXLength const & len,
197 string const & default_unit)
200 BOOST_ASSERT(input && input->objclass == FL_INPUT &&
201 choice && choice->objclass == FL_CHOICE);
204 fl_set_input(input, "");
205 fl_set_choice_text(choice, default_unit.c_str());
207 fl_set_input(input, tostr(len.value()).c_str());
209 // Set the choice to the desired unit, if present in the choice.
210 // Else set the choice to the default unit.
211 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
213 vector<string> const vec = getVector(choice);
214 vector<string>::const_iterator it =
215 std::find(vec.begin(), vec.end(), unit);
216 if (it != vec.end()) {
217 fl_set_choice_text(choice, unit.c_str());
219 fl_set_choice_text(choice, default_unit.c_str());
225 // Take a string and add breaks so that it fits into a desired label width, w
226 string formatted(string const & sin, int w, int size, int style)
232 string::size_type curpos = 0;
235 string::size_type const nxtpos1 = sin.find(' ', curpos);
236 string::size_type const nxtpos2 = sin.find('\n', curpos);
237 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
239 string const word = nxtpos == string::npos ?
240 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
242 bool const newline = (nxtpos2 != string::npos &&
245 string const line_plus_word =
246 line.empty() ? word : line + ' ' + word;
249 fl_get_string_width(style, size,
250 line_plus_word.c_str(),
251 int(line_plus_word.length()));
262 } else if (newline) {
263 sout += line_plus_word + '\n';
272 if (nxtpos == string::npos) {
285 void setCursorColor(int color)
287 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
288 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
289 fl_set_cursor_color(XC_watch, color, FL_WHITE);
290 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
296 // sorted by hand to prevent LyXLex from complaining on read().
297 keyword_item xformTags[] = {
298 { "\\gui_background", FL_COL1 },
299 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
300 { "\\gui_buttonleft", FL_LEFT_BCOL },
301 { "\\gui_buttonright", FL_RIGHT_BCOL },
302 { "\\gui_buttontop", FL_TOP_BCOL },
303 { "\\gui_inactive", FL_INACTIVE },
304 { "\\gui_pointer", FL_FREE_COL16 },
305 { "\\gui_push_button", FL_YELLOW },
306 { "\\gui_selected", FL_MCOL },
307 { "\\gui_text", FL_BLACK }
311 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
316 bool XformsColor::read(string const & filename)
318 LyXLex lexrc(xformTags, xformCount);
319 if (!lexrc.setFile(filename))
322 while (lexrc.isOK()) {
323 int const le = lexrc.lex();
326 case LyXLex::LEX_UNDEF:
327 lexrc.printError("Unknown tag `$$Token'");
329 case LyXLex::LEX_FEOF:
334 string const tag = lexrc.getString();
338 if (!lexrc.next()) break;
339 col.r = lexrc.getInteger();
341 if (!lexrc.next()) break;
342 col.g = lexrc.getInteger();
344 if (!lexrc.next()) break;
345 col.b = lexrc.getInteger();
347 fl_mapcolor(le, col.r, col.g, col.b);
349 if (tag == "\\gui_pointer") {
350 setCursorColor(FL_FREE_COL16);
358 bool XformsColor::write(string const & filename)
360 ofstream os(filename.c_str());
365 << "### file " << filename << "\n\n"
366 << "### This file is written by LyX, if you want to make your own\n"
367 << "### modifications you should do them from inside LyX and save\n"
370 for (int i = 0; i < xformCount; ++i) {
371 string const tag = xformTags[i].tag;
372 int const colorID = xformTags[i].code;
375 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
378 << color.r << ' ' << color.g << ' ' << color.b << '\n';
385 string RWInfo::error_message;
387 bool RWInfo::WriteableDir(string const & name)
389 error_message.erase();
391 if (!AbsolutePath(name)) {
392 error_message = _("The absolute path is required.");
396 FileInfo const tp(name);
397 if (!tp.isOK() || !tp.isDir()) {
398 error_message = _("Directory does not exist.");
402 if (!tp.writable()) {
403 error_message = _("Cannot write to this directory.");
411 bool RWInfo::ReadableDir(string const & name)
413 error_message.erase();
415 if (!AbsolutePath(name)) {
416 error_message = _("The absolute path is required.");
420 FileInfo const tp(name);
421 if (!tp.isOK() || !tp.isDir()) {
422 error_message = _("Directory does not exist.");
426 if (!tp.readable()) {
427 error_message = _("Cannot read this directory.");
435 bool RWInfo::WriteableFile(string const & name)
437 // A writeable file is either:
438 // * An existing file to which we have write access, or
439 // * A file that doesn't yet exist but that would exist in a writeable
442 error_message.erase();
445 error_message = _("No file input.");
449 string const dir = OnlyPath(name);
450 if (!AbsolutePath(dir)) {
451 error_message = _("The absolute path is required.");
457 if (!d.isOK() || !d.isDir()) {
461 if (!d.isOK() || !d.isDir()) {
462 error_message = _("Directory does not exist.");
467 error_message = _("Cannot write to this directory.");
472 if (dir == name || (f.isOK() && f.isDir())) {
473 error_message = _("A file is required, not a directory.");
477 if (f.isOK() && f.exist() && !f.writable()) {
478 error_message = _("Cannot write to this file.");
486 bool RWInfo::ReadableFile(string const & name)
488 error_message.erase();
491 error_message = _("No file input.");
495 string const dir = OnlyPath(name);
496 if (!AbsolutePath(dir)) {
497 error_message = _("The absolute path is required.");
503 if (!d.isOK() && !d.isDir()) {
507 if (!d.isOK() || !d.isDir()) {
508 error_message = _("Directory does not exist.");
513 error_message = _("Cannot read from this directory.");
518 if (dir == name || (f.isOK() && f.isDir())) {
519 error_message = _("A file is required, not a directory.");
524 error_message = _("File does not exist.");
529 error_message = _("Cannot read from this file.");