File: gui.c | Size: 6,475 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"
 
extern char vyrok[512];
 
void gui_init_right_menu(void)
{
  static char *nazvy[]={
    "Koniec","Info","Export","Ulozit","Nahrat","Vyrok",
    NULL
  };
  static char *nazvy2[]={
    "Uvod","Strom",
    NULL
  };
  int i;
 
  basic_clear_buttons();
 
  for(i=0;nazvy[i];i++){
    basic_add_button(685,570-i*25,105,20,nazvy[i],i);
  }
 
  for(i=0;nazvy2[i];i++){
    basic_add_button(685,10+i*25,105,20,nazvy2[i],i+100);
  }
 
  basic_draw_buttons(actual);
 
  basic_set_button(actual,100);
}
 
/*
int gui_get_scrollbar(BITMAP *bmp,int pos,int max,int w1,int w2,int dx)
{
  int w,xx=0;
 
  l-=22;
  w=((double)w2/(double)w1)*(double)l;
  if(w>l) w=l;
 
  if(abs(l-w)>0){
    xx=(double)(dx*max)/(double)(l-w);
  }
  return pos+xx;
}
 
void gui_scrollbar(BITMAP *bmp,int pos,int max,int w1,int w2,int x,int y,int l,char vert)
{
  int w,xx;
 
  l-=22;
  w=((double)w2/(double)w1)*(double)l;
  if(w>l) w=l;
 
  xx=((double)pos/(double)max)*(double)(l-w);
 
  if(vert==0){
    rectfill(bmp,x+10,y,x+12+l,y+10,makecol(228,228,228));
    basic_box(bmp,x+11+xx,y+1,w,8,6);
 
    basic_box(bmp,x+1,y+1,8,8,6);
    basic_box(bmp,x+l+12,y+1,8,8,6);
  }
  else{
    rectfill(bmp,x,y+10,x+10,y+12+l,makecol(228,228,228));
    basic_box(bmp,x+1,y+11+xx,8,w,6);
 
    basic_box(bmp,x+1,y+1,8,8,6);
    basic_box(bmp,x+1,y+l+12,8,8,6);
  }
 
}
//        gui_scrollbar(bmp2,sx,w-665,w,665,0,590,665,0);
//        gui_scrollbar(bmp2,sy,h-590,h,590,665,0,590,1);
 
*/
 
void gui_start(void)
{
  int c=1,r,sx,sy,w,h,mode=100,mx,my,l;
  char filename[1024],exp_filename[1024]="expC",tmp[1024];
  char mb_second=0;
  FILE *s;
 
  blit(data[vojtas].dat,bmp2,0,0,0,0,675,600);
  basic_box(actual,675,0,125,600,6);
  gui_init_right_menu();
 
  while(1){
    if(c==1){
      basic_unset_buttons(actual);
      basic_set_button(actual,mode);
      if(mode==100) blit(data[vojtas].dat,bmp2,0,0,0,0,675,600);
      if(mode==101) blit(exp_bmp,bmp2,sx,sy,0,0,675,600);
      blit(bmp2,actual,0,0,0,0,675,600);
      c=0;
    }
 
    show_mouse(actual);
    blit(actual,screen,0,0,0,0,800,600);
    show_mouse(0);
 
    if(mouse_b&1){
      r=basic_check_buttons(mouse_x,mouse_y);
      if(r==0) break;
      if(r==1){
        blit(actual,screen,0,0,0,0,800,600);
        alert("Informacie o programe","Na tejto sekcii sa stale pracuje.",NULL,"&OK",0,'o',0);
      }
      if(r==2){ // export
        blit(actual,screen,0,0,0,0,800,600);
        if(mode>100){
          replace_extension(filename,exp_filename,"bmp",512);
          if(file_select_ex("Vyberte BMP subor pre export",filename,"bmp",512,0,0)!=0){
            replace_extension(tmp,filename,"bmp",512);
            packfile_password("");
            save_bmp(tmp,exp_bmp,0);
          }
        }
        else{
          alert("Upozornenie","Najprv vyberte obrazok, ktory chcete exportovat.",NULL,"&OK",0,'o',0);
        }
      }
 
      if(r==3){ // ulozenie
        blit(actual,screen,0,0,0,0,800,600);
        replace_extension(filename,exp_filename,"exp",512);
        if(file_select_ex("Vyberte subor pre ulozenie",filename,"exp",512,0,0)!=0){
          replace_extension(exp_filename,filename,"exp",512);
          packfile_password("");
          s=fopen(exp_filename,"w");
          if(s==0) alert("Chyba","Nemozem zapisovat do suboru!",NULL,"&OK",0,'o',0);
          else{
            fprintf(s,"%s\n",vyrok);
            fclose(s);
          }
        }
      }
 
      if(r==4){ // nacitanie
        blit(actual,screen,0,0,0,0,800,600);
        replace_extension(filename,exp_filename,"exp",512);
        if(file_select_ex("Vyberte subor pre nacitanie",filename,"exp",512,0,0)!=0){
          replace_extension(exp_filename,filename,"exp",512);
          packfile_password("");
          s=fopen(exp_filename,"r");
          if(s==0) alert("Chyba","Nemozem otvorit subor!",NULL,"&OK",0,'o',0);
          else{
            fgets(vyrok,510,s);
            l=strlen(vyrok);
            if(l>0&&vyrok[l-1]=='\n') vyrok[l-1]=0;
            fclose(s);
 
            vdialog_show();
            mode=100;
            c=1;
          }
        }
      }
 
      if(r==5){ // editovanie vyroku
        blit(actual,screen,0,0,0,0,800,600);
        vdialog_show();
        mode=100;
        c=1;
      }
 
      if(r>=100){
        mode=r;
        if(r==101){
          if(expr2bmp()<0){
            alert("Generator stromu","Nepodarilo sa vygenerovat strom.","Jedna sa o chybu v programe!","&OK",0,'o',0);
            mode=100;
          }
          else{
            w=exp_bmp->w-675;
            if(w<0) w=0;
            h=exp_bmp->h-600;
            if(h<0) h=0;
            sx=0;sy=0;
          }
        }
        c=1;
      }
    }
    if((mouse_b&2)&&mouse_x<675&&mode>100){
      if(mb_second==0){
        get_mouse_mickeys(&mx,&my);
        position_mouse(675/2,300);
        set_mouse_sprite(data[mouse_move].dat);
        set_mouse_sprite_focus(12,12);
        mb_second=1;
      }
      get_mouse_mickeys(&mx,&my);
      if(mx!=0||my!=0){
        position_mouse(675/2,300);
        sx+=mx;
        if(sx<0) sx=0;
        if(sx>w) sx=w;
 
        sy+=my;
        if(sy<0) sy=0;
        if(sy>h) sy=h;
 
        c=1;
      }
    }
    else{
      if(mb_second!=0){
        set_mouse_sprite(data[mouse_std].dat);
        set_mouse_sprite_focus(2,2);
        mb_second=0;
      }
    }
 
    if(key[KEY_ESC]) break;
  }
}