/
Inicio :: Foros

 F.A.Q.F.A.Q.                  Conéctese para revisar sus mensajesConéctese para revisar sus mensajes   

Falla con la compilación de la biblioteca Xgraphics.c =)

 
      Índice del Foro elrincondelc.com -> Gráficos
Ver tema anterior :: Ver siguiente tema  
AutorMensaje
compusystems



Registrado: 05 Oct 2012
Mensajes: 13
Ubicación: Colombia

MensajePublicado: 05/10/2012 9:30 am
Título: Falla con la compilación de la biblioteca Xgraphics.c =)

Hola, primero les comento que hay un libro de programación de lenguaje C y C++ llamado (C y C++ de afán), con muchos ejemplos didácticos, escrito por un profesor de la Universidad de Antioquia (Medellin Colombia) llamado (Manuel José Páez Mejía)

Pueden ver algunos capitulos libremente en: o buscar en google (C y C ++ de afán)
http://books.google.com.co/books?id=E1CMxC4KKSYC&pg=PA157&lpg=PA157&dq=Manuel+Jos%C3%A9+P%C3%A1ez+Mej%C3%ADa+xgraphics&source=bl&ots=r_ayZLXuqb&sig=nTQMbs8653cKKBc5H4oZN_2bEbM&hl=es&sa=X&ei=Vc5pUJbsNIa09gTbz4HYBw&ved=0CB0Q6AEwAA#v=onepage&q=Manuel%20Jos%C3%A9%20P%C3%A1ez%20Mej%C3%ADa%20xgraphics&f=false

En el capitulo 8, pagina 155, habla sobre Graficacion utilizando una biblioteca llamada Xgraphics.c de muy fácil manejo, pero no logro compilar los ejemplos que muestra el libro, para la cual les solicito por favor me colaboren utilizando un compilador cualesquiera de C/C++ y detectando por que no compila satisfactoriamente.  Sad


Esta biblioteca de graficacion Xgraphics.c sirve para crear interfaces gráficas (GUI) simples, para entrar datos con menús y visualizar resultados en ventanas, por que comúnmente la biblioteca I/O de C/C++ es de entrada y salida de datos en forma de texto y lineal

Anexo abajo los archivos de Xgraphics.c ,  Xgraphic.h y unos ejemplo de prueba para que traten de compilarlo y detectar fallas
Muchas Gracias

PD: les pregunto que tan importante les parece este libro?
Existe una biblioteca similar a Xgraphics.c para realizar GUI fácilmente en C/C++


Ultima edición por compusystems el 11/10/2012 12:06 pm; editado 4 veces
Volver arriba
compusystems



Registrado: 05 Oct 2012
Mensajes: 13
Ubicación: Colombia

MensajePublicado: 05/10/2012 9:32 am
Título:

1er Archivo del proyecto

Xgraphics.c

Código:

/*
File Name: Xgraphics.c
Version:  1.00 ( 28/04/96 )
By:         Martin Lueders (lueders@physik.uni-wuerzburg.de)

Copyright (C)
**  This program is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or  (at your option) any later version.
**  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
**  You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "Xgraphics.h" //

void InitX(){
   char **fontlist;
   int  i,count;
   Font myfont;
   char myfontpattern[40];
   XColor col, colexact;

   /* set display to value of environment DISPLAY */
   mydisplay=XOpenDisplay("");
   if (mydisplay==NULL){
      printf("Cannot connect to X Server\n");
      exit(0);
   }
   myscreen=DefaultScreen(mydisplay);

   /* set colors */
   mycmap = DefaultColormap(mydisplay,myscreen);

   black=BlackPixel(mydisplay,myscreen);
   white=WhitePixel(mydisplay,myscreen);
   global_color = black;
   global_function = GXcopy;

   mygc    = XCreateGC(mydisplay,DefaultRootWindow(mydisplay),0,0);
   cleargc = XCreateGC(mydisplay,DefaultRootWindow(mydisplay),0,0);
   initgc  = XCreateGC(mydisplay,DefaultRootWindow(mydisplay),0,0);

   XSetForeground(mydisplay,mygc,black);
   XSetBackground(mydisplay,mygc,white);

   XSetForeground(mydisplay,cleargc,white);
   XSetBackground(mydisplay,cleargc,black);

   XFlush(mydisplay);

   if( DefaultDepth(mydisplay,myscreen) == 1 ) {

      mycolors[0] = white;
      mycolors[1] = black;
      NofColors = 2;

   } else {
      if ( DefaultVisual(mydisplay,myscreen)->class < 2 ) {
         for(i=NofColors=0;i<MAX_COLORS;++i) {
   if (XAllocNamedColor(mydisplay,mycmap,graylist[i],&col,&colexact) )
      mycolors[NofColors++] = col.pixel;
         }
      } else {
         for(i=NofColors=0;i<MAX_COLORS;++i) {
   if (XAllocNamedColor(mydisplay,mycmap,colorlist[i],&col,&colexact) )
      mycolors[NofColors++] = col.pixel;
         }
      }
   }

   /* set default font */
   strcpy(myfontpattern,"-*-helvetica-bold-r-normal--14*");

   fontlist = XListFonts(mydisplay,myfontpattern,20,&count);
   if(count) {
      myfont=XLoadFont(mydisplay,myfontpattern);
      XSetFont(mydisplay,mygc,myfont);
   } /* if */
   XFreeFontNames(fontlist);

   XCopyGC(mydisplay,mygc,-1,initgc);

   /* reset internal lists */

   worldlist = (struct win_node *) NULL;
   buttonflag = 0;
   buttonlist.buttons = (ButtonType *) NULL;
   buttonlist.NofButtons = 0;
}


/*=====*\
void ExitX(){
   /* destroy remaining windows */
   while(worldlist) DestroyWindow(worldlist->win);

   /* free memory */
   if ( buttonlist.buttons ) free ( buttonlist.buttons );

   XFreeGC(mydisplay,mygc);
   XFreeGC(mydisplay,cleargc);

   XCloseDisplay(mydisplay);
}

/*=====*\

/* internal world- and win- handling */

struct world_node *make_world_node(){
   return( (struct world_node *) calloc(1,sizeof(struct world_node)) );
}

struct win_node *make_win_node(){
   return( (struct win_node *) calloc(1,sizeof(struct win_node)) );
}

struct win_node *find_win_node(Window window){
   struct win_node *winptr;
   int notfound=1;

   winptr = worldlist;

   while(winptr && notfound) {
      if( winptr->win == window ) notfound = 0;
      else winptr = winptr->nextwin;
   }
   return winptr;
}

struct world_node *find_world_node(World world){
   struct world_node *worldptr;
   int notfound=1;

   worldptr = find_win_node(world->win)->nextworld;

   while(worldptr && notfound) {
      if( worldptr->world == world ) notfound = 0;
      else worldptr = worldptr->nextworld;
   }
   return worldptr;
}

/*=====*\

Window CreateWindow(int width, int height, char *name){
   Window tmp;
   struct win_node *winptr;

   XSizeHints *sizehints;
   XWMHints   *wmhints;
   XClassHint *classhints;
   XTextProperty windowname, iconname;


   /* define window attributes */
   if(!(sizehints=XAllocSizeHints())){
      fprintf(stderr,"Xgraphics: failure allocating memory\n");
      exit(-1);
   }

   if(!(wmhints=XAllocWMHints())){
      fprintf(stderr,"Xgraphics: failure allocating memory\n");
      exit(-1);
   }

   if(!(classhints=XAllocClassHint())){
      fprintf(stderr,"Xgraphics: failure allocating memory\n");
      exit(-1);
   }

   if(XStringListToTextProperty(&name,1,&windowname) ==0) {
      fprintf(stderr,"Xgraphics: failure allocating structure\n");
      exit(-1);
   }

   if(XStringListToTextProperty(&name,1,&iconname) ==0) {
      fprintf(stderr,"Xgraphics: failure allocating structure\n");
      exit(-1);
   }

   wmhints->initial_state = NormalState;
   wmhints->input = True;
   wmhints->flags = StateHint | InputHint;

/*sizehints->base_width = width;
   sizehints->base_height = height;
   sizehints->flags = PBaseSize;
*/

   attributes.backing_store    = Always;
   attributes.background_pixel = white;

   /* create a window */
   tmp =  XCreateWindow(mydisplay,DefaultRootWindow(mydisplay),
                   200,200,width,height,                      /* coords */
                   17,CopyFromParent,            /* border width, depth */
                   InputOutput,                                /* class */
                   CopyFromParent,                       /* visual type */
                   CWBackingStore|
          CWBackPixel,                            /* valuemask */
                   &attributes);                          /* attributes */

   XSelectInput(mydisplay,tmp,
         OwnerGrabButtonMask |
                  ButtonPressMask | ButtonReleaseMask |         /* Mouse */
                  EnterWindowMask |               /* Mouse enters Window */
         StructureNotifyMask |              /* Geometry changes */
                  KeyPressMask    |                          /* Keyboard */
                  ExposureMask);                    /* Window - Exposure */

   XSetWMProperties(mydisplay,tmp,&windowname,&iconname,NULL,0,sizehints,
          wmhints,classhints);


   /* update window/world - database */
   winptr    = worldlist;
   worldlist = make_win_node();

   worldlist->nextwin   = winptr;
   worldlist->prevwin   = (struct win_node *) NULL;
   worldlist->nextworld = (struct world_node *) NULL;
   worldlist->win       = tmp;

   if(winptr) winptr->prevwin = worldlist;

   return tmp;
}

/*=====*\
void ShowWindow(Window win){
   XMapRaised(mydisplay,win);
}

void HideWindow(Window win){
   XUnmapWindow(mydisplay,win);
   XSync(mydisplay,True);
}

/*=====*\

void DestroyWindow(Window win){
   struct win_node   *winptr;
   struct world_node *worldptr;

   /* find entry in database */
   winptr = find_win_node(win);
   worldptr = winptr->nextworld;

   /* destroy worlds within this window */
   while(winptr->nextworld) DestroyWorld(winptr->nextworld->world);

   /* delete database-entry */
   if(winptr->nextwin) winptr->nextwin->prevwin = winptr->prevwin;
   if(winptr->prevwin) winptr->prevwin->nextwin = winptr->nextwin;
   else worldlist = winptr->nextwin;

   /* destroy window */
   XDestroyWindow(mydisplay,win);
   free(winptr);
}

void DestroyWorld(World world){
   struct world_node *worldptr;

   /* find database-entry */
   worldptr = find_world_node(world);

   /* free GC's */
   if(world->clipping) XFreeGC(mydisplay,world->gcw);
   if(world->gcp != mygc) XFreeGC(mydisplay,world->gcp);

   /* delete database-entry */
   if(worldptr->nextworld) worldptr->nextworld->prevworld = worldptr->prevworld;
   if(worldptr->prevworld) worldptr->prevworld->nextworld = worldptr->nextworld;
   else find_win_node(world->win)->nextworld = worldptr->nextworld;

   /* destroy world */
   free(world);
   free(worldptr);
}


/*=====*\

void ClearWindow(Window win){
   XClearWindow(mydisplay,win);
}

/*=====*\

World CreateWorld(Window window, int px, int py, int pwidth, int pheight,
         double wx1, double wy1, double wx2, double wy2,
         int scalable, int gravity)
{

   World  tmpworld;
   struct win_node   *winptr;
   struct world_node *worldptr;
   Window tmp;
   int    gx,gy;
   unsigned int gb,gd;
   XRectangle rect;

   /* create world (memory) */
   tmpworld = (World) calloc(1,sizeof(Worldstruct));

   /* if neccessary create window */
   if(window == 0) { tmpworld->win = CreateWindow(px+pwidth,py+pheight,"");
            ShowWindow(tmpworld->win); }
   else            tmpworld->win = window;

   /* create Backing-storage Pixmap */
   if( !scalable ) {
      tmpworld->pixmap = XCreatePixmap(mydisplay,tmpworld->win,pwidth,pheight,
                  DefaultDepth(mydisplay,myscreen));
      XFillRectangle(mydisplay,tmpworld->pixmap,cleargc,0,0,pwidth,pheight);
   }
   else
      tmpworld->pixmap = 0;

   /* set coords */
   tmpworld->px = px;
   tmpworld->py = py;
   tmpworld->pwidth  = pwidth;
   tmpworld->pheight = pheight;

   tmpworld->bbx = px;
   tmpworld->bby = py;
   tmpworld->bbwidth = pwidth;
   tmpworld->bbheight = pheight;

   tmpworld->x1 = wx1;
   tmpworld->y1 = wy1;
   tmpworld->x2 = wx2;
   tmpworld->y2 = wy2;

   tmpworld->fx = (double)(pwidth-1)/(wx2-wx1);
   tmpworld->fy = (double)(pheight-1)/(wy2-wy1);

   tmpworld->aspect = (double)pheight/(double)pwidth;

   /* get coords of window */
   XGetGeometry(mydisplay,tmpworld->win,&tmp,&gx,&gy,
             &(tmpworld->winwidth),&(tmpworld->winheight),&gb,&gd);

   /* set params */
   tmpworld->scalable = scalable;
   tmpworld->gravity  = gravity;
   tmpworld->clipping = CLIPPING;

   /* create CG for Pixmap */
   tmpworld->gcp = XCreateGC(mydisplay,DefaultRootWindow(mydisplay),0,0);
   XCopyGC(mydisplay,initgc,-1,tmpworld->gcp);

   /* create CG for world */
   if (CLIPPING) {
      tmpworld->gcw = XCreateGC(mydisplay,DefaultRootWindow(mydisplay),0,0);
      XCopyGC(mydisplay,initgc,-1,tmpworld->gcw);
      rect.x = px;
      rect.y = py;
      rect.width  = pwidth;
      rect.height = pheight;
      XSetClipRectangles(mydisplay,tmpworld->gcw,0,0,&rect,1,Unsorted);
   } else
      tmpworld->gcw = tmpworld->gcp;

   /* set default color */
   tmpworld->color = black;
   tmpworld->function = GXcopy;

   /* create database-entry */
   winptr = find_win_node(tmpworld->win);
   if(winptr){
      worldptr = winptr->nextworld;
      winptr->nextworld = make_world_node();
      winptr->nextworld->world = tmpworld;
      winptr->nextworld->nextworld = worldptr;
      winptr->nextworld->prevworld = (struct world_node *) NULL;

      if(worldptr) worldptr->prevworld = winptr->nextworld;
   }
   else { fprintf(stderr,"Problems with alloc !\n"); exit(0); }

   return tmpworld;
}

/*=====*\

void ClearWorld(World world){
   ClearArea(world->win,world->px,world->py,world->pwidth,world->pheight);

   if(world->pixmap) XFillRectangle(mydisplay,world->pixmap,cleargc,
               0,0,
               world->pwidth,world->pheight);
   XFlush(mydisplay);
}

/*=====*\
void ExposeWorld(World world){
   if(world->pixmap) {
      XCopyArea(mydisplay,world->pixmap,world->win,mygc,0,0,
            world->pwidth,world->pheight,
            world->px,world->py);
      XFlush(mydisplay);
   }
}

/*=====*\
void ResizeWorld(World world, int newwidth, int newheight)
{
   int delta_width, delta_height, bbx_a, bby_a;
   XRectangle rect;

   /* differences in size */
   delta_width  = (newwidth-world->winwidth);
   delta_height = (newheight-world->winheight);

   /* bounding-box */
   bbx_a = world->bbx;
   bby_a = world->bby;

   /* new coords for non-scalable worlds */
   if( !(world->scalable) )
      switch(world->gravity) {
      case NorthGravity:
         world->px += delta_width/2;
         break;

      case EastGravity:
         world->px += delta_width;
         world->py += delta_height/2;
         break;

      case SouthGravity:
         world->px += delta_width/2;
         world->py += delta_height;
         break;

      case WestGravity:
         world->py += delta_height/2;
         break;

      case NorthEastGravity:
         world->px += delta_width;
         break;

      case SouthEastGravity:
         world->px += delta_width;
         world->py += delta_height;
         break;

      case SouthWestGravity:
         world->py += delta_height;
         break;

      case NorthWestGravity:
      default:
         break;

   } else {

   /* scalable worlds */
      if( world->scalable & FIXED_WIDTH ) {
         switch(world->gravity) {
         case NorthWestGravity:
         case WestGravity:
         case SouthWestGravity:
   break;
         case NorthEastGravity:
         case EastGravity:
         case SouthEastGravity:
   world->bbx += delta_width;
   break;
         default:
   world->bbx += delta_width/2;
   break;
         }
      } else {
         world->bbx += (world->scalable & FLOAT_WEST)  ?
   world->bbx*(delta_width/(double)(world->winwidth)) : 0;

         world->bbwidth = (world->scalable & FLOAT_EAST) ?
   (bbx_a + world->bbwidth)*
      (newwidth/(double)(world->winwidth)) - world->bbx:
      (world->bbwidth + delta_width) + (bbx_a - world->bbx);
      }

      if( world->scalable & FIXED_HEIGHT ) {
         switch(world->gravity) {
         case NorthEastGravity:
         case NorthGravity:
         case NorthWestGravity:
   break;
         case SouthEastGravity:
         case SouthGravity:
         case SouthWestGravity:
   world->bby += delta_height;
   break;
         default:
   world->bby += delta_height/2;
   break;
         }
      } else {

         world->bby += (world->scalable & FLOAT_NORTH) ?
   world->bby*(delta_height/(double)(world->winheight)) : 0;

         world->bbheight = (world->scalable & FLOAT_SOUTH) ?
   (bby_a + world->bbheight)*
      (newheight/(double)(world->winheight)) - world->bby:
      (world->bbheight + delta_height) + (bby_a - world->bby);
      }

      if ( world->scalable & FREE_ASPECT ) {
         world->px = world->bbx; world->py = world->bby;
         world->pwidth = world->bbwidth; world->pheight = world->bbheight;
      } else {
         if ( world->bbwidth * world->aspect > world->bbheight ) {
   world->pheight = world->bbheight;
   world->pwidth = world->pheight/world->aspect;
   world->py = world->bby;
   world->px = world->bbx + (world->bbwidth - world->pwidth)/2;
         } else {
   world->pwidth = world->bbwidth;
   world->pheight = world->pwidth*world->aspect;
   world->px = world->bbx;
   world->py = world->bby + (world->bbheight - world->pheight)/2;
         }
      }

      world->fx = (double)(world->pwidth-1)/(world->x2 - world->x1);
      world->fy = (double)(world->pheight-1)/(world->y2 - world->y1);
   }
   world->winwidth  = newwidth;
   world->winheight = newheight;

   if ( world->pwidth  < 1 ) world->pwidth  = 1;
   if ( world->pheight < 1 ) world->pheight = 1;

   world->aspect = (double)world->pheight/(double)world->pwidth;
   if (world->clipping) {
      rect.x = world->px;
      rect.y = world->py;
      rect.width  = world->pwidth;
      rect.height = world->pheight;
      XSetClipRectangles(mydisplay,world->gcw,0,0,&rect,1,Unsorted);
   }
}

/*=====*\

void    RescaleWorld(World world, double nx1, double ny1, double nx2, double ny2)
{
   world->x1 = nx1;
   world->y1 = ny1;
   world->x2 = nx2;
   world->y2 = ny2;

   world->fx = (double)(world->pwidth-1)/(nx2-nx1);
   world->fy = (double)(world->pheight-1)/(ny2-ny1);
}

/*=====*\

void InitButtons(Window win, const char* buttonstring,
       int width)
{
   struct blist { char type; char text[20]; char key; } *list;

   XSetWindowAttributes b_attributes;
   const char *ptr;
   char  *ptr2;
   int i, j, count = 1,bcount = 0, x, height, gx, gy;
   unsigned int gwidth, gheight, gb, gd;
   Window tmp;

   ptr = buttonstring;
   while(*ptr) if(*ptr++ == ',') (count)++;
   count /= 3;

   /* parse buttonstring */

   list = (struct blist *) calloc(count,sizeof(struct blist));

   ptr = buttonstring; i=0;
   while(*ptr && i<count) {
      list[i].type = *ptr++;
      while ( (*ptr++ != ',') && *ptr);
      ptr2 = list[i].text;
      for(j=0; (*ptr!=',') && (*ptr) && j<30 ; *ptr2++ = *ptr++,j++ );
      if(*ptr++ !=',') { printf("Fehler 2 ! %d \n",i); exit(0); };
      if (list[i].type == 'b' ) list[i].key = *ptr++; else list[i].key = ' ';
      while (*ptr++ !=',' && i<count-1);
      i++;
   }

   /* count buttons */
   for(i=0,bcount=0;i<count;++i) if (list[i].type == 'b') ++bcount;

   /* calculate geometry */
   height = (count + 1) * BUTTONHEIGHT;
   XGetGeometry(mydisplay, win, &tmp, &gx, &gy, &gwidth, &gheight, &gb, &gd );
   x = gwidth - width;

   /* remove existing buttons */
   if ( buttonlist.buttons ) {
      free( buttonlist.buttons );
      ClearWorld( buttonlist.world );
      DestroyWorld( buttonlist.world );
   }

   /* create button-world */
   buttonlist.buttons = (ButtonType *) calloc(bcount,sizeof(ButtonType));
   buttonlist.world = CreateWorld(win,x,0,width,height,0,0,
             width-1,height-1,0,NorthEastGravity);
   buttonlist.NofButtons = bcount;

   b_attributes.win_gravity = NorthEastGravity;

   /* create buttons */
   for(i=0,j=0;i<count;++i) {
      if ( list[i].type == 'b' ) {
/*
         WFillRectangle(buttonlist.world,5,i*BUTTONHEIGHT+2,
             width-10,(i+1)*BUTTONHEIGHT-4,0);
*/
         WDrawRectangle(buttonlist.world,5,i*BUTTONHEIGHT+2,
             width-10,(i+1)*BUTTONHEIGHT-4,1);
         WDrawString(buttonlist.world,10,(i+1)*BUTTONHEIGHT-13,list[i].text,1);

         buttonlist.buttons[j].win =
   XCreateWindow(mydisplay,win,x+5,i*BUTTONHEIGHT+2,
               width-12,BUTTONHEIGHT-4,0,CopyFromParent,
               InputOnly,CopyFromParent,
               CWWinGravity,&b_attributes);

         XSelectInput(mydisplay,buttonlist.buttons[j].win,ButtonPressMask);
         ShowWindow(buttonlist.buttons[j].win);
         buttonlist.buttons[j].keycode = XKeysymToKeycode(mydisplay,list[i].key);
         if ( XKeycodeToKeysym(mydisplay,buttonlist.buttons[j].keycode,0)
      == list[i].key )
   buttonlist.buttons[j++].state = 0;
         else
   buttonlist.buttons[j++].state = 1;

      } else {

         WDrawString(buttonlist.world,5,(i+1)*BUTTONHEIGHT-13,list[i].text,1);
      }
   }
   buttonflag = 1;
   free(list);
}

/*=====*\

int GetEvent(XEvent* event, int wait_flag)
{
   int i, ret;
   int newwidth, newheight;
   struct win_node   *winptr;
   struct world_node *worldptr;
   static int configure_flag=0;
   char buffer[2];

   if(wait_flag)
   {
      /* wait for next event */
      XNextEvent(mydisplay,event);
      ret = 1;
   }
   else
   {
      /* check whether an event is queued */
      if(XEventsQueued(mydisplay,QueuedAfterFlush))
      {
         /* read event */
         XNextEvent(mydisplay,event);
         ret = 1;
      }
      else
      {
         /* if no event queued, return 0 */
         event->type = 0;
         ret = 0;
      }
   };

   /* serve standard-events */
   if(ret)
      {
         switch(event->type) {

         case EnterNotify:
   /* mouse entered window */
/*  XSetInputFocus(mydisplay,event->xcrossing.window,
                RevertToParent,CurrentTime);
*/
   break;

         case MappingNotify:
   /* someone changed keyboard-layout */
   XRefreshKeyboardMapping(&(event->xmapping));
   break;

         case Expose:
   /* window gets visible again */
   if(configure_flag) {
      XClearWindow(mydisplay,event->xexpose.window);

      winptr = find_win_node(event->xexpose.window);
      worldptr = winptr->nextworld;

      while(worldptr) {
         ExposeWorld(worldptr->world);
         worldptr = worldptr->nextworld;
      }
   } else {
      event->type = 0;
      ret = 0;
   }
   configure_flag = 0;
   break;

         case ConfigureNotify:
   /* window geometry changed */
   newwidth  = event->xconfigure.width;
   newheight = event->xconfigure.height;

   winptr = find_win_node(event->xconfigure.window);
   worldptr = winptr->nextworld;

   while(worldptr) {

      ResizeWorld(worldptr->world,newwidth,newheight);
      worldptr = worldptr->nextworld;
   }
   configure_flag = 1;
   break;

         case ButtonPress:
   /* someone pressed a mouse button */
   if (buttonflag) {
      /* check whether it was in a 'button' and return key-event */
      for(i=0; i<buttonlist.NofButtons; i++) {
         if (event->xbutton.window == buttonlist.buttons[i].win) {
            event->type = KeyPress;
            event->xkey.keycode = buttonlist.buttons[i].keycode;
            event->xkey.state = buttonlist.buttons[i].state;

         }
      }
   }
   break;

         case KeyPress:
   /* someone pressed a key */
   if (XLookupString(&(event->xkey),buffer,2,0,0) != 1) {
      event->type = 0;
      ret = 0;
   }
   break;

         default:
   break;
         }
      };
   return ret;
}

/*=====*\

char ExtractChar(XEvent event)
{
   char buffer[2];

   if(event.type != KeyPress) return 0;
   if(XLookupString(&event.xkey,buffer,2,0,0)==1) return buffer[0];
   else return 0;
}

/*=====*\

int WGetMousePos(World world, XEvent event, double *x, double *y)
{
   int ret = 0;

   if(event.type != ButtonPress) return 0;

   /* get coords and convert to world-coords */
   *x = (event.xbutton.x - world->px)/world->fx + world->x1;
   *y = (event.xbutton.y - world->py)/world->fy + world->y1;

   /* in world ? */
   if ( (*x >= world->x1) && (*x <= world->x2) &&
          (*y >= world->y1) && (*y <= world->y2) ) ret = event.xbutton.button;

   return ret;
}

/*=====*\


int GetNumber( Window win, int x, int y, double *value )
{
   char   str[30],  zeichen = ' ';
   int    i=1;
   XEvent myevent;

   str[0] = ' ';str[1] = '\0';

   while ( zeichen != '\15' )           /* repeat until RETURN pressed */
      {
         GetEvent(&myevent,1);

         switch (myevent.type){

         case KeyPress:
   zeichen = ExtractChar(myevent);
   switch(zeichen){

               /**********   BACK SPACE  ***************************************/

   case '\10':
      {
         if(i>0)
            {

      DrawString (win, x,y,str,1);

      str[i+1] = '\0'; str[i] = ' '; str[--i] = ' ';
      DrawString (win, x,y,str,1);
      break;
            }
         else XBell(mydisplay,0);
      }

               /**********   RETURN   ******************************************/

   case '\15':
      {     str[i] = '\0';
      break; }


               /**********   MINUS SIGN  ***************************************/

   case '-'  :
      {     if (i>0) i--; }


               /**********   NUMBER   ******************************************/

   case '0'  :
   case '1'  :
   case '2'  :
   case '3'  :
   case '4'  :
   case '5'  :
   case '6'  :
   case '7'  :
   case '8'  :
   case '9'  :
   case '.'  :


      {        str[i++] = zeichen;
          str[i] = ' ';
          str[i+1] = ' ';
          str[i+2] = '\0';
          DrawString (win, x,y,str,1);
          break;}
   } /* switch zeichen */
   break;

         default: break;
         } /* switch */
      } /* while */


   /**************   Conversion to double *******************************/

   if ( !( str[0] == '\0' || str[1] == '\0') ) {
      *value = atof(str);
      return 1;
   } else return 0;
}



/*=====*\

int WGetNumber( World world, double x, double y, double *value )
{
   char   str[30],  zeichen = ' ';
   int    i=1;
   XEvent myevent;

   str[0] = ' ';str[1] = '\0';

   while ( zeichen != '\15' )           /* repeat until RETURN pressed */
      {
         GetEvent(&myevent,1);

         switch (myevent.type){

         case KeyPress:
   zeichen = ExtractChar(myevent);
   switch(zeichen){

               /**********   BACK SPACE  ***************************************/

   case '\10':
      {     if(i>0)
         {

            WDrawString (world, x,y,str,1);

            str[i+1] = '\0'; str[i] = ' '; str[--i] = ' ';
            WDrawString (world, x,y,str,1);
            break;
         }
      else XBell(mydisplay,0);
            }

               /**********   RETURN   ******************************************/

   case '\15':
      {     str[i] = '\0';
      break; }


               /**********   MINUS SIGN  ***************************************/

   case '-'  :
      {     if (i>0) i--; }


               /**********   NUMBER   ******************************************/

   case '0'  :
   case '1'  :
   case '2'  :
   case '3'  :
   case '4'  :
   case '5'  :
   case '6'  :
   case '7'  :
   case '8'  :
   case '9'  :
   case '.'  :


      {        str[i++] = zeichen;
          str[i] = ' ';
          str[i+1] = ' ';
          str[i+2] = '\0';
          WDrawString (world, x,y,str,1);
          break;}
   } /* switch zeichen */
   break;

         default: break;
         } /* switch */
      } /* while */


   /**************   Conversion to double *******************************/

   if ( !( str[0] == '\0' || str[1] == '\0') ) {
      *value = atof(str);
      return 1;
   }
   else return 0;
}

/*=====*\

int GetString( Window win, int x, int y, int length, char *str )
{
   char   zeichen = ' ';
   int    i=0;
   XEvent myevent;

   str[0] = ' ';str[1] = '\0';

   while ( zeichen != '\15' )           /* repeat until RETURN pressed */
      {
         GetEvent(&myevent,1);

         switch (myevent.type){

         case KeyPress:
   zeichen = ExtractChar(myevent);
   switch(zeichen){

               /**********   BACK SPACE  ***************************************/

   case '\10':
      {
         if(i>0)
            {

      DrawString (win, x,y,str,1);

      XBell(mydisplay,0);

      str[i+1] = '\0'; str[i] = ' '; str[--i] = ' ';
      DrawString (win, x,y,str,1);
      break;
            }
         else XBell(mydisplay,0);
      }

               /**********   RETURN   ******************************************/

   case '\15':
      {     str[i] = '\0';
      break; }


               /**********  CHARACTER   ****************************************/

   default  :


      {        str[i++] = zeichen;
          str[i] = ' ';
          str[i+1] = ' ';
          str[i+2] = '\0';
          DrawString (win, x,y,str,1);
          break;}
   } /* switch zeichen */
   break;

         } /* switch */
      } /* while */


   /******************************************************************/

   if ( !( str[0] == '\0' || str[1] == '\0') ) {
      str[i] = '\0';
      return 1;
   } else return 0;
}



/*=====*\

int WGetString( World world, double x, double y, int length, char *string )
{
   char   str[30],  zeichen = ' ';
   int    i=0;
   XEvent myevent;

   str[0] = ' ';str[1] = '\0';

   while ( zeichen != '\15' )           /* repeat until RETURN pressed */
      {
         GetEvent(&myevent,1);

         switch (myevent.type){

         case KeyPress:
   zeichen = ExtractChar(myevent);
   switch(zeichen){

               /**********   BACK SPACE  ***************************************/

   case '\10':
      {     if(i>0)
         {

            WDrawString (world, x,y,str,1);

            str[i+1] = '\0'; str[i] = ' '; str[--i] = ' ';
            WDrawString (world, x,y,str,1);
            break;
         }
      else XBell(mydisplay,0);
            }

               /**********   RETURN   ******************************************/

   case '\15':
      {     str[i] = '\0';
      break; }


               /**********   CHARACTER   ***************************************/

   default  :


      {        str[i++] = zeichen;
          str[i] = ' ';
          str[i+1] = ' ';
          str[i+2] = '\0';
          WDrawString (world, x,y,str,1);
          break;}
   } /* switch zeichen */
   break;

         } /* switch */
      } /* while */


   /***************************************************************/

   if ( !( str[0] == '\0' || str[1] == '\0') ) {
      str[i] = '\0';
      strcpy(string,str);
      return 1;
   }
   else return 0;
}



/*=====*\

void WSetColor(World world, unsigned long color_func)
{
   unsigned long color;
   unsigned function;

   /* extract color and drawing-function */
   function  = (color_func&4096)?(color_func/256)%16:3;
   color     = (function==GXxor)?                  /* colors should appear */
      mycolors[(color_func%256)%NofColors]^white:   /* normal on white      */
         mycolors[(color_func%256)%NofColors];       /* background           */

   if(color != world->color) {
      XSetForeground(mydisplay,world->gcw,color);
      if(world->pixmap) XSetForeground(mydisplay,world->gcp,color);
      world->color = color;
   }

   if(function != world->function) {
      XSetFunction(mydisplay,world->gcw,function);
      if(world->pixmap) XSetFunction(mydisplay,world->gcp,function);
      world->function = function;
   }
}


void SetColor(unsigned long color_func)
{
   unsigned long color;
   unsigned function;

   function  = (color_func&4096)?(color_func/256)%16:3;
   color     = (function==GXxor)?
      mycolors[(color_func%256)%NofColors]^white:
         mycolors[(color_func%256)%NofColors];

   if(color != global_color) {
      XSetForeground(mydisplay,mygc,color);
      global_color = color;
   }

   if(function != global_function) {
      XSetFunction(mydisplay,mygc,function);
      global_function = function;
   }

}


/*=====*\

void ClearArea(Window win, int x, int y, int width, int height)
{
   XClearArea(mydisplay,win,x,y,width,height,False);
}

/*=====*\

void DrawPoint(Window win, int x, int y, int c)
{
   SetColor(c);

   XDrawPoint(mydisplay,win,mygc,x,y);
}

/*=====*\

void DrawPoints(Window win, XPoint *points, int NofPoints, int c)
{
   SetColor(c);

   XDrawPoints(mydisplay, win, mygc, points, NofPoints, CoordModeOrigin);
}

/*=====*\

void DrawLine(Window win, int x1,int y1, int x2, int y2, int c)
{
   SetColor(c);

   XDrawLine(mydisplay,win,mygc,x1,y1,x2,y2);
}

/*=====*\

void DrawLines(Window win, XPoint *points, int NofPoints, int c)
{
   SetColor(c);

   XDrawLines(mydisplay, win, mygc, points, NofPoints, CoordModeOrigin);
}


/*=====*\

void DrawCircle(Window win, int x, int y, int r, int c)
{
   int rr;

   rr = abs(r);
   SetColor(c);

   XDrawArc(mydisplay,win,mygc,x-rr,y-rr,2*rr,2*rr,0,23040);
}

void FillCircle(Window win, int x, int y, int r, int c)
{
   int rr;

   rr = abs(r);
   SetColor(c);

   XFillArc(mydisplay,win,mygc,x-rr,y-rr,2*rr,2*rr,0,23040);
}


/*=====*\

void DrawString(Window win, int x, int y, const char* text, int c)
{
   SetColor(c);

   XDrawImageString(mydisplay,win,mygc,x,y,text,strlen(text));
   XFlush(mydisplay);
}

/*=====*\

void DrawRectangle(Window win, int x1, int y1, int x2, int y2, int c)
{
   int temp;
   SetColor(c);

   if (x1 > x2) { temp=x1; x1=x2; x2=temp; };
   if (y1 > y2) { temp=y1; y1=y2; y2=temp; };

   XDrawRectangle(mydisplay, win, mygc, x1, y1, x2-x1, y2-y1);
   XFlush(mydisplay);
}

void FillRectangle(Window win, int x1, int y1, int x2, int y2, int c)
{
   int temp;
   SetColor(c);

   if (x1 > x2) { temp=x1; x1=x2; x2=temp; };
   if (y1 > y2) { temp=y1; y1=y2; y2=temp; };

   XFillRectangle(mydisplay, win, mygc, x1, y1, x2-x1, y2-y1);
   XFlush(mydisplay);
}

/*=====*\

void DrawPoly(Window win, XPoint *points, int NofPoints, int c)
{
   int i;
   XPoint *xpoints;

   if( !(xpoints = (XPoint*) calloc(NofPoints+1,sizeof(XPoint)) ) ) {
      fprintf(stderr,"Calloc error XLines !!!\n");
      exit(-1);
   }


   for(i=0; i<NofPoints; ++i) {
      xpoints[i].x = points[i].x;
      xpoints[i].y = points[i].y;
   }

   if ((xpoints[0].x != xpoints[NofPoints-1].x)
      ||(xpoints[0].y != xpoints[NofPoints-1].y)) {
         xpoints[NofPoints].x = xpoints[0].x;
         xpoints[NofPoints].y = xpoints[0].y;
         NofPoints ++;
      }


   SetColor(c);

   XDrawLines(mydisplay,win,mygc,
            xpoints,NofPoints,CoordModeOrigin);

   free(xpoints);

   XFlush(mydisplay);
}

/*=====*\

void FillPoly(Window win, XPoint *points, int NofPoints, int c, int cfill)
{
   int i;
   XPoint *xpoints;

   if( !(xpoints = (XPoint*) calloc(NofPoints+1,sizeof(XPoint)) ) ) {
      fprintf(stderr,"Calloc error XLines !!!\n");
      exit(-1);
   }


   for(i=0; i<NofPoints; ++i) {
      xpoints[i].x = points[i].x;
      xpoints[i].y = points[i].y;
   }

   if ((xpoints[0].x != xpoints[NofPoints-1].x)
      ||(xpoints[0].y != xpoints[NofPoints-1].y)) {
         xpoints[NofPoints].x = xpoints[0].x;
         xpoints[NofPoints].y = xpoints[0].y;
         NofPoints ++;
      }

   SetColor(cfill);

   XFillPolygon(mydisplay, win, mygc, points, NofPoints, Complex,
             CoordModeOrigin);


   SetColor(c);

   XDrawLines(mydisplay,win,mygc,
            xpoints,NofPoints,CoordModeOrigin);

   free(xpoints);

   XFlush(mydisplay);
}

/*=====*\


void WDrawPoint(World world, double x, double y, int c)
{
   int px, py;

   px = (x-world->x1)*world->fx;
   py = (y-world->y1)*world->fy;

   WSetColor(world,c);

   XDrawPoint(mydisplay,world->win,world->gcw,world->px+px,world->py+py);
   if(world->pixmap) XDrawPoint(mydisplay,world->pixmap,world->gcp,px,py);
   XFlush(mydisplay);
}

/*=====*\

void WDrawPoints(World world, WPoint *points, int NofPoints, int c)
{
   int i;
   XPoint *xpoints;

   if( !(xpoints = (XPoint*) calloc(NofPoints,sizeof(XPoint)) ) ) {
      fprintf(stderr,"Calloc error XPoints !!!\n");
      exit(-1);
   }


   for(i=0; i<NofPoints; ++i) {
      xpoints[i].x = world->px + (points[i].x-world->x1)*world->fx;
      xpoints[i].y = world->py + (points[i].y-world->y1)*world->fy;
   }

   WSetColor(world,c);

   XDrawPoints(mydisplay,world->win,world->gcw,
            xpoints,NofPoints,CoordModeOrigin);

   if(world->pixmap){
      for(i=0; i<NofPoints; ++i) {
         xpoints[i].x -= world->px;
         xpoints[i].y -= world->py;
      }

      XDrawPoints(mydisplay,world->pixmap,world->gcp,
      xpoints,NofPoints,CoordModeOrigin);
   }
   free(xpoints);
}

/*=====*\

void WDrawLine(World world, double x1, double y1, double x2, double y2, int c)
{
   int px1, px2, py1, py2;

   px1 = (x1-world->x1)*world->fx;
   py1 = (y1-world->y1)*world->fy;
   px2 = (x2-world->x1)*world->fx;
   py2 = (y2-world->y1)*world->fy;

   WSetColor(world,c);

   XDrawLine(mydisplay,world->win,world->gcw,
         world->px+px1,world->py+py1,world->px+px2,world->py+py2);

   if(world->pixmap) XDrawLine(mydisplay,world->pixmap,world->gcp,
                  px1,py1,px2,py2);

   XFlush(mydisplay);
}

/*=====*\

void WDrawLines(World world, WPoint *points, int NofPoints, int c)
{
   int i;
   XPoint *xpoints;

   if( !(xpoints = (XPoint*) calloc(NofPoints,sizeof(XPoint)) ) ) {
      fprintf(stderr,"Calloc error XLines !!!\n");
      exit(-1);
   }


   for(i=0; i<NofPoints; ++i) {
      xpoints[i].x = world->px + (points[i].x-world->x1)*world->fx;
      xpoints[i].y = world->py + (points[i].y-world->y1)*world->fy;
   }

   WSetColor(world,c);

   XDrawLines(mydisplay,world->win,world->gcw,
            xpoints,NofPoints,CoordModeOrigin);

   if(world->pixmap){
      for(i=0; i<NofPoints; ++i) {
         xpoints[i].x -= world->px;
         xpoints[i].y -= world->py;
      }

      XDrawLines(mydisplay,world->pixmap,world->gcp,
      xpoints,NofPoints,CoordModeOrigin);
   }
   free(xpoints);

   XFlush(mydisplay);
}

/*=====*\

void WDrawCircle(World world, double x, double y, double r, int c)
{
   int px, py, prx, pry;

   px = (x-world->x1)*world->fx;
   py = (y-world->y1)*world->fy;
   prx = abs((int) (r*world->fx));
   pry = abs((int) (r*world->fy));

   if(prx==0) prx=1;
   if(pry==0) pry=1;

   WSetColor(world,c);

   XDrawArc(mydisplay,world->win,world->gcw,world->px+px-prx,
       world->py+py-pry,2*prx,2*pry,0,23040);

   if(world->pixmap) XDrawArc(mydisplay,world->pixmap,world->gcp,px-prx,
               py-pry,2*prx,2*pry,0,23040);
}

void WFillCircle(World world, double x, double y, double r, int c)
{
   int px, py, prx, pry;

   px = (x-world->x1)*world->fx;
   py = (y-world->y1)*world->fy;
   prx = abs((int) (r*world->fx));
   pry = abs((int) (r*world->fy));

   if(prx==0) prx=1;
   if(pry==0) pry=1;

   WSetColor(world,c);

   XFillArc(mydisplay,world->win,world->gcw,world->px+px-prx,
       world->py+py-pry,2*prx,2*pry,0,23040);

   if(world->pixmap) XFillArc(mydisplay,world->pixmap,world->gcp,px-prx,
               py-pry,2*prx,2*pry,0,23040);
}


/*=====*\

void WDrawString(World world, double x, double y, const char* text, int c)
{
   int px,py;

   px = (x-world->x1)*world->fx;
   py = (y-world->y1)*world->fy;

   WSetColor(world,c);

   XDrawImageString(mydisplay,world->win,world->gcw,
          world->px+px,world->py+py,text,strlen(text));

   if(world->pixmap)   XDrawImageString(mydisplay,world->pixmap,world->gcp,
                     px,py,text,strlen(text));
   XFlush(mydisplay);
}

/*=====*\

void WDrawRectangle(World world, double x1, double y1, double x2, double y2, int c)
{
   int px1,px2,py1,py2, temp;

   px1 = (x1-world->x1)*world->fx;
   py1 = (y1-world->y1)*world->fy;
   px2 = (x2-world->x1)*world->fx;
   py2 = (y2-world->y1)*world->fy;

   if (px1>px2) { temp=px1; px1=px2; px2=temp; };
   if (py1>py2) { temp=py1; py1=py2; py2=temp; };

   WSetColor(world,c);

   XDrawRectangle(mydisplay, world->win, world->gcw,
       world->px+px1, world->py+py1, px2-px1, py2-py1);
   if(world->pixmap) XDrawRectangle(mydisplay, world->pixmap, world->gcp,
               px1, py1, px2-px1, py2-py1);
   XFlush(mydisplay);
}


void WFillRectangle(World world, double x1, double y1, double x2, double y2, int c)
{
   int px1,px2,py1,py2, temp;

   px1 = (x1-world->x1)*world->fx;
   py1 = (y1-world->y1)*world->fy;
   px2 = (x2-world->x1)*world->fx;
   py2 = (y2-world->y1)*world->fy;

   if (px1>px2) { temp=px1; px1=px2; px2=temp; };
   if (py1>py2) { temp=py1; py1=py2; py2=temp; };

   WSetColor(world,c);

   XFillRectangle(mydisplay, world->win, world->gcw,
       world->px+px1, world->py+py1, px2-px1, py2-py1);
   if(world->pixmap) XFillRectangle(mydisplay, world->pixmap, world->gcp,
               px1, py1, px2-px1, py2-py1);
   XFlush(mydisplay);
}

/*=====*\

void WDrawPoly(World world, WPoint *points, int NofPoints, int c)
{
   int i;
   XPoint *xpoints;

   if( !(xpoints = (XPoint*) calloc(NofPoints+1,sizeof(XPoint)) ) ) {
      fprintf(stderr,"Calloc error XLines !!!\n");
      exit(-1);
   }


   for(i=0; i<NofPoints; ++i) {
      xpoints[i].x = world->px + (points[i].x-world->x1)*world->fx;
      xpoints[i].y = world->py + (points[i].y-world->y1)*world->fy;
   }

   if ((xpoints[0].x != xpoints[NofPoints-1].x)
      ||(xpoints[0].y != xpoints[NofPoints-1].y)) {
         xpoints[NofPoints].x = xpoints[0].x;
         xpoints[NofPoints].y = xpoints[0].y;
         NofPoints ++;
      }


   WSetColor(world,c);

   XDrawLines(mydisplay,world->win,world->gcw,
            xpoints,NofPoints,CoordModeOrigin);

   if(world->pixmap){
      for(i=0; i<NofPoints; ++i) {
         xpoints[i].x -= world->px;
         xpoints[i].y -= world->py;
      }

      XDrawLines(mydisplay,world->pixmap,world->gcp,
      xpoints,NofPoints,CoordModeOrigin);
   }
   free(xpoints);

   XFlush(mydisplay);
}

/*=====*\

void WFillPoly(World world, WPoint *points, int NofPoints, int c, int cfill)
{
   int i;
   XPoint *xpoints;

   if( !(xpoints = (XPoint*) calloc(NofPoints+1,sizeof(XPoint)) ) ) {
      fprintf(stderr,"Calloc error XLines !!!\n");
      exit(-1);
   }

   for(i=0; i<NofPoints; ++i) {
      xpoints[i].x = world->px + (points[i].x-world->x1)*world->fx;
      xpoints[i].y = world->py + (points[i].y-world->y1)*world->fy;
   }

   WSetColor(world,cfill);

   XFillPolygon(mydisplay, world->win, world->gcw, xpoints, NofPoints,
             Complex, CoordModeOrigin);

   if ((xpoints[0].x != xpoints[NofPoints-1].x)
      ||(xpoints[0].y != xpoints[NofPoints-1].y)) {
         xpoints[NofPoints].x = xpoints[0].x;
         xpoints[NofPoints].y = xpoints[0].y;
         NofPoints ++;
      }

   WSetColor(world,c);

   XDrawLines(mydisplay,world->win,world->gcw,
            xpoints,NofPoints,CoordModeOrigin);

   if(world->pixmap){
      for(i=0; i<NofPoints; ++i) {
         xpoints[i].x -= world->px;
         xpoints[i].y -= world->py;
      }

      WSetColor(world,cfill);

      XFillPolygon(mydisplay, world->pixmap, world->gcp, xpoints, NofPoints,
       Complex, CoordModeOrigin);

      WSetColor(world,c);

      XDrawLines(mydisplay,world->pixmap,world->gcp,
      xpoints,NofPoints,CoordModeOrigin);
   }
   free(xpoints);

   XFlush(mydisplay);
}
/*End*/



Ultima edición por compusystems el 08/10/2012 8:00 pm; editado 1 vez
Volver arriba
compusystems



Registrado: 05 Oct 2012
Mensajes: 13
Ubicación: Colombia

MensajePublicado: 05/10/2012 10:04 am
Título:

2do Archivo del proyecto

Xgraphics.h

Código:
/*

File Name: Xgraphics.h
Version:  1.00 ( 28/March/1996 )
By:         Martin Lueders (lueders@physik.uni-wuerzburg.de)


Copyright (C)
**  This program is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or  (at your option) any later version.
**  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
**  You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#ifndef _XGRAPHICS_
      //#then

      #define _XGRAPHICS_

      #include <X11/Xlib.h> // Nota: la biblioteca Xlib.h se encuentra en http://ftp.x.org/pub/?C=M;O=A
      #include <X11/Xutils.c> //  mas info en: http://en.wikipedia.org/wiki/Xlib

      #include <ctype.h>
      #include <stdlib.h>
      #include <string.h>
      #include <stdio.h>

      #define BUTTONHEIGHT 35
      #define MAX_COLORS   16
      #define CLIPPING      1
      #define XOR          4096+256*GXxor

   /* Constants for scalable */

   #define SCALABLE       1
   #define FREE_ASPECT    2
   #define FLOAT_NORTH    4
   #define FLOAT_SOUTH    8
   #define FLOAT_EAST    16
   #define FLOAT_WEST    32
   #define FIXED_WIDTH   64
   #define FIXED_HEIGHT 128


   /* Declarations of used datatypes */

   typedef struct world_type {
     Window   win;
     Pixmap   pixmap;
     GC       gcw, gcp;
     int      px, py, pwidth, pheight,
              bbx, bby;
     unsigned int bbwidth, bbheight,
              winwidth, winheight;
     double   x1, y1, x2, y2;
     double   fx, fy, aspect;
     int      scalable;
     int      clipping;
     int      gravity;
     unsigned long color;
     int      function;
   } Worldstruct, *World;


   typedef struct w_point {
     double x, y;
   } WPoint;


   typedef struct button_type {
     Window  win;
     int     keycode;
     int     state;
   } ButtonType;

   typedef struct button_list {
     ButtonType *buttons;
     World      world;
     int        NofButtons;
   } ButtonList;

   /* Declaration of variables */

   Display         *mydisplay;
   int             myscreen;
   Window          mywindow;
   World           myworld;
   unsigned long   black,white,global_color;
   int             global_function;
   GC              mygc,cleargc,initgc;
   XEvent          myevent;
   XSetWindowAttributes attributes;
   int             buttonflag;
   ButtonList      buttonlist;
   unsigned long   mycolors[MAX_COLORS];
   int             NofColors;
   Colormap        mycmap;

   static char *colorlist[] = {
     "white",
     "black",
     "red",
     "green",
     "blue",
     "yellow",
     "orange",
     "brown",
     "cyan",
     "gray",
     "tomato1",
     "limegreen",
     "lightblue",
     "yellow4",
     "orangered",
     "darkgreen",
     "blueviolet"
   };

   static char *graylist[] = {
     "white",
     "black",
     "gray90",
     "gray85",
     "gray80",
     "gray75",
     "gray70",
     "gray65",
     "gray60",
     "gray55",
     "gray50",
     "gray45",
     "gray40",
     "gray35",
     "gray30",
     "gray25",
     "gray20"
   };

   struct world_node {
     World  world;
     struct world_node *prevworld,
                       *nextworld;
   } *make_world_node(), *find_world_node(World world);

   struct win_node {
     Window win;
     struct world_node *nextworld;
     struct win_node   *prevwin,
                       *nextwin;
   } *worldlist, *make_win_node(), *find_win_node(Window window);

   void WSetColor(World world, unsigned long color);
   void SetColor(unsigned long color);

   /*****************************************************************************/

   /* Initialisation of X-parameters */

   void  InitX();

   /*
      InitX() provides the connection to the X-server and sets defaults for
      the appearance of the windows.
   */


   void  ExitX();

   /*
      ExitX() closes all remaining windows, frees the allocated memory and
      in the end shuts down the connection to the X-server.
   */


   /*****************************************************************************/

   /* Window-handling */

   Window  CreateWindow(
              int width, int height,         /* window size           */
              char *name                     /* window title          */
              );
   /*
      Creates a window with th given defaults.
   */


   void  ShowWindow(Window win);

   /*
      Displays the window.
   */


   void  HideWindow(Window win);

   /*
      Removes the window from the screen.
   */


   void  DestroyWindow(Window win);

   /*
      Closes the window.
   */


   void  ClearWindow(Window win);

   /*
      Clears the window.
   */

   /*****************************************************************************/

   /* World handling */
   World CreateWorld(
             Window win,
             int px, int py,
             int pwidth, int pheight,
             double wx1, double wy1, double wx2, double wy2,
                    int scalable,
             int gravity
             );

   /*
      Creates a world in the window win with the window-coordinates (px,py)
      and the size (pwidth, pheight). Defines a new coordinatesystem with
      the upper left corner (wx1,wy1) and the lower right corner (wx2,wy2).
      The resizing behavior is defined by scalable and gravity.
   */


   void ClearWorld(World world);

   /*
      Clears a World.
   */

   void  DestroyWorld(World world);

   /*
      Destroys a world and frees allocated memory.
   */

   void ExposeWorld(World world);

   /*
      Redraws non-scalable worlds. Used internal to handle Expose-events.
   */


   void ResizeWorld(World world, int newwidth, int newheight);

   /*
      Changes the size of a scalable world after the window is resized.
   */


   void RescaleWorld(World world, double nx1, double ny1, double nx2, double ny2);

   /*
      Changes the world-coordinate system.
   */

   /*****************************************************************************/
   /* Event handling */

   void  InitButtons(Window win, const char* buttonstring, int width);

   /*
      Creates a row of buttons at the right side of te window.
      The syntax of buttonstring is:
        "[t/b],[text],[key], [t/b],[text],[key],  ... "
      Entries with 't' create a textline, entries with 'b' create a button,
      which when clicked with the mouse, returns a KeyPress event for the
      key 'key'. This can be handled as a normal KeyPress event.
   */

   int GetEvent(XEvent* event, int wait_flag);

   /*
      GetEvent reads an event from the event queue of the program.
      If wait_flag > 0, the function waits until the next event ( to
      conserv computing time ), otherwise the function return 0 and a
      event type 0 if no event is in the queue.
   */


   char ExtractChar(XEvent event);

   /*
      ExtractChar() returns the character code of the key pressed from an
      KeyEvent.
   */


   int GetNumber(Window win, int x, int y, double *value);

   /*
      GetNumber allows to enter a number in the window with editing the
      line by using BACKSPACE. The input appears in the window at (x,y).
   */

   int WGetNumber(World world, double x, double y, double *value);

   /*
      Like GetNumber, but with world-coordinates.
   */


   int GetString(Window win, int x, int y, int length, char *string);

   /*
      GetNumber allows to enter a string in the window with editing the
      line by using BACKSPACE. The input appears in the window at (x,y).
   */

   int WGetString(World world, double x, double y, int length, char *string);

   /*
      Like GetString, but with world-coordinates.
   */

   int   WGetMousePos(World world, XEvent event, double *x, double *y);

   /*
      Returns the position of a mouse click in coordinates of the world
      given as argument.
   */

   /* Drawingroutines for Windows */

   void  ClearArea(Window win, int x, int y, int width, int height);

   void  SetColor(unsigned long color);
   void  SetFunction(int func);

   void  DrawPoint(Window win, int x, int y, int c);
   void  DrawPoints(Window win, XPoint *points, int NofPoints, int c);
   void  DrawLine(Window win, int x1, int y1, int x2, int y2, int c);
   void  DrawLines(Window win, XPoint *points, int NofPoints, int c);
   void  DrawCircle(Window win, int x, int y, int r, int c);
   void  FillCircle(Window win, int x, int y, int r, int c);
   void  DrawString(Window win, int x, int y, const char* text, int c);
   void  DrawRectangle(Window win, int x1, int y1, int x2, int y2, int c);
   void  FillRectangle(Window win, int x1, int y1, int x2, int y2, int c);
   void  DrawPoly(Window win, XPoint *points, int NofPoints, int c);
   void  FillPoly(Window win, XPoint *points, int NofPoints, int c, int cfill);


   /* Drawingroutines for worlds */

   void  WSetColor(World world, unsigned long color);
   void  WSetFunction(World world, int func);

   void  WDrawPoint(World world, double x, double y, int c);
   void  WDrawPoints(World world, WPoint *points, int NofPoints, int c);
   void  WDrawLine(World world, double x1, double y1, double x2, double y2, int c);
   void  WDrawLines(World world, WPoint *points, int NofPoints, int c);
   void  WDrawCircle(World world, double x, double y, double r, int c);
   void  WFillCircle(World world, double x, double y, double r, int c);
   void  WDrawString(World world, double x, double y, const char* text, int c);
   void  WDrawRectangle(World world,
                double x1, double y1, double x2, double y2, int c);
   void  WFillRectangle(World world,
                double x1, double y1, double x2, double y2, int c);
   void  WDrawPoly(World world, WPoint *points, int NofPoints, int c);
   void  WFillPoly(World world,
             WPoint *points, int NofPoints, int c, int cfill);

#endif


Ultima edición por compusystems el 08/10/2012 8:06 pm; editado 2 veces
Volver arriba
compusystems



Registrado: 05 Oct 2012
Mensajes: 13
Ubicación: Colombia

MensajePublicado: 05/10/2012 10:05 am
Título:

3cer archivo del proyecto

"Ejemplo63.c"

Código:


// Ejemplo 63 pagina 158 By: Manuel José Páez Mejía
/*

En el siguiente programa informático escrito en lenguaje c, se utiliza el mínimo numero de instrucciones que debe tener una aplicación utilizando la biblioteca Xgraphics.c

Las partes del programa son:

A: La inclusión del encabezado "Xgraphics.h"
B: Declaración del tipo de objeto myVentana, la cual es una estructura de datos con la información de la ventana
C: Declaración de un identificador para detectar el evento para tomar una acción (presionar el botón izquierdo del ratón por ejemplo para cerrar la ventana),
D: Especificar anchura, altura y nombre de la ventana con la funcion CreateWindow( ... )
E: Se dibujan diferentes figuras geométricas con las funciones DrawLine, DrawCircle, FillCircle, DrawRectangle, DrawString
*/

#include <stdlib.h>
#include "Xgraphics.h"

// Declaracion de identificadores y objetos.
Window miVentana;
XEvent miEvento;
int i, done = 0;

// Inicializacion de identificadores.
done = 0;

main( ){

   InitX(); // Habilita modo gráfico
   miVentana = CreateWindow( 400, 410, "TITLE: PRIMITIVOS"); // Crea una ventana de 400 * 390 pixeles en blanco
   ShowWindow( miVentana ); // Despliega la ventana en el PC
        unsigned int count;

   for( count = 1; count < 20; count = count + 1;) {
      DrawLine( miVentana, 10, 10+5*count, 300, 10+5*count, count );//  Dibuja rectas.
   }

   DrawCircle( miVentana, 150, 150, 40, 4 ); // Dibuja un circulo
   DrawString( miVentana, 150, 250, "Mi circulo", 6 ); // Etiqueta el circulo
   FillCircle( miVentana, 300, 250, 10, 7); // Dibuja un circulo relleno.
   DrawRentangle( miVentana, 2, 2, 398, 398, 3 ); // Crea un marco.

   while( !done ){
      GetEvent( &miEvento, 1); // Cierra ventana si ...
      if( miEvento.type == ButtomPress){
         done = 1;
      }
   ExitX(); // Cierra Ventanas
   }
}


Ultima edición por compusystems el 08/10/2012 8:22 pm; editado 3 veces
Volver arriba
compusystems



Registrado: 05 Oct 2012
Mensajes: 13
Ubicación: Colombia

MensajePublicado: 05/10/2012 10:06 am
Título:

Para crear el proyecto solo se requiere básicamente de 4 archivos:

Cita:
Xgraphics.c // Biblioteca con funciones básicas de una GUI, que se puede extraer de arriba, al igual que la siguiente
Xgraphics.h //
xlib.h         // Biblioteca requerida por Xgraphics que se descarga de los links de abajo
Ejemplo63.c o Ejemplo66.c // Ejemplos de prueba que se pueden ver del libro mencionado anteriormente y que se anexa también arriba


Posibles causas de una compilación NO satisfactoria:

A- Compilador adecuado, yo uso el compilador "ZINJAI" http://zinjai.sourceforge.net/

B- Ubicación de los archivos Xlib

#include <X11/V10R3/include/X/Xlib.h>  // Nota: la biblioteca Xlib.h se encuentra en http://ftp.x.org/pub/?C=M;O=A
#include <X11/V10R3/xshell/Xutils.c>   //  mas info en: http://en.wikipedia.org/wiki/Xlib

Muchos subdirectorios

C- Hay diferentes versiones de Xlib que pueden generar conflictos

      http://ftp.x.org/pub/?C=M;O=A
Parent Directory -
X10R3/ 04-May-2001 08:00 -
X10R4/ 04-May-2001 08:00 -
X11R1/ 04-May-2001 08:00 -
X11R2/ 04-May-2001 08:00 -
X11R3/ 04-May-2001 08:00 -
X11R4/ 04-May-2001 08:00 -
X11R5/ 04-May-2001 08:00 -
X11R6.1/ 04-May-2001 08:00 -
X11R6.3/ 04-May-2001 08:00 -
X11R6/ 04-May-2001 08:00 -
unsupported/ 04-May-2001 08:00 -
X11R6.5.1/ 09-Aug-2001 08:00 -
X11R6.7/ 14-May-2004 09:22 -
X11R6.4/ 21-Sep-2004 08:00 -
X11R6.6/ 21-Sep-2004 08:00 -
X11R6.7.0/ 29-Dec-2004 15:09 -
X11R6.8.0/ 29-Dec-2004 15:09 -
X11R6.8.1/ 29-Dec-2004 15:09 -
X11R6.8.2/ 09-Feb-2005 16:45 -
XTS5.0.2/ 18-Apr-2005 08:00 -
X11R7.0/ 21-Dec-2005 11:56 -
X11R6.9.0/ 21-Dec-2005 16:25 -
X11R7.1/ 22-May-2006 19:30 -
XTS/ 13-Dec-2006 10:59 -
X11R7.2/ 15-Feb-2007 17:56 -
X11R7.3/ 06-Sep-2007 05:45 -
X11R7.4/ 23-Sep-2008 16:27 -
X11R7.5-RC1/ 18-Oct-2009 02:00 -
X11R7.6-RC1/ 12-Nov-2010 23:00 -
X11R7.5/ 19-Dec-2010 04:02 -
individual/ 24-May-2011 20:39 -
X11R7.7-RC1/ 05-Jun-2012 23:08 -
X11R7.6/ 05-Jun-2012 23:08 -
X11R7.7/ 06-Jun-2012 18:50 -
current/ 06-Jun-2012 18:50

Código:
El error se genera al tratar de leer la biblioteca xLIB.h y no se porque razón, a ver si alguno de vosotros saca un ratito y compila EL EJEMPLO ANTERIOR


Espero me puedan ayudar Embarassed, pues parece ser este foro el mejor de todos con respecto al lenguaje C

Gracias
Volver arriba
Sorancio



Registrado: 29 May 2009
Mensajes: 1157
Ubicación: España

MensajePublicado: 05/10/2012 10:53 am
Título:

¡Hola!

Habría que saber que Sistema Operativo usas: X11 es para Linux y aún así no todos los sitemas tienen X11.

X11 no es "simple". Es super complejísimo. Imagino que el libro es sólo una introducción. X11 es la base de entornos gráficos como XFCE o GNOME.

Si buscas una interfaz amigable en C, utiliza GTK+. Si buscas una interfaz simple en C++, usa Qt o wxWidgets.
_________________
Mi página web (en inglés):
Volver arriba
daltomi



Registrado: 28 Abr 2007
Mensajes: 335
Ubicación: Argentina

MensajePublicado: 06/10/2012 1:11 pm
Título:

Buenas.

El programa muestra lo siguiente, enlace


Debes correguir muchas cosas:
1- Existe un "parásito" en el archivo Xgraphics.h: algunos bloques de comentarios son erróneos en su cierre
/* */ <--- Correcto
/* \* <---- Incorrecto

2- Los archivos de encabezado del Xlib dependerá de tú SO, en mi caso (GNU/Linux) es:
Código:

#include <X11/Xlib.h>
#include <X11/Xutil.h>


Deben ir después de #ifndef..., no antes.


3- Errores del archivo ejemploXXX.c
a) no existe myVentana, debe ser miVentana.
b) no existe count, debe ser unsigned int count
c) no se "pinta" nada en pantalla porque el evento Expose borra el fondo con color blanco.

Para solucionar esto, debes capturar el evento Expose y pintar allí los gráficos:

Código:

     while( !done ){
      GetEvent( &miEvento, 1);

      if( miEvento.type == Expose ) //<--- Nuevo
      {
          DrawString( miVentana, 150, 250, "Mi circulo", 6 ); // Etiqueta el circulo
          FillCircle( miVentana, 300, 250, 10, 7); // Dibuja un circulo relleno.
          DrawRectangle( miVentana, 2, 2, 398, 398, 3 ); // Crea un marco.
          for( count = 1; count < 20; count++) {
                 DrawLine( miVentana, 10+5*count, 300, 1+5*count, count,2 );//  Dibuja rectas.
          }
      }
     


Para enlazar el programa, en mi caso, -lX11

No recuerdo en éste momento si existe otro error.
Saludos.
Volver arriba
Dirección AIM
compusystems



Registrado: 05 Oct 2012
Mensajes: 13
Ubicación: Colombia

MensajePublicado: 08/10/2012 7:25 pm
Título:

Muchas gracias, Daltomi , corrigiendo archivos ...

Antes Xgraphics.h
Código:

#include <X11/Xlib.h>
#include <X11/Xutils.c>

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

 #ifndef _XGRAPHICS_
      //#then
      #define _XGRAPHICS_
      #define BUTTONHEIGHT 35
      #define MAX_COLORS   16
      ...
#endif
     



Ahora Xgraphics.h
Código:

#ifndef _XGRAPHICS_
      //#then
      #define _XGRAPHICS_

      #include <X11/Xlib.h>
      #include <X11/Xutils.c>

      #include <ctype.h>
      #include <stdlib.h>
      #include <string.h>
      #include <stdio.h>


      #define BUTTONHEIGHT 35
      #define MAX_COLORS   16
      ...
#endif
 


Entonces en un archivo .h el bloque #ifndef ... #endif debe incluir por regla los #include? esto es una regla general?

Cita:
X11 no es "simple". Es super complejísimo. Imagino que el libro es sólo una introducción. X11 es la base de entornos gráficos como XFCE o GNOME
.

pero a simple vista las funciones de la biblioteca xGraphics.c se ven amigables, aunque estas llaman a las funciones de x11

Cita:
Si buscas una interfaz amigable en C, utiliza GTK+. Si buscas una interfaz simple en C++, usa Qt o wxWidgets.


Voy ha incluir mas ejemplos con xGraphics y por favor puedes hacer el port a Qt o wxWidgets para ver una comparacion entre codigos

Muchas Gracias Smile
Volver arriba
      Índice del Foro elrincondelc.com -> Gráficos
Página 1 de 1Todas las horas están en GMT - 8 Horas

 
No puede crear mensajes
No puede responder temas
No puede editar sus mensajes
No puede borrar sus mensajes
No puede votar en encuestas

(c) ElRincondelC.com

Un proyecto de UrlanHeat.com