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