My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

project of editor(like notepad) in c++

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<string.h>
#include<fstream.h>
#include<process.h>

void getkey(int&,int&,int&);        // IT DETECT ANY KEY
void lostfocus_c();                 // IT WILL RUB ONE CHARACTER
void lostfocus_l(int);              // IT WILL RUB THE COMPLETE LINE
void lostfocus_p(int);              // IT WILL RUB ALL TEXT AFTER THE CURSOR
                    // INCLUDING WITH THAT LINE IN WHICH CURSOR IS PRESENT
void cursor_draw(int);              // IT WILL DRAW THE BLACK CURSOR
void cursor_lost();                 // IT WILL RUB THE CURSOR
void box();                         // IT WILL DRAW THE FRAME OF EDITOR
void file();                        // IT WILL SAVE THE FILE
void leftarrow();
void rightarrow();
void uparrow();
void downarrow();
void enter();
void backspace();
void alphabet();
char s1[2],s2[80];                 // S2 STORES THE VALUE OF CURRENT STRING
char str[46][80];                 // STR STORE THE WHOLE EDITOR TEXT
int cur=0,max=0,tp=60,bk=15,text=6,pixx=0,pixy=60,font=2,ln;
int hor=1,temp,statl=0,statt=-1,s,a,p,bx1=10,by1=39,bx2=348,by2=480,box_clr=6;
char ch;


void main()
{    // max will give how many strings have been enterd in the array and
    // cur will give the current position in the array of string
    //s2 will have the string being enterd by the user
    int d,m;
    d=DETECT;
    initgraph(&d,&m,"c:\\tc");
    strcpy(s2,"");
    setbkcolor(YELLOW);
    setcolor(text);
    settextstyle(0,HORIZ_DIR,font);
    box();
    cursor_draw(0);
    while(1)
    {    getkey(s,a,p);
        switch(s)
        {    //esc key
            case 1:        /*int gg=0;
                    for(int i=0;i<=max;i++)
                    {    outtextxy(300,gg,str[i]);
                        gg+=18;
                    }
                    getch();*/
                    return;

            //save
            case 60:    file();
                    break;

            //uparrow
            case 72:        uparrow();
                    break;

            //downarrow
            case 80:    downarrow();
                    break;

            // right arrow key
            case 77:    rightarrow();
                    break;

            //left arrow key
            case 75:    leftarrow();
                    break;

            //enter key
            default:    switch(a)
                    {       //enter
                        case 13:    enter();
                                break;

                        //backspace
                        case  8:    backspace();
                                break;
                        default:    alphabet();
                                break;
                    }
        }
    }
    file();
    closegraph();
}

void file()
{    ofstream f;
    f.open("c:/dhamii.txt",ios::out|ios::in);
    if(f.fail())
    {    outtextxy(10,10,"file cannot open correctly");
        getch();
        return;
    }
    for(int i=0;i<=max;i++)
    {    f<<str[i]<<endl;
    }
    f.close();
}

void getkey(int &sc,int &ac,int &sp)
{    REGS ix,ox;
    ix.h.ah=0x00;
    int86(0x16,&ix,&ox);
    sc=ox.h.ah;
    ac=ox.h.al;
    ix.h.ah=0x02;
    int86(0x16,&ix,&ox);
    sp=ox.h.al;
}

void box()
{    setcolor(bk);

    int butn=4;
       //    setcolor(box_clr);
    rectangle(bx1+5,by1+5,bx2-11,by2-11);
    setfillstyle(1,bk);
    floodfill(bx1+12,by1+12,bk);
    setcolor(box_clr);
    int a[]={bx1,by1,bx2,by1,bx2,by1+4,bx1+4,by1+4,bx1+4,by2,bx1,by2,bx1,by1};
    setfillstyle(3,box_clr);
    /* draw a filled polygon */
    fillpoly(7,a);
    floodfill(bx1,by1,box_clr);
    int b[]={bx2-10,by1+5,bx2,by1+5,bx2,by2,bx1+5,by2,bx1+5,by2-10,bx2-10,by2-10,bx2-10,by1+5};
    setfillstyle(9,box_clr);
    fillpoly(7,b);
    //right top button
    setcolor(butn);
    rectangle(bx2-10,by1+5,bx2,by1+20);
    line(bx2-10,by1+15,bx2,by1+15);
    setfillstyle(1,butn);
    floodfill(bx2-8,by1+17,butn);
    int c[]={bx2-10,by1+5,bx2,by1+5,bx2-5,by1+14,bx2-10,by1+5};
    setfillstyle(1,butn);
    fillpoly(4,c);
    //right bottom button
    rectangle(bx2-10,by2-25,bx2,by2-10);
    line(bx2-10,by2-20,bx2,by2-20);
    setfillstyle(1,butn);
    floodfill(bx2-8,by2-23,butn);
    int d[]={bx2-5,by2-19,bx2,by2-10,bx2-10,by2-10,bx2-5,by2-19};
    setfillstyle(1,butn);
    fillpoly(4,d);
    //lower right button
    rectangle(bx2-25,by2-10,bx2-10,by2);
    line(bx2-20,by2-10,bx2-20,by2);
    setfillstyle(1,butn);
    floodfill(bx2-23,by2-8,butn);
    int e[]={bx2-19,by2-5,bx2-10,by2-10,bx2-10,by2,bx2-19,by2-5};
    setfillstyle(1,butn);
    fillpoly(4,e);
    //lower left button
    rectangle(bx1+5,by2-10,bx1+20,by2);
    line(bx1+15,by2-10,bx1+15,by2);
    setfillstyle(1,butn);
    floodfill(bx1+17,by2-8,butn);
    int f[]={bx1+5,by2-10,bx1+14,by2-5,bx1+5,by2,bx1+5,by2-10};
    setfillstyle(1,butn);
    fillpoly(4,f);
    setcolor(text);
}

// Function to wash the particular character
void lostfocus_c()
{    int len;
    char ch[2];
    // len=strlen(s2);
    if(pixx!=16)
    {    cursor_lost();
        len=pixx/(8*font)-2/font;
        pixx=pixx-8*font;
    }
    else
    {    if(tp!=60)
        {    cursor_lost();
            cur--;
            strcpy(s2,str[cur]);
            tp=tp-10*font;
            pixx=8*font*(strlen(s2))+8*font;
        }
        else
        return;
    }
    cursor_draw(1);
    ch[0]=s2[len-1];
    ch[1]='\0';
    setcolor(bk);
    outtextxy(pixx+1,tp,ch);
    setcolor(text);
}

void lostfocus_l(int x)
{        setcolor(bk);
     outtextxy(17,tp,str[cur]);
     setcolor(text);
}

void lostfocus_p(int x)
{    setcolor(bk);
    temp=tp;
    for(int i=x;i<=max;i++)
    {    outtextxy(17,temp,str[i]);
        temp=temp+10*font;
    }
    setcolor(text);
}

void cursor_draw(int l)
{    int len;
    if(l==0)
    {    len=strlen(s2);
        //if(len==0)
        //{    pixx=16;
        //}
        //else
            pixx=8*font*len+16;
            pixy=cur*font*10+60;
            setcolor(8);
            line(pixx,pixy-2,pixx,pixy+7*font+1);
            setcolor(text);

    }
    else
    {    pixy=cur*font*10+60;
        setcolor(8);
        line(pixx,pixy-2,pixx,pixy+7*font+1);
        setcolor(text);
    }
    /*  while(!kbhit())
    {cursor_lost();
    delay(50);
    line(pixx,pixy,pixx,pixy+8);
    delay(50);
    }*/
}

void cursor_lost()
{    setcolor(bk);
    line(pixx,pixy-2,pixx,pixy+7*font+1);
    setcolor(text);
}

void uparrow()
{    strcpy(str[cur],s2);
    if(cur>0)
    {    cursor_lost();
        cur--;
        strcpy(s2,str[cur]);
        tp=tp-10*font;
        if(strlen(s2)*font*8>=pixx)
            cursor_draw(1);
        else
            cursor_draw(0);
    }
}
void downarrow()
{    strcpy(str[cur],s2);
    if(cur<max)
    {    cursor_lost();
        cur++;
        strcpy(s2,str[cur]);
        tp=tp+10*font;
        if(strlen(s2)*font*8>=pixx)
        cursor_draw(1);
        else
        cursor_draw(0);
    }
}

void leftarrow()
{    ln=strlen(s2);
    if(pixx!=16)
    {    cursor_lost();
        pixx=pixx-8*font;
        cursor_draw(1);
    }
    else
    {    if(cur>0)
        {    cursor_lost();
            cur--;
            strcpy(s2,str[cur]);
            tp=tp-10*font;
            cursor_draw(0);
        }
    }

}

void rightarrow()
{    ln=strlen(s2);
    if(ln*8*font+16!=pixx)
    {    cursor_lost();
        pixx=pixx+8*font;
        cursor_draw(1);
    }
    else
    {    if(cur<max)
        {    cursor_lost();
            cur++;
            strcpy(s2,str[cur]);
            tp=tp+10*font;
            pixx=16;
            pixy=pixy+font*10;
            cursor_draw(1);
        }
    }
}

void enter()
{    int remng_char;
    ln=strlen(str[cur]);
    temp=ln*font*8+16;
    //enter in this loop when there is no character after tha cursor
    //in that line and the rest of the editor
    if(max==cur&&temp==pixx)
    {    cursor_lost();
        strcpy(str[cur],s2);
        strcpy(s2,"");
        if(cur==max)
        {    max++;
        }
        cur++;
        tp+=10*font;
        cursor_draw(0);
    }
    else
    {    char stgr[50];
        strcpy(s2,str[cur]);
        //remng_char is the no of remaning character in the
        //current line after the cursor
        remng_char=(temp-pixx)/8*font;
        cursor_lost();
        lostfocus_p(cur);
        lostfocus_l(cur);
        for(int i=0;i<max-cur;i++)
        {    strcpy(str[max-i+1],str[max-i]);
        }
        max++;
        temp=tp+2*10*font;
        for(i=cur+2;i<=max;i++)
        {    outtextxy(17,temp,str[i]);
            temp=temp+10*font;
        }
        int m=ln-remng_char;
        int ii=0;
        for(i=0;i<remng_char;i++)
        {    stgr[ii]=str[cur][m];
            ii++;
            m++;
        }
        /*  while(s2[i]!='\0')
        {stg[ii]=s2[i];
        ii++;
        i++;
        }*/
        stgr[ii]='\0';
        // strupr(stgr);
        outtextxy(400,100,stgr);
        /*   s2[ln-remng_char]='\0';
        strcpy(str[cur],s2);
        cursor_lost();
        outtextxy(17,tp,s2);
        /*       cur++;
        tp=tp+10*font;
        strcpy(str[cur],stg);
        strcpy(s2,stg);
        outtextxy(17,tp,s2);
        pixx=16;
        cursor_draw(1);   */
    }
}

void backspace()
{    int pos;
    //code for backspace key
    //   lostfocus(tp);
    ln=strlen(s2);
    if(pixx!=16)
    {    pos=pixx/(8*font)-2/font;
        pixx=pixx+8*font*(ln-pos);
        for(int i=0;i<=ln-pos;i++)
        {    lostfocus_c();
        }
        for(i=0;i<ln-pos;i++)
        {    s2[pos+i-1]=s2[pos+i];
        }
        s2[ln-1]='\0';
        strcpy(str[cur],s2);
        outtextxy(17,tp,s2);
    }
    else
    {    int temp1;
        temp=strlen(str[cur]);
        ln=strlen(str[cur-1]);
        if(temp+ln<=40/font&&tp!=60)
        {    lostfocus_p(cur);
            cursor_lost();
            strcat(str[cur-1],str[cur]);
            // lostfocus_p(cur-1);
            for(int i=cur;i<max;i++)
            {    strcpy(str[i],str[i+1]);
            }
            strcpy(str[max],"");
            max--;
            temp1=tp;
            outtextxy(17,tp-10*font,str[cur-1]);
            for(i=cur;i<=max;i++)
            {    outtextxy(17,tp,str[i]);
                temp1=temp1+10*font;
            }
            tp=tp-10*font;
            cur--;
            strcpy(s2,str[cur]);
            pixx=pixx+8*font*ln;
            cursor_draw(1);
        }
        else
        {    if(tp!=60)
            {    int pos,ln1;
                char stg[50];
                ln=strlen(str[cur-1]);
                pos=40/font-ln;
                for(int i=0;i<pos;i++)
                {    stg[i]=str[cur][i];
                }
                stg[i]='\0';
                cursor_lost();
                lostfocus_l(cur);
                strcat(str[cur-1],stg);
                strrev(s2);
                ln1=strlen(s2);
                s2[ln1-pos]='\0';
                strrev(s2);
                outtextxy(17,tp,s2);
                strcpy(str[cur],s2);
                pixx=pixx+8*ln*font;
                //     cursor_draw(1);
                cur--;
                tp=tp-10*font;
                cursor_draw(1);
                strcpy(s2,str[cur]);
                outtextxy(17,tp,s2);
            }
        }
    }
}

void alphabet()
{    // code for any other alphabet
    ln=strlen(s2);
    if(ln<40/font)
    {    //put character when editing is in the last line
        if(cur==max)
        {    cursor_lost();
            s1[0]=a;
            s1[1]='\0';
            strupr(s1);
            strcat(s2,s1);
            outtextxy(17,tp,s2);
            strcpy(str[cur],s2);
            cursor_draw(0);
        }
        //put any character in partial filled line
        else
        {    ln=strlen(s2);
            if(ln!=40/font)
            {    if(pixx!=ln*8*font+16)
                {    int pos;
                    lostfocus_l(cur);
                    pos=pixx/(8*font)-2/font;
                    for(int i=0;i<=ln-pos;i++)
                    {    s2[ln-i+1]=s2[ln-i];
                    }
                    s2[pos]=a;
                    strupr(s2);
                    outtextxy(17,tp,s2);
                    strcpy(str[cur],s2);
                    cursor_lost();
                    pixx=pixx+8*font;
                    cursor_draw(1);
                }
                else
                {    cursor_lost();
                    s1[0]=a;
                    s1[1]='\0';
                    strupr(s1);
                    strcat(s2,s1);
                    outtextxy(17,tp,s2);
                    strcpy(str[cur],s2);
                    cursor_draw(0);
                }
            }
        }
    }
    //put character at the last of any line
    else
    {    if(pixx==336)
        {    lostfocus_p(cur);
            for(int i=0;i<(max-cur);i++)
            {    strcpy(str[max-i+1],str[max-i]);
            }
            max++;
            temp=tp+2*10*font;
            for(i=cur+2;i<=max;i++)
            {    outtextxy(17,temp,str[i]);
                temp=temp+10*font;
            }
            outtextxy(17,tp,str[cur]);
            tp=tp+10*font;
            cur++;
            cursor_lost();
            s1[0]=a;
            s1[1]='\0';
            strupr(s1);
            strcpy(s2,"");
            strcat(s2,s1);
            outtextxy(17,tp,s2);
            cursor_draw(0);
            strcpy(str[cur],s2);
        }
        else
        {    //put any character in completely filled line
            if(statl==0&&statt==-1)
            {    lostfocus_p(cur);
                for(int i=0;i<=(max-cur+1);i++)
                {    strcpy(str[max-i+1],str[max-i]);
                }
                max++;
                temp=tp+2*10*font;
                for(i=cur+2;i<=max;i++)
                {    outtextxy(17,temp,str[i]);
                    temp=temp+10*font;
                }
                int pos;
                ln=strlen(s2);
                lostfocus_l(cur);
                pos=pixx/(8*font)-2/font;
                for(i=0;i<=ln-pos;i++)
                {    s2[ln-i+1]=s2[ln-i];
                }
                s2[pos]=a;
                s1[0]=s2[ln];
                s1[1]='\0';
                s2[ln]='\0';
                strupr(s2);
                strcpy(str[cur],s2);
                outtextxy(17,tp,str[cur]);
                tp=tp+10*font;
                cur++;
                cursor_lost();
                //s1[0]=a;
                //    s1[1]='\0';
                strupr(s1);
                strcpy(s2,"");
                strcat(s2,s1);
                outtextxy(17,tp,s2);
                //    cursor_draw(0);
                strcpy(str[cur],s2);
                cur--;
                tp=tp-10*font;
                strcpy(s2,str[cur]);
                pixx=pixx+8*font;
                cursor_draw(1);
                statt=tp;
                statl=1;
            }
            else
            {    int pos;
                ln=strlen(s2);
                lostfocus_l(cur);
                pos=pixx/(8*font)-2/font;
                for(int i=0;i<=ln-pos;i++)
                {    s2[ln-i+1]=s2[ln-i];
                }
                s2[pos]=a;
                s1[0]=s2[ln];
                s1[1]='\0';
                s2[ln]='\0';
                strupr(s2);
                strcpy(str[cur],s2);
                outtextxy(17,tp,str[cur]);
                tp=tp+10*font;
                cur++;
                cursor_lost();
                //s1[0]=a;
                //    s1[1]='\0';
                strupr(s1);
                strcpy(s2,str[cur]);
                lostfocus_l(cur);
                strcat(s2,s1);
                strrev(s2);
                outtextxy(17,tp,s2);
                //    cursor_draw(0);
                strcpy(str[cur],s2);
                cur--;
                tp=tp-10*font;
                strcpy(s2,str[cur]);
                pixx=pixx+8*font;
                cursor_draw(1);
                statt=tp;
                statl=1;
            }
        }
    }
}

0 comments: