]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/bmtable.c
fix crash with "save as"
[lyx.git] / src / frontends / xforms / bmtable.c
index 74e8ad48498777e1ddc43e27df48bbd59942079c..452e5c2758da4d7c519c3c7c911fcce8d5d1a8ca 100644 (file)
@@ -1,17 +1,17 @@
-/*
- *  File:        bmtable.c
- *  Purpose:     Implementation of the XForms object bmtable. 
- *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
- *  Created:     November 1995
- *  Description: A bitmap table uses a single bitmap to simulate a 2d array 
- *               of bitmap buttons. It can be used to build bitmap menus.
- *               
- *  Copyright 1995, 1996 Alejandro Aguilar Sierra 
+/**
+ * \file bmtable.c
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Alejandro Aguilar Sierra
+ *
+ * Full author contact details are available in file CREDITS
  *
- *  You are free to use and modify this code under the terms of
- *  the GNU General Public Licence version 2 or later. 
- *  
- */ 
+ * Implementation of the XForms object bmtable.
+ *
+ * A bitmap table uses a single bitmap to simulate a 2d array
+ * of bitmap buttons. It can be used to build bitmap menus.
+ */
 
 #include <config.h>
 
@@ -27,8 +27,8 @@
 extern "C"
 {
 #endif
-  
-typedef struct   {   
+
+typedef struct   {
   int nx, ny;   /**< Dimensions of the table */
   int dx, dy;   /**< Size of each item */
    int bx, by;   /**< Bitmap's position */
@@ -36,20 +36,20 @@ typedef struct   {
    unsigned char const * bdata;  /**< Bitmap data */
    int maxi;     /**< Number of items */
    int i;        /**< Current position */
-   int mousebut; /**< mouse button pushed */  
+   int mousebut; /**< mouse button pushed */
    Pixmap pix;   /**< Pixmap from data (temporal) */
 } BMTABLE_SPEC;
-                 
 
-int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx, 
+
+int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
                       FL_Coord my, int key, void * xev);
 
 
-FL_OBJECT * fl_create_bmtable(int type, FL_Coord x, FL_Coord y, 
+FL_OBJECT * fl_create_bmtable(int type, FL_Coord x, FL_Coord y,
                              FL_Coord w, FL_Coord h, char const * label)
 {
    FL_OBJECT * ob;
-   
+
    ob = fl_make_object(FL_BMTABLE, type, x, y, w, h, label, handle_bitmaptable);
    ob->boxtype = FL_BMTABLE_BOXTYPE;
    ob->spec = fl_calloc(1, sizeof(BMTABLE_SPEC));
@@ -60,14 +60,14 @@ FL_OBJECT * fl_create_bmtable(int type, FL_Coord x, FL_Coord y,
 }
 
 
-FL_OBJECT *fl_add_bmtable(int type, FL_Coord x, FL_Coord y, 
+FL_OBJECT *fl_add_bmtable(int type, FL_Coord x, FL_Coord y,
                              FL_Coord w, FL_Coord h, char const *label)
 {
    FL_OBJECT *ob;
-   
-   ob = fl_create_bmtable(type, x, y, w, h, label);  
-   fl_add_object(fl_current_form, ob); 
-   
+
+   ob = fl_create_bmtable(type, x, y, w, h, label);
+   fl_add_object(fl_current_form, ob);
+
    return ob;
 }
 
@@ -80,7 +80,7 @@ static void draw_bitmaptable(FL_OBJECT *ob)
        BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
        GC gc = fl_state[fl_get_vclass()].gc[0];
        if (!sp) return;
-   
+
        /* draw the bounding box first */
        lx = sp->maxi % sp->nx;
        fl_drw_box(ob->boxtype, ob->x, ob->y, ob->w, ob->h, ob->col1, ob->bw);
@@ -93,11 +93,11 @@ static void draw_bitmaptable(FL_OBJECT *ob)
                fl_drw_frame(FL_DOWN_FRAME, xx, yy, ww, hh, ob->col1, ob->bw);
                fl_rectf(xx, yy, ww + i, hh + i, ob->col1);
        }
-    
+
        /* draw the background bitmap */
        if (sp->bdata)  {
                if (!sp->pix) {
-                       sp->pix = XCreatePixmapFromBitmapData(fl_get_display(), fl_winget(), 
+                       sp->pix = XCreatePixmapFromBitmapData(fl_get_display(), fl_winget(),
                                                              (char*)sp->bdata,
                                                               sp->bw, sp->bh,
                                        fl_get_flcolor(ob->lcol), fl_get_flcolor(ob->col1),
@@ -106,7 +106,7 @@ static void draw_bitmaptable(FL_OBJECT *ob)
                }
        }
        if (sp->pix) {
-               /* Adjust position */ 
+               /* Adjust position */
                if (sp->bx < FL_abs(ob->bw) + 1) {
                        xx = FL_abs(ob->bw) - sp->bx + 1;
                        mx = ob->x + FL_abs(ob->bw) + 1;
@@ -114,18 +114,18 @@ static void draw_bitmaptable(FL_OBJECT *ob)
                        xx = 0;
                        mx = ob->x + sp->bx;
                }
-               if (sp->by < FL_abs(ob->bw) + 1)  {      
+               if (sp->by < FL_abs(ob->bw) + 1)  {
                        yy = FL_abs(ob->bw) - sp->by + 1;
                        my = ob->y + FL_abs(ob->bw) + 1;
                } else   {
                        yy = 0;
                        my = ob->y + sp->by;
-               }                 
-               ww = (mx + sp->bw < ob->x + ob->w - FL_abs(ob->bw)) ? 
+               }
+               ww = (mx + sp->bw < ob->x + ob->w - FL_abs(ob->bw)) ?
                        sp->bw: ob->x + ob->w - FL_abs(ob->bw) - mx;
                hh = (my + sp->bh < ob->y + ob->h - FL_abs(ob->bw)) ?
-                       sp->bh: ob->y + ob->h - FL_abs(ob->bw) - my; 
-      
+                       sp->bh: ob->y + ob->h - FL_abs(ob->bw) - my;
+
                i = FL_abs(ob->bw);
                j = hh - ((lx) ? sp->dy+2*i: 0);
                XCopyArea(fl_get_display(), sp->pix, fl_winget(), gc, xx, yy, ww, j, mx, my);
@@ -136,21 +136,21 @@ static void draw_bitmaptable(FL_OBJECT *ob)
                        XFlush(fl_get_display());
                }
        }
-   
-   
+
+
        /* draw the grid if type > FLAT */
        if (ob->type > FL_BMTABLE_FLAT)  {
-               mx = ob->x + ob->w; 
-               my = ob->y + ob->h; 
-               ww = ob->w; 
+               mx = ob->x + ob->w;
+               my = ob->y + ob->h;
+               ww = ob->w;
                for (yy= ob->y; yy<= my; yy+= sp->dy) {
-                       if (ob->boxtype!= FL_FLAT_BOX && (yy == ob->y || yy>my-sp->dy)) 
+                       if (ob->boxtype!= FL_FLAT_BOX && (yy == ob->y || yy>my-sp->dy))
                                continue;
                        if (lx>0 && yy>= my-sp->dy - sp->dy/2)
                                ww = lx*sp->dx;
-                       fl_diagline(ob->x, yy, ww, 1, FL_BOTTOM_BCOL); 
-                       fl_diagline(ob->x, yy+1, ww-2, 1, FL_TOP_BCOL); 
-               }          
+                       fl_diagline(ob->x, yy, ww, 1, FL_BOTTOM_BCOL);
+                       fl_diagline(ob->x, yy+1, ww-2, 1, FL_TOP_BCOL);
+               }
                hh = ob->h;
                for (xx= ob->x; xx<= mx; xx+= sp->dx)  {
                        if (ob->boxtype!= FL_FLAT_BOX && (xx == ob->x || xx>mx-sp->dx))
@@ -159,11 +159,11 @@ static void draw_bitmaptable(FL_OBJECT *ob)
                                hh = (sp->ny - 1) * sp->dy;
                        fl_diagline(xx, ob->y, 1, hh, FL_RIGHT_BCOL);
                        fl_diagline(xx+1, ob->y + 1, 1, hh - 2, FL_LEFT_BCOL);
-               }        
-       }  
-   
+               }
+       }
+
        /* Simulate a pushed button */
-       if (ob->pushed && 0 <= sp->i && sp->i < sp->maxi)  {  
+       if (ob->pushed && 0 <= sp->i && sp->i < sp->maxi)  {
                i = sp->i % sp->nx;
                j = sp->i/sp->nx;
                ww = sp->dx-2*FL_abs(ob->bw);
@@ -175,14 +175,14 @@ static void draw_bitmaptable(FL_OBJECT *ob)
 }
 
 
-int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx, 
+int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
                                  FL_Coord my, int key, void * xev)
 {
        int i, j;
        BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
-   
+
        switch (event)  {
-    case FL_DRAW: 
+    case FL_DRAW:
                draw_bitmaptable(ob);
                break;
     case FL_MOUSE:
@@ -200,17 +200,17 @@ int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
                                fl_redraw_object(ob);
                        }
                }
-               break;        
+               break;
     case FL_PUSH:
                sp->mousebut = key;
-               i = (mx - ob->x)/sp->dx + ((my - ob->y)/sp->dy)*sp->nx; 
+               i = (mx - ob->x)/sp->dx + ((my - ob->y)/sp->dy)*sp->nx;
                if (0 <= i && i < sp->maxi)  {
                        sp->i =  i;
                        fl_redraw_object(ob);
                } else
-                       sp->i =  -1; 
+                       sp->i =  -1;
                break;
-    case FL_RELEASE:    
+    case FL_RELEASE:
                fl_redraw_object(ob);
                return 1;
     case FL_FREEMEM:
@@ -218,7 +218,7 @@ int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
                    XFreePixmap(fl_get_display(), sp->pix);
                    XFlush(fl_get_display());
            }
-               fl_free(((BMTABLE_SPEC*)ob->spec));      
+               fl_free(((BMTABLE_SPEC*)ob->spec));
                break;
        }
        return 0;
@@ -226,23 +226,23 @@ int handle_bitmaptable(FL_OBJECT * ob, int event, FL_Coord mx,
 
 
 /*
- * The table has nx columns of dx width each and ny rows of dy height each. 
+ * The table has nx columns of dx width each and ny rows of dy height each.
  * Initially the position of the firts item is supposed to be the same that
  * the object position (x, y), and the number of items is supposed to be
  * exactly nx*ny.
- * 
+ *
  * The user could change these later. See below.
- */ 
-void fl_set_bmtable_data(FL_OBJECT * ob, int nx, int ny, int bw, int bh, 
+ */
+void fl_set_bmtable_data(FL_OBJECT * ob, int nx, int ny, int bw, int bh,
                        unsigned char const * bdata)
 {
    BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
    if (sp) {
      sp->nx = nx;
-     sp->ny = ny; 
+     sp->ny = ny;
      sp->bx = FL_abs(ob->bw);
      sp->by = FL_abs(ob->bw);
-     sp->dx = ob->w/nx; 
+     sp->dx = ob->w/nx;
      sp->dy = ob->h/ny;
      sp->i = -1;
      sp->maxi = sp->nx * sp->ny;
@@ -261,10 +261,10 @@ void fl_set_bmtable_pixmap_data(FL_OBJECT * ob, int nx, int ny,
                Pixmap dummy_shapemask = 0;
                XpmAttributes dumb_attributes = { 0 };
                sp->nx = nx;
-               sp->ny = ny; 
+               sp->ny = ny;
                sp->bx = FL_abs(ob->bw);
                sp->by = FL_abs(ob->bw);
-               sp->dx = ob->w/nx; 
+               sp->dx = ob->w/nx;
                sp->dy = ob->h/ny;
                sp->i = -1;
                sp->maxi = sp->nx * sp->ny;
@@ -289,16 +289,16 @@ void fl_set_bmtable_pixmap_data(FL_OBJECT * ob, int nx, int ny,
 /*
  *  This function works only for X11R6 or later
  */
-#if XlibSpecificationRelease > 5 
+#if XlibSpecificationRelease > 5
 
 void fl_set_bmtable_file(FL_OBJECT * ob, int nx, int ny, char const * filename)
-{      
+{
    int xh;
    int yh;
    unsigned int bw;
    unsigned int bh;
    unsigned char * bdata;
-   
+
    if (XReadBitmapFileData(filename, &bw, &bh,
                          &bdata, &xh, &yh) == BitmapSuccess)
      fl_set_bmtable_data(ob, nx, ny, bw, bh, bdata);
@@ -309,7 +309,7 @@ void fl_set_bmtable_file(FL_OBJECT * ob, int nx, int ny, char const * filename)
 
 void fl_set_bmtable_file(FL_OBJECT * ob, int nx, int ny, char const * filename)
 {
-  fprintf(stderr, "Set bmtable file: Sorry, I need X11 release 6 to do " 
+  fprintf(stderr, "Set bmtable file: Sorry, I need X11 release 6 to do "
           "work!\n");
 }
 
@@ -318,17 +318,17 @@ void fl_set_bmtable_file(FL_OBJECT * ob, int nx, int ny, char const * filename)
 
 
 void fl_set_bmtable_pixmap_file(FL_OBJECT *ob, int nx, int ny, char const *filename)
-{      
+{
   /* extern Colormap color_map; */
        BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;
        if (sp) {
                Pixmap dummy_shapemask = 0;
                XpmAttributes dumb_attributes = { 0 };
                sp->nx = nx;
-               sp->ny = ny; 
+               sp->ny = ny;
                sp->bx = FL_abs(ob->bw);
                sp->by = FL_abs(ob->bw);
-               sp->dx = ob->w/nx; 
+               sp->dx = ob->w/nx;
                sp->dy = ob->h/ny;
                sp->i = -1;
                sp->maxi = sp->nx * sp->ny;
@@ -337,7 +337,7 @@ void fl_set_bmtable_pixmap_file(FL_OBJECT *ob, int nx, int ny, char const *filen
                dumb_attributes.colormap = fl_state[fl_get_vclass()].colormap;
                dumb_attributes.closeness = 30000;
                dumb_attributes.valuemask = XpmColormap | XpmCloseness;
-   
+
                if (XReadPixmapFile(fl_get_display(), fl_winget(), (char *)filename,
                                    &(sp->pix), &dummy_shapemask,
                                    &dumb_attributes) == XpmSuccess) {
@@ -359,35 +359,35 @@ void fl_set_bmtable_pixmap_file(FL_OBJECT *ob, int nx, int ny, char const *filen
  */
 void fl_set_bmtable_adjust(FL_OBJECT *ob, int px, int py, int dx, int dy)
 {
-   BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;   
+   BMTABLE_SPEC *sp = (BMTABLE_SPEC *)ob->spec;
    if (sp) {
      sp->bx += px;
-     sp->by += py;                         
+     sp->by += py;
      sp->dx += dx;
      sp->dy += dy;
    }
 }
 
-/* 
+/*
  * This function returns the table's selected position.
  */
 int fl_get_bmtable(FL_OBJECT *ob)
-{ 
+{
    if ((BMTABLE_SPEC *)ob->spec)
      return  ((BMTABLE_SPEC *)ob->spec)->i;
-   else 
+   else
      return 0;
 }
 
 
-/* 
+/*
  * You can change the max number of items if you want.
  */
 void fl_set_bmtable_maxitems(FL_OBJECT * ob, int i)
 {
    if (i > 0 && (BMTABLE_SPEC *)ob->spec)
      ((BMTABLE_SPEC *)ob->spec)->maxi = i;
-}   
+}
 
 
 int fl_get_bmtable_maxitems(FL_OBJECT * ob)
@@ -408,7 +408,7 @@ void fl_replace_bmtable_item(FL_OBJECT * ob, int id, int cw, int ch, char * data
 void fl_get_bmtable_item(FL_OBJECT * ob, int id, int * cw, int * ch, char * data)
 {
    fprintf(stderr, "Get bmtable item: Sorry, not yet implemented!\n");
-}  
+}
 
 void fl_set_bmtable(FL_OBJECT * ob, int pushed, int pos)
 {
@@ -443,12 +443,12 @@ void fl_draw_bmtable_item(FL_OBJECT * ob, int i, Drawable d, int xx, int yy)
    int h;
    GC gc = fl_state[fl_get_vclass()].gc[0];
    BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
-   
+
    if (sp && sp->pix) {
       x = (i % sp->nx)*sp->dx + FL_abs(ob->bw);
       y = (i/sp->nx)*sp->dy + FL_abs(ob->bw);
       w = sp->dx-2*FL_abs(ob->bw);
-      h = sp->dy-2*FL_abs(ob->bw);      
+      h = sp->dy-2*FL_abs(ob->bw);
       XCopyArea(fl_get_display(), sp->pix, d, gc, x, y, w, h, xx, yy);
       XFlush(fl_get_display());
    }
@@ -460,7 +460,7 @@ void fl_free_bmtable_bitmap(FL_OBJECT * ob)
   BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
 
   /* dump the temporary pixmap */
-  if (sp && sp->pix) { 
+  if (sp && sp->pix) {
     XFreePixmap(fl_get_display(), sp->pix);
     XFlush(fl_get_display());
     sp->pix = 0;
@@ -480,7 +480,7 @@ void fl_free_bmtable_pixmap(FL_OBJECT *ob)
   BMTABLE_SPEC * sp = (BMTABLE_SPEC *)ob->spec;
 
   /* dump the temporary pixmap */
-  if (sp && sp->pix) { 
+  if (sp && sp->pix) {
     XFreePixmap(fl_get_display(), sp->pix);
     XFlush(fl_get_display());
     sp->pix = 0;