]> git.lyx.org Git - lyx.git/blob - src/FuncRequest.cpp
03ff0c833891e95dd734d1f27c267e96a5abd193
[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), button_(mouse_button::none)
39 {}
40
41
42 FuncRequest::FuncRequest(FuncCode act, docstring const & arg, Origin o)
43         : argument_(arg), action_(act), origin_(o), x_(0), y_(0),
44           button_(mouse_button::none)
45 {}
46
47
48 FuncRequest::FuncRequest(FuncCode act, string const & arg, Origin o)
49         : argument_(from_utf8(arg)), action_(act), origin_(o), x_(0), y_(0),
50           button_(mouse_button::none)
51 {}
52
53
54 FuncRequest::FuncRequest(FuncCode act, int ax, int ay,
55                          mouse_button::state but, Origin o)
56         : action_(act), origin_(o), x_(ax), y_(ay), button_(but)
57 {}
58
59
60 FuncRequest::FuncRequest(FuncRequest const & cmd, docstring const & arg, Origin o)
61         : argument_(arg), action_(cmd.action()), origin_(o),
62           x_(cmd.x_), y_(cmd.y_), button_(cmd.button_)
63 {}
64
65
66 FuncRequest::FuncRequest(FuncRequest const & cmd, string const & arg, Origin o)
67         : argument_(from_utf8(arg)), action_(cmd.action()), origin_(o),
68           x_(cmd.x_), y_(cmd.y_), button_(cmd.button_)
69 {}
70
71
72 namespace {
73
74 // Extracts arguments from str into args. Arguments are delimted by
75 // whitespace or by double quotes.
76 // We extract at most max + 1 arguments, treating args[max] as 
77 // continuing to eol.
78 void splitArg(vector<string> & args, string const & str, 
79                 unsigned int max = UINT_MAX)
80 {
81         istringstream is(str);
82         while (is) {
83                 if (args.size() == max) {
84                         string s;
85                         getline(is, s);
86                         args.push_back(trim(s));
87                         return;
88                 }               
89
90                 char c;
91                 string s;
92                 is >> c;
93                 if (is) {
94                         if (c == '"')
95                                 // get quote delimited argument
96                                 getline(is, s, '"');
97                         else {
98                                 // get whitespace delimited argument
99                                 is.putback(c);
100                                 is >> s;
101                         }
102                         args.push_back(s);
103                 }
104         }
105 }
106
107 }
108
109 string FuncRequest::getArg(unsigned int i) const
110 {
111         vector<string> args;
112         splitArg(args, to_utf8(argument_));
113         return i < args.size() ? args[i] : string();
114 }
115
116
117 string FuncRequest::getLongArg(unsigned int i) const
118 {
119         vector<string> args;
120         splitArg(args, to_utf8(argument_), i);
121         return i < args.size() ? args[i] : string();
122 }
123
124
125 bool operator==(FuncRequest const & lhs, FuncRequest const & rhs)
126 {
127         return lhs.action() == rhs.action() && lhs.argument() == rhs.argument();
128 }
129
130
131 ostream & operator<<(ostream & os, FuncRequest const & cmd)
132 {
133         return os
134                 << " action: " << cmd.action() 
135                 << " [" << lyxaction.getActionName(cmd.action()) << "] " 
136                 << " arg: '" << to_utf8(cmd.argument()) << "'"
137                 << " x: " << cmd.x()
138                 << " y: " << cmd.y();
139 }
140
141
142 } // namespace lyx