]> git.lyx.org Git - lyx.git/blob - src/FuncRequest.cpp
First version of separate translation machinery for strings that go into
[lyx.git] / src / FuncRequest.cpp
1 /**
2  * \file FuncRequest.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "FuncRequest.h"
14 #include "LyXAction.h"
15
16 #include "support/lstrings.h"
17
18 #include <climits>
19 #include <iostream>
20 #include <sstream>
21 #include <vector>
22
23 using namespace std;
24 using namespace lyx::support;
25
26 namespace lyx {
27
28 FuncRequest const FuncRequest::unknown(LFUN_UNKNOWN_ACTION);
29 FuncRequest const FuncRequest::noaction(LFUN_NOACTION);
30
31 FuncRequest::FuncRequest(Origin o)
32         : action_(LFUN_NOACTION), origin_(o), x_(0), y_(0),
33           button_(mouse_button::none)
34 {}
35
36
37 FuncRequest::FuncRequest(FuncCode act, Origin o)
38         : action_(act), origin_(o), x_(0), y_(0),
39         button_(mouse_button::none)
40 {}
41
42
43 FuncRequest::FuncRequest(FuncCode act, docstring const & arg, Origin o)
44         : action_(act), argument_(arg), origin_(o), x_(0), y_(0),
45           button_(mouse_button::none)
46 {}
47
48
49 FuncRequest::FuncRequest(FuncCode act, string const & arg, Origin o)
50         : action_(act), argument_(from_utf8(arg)), origin_(o), x_(0), y_(0),
51           button_(mouse_button::none)
52 {}
53
54
55 FuncRequest::FuncRequest(FuncCode act, int ax, int ay,
56                          mouse_button::state but, Origin o)
57         : action_(act), origin_(o), x_(ax), y_(ay), button_(but)
58 {}
59
60
61 FuncRequest::FuncRequest(FuncRequest const & cmd, docstring const & arg, Origin o)
62         : action_(cmd.action()), argument_(arg), origin_(o),
63           x_(cmd.x_), y_(cmd.y_), button_(cmd.button_)
64 {}
65
66
67 FuncRequest::FuncRequest(FuncRequest const & cmd, string const & arg, Origin o)
68         : action_(cmd.action()), argument_(from_utf8(arg)), origin_(o),
69           x_(cmd.x_), y_(cmd.y_), button_(cmd.button_)
70 {}
71
72
73 namespace {
74
75 // Extracts arguments from str into args. Arguments are delimted by
76 // whitespace or by double quotes.
77 // We extract at most max + 1 arguments, treating args[max] as
78 // continuing to eol.
79 void splitArg(vector<string> & args, string const & str,
80                 unsigned int max = UINT_MAX)
81 {
82         istringstream is(str);
83         while (is) {
84                 if (args.size() == max) {
85                         string s;
86                         getline(is, s);
87                         args.push_back(trim(s));
88                         return;
89                 }
90
91                 char c;
92                 string s;
93                 is >> c;
94                 if (is) {
95                         if (c == '"')
96                                 // get quote delimited argument
97                                 getline(is, s, '"');
98                         else {
99                                 // get whitespace delimited argument
100                                 is.putback(c);
101                                 is >> s;
102                         }
103                         args.push_back(s);
104                 }
105         }
106 }
107
108 }
109
110 string FuncRequest::getArg(unsigned int i) const
111 {
112         vector<string> args;
113         splitArg(args, to_utf8(argument_));
114         return i < args.size() ? args[i] : string();
115 }
116
117
118 string FuncRequest::getLongArg(unsigned int i) const
119 {
120         vector<string> args;
121         splitArg(args, to_utf8(argument_), i);
122         return i < args.size() ? args[i] : string();
123 }
124
125
126 bool operator==(FuncRequest const & lhs, FuncRequest const & rhs)
127 {
128         return lhs.action() == rhs.action() && lhs.argument() == rhs.argument();
129 }
130
131
132 ostream & operator<<(ostream & os, FuncRequest const & cmd)
133 {
134         return os
135                 << " action: " << cmd.action() 
136                 << " [" << lyxaction.getActionName(cmd.action()) << "] " 
137                 << " arg: '" << to_utf8(cmd.argument()) << "'"
138                 << " x: " << cmd.x()
139                 << " y: " << cmd.y();
140 }
141
142
143 } // namespace lyx