{$I VAR.INC}
{$I WINDMNGR.INC}
{$C-}
{$I-}

var Screen_real: Array[1..4000] of byte absolute $B800:$0000;
    temp_screen: Array[1..4000] of byte;
    Windows: Array[1..8] of Record
                  UpperLeftX,UpperLeftY,LowerRightX,LowerRightY,
                  ForeGround,BackGround,FrameType,color: Integer;
                  Title: String[80];
             end;
    saved,yestitle,stored:boolean;
    wintitle:string[80];
    title_color:integer;

Procedure Window_Title2(Name: Maxstr; color,i,j:integer);

var m, k, l: integer;

begin
     l:=3;
     color:=color+(j*16);
     temp_screen[(((windows[i].upperlefty)*160)+(windows[i].upperleftx*2))+l]:=$5b;
     for m:=1 to length(Name) do
             begin
                  k:=(((windows[i].upperlefty)*160)+(windows[i].upperleftx*2))+l+1;
                  temp_Screen[k+1]:=ord(copy(Name,m,1));
                  temp_Screen[k+2]:=Color;
                  l:=l+2
             end;
     temp_Screen[k+3]:=$5d
end;

{ Thats all.. }


Procedure Writexy2(long_string:maxstr; xcoord,ycoord,color: integer);

{ This procedure Draws whatever you want, wherever you want, by changing the
  value of Screen in the variable declaration, it can draw a "Picture" any-
  were in memory. This allows for the Speed of the window making process..}

var str_len, real_pos, scr_pos: integer;

begin
{$I-}
     str_len:=length(long_string); { So I know how much to write }
     Scr_pos:=0;

     { The next 8 lines write the string in every "even" location in memory
       and ever odd location gets the attribute with determines how the
       string is displayed on the screen}
     for real_pos:=1 to str_len do
                 if scr_pos < 4001 then
                       begin
                            scr_pos:=((xcoord*2)-1)+(ycoord*160);
                            temp_screen[scr_pos]:=ord(copy(long_string,real_pos,1));
                            temp_screen[scr_pos+1]:=color;
                            xcoord:=xcoord+1;
                       end
{$I+}
end;

Procedure Clrtemp;
var i:integer;
begin
     for i:=1 to 4000 do temp_screen[i]:=$00
end;

Procedure Store_File;
var outfile: text;
    ch: char;
    i,j: integer;
begin
     Assign(outfile,'DEFS.INC');
     rewrite(outfile);
     Writeln(outfile,'Procedure Select_Window(Win_Num: Integer);');
     Write(outfile,'Const Windows: Array[1..9,1..8] of integer =(');
     for i:=1 to 8 do
              writeln(outfile,'(',Windows[i].UpperleftX,',',
                                  Windows[i].Upperlefty,',',
                                  Windows[i].Lowerrightx,',',
                                  Windows[i].Lowerrighty,',',
                                  Windows[i].Foreground,',',
                                  Windows[i].Background,',',
                                  Windows[i].Frametype,',',
                                  Windows[i].Color,
                              '),');
     Writeln(outfile,'(0,0,0,0,0,0,0,0));');
     writeln(outfile,' ');
     Write(outfile,'  Title:Array[1..9] of string[80] = (');
     for i:=1 to 8 do
              if windows[i].title <> '' then writeln(outfile,'''',windows[i].title,'''',',')
              else writeln(outfile,'''''',',');
     Writeln(outfile,''''');');
     writeln(outfile,' ');
     writeln(outfile,'Begin');
     Writeln(outfile,'     If (windows[win_num,3] > 0) and (Win_num > Stack_top) then');
     Writeln(outfile,'        Begin');
     Writeln(outfile,'            Add_Window(Windows[Win_Num,1],');
     Writeln(Outfile,'                       Windows[Win_Num,2],');
     Writeln(Outfile,'                       Windows[Win_Num,3],');
     Writeln(Outfile,'                       Windows[Win_Num,4],');
     Writeln(Outfile,'                       Windows[Win_Num,5],');
     Writeln(Outfile,'                       Windows[Win_Num,6],');
     Writeln(Outfile,'                       Windows[Win_Num,7]);');
     Writeln(Outfile,'     If Title[win_num] <> '''' then');
     Writeln(Outfile,'            Window_Title(Title[Win_num],Windows[Win_Num,8])');
     Writeln(Outfile,'     end');
     Writeln(Outfile,'     else');
     Writeln(Outfile,'         If windows[Win_num,4] > 0 then');
     Writeln(Outfile,'                begin');
     Writeln(Outfile,'                     Imig[Last_window_num].w1:=wherex;');
     Writeln(Outfile,'                     Imig[Last_window_num].w2:=wherey;');
     Writeln(Outfile,'                     Window(1,1,80,25);');
     Writeln(Outfile,'                     GotoXY(1,1);');
     Writeln(Outfile,'                     Window(Imig[Win_num].x1,Imig[Win_num].y1,');
     Writeln(Outfile,'                            Imig[Win_num].x2,Imig[Win_num].y2);');
     Writeln(Outfile,'                     Textcolor(Imig[win_num].c1+(Imig[win_num].b1*16));');
     Writeln(Outfile,'                     GotoXY(1,1);');
     Writeln(Outfile,'                     GotoXY(Imig[win_num].w1,Imig[win_num].w2);');
     Writeln(Outfile,'                     Last_Window_Num:=Win_Num');
     Writeln(Outfile,'                End');
     Writeln(outfile,'End;');
     Close(outfile);
     Saved:=true;
end;



Procedure Frame2(WindowType, UpperLeftX, UpperLeftY, LowerRightX, LowerRightY, color: Integer);

{ This procedure draws the window frame in another part of memory. }
var i: integer;
begin
     WriteXY2(chr(Fc[WindowType,1]),UpperLeftX, UpperLeftY,color);
     for i:=UpperLeftX+1 to LowerRightX-1 do WriteXY2(chr(Fc[WindowType,2]),i,UpperleftY,color);
     WriteXY2(chr(Fc[WindowType,3]),i+1,UpperleftY,color);
     for i:=UpperLeftY+1 to LowerRightY-1 do
         begin
              WriteXY2(chr(Fc[WindowType,4]),UpperLeftX , i,color);
              WriteXY2(chr(Fc[WindowType,4]),LowerRightX, i,color);
         end;
     WriteXY2(chr(Fc[WindowType,5]),UpperLeftX, LowerRightY, color);
    for i:=UpperLeftX+1 to LowerRightX-1 do WriteXY2(chr(Fc[WindowType,6]),i,LowerrightY,color);
     WriteXY2(chr(Fc[WindowType,7]),i+1,LowerRightY,color);
end  { Frame 2};

Procedure GetChar(VAR Ch: Char; VAR Extended: Boolean);
Type
 Registers = Record
              AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags: Integer;
             End;
Var
 Reg: Registers;
 AL:  Integer;
Begin
Ch:=#0; Extended:=False;
If KeyPressed Then
 Begin
 Reg.Ax:=$0800;                { -Set AH as $8 for Dos Function call      }
 Intr($21,Reg);                { -Calls Interupt $21 for Dos Fucntion call}
 AL:=(Reg.AX AND $00FF);       { -Derive AL from AX                       }
 Ch:=Chr(AL);                  { -Set Ch to character to AL               }
 If Ch=#0 then
  Begin                       { Routine to get extended character scan code }
  Reg.Ax:=$0800;
  Intr($21,Reg);
  Ch:=Chr((Reg.AX AND $00FF));
  Extended:=True;
  End;
 End;
End;

Procedure Beep;
begin
     Sound(1500);
     delay(10);
     Nosound
end;

Procedure color_Frame(win, Foreground, Background: Integer);
var i,j,k,color: Integer;
begin
     if background < 8 then color:=foreground+(Background*16);
     k:=1;
     for j:=(Windows[win].UpperLefty) to Windows[win].LowerRighty do
         for i:=(Windows[win].Upperleftx) to (Windows[win].Lowerrightx) do
             begin
                  k:=(j*160)+(i*2);
                  temp_screen[k]:=Color;
             end;
     end;

Procedure Edit_window;

var i,j,k,ux,uy,lx,ly,f,c1,b1: Integer;
    Ch: Char;
    Extended,saved: Boolean;
    x11,y11,x22,y22,forg,bak:string[3];

begin
     yestitle:=false;
     Add_Window(0,0,80,25,15,0,5);
     Normvideo;
     Window(1,1,80,25);
     repeat
           gotoxy(1,1);
           Clrscr;
           textcolor(1);
           Write(' Edit Window #(1-8): ');
           textcolor(7);
           read(i);
     until (i<9) and (i>0);
     if windows[i].title <> '' then yestitle:=true;
     if windows[i].frametype=0 then
           begin
                Windows[i].Upperleftx:=5;
                Windows[i].Upperlefty:=5;
                Windows[i].Lowerrightx:=10;
                Windows[i].Lowerrighty:=10;
                Windows[i].Foreground:=15;
                Windows[i].Frametype:=1;
                Windows[i].Background:=7;
                Windows[i].Foreground:=1;
           end;
     repeat
           Clrtemp;
           Str(windows[i].upperleftx,x11);
           Str(windows[i].upperlefty,y11);
           Str(windows[i].lowerrightx,x22);
           str(windows[i].lowerrighty,y22);
           Str(windows[i].foreground,forg);
           Str(windows[i].background,bak);
           if length(x11) < 2 then x11:='0'+x11;
           if length(y11) < 2 then y11:='0'+y11;
           if length(x22) < 2 then x22:='0'+x22;
           if length(y22) < 2 then y22:='0'+y22;
           if (length(forg)<2) then forg:='0'+forg;
           if (length(forg)<3) then forg:='0'+forg;
           Writexy2('  STATUS   X1:'+X11+' Y1:'+Y11+'   X2:'+X22+' Y2:'+Y22+'     FOREGROUND:'+FORG+'  BACKGROUND:'+BAK,0,0,7
                   );
           Writexy2(' 1=Expand-X  2=Expand-Y 3=Frame Type 4=Foreground 5=background 6=Title',1,1,14);
           Writexy2(' 10=Exit to Main Menu, Use cursor keys to move window',1,2,14);
           Frame2(Windows[i].frametype,Windows[i].Upperleftx,
                  Windows[i].Upperlefty,Windows[i].LowerRightx,
                  Windows[i].Lowerrighty,Windows[i].foreground);
           Color_frame(i,Windows[i].foreground,windows[i].background);
           if yestitle then Window_title2(windows[i].title,windows[i].color,i,windows[i].background);
           move(temp_screen,screen_real,4000);
           beep;
           repeat
                 repeat until keypressed;
                 Getchar(ch, extended);
           Until ord(ch) in [59,60,61,62,63,64,72,77,67,80,75,68];
           saved:=false;
           if extended then case ord(ch) of
                          64:begin
                                  yestitle:=true;
                                  gotoxy(1,1);
                                  write('                                                                              ');
                                  textcolor(1);
                                  gotoxy(1,1);
                                  write('Enter Window title: ');
                                  textcolor(7);
                                  readln(wintitle);
                                  gotoxy(1,1);
                                  write('                                                                 ');
                                  gotoxy(1,1);
                                  textcolor(1);
                                  write('Enter color of title: ');
                                  textcolor(7);
                                  readln(title_color);
                                  windows[i].color:=Title_color;
                                  windows[i].title:=wintitle;
                             end;
                          59:windows[i].LowerRightX:=Windows[i].LowerRightX+1;
                          60:Windows[i].LowerRightY:=Windows[i].LowerRightY+1;
                          61:begin
                                  Windows[i].Frametype:=Windows[i].Frametype+1;
                                  if Windows[i].Frametype > 4
                                         then Windows[i].Frametype:=1
                             end;
                          62:begin
                                  clrtemp;
                                  gotoxy(1,1);
                                  write('                                                                              ');
                                  gotoxy(1,1);
                                  textcolor(1);
                                  Write(' Enter new Foreground Color (0-255): ');
                                  textcolor(7);
                                  repeat
                                        readln(Windows[i].Foreground)
                                  until Windows[i].Foreground < 256;
                             end;
                          77:begin
                                if Windows[i].LowerRightX<80 then begin
                                  Windows[i].Upperleftx:=Windows[i].Upperleftx+1;
                                  Windows[i].Lowerrightx:=Windows[i].lowerrightx+1;
                                end
                                else beep
                             end;
                          72:Begin
                                if windows[i].upperlefty>0 then begin
                                  Windows[i].LowerRightY:=Windows[i].Lowerrighty-1;
                                  Windows[i].Upperlefty:=Windows[i].Upperlefty-1;
                                end
                                else beep
                             end;
                          63:begin
                              repeat
                                  move(screen_real,temp_screen,4000);
                                  gotoxy(1,1);
                                  write('                                                                              ');
                                  gotoxy(1,1);
                                  textcolor(1);
                                  Write('Enter New Background Color (0-8): ');
                                  textcolor(7);
                                  readln(Windows[i].background);
                              until windows[i].background < 8;
                              end;
                         80:Begin
                                if windows[i].lowerrighty<24 then begin
                                  Windows[i].LowerRightY:=Windows[i].Lowerrighty+1;
                                  Windows[i].Upperlefty:=Windows[i].Upperlefty+1;
                                end
                                else beep
                             end;
                           75:begin
                                if windows[i].Upperleftx>1 then begin
                                  Windows[i].Upperleftx:=Windows[i].Upperleftx-1;
                                  Windows[i].Lowerrightx:=Windows[i].lowerrightx-1;
                                end
                                else beep
                              end;
                   end;
     until ord(ch)=68;
     stored:=false;
     Remove(1);
end;

Procedure Show_all;
var ii: Integer;
begin
     Add_Window(1,1,80,25,15,0,0);
     NormVideo;
     Window(1,1,80,25);
     repeat
           GotoXY(1,1);
           GotoXY(1,1);
           Textcolor(1);
           Writeln('Display Window');
           Write('Number <0=Main Menu>: ');
           textcolor(7);
           readln(ii);
           if ii<9 then
           if (ii>0) and (windows[ii].Upperleftx > 0) then
               begin
                   with windows[ii] do
                        begin
                             Add_Window(Upperleftx,Upperlefty,LowerRightX,LowerRightY,FOreground,Background,frametype);
                             Window_title(title,color)
                        end
               end
           else
               If ii>0 then
                   begin
                        gotoxy(1,1);
                        Write('                                                                ');
                        gotoxy(1,1);
                        textcolor(1);
                        Write('Window not defined!');
                        delay(3000)
                   end;
           Gotoxy(1,1);
           Write('                                                                ');
    Normvideo;
    window(1,1,80,25);
    Until ii=0;
    Remove(Stack_top-1);
end;



var ch: Char;
    extended: Boolean;
    i: integer;

begin
     Stored:=true;
     Scrn_Off;
     Set_page($01);
     Initialize;
     for i:=1 to 4000 do screen_real[i]:=00;
     ClrScr;
     saved:=true;
     for i:=1 to 8 do
         begin
              windows[i].Upperleftx:=0;
              windows[i].upperlefty:=0;
              windows[i].lowerrightx:=0;
              windows[i].lowerrighty:=0;
              windows[i].foreground:=0;
              windows[i].background:=0;
              windows[i].frametype:=0;
              windows[i].color:=0
         end;
     gotoxy(17,3);
     Textcolor(15);
     Writeln('THE WINDOW MANAGER/EDITOR SYSTEM WINDOW EDITOR');
     gotoxy(17,5);
     Textcolor(7);
     Writeln('              (C) Copyright 1985');
     gotoxy(17,6);
     Writeln('                      by');
     gotoxy(17,7);
     Writeln('                 Jim Everingham');
     Add_window(5,10,75,20,0,0,2);
     gotoxy(10,2);
     Writeln(' Options:');
     writeln('                   <1> Edit/Configure window file');
     writeln('                   <2> Save Definitions to disk file');
     writeln('                   <3> Clear Defined Window');
     writeln('                   <4> Display defined windows simultaneously');
     writeln('                   <5> Exit to sytem');
     Color_Window(0,4);
     Scrn_on;
     Set_page($00);
     For I:=1 to 8 do
               begin
                          windows[i].Upperleftx:=0;
                          windows[i].upperlefty:=0;
                          windows[i].lowerrightx:=0;
                          windows[i].lowerrighty:=0;
                          windows[i].foreground:=0;
                          windows[i].background:=0;
                          windows[i].frametype:=0;
                          Windows[i].Title:='';
                end;
     Window_Title(' Main Menu ',128+(4*16));
     beep;
     repeat
           repeat
                 repeat until keypressed;
                 read(kbd,ch);
                 beep;
           until ch in ['1'..'5'];
           case ch of
                '1': Edit_window;
                '2': Store_file;
                '4': Show_All;
                '5': begin
                          normvideo;
                          Add_Window(1,1,80,25,1,0,5);
                          clrscr;
                          if not stored then
                                 begin
                                      Write('File not saved.  Are you sure you want to abort?');
                                      read(kbd,ch);
                                      if upcase(ch)='Y' then halt
                                      else remove(1);
                                 end
                         else halt
                     end;
                '3': begin
                          Add_window(1,1,80,25,15,0,5);
                          clrscr;
                          textcolor(1);
                          Write('Clear Window number: ');
                          textcolor(7);
                          repeat
                                readln(i);
                          until (i<8) and (i>0);
                          windows[i].Upperleftx:=0;
                          windows[i].upperlefty:=0;
                          windows[i].lowerrightx:=0;
                          windows[i].lowerrighty:=0;
                          windows[i].foreground:=0;
                          windows[i].background:=0;
                          windows[i].frametype:=0;
                          Windows[i].Title:='';
                          Remove(1);
                    end;
           end;
    until true=false;
end.TitleF>',<EColorF>);

         Where:
 