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 gsv.SourceCompletion;
26 
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import glib.ListG;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import gsv.SourceCompletionContext;
33 private import gsv.SourceCompletionInfo;
34 private import gsv.SourceCompletionProviderIF;
35 private import gsv.SourceView;
36 private import gsv.c.functions;
37 public  import gsv.c.types;
38 public  import gsvc.gsvtypes;
39 private import gtk.BuildableIF;
40 private import gtk.BuildableT;
41 private import gtk.TextIter;
42 private import std.algorithm;
43 
44 
45 /** */
46 public class SourceCompletion : ObjectG, BuildableIF
47 {
48 	/** the main Gtk struct */
49 	protected GtkSourceCompletion* gtkSourceCompletion;
50 
51 	/** Get the main Gtk struct */
52 	public GtkSourceCompletion* getSourceCompletionStruct(bool transferOwnership = false)
53 	{
54 		if (transferOwnership)
55 			ownedRef = false;
56 		return gtkSourceCompletion;
57 	}
58 
59 	/** the main Gtk struct as a void* */
60 	protected override void* getStruct()
61 	{
62 		return cast(void*)gtkSourceCompletion;
63 	}
64 
65 	protected override void setStruct(GObject* obj)
66 	{
67 		gtkSourceCompletion = cast(GtkSourceCompletion*)obj;
68 		super.setStruct(obj);
69 	}
70 
71 	/**
72 	 * Sets our main struct and passes it to the parent class.
73 	 */
74 	public this (GtkSourceCompletion* gtkSourceCompletion, bool ownedRef = false)
75 	{
76 		this.gtkSourceCompletion = gtkSourceCompletion;
77 		super(cast(GObject*)gtkSourceCompletion, ownedRef);
78 	}
79 
80 	// add the Buildable capabilities
81 	mixin BuildableT!(GtkSourceCompletion);
82 
83 
84 	/** */
85 	public static GType getType()
86 	{
87 		return gtk_source_completion_get_type();
88 	}
89 
90 	/**
91 	 * Add a new #GtkSourceCompletionProvider to the completion object. This will
92 	 * add a reference @provider, so make sure to unref your own copy when you
93 	 * no longer need it.
94 	 *
95 	 * Params:
96 	 *     provider = a #GtkSourceCompletionProvider.
97 	 *
98 	 * Returns: %TRUE if @provider was successfully added, otherwise if @error
99 	 *     is provided, it will be set with the error and %FALSE is returned.
100 	 *
101 	 * Throws: GException on failure.
102 	 */
103 	public bool addProvider(SourceCompletionProviderIF provider)
104 	{
105 		GError* err = null;
106 
107 		auto p = gtk_source_completion_add_provider(gtkSourceCompletion, (provider is null) ? null : provider.getSourceCompletionProviderStruct(), &err) != 0;
108 
109 		if (err !is null)
110 		{
111 			throw new GException( new ErrorG(err) );
112 		}
113 
114 		return p;
115 	}
116 
117 	/**
118 	 * Block interactive completion. This can be used to disable interactive
119 	 * completion when inserting or deleting text from the buffer associated with
120 	 * the completion. Use gtk_source_completion_unblock_interactive() to enable
121 	 * interactive completion again.
122 	 *
123 	 * This function may be called multiple times. It will continue to block
124 	 * interactive completion until gtk_source_completion_unblock_interactive()
125 	 * has been called the same number of times.
126 	 */
127 	public void blockInteractive()
128 	{
129 		gtk_source_completion_block_interactive(gtkSourceCompletion);
130 	}
131 
132 	/**
133 	 * Create a new #GtkSourceCompletionContext for @completion. The position where
134 	 * the completion occurs can be specified by @position. If @position is %NULL,
135 	 * the current cursor position will be used.
136 	 *
137 	 * Params:
138 	 *     position = a #GtkTextIter, or %NULL.
139 	 *
140 	 * Returns: a new #GtkSourceCompletionContext.
141 	 *     The reference being returned is a 'floating' reference,
142 	 *     so if you invoke gtk_source_completion_show() with this context
143 	 *     you don't need to unref it.
144 	 */
145 	public SourceCompletionContext createContext(TextIter position)
146 	{
147 		auto p = gtk_source_completion_create_context(gtkSourceCompletion, (position is null) ? null : position.getTextIterStruct());
148 
149 		if(p is null)
150 		{
151 			return null;
152 		}
153 
154 		return ObjectG.getDObject!(SourceCompletionContext)(cast(GtkSourceCompletionContext*) p);
155 	}
156 
157 	/**
158 	 * The info widget is the window where the completion displays optional extra
159 	 * information of the proposal.
160 	 *
161 	 * Returns: The #GtkSourceCompletionInfo window
162 	 *     associated with @completion.
163 	 */
164 	public SourceCompletionInfo getInfoWindow()
165 	{
166 		auto p = gtk_source_completion_get_info_window(gtkSourceCompletion);
167 
168 		if(p is null)
169 		{
170 			return null;
171 		}
172 
173 		return ObjectG.getDObject!(SourceCompletionInfo)(cast(GtkSourceCompletionInfo*) p);
174 	}
175 
176 	/**
177 	 * Get list of providers registered on @completion. The returned list is owned
178 	 * by the completion and should not be freed.
179 	 *
180 	 * Returns: list of #GtkSourceCompletionProvider.
181 	 */
182 	public ListG getProviders()
183 	{
184 		auto p = gtk_source_completion_get_providers(gtkSourceCompletion);
185 
186 		if(p is null)
187 		{
188 			return null;
189 		}
190 
191 		return new ListG(cast(GList*) p);
192 	}
193 
194 	/**
195 	 * The #GtkSourceView associated with @completion, or %NULL if the view has been
196 	 * destroyed.
197 	 *
198 	 * Returns: The #GtkSourceView associated with
199 	 *     @completion, or %NULL.
200 	 */
201 	public SourceView getView()
202 	{
203 		auto p = gtk_source_completion_get_view(gtkSourceCompletion);
204 
205 		if(p is null)
206 		{
207 			return null;
208 		}
209 
210 		return ObjectG.getDObject!(SourceView)(cast(GtkSourceView*) p);
211 	}
212 
213 	/**
214 	 * Hides the completion if it is active (visible).
215 	 */
216 	public void hide()
217 	{
218 		gtk_source_completion_hide(gtkSourceCompletion);
219 	}
220 
221 	/**
222 	 * Move the completion window to a specific iter.
223 	 *
224 	 * Deprecated: Use gtk_source_completion_provider_get_start_iter() instead.
225 	 *
226 	 * Params:
227 	 *     iter = a #GtkTextIter.
228 	 */
229 	public void moveWindow(TextIter iter)
230 	{
231 		gtk_source_completion_move_window(gtkSourceCompletion, (iter is null) ? null : iter.getTextIterStruct());
232 	}
233 
234 	/**
235 	 * Remove @provider from the completion.
236 	 *
237 	 * Params:
238 	 *     provider = a #GtkSourceCompletionProvider.
239 	 *
240 	 * Returns: %TRUE if @provider was successfully removed, otherwise if @error
241 	 *     is provided, it will be set with the error and %FALSE is returned.
242 	 *
243 	 * Throws: GException on failure.
244 	 */
245 	public bool removeProvider(SourceCompletionProviderIF provider)
246 	{
247 		GError* err = null;
248 
249 		auto p = gtk_source_completion_remove_provider(gtkSourceCompletion, (provider is null) ? null : provider.getSourceCompletionProviderStruct(), &err) != 0;
250 
251 		if (err !is null)
252 		{
253 			throw new GException( new ErrorG(err) );
254 		}
255 
256 		return p;
257 	}
258 
259 	/**
260 	 * Starts a new completion with the specified #GtkSourceCompletionContext and
261 	 * a list of potential candidate providers for completion.
262 	 *
263 	 * It can be convenient for showing a completion on-the-fly, without the need to
264 	 * add or remove providers to the #GtkSourceCompletion.
265 	 *
266 	 * Another solution is to add providers with
267 	 * gtk_source_completion_add_provider(), and implement
268 	 * gtk_source_completion_provider_match() for each provider.
269 	 *
270 	 * Params:
271 	 *     providers = a list of #GtkSourceCompletionProvider, or %NULL.
272 	 *     context = The #GtkSourceCompletionContext
273 	 *         with which to start the completion.
274 	 *
275 	 * Returns: %TRUE if it was possible to the show completion window.
276 	 */
277 	public bool show(ListG providers, SourceCompletionContext context)
278 	{
279 		return gtk_source_completion_show(gtkSourceCompletion, (providers is null) ? null : providers.getListGStruct(), (context is null) ? null : context.getSourceCompletionContextStruct()) != 0;
280 	}
281 
282 	/**
283 	 * Unblock interactive completion. This can be used after using
284 	 * gtk_source_completion_block_interactive() to enable interactive completion
285 	 * again.
286 	 */
287 	public void unblockInteractive()
288 	{
289 		gtk_source_completion_unblock_interactive(gtkSourceCompletion);
290 	}
291 
292 	protected class OnActivateProposalDelegateWrapper
293 	{
294 		void delegate(SourceCompletion) dlg;
295 		gulong handlerId;
296 
297 		this(void delegate(SourceCompletion) dlg)
298 		{
299 			this.dlg = dlg;
300 			onActivateProposalListeners ~= this;
301 		}
302 
303 		void remove(OnActivateProposalDelegateWrapper source)
304 		{
305 			foreach(index, wrapper; onActivateProposalListeners)
306 			{
307 				if (wrapper.handlerId == source.handlerId)
308 				{
309 					onActivateProposalListeners[index] = null;
310 					onActivateProposalListeners = std.algorithm.remove(onActivateProposalListeners, index);
311 					break;
312 				}
313 			}
314 		}
315 	}
316 	OnActivateProposalDelegateWrapper[] onActivateProposalListeners;
317 
318 	/**
319 	 * The #GtkSourceCompletion::activate-proposal signal is a
320 	 * keybinding signal which gets emitted when the user initiates
321 	 * a proposal activation.
322 	 *
323 	 * Applications should not connect to it, but may emit it with
324 	 * g_signal_emit_by_name() if they need to control the proposal
325 	 * activation programmatically.
326 	 */
327 	gulong addOnActivateProposal(void delegate(SourceCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
328 	{
329 		auto wrapper = new OnActivateProposalDelegateWrapper(dlg);
330 		wrapper.handlerId = Signals.connectData(
331 			this,
332 			"activate-proposal",
333 			cast(GCallback)&callBackActivateProposal,
334 			cast(void*)wrapper,
335 			cast(GClosureNotify)&callBackActivateProposalDestroy,
336 			connectFlags);
337 		return wrapper.handlerId;
338 	}
339 
340 	extern(C) static void callBackActivateProposal(GtkSourceCompletion* sourcecompletionStruct, OnActivateProposalDelegateWrapper wrapper)
341 	{
342 		wrapper.dlg(wrapper.outer);
343 	}
344 
345 	extern(C) static void callBackActivateProposalDestroy(OnActivateProposalDelegateWrapper wrapper, GClosure* closure)
346 	{
347 		wrapper.remove(wrapper);
348 	}
349 
350 	protected class OnHideDelegateWrapper
351 	{
352 		void delegate(SourceCompletion) dlg;
353 		gulong handlerId;
354 
355 		this(void delegate(SourceCompletion) dlg)
356 		{
357 			this.dlg = dlg;
358 			onHideListeners ~= this;
359 		}
360 
361 		void remove(OnHideDelegateWrapper source)
362 		{
363 			foreach(index, wrapper; onHideListeners)
364 			{
365 				if (wrapper.handlerId == source.handlerId)
366 				{
367 					onHideListeners[index] = null;
368 					onHideListeners = std.algorithm.remove(onHideListeners, index);
369 					break;
370 				}
371 			}
372 		}
373 	}
374 	OnHideDelegateWrapper[] onHideListeners;
375 
376 	/**
377 	 * Emitted when the completion window is hidden. The default handler
378 	 * will actually hide the window.
379 	 */
380 	gulong addOnHide(void delegate(SourceCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
381 	{
382 		auto wrapper = new OnHideDelegateWrapper(dlg);
383 		wrapper.handlerId = Signals.connectData(
384 			this,
385 			"hide",
386 			cast(GCallback)&callBackHide,
387 			cast(void*)wrapper,
388 			cast(GClosureNotify)&callBackHideDestroy,
389 			connectFlags);
390 		return wrapper.handlerId;
391 	}
392 
393 	extern(C) static void callBackHide(GtkSourceCompletion* sourcecompletionStruct, OnHideDelegateWrapper wrapper)
394 	{
395 		wrapper.dlg(wrapper.outer);
396 	}
397 
398 	extern(C) static void callBackHideDestroy(OnHideDelegateWrapper wrapper, GClosure* closure)
399 	{
400 		wrapper.remove(wrapper);
401 	}
402 
403 	protected class OnMoveCursorDelegateWrapper
404 	{
405 		void delegate(GtkScrollStep, int, SourceCompletion) dlg;
406 		gulong handlerId;
407 
408 		this(void delegate(GtkScrollStep, int, SourceCompletion) dlg)
409 		{
410 			this.dlg = dlg;
411 			onMoveCursorListeners ~= this;
412 		}
413 
414 		void remove(OnMoveCursorDelegateWrapper source)
415 		{
416 			foreach(index, wrapper; onMoveCursorListeners)
417 			{
418 				if (wrapper.handlerId == source.handlerId)
419 				{
420 					onMoveCursorListeners[index] = null;
421 					onMoveCursorListeners = std.algorithm.remove(onMoveCursorListeners, index);
422 					break;
423 				}
424 			}
425 		}
426 	}
427 	OnMoveCursorDelegateWrapper[] onMoveCursorListeners;
428 
429 	/**
430 	 * The #GtkSourceCompletion::move-cursor signal is a keybinding
431 	 * signal which gets emitted when the user initiates a cursor
432 	 * movement.
433 	 *
434 	 * The <keycap>Up</keycap>, <keycap>Down</keycap>,
435 	 * <keycap>PageUp</keycap>, <keycap>PageDown</keycap>,
436 	 * <keycap>Home</keycap> and <keycap>End</keycap> keys are bound to the
437 	 * normal behavior expected by those keys.
438 	 *
439 	 * When @step is equal to %GTK_SCROLL_PAGES, the page size is defined by
440 	 * the #GtkSourceCompletion:proposal-page-size property. It is used for
441 	 * the <keycap>PageDown</keycap> and <keycap>PageUp</keycap> keys.
442 	 *
443 	 * Applications should not connect to it, but may emit it with
444 	 * g_signal_emit_by_name() if they need to control the cursor
445 	 * programmatically.
446 	 *
447 	 * Params:
448 	 *     step = The #GtkScrollStep by which to move the cursor
449 	 *     num = The amount of steps to move the cursor
450 	 */
451 	gulong addOnMoveCursor(void delegate(GtkScrollStep, int, SourceCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
452 	{
453 		auto wrapper = new OnMoveCursorDelegateWrapper(dlg);
454 		wrapper.handlerId = Signals.connectData(
455 			this,
456 			"move-cursor",
457 			cast(GCallback)&callBackMoveCursor,
458 			cast(void*)wrapper,
459 			cast(GClosureNotify)&callBackMoveCursorDestroy,
460 			connectFlags);
461 		return wrapper.handlerId;
462 	}
463 
464 	extern(C) static void callBackMoveCursor(GtkSourceCompletion* sourcecompletionStruct, GtkScrollStep step, int num, OnMoveCursorDelegateWrapper wrapper)
465 	{
466 		wrapper.dlg(step, num, wrapper.outer);
467 	}
468 
469 	extern(C) static void callBackMoveCursorDestroy(OnMoveCursorDelegateWrapper wrapper, GClosure* closure)
470 	{
471 		wrapper.remove(wrapper);
472 	}
473 
474 	protected class OnMovePageDelegateWrapper
475 	{
476 		void delegate(GtkScrollStep, int, SourceCompletion) dlg;
477 		gulong handlerId;
478 
479 		this(void delegate(GtkScrollStep, int, SourceCompletion) dlg)
480 		{
481 			this.dlg = dlg;
482 			onMovePageListeners ~= this;
483 		}
484 
485 		void remove(OnMovePageDelegateWrapper source)
486 		{
487 			foreach(index, wrapper; onMovePageListeners)
488 			{
489 				if (wrapper.handlerId == source.handlerId)
490 				{
491 					onMovePageListeners[index] = null;
492 					onMovePageListeners = std.algorithm.remove(onMovePageListeners, index);
493 					break;
494 				}
495 			}
496 		}
497 	}
498 	OnMovePageDelegateWrapper[] onMovePageListeners;
499 
500 	/**
501 	 * The #GtkSourceCompletion::move-page signal is a keybinding
502 	 * signal which gets emitted when the user initiates a page
503 	 * movement (i.e. switches between provider pages).
504 	 *
505 	 * <keycombo><keycap>Control</keycap><keycap>Left</keycap></keycombo>
506 	 * is for going to the previous provider.
507 	 * <keycombo><keycap>Control</keycap><keycap>Right</keycap></keycombo>
508 	 * is for going to the next provider.
509 	 * <keycombo><keycap>Control</keycap><keycap>Home</keycap></keycombo>
510 	 * is for displaying all the providers.
511 	 * <keycombo><keycap>Control</keycap><keycap>End</keycap></keycombo>
512 	 * is for going to the last provider.
513 	 *
514 	 * When @step is equal to #GTK_SCROLL_PAGES, the page size is defined by
515 	 * the #GtkSourceCompletion:provider-page-size property.
516 	 *
517 	 * Applications should not connect to it, but may emit it with
518 	 * g_signal_emit_by_name() if they need to control the page selection
519 	 * programmatically.
520 	 *
521 	 * Params:
522 	 *     step = The #GtkScrollStep by which to move the page
523 	 *     num = The amount of steps to move the page
524 	 */
525 	gulong addOnMovePage(void delegate(GtkScrollStep, int, SourceCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
526 	{
527 		auto wrapper = new OnMovePageDelegateWrapper(dlg);
528 		wrapper.handlerId = Signals.connectData(
529 			this,
530 			"move-page",
531 			cast(GCallback)&callBackMovePage,
532 			cast(void*)wrapper,
533 			cast(GClosureNotify)&callBackMovePageDestroy,
534 			connectFlags);
535 		return wrapper.handlerId;
536 	}
537 
538 	extern(C) static void callBackMovePage(GtkSourceCompletion* sourcecompletionStruct, GtkScrollStep step, int num, OnMovePageDelegateWrapper wrapper)
539 	{
540 		wrapper.dlg(step, num, wrapper.outer);
541 	}
542 
543 	extern(C) static void callBackMovePageDestroy(OnMovePageDelegateWrapper wrapper, GClosure* closure)
544 	{
545 		wrapper.remove(wrapper);
546 	}
547 
548 	protected class OnPopulateContextDelegateWrapper
549 	{
550 		void delegate(SourceCompletionContext, SourceCompletion) dlg;
551 		gulong handlerId;
552 
553 		this(void delegate(SourceCompletionContext, SourceCompletion) dlg)
554 		{
555 			this.dlg = dlg;
556 			onPopulateContextListeners ~= this;
557 		}
558 
559 		void remove(OnPopulateContextDelegateWrapper source)
560 		{
561 			foreach(index, wrapper; onPopulateContextListeners)
562 			{
563 				if (wrapper.handlerId == source.handlerId)
564 				{
565 					onPopulateContextListeners[index] = null;
566 					onPopulateContextListeners = std.algorithm.remove(onPopulateContextListeners, index);
567 					break;
568 				}
569 			}
570 		}
571 	}
572 	OnPopulateContextDelegateWrapper[] onPopulateContextListeners;
573 
574 	/**
575 	 * Emitted just before starting to populate the completion with providers.
576 	 * You can use this signal to add additional attributes in the context.
577 	 *
578 	 * Params:
579 	 *     context = The #GtkSourceCompletionContext for the current completion
580 	 */
581 	gulong addOnPopulateContext(void delegate(SourceCompletionContext, SourceCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
582 	{
583 		auto wrapper = new OnPopulateContextDelegateWrapper(dlg);
584 		wrapper.handlerId = Signals.connectData(
585 			this,
586 			"populate-context",
587 			cast(GCallback)&callBackPopulateContext,
588 			cast(void*)wrapper,
589 			cast(GClosureNotify)&callBackPopulateContextDestroy,
590 			connectFlags);
591 		return wrapper.handlerId;
592 	}
593 
594 	extern(C) static void callBackPopulateContext(GtkSourceCompletion* sourcecompletionStruct, GtkSourceCompletionContext* context, OnPopulateContextDelegateWrapper wrapper)
595 	{
596 		wrapper.dlg(ObjectG.getDObject!(SourceCompletionContext)(context), wrapper.outer);
597 	}
598 
599 	extern(C) static void callBackPopulateContextDestroy(OnPopulateContextDelegateWrapper wrapper, GClosure* closure)
600 	{
601 		wrapper.remove(wrapper);
602 	}
603 
604 	protected class OnShowDelegateWrapper
605 	{
606 		void delegate(SourceCompletion) dlg;
607 		gulong handlerId;
608 
609 		this(void delegate(SourceCompletion) dlg)
610 		{
611 			this.dlg = dlg;
612 			onShowListeners ~= this;
613 		}
614 
615 		void remove(OnShowDelegateWrapper source)
616 		{
617 			foreach(index, wrapper; onShowListeners)
618 			{
619 				if (wrapper.handlerId == source.handlerId)
620 				{
621 					onShowListeners[index] = null;
622 					onShowListeners = std.algorithm.remove(onShowListeners, index);
623 					break;
624 				}
625 			}
626 		}
627 	}
628 	OnShowDelegateWrapper[] onShowListeners;
629 
630 	/**
631 	 * Emitted when the completion window is shown. The default handler
632 	 * will actually show the window.
633 	 */
634 	gulong addOnShow(void delegate(SourceCompletion) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
635 	{
636 		auto wrapper = new OnShowDelegateWrapper(dlg);
637 		wrapper.handlerId = Signals.connectData(
638 			this,
639 			"show",
640 			cast(GCallback)&callBackShow,
641 			cast(void*)wrapper,
642 			cast(GClosureNotify)&callBackShowDestroy,
643 			connectFlags);
644 		return wrapper.handlerId;
645 	}
646 
647 	extern(C) static void callBackShow(GtkSourceCompletion* sourcecompletionStruct, OnShowDelegateWrapper wrapper)
648 	{
649 		wrapper.dlg(wrapper.outer);
650 	}
651 
652 	extern(C) static void callBackShowDestroy(OnShowDelegateWrapper wrapper, GClosure* closure)
653 	{
654 		wrapper.remove(wrapper);
655 	}
656 }