3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * Full author contact details are available in file CREDITS
10 * Implementation of the XForms object bmtable.
12 * A bitmap table uses a single bitmap to simulate a 2d array
13 * of bitmap buttons. It can be used to build bitmap menus.
20 #include XPM_H_LOCATION
22 #if defined(__cplusplus)
28 int nx, ny; /**< Dimensions of the table */
29 int dx, dy; /**< Size of each item */
30 int bx, by; /**< Bitmap's position */
31 int bw, bh; /**< Bitmap dimensions */
32 unsigned char const * bdata; /**< Bitmap data */
33 int maxi; /**< Number of items */
34 int i; /**< Current position */
35 int mousebut; /**< mouse button pushed */
36 Pixmap pix; /**< Pixmap from data (temporal) */
40 int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
41 FL_Coord my, int key, void * xev);
44 FL_OBJECT * fl_create_bmtable(int type, FL_Coord x, FL_Coord y,
45 FL_Coord w, FL_Coord h, char const * label)
49 ob = fl_make_object(FL_BMTABLE, type, x, y, w, h, label, handle_bitmaptable);
50 ob->boxtype = FL_BMTABLE_BOXTYPE;
51 ob->spec = fl_calloc(1, sizeof(BMTABLE_SPEC));
52 ((BMTABLE_SPEC *)ob->spec)->pix = 0;
53 ((BMTABLE_SPEC *)ob->spec)->bdata = 0;
54 ((BMTABLE_SPEC *)ob->spec)->mousebut = -1;
59 FL_OBJECT *fl_add_bmtable(int type, FL_Coord x, FL_Coord y,
60 FL_Coord w, FL_Coord h, char const *label)
64 ob = fl_create_bmtable(type, x, y, w, h, label);
65 fl_add_object(fl_current_form, ob);
71 static void draw_bitmaptable(FL_OBJECT *ob)
75 FL_Coord xx, yy, ww, hh;
76 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
77 GC gc = fl_state[fl_get_vclass()].gc[0];
80 /* draw the bounding box first */
81 lx = sp->maxi % sp->nx;
82 fl_drw_box(ob->boxtype, ob->x, ob->y, ob->w, ob->h, ob->col1, ob->bw);
85 xx = ob->x + sp->dx * lx + i;
86 yy = ob->y + (sp->ny - 1) * sp->dy + i;
87 ww = ob->x + ob->w - xx - i;
88 hh = ob->y + ob->h - yy - i;
89 fl_drw_frame(FL_DOWN_FRAME, xx, yy, ww, hh, ob->col1, ob->bw);
90 fl_rectf(xx, yy, ww + i, hh + i, ob->col1);
93 /* draw the background bitmap */
96 sp->pix = XCreatePixmapFromBitmapData(fl_get_display(), fl_winget(),
99 fl_get_flcolor(ob->lcol), fl_get_flcolor(ob->col1),
100 /*DefaultDepth(fl_get_display(), DefaultScreen(fl_get_display()))*/ fl_state[fl_get_vclass()].depth);
101 XFlush(fl_get_display());
105 /* Adjust position */
106 if (sp->bx < FL_abs(ob->bw) + 1) {
107 xx = FL_abs(ob->bw) - sp->bx + 1;
108 mx = ob->x + FL_abs(ob->bw) + 1;
113 if (sp->by < FL_abs(ob->bw) + 1) {
114 yy = FL_abs(ob->bw) - sp->by + 1;
115 my = ob->y + FL_abs(ob->bw) + 1;
120 ww = (mx + sp->bw < ob->x + ob->w - FL_abs(ob->bw)) ?
121 sp->bw: ob->x + ob->w - FL_abs(ob->bw) - mx;
122 hh = (my + sp->bh < ob->y + ob->h - FL_abs(ob->bw)) ?
123 sp->bh: ob->y + ob->h - FL_abs(ob->bw) - my;
126 j = hh - ((lx) ? sp->dy+2*i: 0);
127 XCopyArea(fl_get_display(), sp->pix, fl_winget(), gc, xx, yy, ww, j, mx, my);
128 XFlush(fl_get_display());
130 XCopyArea(fl_get_display(), sp->pix, fl_winget(), gc, xx,
131 yy+j, lx*sp->dx-2*i, hh-j, mx, my+j);
132 XFlush(fl_get_display());
137 /* draw the grid if type > FLAT */
138 if (ob->type > FL_BMTABLE_FLAT) {
142 for (yy= ob->y; yy<= my; yy+= sp->dy) {
143 if (ob->boxtype!= FL_FLAT_BOX && (yy == ob->y || yy>my-sp->dy))
145 if (lx>0 && yy>= my-sp->dy - sp->dy/2)
147 fl_diagline(ob->x, yy, ww, 1, FL_BOTTOM_BCOL);
148 fl_diagline(ob->x, yy+1, ww-2, 1, FL_TOP_BCOL);
151 for (xx= ob->x; xx<= mx; xx+= sp->dx) {
152 if (ob->boxtype!= FL_FLAT_BOX && (xx == ob->x || xx>mx-sp->dx))
154 if (lx>0 && xx>= ob->x + lx * sp->dx)
155 hh = (sp->ny - 1) * sp->dy;
156 fl_diagline(xx, ob->y, 1, hh, FL_RIGHT_BCOL);
157 fl_diagline(xx+1, ob->y + 1, 1, hh - 2, FL_LEFT_BCOL);
161 /* Simulate a pushed button */
162 if (ob->pushed && 0 <= sp->i && sp->i < sp->maxi) {
165 ww = sp->dx-2*FL_abs(ob->bw);
166 hh = sp->dy-2*FL_abs(ob->bw);
167 xx = ob->x + sp->dx*i + FL_abs(ob->bw);
168 yy = ob->y + sp->dy*j + FL_abs(ob->bw);
169 fl_drw_frame(FL_DOWN_FRAME, xx, yy, ww, hh, ob->col1, ob->bw);
174 int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
175 FL_Coord my, int key, void * xev)
178 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
182 draw_bitmaptable(ob);
185 if (!ob->belowmouse) { /* This never happens. Why? */
187 fl_redraw_object(ob);
190 i = (mx - ob->x)/sp->dx; j = (my - ob->y)/sp->dy;
191 if (i>= 0 && i< sp->nx && j>= 0 && j< sp->ny) {
193 if (i >= sp->maxi) i = -1;
196 fl_redraw_object(ob);
202 i = (mx - ob->x)/sp->dx + ((my - ob->y)/sp->dy)*sp->nx;
203 if (0 <= i && i < sp->maxi) {
205 fl_redraw_object(ob);
210 fl_redraw_object(ob);
214 XFreePixmap(fl_get_display(), sp->pix);
215 XFlush(fl_get_display());
217 fl_free(((BMTABLE_SPEC*)ob->spec));
225 * The table has nx columns of dx width each and ny rows of dy height each.
226 * Initially the position of the firts item is supposed to be the same that
227 * the object position (x, y), and the number of items is supposed to be
230 * The user could change these later. See below.
232 void fl_set_bmtable_data(FL_OBJECT * ob, int nx, int ny, int bw, int bh,
233 unsigned char const * bdata)
235 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
239 sp->bx = FL_abs(ob->bw);
240 sp->by = FL_abs(ob->bw);
244 sp->maxi = sp->nx * sp->ny;
252 void fl_set_bmtable_pixmap_data(FL_OBJECT * ob, int nx, int ny,
255 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
257 Pixmap dummy_shapemask = 0;
258 XpmAttributes dumb_attributes = { 0 };
261 sp->bx = FL_abs(ob->bw);
262 sp->by = FL_abs(ob->bw);
266 sp->maxi = sp->nx * sp->ny;
268 dumb_attributes.colormap = fl_state[fl_get_vclass()].colormap;
269 dumb_attributes.closeness = 30000;
270 dumb_attributes.valuemask = XpmColormap | XpmCloseness;
271 if (XCreatePixmapFromData(fl_get_display(), fl_winget(), pdata,
272 &(sp->pix), &dummy_shapemask,
273 &dumb_attributes) == XpmSuccess) {
274 sp->bw = dumb_attributes.width;
275 sp->bh = dumb_attributes.height;
276 XpmFreeAttributes(&dumb_attributes);
277 if (dummy_shapemask) {
278 XFreePixmap(fl_get_display(), dummy_shapemask);
286 * This function works only for X11R6 or later
288 #if XlibSpecificationRelease > 5
290 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);
301 XFlush(fl_get_display());
306 void fl_set_bmtable_file(FL_OBJECT * ob, int nx, int ny, char const * filename)
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;
321 Pixmap dummy_shapemask = 0;
322 XpmAttributes dumb_attributes = { 0 };
323 XpmColorSymbol xpm_col;
326 sp->bx = FL_abs(ob->bw);
327 sp->by = FL_abs(ob->bw);
331 sp->maxi = sp->nx * sp->ny;
335 xpm_col.value = "None";
336 xpm_col.pixel = fl_get_flcolor(ob->col1);
337 dumb_attributes.colormap = fl_state[fl_get_vclass()].colormap;
338 dumb_attributes.numsymbols = 1;
339 dumb_attributes.colorsymbols = &xpm_col;
340 dumb_attributes.closeness = 30000;
341 dumb_attributes.valuemask = XpmColormap | XpmCloseness | XpmColorSymbols;
343 if (XReadPixmapFile(fl_get_display(), fl_winget(), (char *)filename,
344 &(sp->pix), &dummy_shapemask,
345 &dumb_attributes) == XpmSuccess) {
346 sp->bw = dumb_attributes.width;
347 sp->bh = dumb_attributes.height;
348 XpmFreeAttributes(&dumb_attributes);
349 if (dummy_shapemask) {
350 XFreePixmap(fl_get_display(), dummy_shapemask);
353 /* XFlush(fl_get_display()); */
359 * This function allows to adjust the position of the first item and its
360 * size (dx, dy). The input values are incremental, not absolute.
362 void fl_set_bmtable_adjust(FL_OBJECT *ob, int px, int py, int dx, int dy)
364 BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;
374 * This function returns the table's selected position.
376 int fl_get_bmtable(FL_OBJECT *ob)
378 if ((BMTABLE_SPEC *)ob->spec)
379 return ((BMTABLE_SPEC *)ob->spec)->i;
386 * You can change the max number of items if you want.
388 void fl_set_bmtable_maxitems(FL_OBJECT * ob, int i)
390 if (i > 0 && (BMTABLE_SPEC *)ob->spec)
391 ((BMTABLE_SPEC *)ob->spec)->maxi = i;
395 int fl_get_bmtable_maxitems(FL_OBJECT * ob)
397 if ((BMTABLE_SPEC *)ob->spec)
398 return ((BMTABLE_SPEC *)ob->spec)->maxi;
404 void fl_replace_bmtable_item(FL_OBJECT * ob, int id, int cw, int ch, char * data)
406 fprintf(stderr, "Replace bmtable item: Sorry, not yet implemented!\n");
410 void fl_get_bmtable_item(FL_OBJECT * ob, int id, int * cw, int * ch, char * data)
412 fprintf(stderr, "Get bmtable item: Sorry, not yet implemented!\n");
415 void fl_set_bmtable(FL_OBJECT * ob, int pushed, int pos)
417 if ((BMTABLE_SPEC *)ob->spec)
418 ((BMTABLE_SPEC *)ob->spec)->i = (pushed) ? pos: -1;
422 int fl_get_bmtable_numb(FL_OBJECT *ob)
424 if ((BMTABLE_SPEC *)ob->spec)
425 return ((BMTABLE_SPEC *)ob->spec)->mousebut;
431 Pixmap fl_get_bmtable_pixmap(FL_OBJECT * ob)
433 if ((BMTABLE_SPEC *)ob->spec)
434 return ((BMTABLE_SPEC *)ob->spec)->pix;
440 void fl_draw_bmtable_item(FL_OBJECT * ob, int i, Drawable d, int xx, int yy)
446 GC gc = fl_state[fl_get_vclass()].gc[0];
447 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
450 x = (i % sp->nx)*sp->dx + FL_abs(ob->bw);
451 y = (i/sp->nx)*sp->dy + FL_abs(ob->bw);
452 w = sp->dx-2*FL_abs(ob->bw);
453 h = sp->dy-2*FL_abs(ob->bw);
454 XCopyArea(fl_get_display(), sp->pix, d, gc, x, y, w, h, xx, yy);
455 XFlush(fl_get_display());
459 /* Free the current bitmap and pixmap in preparation for installing a new one */
460 void fl_free_bmtable_bitmap(FL_OBJECT * ob)
462 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
464 /* dump the temporary pixmap */
466 XFreePixmap(fl_get_display(), sp->pix);
467 XFlush(fl_get_display());
471 /* and free the space taken by bdata etc. */
472 if (sp && sp->bdata) {
473 fl_free((void*)sp->bdata);
478 /* Free the current pixmap in preparation for installing a new one */
479 /* This is needed when using data instead of files to set bitmaps */
480 void fl_free_bmtable_pixmap(FL_OBJECT *ob)
482 BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
484 /* dump the temporary pixmap */
486 XFreePixmap(fl_get_display(), sp->pix);
487 XFlush(fl_get_display());
492 #if defined(__cplusplus)