]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/bmtable.c
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / frontends / xforms / bmtable.c
1 /**
2  * \file bmtable.c
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include <stdlib.h>
14 #include "bmtable.h"
15 #include "lyx_xpm.h"
16
17 #if defined(__cplusplus)
18 extern "C"
19 {
20 #endif
21
22 typedef struct   {
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) */
32 } BMTABLE_SPEC;
33
34
35 int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
36                        FL_Coord my, int key, void * xev);
37
38
39 FL_OBJECT * fl_create_bmtable(int type, FL_Coord x, FL_Coord y,
40                               FL_Coord w, FL_Coord h, char const * label)
41 {
42         FL_OBJECT * ob;
43
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;
50         return ob;
51 }
52
53
54 FL_OBJECT *fl_add_bmtable(int type, FL_Coord x, FL_Coord y,
55                           FL_Coord w, FL_Coord h, char const *label)
56 {
57         FL_OBJECT *ob;
58
59         ob = fl_create_bmtable(type, x, y, w, h, label);
60         fl_add_object(fl_current_form, ob);
61
62         return ob;
63 }
64
65
66 static void draw_bitmaptable(FL_OBJECT *ob)
67 {
68         int i, j, lx;
69         FL_Coord mx, my;
70         FL_Coord xx, yy, ww, hh;
71         BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
72         GC gc = fl_state[fl_get_vclass()].gc[0];
73         if (!sp) return;
74
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);
78         if (lx) {
79                 i = FL_abs(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);
86         }
87
88         /* draw the background bitmap */
89         if (sp->bdata)  {
90                 if (!sp->pix) {
91                         sp->pix = XCreatePixmapFromBitmapData(fl_get_display(), fl_winget(),
92                                                               (char*)sp->bdata,
93                                                               sp->bw, sp->bh,
94                                                               fl_get_flcolor(ob->lcol), fl_get_flcolor(ob->col1),
95                                                               /*DefaultDepth(fl_get_display(), DefaultScreen(fl_get_display()))*/ fl_state[fl_get_vclass()].depth);
96                         XFlush(fl_get_display());
97                 }
98         }
99         if (sp->pix) {
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;
104                 } else  {
105                         xx = 0;
106                         mx = ob->x + sp->bx;
107                 }
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;
111                 } else   {
112                         yy = 0;
113                         my = ob->y + sp->by;
114                 }
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;
119
120                 i = FL_abs(ob->bw);
121                 j = hh - ((lx) ? sp->dy+2*i: 0);
122                 XCopyArea(fl_get_display(), sp->pix, fl_winget(), gc, xx, yy, ww, j, mx, my);
123                 XFlush(fl_get_display());
124                 if (lx) {
125                         XCopyArea(fl_get_display(), sp->pix, fl_winget(), gc, xx,
126                                   yy+j, lx*sp->dx-2*i, hh-j, mx, my+j);
127                         XFlush(fl_get_display());
128                 }
129         }
130
131
132         /* draw the grid if type > FLAT */
133         if (ob->type > FL_BMTABLE_FLAT)  {
134                 mx = ob->x + ob->w;
135                 my = ob->y + ob->h;
136                 ww = ob->w;
137                 for (yy= ob->y; yy<= my; yy+= sp->dy) {
138                         if (ob->boxtype!= FL_FLAT_BOX && (yy == ob->y || yy>my-sp->dy))
139                                 continue;
140                         if (lx>0 && yy>= my-sp->dy - sp->dy/2)
141                                 ww = lx*sp->dx;
142                         fl_diagline(ob->x, yy, ww, 1, FL_BOTTOM_BCOL);
143                         fl_diagline(ob->x, yy+1, ww-2, 1, FL_TOP_BCOL);
144                 }
145                 hh = ob->h;
146                 for (xx= ob->x; xx<= mx; xx+= sp->dx)  {
147                         if (ob->boxtype!= FL_FLAT_BOX && (xx == ob->x || xx>mx-sp->dx))
148                                 continue;
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);
153                 }
154         }
155
156         /* Simulate a pushed button */
157         if (ob->pushed && 0 <= sp->i && sp->i < sp->maxi)  {
158                 i = sp->i % sp->nx;
159                 j = sp->i/sp->nx;
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);
165         }
166 }
167
168
169 int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
170                        FL_Coord my, int key, void * xev)
171 {
172         int i, j;
173         BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
174
175         switch (event)  {
176         case FL_DRAW:
177                 draw_bitmaptable(ob);
178                 break;
179         case FL_MOUSE:
180                 if (!ob->belowmouse) {    /* This never happens. Why? */
181                         sp->i = -1;
182                         fl_redraw_object(ob);
183                         break;
184                 }
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)   {
187                         i += j*sp->nx;
188                         if (i >= sp->maxi) i = -1;
189                         if (sp->i !=  i)  {
190                                 sp->i = i;
191                                 fl_redraw_object(ob);
192                         }
193                 }
194                 break;
195         case FL_PUSH:
196                 sp->mousebut = key;
197                 i = (mx - ob->x)/sp->dx + ((my - ob->y)/sp->dy)*sp->nx;
198                 if (0 <= i && i < sp->maxi)  {
199                         sp->i =  i;
200                         fl_redraw_object(ob);
201                 } else
202                         sp->i =  -1;
203                 break;
204         case FL_RELEASE:
205                 fl_redraw_object(ob);
206                 return 1;
207         case FL_FREEMEM:
208                 if (sp->pix) {
209                         XFreePixmap(fl_get_display(), sp->pix);
210                         XFlush(fl_get_display());
211                 }
212                 fl_free(((BMTABLE_SPEC*)ob->spec));
213                 break;
214         }
215         return 0;
216 }
217
218
219 /*
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
223  * exactly nx*ny.
224  *
225  * The user could change these later. See below.
226  */
227 void fl_set_bmtable_data(FL_OBJECT * ob, int nx, int ny, int bw, int bh,
228                          unsigned char const * bdata)
229 {
230         BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
231         if (sp) {
232                 sp->nx = nx;
233                 sp->ny = ny;
234                 sp->bx = FL_abs(ob->bw);
235                 sp->by = FL_abs(ob->bw);
236                 sp->dx = ob->w/nx;
237                 sp->dy = ob->h/ny;
238                 sp->i = -1;
239                 sp->maxi = sp->nx * sp->ny;
240                 sp->bw = bw;
241                 sp->bh = bh;
242                 sp->bdata = bdata;
243         }
244 }
245
246
247 void fl_set_bmtable_pixmap_data(FL_OBJECT * ob, int nx, int ny,
248                                 char ** pdata)
249 {
250         BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
251         if (sp) {
252                 Pixmap dummy_shapemask = 0;
253                 XpmAttributes dumb_attributes = { 0 };
254                 sp->nx = nx;
255                 sp->ny = ny;
256                 sp->bx = FL_abs(ob->bw);
257                 sp->by = FL_abs(ob->bw);
258                 sp->dx = ob->w/nx;
259                 sp->dy = ob->h/ny;
260                 sp->i = -1;
261                 sp->maxi = sp->nx * sp->ny;
262                 sp->bdata = 0;
263                 dumb_attributes.colormap = fl_state[fl_get_vclass()].colormap;
264                 dumb_attributes.closeness = 30000;
265                 dumb_attributes.valuemask = XpmColormap | XpmCloseness;
266                 if (XCreatePixmapFromData(fl_get_display(), fl_winget(), pdata,
267                                           &(sp->pix), &dummy_shapemask,
268                                           &dumb_attributes) == XpmSuccess) {
269                         sp->bw = dumb_attributes.width;
270                         sp->bh = dumb_attributes.height;
271                         XpmFreeAttributes(&dumb_attributes);
272                         if (dummy_shapemask) {
273                                 XFreePixmap(fl_get_display(), dummy_shapemask);
274                         }
275                 }
276         }
277 }
278
279
280 /*
281  *  This function works only for X11R6 or later
282  */
283 #if XlibSpecificationRelease > 5
284
285 void fl_set_bmtable_file(FL_OBJECT * ob, int nx, int ny, char const * filename)
286 {
287         int xh;
288         int yh;
289         unsigned int bw;
290         unsigned int bh;
291         unsigned char * bdata;
292
293         if (XReadBitmapFileData(filename, &bw, &bh,
294                                 &bdata, &xh, &yh) == BitmapSuccess)
295                 fl_set_bmtable_data(ob, nx, ny, bw, bh, bdata);
296         XFlush(fl_get_display());
297 }
298
299 #else
300
301 void fl_set_bmtable_file(FL_OBJECT * ob, int nx, int ny, char const * filename)
302 {
303         fprintf(stderr, "Set bmtable file: Sorry, I need X11 release 6 to do "
304                 "work!\n");
305 }
306
307 #endif
308
309
310
311 void fl_set_bmtable_pixmap_file(FL_OBJECT *ob, int nx, int ny, char const *filename)
312 {
313         /* extern Colormap color_map; */
314         BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;
315         if (sp) {
316                 Pixmap dummy_shapemask = 0;
317                 XpmAttributes dumb_attributes = { 0 };
318                 XpmColorSymbol xpm_col;
319                 sp->nx = nx;
320                 sp->ny = ny;
321                 sp->bx = FL_abs(ob->bw);
322                 sp->by = FL_abs(ob->bw);
323                 sp->dx = ob->w/nx;
324                 sp->dy = ob->h/ny;
325                 sp->i = -1;
326                 sp->maxi = sp->nx * sp->ny;
327                 sp->bdata = 0;
328
329                 xpm_col.name = NULL;
330                 xpm_col.value = "None";
331                 xpm_col.pixel = fl_get_flcolor(ob->col1);
332                 dumb_attributes.colormap = fl_state[fl_get_vclass()].colormap;
333                 dumb_attributes.numsymbols = 1;
334                 dumb_attributes.colorsymbols = &xpm_col;
335                 dumb_attributes.closeness = 30000;
336                 dumb_attributes.valuemask = XpmColormap | XpmCloseness | XpmColorSymbols;
337
338                 if (XReadPixmapFile(fl_get_display(), fl_winget(), (char *)filename,
339                                     &(sp->pix), &dummy_shapemask,
340                                     &dumb_attributes) == XpmSuccess) {
341                         sp->bw = dumb_attributes.width;
342                         sp->bh = dumb_attributes.height;
343                         XpmFreeAttributes(&dumb_attributes);
344                         if (dummy_shapemask) {
345                                 XFreePixmap(fl_get_display(), dummy_shapemask);
346                         }
347                 }
348                 /* XFlush(fl_get_display()); */
349         }
350 }
351
352
353 /*
354  * This function allows to adjust the position of the first item and its
355  * size (dx, dy). The input values are incremental, not absolute.
356  */
357 void fl_set_bmtable_adjust(FL_OBJECT *ob, int px, int py, int dx, int dy)
358 {
359         BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;
360         if (sp) {
361                 sp->bx += px;
362                 sp->by += py;
363                 sp->dx += dx;
364                 sp->dy += dy;
365         }
366 }
367
368 /*
369  * This function returns the table's selected position.
370  */
371 int fl_get_bmtable(FL_OBJECT *ob)
372 {
373         if ((BMTABLE_SPEC *)ob->spec)
374                 return  ((BMTABLE_SPEC *)ob->spec)->i;
375         else
376                 return 0;
377 }
378
379
380 /*
381  * You can change the max number of items if you want.
382  */
383 void fl_set_bmtable_maxitems(FL_OBJECT * ob, int i)
384 {
385         if (i > 0 && (BMTABLE_SPEC *)ob->spec)
386                 ((BMTABLE_SPEC *)ob->spec)->maxi = i;
387 }
388
389
390 int fl_get_bmtable_maxitems(FL_OBJECT * ob)
391 {
392         if ((BMTABLE_SPEC *)ob->spec)
393                 return  ((BMTABLE_SPEC *)ob->spec)->maxi;
394         else
395                 return 0;
396 }
397
398
399 void fl_replace_bmtable_item(FL_OBJECT * ob, int id, int cw, int ch, char * data)
400 {
401         fprintf(stderr, "Replace bmtable item: Sorry, not yet implemented!\n");
402 }
403
404
405 void fl_get_bmtable_item(FL_OBJECT * ob, int id, int * cw, int * ch, char * data)
406 {
407         fprintf(stderr, "Get bmtable item: Sorry, not yet implemented!\n");
408 }
409
410 void fl_set_bmtable(FL_OBJECT * ob, int pushed, int pos)
411 {
412         if ((BMTABLE_SPEC *)ob->spec)
413                 ((BMTABLE_SPEC *)ob->spec)->i = (pushed) ? pos: -1;
414 }
415
416
417 int fl_get_bmtable_numb(FL_OBJECT *ob)
418 {
419         if ((BMTABLE_SPEC *)ob->spec)
420                 return ((BMTABLE_SPEC *)ob->spec)->mousebut;
421         else
422                 return 0;
423 }
424
425
426 Pixmap fl_get_bmtable_pixmap(FL_OBJECT * ob)
427 {
428         if ((BMTABLE_SPEC *)ob->spec)
429                 return ((BMTABLE_SPEC *)ob->spec)->pix;
430         else
431                 return 0;
432 }
433
434
435 void fl_draw_bmtable_item(FL_OBJECT * ob, int i, Drawable d, int xx, int yy)
436 {
437         int x;
438         int y;
439         int w;
440         int h;
441         GC gc = fl_state[fl_get_vclass()].gc[0];
442         BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
443
444         if (sp && sp->pix) {
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_get_display(), sp->pix, d, gc, x, y, w, h, xx, yy);
450                 XFlush(fl_get_display());
451         }
452 }
453
454 /* Free the current bitmap and pixmap in preparation for installing a new one */
455 void fl_free_bmtable_bitmap(FL_OBJECT * ob)
456 {
457         BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
458
459         /* dump the temporary pixmap */
460         if (sp && sp->pix) {
461                 XFreePixmap(fl_get_display(), sp->pix);
462                 XFlush(fl_get_display());
463                 sp->pix = 0;
464         }
465
466         /* and free the space taken by bdata etc. */
467         if (sp && sp->bdata) {
468                 fl_free((void*)sp->bdata);
469                 sp->bdata = 0;
470         }
471 }
472
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)
476 {
477         BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
478
479         /* dump the temporary pixmap */
480         if (sp && sp->pix) {
481                 XFreePixmap(fl_get_display(), sp->pix);
482                 XFlush(fl_get_display());
483                 sp->pix = 0;
484         }
485 }
486
487 #if defined(__cplusplus)
488 }
489 #endif