1 /*
2  * GNT - The GLib Ncurses Toolkit
3  *
4  * GNT is the legal property of its developers, whose names are too numerous
5  * to list here.  Please refer to the COPYRIGHT file distributed with this
6  * source distribution.
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
21  */
22 
23 #ifndef GNT_BINDABLE_H
24 #define GNT_BINDABLE_H
25 /**
26  * SECTION:gntbindable
27  * @section_id: libgnt-gntbindable
28  * @title: GntBindable
29  * @short_description: Key-bindable GObjects
30  */
31 
32 #include <stdio.h>
33 #include <glib.h>
34 #include <glib-object.h>
35 #include <ncurses.h>
36 
37 #define GNT_TYPE_BINDABLE				(gnt_bindable_get_gtype())
38 #define GNT_BINDABLE(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), GNT_TYPE_BINDABLE, GntBindable))
39 #define GNT_BINDABLE_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), GNT_TYPE_BINDABLE, GntBindableClass))
40 #define GNT_IS_BINDABLE(obj)			(G_TYPE_CHECK_INSTANCE_TYPE((obj), GNT_TYPE_BINDABLE))
41 #define GNT_IS_BINDABLE_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass), GNT_TYPE_BINDABLE))
42 #define GNT_BINDABLE_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), GNT_TYPE_BINDABLE, GntBindableClass))
43 
44 #define	GNTDEBUG
45 
46 typedef struct _GntBindable			GntBindable;
47 typedef struct _GntBindableClass		GntBindableClass;
48 
49 struct _GntBindable
50 {
51 	GObject inherit;
52 };
53 
54 struct _GntBindableClass
55 {
56 	GObjectClass parent;
57 
58 	GHashTable *remaps;   /* Key remaps */
59 	GHashTable *actions;  /* name -> Action */
60 	GHashTable *bindings; /* key -> ActionParam */
61 
62 	GntBindable * help_window;
63 
64 	/*< private >*/
65 	void (*gnt_reserved2)(void);
66 	void (*gnt_reserved3)(void);
67 	void (*gnt_reserved4)(void);
68 };
69 
70 G_BEGIN_DECLS
71 
72 GType gnt_bindable_get_gtype(void);
73 
74 /******************/
75 /*   Key Remaps   */
76 /******************/
77 const char * gnt_bindable_remap_keys(GntBindable *bindable, const char *text);
78 
79 /******************/
80 /* Bindable Actions */
81 /******************/
82 typedef gboolean (*GntBindableActionCallback) (GntBindable *bindable, GList *params);
83 typedef gboolean (*GntBindableActionCallbackNoParam)(GntBindable *bindable);
84 
85 #ifndef GNT_DISABLE_DEPRECATED
86 /**
87  * GntBindableAction:
88  *
89  * Deprecated: 2.14.0: This is an internal implementation detail.
90  */
91 typedef struct _GntBindableAction GntBindableAction;
92 /**
93  * GntBindableActionParam:
94  *
95  * Deprecated: 2.14.0: This is an internal implementation detail.
96  */
97 typedef struct _GntBindableActionParam GntBindableActionParam;
98 #endif
99 
100 struct _GntBindableAction
101 {
102 	char *name;        /* The name of the action */
103 	union {
104 		GntBindableActionCallback action;
105 		GntBindableActionCallbackNoParam action_noparam;
106 	} u;
107 };
108 
109 struct _GntBindableActionParam
110 {
111 	struct _GntBindableAction *action;
112 	GList *list;
113 };
114 
115 #ifndef GNT_DISABLE_DEPRECATED
116 /*GntBindableAction *gnt_bindable_action_parse(const char *name);*/
117 
118 /**
119  * gnt_bindable_action_free:
120  * @action: The bindable action.
121  *
122  * Free a bindable action.
123  *
124  * Deprecated: 2.14.0: This is an internal implementation detail.
125  */
126 void gnt_bindable_action_free(GntBindableAction *action);
127 
128 /**
129  * gnt_bindable_action_param_free:
130  * @param:  The GntBindableActionParam to free.
131  *
132  * Free a GntBindableActionParam.
133  *
134  * Deprecated: 2.14.0: This is an internal implementation detail.
135  */
136 void gnt_bindable_action_param_free(GntBindableActionParam *param);
137 #endif
138 
139 /**
140  * gnt_bindable_class_register_action:
141  * @klass:    The class the binding is for.
142  * @name:     The name of the binding.
143  * @callback: (scope call): The callback for the binding.
144  * @trigger:  The default trigger for the binding, or %NULL, followed by a
145  *            %NULL-terminated list of default parameters.
146  *
147  * Register a bindable action for a class.
148  */
149 void gnt_bindable_class_register_action(GntBindableClass *klass, const char *name, GntBindableActionCallback callback, const char *trigger, ...);
150 
151 /**
152  * gnt_bindable_register_binding:
153  * @klass:     The class the binding is for.
154  * @name:      The name of the binding.
155  * @trigger:   A new trigger for the binding, followed by a %NULL-terminated list of parameters for the callback.
156  *
157  * Register a key-binding to an existing action.
158  */
159 void gnt_bindable_register_binding(GntBindableClass *klass, const char *name, const char *trigger, ...);
160 
161 /**
162  * gnt_bindable_perform_action_key:
163  * @bindable:  The bindable object.
164  * @keys:      The key to trigger the action.
165  *
166  * Perform an action from a keybinding.
167  *
168  * Returns:  %TRUE if the action was performed successfully, %FALSE otherwise.
169  */
170 gboolean gnt_bindable_perform_action_key(GntBindable *bindable, const char *keys);
171 
172 /**
173  * gnt_bindable_check_key:
174  * @bindable:  The bindable object.
175  * @keys:      The key to check for.
176  *
177  * Discover if a key is bound.
178  *
179  * Returns:  %TRUE if the the key has an action associated with it.
180  *
181  * Since: 2.4.2
182  */
183 gboolean gnt_bindable_check_key(GntBindable *bindable, const char *keys);
184 
185 /**
186  * gnt_bindable_perform_action_named:
187  * @bindable:  The bindable object.
188  * @name:      The action to perform, followed by a %NULL-terminated list of parameters.
189  *
190  * Perform an action on a bindable object.
191  *
192  * Returns:  %TRUE if the action was performed successfully, %FALSE otherwise.
193  */
194 gboolean gnt_bindable_perform_action_named(GntBindable *bindable, const char *name, ...) G_GNUC_NULL_TERMINATED;
195 
196 /**
197  * gnt_bindable_bindings_view:
198  * @bind:  The object to list the bindings for.
199  *
200  * Returns a GntTree populated with "key" -> "binding" for the widget.
201  *
202  * Returns: (transfer full): The GntTree.
203  *
204  * Since: 2.1.1
205  */
206 GntBindable * gnt_bindable_bindings_view(GntBindable *bind);
207 
208 /**
209  * gnt_bindable_build_help_window:
210  * @bindable:   The object to list the bindings for.
211  *
212  * Builds a window that list the key bindings for a GntBindable object.
213  * From this window a user can select a listing to rebind a new key for the given action.
214  *
215  * Returns:  %TRUE
216  *
217  * Since: 2.1.1
218  */
219 
220 gboolean gnt_bindable_build_help_window(GntBindable *bindable);
221 
222 G_END_DECLS
223 
224 #endif /* GNT_BINDABLE_H */
225 
226