]> git.lyx.org Git - lyx.git/blob - src/FuncRequest.cpp
resolve shortcut conflict
[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         : action(act), argument_(arg), 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         : action(act), argument_(from_utf8(arg)), 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         : action(cmd.action), argument_(arg), 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         : action(cmd.action), argument_(from_utf8(arg)), origin(o),
68           x(cmd.x), y(cmd.y), button_(cmd.button_)
69 {}
70
71
72 mouse_button::state FuncRequest::button() const
73 {
74         return button_;
75 }
76
77
78 namespace {
79
80 // Extracts arguments from str into args. Arguments are delimted by
81 // whitespace or by double quotes.
82 // We extract at most max + 1 arguments, treating args[max] as 
83 // continuing to eol.
84 void splitArg(vector<string> & args, string const & str, 
85                 unsigned int max = UINT_MAX)
86 {
87         istringstream is(str);
88         while (is) {
89                 if (args.size() == max) {
90                         string s;
91                         getline(is, s);
92                         args.push_back(trim(s));
93                         return;
94                 }               
95
96                 char c;
97                 string s;
98                 is >> c;
99                 if (is) {
100                         if (c == '"')
101                                 // get quote delimited argument
102                                 getline(is, s, '"');
103                         else {
104                                 // get whitespace delimited argument
105                                 is.putback(c);
106                                 is >> s;
107                         }
108                         args.push_back(s);
109                 }
110         }
111 }
112
113 }
114
115 string FuncRequest::getArg(unsigned int i) const
116 {
117         vector<string> args;
118         splitArg(args, to_utf8(argument_));
119         return i < args.size() ? args[i] : string();
120 }
121
122
123 string FuncRequest::getLongArg(unsigned int i) const
124 {
125         vector<string> args;
126         splitArg(args, to_utf8(argument_), i);
127         return i < args.size() ? args[i] : string();
128 }
129
130
131 bool operator==(FuncRequest const & lhs, FuncRequest const & rhs)
132 {
133         return lhs.action == rhs.action && lhs.argument() == rhs.argument();
134 }
135
136
137 ostream & operator<<(ostream & os, FuncRequest const & cmd)
138 {
139         return os
140                 << " action: " << cmd.action 
141                 << " [" << lyxaction.getActionName(cmd.action) << "] " 
142                 << " arg: '" << to_utf8(cmd.argument()) << "'"
143                 << " x: " << cmd.x
144                 << " y: " << cmd.y;
145 }
146
147
148 } // namespace lyx