2 * \file xforms_helpers.C
3 * Copyright 2000-2002 The LyX Team.
4 * See the file COPYING.
6 * \author Angus Leeming, a.leeming@ic.ac.uk
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
35 // Extract shortcut from <ident>|<shortcut> string
36 char const * flyx_shortcut_extract(char const * sc)
38 // Find '|' in the sc and return the string after that.
39 register char const * sd = sc;
40 while (sd[0]!= 0 && sd[0] != '|') ++sd;
50 // Extract identifier from <ident>|<shortcut> string
51 char const * flyx_ident_extract(char const * sc)
53 register char const * se = sc;
54 while (se[0]!= 0 && se[0] != '|') ++se;
56 if (se[0] == 0) return sc;
58 char * sb = new char[se - sc + 1];
60 register char const * sd = sc;
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_BLACK);
77 fl_deactivate_object(ob);
78 fl_set_object_lcol(ob, FL_INACTIVE);
83 // Given an fl_choice, create a vector of its entries
84 vector<string> const getVectorFromChoice(FL_OBJECT * ob)
87 if (!ob || ob->objclass != FL_CHOICE)
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(strip(frontStrip(text)));
99 /// Given an fl_input, return its contents.
100 string const getStringFromInput(FL_OBJECT * ob)
102 if (!ob || ob->objclass != FL_INPUT)
105 char const * tmp = fl_get_input(ob);
106 return (tmp) ? tmp : string();
110 // Given an fl_browser, return the contents of line
111 string const getStringFromBrowser(FL_OBJECT * ob, int line)
113 if (!ob || ob->objclass != FL_BROWSER ||
114 line < 1 || line > fl_get_browser_maxline(ob))
117 char const * tmp = fl_get_browser_line(ob, line);
118 return (tmp) ? tmp : string();
121 // Given an fl_browser, return the contents of the currently
123 // If nothing is selected, return an empty string
124 string const getSelectedStringFromBrowser(FL_OBJECT * ob)
126 if (!ob || ob->objclass != FL_BROWSER)
129 int const line = fl_get_browser(ob);
130 if (line < 1 || line > fl_get_browser_maxline(ob))
133 if (!fl_isselected_browser_line(ob, line))
136 char const * tmp = fl_get_browser_line(ob, line);
137 return (tmp) ? tmp : string();
141 // Given an fl_browser, create a vector of its entries
142 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
145 if (!ob || ob->objclass != FL_BROWSER)
148 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
149 string const text = fl_get_browser_line(ob, i+1);
150 vec.push_back(strip(frontStrip(text)));
157 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
160 lyx::Assert(input && input->objclass == FL_INPUT &&
161 choice && choice->objclass == FL_CHOICE);
163 string const length = strip(frontStrip(fl_get_input(input)));
167 //don't return unit-from-choice if the input(field) contains a unit
168 if (isValidGlueLength(length))
171 string unit = strip(frontStrip(fl_get_choice_text(choice)));
172 unit = subst(unit, "%%", "%");
174 return length + unit;
178 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
180 string const & default_unit)
182 // use input field only for gluelengths
183 if (!isValidLength(str) && !isStrDbl(str)) {
184 fl_set_input(input, str.c_str());
185 // we assume that "default_unit" is in the choice as "we"
186 // have control over that!
187 // No need to check for it's precence in the choice, therefore.
188 fl_set_choice_text(choice, default_unit.c_str());
190 updateWidgetsFromLength(input, choice,
191 LyXLength(str), default_unit);
196 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
197 LyXLength const & len,
198 string const & default_unit)
201 lyx::Assert(input && input->objclass == FL_INPUT &&
202 choice && choice->objclass == FL_CHOICE);
205 fl_set_input(input, "");
206 fl_set_choice_text(choice, default_unit.c_str());
208 ostringstream buffer;
209 buffer << len.value();
210 fl_set_input(input, buffer.str().c_str());
212 // Set the choice to the desired unit, if present in the choice.
213 // Else set the choice to the default unit.
214 string const unit = subst(stringFromUnit(len.unit()),"%","%%");
216 vector<string> const vec = getVectorFromChoice(choice);
217 vector<string>::const_iterator it =
218 std::find(vec.begin(), vec.end(), unit);
219 if (it != vec.end()) {
220 fl_set_choice_text(choice, unit.c_str());
222 fl_set_choice_text(choice, default_unit.c_str());
228 // Take a string and add breaks so that it fits into a desired label width, w
229 string formatted(string const & sin, int w, int size, int style)
232 if (sin.empty()) return sout;
234 string::size_type curpos = 0;
237 string::size_type const nxtpos1 = sin.find(' ', curpos);
238 string::size_type const nxtpos2 = sin.find('\n', curpos);
239 string::size_type const nxtpos = std::min(nxtpos1, nxtpos1);
241 string const word = nxtpos == string::npos ?
242 sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
244 bool const newline = (nxtpos2 != string::npos &&
247 string const line_plus_word =
248 line.empty() ? word : line + ' ' + word;
251 fl_get_string_width(style, size,
252 line_plus_word.c_str(),
253 int(line_plus_word.length()));
264 } else if (newline) {
265 sout += line_plus_word + '\n';
274 if (nxtpos == string::npos) {
287 void setCursorColor(int color)
289 fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
290 fl_set_cursor_color(XC_xterm, color, FL_WHITE);
291 fl_set_cursor_color(XC_watch, color, FL_WHITE);
292 fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
298 // sorted by hand to prevent LyXLex from complaining on read().
299 keyword_item xformTags[] = {
300 { "\\gui_background", FL_COL1 },
301 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
302 { "\\gui_buttonleft", FL_LEFT_BCOL },
303 { "\\gui_buttonright", FL_RIGHT_BCOL },
304 { "\\gui_buttontop", FL_TOP_BCOL },
305 { "\\gui_inactive", FL_INACTIVE },
306 { "\\gui_pointer", FL_FREE_COL16 },
307 { "\\gui_push_button", FL_YELLOW },
308 { "\\gui_selected", FL_MCOL },
309 { "\\gui_text", FL_BLACK }
313 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
318 bool XformsColor::read(string const & filename)
320 LyXLex lexrc(xformTags, xformCount);
321 if (!lexrc.setFile(filename))
324 while (lexrc.isOK()) {
325 int const le = lexrc.lex();
328 case LyXLex::LEX_UNDEF:
329 lexrc.printError("Unknown tag `$$Token'");
331 case LyXLex::LEX_FEOF:
336 string const tag = lexrc.getString();
340 if (!lexrc.next()) break;
341 col.r = lexrc.getInteger();
343 if (!lexrc.next()) break;
344 col.g = lexrc.getInteger();
346 if (!lexrc.next()) break;
347 col.b = lexrc.getInteger();
349 fl_mapcolor(le, col.r, col.g, col.b);
351 if (tag == "\\gui_pointer") {
352 setCursorColor(FL_FREE_COL16);
360 bool XformsColor::write(string const & filename)
362 ofstream os(filename.c_str());
366 os << "### This file is part of\n"
367 << "### ========================================================\n"
368 << "### LyX, The Document Processor\n"
370 << "### Copyright 1995 Matthias Ettrich\n"
371 << "### Copyright 1995-2002 The LyX Team.\n"
373 << "### ========================================================\n"
375 << "# This file is written by LyX, if you want to make your own\n"
376 << "# modifications you should do them from inside LyX and save\n"
379 for (int i = 0; i < xformCount; ++i) {
380 string const tag = xformTags[i].tag;
381 int const colorID = xformTags[i].code;
384 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
387 << color.r << " " << color.g << " " << color.b << "\n";
394 string RWInfo::error_message;
396 bool RWInfo::WriteableDir(string const & name)
398 error_message.erase();
400 if (!AbsolutePath(name)) {
401 error_message = _("The absolute path is required.");
405 FileInfo const tp(name);
406 if (!tp.isOK() || !tp.isDir()) {
407 error_message = _("Directory does not exist.");
411 if (!tp.writable()) {
412 error_message = _("Cannot write to this directory.");
420 bool RWInfo::ReadableDir(string const & name)
422 error_message.erase();
424 if (!AbsolutePath(name)) {
425 error_message = _("The absolute path is required.");
429 FileInfo const tp(name);
430 if (!tp.isOK() || !tp.isDir()) {
431 error_message = _("Directory does not exist.");
435 if (!tp.readable()) {
436 error_message = _("Cannot read this directory.");
444 bool RWInfo::WriteableFile(string const & name)
446 // A writeable file is either:
447 // * An existing file to which we have write access, or
448 // * A file that doesn't yet exist but that would exist in a writeable
451 error_message.erase();
454 error_message = _("No file input.");
458 string const dir = OnlyPath(name);
459 if (!AbsolutePath(dir)) {
460 error_message = _("The absolute path is required.");
466 if (!d.isOK() || !d.isDir()) {
470 if (!d.isOK() || !d.isDir()) {
471 error_message = _("Directory does not exist.");
476 error_message = _("Cannot write to this directory.");
481 if (dir == name || (f.isOK() && f.isDir())) {
482 error_message = _("A file is required, not a directory.");
486 if (f.isOK() && f.exist() && !f.writable()) {
487 error_message = _("Cannot write to this file.");
495 bool RWInfo::ReadableFile(string const & name)
497 error_message.erase();
500 error_message = _("No file input.");
504 string const dir = OnlyPath(name);
505 if (!AbsolutePath(dir)) {
506 error_message = _("The absolute path is required.");
512 if (!d.isOK() && !d.isDir()) {
516 if (!d.isOK() || !d.isDir()) {
517 error_message = _("Directory does not exist.");
522 error_message = _("Cannot read from this directory.");
527 if (dir == name || (f.isOK() && f.isDir())) {
528 error_message = _("A file is required, not a directory.");
533 error_message = _("File does not exist.");
538 error_message = _("Cannot read from this file.");