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 "lyxlength.h"
18 #include "lyxgluelength.h"
21 #include "support/LAssert.h"
22 #include "support/FileInfo.h"
23 #include "support/filetools.h"
24 #include "support/tostr.h"
25 #include "support/lstrings.h" // frontStrip, strip
27 #include "lyx_forms.h"
32 using namespace lyx::support;
40 bool isActive(FL_OBJECT * ob)
42 return ob && ob->active > 0;
46 std::pair<string, string> parse_shortcut(string const & str)
48 string::size_type i = str.find_first_of("&");
49 if (i == string::npos || i == str.length() - 1)
50 return make_pair(str, string());
54 string::value_type c = str[i + 1];
55 return make_pair(str.substr(0, i) + str.substr(i + 1),
60 // A wrapper for the xforms routine, but this one accepts uint args
61 unsigned long fl_getmcolor(int i,
62 unsigned int * r, unsigned int * g, unsigned int * b)
65 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
73 // Set an FL_OBJECT to activated or deactivated
74 void setEnabled(FL_OBJECT * ob, bool enable)
77 fl_activate_object(ob);
78 fl_set_object_lcol(ob, FL_LCOL);
80 fl_deactivate_object(ob);
81 fl_set_object_lcol(ob, FL_INACTIVE);
86 // Given an fl_choice or an fl_browser, create a vector of its entries
87 vector<string> const getVector(FL_OBJECT * ob)
91 switch (ob->objclass) {
93 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
94 string const text = fl_get_choice_item_text(ob, i+1);
95 vec.push_back(trim(text));
99 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
100 string const text = fl_get_browser_line(ob, i+1);
101 vec.push_back(trim(text));
113 string const getString(FL_OBJECT * ob, int line)
115 // Negative line value does not make sense.
118 char const * tmp = 0;
119 switch (ob->objclass) {
121 tmp = fl_get_input(ob);
126 line = fl_get_browser(ob);
128 if (line >= 1 && line <= fl_get_browser_maxline(ob))
129 tmp = fl_get_browser_line(ob, line);
134 line = fl_get_choice(ob);
136 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
137 tmp = fl_get_choice_item_text(ob, line);
141 tmp = fl_get_combox_text(ob);
148 return tmp ? trim(tmp) : string();
151 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
154 Assert(input && input->objclass == FL_INPUT &&
155 choice && choice->objclass == FL_CHOICE);
157 string const length = trim(fl_get_input(input));
161 // don't return unit-from-choice if the input(field) contains a unit
162 if (isValidGlueLength(length))
165 string unit = trim(fl_get_choice_text(choice));
166 unit = subst(unit, "%%", "%");
168 return length + unit;
172 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
174 string const & default_unit)
176 // use input field only for gluelengths
177 if (!isValidLength(str) && !isStrDbl(str)) {
178 fl_set_input(input, str.c_str());
179 // we assume that "default_unit" is in the choice as "we"
180 // have control over that!
181 // No need to check for its presence in the choice, therefore.
182 fl_set_choice_text(choice, default_unit.c_str());
184 updateWidgetsFromLength(input, choice,
185 LyXLength(str), default_unit);
190 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
191 LyXLength const & len,
192 string const & default_unit)
195 Assert(input && input->objclass == FL_INPUT &&
196 choice && choice->objclass == FL_CHOICE);
199 fl_set_input(input, "");
200 fl_set_choice_text(choice, default_unit.c_str());
202 fl_set_input(input, tostr(len.value()).c_str());
204 // Set the choice to the desired unit, if present in the choice.
205 // Else set the choice to the default unit.
206 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
208 vector<string> const vec = getVector(choice);
209 vector<string>::const_iterator it =
210 std::find(vec.begin(), vec.end(), unit);
211 if (it != vec.end()) {
212 fl_set_choice_text(choice, unit.c_str());
214 fl_set_choice_text(choice, default_unit.c_str());
220 // Take a string and add breaks so that it fits into a desired label width, w
221 string formatted(string const & sin, int w, int size, int style)
227 string::size_type curpos = 0;
230 string::size_type const nxtpos1 = sin.find(' ', curpos);
231 string::size_type const nxtpos2 = sin.find('\n', curpos);
232 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
234 string const word = nxtpos == string::npos ?
235 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
237 bool const newline = (nxtpos2 != string::npos &&
240 string const line_plus_word =
241 line.empty() ? word : line + ' ' + word;
244 fl_get_string_width(style, size,
245 line_plus_word.c_str(),
246 int(line_plus_word.length()));
257 } else if (newline) {
258 sout += line_plus_word + '\n';
267 if (nxtpos == string::npos) {
280 void setCursorColor(int color)
282 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
283 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
284 fl_set_cursor_color(XC_watch, color, FL_WHITE);
285 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
291 // sorted by hand to prevent LyXLex from complaining on read().
292 keyword_item xformTags[] = {
293 { "\\gui_background", FL_COL1 },
294 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
295 { "\\gui_buttonleft", FL_LEFT_BCOL },
296 { "\\gui_buttonright", FL_RIGHT_BCOL },
297 { "\\gui_buttontop", FL_TOP_BCOL },
298 { "\\gui_inactive", FL_INACTIVE },
299 { "\\gui_pointer", FL_FREE_COL16 },
300 { "\\gui_push_button", FL_YELLOW },
301 { "\\gui_selected", FL_MCOL },
302 { "\\gui_text", FL_BLACK }
306 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
311 bool XformsColor::read(string const & filename)
313 LyXLex lexrc(xformTags, xformCount);
314 if (!lexrc.setFile(filename))
317 while (lexrc.isOK()) {
318 int const le = lexrc.lex();
321 case LyXLex::LEX_UNDEF:
322 lexrc.printError("Unknown tag `$$Token'");
324 case LyXLex::LEX_FEOF:
329 string const tag = lexrc.getString();
333 if (!lexrc.next()) break;
334 col.r = lexrc.getInteger();
336 if (!lexrc.next()) break;
337 col.g = lexrc.getInteger();
339 if (!lexrc.next()) break;
340 col.b = lexrc.getInteger();
342 fl_mapcolor(le, col.r, col.g, col.b);
344 if (tag == "\\gui_pointer") {
345 setCursorColor(FL_FREE_COL16);
353 bool XformsColor::write(string const & filename)
355 ofstream os(filename.c_str());
360 << "### file " << filename << "\n\n"
361 << "### This file is written by LyX, if you want to make your own\n"
362 << "### modifications you should do them from inside LyX and save\n"
365 for (int i = 0; i < xformCount; ++i) {
366 string const tag = xformTags[i].tag;
367 int const colorID = xformTags[i].code;
370 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
373 << color.r << ' ' << color.g << ' ' << color.b << '\n';
380 string RWInfo::error_message;
382 bool RWInfo::WriteableDir(string const & name)
384 error_message.erase();
386 if (!AbsolutePath(name)) {
387 error_message = _("The absolute path is required.");
391 FileInfo const tp(name);
392 if (!tp.isOK() || !tp.isDir()) {
393 error_message = _("Directory does not exist.");
397 if (!tp.writable()) {
398 error_message = _("Cannot write to this directory.");
406 bool RWInfo::ReadableDir(string const & name)
408 error_message.erase();
410 if (!AbsolutePath(name)) {
411 error_message = _("The absolute path is required.");
415 FileInfo const tp(name);
416 if (!tp.isOK() || !tp.isDir()) {
417 error_message = _("Directory does not exist.");
421 if (!tp.readable()) {
422 error_message = _("Cannot read this directory.");
430 bool RWInfo::WriteableFile(string const & name)
432 // A writeable file is either:
433 // * An existing file to which we have write access, or
434 // * A file that doesn't yet exist but that would exist in a writeable
437 error_message.erase();
440 error_message = _("No file input.");
444 string const dir = OnlyPath(name);
445 if (!AbsolutePath(dir)) {
446 error_message = _("The absolute path is required.");
452 if (!d.isOK() || !d.isDir()) {
456 if (!d.isOK() || !d.isDir()) {
457 error_message = _("Directory does not exist.");
462 error_message = _("Cannot write to this directory.");
467 if (dir == name || (f.isOK() && f.isDir())) {
468 error_message = _("A file is required, not a directory.");
472 if (f.isOK() && f.exist() && !f.writable()) {
473 error_message = _("Cannot write to this file.");
481 bool RWInfo::ReadableFile(string const & name)
483 error_message.erase();
486 error_message = _("No file input.");
490 string const dir = OnlyPath(name);
491 if (!AbsolutePath(dir)) {
492 error_message = _("The absolute path is required.");
498 if (!d.isOK() && !d.isDir()) {
502 if (!d.isOK() || !d.isDir()) {
503 error_message = _("Directory does not exist.");
508 error_message = _("Cannot read from this directory.");
513 if (dir == name || (f.isOK() && f.isDir())) {
514 error_message = _("A file is required, not a directory.");
519 error_message = _("File does not exist.");
524 error_message = _("Cannot read from this file.");