home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / helpbrowser / queue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-28  |  3.7 KB  |  184 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * The GIMP Help Browser
  5.  * Copyright (C) 1999 Sven Neumann <sven@gimp.org>
  6.  *                    Michael Natterer <mitschel@cs.tu-berlin.de>
  7.  *
  8.  * Some code & ideas stolen from the GNOME help browser.
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23.  */
  24.  
  25. #include "queue.h"
  26.  
  27. struct _Queue
  28. {
  29.   GList *queue;
  30.   GList *current;
  31. };
  32.  
  33. typedef struct _QueueElement QueueElement;
  34.  
  35. struct _QueueElement
  36. {
  37.   gchar *ref;
  38.   gint   pos;
  39. };
  40.  
  41. Queue *
  42. queue_new (void)
  43. {
  44.   Queue *h;
  45.  
  46.   h = g_malloc (sizeof (Queue));
  47.   
  48.   h->queue = NULL;
  49.   h->current = NULL;
  50.  
  51.   return (h);
  52. }
  53.  
  54. static void
  55. queue_free_element (QueueElement *el,
  56.             gpointer      foo)
  57. {
  58.   g_free (el->ref);
  59.   g_free (el);
  60. }
  61.  
  62. void
  63. queue_free (Queue *h)
  64. {
  65.   g_return_if_fail (h != NULL);
  66.  
  67.   /* needs to free data in list as well! */
  68.   if (h->queue)
  69.     {
  70.       g_list_foreach (h->queue, (GFunc) queue_free_element, NULL);
  71.       g_list_free (h->queue);
  72.     }
  73.  
  74.   g_free (h);
  75. }
  76.  
  77. void
  78. queue_move_prev (Queue *h)
  79. {
  80.   if (!h || !h->queue || (h->current == g_list_first (h->queue)))
  81.     return;
  82.  
  83.   h->current = g_list_previous (h->current);
  84. }
  85.  
  86. void
  87. queue_move_next (Queue *h)
  88. {
  89.   if (!h || !h->queue || (h->current == g_list_last (h->queue)))
  90.     return;
  91.  
  92.   h->current = g_list_next (h->current);
  93. }
  94.  
  95. gchar *
  96. queue_prev (Queue *h,
  97.         gint  *pos)
  98. {
  99.   GList *p;
  100.  
  101.   if (!h || !h->queue || (h->current == g_list_first (h->queue)))
  102.     return NULL;
  103.  
  104.   p = g_list_previous (h->current);
  105.     
  106.   if (pos)
  107.     *pos = ((QueueElement *)p->data)->pos;
  108.  
  109.   return ((QueueElement *)p->data)->ref;
  110. }
  111.  
  112. gchar *
  113. queue_next (Queue *h,
  114.         gint  *pos)
  115. {
  116.   GList *p;
  117.  
  118.   if (!h || !h->queue || (h->current == g_list_last(h->queue)))
  119.     return NULL;
  120.  
  121.   p = g_list_next (h->current);
  122.  
  123.   if (pos)
  124.     *pos = ((QueueElement *)p->data)->pos;
  125.  
  126.   return ((QueueElement *)p->data)->ref;
  127. }
  128.  
  129. void 
  130. queue_mark_current (Queue *h,
  131.             gint   pos)
  132. {
  133.   if (h->current)
  134.     ((QueueElement *)(h->current->data))->pos = pos;
  135. }
  136.  
  137. void 
  138. queue_add (Queue *h,
  139.        gchar *ref,
  140.        gint   pos)
  141. {
  142.   GList *trash=NULL;
  143.   QueueElement *el;
  144.  
  145.   g_return_if_fail (h != NULL);
  146.   g_return_if_fail (ref != NULL);
  147.  
  148.   if (h->current)
  149.     {
  150.       trash = h->current->next;
  151.       h->current->next = NULL;
  152.     }
  153.  
  154.   el = g_malloc (sizeof (*el));
  155.   el->pos = pos;
  156.   el->ref = g_strdup (ref);
  157.   h->queue = g_list_append (h->queue, el);
  158.   h->current = g_list_last (h->queue);
  159.  
  160.   if (trash)
  161.     {
  162.       g_list_foreach (trash, (GFunc)queue_free_element, NULL);
  163.       g_list_free (trash);
  164.     }    
  165. }
  166.  
  167. gboolean
  168. queue_isnext (Queue *h)
  169. {
  170.   if (!h || !h->queue || (h->current == g_list_last (h->queue)))
  171.     return FALSE;
  172.   
  173.   return (g_list_next(h->current) != NULL);
  174. }
  175.  
  176. gboolean
  177. queue_isprev (Queue *h)
  178. {
  179.   if (!h || !h->queue || (h->current == g_list_first (h->queue)))
  180.     return FALSE;
  181.   
  182.   return (g_list_previous (h->current) != NULL);
  183. }
  184.