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/lstrings.h" // frontStrip, strip
26 #include "lyx_forms.h"
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 lyx::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 lyx::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 lyx::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 ostringstream buffer;
200 buffer << len.value();
201 fl_set_input(input, buffer.str().c_str());
203 // Set the choice to the desired unit, if present in the choice.
204 // Else set the choice to the default unit.
205 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
207 vector<string> const vec = getVector(choice);
208 vector<string>::const_iterator it =
209 std::find(vec.begin(), vec.end(), unit);
210 if (it != vec.end()) {
211 fl_set_choice_text(choice, unit.c_str());
213 fl_set_choice_text(choice, default_unit.c_str());
219 // Take a string and add breaks so that it fits into a desired label width, w
220 string formatted(string const & sin, int w, int size, int style)
223 if (sin.empty()) return sout;
225 string::size_type curpos = 0;
228 string::size_type const nxtpos1 = sin.find(' ', curpos);
229 string::size_type const nxtpos2 = sin.find('\n', curpos);
230 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
232 string const word = nxtpos == string::npos ?
233 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
235 bool const newline = (nxtpos2 != string::npos &&
238 string const line_plus_word =
239 line.empty() ? word : line + ' ' + word;
242 fl_get_string_width(style, size,
243 line_plus_word.c_str(),
244 int(line_plus_word.length()));
255 } else if (newline) {
256 sout += line_plus_word + '\n';
265 if (nxtpos == string::npos) {
278 void setCursorColor(int color)
280 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
281 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
282 fl_set_cursor_color(XC_watch, color, FL_WHITE);
283 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
289 // sorted by hand to prevent LyXLex from complaining on read().
290 keyword_item xformTags[] = {
291 { "\\gui_background", FL_COL1 },
292 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
293 { "\\gui_buttonleft", FL_LEFT_BCOL },
294 { "\\gui_buttonright", FL_RIGHT_BCOL },
295 { "\\gui_buttontop", FL_TOP_BCOL },
296 { "\\gui_inactive", FL_INACTIVE },
297 { "\\gui_pointer", FL_FREE_COL16 },
298 { "\\gui_push_button", FL_YELLOW },
299 { "\\gui_selected", FL_MCOL },
300 { "\\gui_text", FL_BLACK }
304 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
309 bool XformsColor::read(string const & filename)
311 LyXLex lexrc(xformTags, xformCount);
312 if (!lexrc.setFile(filename))
315 while (lexrc.isOK()) {
316 int const le = lexrc.lex();
319 case LyXLex::LEX_UNDEF:
320 lexrc.printError("Unknown tag `$$Token'");
322 case LyXLex::LEX_FEOF:
327 string const tag = lexrc.getString();
331 if (!lexrc.next()) break;
332 col.r = lexrc.getInteger();
334 if (!lexrc.next()) break;
335 col.g = lexrc.getInteger();
337 if (!lexrc.next()) break;
338 col.b = lexrc.getInteger();
340 fl_mapcolor(le, col.r, col.g, col.b);
342 if (tag == "\\gui_pointer") {
343 setCursorColor(FL_FREE_COL16);
351 bool XformsColor::write(string const & filename)
353 ofstream os(filename.c_str());
358 << "### file " << filename << "\n\n"
359 << "### This file is written by LyX, if you want to make your own\n"
360 << "### modifications you should do them from inside LyX and save\n"
363 for (int i = 0; i < xformCount; ++i) {
364 string const tag = xformTags[i].tag;
365 int const colorID = xformTags[i].code;
368 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
371 << color.r << ' ' << color.g << ' ' << color.b << '\n';
378 string RWInfo::error_message;
380 bool RWInfo::WriteableDir(string const & name)
382 error_message.erase();
384 if (!AbsolutePath(name)) {
385 error_message = _("The absolute path is required.");
389 FileInfo const tp(name);
390 if (!tp.isOK() || !tp.isDir()) {
391 error_message = _("Directory does not exist.");
395 if (!tp.writable()) {
396 error_message = _("Cannot write to this directory.");
404 bool RWInfo::ReadableDir(string const & name)
406 error_message.erase();
408 if (!AbsolutePath(name)) {
409 error_message = _("The absolute path is required.");
413 FileInfo const tp(name);
414 if (!tp.isOK() || !tp.isDir()) {
415 error_message = _("Directory does not exist.");
419 if (!tp.readable()) {
420 error_message = _("Cannot read this directory.");
428 bool RWInfo::WriteableFile(string const & name)
430 // A writeable file is either:
431 // * An existing file to which we have write access, or
432 // * A file that doesn't yet exist but that would exist in a writeable
435 error_message.erase();
438 error_message = _("No file input.");
442 string const dir = OnlyPath(name);
443 if (!AbsolutePath(dir)) {
444 error_message = _("The absolute path is required.");
450 if (!d.isOK() || !d.isDir()) {
454 if (!d.isOK() || !d.isDir()) {
455 error_message = _("Directory does not exist.");
460 error_message = _("Cannot write to this directory.");
465 if (dir == name || (f.isOK() && f.isDir())) {
466 error_message = _("A file is required, not a directory.");
470 if (f.isOK() && f.exist() && !f.writable()) {
471 error_message = _("Cannot write to this file.");
479 bool RWInfo::ReadableFile(string const & name)
481 error_message.erase();
484 error_message = _("No file input.");
488 string const dir = OnlyPath(name);
489 if (!AbsolutePath(dir)) {
490 error_message = _("The absolute path is required.");
496 if (!d.isOK() && !d.isDir()) {
500 if (!d.isOK() || !d.isDir()) {
501 error_message = _("Directory does not exist.");
506 error_message = _("Cannot read from this directory.");
511 if (dir == name || (f.isOK() && f.isDir())) {
512 error_message = _("A file is required, not a directory.");
517 error_message = _("File does not exist.");
522 error_message = _("Cannot read from this file.");