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