1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 /* AbiSource Program Utilities
3  * Copyright (C) 1998 AbiSource, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301 USA.
19  */
20 
21 #ifndef EV_EDITMETHOD_H
22 #define EV_EDITMETHOD_H
23 
24 #include "ut_types.h"
25 #include "ut_vector.h"
26 #include "ut_string_class.h"
27 
28 class AV_View;
29 
30 /*****************************************************************
31 ******************************************************************
32 ** The EditMethod mechanism provides essentially a 'call-by-name'
33 ** capability.  A key, mouse, menu, or toolbar event may be bound
34 ** to a 'named' function (of type EV_EditMethod_Fn).
35 **
36 ** EV_EditMethodType provides some crude type-checking (primarily
37 ** this is used by the invoker to check if the function requires
38 ** data ('insertData' can be bound to a key like 'a' but would have
39 ** problems if bound to 'mouse-1-click').
40 **
41 ** All EditMethod functions have a fixed calling sequence.  An
42 ** instance of EV_EditMethodCallData passed to the function is
43 ** used to provide all of the actual data that the function needs.
44 **
45 ** A EV_EditMethod defines a "named function" and consists of the
46 ** name, a pointer to the actual code, type-checking information,
47 ** and a short description of the function.  [I debated whether it
48 ** would be better to put all of these fields as static members of
49 ** a class consisting of just the static function -- I'm not sure
50 ** it matters one way or another.  Perhaps the current way will be
51 ** a little easier when it comes time to localize the short
52 ** descriptions....]
53 **
54 ** A EV_EditMethodContainer is just that, a container for the
55 ** EV_EditMethod's.  It provides simple lookup capability.  It
56 ** currently has 2 tables of EditMethod's -- a static one (the set
57 ** of our builtin functions) and a dynamic one (intended for use
58 ** by JavaScript later).  The container provides the entire set
59 ** of defined functions -- if a function isn't listed in the
60 ** container, it cannot be found or bound to.
61 **
62 ** The actual tables of defined functions are defined by the
63 ** application and not in this library.
64 ******************************************************************
65 *****************************************************************/
66 
67 /*****************************************************************/
68 /*****************************************************************/
69 
70 typedef UT_uint32 EV_EditMethodType;
71 #define EV_EMT_REQUIREDATA		(static_cast<EV_EditMethodType>(0x1))
72 #define EV_EMT_REQUIRE_SCRIPT_NAME		(static_cast<EV_EditMethodType>(0x2))
73 /* define a method that can be called without a frame. */
74 #define EV_EMT_APP_METHOD		(static_cast<EV_EditMethodType>(0x4))
75 
76 /*****************************************************************/
77 /*****************************************************************/
78 
79 class ABI_EXPORT EV_EditMethodCallData
80 {
81 public:
82 	EV_EditMethodCallData();
83 	EV_EditMethodCallData(const UT_UCSChar * pData, UT_uint32 dataLength);
84 	EV_EditMethodCallData(const char * pChar, UT_uint32 dataLength);
85 	EV_EditMethodCallData(const UT_String& stScriptName);
86 	~EV_EditMethodCallData();
87 
getX()88 	UT_sint32			getX() { return m_xPos; }
getY()89 	UT_sint32			getY() { return m_yPos; }
getScriptName()90 	const UT_String&	getScriptName() { return m_stScriptName; }
91 
92 // private:
93 	UT_UCSChar *		m_pData;
94 	UT_uint32			m_dataLength;
95 	bool				m_bAllocatedData;
96 	UT_sint32			m_xPos;
97 	UT_sint32			m_yPos;
98 
99 private:
100 	UT_String			m_stScriptName;
101 };
102 
103 typedef bool (*EV_EditMethod_pCtxtFn)(AV_View * pView, EV_EditMethodCallData * pCallData,
104 									  void * context);
105 typedef bool ( EV_EditMethod_CtxtFn) (AV_View * pView, EV_EditMethodCallData * pCallData,
106 									  void * context);
107 
108 typedef bool (*EV_EditMethod_pFn)(AV_View * pView, EV_EditMethodCallData * pCallData);
109 typedef bool ( EV_EditMethod_Fn) (AV_View * pView, EV_EditMethodCallData * pCallData);
110 
111 /*****************************************************************/
112 /*****************************************************************/
113 
114 class ABI_EXPORT EV_EditMethod
115 {
116 public:
117 	EV_EditMethod(const char * szName, EV_EditMethod_pFn fn, EV_EditMethodType emt,
118 				  const char * szDescription);
119 	EV_EditMethod(const char * szName, EV_EditMethod_pCtxtFn fn, EV_EditMethodType emt,
120 				  const char * szDescription, void * context);
121 
122 	bool 					Fn(AV_View * pView, EV_EditMethodCallData * pCallData) const;
123 
124 	EV_EditMethodType		getType() const;
125 	const char *			getName() const;
126 	const char *			getDescription() const;
127 
128 protected:
129 	const char *			m_szName; // used for lookup; not malloced; should not be localized
130 	EV_EditMethod_pFn		m_fn;
131 	EV_EditMethod_pCtxtFn	m_CtxtFn;
132 	EV_EditMethodType		m_emt;
133 	const char *			m_szDescription; // not malloced; this can be localized
134 	void *					m_context;
135 };
136 
137 /*****************************************************************/
138 /*****************************************************************/
139 
140 class ABI_EXPORT EV_EditMethodContainer
141 {
142 public:
143 	EV_EditMethodContainer(UT_uint32 cStatic, EV_EditMethod arrayStaticEditMethods[]);
144 	~EV_EditMethodContainer();
145 
146 	bool				addEditMethod(EV_EditMethod * pem);
147 	bool removeEditMethod(EV_EditMethod * pem);
148 	UT_uint32			countEditMethods();
149 	EV_EditMethod *		getNthEditMethod(UT_uint32 ndx);
150 	EV_EditMethod *		findEditMethodByName(const char * szName) const;
151 
152 protected:
153 	UT_uint32			m_countStatic;
154 	EV_EditMethod *		m_arrayStaticEditMethods;		// not malloced
155 	UT_GenericVector<EV_EditMethod *>	m_vecDynamicEditMethods;
156 };
157 
158 /*****************************************************************/
159 /*****************************************************************/
160 
161 ABI_EXPORT bool ev_EditMethod_exists (const char * methodName);
162 ABI_EXPORT bool ev_EditMethod_exists (const UT_String & methodName);
163 
164 /*****************************************************************/
165 /*****************************************************************/
166 
167 ABI_EXPORT bool ev_EditMethod_invoke (const EV_EditMethod * pEM, EV_EditMethodCallData * pData);
168 ABI_EXPORT bool ev_EditMethod_invoke (const EV_EditMethod * pEM, const UT_String & data);
169 ABI_EXPORT bool ev_EditMethod_invoke (const EV_EditMethod * pEM, const UT_UCS4String & data);
170 
171 ABI_EXPORT bool ev_EditMethod_invoke (const char * methodName, const UT_String & data);
172 ABI_EXPORT bool ev_EditMethod_invoke (const char * methodName, const UT_UCS4String & data);
173 ABI_EXPORT bool ev_EditMethod_invoke (const char * methodName, const char * data);
174 ABI_EXPORT bool ev_EditMethod_invoke (const char * methodName, const UT_UCSChar * data);
175 ABI_EXPORT bool ev_EditMethod_invoke (const UT_String& methodName, const UT_String & data);
176 ABI_EXPORT bool ev_EditMethod_invoke (const UT_String& methodName, const UT_UCS4String & data);
177 
178 ABI_EXPORT EV_EditMethod* ev_EditMethod_lookup (const char * methodName);
179 ABI_EXPORT EV_EditMethod* ev_EditMethod_lookup (const UT_String & methodName);
180 
181 #endif /* EV_EDITMETHOD_H */
182