]> git.lyx.org Git - lyx.git/blob - src/FuncRequest.cpp
Enable OK/Apply buttons when resetting to class defaults.
[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/debug.h"
17 #include "support/lstrings.h"
18
19 #include <climits>
20 #include <iostream>
21 #include <sstream>
22 #include <vector>
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
29 FuncRequest const FuncRequest::unknown(LFUN_UNKNOWN_ACTION);
30 FuncRequest const FuncRequest::noaction(LFUN_NOACTION);
31
32 FuncRequest::FuncRequest(Origin o)
33         : action_(LFUN_NOACTION), origin_(o), x_(0), y_(0),
34           button_(mouse_button::none)
35 {}
36
37
38 FuncRequest::FuncRequest(FuncCode act, Origin o)
39         : action_(act), origin_(o), x_(0), y_(0),
40         button_(mouse_button::none)
41 {}
42
43
44 FuncRequest::FuncRequest(FuncCode act, docstring const & arg, Origin o)
45         : action_(act), argument_(arg), origin_(o), x_(0), y_(0),
46           button_(mouse_button::none)
47 {}
48
49
50 FuncRequest::FuncRequest(FuncCode act, string const & arg, Origin o)
51         : action_(act), argument_(from_utf8(arg)), origin_(o), x_(0), y_(0),
52           button_(mouse_button::none)
53 {}
54
55
56 FuncRequest::FuncRequest(FuncCode act, int ax, int ay,
57                          mouse_button::state but, Origin o)
58         : action_(act), origin_(o), x_(ax), y_(ay), button_(but)
59 {}
60
61
62 FuncRequest::FuncRequest(FuncRequest const & cmd, docstring const & arg, Origin o)
63         : action_(cmd.action()), argument_(arg), origin_(o),
64           x_(cmd.x_), y_(cmd.y_), button_(cmd.button_)
65 {}
66
67
68 FuncRequest::FuncRequest(FuncRequest const & cmd, string const & arg, Origin o)
69         : action_(cmd.action()), argument_(from_utf8(arg)), origin_(o),
70           x_(cmd.x_), y_(cmd.y_), button_(cmd.button_)
71 {}
72
73
74 namespace {
75
76 // Extracts arguments from str into args. Arguments are delimted by
77 // whitespace or by double quotes.
78 // We extract at most max + 1 arguments, treating args[max] as
79 // continuing to eol.
80 void splitArg(vector<string> & args, string const & str,
81                 unsigned int max = UINT_MAX)
82 {
83         istringstream is(str);
84         while (is) {
85                 if (args.size() == max) {
86                         string s;
87                         getline(is, s);
88                         args.push_back(trim(s));
89                         return;
90                 }
91
92                 char c;
93                 string s;
94                 is >> c;
95                 if (is) {
96                         if (c == '"')
97                                 // get quote delimited argument
98                                 getline(is, s, '"');
99                         else {
100                                 // get whitespace delimited argument
101                                 is.putback(c);
102                                 is >> s;
103                         }
104                         args.push_back(s);
105                 }
106         }
107 }
108
109 }
110
111 string FuncRequest::getArg(unsigned int i) const
112 {
113         vector<string> args;
114         splitArg(args, to_utf8(argument_));
115         return i < args.size() ? args[i] : string();
116 }
117
118
119 string FuncRequest::getLongArg(unsigned int i) const
120 {
121         vector<string> args;
122         splitArg(args, to_utf8(argument_), i);
123         return i < args.size() ? args[i] : string();
124 }
125
126
127 bool operator==(FuncRequest const & lhs, FuncRequest const & rhs)
128 {
129         return lhs.action() == rhs.action() && lhs.argument() == rhs.argument();
130 }
131
132
133 ostream & operator<<(ostream & os, FuncRequest const & cmd)
134 {
135         return os
136                 << " action: " << cmd.action()
137                 << " [" << lyxaction.getActionName(cmd.action()) << "] "
138                 << " arg: '" << to_utf8(cmd.argument()) << "'"
139                 << " x: " << cmd.x()
140                 << " y: " << cmd.y();
141 }
142
143
144 LyXErr & operator<<(LyXErr &l, FuncRequest const &fr)
145 {
146         ostringstream oss;
147         oss << fr;
148         return l << oss.str();
149 }
150
151 } // namespace lyx