]> git.lyx.org Git - lyx.git/blob - src/FuncRequest.cpp
* docstream: factorize out some code and introduce odocfstream::reset()
[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
15 #include <iostream>
16 #include <sstream>
17 #include <vector>
18
19 using std::getline;
20 using std::istringstream;
21 using std::vector;
22 using std::string;
23
24
25 namespace lyx {
26
27 FuncRequest const FuncRequest::unknown(LFUN_UNKNOWN_ACTION);
28 FuncRequest const FuncRequest::noaction(LFUN_NOACTION);
29
30 FuncRequest::FuncRequest(Origin o)
31         : action(LFUN_NOACTION), origin(o), x(0), y(0),
32           button_(mouse_button::none)
33 {}
34
35
36 FuncRequest::FuncRequest(kb_action act, Origin o)
37         : action(act), origin(o), x(0), y(0), button_(mouse_button::none)
38 {}
39
40
41 FuncRequest::FuncRequest(kb_action act, docstring const & arg, Origin o)
42         : action(act), argument_(arg), origin(o), x(0), y(0),
43           button_(mouse_button::none)
44 {}
45
46
47 FuncRequest::FuncRequest(kb_action act, string const & arg, Origin o)
48         : action(act), argument_(from_utf8(arg)), origin(o), x(0), y(0),
49           button_(mouse_button::none)
50 {}
51
52
53 FuncRequest::FuncRequest(kb_action act, int ax, int ay,
54                          mouse_button::state but, Origin o)
55         : action(act), origin(o), x(ax), y(ay), button_(but)
56 {}
57
58
59 FuncRequest::FuncRequest(FuncRequest const & cmd, docstring const & arg, Origin o)
60         : action(cmd.action), argument_(arg), origin(o),
61           x(cmd.x), y(cmd.y), button_(cmd.button_)
62 {}
63
64
65 FuncRequest::FuncRequest(FuncRequest const & cmd, string const & arg, Origin o)
66         : action(cmd.action), argument_(from_utf8(arg)), origin(o),
67           x(cmd.x), y(cmd.y), button_(cmd.button_)
68 {}
69
70
71 mouse_button::state FuncRequest::button() const
72 {
73         return button_;
74 }
75
76
77 void split(vector<string> & args, string const & str)
78 {
79         istringstream is(str);
80         while (is) {
81                 char c;
82                 string s;
83                 is >> c;
84                 if (is) {
85                         if (c == '"')
86                                 getline(is, s, '"');
87                         else {
88                                 is.putback(c);
89                                 is >> s;
90                         }
91                         args.push_back(s);
92                 }
93         }
94 }
95
96
97 string FuncRequest::getArg(unsigned int i) const
98 {
99         vector<string> args;
100         split(args, to_utf8(argument_));
101         return i < args.size() ? args[i] : string();
102 }
103
104
105 bool operator==(FuncRequest const & lhs, FuncRequest const & rhs)
106 {
107         return lhs.action == rhs.action && lhs.argument() == rhs.argument();
108 }
109
110
111 std::ostream & operator<<(std::ostream & os, FuncRequest const & cmd)
112 {
113         return os
114                 << " action: " << cmd.action
115                 << " arg: '" << to_utf8(cmd.argument()) << "'"
116                 << " x: " << cmd.x
117                 << " y: " << cmd.y;
118 }
119
120
121 } // namespace lyx