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
14 #include "xforms_helpers.h"
18 #include "lyxlength.h"
19 #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
28 #include FORMS_H_LOCATION
35 bool isActive(FL_OBJECT * ob)
37 return ob && ob->active > 0;
40 std::pair<string, string> parse_shortcut(string const & str)
42 string::size_type i = str.find_first_of("&");
43 if (i == string::npos || i == str.length() - 1)
44 return make_pair(str, string());
48 string::value_type c = str[i + 1];
49 return make_pair(str.substr(0, i) + str.substr(i + 1),
54 // A wrapper for the xforms routine, but this one accepts uint args
55 unsigned long fl_getmcolor(int i,
56 unsigned int * r, unsigned int * g, unsigned int * b)
59 unsigned long ret_val = ::fl_getmcolor(i, &r2, &g2, &b2);
67 // Set an FL_OBJECT to activated or deactivated
68 void setEnabled(FL_OBJECT * ob, bool enable)
71 fl_activate_object(ob);
72 fl_set_object_lcol(ob, FL_LCOL);
74 fl_deactivate_object(ob);
75 fl_set_object_lcol(ob, FL_INACTIVE);
80 // Given an fl_choice or an fl_browser, create a vector of its entries
81 vector<string> const getVector(FL_OBJECT * ob)
85 switch (ob->objclass) {
87 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
88 string const text = fl_get_choice_item_text(ob, i+1);
89 vec.push_back(trim(text));
93 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
94 string const text = fl_get_browser_line(ob, i+1);
95 vec.push_back(trim(text));
107 string const getString(FL_OBJECT * ob, int line)
109 // Negative line value does not make sense.
110 lyx::Assert(line >= 0);
112 char const * tmp = 0;
113 switch (ob->objclass) {
115 tmp = fl_get_input(ob);
120 line = fl_get_browser(ob);
122 if (line >= 1 && line <= fl_get_browser_maxline(ob))
123 tmp = fl_get_browser_line(ob, line);
128 line = fl_get_choice(ob);
130 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
131 tmp = fl_get_choice_item_text(ob, line);
138 return tmp ? trim(tmp) : string();
141 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
144 lyx::Assert(input && input->objclass == FL_INPUT &&
145 choice && choice->objclass == FL_CHOICE);
147 string const length = trim(fl_get_input(input));
151 // don't return unit-from-choice if the input(field) contains a unit
152 if (isValidGlueLength(length))
155 string unit = trim(fl_get_choice_text(choice));
156 unit = subst(unit, "%%", "%");
158 return length + unit;
162 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
164 string const & default_unit)
166 // use input field only for gluelengths
167 if (!isValidLength(str) && !isStrDbl(str)) {
168 fl_set_input(input, str.c_str());
169 // we assume that "default_unit" is in the choice as "we"
170 // have control over that!
171 // No need to check for its presence in the choice, therefore.
172 fl_set_choice_text(choice, default_unit.c_str());
174 updateWidgetsFromLength(input, choice,
175 LyXLength(str), default_unit);
180 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
181 LyXLength const & len,
182 string const & default_unit)
185 lyx::Assert(input && input->objclass == FL_INPUT &&
186 choice && choice->objclass == FL_CHOICE);
189 fl_set_input(input, "");
190 fl_set_choice_text(choice, default_unit.c_str());
192 ostringstream buffer;
193 buffer << len.value();
194 fl_set_input(input, buffer.str().c_str());
196 // Set the choice to the desired unit, if present in the choice.
197 // Else set the choice to the default unit.
198 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
200 vector<string> const vec = getVector(choice);
201 vector<string>::const_iterator it =
202 std::find(vec.begin(), vec.end(), unit);
203 if (it != vec.end()) {
204 fl_set_choice_text(choice, unit.c_str());
206 fl_set_choice_text(choice, default_unit.c_str());
212 // Take a string and add breaks so that it fits into a desired label width, w
213 string formatted(string const & sin, int w, int size, int style)
216 if (sin.empty()) return sout;
218 string::size_type curpos = 0;
221 string::size_type const nxtpos1 = sin.find(' ', curpos);
222 string::size_type const nxtpos2 = sin.find('\n', curpos);
223 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
225 string const word = nxtpos == string::npos ?
226 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
228 bool const newline = (nxtpos2 != string::npos &&
231 string const line_plus_word =
232 line.empty() ? word : line + ' ' + word;
235 fl_get_string_width(style, size,
236 line_plus_word.c_str(),
237 int(line_plus_word.length()));
248 } else if (newline) {
249 sout += line_plus_word + '\n';
258 if (nxtpos == string::npos) {
271 void setCursorColor(int color)
273 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
274 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
275 fl_set_cursor_color(XC_watch, color, FL_WHITE);
276 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
282 // sorted by hand to prevent LyXLex from complaining on read().
283 keyword_item xformTags[] = {
284 { "\\gui_background", FL_COL1 },
285 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
286 { "\\gui_buttonleft", FL_LEFT_BCOL },
287 { "\\gui_buttonright", FL_RIGHT_BCOL },
288 { "\\gui_buttontop", FL_TOP_BCOL },
289 { "\\gui_inactive", FL_INACTIVE },
290 { "\\gui_pointer", FL_FREE_COL16 },
291 { "\\gui_push_button", FL_YELLOW },
292 { "\\gui_selected", FL_MCOL },
293 { "\\gui_text", FL_BLACK }
297 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
302 bool XformsColor::read(string const & filename)
304 LyXLex lexrc(xformTags, xformCount);
305 if (!lexrc.setFile(filename))
308 while (lexrc.isOK()) {
309 int const le = lexrc.lex();
312 case LyXLex::LEX_UNDEF:
313 lexrc.printError("Unknown tag `$$Token'");
315 case LyXLex::LEX_FEOF:
320 string const tag = lexrc.getString();
324 if (!lexrc.next()) break;
325 col.r = lexrc.getInteger();
327 if (!lexrc.next()) break;
328 col.g = lexrc.getInteger();
330 if (!lexrc.next()) break;
331 col.b = lexrc.getInteger();
333 fl_mapcolor(le, col.r, col.g, col.b);
335 if (tag == "\\gui_pointer") {
336 setCursorColor(FL_FREE_COL16);
344 bool XformsColor::write(string const & filename)
346 ofstream os(filename.c_str());
351 << "### file " << filename << "\n\n"
352 << "### This file is written by LyX, if you want to make your own\n"
353 << "### modifications you should do them from inside LyX and save\n"
356 for (int i = 0; i < xformCount; ++i) {
357 string const tag = xformTags[i].tag;
358 int const colorID = xformTags[i].code;
361 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
364 << color.r << ' ' << color.g << ' ' << color.b << '\n';
371 string RWInfo::error_message;
373 bool RWInfo::WriteableDir(string const & name)
375 error_message.erase();
377 if (!AbsolutePath(name)) {
378 error_message = _("The absolute path is required.");
382 FileInfo const tp(name);
383 if (!tp.isOK() || !tp.isDir()) {
384 error_message = _("Directory does not exist.");
388 if (!tp.writable()) {
389 error_message = _("Cannot write to this directory.");
397 bool RWInfo::ReadableDir(string const & name)
399 error_message.erase();
401 if (!AbsolutePath(name)) {
402 error_message = _("The absolute path is required.");
406 FileInfo const tp(name);
407 if (!tp.isOK() || !tp.isDir()) {
408 error_message = _("Directory does not exist.");
412 if (!tp.readable()) {
413 error_message = _("Cannot read this directory.");
421 bool RWInfo::WriteableFile(string const & name)
423 // A writeable file is either:
424 // * An existing file to which we have write access, or
425 // * A file that doesn't yet exist but that would exist in a writeable
428 error_message.erase();
431 error_message = _("No file input.");
435 string const dir = OnlyPath(name);
436 if (!AbsolutePath(dir)) {
437 error_message = _("The absolute path is required.");
443 if (!d.isOK() || !d.isDir()) {
447 if (!d.isOK() || !d.isDir()) {
448 error_message = _("Directory does not exist.");
453 error_message = _("Cannot write to this directory.");
458 if (dir == name || (f.isOK() && f.isDir())) {
459 error_message = _("A file is required, not a directory.");
463 if (f.isOK() && f.exist() && !f.writable()) {
464 error_message = _("Cannot write to this file.");
472 bool RWInfo::ReadableFile(string const & name)
474 error_message.erase();
477 error_message = _("No file input.");
481 string const dir = OnlyPath(name);
482 if (!AbsolutePath(dir)) {
483 error_message = _("The absolute path is required.");
489 if (!d.isOK() && !d.isDir()) {
493 if (!d.isOK() || !d.isDir()) {
494 error_message = _("Directory does not exist.");
499 error_message = _("Cannot read from this directory.");
504 if (dir == name || (f.isOK() && f.isDir())) {
505 error_message = _("A file is required, not a directory.");
510 error_message = _("File does not exist.");
515 error_message = _("Cannot read from this file.");