File: x_gdiobj_pool.c

package info (click to toggle)
mlterm 3.1.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 23,168 kB
  • sloc: ansic: 102,795; sh: 9,676; java: 2,018; perl: 1,601; makefile: 1,595; cpp: 771; sed: 16
file content (246 lines) | stat: -rw-r--r-- 3,736 bytes parent folder | download | duplicates (3)
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
/*
 *	$Id$
 */

#include  "x_gdiobj_pool.h"

#include  <kiklib/kik_mem.h>


typedef struct  stock_pen
{
	HPEN  pen ;
	u_long  rgb ;
	int  ref_count ;

} stock_pen_t ;

typedef struct  stock_brush
{
	HBRUSH  brush ;
	u_long  rgb ;
	int  ref_count ;

} stock_brush_t ;


/* --- static variables --- */

static stock_pen_t *  stock_pens ;
static u_int  num_of_stock_pens ;
static stock_brush_t *  stock_brushes ;
static u_int  num_of_stock_brushes ;


/* --- static functions --- */

static int
garbage_unused_objects(void)
{
	int  count ;

	for( count = 0 ; count < num_of_stock_pens ;)
	{
		if( stock_pens[count].ref_count <= 0)
		{
			DeleteObject( stock_pens[count].pen) ;
			stock_pens[count] = stock_pens[--num_of_stock_pens] ;
		}
		else
		{
			count ++ ;
		}
	}

	for( count = 0 ; count < num_of_stock_brushes ;)
	{
		if( stock_brushes[count].ref_count <= 0)
		{
			DeleteObject( stock_brushes[count].brush) ;
			stock_brushes[count] = stock_brushes[--num_of_stock_brushes] ;
		}
		else
		{
			count ++ ;
		}
	}

	return  1 ;
}


/* --- global functions --- */

int
x_gdiobj_pool_init(void)
{
	return  1 ;
}

int
x_gdiobj_pool_final(void)
{
	u_int  count ;

	for( count = 0 ; count < num_of_stock_pens ; count++)
	{
		DeleteObject( stock_pens[count].pen) ;
	}

	for( count = 0 ; count < num_of_stock_brushes ; count++)
	{
		DeleteObject( stock_brushes[count].brush) ;
	}

	free( stock_pens) ;
	free( stock_brushes) ;

	return  1 ;
}

HPEN
x_acquire_pen(
	u_long  rgb
	)
{
	u_int  count ;

	/* Remove alpha */
	rgb &= 0xffffff ;

	for( count = 0 ; count < num_of_stock_pens ; count++)
	{
		if( rgb == stock_pens[count].rgb)
		{
			stock_pens[count].ref_count ++ ;

			return  stock_pens[count].pen ;
		}
	}

	if( rgb == RGB(0,0,0))
	{
		return  GetStockObject( BLACK_PEN) ;
	}
	else if( rgb == RGB(0xff,0xff,0xff))
	{
		return  GetStockObject( WHITE_PEN) ;
	}
	else
	{
		void *  p ;

		if( num_of_stock_pens % 10 == 9)
		{
			garbage_unused_objects() ;
		}

		if( ( p = realloc( stock_pens , sizeof( stock_pen_t) * (num_of_stock_pens + 1)))
			== NULL)
		{
			return  None ;
		}

		stock_pens = p ;

		stock_pens[num_of_stock_pens].rgb = rgb ;
		stock_pens[num_of_stock_pens].pen = CreatePen( PS_SOLID, 1, rgb) ;
		stock_pens[num_of_stock_pens].ref_count = 1 ;

		return  stock_pens[num_of_stock_pens++].pen ;
	}
}

int
x_release_pen(
	HPEN  pen
	)
{
	u_int  count ;

	for( count = 0 ; count < num_of_stock_pens ; count++)
	{
		if( pen == stock_pens[count].pen)
		{
			-- stock_pens[count].ref_count ;

			return  1 ;
		}
	}

	return  0 ;
}

HBRUSH
x_acquire_brush(
	u_long  rgb
	)
{
	u_int  count ;

	/* Remove alpha */
	rgb &= 0xffffff ;

	for( count = 0 ; count < num_of_stock_brushes ; count++)
	{
		if( rgb == stock_brushes[count].rgb)
		{
			stock_brushes[count].ref_count ++ ;

			return  stock_brushes[count].brush ;
		}
	}

	if( rgb == RGB(0,0,0))
	{
		return  GetStockObject( BLACK_BRUSH) ;
	}
	else if( rgb == RGB(0xff,0xff,0xff))
	{
		return  GetStockObject( WHITE_BRUSH) ;
	}
	else
	{
		void *  p ;

		if( num_of_stock_brushes % 10 == 9)
		{
			garbage_unused_objects() ;
		}
		
		if( ( p = realloc( stock_brushes ,
					sizeof( stock_brush_t) * (num_of_stock_brushes + 1)))
			== NULL)
		{
			return  None ;
		}

		stock_brushes = p ;

		stock_brushes[num_of_stock_brushes].rgb = rgb ;
		stock_brushes[num_of_stock_brushes].brush = CreateSolidBrush( rgb) ;
		stock_brushes[num_of_stock_brushes].ref_count = 1 ;

		return  stock_brushes[num_of_stock_brushes++].brush ;
	}
}

int
x_release_brush(
	HBRUSH  brush
	)
{
	u_int  count ;

	for( count = 0 ; count < num_of_stock_brushes ; count++)
	{
		if( brush == stock_brushes[count].brush)
		{
			-- stock_brushes[count].ref_count ;

			return  1 ;
		}
	}

	return  0 ;
}