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 glib.ListG;
26 
27 private import gobject.ObjectG;
28 private import gtkc.glib;
29 public  import gtkc.glibtypes;
30 
31 
32 /**
33  * The #GList struct is used for each element in a doubly-linked list.
34  */
35 public class ListG
36 {
37 	/** the main Gtk struct */
38 	protected GList* gList;
39 
40 	/** Get the main Gtk struct */
41 	public GList* getListGStruct()
42 	{
43 		return gList;
44 	}
45 
46 	/** the main Gtk struct as a void* */
47 	protected void* getStruct()
48 	{
49 		return cast(void*)gList;
50 	}
51 
52 	/**
53 	 * Sets our main struct and passes it to the parent class.
54 	 */
55 	public this (GList* gList)
56 	{
57 		this.gList = gList;
58 	}
59 
60 	/** */
61 	@property void* data()
62 	{
63 		return gList.data;
64 	}
65 	
66 	/**
67 	 * get the next element
68 	 * Returns: the next element, or NULL if there are no more elements.
69 	 */
70 	@property ListG next()
71 	{
72 		if ( gList.next is null )
73 		{
74 			return null;
75 		}
76 		
77 		return new ListG(gList.next);
78 	}
79 	
80 	/**
81 	 * get the previous element
82 	 * Returns: the previous element, or NULL if there are no more elements.
83 	 */
84 	@property ListG previous()
85 	{
86 		if ( gList.prev is null )
87 		{
88 			return null;
89 		}
90 		
91 		return new ListG(gList.prev);
92 	}
93 	
94 	/**
95 	 * Turn the list into a D array of the desiered type.
96 	 * Type T wraps should match the type of the data.
97 	 */
98 	public T[] toArray(T, TC = typeof(T.tupleof[0]))()
99 	{
100 		T[] arr = new T[length()];
101 		ListG list = this;
102 		size_t count;
103 		
104 		while(list !is null && count < arr.length)
105 		{
106 			arr[count] = ObjectG.getDObject!(T)(cast(TC)list.data);
107 			list = list.next();
108 			count++;
109 		}
110 		
111 		return arr;
112 	}
113 	
114 	unittest
115 	{
116 		import gobject.Value;
117 		
118 		auto list = new ListG(null);
119 		list = list.append(new Value(0).getValueStruct());
120 		list = list.append(new Value(1).getValueStruct());
121 		auto arr = list.toArray!Value();
122 		
123 		assert(arr[0].getInt() == 0);
124 		assert(arr[1].getInt() == 1);
125 	}
126 
127 	/**
128 	 */
129 
130 	/**
131 	 * Allocates space for one #GList element. It is called by
132 	 * g_list_append(), g_list_prepend(), g_list_insert() and
133 	 * g_list_insert_sorted() and so is rarely used on its own.
134 	 *
135 	 * Return: a pointer to the newly-allocated #GList element
136 	 */
137 	public static ListG alloc()
138 	{
139 		auto p = g_list_alloc();
140 		
141 		if(p is null)
142 		{
143 			return null;
144 		}
145 		
146 		return new ListG(cast(GList*) p);
147 	}
148 
149 	/**
150 	 * Adds a new element on to the end of the list.
151 	 *
152 	 * Note that the return value is the new start of the list,
153 	 * if @list was empty; make sure you store the new value.
154 	 *
155 	 * g_list_append() has to traverse the entire list to find the end,
156 	 * which is inefficient when adding multiple elements. A common idiom
157 	 * to avoid the inefficiency is to use g_list_prepend() and reverse
158 	 * the list with g_list_reverse() when all elements have been added.
159 	 *
160 	 * |[<!-- language="C" -->
161 	 * // Notice that these are initialized to the empty list.
162 	 * GList *string_list = NULL, *number_list = NULL;
163 	 *
164 	 * // This is a list of strings.
165 	 * string_list = g_list_append (string_list, "first");
166 	 * string_list = g_list_append (string_list, "second");
167 	 *
168 	 * // This is a list of integers.
169 	 * number_list = g_list_append (number_list, GINT_TO_POINTER (27));
170 	 * number_list = g_list_append (number_list, GINT_TO_POINTER (14));
171 	 * ]|
172 	 *
173 	 * Params:
174 	 *     data = the data for the new element
175 	 *
176 	 * Return: either @list or the new start of the #GList if @list was %NULL
177 	 */
178 	public ListG append(void* data)
179 	{
180 		auto p = g_list_append(gList, data);
181 		
182 		if(p is null)
183 		{
184 			return null;
185 		}
186 		
187 		return new ListG(cast(GList*) p);
188 	}
189 
190 	/**
191 	 * Adds the second #GList onto the end of the first #GList.
192 	 * Note that the elements of the second #GList are not copied.
193 	 * They are used directly.
194 	 *
195 	 * This function is for example used to move an element in the list.
196 	 * The following example moves an element to the top of the list:
197 	 * |[<!-- language="C" -->
198 	 * list = g_list_remove_link (list, llink);
199 	 * list = g_list_concat (llink, list);
200 	 * ]|
201 	 *
202 	 * Params:
203 	 *     list2 = the #GList to add to the end of the first #GList,
204 	 *         this must point  to the top of the list
205 	 *
206 	 * Return: the start of the new #GList, which equals @list1 if not %NULL
207 	 */
208 	public ListG concat(ListG list2)
209 	{
210 		auto p = g_list_concat(gList, (list2 is null) ? null : list2.getListGStruct());
211 		
212 		if(p is null)
213 		{
214 			return null;
215 		}
216 		
217 		return new ListG(cast(GList*) p);
218 	}
219 
220 	/**
221 	 * Copies a #GList.
222 	 *
223 	 * Note that this is a "shallow" copy. If the list elements
224 	 * consist of pointers to data, the pointers are copied but
225 	 * the actual data is not. See g_list_copy_deep() if you need
226 	 * to copy the data as well.
227 	 *
228 	 * Return: the start of the new list that holds the same data as @list
229 	 */
230 	public ListG copy()
231 	{
232 		auto p = g_list_copy(gList);
233 		
234 		if(p is null)
235 		{
236 			return null;
237 		}
238 		
239 		return new ListG(cast(GList*) p);
240 	}
241 
242 	/**
243 	 * Makes a full (deep) copy of a #GList.
244 	 *
245 	 * In contrast with g_list_copy(), this function uses @func to make
246 	 * a copy of each list element, in addition to copying the list
247 	 * container itself.
248 	 *
249 	 * @func, as a #GCopyFunc, takes two arguments, the data to be copied
250 	 * and a @user_data pointer. It's safe to pass %NULL as user_data,
251 	 * if the copy function takes only one argument.
252 	 *
253 	 * For instance, if @list holds a list of GObjects, you can do:
254 	 * |[<!-- language="C" -->
255 	 * another_list = g_list_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
256 	 * ]|
257 	 *
258 	 * And, to entirely free the new list, you could do:
259 	 * |[<!-- language="C" -->
260 	 * g_list_free_full (another_list, g_object_unref);
261 	 * ]|
262 	 *
263 	 * Params:
264 	 *     func = a copy function used to copy every element in the list
265 	 *     userData = user data passed to the copy function @func, or %NULL
266 	 *
267 	 * Return: the start of the new list that holds a full copy of @list,
268 	 *     use g_list_free_full() to free it
269 	 *
270 	 * Since: 2.34
271 	 */
272 	public ListG copyDeep(GCopyFunc func, void* userData)
273 	{
274 		auto p = g_list_copy_deep(gList, func, userData);
275 		
276 		if(p is null)
277 		{
278 			return null;
279 		}
280 		
281 		return new ListG(cast(GList*) p);
282 	}
283 
284 	/**
285 	 * Removes the node link_ from the list and frees it.
286 	 * Compare this to g_list_remove_link() which removes the node
287 	 * without freeing it.
288 	 *
289 	 * Params:
290 	 *     link = node to delete from @list
291 	 *
292 	 * Return: the (possibly changed) start of the #GList
293 	 */
294 	public ListG deleteLink(ListG link)
295 	{
296 		auto p = g_list_delete_link(gList, (link is null) ? null : link.getListGStruct());
297 		
298 		if(p is null)
299 		{
300 			return null;
301 		}
302 		
303 		return new ListG(cast(GList*) p);
304 	}
305 
306 	/**
307 	 * Finds the element in a #GList which contains the given data.
308 	 *
309 	 * Params:
310 	 *     data = the element data to find
311 	 *
312 	 * Return: the found #GList element, or %NULL if it is not found
313 	 */
314 	public ListG find(void* data)
315 	{
316 		auto p = g_list_find(gList, data);
317 		
318 		if(p is null)
319 		{
320 			return null;
321 		}
322 		
323 		return new ListG(cast(GList*) p);
324 	}
325 
326 	/**
327 	 * Finds an element in a #GList, using a supplied function to
328 	 * find the desired element. It iterates over the list, calling
329 	 * the given function which should return 0 when the desired
330 	 * element is found. The function takes two #gconstpointer arguments,
331 	 * the #GList element's data as the first argument and the
332 	 * given user data.
333 	 *
334 	 * Params:
335 	 *     data = user data passed to the function
336 	 *     func = the function to call for each element.
337 	 *         It should return 0 when the desired element is found
338 	 *
339 	 * Return: the found #GList element, or %NULL if it is not found
340 	 */
341 	public ListG findCustom(void* data, GCompareFunc func)
342 	{
343 		auto p = g_list_find_custom(gList, data, func);
344 		
345 		if(p is null)
346 		{
347 			return null;
348 		}
349 		
350 		return new ListG(cast(GList*) p);
351 	}
352 
353 	/**
354 	 * Gets the first element in a #GList.
355 	 *
356 	 * Return: the first element in the #GList,
357 	 *     or %NULL if the #GList has no elements
358 	 */
359 	public ListG first()
360 	{
361 		auto p = g_list_first(gList);
362 		
363 		if(p is null)
364 		{
365 			return null;
366 		}
367 		
368 		return new ListG(cast(GList*) p);
369 	}
370 
371 	/**
372 	 * Calls a function for each element of a #GList.
373 	 *
374 	 * Params:
375 	 *     func = the function to call with each element's data
376 	 *     userData = user data to pass to the function
377 	 */
378 	public void foreac(GFunc func, void* userData)
379 	{
380 		g_list_foreach(gList, func, userData);
381 	}
382 
383 	/**
384 	 * Frees all of the memory used by a #GList.
385 	 * The freed elements are returned to the slice allocator.
386 	 *
387 	 * If list elements contain dynamically-allocated memory, you should
388 	 * either use g_list_free_full() or free them manually first.
389 	 */
390 	public void free()
391 	{
392 		g_list_free(gList);
393 	}
394 
395 	/**
396 	 * Frees one #GList element, but does not update links from the next and
397 	 * previous elements in the list, so you should not call this function on an
398 	 * element that is currently part of a list.
399 	 *
400 	 * It is usually used after g_list_remove_link().
401 	 */
402 	public void free1()
403 	{
404 		g_list_free_1(gList);
405 	}
406 
407 	/**
408 	 * Convenience method, which frees all the memory used by a #GList,
409 	 * and calls @free_func on every element's data.
410 	 *
411 	 * Params:
412 	 *     freeFunc = the function to be called to free each element's data
413 	 *
414 	 * Since: 2.28
415 	 */
416 	public void freeFull(GDestroyNotify freeFunc)
417 	{
418 		g_list_free_full(gList, freeFunc);
419 	}
420 
421 	/**
422 	 * Gets the position of the element containing
423 	 * the given data (starting from 0).
424 	 *
425 	 * Params:
426 	 *     data = the data to find
427 	 *
428 	 * Return: the index of the element containing the data,
429 	 *     or -1 if the data is not found
430 	 */
431 	public int index(void* data)
432 	{
433 		return g_list_index(gList, data);
434 	}
435 
436 	/**
437 	 * Inserts a new element into the list at the given position.
438 	 *
439 	 * Params:
440 	 *     data = the data for the new element
441 	 *     position = the position to insert the element. If this is
442 	 *         negative, or is larger than the number of elements in the
443 	 *         list, the new element is added on to the end of the list.
444 	 *
445 	 * Return: the (possibly changed) start of the #GList
446 	 */
447 	public ListG insert(void* data, int position)
448 	{
449 		auto p = g_list_insert(gList, data, position);
450 		
451 		if(p is null)
452 		{
453 			return null;
454 		}
455 		
456 		return new ListG(cast(GList*) p);
457 	}
458 
459 	/**
460 	 * Inserts a new element into the list before the given position.
461 	 *
462 	 * Params:
463 	 *     sibling = the list element before which the new element
464 	 *         is inserted or %NULL to insert at the end of the list
465 	 *     data = the data for the new element
466 	 *
467 	 * Return: the (possibly changed) start of the #GList
468 	 */
469 	public ListG insertBefore(ListG sibling, void* data)
470 	{
471 		auto p = g_list_insert_before(gList, (sibling is null) ? null : sibling.getListGStruct(), data);
472 		
473 		if(p is null)
474 		{
475 			return null;
476 		}
477 		
478 		return new ListG(cast(GList*) p);
479 	}
480 
481 	/**
482 	 * Inserts a new element into the list, using the given comparison
483 	 * function to determine its position.
484 	 *
485 	 * If you are adding many new elements to a list, and the number of
486 	 * new elements is much larger than the length of the list, use
487 	 * g_list_prepend() to add the new items and sort the list afterwards
488 	 * with g_list_sort().
489 	 *
490 	 * Params:
491 	 *     data = the data for the new element
492 	 *     func = the function to compare elements in the list. It should
493 	 *         return a number > 0 if the first parameter comes after the
494 	 *         second parameter in the sort order.
495 	 *
496 	 * Return: the (possibly changed) start of the #GList
497 	 */
498 	public ListG insertSorted(void* data, GCompareFunc func)
499 	{
500 		auto p = g_list_insert_sorted(gList, data, func);
501 		
502 		if(p is null)
503 		{
504 			return null;
505 		}
506 		
507 		return new ListG(cast(GList*) p);
508 	}
509 
510 	/**
511 	 * Inserts a new element into the list, using the given comparison
512 	 * function to determine its position.
513 	 *
514 	 * If you are adding many new elements to a list, and the number of
515 	 * new elements is much larger than the length of the list, use
516 	 * g_list_prepend() to add the new items and sort the list afterwards
517 	 * with g_list_sort().
518 	 *
519 	 * Params:
520 	 *     data = the data for the new element
521 	 *     func = the function to compare elements in the list. It should
522 	 *         return a number > 0 if the first parameter  comes after the
523 	 *         second parameter in the sort order.
524 	 *     userData = user data to pass to comparison function
525 	 *
526 	 * Return: the (possibly changed) start of the #GList
527 	 *
528 	 * Since: 2.10
529 	 */
530 	public ListG insertSortedWithData(void* data, GCompareDataFunc func, void* userData)
531 	{
532 		auto p = g_list_insert_sorted_with_data(gList, data, func, userData);
533 		
534 		if(p is null)
535 		{
536 			return null;
537 		}
538 		
539 		return new ListG(cast(GList*) p);
540 	}
541 
542 	/**
543 	 * Gets the last element in a #GList.
544 	 *
545 	 * Return: the last element in the #GList,
546 	 *     or %NULL if the #GList has no elements
547 	 */
548 	public ListG last()
549 	{
550 		auto p = g_list_last(gList);
551 		
552 		if(p is null)
553 		{
554 			return null;
555 		}
556 		
557 		return new ListG(cast(GList*) p);
558 	}
559 
560 	/**
561 	 * Gets the number of elements in a #GList.
562 	 *
563 	 * This function iterates over the whole list to count its elements.
564 	 * Use a #GQueue instead of a GList if you regularly need the number
565 	 * of items. To check whether the list is non-empty, it is faster to check
566 	 * @list against %NULL.
567 	 *
568 	 * Return: the number of elements in the #GList
569 	 */
570 	public uint length()
571 	{
572 		return g_list_length(gList);
573 	}
574 
575 	/**
576 	 * Gets the element at the given position in a #GList.
577 	 *
578 	 * This iterates over the list until it reaches the @n-th position. If you
579 	 * intend to iterate over every element, it is better to use a for-loop as
580 	 * described in the #GList introduction.
581 	 *
582 	 * Params:
583 	 *     n = the position of the element, counting from 0
584 	 *
585 	 * Return: the element, or %NULL if the position is off
586 	 *     the end of the #GList
587 	 */
588 	public ListG nth(uint n)
589 	{
590 		auto p = g_list_nth(gList, n);
591 		
592 		if(p is null)
593 		{
594 			return null;
595 		}
596 		
597 		return new ListG(cast(GList*) p);
598 	}
599 
600 	/**
601 	 * Gets the data of the element at the given position.
602 	 *
603 	 * This iterates over the list until it reaches the @n-th position. If you
604 	 * intend to iterate over every element, it is better to use a for-loop as
605 	 * described in the #GList introduction.
606 	 *
607 	 * Params:
608 	 *     n = the position of the element
609 	 *
610 	 * Return: the element's data, or %NULL if the position
611 	 *     is off the end of the #GList
612 	 */
613 	public void* nthData(uint n)
614 	{
615 		return g_list_nth_data(gList, n);
616 	}
617 
618 	/**
619 	 * Gets the element @n places before @list.
620 	 *
621 	 * Params:
622 	 *     n = the position of the element, counting from 0
623 	 *
624 	 * Return: the element, or %NULL if the position is
625 	 *     off the end of the #GList
626 	 */
627 	public ListG nthPrev(uint n)
628 	{
629 		auto p = g_list_nth_prev(gList, n);
630 		
631 		if(p is null)
632 		{
633 			return null;
634 		}
635 		
636 		return new ListG(cast(GList*) p);
637 	}
638 
639 	/**
640 	 * Gets the position of the given element
641 	 * in the #GList (starting from 0).
642 	 *
643 	 * Params:
644 	 *     llink = an element in the #GList
645 	 *
646 	 * Return: the position of the element in the #GList,
647 	 *     or -1 if the element is not found
648 	 */
649 	public int position(ListG llink)
650 	{
651 		return g_list_position(gList, (llink is null) ? null : llink.getListGStruct());
652 	}
653 
654 	/**
655 	 * Prepends a new element on to the start of the list.
656 	 *
657 	 * Note that the return value is the new start of the list,
658 	 * which will have changed, so make sure you store the new value.
659 	 *
660 	 * |[<!-- language="C" -->
661 	 * // Notice that it is initialized to the empty list.
662 	 * GList *list = NULL;
663 	 *
664 	 * list = g_list_prepend (list, "last");
665 	 * list = g_list_prepend (list, "first");
666 	 * ]|
667 	 *
668 	 * Do not use this function to prepend a new element to a different
669 	 * element than the start of the list. Use g_list_insert_before() instead.
670 	 *
671 	 * Params:
672 	 *     data = the data for the new element
673 	 *
674 	 * Return: a pointer to the newly prepended element, which is the new
675 	 *     start of the #GList
676 	 */
677 	public ListG prepend(void* data)
678 	{
679 		auto p = g_list_prepend(gList, data);
680 		
681 		if(p is null)
682 		{
683 			return null;
684 		}
685 		
686 		return new ListG(cast(GList*) p);
687 	}
688 
689 	/**
690 	 * Removes an element from a #GList.
691 	 * If two elements contain the same data, only the first is removed.
692 	 * If none of the elements contain the data, the #GList is unchanged.
693 	 *
694 	 * Params:
695 	 *     data = the data of the element to remove
696 	 *
697 	 * Return: the (possibly changed) start of the #GList
698 	 */
699 	public ListG remove(void* data)
700 	{
701 		auto p = g_list_remove(gList, data);
702 		
703 		if(p is null)
704 		{
705 			return null;
706 		}
707 		
708 		return new ListG(cast(GList*) p);
709 	}
710 
711 	/**
712 	 * Removes all list nodes with data equal to @data.
713 	 * Returns the new head of the list. Contrast with
714 	 * g_list_remove() which removes only the first node
715 	 * matching the given data.
716 	 *
717 	 * Params:
718 	 *     data = data to remove
719 	 *
720 	 * Return: the (possibly changed) start of the #GList
721 	 */
722 	public ListG removeAll(void* data)
723 	{
724 		auto p = g_list_remove_all(gList, data);
725 		
726 		if(p is null)
727 		{
728 			return null;
729 		}
730 		
731 		return new ListG(cast(GList*) p);
732 	}
733 
734 	/**
735 	 * Removes an element from a #GList, without freeing the element.
736 	 * The removed element's prev and next links are set to %NULL, so
737 	 * that it becomes a self-contained list with one element.
738 	 *
739 	 * This function is for example used to move an element in the list
740 	 * (see the example for g_list_concat()) or to remove an element in
741 	 * the list before freeing its data:
742 	 * |[<!-- language="C" -->
743 	 * list = g_list_remove_link (list, llink);
744 	 * free_some_data_that_may_access_the_list_again (llink->data);
745 	 * g_list_free (llink);
746 	 * ]|
747 	 *
748 	 * Params:
749 	 *     llink = an element in the #GList
750 	 *
751 	 * Return: the (possibly changed) start of the #GList
752 	 */
753 	public ListG removeLink(ListG llink)
754 	{
755 		auto p = g_list_remove_link(gList, (llink is null) ? null : llink.getListGStruct());
756 		
757 		if(p is null)
758 		{
759 			return null;
760 		}
761 		
762 		return new ListG(cast(GList*) p);
763 	}
764 
765 	/**
766 	 * Reverses a #GList.
767 	 * It simply switches the next and prev pointers of each element.
768 	 *
769 	 * Return: the start of the reversed #GList
770 	 */
771 	public ListG reverse()
772 	{
773 		auto p = g_list_reverse(gList);
774 		
775 		if(p is null)
776 		{
777 			return null;
778 		}
779 		
780 		return new ListG(cast(GList*) p);
781 	}
782 
783 	/**
784 	 * Sorts a #GList using the given comparison function. The algorithm
785 	 * used is a stable sort.
786 	 *
787 	 * Params:
788 	 *     compareFunc = the comparison function used to sort the #GList.
789 	 *         This function is passed the data from 2 elements of the #GList
790 	 *         and should return 0 if they are equal, a negative value if the
791 	 *         first element comes before the second, or a positive value if
792 	 *         the first element comes after the second.
793 	 *
794 	 * Return: the (possibly changed) start of the #GList
795 	 */
796 	public ListG sort(GCompareFunc compareFunc)
797 	{
798 		auto p = g_list_sort(gList, compareFunc);
799 		
800 		if(p is null)
801 		{
802 			return null;
803 		}
804 		
805 		return new ListG(cast(GList*) p);
806 	}
807 
808 	/**
809 	 * Like g_list_sort(), but the comparison function accepts
810 	 * a user data argument.
811 	 *
812 	 * Params:
813 	 *     compareFunc = comparison function
814 	 *     userData = user data to pass to comparison function
815 	 *
816 	 * Return: the (possibly changed) start of the #GList
817 	 */
818 	public ListG sortWithData(GCompareDataFunc compareFunc, void* userData)
819 	{
820 		auto p = g_list_sort_with_data(gList, compareFunc, userData);
821 		
822 		if(p is null)
823 		{
824 			return null;
825 		}
826 		
827 		return new ListG(cast(GList*) p);
828 	}
829 }