[AD] Contribuition to Allegro: move_dialog function

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


As a return gift to the Allegro Community. I'm sending the source code with a example program for a function I've written when I was writing a program using Allegro. The function name is move_dialog. It moves all the objects in a dialog by (y) pixels to up and (x) pixels to the right.

I was wondering whether this function would be suitable to add into Allegro itself or if it is too simple and silly. position_dialog doesn't fit my purposes because it positions the dialog with respect to it's upper left corner object, and this is not desirable when you have a dialog and you wan't to move it only some pixels above or some pixels right. Do you think it's worthwhile adding this function to the library? (It will add only 13 lines of source code and a few bytes overhead in the compiled library).

In case it's accepted, it should be added to gui.c (near position_dialog). I didn't submit a CVS patch (as Chris Jones did in his post (2004-02-29 09:59)) because I don't know yet how to use CVS (I will learn how to use it someday).

 Here is a listing of this function

/* move_dialog:
*  Moves all the objects in a dialog by the amount specified by x and y.
*/
void move_dialog(DIALOG *dialog, int x, int y)
{
 int c;
 ASSERT(dialog);

 for (c=0; dialog[c].proc; c++) {
    dialog[c].x += x;
    dialog[c].y += y;
 }
}

I've also attached an example file with the proposed function. It should compile straighforward with Allegro.

P.S: I apologise for my bad English, since I'm Brazilian and I'm currently in a intermediate english proficiency level. Don't fell ashamed to correct my grammar errors or to sugest me better ways of writing my ideas. Thanks.

Best Regards,
Marcio.

/*    move_dialog source code and example program                      */
/* by Marcio Afonso Arimura Fialho. Written in Feb 2004                */
/*      */

#include <allegro.h>

/* move_dialog source code */
/***************************************************************************/

/* move_dialog:
 *  Moves all the objects in a dialog by the amount specified by x and y.
 */
void move_dialog(DIALOG *dialog, int x, int y)
{
   int c;
   ASSERT(dialog);

   for (c=0; dialog[c].proc; c++) {
      dialog[c].x += x;
      dialog[c].y += y;
   }
}

/* example program */
/****************************************************************************/

DIALOG *the_dialog;
#define AMOUNT 10 /*Displacement of the dialog in pixels in each click.*/

int d_move_up(int msg, DIALOG *d, int c)
{
   int ret;

   /* call the parent object */
   ret = d_button_proc(msg, d, c);

   /* trap the close return value and change the font */
   if (ret == D_CLOSE) {
      move_dialog(the_dialog,0,-AMOUNT);
      return D_REDRAW; 
   }

   /* otherwise just return */
   return ret;
}

int d_move_down(int msg, DIALOG *d, int c)
{
   int ret;

   ret = d_button_proc(msg, d, c);
   if (ret == D_CLOSE) {
      move_dialog(the_dialog,0,AMOUNT);
      return D_REDRAW; 
   }
   return ret;
}

int d_move_left(int msg, DIALOG *d, int c)
{
   int ret;

   ret = d_button_proc(msg, d, c);
   if (ret == D_CLOSE) {
      move_dialog(the_dialog,-AMOUNT,0);
      return D_REDRAW; 
   }
   return ret;
}

int d_move_right(int msg, DIALOG *d, int c)
{
   int ret;

   ret = d_button_proc(msg, d, c);
   if (ret == D_CLOSE) {
      move_dialog(the_dialog,AMOUNT,0);
      return D_REDRAW; 
   }
   return ret;
}

int main ()
 {
   char *title="move_dialog Example"; /* The title */

   the_dialog = (DIALOG [])
   {
    /* (dialog proc)     (x)   (y)   (w)   (h)   (fg)  (bg)  (key) (flags)  (d1) (d2)  (dp)       (dp2) (dp3) */
    { d_clear_proc,       0,   20,    0,    0,   255,  0,    0,    0,       0,   0,    NULL,       NULL, NULL  },
    { d_move_up,         60,   20,   50,   15,   255,  0,    'u',  D_EXIT,  0,   0,    "&UP",      NULL, NULL  },
    { d_move_left,        0,   60,   50,   15,   255,  0,    'f',  D_EXIT,  0,   0,    "LE&FT",    NULL, NULL  },
    { d_move_right,      120,  60,   50,   15,   255,  0,    'g',  D_EXIT,  0,   0,    "RI&GHT",   NULL, NULL  },
    { d_move_down,       60,  100,   50,   15,   255,  0,    'n',  D_EXIT,  0,   0,    "DOW&N",    NULL, NULL  },
    { d_button_proc,     60,   60,   50,   15,   255,  0,    'x',  D_EXIT,  0,   0,    "E&XIT",    NULL, NULL  },
    { d_ctext_proc,      10,   0,   150,   15,   255,  0,    0,    0,       0,   0,    title,      NULL, NULL  },
    { NULL,              0,    0,    0,    0,    0,    0,    0,    0,       0,   0,    NULL,       NULL, NULL  }
   };

   allegro_init();
   install_keyboard();
   install_mouse();

   if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
      if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
    set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
    allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
    return 1;
      }
   }

   set_palette(desktop_palette);
   
   clear_to_color(screen,makecol(255,255,255));

   /* We set up colors to match screen color depth*/
   gui_bg_color=makecol(255,255,0);
   gui_fg_color=makecol(0,0,0);

   set_dialog_color(the_dialog,gui_fg_color,gui_bg_color);
   the_dialog[6].fg=makecol(255,0,0); /* The title object color */
   centre_dialog(the_dialog);
   
   do_dialog(the_dialog, -1);

   return 0;
 }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/