2 * \file xforms_helpers.C
3 * Copyright 2000-2002 The LyX Team.
4 * See the file COPYING.
6 * \author Angus Leeming <leeming@lyx.org>
12 #pragma implementation
15 #include "xforms_helpers.h"
19 #include "lyxlength.h"
20 #include "lyxgluelength.h"
22 #include "support/LAssert.h"
23 #include "support/FileInfo.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h" // frontStrip, strip
30 #include FORMS_H_LOCATION
36 // Set an FL_OBJECT to activated or deactivated
37 void setEnabled(FL_OBJECT * ob, bool enable)
40 fl_activate_object(ob);
41 fl_set_object_lcol(ob, FL_BLACK);
43 fl_deactivate_object(ob);
44 fl_set_object_lcol(ob, FL_INACTIVE);
49 // Given an fl_choice or an fl_browser, create a vector of its entries
50 vector<string> const getVector(FL_OBJECT * ob)
54 switch (ob->objclass) {
56 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
57 string const text = fl_get_choice_item_text(ob, i+1);
58 vec.push_back(trim(text));
62 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
63 string const text = fl_get_browser_line(ob, i+1);
64 vec.push_back(trim(text));
76 string const getString(FL_OBJECT * ob, int line)
80 switch (ob->objclass) {
82 lyx::Assert(line == -1);
83 tmp = fl_get_input(ob);
87 line = fl_get_browser(ob);
89 if (line >= 1 && line <= fl_get_browser_maxline(ob))
90 tmp = fl_get_browser_line(ob, line);
95 line = fl_get_choice(ob);
97 if (line >= 1 && line <= fl_get_choice_maxitems(ob))
98 tmp = fl_get_choice_item_text(ob, line);
105 return (tmp) ? trim(tmp) : string();
108 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
111 lyx::Assert(input && input->objclass == FL_INPUT &&
112 choice && choice->objclass == FL_CHOICE);
114 string const length = trim(fl_get_input(input));
118 //don't return unit-from-choice if the input(field) contains a unit
119 if (isValidGlueLength(length))
122 string unit = trim(fl_get_choice_text(choice));
123 unit = subst(unit, "%%", "%");
125 return length + unit;
129 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
131 string const & default_unit)
133 // use input field only for gluelengths
134 if (!isValidLength(str) && !isStrDbl(str)) {
135 fl_set_input(input, str.c_str());
136 // we assume that "default_unit" is in the choice as "we"
137 // have control over that!
138 // No need to check for it's precence in the choice, therefore.
139 fl_set_choice_text(choice, default_unit.c_str());
141 updateWidgetsFromLength(input, choice,
142 LyXLength(str), default_unit);
147 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
148 LyXLength const & len,
149 string const & default_unit)
152 lyx::Assert(input && input->objclass == FL_INPUT &&
153 choice && choice->objclass == FL_CHOICE);
156 fl_set_input(input, "");
157 fl_set_choice_text(choice, default_unit.c_str());
159 ostringstream buffer;
160 buffer << len.value();
161 fl_set_input(input, buffer.str().c_str());
163 // Set the choice to the desired unit, if present in the choice.
164 // Else set the choice to the default unit.
165 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
167 vector<string> const vec = getVector(choice);
168 vector<string>::const_iterator it =
169 std::find(vec.begin(), vec.end(), unit);
170 if (it != vec.end()) {
171 fl_set_choice_text(choice, unit.c_str());
173 fl_set_choice_text(choice, default_unit.c_str());
179 // Take a string and add breaks so that it fits into a desired label width, w
180 string formatted(string const & sin, int w, int size, int style)
183 if (sin.empty()) return sout;
185 string::size_type curpos = 0;
188 string::size_type const nxtpos1 = sin.find(' ', curpos);
189 string::size_type const nxtpos2 = sin.find('\n', curpos);
190 string::size_type const nxtpos = std::min(nxtpos1, nxtpos1);
192 string const word = nxtpos == string::npos ?
193 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
195 bool const newline = (nxtpos2 != string::npos &&
198 string const line_plus_word =
199 line.empty() ? word : line + ' ' + word;
202 fl_get_string_width(style, size,
203 line_plus_word.c_str(),
204 int(line_plus_word.length()));
215 } else if (newline) {
216 sout += line_plus_word + '\n';
225 if (nxtpos == string::npos) {
238 void setCursorColor(int color)
240 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
241 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
242 fl_set_cursor_color(XC_watch, color, FL_WHITE);
243 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
249 // sorted by hand to prevent LyXLex from complaining on read().
250 keyword_item xformTags[] = {
251 { "\\gui_background", FL_COL1 },
252 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
253 { "\\gui_buttonleft", FL_LEFT_BCOL },
254 { "\\gui_buttonright", FL_RIGHT_BCOL },
255 { "\\gui_buttontop", FL_TOP_BCOL },
256 { "\\gui_inactive", FL_INACTIVE },
257 { "\\gui_pointer", FL_FREE_COL16 },
258 { "\\gui_push_button", FL_YELLOW },
259 { "\\gui_selected", FL_MCOL },
260 { "\\gui_text", FL_BLACK }
264 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
269 bool XformsColor::read(string const & filename)
271 LyXLex lexrc(xformTags, xformCount);
272 if (!lexrc.setFile(filename))
275 while (lexrc.isOK()) {
276 int const le = lexrc.lex();
279 case LyXLex::LEX_UNDEF:
280 lexrc.printError("Unknown tag `$$Token'");
282 case LyXLex::LEX_FEOF:
287 string const tag = lexrc.getString();
291 if (!lexrc.next()) break;
292 col.r = lexrc.getInteger();
294 if (!lexrc.next()) break;
295 col.g = lexrc.getInteger();
297 if (!lexrc.next()) break;
298 col.b = lexrc.getInteger();
300 fl_mapcolor(le, col.r, col.g, col.b);
302 if (tag == "\\gui_pointer") {
303 setCursorColor(FL_FREE_COL16);
311 bool XformsColor::write(string const & filename)
313 ofstream os(filename.c_str());
317 os << "### This file is part of\n"
318 << "### ========================================================\n"
319 << "### LyX, The Document Processor\n"
321 << "### Copyright 1995 Matthias Ettrich\n"
322 << "### Copyright 1995-2002 The LyX Team.\n"
324 << "### ========================================================\n"
326 << "# This file is written by LyX, if you want to make your own\n"
327 << "# modifications you should do them from inside LyX and save\n"
330 for (int i = 0; i < xformCount; ++i) {
331 string const tag = xformTags[i].tag;
332 int const colorID = xformTags[i].code;
335 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
338 << color.r << " " << color.g << " " << color.b << "\n";
345 string RWInfo::error_message;
347 bool RWInfo::WriteableDir(string const & name)
349 error_message.erase();
351 if (!AbsolutePath(name)) {
352 error_message = _("The absolute path is required.");
356 FileInfo const tp(name);
357 if (!tp.isOK() || !tp.isDir()) {
358 error_message = _("Directory does not exist.");
362 if (!tp.writable()) {
363 error_message = _("Cannot write to this directory.");
371 bool RWInfo::ReadableDir(string const & name)
373 error_message.erase();
375 if (!AbsolutePath(name)) {
376 error_message = _("The absolute path is required.");
380 FileInfo const tp(name);
381 if (!tp.isOK() || !tp.isDir()) {
382 error_message = _("Directory does not exist.");
386 if (!tp.readable()) {
387 error_message = _("Cannot read this directory.");
395 bool RWInfo::WriteableFile(string const & name)
397 // A writeable file is either:
398 // * An existing file to which we have write access, or
399 // * A file that doesn't yet exist but that would exist in a writeable
402 error_message.erase();
405 error_message = _("No file input.");
409 string const dir = OnlyPath(name);
410 if (!AbsolutePath(dir)) {
411 error_message = _("The absolute path is required.");
417 if (!d.isOK() || !d.isDir()) {
421 if (!d.isOK() || !d.isDir()) {
422 error_message = _("Directory does not exist.");
427 error_message = _("Cannot write to this directory.");
432 if (dir == name || (f.isOK() && f.isDir())) {
433 error_message = _("A file is required, not a directory.");
437 if (f.isOK() && f.exist() && !f.writable()) {
438 error_message = _("Cannot write to this file.");
446 bool RWInfo::ReadableFile(string const & name)
448 error_message.erase();
451 error_message = _("No file input.");
455 string const dir = OnlyPath(name);
456 if (!AbsolutePath(dir)) {
457 error_message = _("The absolute path is required.");
463 if (!d.isOK() && !d.isDir()) {
467 if (!d.isOK() || !d.isDir()) {
468 error_message = _("Directory does not exist.");
473 error_message = _("Cannot read from this directory.");
478 if (dir == name || (f.isOK() && f.isDir())) {
479 error_message = _("A file is required, not a directory.");
484 error_message = _("File does not exist.");
489 error_message = _("Cannot read from this file.");