File: basics.c | Size: 3,396 bytes | Download file | Back to directory listing | BWPOW's homepage
/*
    expC - Expressions analyzator and viewer
    Copyright (C) 2004 Samuel Kupka
 
    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 <stdio.h>
#include <string.h>
#include <allegro.h>
 
#include "expC.h"
 
typedef struct{
  int x,y,w,h,status,retval;
  char text[64];
}BUTT;
 
#define MAX_BUTTONS 64
 
BUTT butt[MAX_BUTTONS];
int num_butt;
 
void basic_clear_buttons(void)
{
  num_butt=0;
}
 
void basic_add_button(short x,short y,short w,short h,char *text,int retval)
{
  if(num_butt>=MAX_BUTTONS) return;
  butt[num_butt].x=x;
  butt[num_butt].y=y;
  butt[num_butt].w=w;
  butt[num_butt].h=h;
  butt[num_butt].status=0;
  butt[num_butt].retval=retval;
  strcpy(butt[num_butt].text,text);
 
  num_butt++;
}
 
int basic_check_buttons(int mx,int my)
{
  int i;
 
  for(i=0;i<num_butt;i++){
    if(mx>=butt[i].x&&my>=butt[i].y&&mx<=butt[i].x+butt[i].w&&my<=butt[i].y+butt[i].h){
      if(butt[i].status==0) return butt[i].retval;
      else break;
    }
  }
 
  return -1;
}
 
void basic_set_button(BITMAP *bmp,int c)
{
  int i;
  for(i=0;i<num_butt;i++){
    if(butt[i].retval==c&&butt[i].status!=1){
      butt[i].status=1;
      basic_button(bmp,butt[i].x,butt[i].y,butt[i].w,butt[i].h,butt[i].text,1);
    }
  }
}
 
void basic_unset_buttons(BITMAP *bmp)
{
  int i;
  for(i=0;i<num_butt;i++){
    if(butt[i].status==1){
      butt[i].status=0;
      basic_button(bmp,butt[i].x,butt[i].y,butt[i].w,butt[i].h,butt[i].text,0);
    }
  }
}
 
void basic_draw_buttons(BITMAP *bmp)
{
  int i;
  for(i=0;i<num_butt;i++){
    if(butt[i].status)
      basic_button(bmp,butt[i].x,butt[i].y,butt[i].w,butt[i].h,butt[i].text,1);
    else
      basic_button(bmp,butt[i].x,butt[i].y,butt[i].w,butt[i].h,butt[i].text,0);
  }
}
 
 
void basic_button(BITMAP *bmp,short x,short y,short w,short h,char *text,char mode)
{
  int c;
  if(mode){
    basic_box(bmp,x,y,w,h,5);
    c=makecol(255,255,255);
  }
  else{
    basic_box(bmp,x,y,w,h,6);
    c=makecol(0,0,0);
  }
 
  textout_centre_ex(bmp,font,text,x+w/2,y+(h-text_height(font))/2,c,-1);
}
 
 
void basic_box(BITMAP *bmp,short x,short y,short w,short h,char mode)
{
  int c1,c2,c3,c4;
 
  if(mode>=5){
    rectfill(bmp,x,y,x+w,y+h,makecol(168,168,168));
    mode-=5;
  }
 
  rect(bmp,x-1,y-1,x+w+1,y+h+1,0);
  if(mode==1){
    c1=makecol(240,240,240);
    c2=makecol(126,126,126);
 
    c3=makecol(134,134,134);
    c4=makecol(66,66,66);
  }
  else{
    c2=makecol(240,240,240);
    c1=makecol(126,126,126);
 
    c4=makecol(134,134,134);
    c3=makecol(66,66,66);
  }
 
  hline(bmp,x,y,x+w,c1);
  hline(bmp,x+1,y+1,x+w-1,c3);
  vline(bmp,x,y,y+h,c1);
  vline(bmp,x+1,y+1,y+h-1,c3);
 
  hline(bmp,x,y+h,x+w,c2);
  hline(bmp,x+1,y+h-1,x+w-1,c4);
  vline(bmp,x+w,y,y+h,c2);
  vline(bmp,x+w-1,y+1,y+h-1,c4);
}