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