summaryrefslogtreecommitdiffstats
path: root/kspread/functions.h
blob: 9cf7faf296852ca686400303982ba81c0fcc539a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* This file is part of the KDE project
   Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
   Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/


#ifndef KSPREAD_FUNCTIONS
#define KSPREAD_FUNCTIONS

#include <tqstringlist.h>
#include <tqvaluevector.h>

class TQDomElement;

namespace KSpread
{
class Sheet;
class Value;
class ValueCalc;

typedef TQValueVector<Value> valVector;

struct rangeInfo {
  int col1, col2, row1, row2;
};
struct FuncExtra {
  // here we'll add all the extras a function may need
  TQValueVector<rangeInfo> ranges;
  Sheet *sheet;
  int myrow, mycol;
};

typedef Value (*FunctionPtr)(valVector, ValueCalc *, FuncExtra *);

class Function
{
public:
  Function (const TQString& name, FunctionPtr ptr);
  ~Function();
  /**
  setParamCount sets allowed parameter count for a function.
  if max=0, it means max=min. If max=-1, there is no upper limit.
  */
  void setParamCount (int min, int max = 0);
  /** is it okay for the function to receive this many parameters ? */
  bool paramCountOkay (int count);
  /** when set to true, the function can receive arrays. When set to
  false, the auto-array mechamism will be used for arrays (so the
  fuction will receive simple values, not arrays). */
  void setAcceptArray (bool accept = true);
  bool needsExtra ();
  void setNeedsExtra (bool extra);
  TQString name() const;
  TQString localizedName() const;
  TQString helpText() const;
  void setHelpText( const TQString& text );
  Value exec (valVector args, ValueCalc *calc, FuncExtra *extra = 0);
  
private:
  class Private;
  Private* d;
};

enum ParameterType { KSpread_Int, KSpread_Float, KSpread_String, KSpread_Boolean, KSpread_Any };

class FunctionParameter
{
public:
  FunctionParameter();
  FunctionParameter( const FunctionParameter& param );
  FunctionParameter( const TQDomElement& element );
  
  TQString helpText() const { return m_help; }
  ParameterType type() const { return m_type; }
  bool hasRange() const { return m_range; }
    
private:
  TQString m_help;
  ParameterType m_type;
  bool m_range;
};

class FunctionDescription
{
public:
  FunctionDescription();
  FunctionDescription (const TQDomElement& element);
  FunctionDescription (const FunctionDescription& desc);
  
  const TQStringList& examples() { return m_examples; }
  const TQStringList& syntax() { return m_syntax; }
  const TQStringList& related() { return m_related; }
  const TQStringList& helpText() const { return m_help; }
  TQString name() const { return m_name; }
  ParameterType type() const { return m_type; }
  
  int params() const { return m_params.count(); }
  FunctionParameter& param( int i ) { return m_params[ i ]; }
  
  void setGroup( const TQString& g ) { m_group = g; }
  TQString group() const { return m_group; }
  
  TQString toTQML() const;
    
private:
  TQString m_group;
  TQStringList m_examples;
  TQStringList m_syntax;
  TQStringList m_related;
  TQStringList m_help;
  TQString m_name;
  ParameterType m_type;
  TQValueList<FunctionParameter> m_params;
};

class FunctionRepository
{
public:
  FunctionRepository();
  ~FunctionRepository();
    
  static FunctionRepository *self();
    
  void add (Function *function);
  
  Function *function (const TQString& name);

  FunctionDescription *functionInfo (const TQString& name);

  /** return functions within a group, or all if no group given */
  TQStringList functionNames (const TQString& group = TQString());
    
  const TQStringList &groups () const { return m_groups; }
    
private:

  class Private;
  Private* d;

  static FunctionRepository* s_self;

  /** loads function descriptions from a XML file */
  void loadFile (const TQString& filename);
  
  TQStringList m_groups;
  
  // no copy or assign
  FunctionRepository( const FunctionRepository& );
  FunctionRepository& operator=( const FunctionRepository& );
};


} // namespace KSpread


#endif // KSPREAD_FUNCTIONS