#include "alg.h" #include "mylib.h" #include "alg_cfg.h" #include "demohelp.h" #include "lib.h" #ifdef __DOS32__ extern "C" int CorrectPCX(char* NameFile1,char* NameFile2); #endif TLibDialog::TLibDialog( ) : TDialog( TRect( 0, 0, 33, 13 ),""), TWindowInit( TLibDialog::initFrame ) { char str[256],*str1; options |= ofCentered; helpCtx=hcLib; getcwd(PathDir,sizeof(PathDir)); sprintf(str,Text_msg[41],PathDir); title=newStr(str); PCXColect = new TCompCollect(10,10); ScrollBar1 = new TScrollBar( TRect( 15, 2, 16, 12) ); insert(ScrollBar1); MyTListBox1 = new TMyTListBox(TRect( 2, 2, 15, 12), 1, ScrollBar1,NULL); insert(MyTListBox1); TMyLabel *Label = new TMyLabel(TRect(2, 1, 16, 2),"PCX",MyTListBox1,1); insert(Label); WAVColect = new TCompCollect(10,10); ScrollBar2 = new TScrollBar( TRect( 30, 2, 31, 12) ); insert(ScrollBar2); MyTListBox2 = new TMyTListBox(TRect( 17, 2, 30, 12), 1, ScrollBar2,NULL); insert(MyTListBox2); Label = new TMyLabel(TRect(17, 1, 31, 2),"WAV",MyTListBox2,1); insert(Label); myApp->LoadLib(NamePCXLib,NameWAVLib, PCXColect, WAVColect); MyTListBox1->newList(PCXColect); MyTListBox2->newList(WAVColect); MyTListBox1->select(); OldIndChek1=OldIndChek2=0; } TLibDialog::~TLibDialog() { myApp->FreeColect(PCXColect); myApp->FreeColect(WAVColect); } void TLibDialog::handleEvent ( TEvent& event ) { int i,k1,control; char str[256],str1[256],*str2,*str3; TMyTListBox *TempLBox=NULL; TDialog::handleEvent(event); if( event.what == evBroadcast && event.message.command == cmScrollBarChanged) //List ListBox { if(MyTListBox1->state & sfFocused) // ListBox1 { #ifdef __DOS32__ if(getShiftState() & (kbLeftShift|kbRightShift)) { str2=(char *)MyTListBox1->list()->at(OldIndChek1); str3=(char *)MyTListBox1->list()->at(MyTListBox1->focused); sprintf(str,Text_msg[109],PathDir,NamePCXLib); ExtrFile(str,str2,"tmp1.tmp"); ExtrFile(str,str3,"tmp2.tmp"); DeleteFile(str,OldIndChek1); AddFile(str,"tmp2.tmp",OldIndChek1,str3); DeleteFile(str,MyTListBox1->focused); AddFile(str,"tmp1.tmp",MyTListBox1->focused,str2); ::remove("tmp1.tmp"); ::remove("tmp2.tmp"); MyTListBox1->list()->atRemove(OldIndChek1); MyTListBox1->list()->atInsert(OldIndChek1,str3); MyTListBox1->list()->atRemove(MyTListBox1->focused); MyTListBox1->list()->atInsert(MyTListBox1->focused,str2); MyTListBox1->drawView(); } #endif clearEvent(event); OldIndChek1=MyTListBox1->focused; } if(MyTListBox2->state & sfFocused) // ListBox1 { #ifdef __DOS32__ if(getShiftState() & (kbLeftShift|kbRightShift)) { str2=(char *)MyTListBox2->list()->at(OldIndChek2); str3=(char *)MyTListBox2->list()->at(MyTListBox2->focused); sprintf(str,Text_msg[109],PathDir,NameWAVLib); ExtrFile(str,str2,"tmp1.tmp"); ExtrFile(str,str3,"tmp2.tmp"); DeleteFile(str,OldIndChek2); AddFile(str,"tmp2.tmp",OldIndChek2,str3); DeleteFile(str,MyTListBox2->focused); AddFile(str,"tmp1.tmp",MyTListBox2->focused,str2); ::remove("tmp1.tmp"); ::remove("tmp2.tmp"); MyTListBox2->list()->atRemove(OldIndChek2); MyTListBox2->list()->atInsert(OldIndChek2,str3); MyTListBox2->list()->atRemove(MyTListBox2->focused); MyTListBox2->list()->atInsert(MyTListBox2->focused,str2); MyTListBox2->drawView(); } #endif clearEvent(event); OldIndChek2=MyTListBox2->focused; } } if( event.what == evCommand ) { switch( event.message.command ) { case cmAdd: case cmInsert: str2 = new char[13]; str2[0]=0; if(MyTListBox1->state & sfFocused) { if(event.message.command==cmInsert) k1=MyTListBox1->focused; else k1=MyTListBox1->list()->getCount(); TFileDialog *d = new TFileDialog( "*.pcx", "Open a File", "~N~ame", fdOpenButton, 100 ); if( d != 0 && myApp->deskTop->execView( d ) != cmCancel ) { d->getFileName(str1); for(i=strlen(str1);i>0;i--)if(str1[i]==Text_msg[109][2]/* '\\'*/) break; strcpy(str2,str1+i+1); for(i=0;ilist()->getCount();i++) if(!strncmp(str2,(char *)MyTListBox1->list()->at(i),12)) { i= -1;break;} if(i== -1){ DispFixErr(0,Text_msg[15],str2); free(str2); break;} sprintf(str,Text_msg[109],PathDir,NamePCXLib); AddFile(str,str1,k1,str1); MyTListBox1->list()->atInsert(k1,str2); //insert(str2); MyTListBox1->setRange( MyTListBox1->list()->getCount() ); MyTListBox1->drawView(); } else free(str2); destroy( d ); } if(MyTListBox2->state & sfFocused) { if(event.message.command==cmInsert) k1=MyTListBox2->focused; else k1=MyTListBox2->list()->getCount(); TFileDialog *d= new TFileDialog( "*.wav", "Open a File", "~N~ame", fdOpenButton, 100 ); if( d != 0 && myApp->deskTop->execView( d ) != cmCancel ) { d->getFileName(str1); for(i=strlen(str1);i>0;i--)if(str1[i]==Text_msg[109][2]/*'\\'*/) break; strcpy(str2,str1+i+1); for(i=0;ilist()->getCount();i++) if(!strncmp(str2,(char *)MyTListBox2->list()->at(i),12)) { i= -1;break;} if(i== -1){ DispFixErr(0,Text_msg[15],str2); free(str2); break;} sprintf(str,Text_msg[109],PathDir,NameWAVLib); AddFile(str,str1,k1,str1); MyTListBox2->list()->atInsert(k1,str2); //insert(str2); MyTListBox2->setRange( MyTListBox2->list()->getCount() ); MyTListBox2->drawView(); } else free(str2); destroy( d ); } clearEvent(event); break; case cmDelete: if(MyTListBox1->state & sfFocused && MyTListBox1->list()->getCount()) { TempLBox=MyTListBox1; sprintf(str,Text_msg[109],PathDir,NamePCXLib);} if((MyTListBox2->state & sfFocused) && MyTListBox2->list()->getCount()) // ListBox1 { TempLBox=MyTListBox2; sprintf(str,Text_msg[109],PathDir,NameWAVLib);} if(TempLBox) { for(k1=0,i=0;ilist()->getCount();i++) if(TempLBox->list()->GetOptItem(i)) k1++; if(k1) control = messageBox(mfWarning | mfOKCancel,Text_msg[46],k1); else { TempLBox->list()->SetOptItem(TempLBox->focused,1); control=cmOK; } if(control==cmOK) for(i=0;ilist()->getCount();i++) if(TempLBox->list()->GetOptItem(i)) { DeleteFile(str,i);//(char *)TempLBox->list()->at(i)); TempLBox->list()->atFree(i); i--; } TempLBox->setRange( TempLBox->list()->getCount() ); TempLBox->drawView(); } clearEvent(event); break; case cmRename: if((MyTListBox1->state & sfFocused) && MyTListBox1->list()->getCount()) // ListBox1 { TempLBox=MyTListBox1; sprintf(str,Text_msg[109],PathDir,NamePCXLib);} if((MyTListBox2->state & sfFocused) && MyTListBox2->list()->getCount()) // ListBox1 { TempLBox=MyTListBox2; sprintf(str,Text_msg[109],PathDir,NameWAVLib);} if(TempLBox) { for(k1=0,i=0;ilist()->getCount();i++) if(TempLBox->list()->GetOptItem(i)) k1++; if(k1) control = messageBox(mfWarning | mfOKCancel,Text_msg[104],k1); else { TempLBox->list()->SetOptItem(TempLBox->focused,1); control=cmOK; } if(control==cmOK) for(i=0;ilist()->getCount();i++) if(TempLBox->list()->GetOptItem(i)) ExtrFile(str,(char *)TempLBox->list()->at(i),(char *)TempLBox->list()->at(i)); } clearEvent(event); break; case cmRes1: control = messageBox(mfWarning | mfOKCancel,Text_msg[116],k1); if(control==cmOK) { sprintf(str,Text_msg[109],PathDir,NamePCXLib); ExtrFile(str,(char *)MyTListBox1->list()->at(MyTListBox1->focused),"tmp1.tmp"); #ifdef __DOS32__ CorrectPCX("tmp1.tmp","tmp2.tmp"); #endif DeleteFile(str,MyTListBox1->focused); AddFile(str,"tmp2.tmp",MyTListBox1->focused,(char *)MyTListBox1->list()->at(MyTListBox1->focused)); ::remove("tmp1.tmp"); ::remove("tmp2.tmp"); } default: ; } } } void TLibDialog::AddFile(char* NameLib,char* NameFile,int pos,char* NameFile1) { LIBDIR lbpcx; LIBHEADER lbpcxHd; int i=0,fh,fh1,fh2,counter=0,flib_len; char *buf; if((fh=open(NameLib,O_BIN|O_RDWR)) > 0) lseek(fh,sizeof(LIBHEADER),SEEK_SET); else { fh=open(NameLib,O_BIN|O_RDWR|O_CREAT|O_TRUNC,S_IWRITE); strcpy(lbpcxHd.id,"Lib"); strcpy(lbpcxHd.copyright,"Copyright (c) NIP \"DIYA\", 1990-2001"); ::write(fh,&lbpcxHd,sizeof(LIBHEADER)); } flib_len=(long)filelength_(fh)-sizeof(LIBHEADER); while(i < flib_len) { if(counter==pos) break; ::read(fh,&lbpcx,sizeof(LIBDIR)); i+=sizeof(LIBDIR); lseek(fh,lbpcx.filesize,SEEK_CUR); counter++; i+=lbpcx.filesize; } if(flib_len>=i) { if((fh2=open("tmp.tmp",O_BIN|O_CREAT|O_TRUNC|O_RDWR,S_IWRITE|S_IREAD))<0) return; //DispFixErr(1,"TEST %d",flib_len-i); copyfile(fh, fh2, flib_len-i); lseek(fh,i+sizeof(LIBHEADER),SEEK_SET); } fh1=open(NameFile,O_BIN|O_RDONLY); for(i=strlen(NameFile1);i>0;i--)if(NameFile1[i]==Text_msg[109][2]/*'\\'*/){i++; break;} lbpcx.synch=1; strcpy(lbpcx.filename,NameFile1+i); lbpcx.filesize=filelength_(fh1); ::write(fh,&lbpcx,sizeof(LIBDIR)); copyfile(fh1, fh, lbpcx.filesize); if(flib_len>=i) { lseek(fh2,0,SEEK_SET); copyfile(fh2,fh,filelength_(fh2)); lseek(fh,i+sizeof(LIBHEADER),SEEK_SET); lseek(fh2,0,SEEK_SET); } ::close(fh); ::close(fh1); ::close(fh2); ::remove("tmp.tmp"); delete buf; } void TLibDialog::ExtrFile(char* NameLib,char* NameFile,char* NameFile1) { LIBDIR lbpcx; LIBHEADER lbpcxHd; int flib_len,i,ii,fh,fh1,bl; char *buf; buf = new char[500000]; fh1=open(NameFile1,O_BIN|O_RDWR|O_CREAT|O_TRUNC,S_IWRITE); fh=open(NameLib,O_BIN|O_RDONLY); flib_len=(long)filelength_(fh)-sizeof(LIBHEADER); ::read(fh,&lbpcxHd,sizeof(LIBHEADER)); i=0; while(i < flib_len) { ::read(fh,&lbpcx,sizeof(LIBDIR)); i+=sizeof(LIBDIR); if(strncmp(NameFile,lbpcx.filename,12))lseek(fh,lbpcx.filesize,SEEK_CUR); else { bl=lbpcx.filesize/500000; for(ii=0;iifreeAll(); if((fh=open(NamePCX,O_BIN|O_RDONLY))>0) { flib_len=(long)filelength_(fh)-sizeof(LIBHEADER); ::read(fh,&lbpcxHd,sizeof(LIBHEADER)); i=0; while(i < flib_len) { ::read(fh,&lbpcx,sizeof(LIBDIR)); i+=sizeof(LIBDIR); namefile=new char[13]; strncpy(namefile,lbpcx.filename,13); ColectPCX->insert(namefile); lseek(fh,lbpcx.filesize,SEEK_CUR); i+=lbpcx.filesize; } close(fh); } else DispFixErr(1,Text_msg[12],NamePCX); } if(NameWAV && ColectWAV) { ColectWAV->freeAll(); if((fh=open(NameWAV,O_BIN|O_RDONLY))>0) { flib_len=(long)filelength_(fh)-sizeof(LIBHEADER); ::read(fh,&lbpcxHd,sizeof(LIBHEADER)); i=0; while(i < flib_len) { ::read(fh,&lbpcx,sizeof(LIBDIR)); i+=sizeof(LIBDIR); namefile=new char[13]; strncpy(namefile,lbpcx.filename,13); ColectWAV->insert(namefile); lseek(fh,lbpcx.filesize,SEEK_CUR); i+=lbpcx.filesize; } close(fh); } else DispFixErr(1,Text_msg[12],NameWAV); } }