]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/xform_helpers.C
Angus latest patch + some tweaks
[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
15 using std::vector;
16
17 // Take a string and add breaks so that it fits into a desired label width, w
18 string formatted( string const & sin, int w, int size, int style )
19 {
20         string sout;
21         if( sin.empty() ) return sout;
22
23         // break sin up into a vector of individual words
24         vector<string> sentence;
25         string word;
26         for( string::const_iterator sit = sin.begin();
27              sit != sin.end(); ++sit ) {
28                 if( (*sit) == ' ' || (*sit) == '\n') {
29                         sentence.push_back(word);
30                         word.erase();
31                 } else {
32                         word += (*sit);
33                 }
34         }
35         // Flush remaining contents of word
36         if( !word.empty() ) sentence.push_back(word);
37
38         string line, l1;
39         for( vector<string>::const_iterator vit = sentence.begin();
40              vit != sentence.end(); ++vit ) {
41                 if( !l1.empty() ) l1 += ' ';
42                 l1 += (*vit);
43                 int length = fl_get_string_width(style, size, l1.c_str(),
44                                                  int(l1.length()));
45                 if( length >= w ) {
46                         if( !sout.empty() ) sout += '\n';
47                         sout += line;
48                         l1 = (*vit);
49                 }
50
51                 line = l1;
52         }
53         // Flush remaining contents of line
54         if( !line.empty() ) {
55                 if( !sout.empty() ) sout += '\n';
56                 sout += line;
57         }
58         
59         return sout;
60 }