summaryrefslogtreecommitdiffstats
path: root/generic/ttk/ttkImage.c
blob: 4e93235d3ebd4aa781b8b091b92b1d76bec26b9c (plain)
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
/* $Id: ttkImage.c,v 1.1 2006/10/31 01:42:26 hobbs Exp $
 * 	Ttk widget set -- image element factory.
 *
 * Copyright (C) 2004 Pat Thoyts <patthoyts@users.sf.net>
 * Copyright (C) 2004 Joe English
 *
 */

#include <string.h>
#include <tk.h>
#include "ttkTheme.h"

#define MIN(a,b) ((a) < (b) ? (a) : (b))

/*------------------------------------------------------------------------
 * +++ Drawing utilities.
 */

/* LPadding, CPadding, RPadding --
 * 	Split a box+padding pair into left, center, and right boxes.
 */
static Ttk_Box LPadding(Ttk_Box b, Ttk_Padding p)
    { return Ttk_MakeBox(b.x, b.y, p.left, b.height); }

static Ttk_Box CPadding(Ttk_Box b, Ttk_Padding p)
    { return Ttk_MakeBox(b.x+p.left, b.y, b.width-p.left-p.right, b.height); }

static Ttk_Box RPadding(Ttk_Box b, Ttk_Padding p)
    { return  Ttk_MakeBox(b.x+b.width-p.right, b.y, p.right, b.height); }

/* TPadding, MPadding, BPadding --
 * 	Split a box+padding pair into top, middle, and bottom parts.
 */
static Ttk_Box TPadding(Ttk_Box b, Ttk_Padding p)
    { return Ttk_MakeBox(b.x, b.y, b.width, p.top); }

static Ttk_Box MPadding(Ttk_Box b, Ttk_Padding p)
    { return Ttk_MakeBox(b.x, b.y+p.top, b.width, b.height-p.top-p.bottom); }

static Ttk_Box BPadding(Ttk_Box b, Ttk_Padding p)
    { return Ttk_MakeBox(b.x, b.y+b.height-p.bottom, b.width, p.bottom); }

/* Ttk_Fill --
 *	Fill the destination area of the drawable by replicating
 *	the source area of the image.
 */
static void Ttk_Fill(
    Tk_Window tkwin, Drawable d, Tk_Image image, Ttk_Box src, Ttk_Box dst)
{
    int dr = dst.x + dst.width;
    int db = dst.y + dst.height;
    int x,y;

    if (!(src.width && src.height && dst.width && dst.height))
	return;

    for (x = dst.x; x < dr; x += src.width) {
	int cw = MIN(src.width, dr - x);
	for (y = dst.y; y <= db; y += src.height) {
	    int ch = MIN(src.height, db - y);
	    Tk_RedrawImage(image, src.x, src.y, cw, ch, d, x, y);
	}
    }
}

/* Ttk_Stripe --
 * 	Fill a horizontal stripe of the destination drawable.
 */
static void Ttk_Stripe(
    Tk_Window tkwin, Drawable d, Tk_Image image,
    Ttk_Box src, Ttk_Box dst, Ttk_Padding p)
{
    Ttk_Fill(tkwin, d, image, LPadding(src,p), LPadding(dst,p));
    Ttk_Fill(tkwin, d, image, CPadding(src,p), CPadding(dst,p));
    Ttk_Fill(tkwin, d, image, RPadding(src,p), RPadding(dst,p));
}

/* Ttk_Tile --
 * 	Fill successive horizontal stripes of the destination drawable.
 */
static void Ttk_Tile(
    Tk_Window tkwin, Drawable d, Tk_Image image,
    Ttk_Box src, Ttk_Box dst, Ttk_Padding p)
{
    Ttk_Stripe(tkwin, d, image, TPadding(src,p), TPadding(dst,p), p);
    Ttk_Stripe(tkwin, d, image, MPadding(src,p), MPadding(dst,p), p);
    Ttk_Stripe(tkwin, d, image, BPadding(src,p), BPadding(dst,p), p);
}

/*------------------------------------------------------------------------
 * +++ Image element definition.
 */

typedef struct {		/* ClientData for image elements */
    Ttk_ResourceCache cache;	/* Resource cache for images */
    Tcl_Obj *baseImage; 	/* Name of default image */
    Ttk_StateMap imageMap;	/* State-based lookup table for images */
    Tcl_Obj *stickyObj;	 	/* Stickiness specification, NWSE */
    Tcl_Obj *borderObj;		/* Border specification */
    Tcl_Obj *paddingObj;	/* Padding specification */
    int minWidth;		/* Minimum width; overrides image width */
    int minHeight;		/* Minimum width; overrides image width */
    unsigned sticky;
    Ttk_Padding border;		/* Fixed border region */
    Ttk_Padding padding;	/* Internal padding */
} ImageData;

static void FreeImageData(void *clientData)
{
    ImageData *imageData = clientData;
    Tcl_DecrRefCount(imageData->baseImage);
    if (imageData->imageMap) {
	Tcl_DecrRefCount(imageData->imageMap);
    }
    if (imageData->stickyObj) {
	Tcl_DecrRefCount(imageData->stickyObj);
    }
    if (imageData->borderObj) {
	Tcl_DecrRefCount(imageData->borderObj);
    }
    if (imageData->paddingObj) {
	Tcl_DecrRefCount(imageData->paddingObj);
    }
    ckfree(clientData);
}

static Tk_OptionSpec ImageOptionSpecs[] =
{
    { TK_OPTION_STRING, "-sticky", "sticky", "Sticky",
	"nswe", Tk_Offset(ImageData,stickyObj), -1,
	0,0,0 },
    { TK_OPTION_STRING, "-border", "border", "Border",
	"0", Tk_Offset(ImageData,borderObj), -1,
	0,0,0 },
    { TK_OPTION_STRING, "-padding", "padding", "Padding",
	NULL, Tk_Offset(ImageData,paddingObj), -1,
	TK_OPTION_NULL_OK,0,0 },
    { TK_OPTION_STRING, "-map", "map", "Map",
	"", Tk_Offset(ImageData,imageMap), -1,
	0,0,0 },
    { TK_OPTION_INT, "-width", "width", "Width",
	"-1", -1, Tk_Offset(ImageData, minWidth),
	0, 0, 0},
    { TK_OPTION_INT, "-height", "height", "Height",
	"-1", -1, Tk_Offset(ImageData, minHeight),
	0, 0, 0},
    { TK_OPTION_END }
};

static void ImageElementGeometry(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
    ImageData *imageData = clientData;
    Tk_Image image = Ttk_UseImage(imageData->cache,tkwin,imageData->baseImage);

    if (image) {
	Tk_SizeOfImage(image, widthPtr, heightPtr);
    }
    if (imageData->minWidth >= 0) {
	*widthPtr = imageData->minWidth;
    }
    if (imageData->minHeight >= 0) {
	*heightPtr = imageData->minHeight;
    }

    *paddingPtr = imageData->padding;
    *widthPtr -= Ttk_PaddingWidth(imageData->padding);
    *heightPtr -= Ttk_PaddingHeight(imageData->padding);
}

static void ImageElementDraw(
    void *clientData, void *elementRecord, Tk_Window tkwin,
    Drawable d, Ttk_Box b, unsigned int state)
{
    ImageData *imageData = clientData;
    Tcl_Obj *imageObj = 0;
    Tk_Image image;
    int imgWidth, imgHeight;
    Ttk_Box src, dst;

    if (imageData->imageMap) {
	imageObj = Ttk_StateMapLookup(NULL, imageData->imageMap, state);
    }
    if (!imageObj) {
	imageObj = imageData->baseImage;
    }
    image = Ttk_UseImage(imageData->cache, tkwin, imageObj);

    if (!image) {
	return;
    }

    Tk_SizeOfImage(image, &imgWidth, &imgHeight);
    src = Ttk_MakeBox(0, 0, imgWidth, imgHeight);
    dst = Ttk_StickBox(b, imgWidth, imgHeight, imageData->sticky);

    Ttk_Tile(tkwin, d, image, src, dst, imageData->border);
}

static Ttk_ElementSpec ImageElementSpec =
{
    TK_STYLE_VERSION_2,
    sizeof(NullElement),
    NullElementOptions,
    ImageElementGeometry,
    ImageElementDraw
};

/*------------------------------------------------------------------------
 * +++ Image element factory.
 */
static int
Ttk_CreateImageElement(
    Tcl_Interp *interp,
    void *clientData,
    Ttk_Theme theme,
    const char *elementName,
    int objc, Tcl_Obj *CONST objv[])
{
    Tk_OptionTable imageOptionTable =
	Tk_CreateOptionTable(interp, ImageOptionSpecs);
    ImageData *imageData;

    imageData = (ImageData*)ckalloc(sizeof(*imageData));

    if (objc <= 0) {
	Tcl_AppendResult(interp, "Must supply a base image", NULL);
	return TCL_ERROR;
    }

    imageData->cache = Ttk_GetResourceCache(interp);
    imageData->imageMap = imageData->stickyObj
	= imageData->borderObj = imageData->paddingObj = 0;
    imageData->minWidth = imageData->minHeight = -1;
    imageData->sticky = TTK_FILL_BOTH;	/* ??? Is this sensible */
    imageData->border = imageData->padding = Ttk_UniformPadding(0);

    /* Can't use Tk_InitOptions() here, since we don't have a Tk_Window
     */
    if (TCL_OK != Tk_SetOptions(interp, (ClientData)imageData,
	    imageOptionTable, objc-1, objv+1,
	    NULL/*tkwin*/, NULL/*savedOptions*/, NULL/*mask*/))
    {
	ckfree((ClientData)imageData);
	return TCL_ERROR;
    }

    imageData->baseImage = Tcl_DuplicateObj(objv[0]);

    if (imageData->borderObj && Ttk_GetBorderFromObj(
		interp, imageData->borderObj, &imageData->border) != TCL_OK)
    {
	goto error;
    }

    imageData->padding = imageData->border;

    if (imageData->paddingObj && Ttk_GetBorderFromObj(
		interp, imageData->paddingObj, &imageData->padding) != TCL_OK)
    {
	goto error;
    }

    if (imageData->stickyObj && Ttk_GetStickyFromObj(
		interp, imageData->stickyObj, &imageData->sticky) != TCL_OK)
    {
	goto error;
    }

    if (!Ttk_RegisterElement(interp, theme,
				elementName, &ImageElementSpec, imageData))
    {
	goto error;
    }

    Ttk_RegisterCleanup(interp, imageData, FreeImageData);
    Tcl_SetObjResult(interp, Tcl_NewStringObj(elementName, -1));
    return TCL_OK;

error:
    FreeImageData(imageData);
    return TCL_ERROR;
}

void Ttk_ImageInit(Tcl_Interp *interp)
{
    Ttk_RegisterElementFactory(interp, "image", Ttk_CreateImageElement, NULL);
}

/*EOF*/