1 /** Collection of some useful xform helper functions
6 #include FORMS_H_LOCATION
8 #include <fstream> // ofstream
12 #pragma implementation
15 #include "xforms_helpers.h"
17 #include "frontends/FileDialog.h"
18 #include "support/FileInfo.h"
19 #include "support/filetools.h"
20 #include "lyx_gui_misc.h" // WriteAlert
27 // Set an FL_OBJECT to activated or deactivated
28 void setEnabled(FL_OBJECT * ob, bool enable)
31 fl_activate_object(ob);
32 fl_set_object_lcol(ob, FL_BLACK);
34 fl_deactivate_object(ob);
35 fl_set_object_lcol(ob, FL_INACTIVE);
40 // Take a string and add breaks so that it fits into a desired label width, w
41 string formatted(string const & sin, int w, int size, int style)
44 if (sin.empty()) return sout;
46 // break sin up into a vector of individual words
47 vector<string> sentence;
49 for (string::const_iterator sit = sin.begin();
50 sit != sin.end(); ++sit) {
51 if ((*sit) == ' ' || (*sit) == '\n') {
52 sentence.push_back(word);
58 // Flush remaining contents of word
59 if (!word.empty() ) sentence.push_back(word);
62 for (vector<string>::const_iterator vit = sentence.begin();
63 vit != sentence.end(); ++vit) {
64 if (!l1.empty() ) l1 += ' ';
66 int length = fl_get_string_width(style, size, l1.c_str(),
69 if (!sout.empty() ) sout += '\n';
76 // Flush remaining contents of line
78 if (!sout.empty() ) sout += '\n';
86 string const browseFile(LyXView * lv, string const & filename,
88 string const & pattern,
89 pair<string,string> const & dir1,
90 pair<string,string> const & dir2)
92 string lastPath = ".";
93 if (!filename.empty()) lastPath = OnlyPath(filename);
95 FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
97 FileDialog::Result result;
100 result = fileDlg.Select(lastPath, pattern, OnlyFilename(filename));
102 if (result.second.empty())
103 return result.second;
105 lastPath = OnlyPath(result.second);
107 if (result.second.find_first_of("#~$% ") == string::npos)
110 WriteAlert(_("Filename can't contain any "
111 "of these characters:"),
112 _("space, '#', '~', '$' or '%'."));
115 return result.second;
121 // sorted by hand to prevent LyXLex from complaining on read().
122 keyword_item xformTags[] = {
123 { "\\gui_background", FL_COL1 },
124 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
125 { "\\gui_buttonleft", FL_LEFT_BCOL },
126 { "\\gui_buttonright", FL_RIGHT_BCOL },
127 { "\\gui_buttontop", FL_TOP_BCOL },
128 { "\\gui_inactive", FL_INACTIVE },
129 { "\\gui_push_button", FL_YELLOW },
130 { "\\gui_selected", FL_MCOL },
131 { "\\gui_text", FL_BLACK }
135 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
140 bool XformsColor::read(string const & filename)
142 LyXLex lexrc(xformTags, xformCount);
143 if (!lexrc.setFile(filename))
146 while (lexrc.IsOK()) {
147 int const le = lexrc.lex();
150 case LyXLex::LEX_UNDEF:
151 lexrc.printError("Unknown tag `$$Token'");
153 case LyXLex::LEX_FEOF:
160 if (!lexrc.next()) break;
161 col.r = lexrc.GetInteger();
163 if (!lexrc.next()) break;
164 col.g = lexrc.GetInteger();
166 if (!lexrc.next()) break;
167 col.b = lexrc.GetInteger();
169 fl_mapcolor(le, col.r, col.g, col.b);
176 bool XformsColor::write(string const & filename)
178 ofstream os(filename.c_str());
182 os << "### This file is part of\n"
183 << "### ========================================================\n"
184 << "### LyX, The Document Processor\n"
186 << "### Copyright 1995 Matthias Ettrich\n"
187 << "### Copyright 1995-2000 The LyX Team.\n"
189 << "### ========================================================\n"
191 << "# This file is written by LyX, if you want to make your own\n"
192 << "# modifications you should do them from inside LyX and save\n"
195 for (int i = 0; i < xformCount; ++i) {
196 string const tag = xformTags[i].tag;
197 int const colorID = xformTags[i].code;
200 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
203 << color.r << " " << color.g << " " << color.b << "\n";
210 string RWInfo::error_message;
212 bool RWInfo::WriteableDir(string const & name)
214 error_message.erase();
216 if (!AbsolutePath(name)) {
217 error_message = N_("The absolute path is required.");
221 FileInfo const tp(name);
223 error_message = N_("Directory does not exist.");
227 if (!tp.writable()) {
228 error_message = N_("Cannot write to this directory.");
236 bool RWInfo::ReadableDir(string const & name)
238 error_message.erase();
240 if (!AbsolutePath(name)) {
241 error_message = N_("The absolute path is required.");
245 FileInfo const tp(name);
247 error_message = N_("Directory does not exist.");
251 if (!tp.readable()) {
252 error_message = N_("Cannot read this directory.");
260 bool RWInfo::WriteableFile(string const & name)
262 // A writeable file is either:
263 // * An existing file to which we have write access, or
264 // * A file that doesn't yet exist but that would exist in a writeable
267 error_message.erase();
270 error_message = N_("No file input.");
274 string const dir = OnlyPath(name);
275 if (!AbsolutePath(dir)) {
276 error_message = N_("The absolute path is required.");
286 error_message = N_("Directory does not exist.");
291 error_message = N_("Cannot write to this directory.");
296 if (dir == name || f.isDir()) {
297 error_message = N_("A file is required, not a directory.");
301 if (f.exist() && !f.writable()) {
302 error_message = N_("Cannot write to this file.");
310 bool RWInfo::ReadableFile(string const & name)
312 error_message.erase();
315 error_message = N_("No file input.");
319 string const dir = OnlyPath(name);
320 if (!AbsolutePath(dir)) {
321 error_message = N_("The absolute path is required.");
331 error_message = N_("Directory does not exist.");
336 error_message = N_("Cannot read from this directory.");
341 if (dir == name || f.isDir()) {
342 error_message = N_("A file is required, not a directory.");
347 error_message = N_("File does not exist.");
352 error_message = N_("Cannot read from this file.");