2 * \file xforms_helpers.C
3 * See the file COPYING.
5 * \author Angus Leeming
7 * Full author contact details are available in file CREDITS
13 #pragma implementation
16 #include "xforms_helpers.h"
20 #include "lyxlength.h"
21 #include "lyxgluelength.h"
23 #include "support/LAssert.h"
24 #include "support/FileInfo.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h" // frontStrip, strip
31 #include FORMS_H_LOCATION
37 // Set an FL_OBJECT to activated or deactivated
38 void setEnabled(FL_OBJECT * ob, bool enable)
41 fl_activate_object(ob);
42 fl_set_object_lcol(ob, FL_BLACK);
44 fl_deactivate_object(ob);
45 fl_set_object_lcol(ob, FL_INACTIVE);
50 // Given an fl_choice or an fl_browser, create a vector of its entries
51 vector<string> const getVector(FL_OBJECT * ob)
55 switch (ob->objclass) {
57 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
58 string const text = fl_get_choice_item_text(ob, i+1);
59 vec.push_back(trim(text));
63 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
64 string const text = fl_get_browser_line(ob, i+1);
65 vec.push_back(trim(text));
77 string const getString(FL_OBJECT * ob, int line)
81 switch (ob->objclass) {
83 lyx::Assert(line == -1);
84 tmp = fl_get_input(ob);
88 line = fl_get_browser(ob);
90 if (line >= 1 && line <= fl_get_browser_maxline(ob))
91 tmp = fl_get_browser_line(ob, line);
96 line = fl_get_choice(ob);
98 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
99 tmp = fl_get_choice_item_text(ob, line);
106 return (tmp) ? trim(tmp) : string();
109 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
112 lyx::Assert(input && input->objclass == FL_INPUT &&
113 choice && choice->objclass == FL_CHOICE);
115 string const length = trim(fl_get_input(input));
119 //don't return unit-from-choice if the input(field) contains a unit
120 if (isValidGlueLength(length))
123 string unit = trim(fl_get_choice_text(choice));
124 unit = subst(unit, "%%", "%");
126 return length + unit;
130 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
132 string const & default_unit)
134 // use input field only for gluelengths
135 if (!isValidLength(str) && !isStrDbl(str)) {
136 fl_set_input(input, str.c_str());
137 // we assume that "default_unit" is in the choice as "we"
138 // have control over that!
139 // No need to check for it's precence in the choice, therefore.
140 fl_set_choice_text(choice, default_unit.c_str());
142 updateWidgetsFromLength(input, choice,
143 LyXLength(str), default_unit);
148 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
149 LyXLength const & len,
150 string const & default_unit)
153 lyx::Assert(input && input->objclass == FL_INPUT &&
154 choice && choice->objclass == FL_CHOICE);
157 fl_set_input(input, "");
158 fl_set_choice_text(choice, default_unit.c_str());
160 ostringstream buffer;
161 buffer << len.value();
162 fl_set_input(input, buffer.str().c_str());
164 // Set the choice to the desired unit, if present in the choice.
165 // Else set the choice to the default unit.
166 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
168 vector<string> const vec = getVector(choice);
169 vector<string>::const_iterator it =
170 std::find(vec.begin(), vec.end(), unit);
171 if (it != vec.end()) {
172 fl_set_choice_text(choice, unit.c_str());
174 fl_set_choice_text(choice, default_unit.c_str());
180 // Take a string and add breaks so that it fits into a desired label width, w
181 string formatted(string const & sin, int w, int size, int style)
184 if (sin.empty()) return sout;
186 string::size_type curpos = 0;
189 string::size_type const nxtpos1 = sin.find(' ', curpos);
190 string::size_type const nxtpos2 = sin.find('\n', curpos);
191 string::size_type const nxtpos = std::min(nxtpos1, nxtpos1);
193 string const word = nxtpos == string::npos ?
194 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
196 bool const newline = (nxtpos2 != string::npos &&
199 string const line_plus_word =
200 line.empty() ? word : line + ' ' + word;
203 fl_get_string_width(style, size,
204 line_plus_word.c_str(),
205 int(line_plus_word.length()));
216 } else if (newline) {
217 sout += line_plus_word + '\n';
226 if (nxtpos == string::npos) {
239 void setCursorColor(int color)
241 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
242 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
243 fl_set_cursor_color(XC_watch, color, FL_WHITE);
244 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
250 // sorted by hand to prevent LyXLex from complaining on read().
251 keyword_item xformTags[] = {
252 { "\\gui_background", FL_COL1 },
253 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
254 { "\\gui_buttonleft", FL_LEFT_BCOL },
255 { "\\gui_buttonright", FL_RIGHT_BCOL },
256 { "\\gui_buttontop", FL_TOP_BCOL },
257 { "\\gui_inactive", FL_INACTIVE },
258 { "\\gui_pointer", FL_FREE_COL16 },
259 { "\\gui_push_button", FL_YELLOW },
260 { "\\gui_selected", FL_MCOL },
261 { "\\gui_text", FL_BLACK }
265 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
270 bool XformsColor::read(string const & filename)
272 LyXLex lexrc(xformTags, xformCount);
273 if (!lexrc.setFile(filename))
276 while (lexrc.isOK()) {
277 int const le = lexrc.lex();
280 case LyXLex::LEX_UNDEF:
281 lexrc.printError("Unknown tag `$$Token'");
283 case LyXLex::LEX_FEOF:
288 string const tag = lexrc.getString();
292 if (!lexrc.next()) break;
293 col.r = lexrc.getInteger();
295 if (!lexrc.next()) break;
296 col.g = lexrc.getInteger();
298 if (!lexrc.next()) break;
299 col.b = lexrc.getInteger();
301 fl_mapcolor(le, col.r, col.g, col.b);
303 if (tag == "\\gui_pointer") {
304 setCursorColor(FL_FREE_COL16);
312 bool XformsColor::write(string const & filename)
314 ofstream os(filename.c_str());
319 << "### file " << filename << "\n\n"
320 << "### This file is written by LyX, if you want to make your own\n"
321 << "### modifications you should do them from inside LyX and save\n"
324 for (int i = 0; i < xformCount; ++i) {
325 string const tag = xformTags[i].tag;
326 int const colorID = xformTags[i].code;
329 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
332 << color.r << " " << color.g << " " << color.b << "\n";
339 string RWInfo::error_message;
341 bool RWInfo::WriteableDir(string const & name)
343 error_message.erase();
345 if (!AbsolutePath(name)) {
346 error_message = _("The absolute path is required.");
350 FileInfo const tp(name);
351 if (!tp.isOK() || !tp.isDir()) {
352 error_message = _("Directory does not exist.");
356 if (!tp.writable()) {
357 error_message = _("Cannot write to this directory.");
365 bool RWInfo::ReadableDir(string const & name)
367 error_message.erase();
369 if (!AbsolutePath(name)) {
370 error_message = _("The absolute path is required.");
374 FileInfo const tp(name);
375 if (!tp.isOK() || !tp.isDir()) {
376 error_message = _("Directory does not exist.");
380 if (!tp.readable()) {
381 error_message = _("Cannot read this directory.");
389 bool RWInfo::WriteableFile(string const & name)
391 // A writeable file is either:
392 // * An existing file to which we have write access, or
393 // * A file that doesn't yet exist but that would exist in a writeable
396 error_message.erase();
399 error_message = _("No file input.");
403 string const dir = OnlyPath(name);
404 if (!AbsolutePath(dir)) {
405 error_message = _("The absolute path is required.");
411 if (!d.isOK() || !d.isDir()) {
415 if (!d.isOK() || !d.isDir()) {
416 error_message = _("Directory does not exist.");
421 error_message = _("Cannot write to this directory.");
426 if (dir == name || (f.isOK() && f.isDir())) {
427 error_message = _("A file is required, not a directory.");
431 if (f.isOK() && f.exist() && !f.writable()) {
432 error_message = _("Cannot write to this file.");
440 bool RWInfo::ReadableFile(string const & name)
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 read from this directory.");
472 if (dir == name || (f.isOK() && f.isDir())) {
473 error_message = _("A file is required, not a directory.");
478 error_message = _("File does not exist.");
483 error_message = _("Cannot read from this file.");