home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / liblayer / src / cl_group.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.8 KB  |  266 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.  *  cl_group.c - Layer group API
  20.  */
  21.  
  22.  
  23. #include "prtypes.h"
  24. #include "plhash.h"
  25. #include "xp.h"
  26. #include "layers.h"
  27. #include "cl_priv.h"
  28.  
  29.  
  30. /* 
  31.  * BUGBUG For now we use this local version to has hash group 
  32.  * names. Use the canned NSPR function when it gets checked in.
  33.  */
  34. static PRHashNumber
  35. cl_hash_string(const void *key)
  36. {
  37.     PRHashNumber h;
  38.     const unsigned char *s;
  39.  
  40.     h = 0;
  41.     for (s = key; *s; s++)
  42.         h = (h >> 28) ^ (h << 4) ^ *s;
  43.     return h;
  44. }
  45.  
  46. static int
  47. cl_compare_strings(const void *key1, const void *key2)
  48. {
  49.     return strcmp(key1, key2) == 0;
  50. }
  51.  
  52. static int
  53. cl_compare_values(const void *value1, const void *value2)
  54. {
  55.     return value1 == value2;
  56. }
  57.  
  58. PRBool 
  59. CL_AddLayerToGroup(CL_Compositor *compositor,
  60.                    CL_Layer *layer,
  61.                    char *name)
  62. {
  63.     XP_List *group_list;
  64.     PRBool ret_val = PR_FALSE;
  65.  
  66.     XP_ASSERT(compositor);
  67.     XP_ASSERT(layer);
  68.     XP_ASSERT(name);
  69.  
  70.     if (!compositor || !layer || !name)
  71.         return PR_FALSE;
  72.     
  73.     if (compositor->group_table == NULL){
  74.         compositor->group_table = PR_NewHashTable(CL_GROUP_HASH_TABLE_SIZE,
  75.                                                   cl_hash_string,
  76.                                                   cl_compare_strings,
  77.                                                   cl_compare_values,
  78.                                                   NULL, NULL);
  79.  
  80.         if (compositor->group_table == NULL)
  81.             return PR_FALSE;
  82.     }
  83.     
  84.     if ((group_list = (XP_List *)PR_HashTableLookup(compositor->group_table, 
  85.                                                     name)) == NULL){
  86.         group_list = XP_ListNew();
  87.         if (group_list == NULL)
  88.             return PR_FALSE;
  89.         
  90.         PR_HashTableAdd(compositor->group_table, name, group_list);
  91.         ret_val = PR_TRUE;
  92.     }
  93.     
  94.     XP_ListAddObject(group_list, layer);
  95.     
  96.     return ret_val;
  97. }
  98.  
  99. void
  100. CL_RemoveLayerFromGroup(CL_Compositor *compositor,
  101.                         CL_Layer *layer,
  102.                         char *name)
  103. {
  104.     XP_List *group_list;
  105.     
  106.     XP_ASSERT(compositor);
  107.     XP_ASSERT(layer);
  108.     XP_ASSERT(name);
  109.     
  110.     if (!compositor || !layer || !name)
  111.         return;
  112.     
  113.     if (compositor->group_table == NULL)
  114.         return;
  115.     
  116.     if ((group_list = (XP_List *)PR_HashTableLookup(compositor->group_table, 
  117.                                                     name)) == NULL)
  118.         return;
  119.     
  120.     /* Remove the layer from the group list */
  121.     XP_ListRemoveObject(group_list, layer);
  122.  
  123.     /* If the list is empty, get rid of it */
  124.     if (XP_ListIsEmpty(group_list)){
  125.         PR_HashTableRemove(compositor->group_table, name);
  126.         XP_ListDestroy(group_list);
  127.     }
  128. }
  129.  
  130. void
  131. CL_DestroyLayerGroup(CL_Compositor *compositor,
  132.                      char *name)
  133. {
  134.     XP_List *group_list;
  135.     
  136.     XP_ASSERT(compositor);
  137.     XP_ASSERT(name);
  138.  
  139.     if (!compositor || !name)
  140.         return;
  141.     
  142.     if (compositor->group_table == NULL)
  143.         return;
  144.     
  145.     if ((group_list = (XP_List *)PR_HashTableLookup(compositor->group_table, 
  146.                                                     name)) == NULL)
  147.         return;
  148.     
  149.     /* Get rid of the list */
  150.     PR_HashTableRemove(compositor->group_table, name);
  151.     XP_ListDestroy(group_list);
  152. }
  153.  
  154.     
  155. /* 
  156.  * BUGBUG CL_HideLayerGroup, CL_UnhideLayerGroup and 
  157.  * CL_OffsetLayerGroup can all be implemented using
  158.  * CL_ForEachLayerInGroup...and they should be. Cutting
  159.  * and pasting was just the easy way out.
  160.  */
  161. void
  162. CL_HideLayerGroup(CL_Compositor *compositor, char *name)
  163. {
  164.     XP_List *group_list;
  165.     CL_Layer *layer;
  166.     
  167.     XP_ASSERT(compositor);
  168.     XP_ASSERT(name);
  169.     
  170.     if (!compositor || !name)
  171.         return;
  172.     
  173.     if (compositor->group_table == NULL)
  174.         return;
  175.  
  176.     group_list = (XP_List *)PR_HashTableLookup(compositor->group_table, 
  177.                                                name);
  178.     if (group_list == 0)
  179.         return;
  180.     
  181.     while ((layer = XP_ListNextObject(group_list)))
  182.         CL_SetLayerHidden(layer, PR_TRUE);
  183. }
  184.  
  185. void 
  186. CL_OffsetLayerGroup(CL_Compositor *compositor, char *name, 
  187.                     int32 x_offset, int32 y_offset)
  188. {
  189.     XP_List *group_list;
  190.     CL_Layer *layer;
  191.     
  192.     XP_ASSERT(compositor);
  193.     XP_ASSERT(name);
  194.     
  195.     if (!compositor || !name)
  196.         return;
  197.     
  198.     if (compositor->group_table == NULL)
  199.         return;
  200.  
  201.     group_list = (XP_List *)PR_HashTableLookup(compositor->group_table, 
  202.                                                name);
  203.     if (group_list == 0)
  204.         return;
  205.     
  206.     while ((layer = XP_ListNextObject(group_list)))
  207.         CL_OffsetLayer(layer, x_offset, y_offset);
  208. }
  209.  
  210. void
  211. CL_UnhideLayerGroup(CL_Compositor *compositor, char *name)
  212. {
  213.     XP_List *group_list;
  214.     CL_Layer *layer;
  215.     
  216.     XP_ASSERT(compositor);
  217.     XP_ASSERT(name);
  218.     
  219.     if (!compositor || !name)
  220.         return;
  221.  
  222.     if (compositor->group_table == NULL)
  223.         return;
  224.  
  225.     group_list = (XP_List *)PR_HashTableLookup(compositor->group_table, 
  226.                                                name);
  227.     if (group_list == 0)
  228.         return;
  229.     
  230.     while ((layer = XP_ListNextObject(group_list)))
  231.         CL_SetLayerHidden(layer, PR_FALSE);
  232. }
  233.  
  234.  
  235. PRBool
  236. CL_ForEachLayerInGroup(CL_Compositor *compositor,
  237.                        char *name,
  238.                        CL_LayerInGroupFunc func,
  239.                        void *closure)
  240. {
  241.     PRBool done;
  242.     XP_List *group_list;
  243.     CL_Layer *layer;
  244.     
  245.     XP_ASSERT(compositor);
  246.     XP_ASSERT(name);
  247.     
  248.     if (!compositor || !name)
  249.         return PR_FALSE;
  250.  
  251.     if (compositor->group_table == NULL)
  252.         return PR_TRUE;
  253.  
  254.     group_list = (XP_List *)PR_HashTableLookup(compositor->group_table, 
  255.                                                name);
  256.     if (group_list == 0)
  257.         return PR_TRUE;
  258.     
  259.     while ((layer = XP_ListNextObject(group_list))) {
  260.         done = (PRBool)!(*func)(layer, closure);
  261.         if (done) return PR_FALSE;
  262.     }
  263.     
  264.     return PR_TRUE;
  265. }
  266.