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