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