]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xform_helpers.C
compiling not tested. two patches from angus with some small fixes
[lyx.git] / src / frontends / xforms / xform_helpers.C
1 /** Collection of some useful xform helper functions
2  */
3
4 #include <config.h>
5
6 #include FORMS_H_LOCATION
7
8 #ifdef __GNUG_
9 #pragma implementation
10 #endif
11
12 #include <vector>
13 #include "xform_helpers.h"
14 #include <fstream> // ofstream
15 #include "lyxlex.h"
16 #include "filedlg.h" // LyXFileDlg
17 #include "support/FileInfo.h"
18 #include "support/filetools.h"
19 #include "lyx_gui_misc.h" // WriteAlert
20 #include "gettext.h"
21
22 using std::ofstream;
23 using std::pair;
24 using std::vector;
25
26 // Take a string and add breaks so that it fits into a desired label width, w
27 string formatted( string const & sin, int w, int size, int style )
28 {
29         string sout;
30         if (sin.empty() ) return sout;
31
32         // break sin up into a vector of individual words
33         vector<string> sentence;
34         string word;
35         for (string::const_iterator sit = sin.begin();
36              sit != sin.end(); ++sit) {
37                 if ((*sit) == ' ' || (*sit) == '\n') {
38                         sentence.push_back(word);
39                         word.erase();
40                 } else {
41                         word += (*sit);
42                 }
43         }
44         // Flush remaining contents of word
45         if (!word.empty() ) sentence.push_back(word);
46
47         string line, l1;
48         for (vector<string>::const_iterator vit = sentence.begin();
49              vit != sentence.end(); ++vit) {
50                 if (!l1.empty() ) l1 += ' ';
51                 l1 += (*vit);
52                 int length = fl_get_string_width(style, size, l1.c_str(),
53                                                  int(l1.length()));
54                 if (length >= w) {
55                         if (!sout.empty() ) sout += '\n';
56                         sout += line;
57                         l1 = (*vit);
58                 }
59
60                 line = l1;
61         }
62         // Flush remaining contents of line
63         if (!line.empty()) {
64                 if (!sout.empty() ) sout += '\n';
65                 sout += line;
66         }
67         
68         return sout;
69 }
70
71
72 string const browseFile( string const & filename,
73                          string const & title,
74                          string const & pattern, 
75                          pair<string,string> const & dir1,
76                          pair<string,string> const & dir2 )
77 {
78         string lastPath = ".";
79         if( !filename.empty() ) lastPath = OnlyPath(filename);
80
81         LyXFileDlg fileDlg;
82
83         if( !dir1.second.empty() ) {
84                 FileInfo fileInfo( dir1.second );
85                 if( fileInfo.isOK() && fileInfo.isDir() )
86                         fileDlg.SetButton( 0, dir1.first, dir1.second );
87         }
88
89         if( !dir2.second.empty() ) {
90                 FileInfo fileInfo( dir2.second );
91                 if( fileInfo.isOK() && fileInfo.isDir() )
92                     fileDlg.SetButton( 1, dir2.first, dir2.second );
93         }
94
95         bool error = false;
96         string buf;
97         do {
98                 string p = fileDlg.Select(title,
99                                           lastPath,
100                                           pattern, filename );
101
102                 if (p.empty()) return p;
103
104                 lastPath = OnlyPath(p);
105
106                 if (p.find_first_of("#~$% ") != string::npos) {
107                         WriteAlert(_("Filename can't contain any "
108                                      "of these characters:"),
109                                    _("space, '#', '~', '$' or '%'."));
110                         error = true;
111                 } else {
112                         error = false;
113                         buf = p;
114                 }
115         } while (error);
116
117         return buf;
118 }
119
120
121 // sorted by hand to prevent LyXLex from complaining on read().
122 static
123 keyword_item xformTags[] = {
124 //      { "\\gui_active_tab", FL_LIGHTER_COL1 },
125         { "\\gui_background", FL_COL1 },
126         { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
127         { "\\gui_buttonleft", FL_LEFT_BCOL },
128         { "\\gui_buttonright", FL_RIGHT_BCOL },
129         { "\\gui_buttontop", FL_TOP_BCOL },
130         { "\\gui_inactive", FL_INACTIVE },
131         { "\\gui_push_button", FL_YELLOW },
132         { "\\gui_selected", FL_MCOL },  
133         { "\\gui_text", FL_BLACK }
134 };
135
136
137 static const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
138
139
140 bool XformColor::read(string const & filename)
141 {
142         LyXLex lexrc(xformTags, xformCount);
143         if (!lexrc.setFile(filename))
144                 return false;
145
146         while (lexrc.IsOK()) {
147                 int const le = lexrc.lex();
148
149                 switch (le) {
150                 case LyXLex::LEX_UNDEF:
151                         lexrc.printError("Unknown tag `$$Token'");
152                         continue; 
153                 case LyXLex::LEX_FEOF:
154                         continue;
155                 default: break;
156                 }
157
158                 RGBColor col;
159
160                 if (!lexrc.next()) break;
161                 col.r = lexrc.GetInteger();
162
163                 if (!lexrc.next()) break;
164                 col.g = lexrc.GetInteger();
165
166                 if (!lexrc.next()) break;
167                 col.b = lexrc.GetInteger();
168
169                 fl_mapcolor(le, col.r, col.g, col.b);
170         }
171         
172         return true;
173 }
174
175
176 bool XformColor::write(string const & filename)
177 {
178         ofstream os(filename.c_str());
179         if (!os)
180                 return false;
181
182         os << "### This file is part of\n"
183            << "### ========================================================\n"
184            << "###          LyX, The Document Processor\n"
185            << "###\n"
186            << "###          Copyright 1995 Matthias Ettrich\n"
187            << "###          Copyright 1995-2000 The LyX Team.\n"
188            << "###\n"
189            << "### ========================================================\n"
190            << "\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"
193            << "\n";
194
195         for (int i = 0; i < xformCount; ++i) {
196                 string const tag  = xformTags[i].tag;
197                 int const colorID = xformTags[i].code;
198                 RGBColor color;
199
200                 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
201
202                 os << tag << " "
203                    << color.r << " " << color.g << " " << color.b << "\n";
204         }
205
206         return true;
207 }