unit uMenuDef;

{==========================================================}
{                                                          }
{  unit uMenuDef                       11/12/2003          }
{                                      Verze 1.2           }
{                                                          }
{  Dokumentovany priklad				   }
{                                                          }
{                                                          }
{                                                          }
{                                                          }
{  (C)2003, SofCon, Stresovicka 49, 160 00 Praha 6         }
{                                                          }
{                                                          }
{==========================================================}

interface

{$I SETS.INC}

uses
  Objects,    { Standardni knihovna             }
  IoDrv,      { Obecne I/O ovladace terminalu   }
  Bitmaps,    { Spravce bitmap                  }
  Fonts,      { Spravce fontu                   }
  Controls,   { Zakladni komponenty             }
  GrCtrls,    { Graficke komponenty             }
  T11Setup;   { Setup pro terminal T11          }

{-------------------------
{ CreateAppPages
{--
{ Procedura pro vytvoreni vsech stranek aplikace
{--
}

procedure CreateAppPages;

{-------------------------
{ AppCustomizeProc
{--
{ Aplikacni konfiguracni procedura komponent
{--
}

procedure AppCustomizeProc( AControl: PControl; AClass: Word );


implementation

uses
  NumToStr,
  valids,
  rstring;



const
  teplota :real = 36.4;


{-----------------------------------------------------------------------}
{ Identifikatory komponent                                              }
{-----------------------------------------------------------------------}

const

  cidPageCtrl          = 1;    { Hlavni komponenta TPageControl         }

  cidMainPage          = 100;  { Hlavni stranka                         }

  cidSecondPage        = 200;  { Dalsi stranka                          }
   cidTeplotaST         = 201;

  cidThirdPage         = 300;
   cidLB                = 301;


  cidEditDialog        = 1000;
   cidBtInsOvr          = 1001;


{-----------------------------------------------------------------------}
{ Odkazy na instance komponent ve stromu aplikace                       }
{-----------------------------------------------------------------------}

var

  Tree :
  record
    PageCtrl : PPageControl;  { Instance hlavni komponenty TPageControl }

    MainPage : record         { Hlavni stranka                          }
    end;

    OtherPage : record        { Dalsi stranka                           }
      st : PStaticText;
    end;

    ThirdPage : record
    end;

    EditDialog : record
      _self : PDialog;
      stText : PStaticText;
      ed : PEdit;
      stUnit : PStaticText;
    end;

  end;



{-------------------------
{ AppCustomizeProc
{--
{ Aplikacni konfiguracni procedura komponent
{--
}

procedure AppCustomizeProc( AControl: PControl; AClass: Word );
begin
end;

{-----------------------------------------------------------------------}
{ Pomocne procedury                                                        }
{-----------------------------------------------------------------------}



{-----------------------------------------------------------------------}
{ Hlavni stranka                                                        }
{-----------------------------------------------------------------------}

{-------------------------
{ MainPageNotify
{--
{ Obsluha oznameni komponent v hlavni strance
{--
}

procedure MainPageNotify( var N: TNotification ); far;
begin
end;

{-------------------------
{ MainPagePaint
{--
{
{--
}

procedure PagePaintFrame( AControl: PControl; ACanvas: PCanvas ); far;
var
  R : TRect;
begin
  { Vykresleni okraje ve vnitrni casti stranky }
  AControl^.GetExtent( R );
  ACanvas^.Pen.Color := clBlack;
  ACanvas^.DrawRect( R.A.X + 1, R.A.Y + 21, R.B.X - 2, R.B.Y - 22 );
end;

{-------------------------
{ CreateMainPage
{--
{ Vytvoreni a inicializace hlavni stranky
{--
}

procedure CreateMainPage;
var
  Page : PPage;
  R    : TRect;
  nav  : PNavigator;
  st   : PStaticText;
  sb   : PScrollBar;
  mo   : PMenuOverlay;
begin
  Page := Tree.PageCtrl^.NewPage( cidMainPage );
  Page^.Customize( ccPage );
  Page^.AfterPaint := PagePaintFrame;
  Page^.AfterNotify := MainPageNotify;

  with Tree.MainPage do
  begin
    { Titulek }
    R.Assign( 1, 1, 319, 20 );
    st := New( PStaticText, Init( R, 'Main page' ) );
    st^.Customize( ccStaticTextTitle );
    Page^.Insert( st );

    { Navigator }
    R.Assign( 1, 220, 319, 239 );
    Nav := New( PNavigator, Init( R, Tree.PageCtrl,
      NewTextPanel( 'Setup', Ord('S'), -4, npfDivider or npfCall, cidTermSetupPage,
      NewTextPanel( 'Other', Ord('O'), -4, npfDivider or npfGoto, cidSecondPage,
      NewTextPanel( 'Third', Ord('T'), -4, npfDivider or npfGoto, cidThirdPage,
      nil)))
    ));
    Nav^.Customize( ccNavigator );
    Page^.Insert( Nav );

    R.Assign( 182, 40, 197, 110 );
    sb := new( PScrollBar, Init( R ));
    sb^.Customize( ccScrollBar );
    Page^.Insert( sb );

    R.Assign( 20, 40, 180, 110 );
    mo := new( PMenuOverlay, Init( r, sb, NewMenu(
      NewMenuItem( 'aaa','bbb', 0, mifNotify, 0,
      NewMenuItem( 'ccc','ddd', 0, mifNotify, 0,
      NewSubMenu( 'sub1', NewMenu(
        NewMenuItem( '...','',0,mifReturn, 0,
        NewMenuItem( 'eee','fff', 0, mifNotify, 0,
        NewMenuItem( 'ggg','hhh', 0, mifNotify, 0,
        nil
      )))),
      NewMenuItem( 'iii','jjj', 0, mifNotify, 0,
      NewMenuItem( 'kkk','lll', 0, mifNotify, 0,
      NewSubMenu( 'sub2', NewMenu(
        NewMenuItem( '...','', 0, mifReturn, 0,
        NewMenuItem( 'mmm','nnn', 0, mifNotify, 0,
        NewMenuItem( 'ooo','ppp', 0, mifNotify, 0,
        NewSubMenu( 'sub2_1', NewMenu(
          NewMenuItem( '...','', 0, mifReturn, 0,
          NewMenuItem( 'qqq','rrr',0, mifNotify, 0,
          nil
        ))),
        NewMenuItem( 'sss','ttt',0,mifNotify, 0,
        nil
      )))))),
      nil
    )))))))));
    mo^.Customize( ccMenuOverlay );
    mo^.SetFlags( mofBorder, 0 );
    Page^.Insert( mo );


  end;

  Tree.PageCtrl^.Insert( Page );
end;


{-----------------------------------------------------------------------}
{ Dalsi stranka                                                         }
{-----------------------------------------------------------------------}

function EditFunc(const sTitle, sText, sUnit: string; var prom : real ):boolean;
var
  r : trect;
  w : word;
  tmp : boolean;
  s : string;
  docas : real;
  pozice : integer;
begin
  tree.editDialog._self^.setTitle(sTitle);

  w := gettextwidth(fidss13,sText);
  r.assign(5,20,5+w,40);
  tree.editDialog.stText^.locate(r);
  tree.editDialog.stText^.setText(sText);

  r.a.x := r.b.x + 5;
  r.b.x := r.a.x + 45; {sirka edit-boxu = 40 pixelu}
  tree.editDialog.ed^.Locate(r);

  r.a.x := r.b.x + 5;
  r.b.x := 315 - 2 - 1 {r.a.x + 30; {sirka jednotek = 25 pixelu};
  tree.editdialog.stUnit^.Locate(r);
  tree.editDialog.stUnit^.setText(sUnit);

{  tree.editDialog.ed^.setValidator(Validator); }
  tree.editDialog.ed^.SetText( RealToStr(prom, 2, tpFixedDes));

  tmp := Application^.execControl( tree.editDialog._self ) = mrOk;
  if tmp then
  begin
    tree.editDialog.ed^.GetText( s );
    val( s, docas, pozice );
    if pozice = 0 then
      prom := docas
    else
      tmp := false;
  end;

  EditFunc := tmp;

  if tmp then
    Tree.OtherPage.st^.SetText( 'Teplota ma byt '+realToStr( teplota, 2, tpFixedDes )+' oC');

end;


procedure EditDialogNotify(var N : tNotification); far;
begin
  case n.control^.id of
    cidBTInsOvr:
      begin
        if n.code = nmClick then
        begin
          tree.editDialog.ed^.SetInsMode(not tree.editDialog.ed^.InsMode);
        end;
      end;
  end;
end;


procedure CreateEditDialog;
var
  r : trect;
  kbd : PKeyPad;
  bt : PButton;
begin
  with tree.editDialog do
  begin
    r.assign( 2, 22, 318,238);
    _self := new(PDialog,init(r,''));
{    _self^.id := cidEditDialogSelf;}
    _self^.customize(ccDialog);
    _self^.afterNotify := EditDialogNotify;

    r.assign(5,20,155,40);
    stText:= new(PStaticText,init(r,''));
    stText^.customize(ccStaticText);
    _self^.insert(stText);

    _self^.GetExtent(R);
    r.grow(-3,-2);

    r.a.y := r.a.y + 43;
    r.b.y := r.b.y - 5;

    r.b.x := r.a.x + 175;

    kbd := new(PKeyPad,init(r));
    kbd^.customize(ccKeyPadNumReal);
    _self^.insert(kbd);

    r.a.x := r.b.x + 10;
    r.b.x := r.a.x + 30;

    kbd := new(PKeyPad,init(r));
    kbd^.customize(ccKeyPadOkCancel);
    _self^.insert(kbd);

    r.assign(80,20,140,40);
    ed := new(PEdit,init(r,20));
    ed^.customize(ccEdit);
    _self^.insert(ed);

    r.assign(145,20,170,40);
    stUnit := new(PStaticText,init(r,''));
    stUnit^.customize(ccStaticText);
    _self^.insert(stUnit);

    r.assign(3,183,3+3*30+2*5,212);
    bt := new (PButton,init(r,'Zpět',btmEndModal,mrCancel));
    bt^.customize(ccButton);
    _self^.insert(bt);

    r.move(110,0);
    r.b.x := r.b.x + 5;
    bt := new (PButton,init(r,'Ins/Ovr',btmSwitch or btmNotify, vkInsert));
    bt^.id := cidBTInsOvr;
    bt^.customize(ccButton);
    _self^.insert(bt);

  end;
end;


{-------------------------
{ OtherPageNotify
{--
{ Obsluha oznameni komponent v dalsi strance
{--
}

procedure OtherPageNotify( var N: TNotification ); far;
begin
  case N.Control^.Id of
    cidSecondPage:
      begin
        if N.Code = nmEnter then
        begin
          Tree.OtherPage.st^.SetText( 'Teplota ma byt '+realToStr( teplota, 2, tpFixedDes )+' oC');
          ClearNotification( N );
        end;
      end;

    cidTeplotaST:
      begin
        if N.Code = nmClick then
        begin
          EditFunc('Editace parametru','Zadejte pozadovanou teplotu:','oC',teplota);
          ClearNotification( N );
        end;
      end;
  end;
end;


{-------------------------
{ CreateOtherPage
{--
{ Vytvoreni a inicializace dalsi stranky
{--
}

procedure CreateOtherPage;
var
  Page : PPage;
  R    : TRect;
  nav  : PNavigator;
  kp   : PKeyPad;
begin
  Page := Tree.PageCtrl^.NewPage( cidSecondPage );
  Page^.Customize( ccPage );
  Page^.AfterNotify := OtherPageNotify;
  Page^.AfterPaint := PagePaintFrame;

  with Tree.OtherPage do
  begin
    { Titulek }
    R.Assign( 1, 1, 319, 20 );
    st := New( PStaticText, Init( R, 'Other page' ));
    st^.Customize( ccStaticTextTitle );
    Page^.Insert( st );

    { Navigator }
    R.Assign( 1, 220, 319, 239 );
    Nav := New( PNavigator, Init( R, Tree.PageCtrl,
      NewTextPanel( 'Main', Ord('M'), -4, npfDivider or npfGoto, cidMainPage,
      NewTextPanel( 'Third', Ord('T'), -4, npfDivider or npfGoto, cidThirdPage,
      nil))
    ));
    Nav^.Customize( ccNavigator );
    Page^.Insert( Nav );


    R.Assign( 10, 30, 170, 50 );
    st := new( PStaticText, init( R, 'Teplota ma byt '+realToStr( teplota, 2, tpFixedDes )+' oC'));
    st^.Id := cidTeplotaST;
    st^.customize( ccStaticText );
    st^.SetFlags( stfBorder, 0 );
    Page^.Insert( st );



  end;

  Tree.PageCtrl^.Insert( Page );
end;


{-----------------------------------------------------------------------}
{ Dalsi stranka                                                         }
{-----------------------------------------------------------------------}

{-------------------------
{ ThirdPageNotify
{--
{ Obsluha oznameni komponent v dalsi strance
{--
}

const
  pole : array [0..24] of string =
  ('Destiny','Trident','Spear','Sword','Lance','Cemetery','Ghoul','Undead',
   'Skeleton','Vampire','Garlic','Axe','Cleric','Monk','Creatrix','Witch',
   'Temple','Gate','Particle','Generation','Apostate','Neophyte','Sanctuary',
   'Tomb','Statue');

procedure ThirdPageNotify( var N: TNotification ); far;
var
  R : TRect;
begin
  case N.Control^.Id of
    cidLB:
      begin
        case N.Code of
          nmGetData:
            begin
              n.text := addr(pole[N.Index]);
              ClearNotification( N );
            end;
          nmSelect:
            begin
              with PListBox( N.Control )^ do
              begin
                GetItemBounds( Selected, R );
                InLineEdit( owner, R, ccEditInLineList, 60,
                  new( PStringValidator, Init( @(pole[selected]), 0, 60 ))
                );
                PaintItems( Selected, 1 );
                ClearNotification( N );
              end;
            end;
        end;
      end;
  end;
end;


{-------------------------
{ CreateThirdPage
{--
{ Vytvoreni a inicializace dalsi stranky
{--
}


procedure CreateThirdPage;
var
  Page : PPage;
  R    : TRect;
  st   : PStaticText;
  nav  : PNavigator;
  sb   : PScrollBar;
  lb   : PListBox;
begin
  Page := Tree.PageCtrl^.NewPage( cidThirdPage );
  Page^.Customize( ccPage );
  Page^.AfterNotify := ThirdPageNotify;
  Page^.AfterPaint := PagePaintFrame;

  { Titulek }
  R.Assign( 1, 1, 319, 20 );
  st := New( PStaticText, Init( R, 'Third page' ));
  st^.Customize( ccStaticTextTitle );
  Page^.Insert( st );

  { Navigator }
  R.Assign( 1, 220, 319, 239 );
  Nav := New( PNavigator, Init( R, Tree.PageCtrl,
    NewTextPanel( 'Main', Ord('M'), -4, npfDivider or npfGoto, cidMainPage,
    NewTextPanel( 'Second', Ord('S'), -4, npfDivider or npfGoto, cidSecondPage,
    nil)
  )));
  Nav^.Customize( ccNavigator );
  Page^.Insert( Nav );

  R.Assign( 20, 92, 300, 107 );
  sb := new( PScrollBar, Init( R ));
  sb^.Customize( ccScrollBar );
  page^.Insert( sb );

  R.Assign( 20, 40, 300, 90 );
  lb := new( PListBox, Init( r, sb ));
  lb^.Id := cidLB;
  lb^.customize( ccListBox );
  lb^.SetColCount( 3 );
  lb^.SetItemCount( 25 );
  lb^.SetFlags( lbfBorder or lbfHorzDiv or lbfVertDiv or lbfUseEnter, 0 );
  page^.Insert( lb );

  Tree.PageCtrl^.Insert( Page );
end;






{-----------------------------------------------------------------------}
{ Pomocne rutiny                                                        }
{-----------------------------------------------------------------------}


{-------------------------
{ CreateAppPages
{--
{ Vytvoreni vsech stranek aplikace
{--
}

procedure CreateAppPages;
var
  R : TRect;
begin
  { Inicializace hlavni komponenty TPageControl }

  R.Assign( 0, 0, 320, 240 );
  Tree.PageCtrl := New( PPageControl, Init( R ) );
  Tree.PageCtrl^.Customize( ccPageControl );
  Tree.PageCtrl^.Id := cidPageCtrl;

  { Vytvoreni jednotlivych stranek aplikace }
  CreateMainPage;
  CreateOtherPage;
  CreateThirdPage;

  { Vytvoreni dialogoveho okna }
  CreateEditDialog;

  { Vytvoreni stranek setupu terminalu T11, knihovna T11Setup }
  CreateT11SetupPages( Tree.PageCtrl );

  { Nastaveni prvni zobrazene stranky po startu aplikace }
  Tree.PageCtrl^.GotoPage( cidMainPage );

  { Vlozeni komponenty TPageControl do aplikace }
  Application^.Insert( Tree.PageCtrl );
end;


end.