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 #pragma implementation
17 #include "xforms_helpers.h"
21 #include "lyxlength.h"
22 #include "lyxgluelength.h"
24 #include "support/LAssert.h"
25 #include "support/FileInfo.h"
26 #include "support/filetools.h"
27 #include "support/lstrings.h" // frontStrip, strip
32 #include FORMS_H_LOCATION
38 bool isActive(FL_OBJECT * ob)
40 return ob && ob->active > 0;
44 // Set an FL_OBJECT to activated or deactivated
45 void setEnabled(FL_OBJECT * ob, bool enable)
48 fl_activate_object(ob);
49 fl_set_object_lcol(ob, FL_BLACK);
51 fl_deactivate_object(ob);
52 fl_set_object_lcol(ob, FL_INACTIVE);
57 // Given an fl_choice or an fl_browser, create a vector of its entries
58 vector<string> const getVector(FL_OBJECT * ob)
62 switch (ob->objclass) {
64 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
65 string const text = fl_get_choice_item_text(ob, i+1);
66 vec.push_back(trim(text));
70 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
71 string const text = fl_get_browser_line(ob, i+1);
72 vec.push_back(trim(text));
84 string const getString(FL_OBJECT * ob, int line)
88 switch (ob->objclass) {
90 lyx::Assert(line == -1);
91 tmp = fl_get_input(ob);
95 line = fl_get_browser(ob);
97 if (line >= 1 && line <= fl_get_browser_maxline(ob))
98 tmp = fl_get_browser_line(ob, line);
103 line = fl_get_choice(ob);
105 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
106 tmp = fl_get_choice_item_text(ob, line);
113 return (tmp) ? trim(tmp) : string();
116 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
119 lyx::Assert(input && input->objclass == FL_INPUT &&
120 choice && choice->objclass == FL_CHOICE);
122 string const length = trim(fl_get_input(input));
126 //don't return unit-from-choice if the input(field) contains a unit
127 if (isValidGlueLength(length))
130 string unit = trim(fl_get_choice_text(choice));
131 unit = subst(unit, "%%", "%");
133 return length + unit;
137 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
139 string const & default_unit)
141 // use input field only for gluelengths
142 if (!isValidLength(str) && !isStrDbl(str)) {
143 fl_set_input(input, str.c_str());
144 // we assume that "default_unit" is in the choice as "we"
145 // have control over that!
146 // No need to check for its presence in the choice, therefore.
147 fl_set_choice_text(choice, default_unit.c_str());
149 updateWidgetsFromLength(input, choice,
150 LyXLength(str), default_unit);
155 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
156 LyXLength const & len,
157 string const & default_unit)
160 lyx::Assert(input && input->objclass == FL_INPUT &&
161 choice && choice->objclass == FL_CHOICE);
164 fl_set_input(input, "");
165 fl_set_choice_text(choice, default_unit.c_str());
167 ostringstream buffer;
168 buffer << len.value();
169 fl_set_input(input, buffer.str().c_str());
171 // Set the choice to the desired unit, if present in the choice.
172 // Else set the choice to the default unit.
173 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
175 vector<string> const vec = getVector(choice);
176 vector<string>::const_iterator it =
177 std::find(vec.begin(), vec.end(), unit);
178 if (it != vec.end()) {
179 fl_set_choice_text(choice, unit.c_str());
181 fl_set_choice_text(choice, default_unit.c_str());
187 // Take a string and add breaks so that it fits into a desired label width, w
188 string formatted(string const & sin, int w, int size, int style)
191 if (sin.empty()) return sout;
193 string::size_type curpos = 0;
196 string::size_type const nxtpos1 = sin.find(' ', curpos);
197 string::size_type const nxtpos2 = sin.find('\n', curpos);
198 string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
200 string const word = nxtpos == string::npos ?
201 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
203 bool const newline = (nxtpos2 != string::npos &&
206 string const line_plus_word =
207 line.empty() ? word : line + ' ' + word;
210 fl_get_string_width(style, size,
211 line_plus_word.c_str(),
212 int(line_plus_word.length()));
223 } else if (newline) {
224 sout += line_plus_word + '\n';
233 if (nxtpos == string::npos) {
246 void setCursorColor(int color)
248 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
249 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
250 fl_set_cursor_color(XC_watch, color, FL_WHITE);
251 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
257 // sorted by hand to prevent LyXLex from complaining on read().
258 keyword_item xformTags[] = {
259 { "\\gui_background", FL_COL1 },
260 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
261 { "\\gui_buttonleft", FL_LEFT_BCOL },
262 { "\\gui_buttonright", FL_RIGHT_BCOL },
263 { "\\gui_buttontop", FL_TOP_BCOL },
264 { "\\gui_inactive", FL_INACTIVE },
265 { "\\gui_pointer", FL_FREE_COL16 },
266 { "\\gui_push_button", FL_YELLOW },
267 { "\\gui_selected", FL_MCOL },
268 { "\\gui_text", FL_BLACK }
272 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
277 bool XformsColor::read(string const & filename)
279 LyXLex lexrc(xformTags, xformCount);
280 if (!lexrc.setFile(filename))
283 while (lexrc.isOK()) {
284 int const le = lexrc.lex();
287 case LyXLex::LEX_UNDEF:
288 lexrc.printError("Unknown tag `$$Token'");
290 case LyXLex::LEX_FEOF:
295 string const tag = lexrc.getString();
299 if (!lexrc.next()) break;
300 col.r = lexrc.getInteger();
302 if (!lexrc.next()) break;
303 col.g = lexrc.getInteger();
305 if (!lexrc.next()) break;
306 col.b = lexrc.getInteger();
308 fl_mapcolor(le, col.r, col.g, col.b);
310 if (tag == "\\gui_pointer") {
311 setCursorColor(FL_FREE_COL16);
319 bool XformsColor::write(string const & filename)
321 ofstream os(filename.c_str());
326 << "### file " << filename << "\n\n"
327 << "### This file is written by LyX, if you want to make your own\n"
328 << "### modifications you should do them from inside LyX and save\n"
331 for (int i = 0; i < xformCount; ++i) {
332 string const tag = xformTags[i].tag;
333 int const colorID = xformTags[i].code;
336 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
339 << color.r << " " << color.g << " " << color.b << "\n";
346 string RWInfo::error_message;
348 bool RWInfo::WriteableDir(string const & name)
350 error_message.erase();
352 if (!AbsolutePath(name)) {
353 error_message = _("The absolute path is required.");
357 FileInfo const tp(name);
358 if (!tp.isOK() || !tp.isDir()) {
359 error_message = _("Directory does not exist.");
363 if (!tp.writable()) {
364 error_message = _("Cannot write to this directory.");
372 bool RWInfo::ReadableDir(string const & name)
374 error_message.erase();
376 if (!AbsolutePath(name)) {
377 error_message = _("The absolute path is required.");
381 FileInfo const tp(name);
382 if (!tp.isOK() || !tp.isDir()) {
383 error_message = _("Directory does not exist.");
387 if (!tp.readable()) {
388 error_message = _("Cannot read this directory.");
396 bool RWInfo::WriteableFile(string const & name)
398 // A writeable file is either:
399 // * An existing file to which we have write access, or
400 // * A file that doesn't yet exist but that would exist in a writeable
403 error_message.erase();
406 error_message = _("No file input.");
410 string const dir = OnlyPath(name);
411 if (!AbsolutePath(dir)) {
412 error_message = _("The absolute path is required.");
418 if (!d.isOK() || !d.isDir()) {
422 if (!d.isOK() || !d.isDir()) {
423 error_message = _("Directory does not exist.");
428 error_message = _("Cannot write to this directory.");
433 if (dir == name || (f.isOK() && f.isDir())) {
434 error_message = _("A file is required, not a directory.");
438 if (f.isOK() && f.exist() && !f.writable()) {
439 error_message = _("Cannot write to this file.");
447 bool RWInfo::ReadableFile(string const & name)
449 error_message.erase();
452 error_message = _("No file input.");
456 string const dir = OnlyPath(name);
457 if (!AbsolutePath(dir)) {
458 error_message = _("The absolute path is required.");
464 if (!d.isOK() && !d.isDir()) {
468 if (!d.isOK() || !d.isDir()) {
469 error_message = _("Directory does not exist.");
474 error_message = _("Cannot read from this directory.");
479 if (dir == name || (f.isOK() && f.isDir())) {
480 error_message = _("A file is required, not a directory.");
485 error_message = _("File does not exist.");
490 error_message = _("Cannot read from this file.");