[AD] Contribution to Allegro: d_textedit_proc function |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
Hello. It's me again. Márcio.
I'm sending my second contribution to the Allegro community.
The d_textedit_proc is a object derived from d_edit_proc. It displays a
editbox as d_edit_proc, but it has an extra field to the left that
allows the program to display a description text. A d_textedit_proc
object looks like this:
description_text : editable_text
I'm sending two versions of this function inside to examples (attached
files exmf1.c and exmf2.c). The function itself is "bracketed" by two
full line comment (like this /*****.....****/). The /*example program*/
comment, before the second full line comment ( /*****.....****/ )
doesn't belong to the function, nor does the first full line comment.
The first version (in exmf1.c), named d_textedit_proc is the basic
version of the object. It renders the editable text with the same font
and colors of the description text.
The second version, named d_textedit_ex_proc (in exmf2.c), is an
enhacement to the first version, but is still incomplete. Beyond having
the features of d_textedit_proc, d_textedit_ex_proc allows you to choose
different colors and font for the editable text. It's still left to be
implemented the editable text justify and positioning offsets. I will
bother finishing these features only if this function is accepted.
What do you think. Should only one of these functions be added. Both of
these functions be added or none of them be added to the library
(allegro)? Are the names suitable or do you propose a better name? I'm
open for suggestions.
I would be very glad to know that I've contributed to Allegro with some
code, but by the other side (and for the sake of the Allegro community)
I think these proposed functions should be added to the library only if
their added functionality makes up for the added library size overhead.
(If both functions are added to the library, they will add about 150
lines of source code and 0.5 kB of object code)
In case these functions are accepted, the best place to put them
(according to my humble opinion) is in the file guiproc.c
Please tell me if the amount of comment is good or excessive or little.
Tell me also how should I add these functions if they are accepted (via
CVS in guiproc.c or submitting their source code to someone).
Cheers,
Marcio.
Brazil.
/* d_textedit_proc source code and example program */
/* by Marcio Afonso Arimura Fialho. Written in Feb 2004 */
/* */
#include <allegro.h>
/* d_textedit_proc source code */
/***************************************************************************/
/*
* This function is similar to d_text_proc and d_edit_proc. It writes a text
* in the left like d_text_proc and creates a d_edit_proc (sub)object to
* the right of the text. The text to be displayed is given by dp.
* dp2 points to the string to be edited. d1 and d2 are the same for d_edit_proc.
*/
#define D_EDIT_PROC_MIN_W 16 /* Minimum allowable width for the d_edit_proc (sub)object */
int d_textedit_proc(int msg, DIALOG *d, int c)
{
int rtn;
int xo, wo; /* d_textedit_proc object left corner and width */
int pix_len;
void *s;
int (*thisproc)(int, DIALOG *, int);
ASSERT(d);
xo=d->x;
wo=d->w;
s=d->dp;
pix_len=gui_strlen(s);
/* Draws the left text */
if (msg==MSG_DRAW) {
int fg = (d->flags & D_DISABLED) ? gui_mg_color : d->fg;
gui_textout_ex(screen, s, xo, d->y, fg, d->bg, FALSE);
}
/* We do a trick here. We replace the d_textedit object by a d_edit object,
and we call d_edit_proc. After that we restore the d_textedit object. */
/* Here we compute the width of the d_edit (sub)object */
d->w-=pix_len;
if (d->w< D_EDIT_PROC_MIN_W)
d->w=D_EDIT_PROC_MIN_W;
d->x=xo+wo-d->w;
/* Here we do the rest of the trick */
d->dp=d->dp2;
d->dp2=NULL; /* We make d->dp2==NULL to prevent possible problems if any
future implementation of d_edit_proc expect dp2 to be NULL */
thisproc=d->proc;
d->proc=d_edit_proc; /*Now we make this object think it is a d_edit_proc object */
/* We must change d->proc to d_edit_proc since
d_edit_proc is recursive (via object_message(d,???,???) ) and we don't
want d_textedit_proc to be called again by d_edit_proc, since
d_textedit_proc changes the object "d" before calling d_edit_proc */
/* Here we call d_edit_proc */
rtn=d_edit_proc(msg,d,c);
/* Let's restore the original object (a d_textedit_proc object) */
d->proc=thisproc;
d->dp2=d->dp;
d->dp=s;
d->x=xo;
d->w=wo;
return rtn;
}
#undef D_EDIT_PROC_MIN_W
/* example program */
/****************************************************************************/
char the_string_1[32*6];
char the_string_2[32*6]="Edit Me!";
DIALOG the_dialog[] =
{
/* (dialog proc) (x) (y) (w) (h) (fg) (bg) (key) (flags) (d1) (d2) (dp) (dp2) (dp3) */
{ d_textedit_proc, 0, 50, 256, 12, 255, 0, 0, 0, 31, 0, "Enter some text here : ", the_string_1, NULL },
{ d_textedit_proc, 0, 70, 256, 12, 255, 0, 0, 0, 31, 0, "Edit this!!! => " , the_string_2, NULL },
{ d_button_proc, 100, 120, 141, 49, 255, 0, 0, D_EXIT, 0, 0, "Exit", NULL, NULL },
{ NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
};
int main ()
{
allegro_init();
install_keyboard();
install_mouse();
char *msg="d_textedit_proc Example";
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));
textout_centre_ex(screen,font,msg,SCREEN_W/2,20,makecol(255,0,0),-1);
/* 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);
/* Here we change some dialog objects colors */
the_dialog[0].fg=makecol(0,0,128);
the_dialog[1].bg=makecol(0,255,255);
the_dialog[1].fg=makecol(128,64,0);
do_dialog(the_dialog, -1);
alert("the_string_1 is",NULL,the_string_1,"OK",NULL,0,0);
alert("the_string_2 is",NULL,the_string_2,"OK",NULL,0,0);
return 0;
}
/* d_textedit_ex_proc source code and example program */
/* by Marcio Afonso Arimura Fialho. Written in Feb 2004 */
/* */
/* Note: This example is incomplete. Please send me feedback.
So I can know it it's worthwhile continuing coding this function. */
#include <allegro.h>
/* d_textedit_ex_proc source code */
/***************************************************************************/
/*
* This function is similar to d_text_proc and d_edit_proc. It writes a text
* in the left like d_text_proc and creates a d_edit_proc (sub)object to
* the right of the text. The text to be displayed is given by dp.
* dp2 points to the string to be edited. d1 and d2 are the same for d_edit_proc.
* dp3 points to a structure holding the color and font used to display the
* text being edited. If dp3 is null, the text being edited is displayed
* with default font and color (the d_textedit_ex_proc colors specified in the
* dialog object list). If dp3->fnt is NULL then the text being edited is displayed
* with the default font "font".
*/
struct d_textedit_ex_str
{
int fg;
int bg;
FONT *fnt;
int v_just; /* Vertical justify:
0 => bottom_justified
1 => centered
2 => top justified */
int v_offset; /* Vertical offset */
/* v_just and v_offset are unimplemented in this version. I'm waiting feedback
to know if it's worthwhile to continue writing this function */
};
#define D_EDIT_PROC_MIN_W 16 /* Minimum allowable width for the d_edit_proc (sub)object */
int d_textedit_ex_proc(int msg, DIALOG *d, int c)
{
int rtn;
int xo, wo; /* d_textedit_ex_proc object left corner and width */
int pix_len;
int old_fg, old_bg; /* old object foreground and background colors */
void *s;
int (*thisproc)(int, DIALOG *, int);
FONT *oldfont;
struct d_textedit_ex_str *aux;
ASSERT(d);
xo=d->x;
wo=d->w;
s=d->dp;
pix_len=gui_strlen(s);
/* Draws the left text */
if (msg==MSG_DRAW) {
int fg = (d->flags & D_DISABLED) ? gui_mg_color : d->fg;
gui_textout_ex(screen, s, xo, d->y, fg, d->bg, FALSE);
}
/* We do a trick here. We replace the d_textedit object by a d_edit object,
and we call d_edit_proc. After that we restore the d_textedit object. */
/* Here we compute the width of the d_edit (sub)object */
d->w-=pix_len;
if (d->w< D_EDIT_PROC_MIN_W)
d->w=D_EDIT_PROC_MIN_W;
d->x=xo+wo-d->w;
/* Here we do the rest of the trick */
d->dp=d->dp2;
d->dp2=NULL; /* We make d->dp2==NULL to prevent possible problems if any
future implementation of d_edit_proc expect dp2 to be NULL */
aux = d->dp3;
if (aux) {
old_fg=d->fg;
old_bg=d->bg;
oldfont=font;
d->fg=aux->fg;
d->bg=aux->bg;
if (aux->fnt)
font=aux->fnt;
}
thisproc=d->proc;
d->proc=d_edit_proc; /*Now we make this object think it is a d_edit_proc object */
/* We must change d->proc to d_edit_proc since
d_edit_proc is recursive (via object_message(d,???,???) ) and we don't
want d_textedit_ex_proc to be called again by d_edit_proc, since
d_textedit_ex_proc changes the object "d" before calling d_edit_proc */
/* Here we call d_edit_proc */
rtn=d_edit_proc(msg,d,c);
/* Let's restore the original object (a d_textedit_ex_proc object) */
d->proc=thisproc;
if (aux) {
d->fg=old_fg;
d->bg=old_bg;
font=oldfont;
}
d->dp2=d->dp;
d->dp=s;
d->x=xo;
d->w=wo;
return rtn;
}
#undef D_EDIT_PROC_MIN_W
/* example program */
/****************************************************************************/
#include "example.h" /*
This example program uses the same datafile used by excustom.c in the
Allegro library. You will need example.h to compile and example.dat to
run this example */
/* we need to load example.dat to access the big font */
DATAFILE *datafile;
char the_string_1[32*6];
char the_string_2[32*6]="Edit Me!";
struct d_textedit_ex_str mf1;
DIALOG the_dialog[] =
{
/* (dialog proc) (x) (y) (w) (h) (fg) (bg) (key) (flags) (d1) (d2) (dp) (dp2) (dp3) */
{ d_textedit_ex_proc, 0, 50, 256, 12, 255, 0, 0, 0, 31, 0, "Enter some text here : ", the_string_1, &mf1 },
{ d_textedit_ex_proc, 0, 120, 256, 12, 255, 0, 0, 0, 31, 0, "Edit this!!! => ", the_string_2, NULL }, /* When dp3 is NULL d_textedit_ex_proc behaves exactly the same as d_textedit_proc */
{ d_button_proc, 100, 150, 141, 49, 255, 0, 0, D_EXIT, 0, 0, "Exit", NULL, NULL },
{ NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
};
int main(int argc, char *argv[])
{
char buf[256];
allegro_init();
install_keyboard();
install_mouse();
char *msg="d_textedit_ex_proc Example";
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));
textout_centre_ex(screen,font,msg,SCREEN_W/2,20,makecol(255,0,0),-1);
/* load the datafile */
replace_filename(buf, argv[0], "example.dat", sizeof(buf));
datafile = load_datafile(buf);
if (!datafile) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Error loading %s!\n", buf);
return 1;
}
/* 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);
/* Here we change some dialog objects colors */
the_dialog[0].fg=makecol(0,0,128);
the_dialog[1].bg=makecol(0,255,255);
the_dialog[1].fg=makecol(128,64,0);
mf1.fg=makecol(0,255,0);
mf1.bg=makecol(128,128,128);
mf1.fnt=datafile[BIG_FONT].dat;
do_dialog(the_dialog, -1);
alert("the_string_1 is",NULL,the_string_1,"OK",NULL,0,0);
alert("the_string_2 is",NULL,the_string_2,"OK",NULL,0,0);
return 0;
}