[go: up one dir, main page]

File: bitmap.c

package info (click to toggle)
libforms1 1.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,648 kB
  • ctags: 14,351
  • sloc: ansic: 90,441; makefile: 9,902; sh: 8
file content (318 lines) | stat: -rw-r--r-- 7,519 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 *
 *  This file is part of the XForms library package.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with XForms; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */


/*
 * $Id: bitmap.c,v 1.0 2002/05/08 05:16:30 zhao Release $
 *.
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *.
 *
 *  FL_BITMAP & FL_BITMAPBUTTON class.
 *
 */

#if defined(F_ID) || defined(DEBUG)
char *fl_id_bmp = "$Id: bitmap.c,v 1.0 2002/05/08 05:16:30 zhao Release $";
#endif

#include "forms.h"

typedef FL_BUTTON_STRUCT SPEC;

static void
free_bitmap(SPEC *p)
{
    if (p->pixmap)
	XFreePixmap(flx->display, p->pixmap);
    p->pixmap = 0;
}

/********************************************************************
 * static bitmap
 ******************************************************************{*/

static void
drawit(Window win, FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h,
       FL_COLOR fcol, FL_COLOR bcol, Pixmap bitmap)
{
    fl_color(fcol);
    fl_bk_color(bcol);
    XCopyPlane(flx->display, bitmap, win, flx->gc, 0, 0, w, h, x, y, 1);
}

static void
draw_bitmap(FL_OBJECT * ob)
{
    SPEC *sp = ((SPEC *) (ob->spec));
    int xx, yy;			/* position of bitmap */

    /* Draw the box */
    fl_drw_box(ob->boxtype, ob->x, ob->y, ob->w, ob->h,
	       ob->col1, ob->bw);

    /* do nothing is not empty data */
    if (sp->bits_w == 0 || !sp->pixmap)
	return;


    /* Calculate position so the bitmap is centered */
    xx = (int) (ob->x + (ob->w - sp->bits_w) / 2);
    yy = (int) (ob->y + (ob->h - sp->bits_h) / 2);

    drawit(FL_ObjWin(ob), xx, yy, sp->bits_w, sp->bits_h,
	   ob->lcol, ob->col1, sp->pixmap);
}



/* Handles an event, returns whether value has changed. */
static int
handle_it(FL_OBJECT * ob, int event, FL_Coord mx, FL_Coord my,
	  int key, void *ev)
{
    switch (event)
    {
    case FL_DRAW:
	draw_bitmap(ob);
    case FL_DRAWLABEL:
	fl_draw_object_label(ob);
	break;
    case FL_FREEMEM:
	free_bitmap(ob->spec);
	fl_free(ob->spec);
	break;
    }
    return 0;
}

/* Creates an object */
FL_OBJECT *
fl_create_bitmap(int type, FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h,
		 const char *label)
{
    FL_OBJECT *ob;

    ob = fl_make_object(FL_BITMAP, type, x, y, w, h, label, handle_it);
    ob->boxtype = FL_BITMAP_BOXTYPE;
    ob->col1 = FL_BITMAP_COL1;
    ob->col2 = FL_BITMAP_COL2;
    ob->lcol = FL_BITMAP_LCOL;
    ob->align = FL_BITMAP_ALIGN;
    ob->active = (type != FL_NORMAL_BITMAP);
    ob->spec = fl_calloc(1, sizeof(SPEC));
    ((SPEC *) (ob->spec))->bits_w = 0;
    return ob;
}

/* Adds an object */
FL_OBJECT *
fl_add_bitmap(int type, FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h,
	      const char *label)
{
    FL_OBJECT *ob;

    ob = fl_create_bitmap(type, x, y, w, h, label);
    fl_add_object(fl_current_form, ob);
    return ob;
}

/* Fills the bitmap with a bitmap. */
void
fl_set_bitmap_data(FL_OBJECT * ob, int w, int h, unsigned char *data)
{
    SPEC *sp;
    Pixmap p;

    if (ob == NULL || ob->objclass != FL_BITMAP)
	return;

    /* only occurs with fdesign -convert */
    if (!flx->display)
	return;

    sp = ((SPEC *) (ob->spec));

    p = XCreateBitmapFromData(flx->display,
			      FL_ObjWin(ob) ? FL_ObjWin(ob) : fl_root,
			      (char *) data, w, h);

    if (p == None)
    {
	M_err("BitmapData", "Can't create bitmap");
	return;
    }

    sp->bits_w = w;
    sp->bits_h = h;
    sp->pixmap = p;

    fl_redraw_object(ob);
}

Pixmap
fl_read_bitmapfile(Window win, const char *file,
		   unsigned int *w, unsigned int *h, int *hotx, int *hoty)
{
    Pixmap p = None;
    int status;

    status = XReadBitmapFile(flx->display, win, (char *) file,
			     w, h, &p, hotx, hoty);

    if (status != BitmapSuccess)
	M_err("ReadBitmap", "%s: %s", file,
	      status == BitmapFileInvalid ? "Invalid file" : "Can't read");
    return p;
}


void
fl_set_bitmap_file(FL_OBJECT * ob, const char *fname)
{
    int xhot, yhot;
    Pixmap p;
    SPEC *sp = ob->spec;

    if (!flx->display)
	return;

    if (ob == NULL || ob->objclass != FL_BITMAP)
    {
	M_err("set_bitmap_file", "object %s not bitmap class", ob ? ob->label : "null");
	return;
    }

    p = fl_read_bitmapfile(FL_ObjWin(ob) ? FL_ObjWin(ob) : fl_root,
			 fname, &(sp->bits_w), &(sp->bits_h), &xhot, &yhot);
    if (p != None)
    {
	free_bitmap(sp);
	sp->pixmap = p;
    }
    fl_redraw_object(ob);
}

/***** End of static BITMAP ************************/


/*******************************************************************
 * BITMAP buttons
 ****************************************************************{*/

static void
draw_bitmapbutton(FL_OBJECT * ob)
{
    SPEC *sp = ob->spec;
    unsigned long fcol;
    int x, y;

    if (FL_IS_UPBOX(ob->boxtype) && sp->val)
	fl_drw_box(FL_TO_DOWNBOX(ob->boxtype), ob->x, ob->y, ob->w, ob->h,
		   ob->col1, ob->bw);
    else
	fl_drw_box(ob->boxtype, ob->x, ob->y, ob->w, ob->h, ob->col1, ob->bw);

    if (sp->pixmap != None)
    {

	x = (int) (ob->x + (ob->w - sp->bits_w) / 2);
	y = (int) (ob->y + (ob->h - sp->bits_h) / 2);

	fcol = ob->belowmouse ? ob->col2 : ob->lcol;
	drawit(FL_ObjWin(ob), x, y, sp->bits_w, sp->bits_h,
	       fcol, ob->col1, sp->pixmap);
    }

    fl_draw_object_label(ob);

}

FL_OBJECT *
fl_create_bitmapbutton(int type, FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h,
		       const char *label)
{
    FL_OBJECT *ob;

    fl_add_button_class(FL_BITMAPBUTTON, draw_bitmapbutton, 0);
    ob = fl_create_generic_button(FL_BITMAPBUTTON, type, x, y, w, h, label);
    ob->boxtype = FL_BITMAPBUTTON_BOXTYPE;
    ob->col1 = FL_BITMAPBUTTON_COL1;
    ob->col2 = FL_BITMAPBUTTON_COL2;
    ob->align = FL_BITMAPBUTTON_ALIGN;
    ob->lcol = FL_BITMAP_LCOL;
    return ob;
}

FL_OBJECT *
fl_add_bitmapbutton(int type, FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h,
		    const char *label)
{
    FL_OBJECT *ob;

    ob = fl_create_bitmapbutton(type, x, y, w, h, label);
    fl_add_object(fl_current_form, ob);
    return ob;
}

void
fl_set_bitmapbutton_data(FL_OBJECT * ob, int w, int h, unsigned char *bits)
{
    SPEC *sp;
    Window win;

    if (!ob || ob->objclass != FL_BITMAPBUTTON)
	return;

    win = FL_ObjWin(ob) ? FL_ObjWin(ob) : fl_root;

    sp = ob->spec;
    free_bitmap(sp);
    sp->bits_w = w;
    sp->bits_h = h;

    sp->pixmap = XCreateBitmapFromData(flx->display, win,
				     (char *) bits, sp->bits_w, sp->bits_h);

    fl_redraw_object(ob);
}

void
fl_set_bitmapbutton_file(FL_OBJECT * ob, const char *file)
{
    SPEC *sp;
    int hx, hy;

    if (!flx->display)
	return;

    if (!ob || ob->objclass != FL_BITMAPBUTTON)
	return;

    sp = ob->spec;
    sp->pixmap = fl_read_bitmapfile(FL_ObjWin(ob) ? FL_ObjWin(ob) : fl_root,
			      file, &(sp->bits_w), &(sp->bits_h), &hx, &hy);
    fl_redraw_object(ob);
}

/************* End of BITMAP BUTTON **************************}****/