1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.IMContext;
26 
27 private import gdk.Device;
28 private import gdk.Event;
29 private import gdk.Surface;
30 private import glib.Str;
31 private import glib.c.functions;
32 private import gobject.ObjectG;
33 private import gobject.Signals;
34 private import gtk.Widget;
35 private import gtk.c.functions;
36 public  import gtk.c.types;
37 private import pango.PgAttributeList;
38 private import std.algorithm;
39 
40 
41 /**
42  * `GtkIMContext` defines the interface for GTK input methods.
43  * 
44  * `GtkIMContext` is used by GTK text input widgets like `GtkText`
45  * to map from key events to Unicode character strings.
46  * 
47  * By default, GTK uses a platform-dependent default input method.
48  * On Windows, the default implementation is IME-based and on Wayland,
49  * it is using the Wayland text protocol. The choice can be overridden
50  * programmatically via the [property@Gtk.Settings:gtk-im-module] setting.
51  * Users may set the `GTK_IM_MODULE` environment variable to override the
52  * default.
53  * 
54  * Text widgets have a :im-module property (e.g. [property@Gtk.TextView:im-module])
55  * that may also be used to set input methods for specific widget instances.
56  * For instance, a certain entry widget might be expected to contain
57  * certain characters which would be easier to input with a specific
58  * input method.
59  * 
60  * An input method may consume multiple key events in sequence before finally
61  * outputting the composed result. This is called *preediting*, and an input
62  * method may provide feedback about this process by displaying the intermediate
63  * composition states as preedit text.
64  * 
65  * For instance, the built-in GTK input method `GtkIMContextSimple` implements
66  * the input of arbitrary Unicode code points by holding down the
67  * <kbd>Control</kbd> and <kbd>Shift</kbd> keys and then typing <kbd>U</kbd>
68  * followed by the hexadecimal digits of the code point. When releasing the
69  * <kbd>Control</kbd> and <kbd>Shift</kbd> keys, preediting ends and the
70  * character is inserted as text. For example,
71  * 
72  * Ctrl+Shift+u 2 0 A C
73  * 
74  * results in the € sign.
75  * 
76  * Additional input methods can be made available for use by GTK widgets as
77  * loadable modules. An input method module is a small shared library which
78  * provides a `GIOExtension` for the extension point named "gtk-im-module".
79  */
80 public class IMContext : ObjectG
81 {
82 	/** the main Gtk struct */
83 	protected GtkIMContext* gtkIMContext;
84 
85 	/** Get the main Gtk struct */
86 	public GtkIMContext* getIMContextStruct(bool transferOwnership = false)
87 	{
88 		if (transferOwnership)
89 			ownedRef = false;
90 		return gtkIMContext;
91 	}
92 
93 	/** the main Gtk struct as a void* */
94 	protected override void* getStruct()
95 	{
96 		return cast(void*)gtkIMContext;
97 	}
98 
99 	/**
100 	 * Sets our main struct and passes it to the parent class.
101 	 */
102 	public this (GtkIMContext* gtkIMContext, bool ownedRef = false)
103 	{
104 		this.gtkIMContext = gtkIMContext;
105 		super(cast(GObject*)gtkIMContext, ownedRef);
106 	}
107 
108 
109 	/** */
110 	public static GType getType()
111 	{
112 		return gtk_im_context_get_type();
113 	}
114 
115 	/**
116 	 * Asks the widget that the input context is attached to delete
117 	 * characters around the cursor position by emitting the
118 	 * GtkIMContext::delete_surrounding signal.
119 	 *
120 	 * Note that @offset and @n_chars are in characters not in bytes
121 	 * which differs from the usage other places in #GtkIMContext.
122 	 *
123 	 * In order to use this function, you should first call
124 	 * gtk_im_context_get_surrounding() to get the current context, and
125 	 * call this function immediately afterwards to make sure that you
126 	 * know what you are deleting. You should also account for the fact
127 	 * that even if the signal was handled, the input context might not
128 	 * have deleted all the characters that were requested to be deleted.
129 	 *
130 	 * This function is used by an input method that wants to make
131 	 * subsitutions in the existing text in response to new input. It is
132 	 * not useful for applications.
133 	 *
134 	 * Params:
135 	 *     offset = offset from cursor position in chars;
136 	 *         a negative value means start before the cursor.
137 	 *     nChars = number of characters to delete.
138 	 *
139 	 * Returns: %TRUE if the signal was handled.
140 	 */
141 	public bool deleteSurrounding(int offset, int nChars)
142 	{
143 		return gtk_im_context_delete_surrounding(gtkIMContext, offset, nChars) != 0;
144 	}
145 
146 	/**
147 	 * Allow an input method to forward key press and release events
148 	 * to another input methodm without necessarily having a `GdkEvent`
149 	 * available.
150 	 *
151 	 * Params:
152 	 *     press = whether to forward a key press or release event
153 	 *     surface = the surface the event is for
154 	 *     device = the device that the event is for
155 	 *     time = the timestamp for the event
156 	 *     keycode = the keycode for the event
157 	 *     state = modifier state for the event
158 	 *     group = the active keyboard group for the event
159 	 *
160 	 * Returns: %TRUE if the input method handled the key event.
161 	 */
162 	public bool filterKey(bool press, Surface surface, Device device, uint time, uint keycode, GdkModifierType state, int group)
163 	{
164 		return gtk_im_context_filter_key(gtkIMContext, press, (surface is null) ? null : surface.getSurfaceStruct(), (device is null) ? null : device.getDeviceStruct(), time, keycode, state, group) != 0;
165 	}
166 
167 	/**
168 	 * Allow an input method to internally handle key press and release
169 	 * events.
170 	 *
171 	 * If this function returns %TRUE, then no further processing
172 	 * should be done for this key event.
173 	 *
174 	 * Params:
175 	 *     event = the key event
176 	 *
177 	 * Returns: %TRUE if the input method handled the key event.
178 	 */
179 	public bool filterKeypress(Event event)
180 	{
181 		return gtk_im_context_filter_keypress(gtkIMContext, (event is null) ? null : event.getEventStruct()) != 0;
182 	}
183 
184 	/**
185 	 * Notify the input method that the widget to which this
186 	 * input context corresponds has gained focus.
187 	 *
188 	 * The input method may, for example, change the displayed
189 	 * feedback to reflect this change.
190 	 */
191 	public void focusIn()
192 	{
193 		gtk_im_context_focus_in(gtkIMContext);
194 	}
195 
196 	/**
197 	 * Notify the input method that the widget to which this
198 	 * input context corresponds has lost focus.
199 	 *
200 	 * The input method may, for example, change the displayed
201 	 * feedback or reset the contexts state to reflect this change.
202 	 */
203 	public void focusOut()
204 	{
205 		gtk_im_context_focus_out(gtkIMContext);
206 	}
207 
208 	/**
209 	 * Retrieve the current preedit string for the input context,
210 	 * and a list of attributes to apply to the string.
211 	 *
212 	 * This string should be displayed inserted at the insertion point.
213 	 *
214 	 * Params:
215 	 *     str = location to store the retrieved
216 	 *         string. The string retrieved must be freed with g_free().
217 	 *     attrs = location to store the retrieved
218 	 *         attribute list.  When you are done with this list, you
219 	 *         must unreference it with pango_attr_list_unref().
220 	 *     cursorPos = location to store position of cursor (in characters)
221 	 *         within the preedit string.
222 	 */
223 	public void getPreeditString(out string str, out PgAttributeList attrs, out int cursorPos)
224 	{
225 		char* outstr = null;
226 		PangoAttrList* outattrs = null;
227 
228 		gtk_im_context_get_preedit_string(gtkIMContext, &outstr, &outattrs, &cursorPos);
229 
230 		str = Str.toString(outstr);
231 		attrs = ObjectG.getDObject!(PgAttributeList)(outattrs);
232 	}
233 
234 	/**
235 	 * Retrieves context around the insertion point.
236 	 *
237 	 * Input methods typically want context in order to constrain input text
238 	 * based on existing text; this is important for languages such as Thai
239 	 * where only some sequences of characters are allowed.
240 	 *
241 	 * This function is implemented by emitting the
242 	 * [signal@Gtk.IMContext::retrieve-surrounding] signal on the input method;
243 	 * in response to this signal, a widget should provide as much context as
244 	 * is available, up to an entire paragraph, by calling
245 	 * [method@Gtk.IMContext.set_surrounding].
246 	 *
247 	 * Note that there is no obligation for a widget to respond to the
248 	 * `::retrieve-surrounding` signal, so input methods must be prepared to
249 	 * function without context.
250 	 *
251 	 * Deprecated: Use [method@Gtk.IMContext.get_surrounding_with_selection] instead.
252 	 *
253 	 * Params:
254 	 *     text = location to store a UTF-8 encoded
255 	 *         string of text holding context around the insertion point.
256 	 *         If the function returns %TRUE, then you must free the result
257 	 *         stored in this location with g_free().
258 	 *     cursorIndex = location to store byte index of the insertion
259 	 *         cursor within @text.
260 	 *
261 	 * Returns: `TRUE` if surrounding text was provided; in this case
262 	 *     you must free the result stored in `text`.
263 	 */
264 	public bool getSurrounding(out string text, out int cursorIndex)
265 	{
266 		char* outtext = null;
267 
268 		auto __p = gtk_im_context_get_surrounding(gtkIMContext, &outtext, &cursorIndex) != 0;
269 
270 		text = Str.toString(outtext);
271 
272 		return __p;
273 	}
274 
275 	/**
276 	 * Retrieves context around the insertion point.
277 	 *
278 	 * Input methods typically want context in order to constrain input
279 	 * text based on existing text; this is important for languages such
280 	 * as Thai where only some sequences of characters are allowed.
281 	 *
282 	 * This function is implemented by emitting the
283 	 * [signal@Gtk.IMContext::retrieve-surrounding] signal on the input method;
284 	 * in response to this signal, a widget should provide as much context as
285 	 * is available, up to an entire paragraph, by calling
286 	 * [method@Gtk.IMContext.set_surrounding_with_selection].
287 	 *
288 	 * Note that there is no obligation for a widget to respond to the
289 	 * `::retrieve-surrounding` signal, so input methods must be prepared to
290 	 * function without context.
291 	 *
292 	 * Params:
293 	 *     text = location to store a UTF-8 encoded
294 	 *         string of text holding context around the insertion point.
295 	 *         If the function returns %TRUE, then you must free the result
296 	 *         stored in this location with g_free().
297 	 *     cursorIndex = location to store byte index of the insertion
298 	 *         cursor within @text.
299 	 *     anchorIndex = location to store byte index of the selection
300 	 *         bound within @text
301 	 *
302 	 * Returns: `TRUE` if surrounding text was provided; in this case
303 	 *     you must free the result stored in `text`.
304 	 *
305 	 * Since: 4.2
306 	 */
307 	public bool getSurroundingWithSelection(out string text, out int cursorIndex, out int anchorIndex)
308 	{
309 		char* outtext = null;
310 
311 		auto __p = gtk_im_context_get_surrounding_with_selection(gtkIMContext, &outtext, &cursorIndex, &anchorIndex) != 0;
312 
313 		text = Str.toString(outtext);
314 
315 		return __p;
316 	}
317 
318 	/**
319 	 * Notify the input method that a change such as a change in cursor
320 	 * position has been made.
321 	 *
322 	 * This will typically cause the input method to clear the preedit state.
323 	 */
324 	public void reset()
325 	{
326 		gtk_im_context_reset(gtkIMContext);
327 	}
328 
329 	/**
330 	 * Set the client widget for the input context.
331 	 *
332 	 * This is the `GtkWidget` holding the input focus. This widget is
333 	 * used in order to correctly position status windows, and may
334 	 * also be used for purposes internal to the input method.
335 	 *
336 	 * Params:
337 	 *     widget = the client widget. This may be %NULL to indicate
338 	 *         that the previous client widget no longer exists.
339 	 */
340 	public void setClientWidget(Widget widget)
341 	{
342 		gtk_im_context_set_client_widget(gtkIMContext, (widget is null) ? null : widget.getWidgetStruct());
343 	}
344 
345 	/**
346 	 * Notify the input method that a change in cursor
347 	 * position has been made.
348 	 *
349 	 * The location is relative to the client window.
350 	 *
351 	 * Params:
352 	 *     area = new location
353 	 */
354 	public void setCursorLocation(GdkRectangle* area)
355 	{
356 		gtk_im_context_set_cursor_location(gtkIMContext, area);
357 	}
358 
359 	/**
360 	 * Sets surrounding context around the insertion point and preedit
361 	 * string.
362 	 *
363 	 * This function is expected to be called in response to the
364 	 * [signal@Gtk.IMContext::retrieve-surrounding] signal, and will
365 	 * likely have no effect if called at other times.
366 	 *
367 	 * Deprecated: Use [method@Gtk.IMContext.set_surrounding_with_selection] instead
368 	 *
369 	 * Params:
370 	 *     text = text surrounding the insertion point, as UTF-8.
371 	 *         the preedit string should not be included within
372 	 *         @text.
373 	 *     len = the length of @text, or -1 if @text is nul-terminated
374 	 *     cursorIndex = the byte index of the insertion cursor within @text.
375 	 */
376 	public void setSurrounding(string text, int len, int cursorIndex)
377 	{
378 		gtk_im_context_set_surrounding(gtkIMContext, Str.toStringz(text), len, cursorIndex);
379 	}
380 
381 	/**
382 	 * Sets surrounding context around the insertion point and preedit
383 	 * string. This function is expected to be called in response to the
384 	 * GtkIMContext::retrieve_surrounding signal, and will likely have no
385 	 * effect if called at other times.
386 	 *
387 	 * Params:
388 	 *     text = text surrounding the insertion point, as UTF-8.
389 	 *         the preedit string should not be included within
390 	 *         @text.
391 	 *     len = the length of @text, or -1 if @text is nul-terminated
392 	 *     cursorIndex = the byte index of the insertion cursor within @text
393 	 *     anchorIndex = the byte index of the selection bound within @text
394 	 *
395 	 * Since: 4.2
396 	 */
397 	public void setSurroundingWithSelection(string text, int len, int cursorIndex, int anchorIndex)
398 	{
399 		gtk_im_context_set_surrounding_with_selection(gtkIMContext, Str.toStringz(text), len, cursorIndex, anchorIndex);
400 	}
401 
402 	/**
403 	 * Sets whether the IM context should use the preedit string
404 	 * to display feedback.
405 	 *
406 	 * If @use_preedit is %FALSE (default is %TRUE), then the IM context
407 	 * may use some other method to display feedback, such as displaying
408 	 * it in a child of the root window.
409 	 *
410 	 * Params:
411 	 *     usePreedit = whether the IM context should use the preedit string.
412 	 */
413 	public void setUsePreedit(bool usePreedit)
414 	{
415 		gtk_im_context_set_use_preedit(gtkIMContext, usePreedit);
416 	}
417 
418 	/**
419 	 * The ::commit signal is emitted when a complete input sequence
420 	 * has been entered by the user. This can be a single character
421 	 * immediately after a key press or the final result of preediting.
422 	 *
423 	 * Params:
424 	 *     str = the completed character(s) entered by the user
425 	 */
426 	gulong addOnCommit(void delegate(string, IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
427 	{
428 		return Signals.connect(this, "commit", dlg, connectFlags ^ ConnectFlags.SWAPPED);
429 	}
430 
431 	/**
432 	 * The ::delete-surrounding signal is emitted when the input method
433 	 * needs to delete all or part of the context surrounding the cursor.
434 	 *
435 	 * Params:
436 	 *     offset = the character offset from the cursor position of the text
437 	 *         to be deleted. A negative value indicates a position before
438 	 *         the cursor.
439 	 *     nChars = the number of characters to be deleted
440 	 *
441 	 * Returns: %TRUE if the signal was handled.
442 	 */
443 	gulong addOnDeleteSurrounding(bool delegate(int, int, IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
444 	{
445 		return Signals.connect(this, "delete-surrounding", dlg, connectFlags ^ ConnectFlags.SWAPPED);
446 	}
447 
448 	/**
449 	 * The ::preedit-changed signal is emitted whenever the preedit sequence
450 	 * currently being entered has changed.  It is also emitted at the end of
451 	 * a preedit sequence, in which case
452 	 * gtk_im_context_get_preedit_string() returns the empty string.
453 	 */
454 	gulong addOnPreeditChanged(void delegate(IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
455 	{
456 		return Signals.connect(this, "preedit-changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
457 	}
458 
459 	/**
460 	 * The ::preedit-end signal is emitted when a preediting sequence
461 	 * has been completed or canceled.
462 	 */
463 	gulong addOnPreeditEnd(void delegate(IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
464 	{
465 		return Signals.connect(this, "preedit-end", dlg, connectFlags ^ ConnectFlags.SWAPPED);
466 	}
467 
468 	/**
469 	 * The ::preedit-start signal is emitted when a new preediting sequence
470 	 * starts.
471 	 */
472 	gulong addOnPreeditStart(void delegate(IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
473 	{
474 		return Signals.connect(this, "preedit-start", dlg, connectFlags ^ ConnectFlags.SWAPPED);
475 	}
476 
477 	/**
478 	 * The ::retrieve-surrounding signal is emitted when the input method
479 	 * requires the context surrounding the cursor.  The callback should set
480 	 * the input method surrounding context by calling the
481 	 * gtk_im_context_set_surrounding() method.
482 	 *
483 	 * Returns: %TRUE if the signal was handled.
484 	 */
485 	gulong addOnRetrieveSurrounding(bool delegate(IMContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
486 	{
487 		return Signals.connect(this, "retrieve-surrounding", dlg, connectFlags ^ ConnectFlags.SWAPPED);
488 	}
489 }