File: gtksizerequestcache.c

package info (click to toggle)
gtk%2B3.0 3.22.11-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 182,828 kB
  • ctags: 82,378
  • sloc: ansic: 1,165,043; xml: 9,259; makefile: 6,914; sh: 5,179; python: 402; perl: 370; cpp: 34; sed: 16
file content (299 lines) | stat: -rw-r--r-- 9,109 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* gtksizerequest.c
 * Copyright (C) 2007-2010 Openismus GmbH
 *
 * Authors:
 *      Mathias Hasselmann <mathias@openismus.com>
 *      Tristan Van Berkom <tristan.van.berkom@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "gtksizerequestcacheprivate.h"

#include <string.h>

void
_gtk_size_request_cache_init (SizeRequestCache *cache)
{
  memset (cache, 0, sizeof (SizeRequestCache));
}

static void
free_sizes_x (SizeRequestX **sizes)
{
  gint i;

  for (i = 0; i < GTK_SIZE_REQUEST_CACHED_SIZES && sizes[i] != NULL; i++)
    g_slice_free (SizeRequestX, sizes[i]);

  g_slice_free1 (sizeof (SizeRequestY *) * GTK_SIZE_REQUEST_CACHED_SIZES, sizes);
}

static void
free_sizes_y (SizeRequestY **sizes)
{
  gint i;

  for (i = 0; i < GTK_SIZE_REQUEST_CACHED_SIZES && sizes[i] != NULL; i++)
    g_slice_free (SizeRequestY, sizes[i]);

  g_slice_free1 (sizeof (SizeRequestY *) * GTK_SIZE_REQUEST_CACHED_SIZES, sizes);
}

void
_gtk_size_request_cache_free (SizeRequestCache *cache)
{
  if (cache->requests_x)
    free_sizes_x (cache->requests_x);
  if (cache->requests_y)
    free_sizes_y (cache->requests_y);
}

void
_gtk_size_request_cache_clear (SizeRequestCache *cache)
{
  _gtk_size_request_cache_free (cache);
  _gtk_size_request_cache_init (cache);
}

void
_gtk_size_request_cache_commit (SizeRequestCache *cache,
                                GtkOrientation    orientation,
                                gint              for_size,
                                gint              minimum_size,
                                gint              natural_size,
				gint              minimum_baseline,
				gint              natural_baseline)
{
  guint         i, n_sizes;

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      g_assert (minimum_baseline == -1);
      g_assert (natural_baseline == -1);
    }

  /* First handle caching of the base requests */
  if (for_size < 0)
    {
      if (orientation == GTK_ORIENTATION_HORIZONTAL)
	{
	  cache->cached_size_x.minimum_size = minimum_size;
	  cache->cached_size_x.natural_size = natural_size;
	}
      else
	{
	  cache->cached_size_y.minimum_size = minimum_size;
	  cache->cached_size_y.natural_size = natural_size;
	  cache->cached_size_y.minimum_baseline = minimum_baseline;
	  cache->cached_size_y.natural_baseline = natural_baseline;
	}

      cache->flags[orientation].cached_size_valid = TRUE;
      return;
    }

  /* Check if the minimum_size and natural_size is already
   * in the cache and if this result can be used to extend
   * that cache entry 
   */
  n_sizes = cache->flags[orientation].n_cached_requests;


  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      SizeRequestX **cached_sizes;
      SizeRequestX  *cached_size;
      cached_sizes = cache->requests_x;

      for (i = 0; i < n_sizes; i++)
	{
	  if (cached_sizes[i]->cached_size.minimum_size == minimum_size &&
	      cached_sizes[i]->cached_size.natural_size == natural_size)
	    {
	      cached_sizes[i]->lower_for_size = MIN (cached_sizes[i]->lower_for_size, for_size);
	      cached_sizes[i]->upper_for_size = MAX (cached_sizes[i]->upper_for_size, for_size);
	      return;
	    }
	}

      /* If not found, pull a new size from the cache, the returned size cache
       * will immediately be used to cache the new computed size so we go ahead
       * and increment the last_cached_request right away */
      if (n_sizes < GTK_SIZE_REQUEST_CACHED_SIZES)
	{
	  cache->flags[orientation].n_cached_requests++;
	  cache->flags[orientation].last_cached_request = cache->flags[orientation].n_cached_requests - 1;
	}
      else
	{
	  if (++cache->flags[orientation].last_cached_request == GTK_SIZE_REQUEST_CACHED_SIZES)
	    cache->flags[orientation].last_cached_request = 0;
	}

      if (cache->requests_x == NULL)
	cache->requests_x = g_slice_alloc0 (sizeof (SizeRequestX *) * GTK_SIZE_REQUEST_CACHED_SIZES);

      if (cache->requests_x[cache->flags[orientation].last_cached_request] == NULL)
	cache->requests_x[cache->flags[orientation].last_cached_request] = g_slice_new (SizeRequestX);

      cached_size = cache->requests_x[cache->flags[orientation].last_cached_request];
      cached_size->lower_for_size = for_size;
      cached_size->upper_for_size = for_size;
      cached_size->cached_size.minimum_size = minimum_size;
      cached_size->cached_size.natural_size = natural_size;
    }
  else
    {
      SizeRequestY **cached_sizes;
      SizeRequestY  *cached_size;
      cached_sizes = cache->requests_y;

      for (i = 0; i < n_sizes; i++)
	{
	  if (cached_sizes[i]->cached_size.minimum_size == minimum_size &&
	      cached_sizes[i]->cached_size.natural_size == natural_size &&
	      cached_sizes[i]->cached_size.minimum_baseline == minimum_baseline &&
	      cached_sizes[i]->cached_size.natural_baseline == natural_baseline)
	    {
	      cached_sizes[i]->lower_for_size = MIN (cached_sizes[i]->lower_for_size, for_size);
	      cached_sizes[i]->upper_for_size = MAX (cached_sizes[i]->upper_for_size, for_size);
	      return;
	    }
	}

      /* If not found, pull a new size from the cache, the returned size cache
       * will immediately be used to cache the new computed size so we go ahead
       * and increment the last_cached_request right away */
      if (n_sizes < GTK_SIZE_REQUEST_CACHED_SIZES)
	{
	  cache->flags[orientation].n_cached_requests++;
	  cache->flags[orientation].last_cached_request = cache->flags[orientation].n_cached_requests - 1;
	}
      else
	{
	  if (++cache->flags[orientation].last_cached_request == GTK_SIZE_REQUEST_CACHED_SIZES)
	    cache->flags[orientation].last_cached_request = 0;
	}

      if (cache->requests_y == NULL)
	cache->requests_y = g_slice_alloc0 (sizeof (SizeRequestY *) * GTK_SIZE_REQUEST_CACHED_SIZES);

      if (cache->requests_y[cache->flags[orientation].last_cached_request] == NULL)
	cache->requests_y[cache->flags[orientation].last_cached_request] = g_slice_new (SizeRequestY);

      cached_size = cache->requests_y[cache->flags[orientation].last_cached_request];
      cached_size->lower_for_size = for_size;
      cached_size->upper_for_size = for_size;
      cached_size->cached_size.minimum_size = minimum_size;
      cached_size->cached_size.natural_size = natural_size;
      cached_size->cached_size.minimum_baseline = minimum_baseline;
      cached_size->cached_size.natural_baseline = natural_baseline;
    }
}

/* looks for a cached size request for this for_size.
 *
 * Note that this caching code was originally derived from
 * the Clutter toolkit but has evolved for other GTK+ requirements.
 */
gboolean
_gtk_size_request_cache_lookup (SizeRequestCache *cache,
                                GtkOrientation    orientation,
                                gint              for_size,
                                gint             *minimum,
                                gint             *natural,
				gint             *minimum_baseline,
				gint             *natural_baseline)
{
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      CachedSizeX *result = NULL;

      if (for_size < 0)
	{
	  if (cache->flags[orientation].cached_size_valid)
	    result = &cache->cached_size_x;
	}
      else
	{
	  guint i;

	  /* Search for an already cached size */
	  for (i = 0; i < cache->flags[orientation].n_cached_requests; i++)
	    {
	      SizeRequestX *cur = cache->requests_x[i];

	      if (cur->lower_for_size <= for_size &&
		  cur->upper_for_size >= for_size)
		{
		  result = &cur->cached_size;
		  break;
		}
	    }
	}

      if (result)
	{
	  *minimum = result->minimum_size;
	  *natural = result->natural_size;
	  *minimum_baseline = -1;
	  *natural_baseline = -1;
	  return TRUE;
	}
      else
	return FALSE;
    }
  else
    {
      CachedSizeY *result = NULL;

      if (for_size < 0)
	{
	  if (cache->flags[orientation].cached_size_valid)
	    result = &cache->cached_size_y;
	}
      else
	{
	  guint i;

	  /* Search for an already cached size */
	  for (i = 0; i < cache->flags[orientation].n_cached_requests; i++)
	    {
	      SizeRequestY *cur = cache->requests_y[i];

	      if (cur->lower_for_size <= for_size &&
		  cur->upper_for_size >= for_size)
		{
		  result = &cur->cached_size;
		  break;
		}
	    }
	}

      if (result)
	{
	  *minimum = result->minimum_size;
	  *natural = result->natural_size;
	  *minimum_baseline = result->minimum_baseline;
	  *natural_baseline = result->natural_baseline;
	  return TRUE;
	}
      else
	return FALSE;
    }
}