3 * Purpose: Implementation of the XForms object bmtable.
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: November 1995
6 * Description: A bitmap table uses a single bitmap to simulate a 2d array
7 * of bitmap buttons. It can be used to build bitmap menus.
9 * Copyright 1995, 1996 Alejandro Aguilar Sierra
11 * You are free to use and modify this code under the terms of
12 * the GNU General Public Licence version 2 or later.
20 #include XPM_H_LOCATION
23 int nx, ny; /* Dimensions of the table */
24 int dx, dy; /* Size of each item */
25 int bx, by; /* Bitmap's position */
26 int bw, bh; /* Bitmap dimensions */
27 unsigned char const * bdata; /* Bitmap data */
28 int maxi; /* Number of items */
29 int i; /* Current position */
30 int mousebut; /* mouse button pushed */
31 Pixmap pix; /* Pixmap from data (temporal) */
35 extern "C" int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
36 FL_Coord my, int key, void * xev);
39 FL_OBJECT * fl_create_bmtable(int type, FL_Coord x, FL_Coord y,
40 FL_Coord w, FL_Coord h, char const * label)
44 ob = fl_make_object(FL_BMTABLE, type, x, y, w, h, label, handle_bitmaptable);
45 ob->boxtype = FL_BMTABLE_BOXTYPE;
46 ob->spec = fl_calloc(1, sizeof(BMTABLE_SPEC));
47 ((BMTABLE_SPEC *)ob->spec)->pix = 0;
48 ((BMTABLE_SPEC *)ob->spec)->bdata= 0;
49 ((BMTABLE_SPEC *)ob->spec)->mousebut= -1;
54 FL_OBJECT *fl_add_bmtable(int type, FL_Coord x, FL_Coord y,
55 FL_Coord w, FL_Coord h, char const *label)
59 ob = fl_create_bmtable(type, x, y, w, h, label);
60 fl_add_object(fl_current_form, ob);
66 static void draw_bitmaptable(FL_OBJECT *ob)
70 FL_Coord xx, yy, ww, hh;
71 BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;
73 GC gc = fl_state[fl_get_vclass()].gc[0];
75 /* draw the bounding box first */
76 lx = sp->maxi % sp->nx;
77 fl_drw_box(ob->boxtype, ob->x, ob->y, ob->w, ob->h, ob->col1, ob->bw);
80 xx = ob->x+ sp->dx*lx + i;
81 yy = ob->y+ (sp->ny-1)*sp->dy+i;
82 ww = ob->x+ob->w - xx - i;
83 hh = ob->y+ob->h-yy-i;
84 fl_drw_frame(FL_DOWN_FRAME, xx, yy, ww, hh, ob->col1, ob->bw);
85 fl_rectf(xx, yy, ww+i, hh+i, ob->col1);
88 /* draw the background bitmap */
91 sp->pix = XCreatePixmapFromBitmapData(fl_display, fl_winget(),
92 const_cast<char*>(reinterpret_cast<char const *>(sp->bdata)),
94 fl_get_flcolor(ob->lcol), fl_get_flcolor(ob->col1),
95 /*DefaultDepth(fl_display, DefaultScreen(fl_display))*/ fl_state[fl_get_vclass()].depth);
100 /* Adjust position */
101 if (sp->bx < FL_abs(ob->bw) + 1) {
102 xx = FL_abs(ob->bw) - sp->bx + 1;
103 mx = ob->x + FL_abs(ob->bw) + 1;
108 if (sp->by < FL_abs(ob->bw) + 1) {
109 yy = FL_abs(ob->bw) - sp->by + 1;
110 my = ob->y + FL_abs(ob->bw) + 1;
115 ww = (mx + sp->bw < ob->x + ob->w - FL_abs(ob->bw)) ?
116 sp->bw: ob->x + ob->w - FL_abs(ob->bw) - mx;
117 hh = (my + sp->bh < ob->y + ob->h - FL_abs(ob->bw)) ?
118 sp->bh: ob->y + ob->h - FL_abs(ob->bw) - my;
121 j = hh - ((lx) ? sp->dy+2*i: 0);
122 XCopyArea(fl_display, sp->pix, fl_winget(), gc, xx, yy, ww, j, mx, my);
125 XCopyArea(fl_display, sp->pix, fl_winget(), gc, xx,
126 yy+j, lx*sp->dx-2*i, hh-j, mx, my+j);
132 /* draw the grid if type > FLAT */
133 if (ob->type > FL_BMTABLE_FLAT) {
137 for (yy= ob->y; yy<= my; yy+= sp->dy) {
138 if (ob->boxtype!= FL_FLAT_BOX && (yy == ob->y || yy>my-sp->dy))
140 if (lx>0 && yy>= my-sp->dy - sp->dy/2)
142 fl_diagline(ob->x, yy, ww, 1, FL_BOTTOM_BCOL);
143 fl_diagline(ob->x, yy+1, ww-2, 1, FL_TOP_BCOL);
146 for (xx= ob->x; xx<= mx; xx+= sp->dx) {
147 if (ob->boxtype!= FL_FLAT_BOX && (xx == ob->x || xx>mx-sp->dx))
149 if (lx>0 && xx>= ob->x+lx*sp->dx)
150 hh = (sp->ny-1)*sp->dy;
151 fl_diagline(xx, ob->y, 1, hh, FL_RIGHT_BCOL);
152 fl_diagline(xx+1, ob->y+1, 1, hh-2, FL_LEFT_BCOL);
156 /* Simulate a pushed button */
157 if (ob->pushed && 0 <= sp->i && sp->i < sp->maxi) {
160 ww = sp->dx-2*FL_abs(ob->bw);
161 hh = sp->dy-2*FL_abs(ob->bw);
162 xx = ob->x + sp->dx*i + FL_abs(ob->bw);
163 yy = ob->y + sp->dy*j + FL_abs(ob->bw);
164 fl_drw_frame(FL_DOWN_FRAME, xx, yy, ww, hh, ob->col1, ob->bw);
169 extern "C" int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
170 FL_Coord my, int key, void */*xev*/)
173 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
177 draw_bitmaptable(ob);
180 if (!ob->belowmouse) { /* This never happens. Why? */
182 fl_redraw_object(ob);
185 i = (mx - ob->x)/sp->dx; j = (my - ob->y)/sp->dy;
186 if (i>= 0 && i< sp->nx && j>= 0 && j< sp->ny) {
188 if (i >= sp->maxi) i = -1;
191 fl_redraw_object(ob);
197 i = (mx - ob->x)/sp->dx + ((my - ob->y)/sp->dy)*sp->nx;
198 if (0 <= i && i < sp->maxi) {
200 fl_redraw_object(ob);
205 fl_redraw_object(ob);
209 XFreePixmap(fl_display, sp->pix);
212 fl_free(((BMTABLE_SPEC*)ob->spec));
220 * The table has nx columns of dx width each and ny rows of dy height each.
221 * Initially the position of the firts item is supposed to be the same that
222 * the object position (x, y), and the number of items is supposed to be
225 * The user could change these later. See below.
227 void fl_set_bmtable_data(FL_OBJECT * ob, int nx, int ny, int bw, int bh,
228 unsigned char const * bdata)
230 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
234 sp->bx = FL_abs(ob->bw);
235 sp->by = FL_abs(ob->bw);
239 sp->maxi = sp->nx * sp->ny;
247 void fl_set_bmtable_pixmap_data(FL_OBJECT * ob, int nx, int ny,
250 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
251 extern Colormap color_map;
255 sp->bx = FL_abs(ob->bw);
256 sp->by = FL_abs(ob->bw);
260 sp->maxi = sp->nx * sp->ny;
262 Pixmap dummy_shapemask = 0;
264 // I can't see why this initalization is needed. (Lgb)
265 XpmAttributes dumb_attributes = {0};
267 XpmAttributes dumb_attributes;
269 dumb_attributes.colormap = color_map;
270 dumb_attributes.closeness = 30000;
271 dumb_attributes.valuemask = XpmColormap | XpmCloseness;
272 if (XCreatePixmapFromData(fl_display, fl_winget(), pdata,
273 &(sp->pix), &dummy_shapemask,
274 &dumb_attributes) == XpmSuccess) {
275 sp->bw = dumb_attributes.width;
276 sp->bh = dumb_attributes.height;
277 XpmFreeAttributes(&dumb_attributes);
278 if (dummy_shapemask) {
279 XFreePixmap(fl_display, dummy_shapemask);
288 * This function works only for X11R6 or later
290 #if XlibSpecificationRelease > 5
292 void fl_set_bmtable_file(FL_OBJECT * ob, int nx, int ny, char const * filename)
296 unsigned char * bdata;
298 if(XReadBitmapFileData(filename, &bw, &bh,
299 &bdata, &xh, &yh) == BitmapSuccess)
300 fl_set_bmtable_data(ob, nx, ny, bw, bh, bdata);
306 void fl_set_bmtable_file(FL_OBJECT *, int, int, char const *)
308 fprintf(stderr, "Set bmtable file: Sorry, I need X11 release 6 to do "
316 void fl_set_bmtable_pixmap_file(FL_OBJECT *ob, int nx, int ny, char const *filename)
318 extern Colormap color_map;
319 BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;
323 sp->bx = FL_abs(ob->bw);
324 sp->by = FL_abs(ob->bw);
328 sp->maxi = sp->nx * sp->ny;
331 Pixmap dummy_shapemask = 0;
333 XpmAttributes dumb_attributes = {0};
335 XpmAttributes dumb_attributes;
337 dumb_attributes.colormap = color_map;
338 dumb_attributes.closeness = 30000;
339 dumb_attributes.valuemask = XpmColormap | XpmCloseness;
341 if (XReadPixmapFile(fl_display, fl_winget(), (char *)filename,
342 &(sp->pix), &dummy_shapemask,
343 &dumb_attributes) == XpmSuccess) {
344 sp->bw = dumb_attributes.width;
345 sp->bh = dumb_attributes.height;
346 XpmFreeAttributes(&dumb_attributes);
347 if (dummy_shapemask) {
348 XFreePixmap(fl_display, dummy_shapemask);
357 * This function allows to adjust the position of the first item and its
358 * size (dx, dy). The input values are incremental, not absolute.
360 void fl_set_bmtable_adjust(FL_OBJECT *ob, int px, int py, int dx, int dy)
362 BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;
372 * This function returns the table's selected position.
374 int fl_get_bmtable(FL_OBJECT *ob)
376 if ((BMTABLE_SPEC *)ob->spec)
377 return ((BMTABLE_SPEC *)ob->spec)->i;
384 * You can change the max number of items if you want.
386 void fl_set_bmtable_maxitems(FL_OBJECT * ob, int i)
388 if (i > 0 && (BMTABLE_SPEC *)ob->spec)
389 ((BMTABLE_SPEC *)ob->spec)->maxi = i;
393 int fl_get_bmtable_maxitems(FL_OBJECT * ob)
395 if ((BMTABLE_SPEC *)ob->spec)
396 return ((BMTABLE_SPEC *)ob->spec)->maxi;
402 void fl_replace_bmtable_item(FL_OBJECT */*ob*/, int /*id*/, int /*cw*/, int /*ch*/, char */*data*/)
404 fprintf(stderr, "Replace bmtable item: Sorry, not yet implemented!\n");
408 void fl_get_bmtable_item(FL_OBJECT */*ob*/, int /*id*/, int */*cw*/, int */*ch*/, char */*data*/)
410 fprintf(stderr, "Get bmtable item: Sorry, not yet implemented!\n");
413 void fl_set_bmtable(FL_OBJECT *ob, int pushed, int pos)
415 if ((BMTABLE_SPEC *)ob->spec)
416 ((BMTABLE_SPEC *)ob->spec)->i = (pushed) ? pos: -1;
420 int fl_get_bmtable_numb(FL_OBJECT *ob)
422 if ((BMTABLE_SPEC *)ob->spec)
423 return ((BMTABLE_SPEC *)ob->spec)->mousebut;
429 Pixmap fl_get_bmtable_pixmap(FL_OBJECT * ob)
431 if ((BMTABLE_SPEC *)ob->spec)
432 return ((BMTABLE_SPEC *)ob->spec)->pix;
438 void fl_draw_bmtable_item(FL_OBJECT * ob, int i, Drawable d, int xx, int yy)
441 GC gc = fl_state[fl_get_vclass()].gc[0];
442 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
445 x = (i % sp->nx)*sp->dx + FL_abs(ob->bw);
446 y = (i/sp->nx)*sp->dy + FL_abs(ob->bw);
447 w = sp->dx-2*FL_abs(ob->bw);
448 h = sp->dy-2*FL_abs(ob->bw);
449 XCopyArea(fl_display, sp->pix, d, gc, x, y, w, h, xx, yy);
454 /* Free the current bitmap and pixmap in preparation for installing a new one */
455 void fl_free_bmtable_bitmap(FL_OBJECT * ob)
457 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
459 /* dump the temporary pixmap */
461 XFreePixmap(fl_display, sp->pix);
466 /* and free the space taken by bdata etc. */
467 if (sp && sp->bdata) {
468 fl_free((void*)sp->bdata);
473 /* Free the current pixmap in preparation for installing a new one */
474 /* This is needed when using data instead of files to set bitmaps */
475 void fl_free_bmtable_pixmap(FL_OBJECT *ob)
477 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
479 /* dump the temporary pixmap */
481 XFreePixmap(fl_display, sp->pix);