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;

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

const

  cidPageCtrl          = 1;    { Hlavni komponenta TPageControl         }

  cidMainPage          = 100;  { Hlavni stranka                         }
   cidMPlv             = 101;

  cidSecondPage         = 200;  { Dalsi stranka                          }

  cidThirdPage          = 300;


{-----------------------------------------------------------------------}
{ 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                           }
      lastText : PStaticText;
      mpo  : PMenuPopup;
    end;

    ThirdPage : record
    end;

  end;


type
  TString30 = string[30];
  TItem = record
            name : TString30;
            wish : TString30;
          end;

const
  wishes :array [1..10] of TItem =
   (
    (name: 'Eve'; wish: 'Fast car'),
    (name: 'Marcela + Michelle'; wish: 'More gifted students'),
    (name: 'Cathy'; wish: 'Big patio for her dogs'),
    (name: 'Vanda'; wish: 'Order in the store'),
    (name: 'Maria'; wish: 'Who knows? Help me!'),
    (name: 'Adam'; wish: 'Health for his child'),
    (name: 'Thomash'; wish: 'Who knows? Help me!'),
    (name: 'Carl'; wish: 'Prosperity of his firm'),
    (name: 'George'; wish: 'Growing his plants'),
    (name: 'Dan'; wish: 'Ability to fullfill ^^^^')
   );




{-------------------------
{ 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;
const
  S :string = '';
var
  R : TRect;
  ed : PEdit;
begin
  case N.Control^.Id of
    cidMPlv:
      begin
        case N.Code of

          nmGetData:
            begin
              case N.Col of
                0: str( N.Row+1, S );
                1: S := wishes[N.Row+1].Name;
                2: S := wishes[N.Row+1].Wish;
              end;
              N.Text := @S;
              ClearNotification( N );
            end;

          nmSelect:
            with PListView( N.Control )^ do
            begin
              GetItemBounds( selected, selcolumn, R );
              ed := new( PEdit, Init( R, 30 ));
              ed^.Customize( ccEdit );

              if selcolumn = 1 then
                ed^.SetText( wishes[selected+1].name )
              else
                ed^.SetText( wishes[selected+1].wish );

              if ( N.Control^.Owner^.ExecControl( ed ) = mrOK ) then
              begin
                if selcolumn = 1 then
                  ed^.GetText( wishes[selected+1].name )
                else
                  ed^.GetText( wishes[selected+1].wish );
              end;

              dispose( ed, done );
              PaintItems( selected, 1 );
              ClearNotification( N );
            end;

        end;
      end;
  end;
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;
  st   : PStaticText;
  nav  : PNavigator;
  sb   : PScrollBar;
  lv   : PListView;
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( 292, 25, 307, 186 );
    sb := new( PScrollBar, init( R ));
    sb^.Customize( ccScrollBar );
    Page^.Insert( sb );

    R.Assign( 10, 25, 290, 186 );
    lv := new( PListView, Init( r, sb,
      NewListColumn('Id.',lcfCenter, 20,
      NewListColumn('Name',lcfLeft or lcfSelectable, 100,
      NewListColumn('Wish',lcfLeft or lcfSelectable, 150,
      nil)))
    ));
    lv^.Id := cidMPlv;
    lv^.Customize( ccListView );
    lv^.SetFlags( lvfBorder or lvfHeader or lvfHorzDiv or lvfVertDiv or lvfUseEnter, 0 );
    lv^.SetItemCount( 10 );

    Page^.Insert( lv );

  end;

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


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

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

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


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

procedure mbaNotify( var N: TNotification ); far;
begin
  if N.Code = nmClick then
  begin
    case N.ItemId of
      1: tree.otherpage.lastText^.SetText('aaa');
      2: tree.otherpage.lastText^.SetText('sub1');
      3: tree.otherpage.lastText^.SetText('sub2');
      4: tree.otherpage.lastText^.SetText('ccc');
      5: tree.otherpage.lastText^.SetText('eee');
      6: tree.otherpage.mpo^.Popup( 120, 85 );
    end;
  end;
end;

procedure CreateOtherPage;
var
  Page : PPage;
  R    : TRect;
  st   : PStaticText;
  nav  : PNavigator;
  mba  : PMenuBar;
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( 5, 25, 315, 45 );
    mba := new( PMenuBar, Init( R, NewMenu(
      NewMenuItem( 'aaa','bbb',1,mifNotify,0,
      NewSubMenu( 'Sub-1', NewMenu(
        NewMenuItem( 'sub1','xx',2,mifNotify,0,
        NewMenuLine(
        NewMenuItem( 'sub2','yy',3,mifNotify,0,
        NewMenuItem( 'EndModal','Alt-X',0,mifEndModal, mrOk,
        nil
      ))))),
      NewMenuItem( 'ccc','ddd',4,mifNotify,0,
      NewMenuItem( 'eee','fff',5,mifNotify,0,
      NewMenuItem( 'EndModal','Alt-X',0, mifEndModal, mrOK,
      NewMenuItem( 'Popup','',6,mifNotify,0,
      nil
      )))))
    ))));
    mba^.Customize( ccMenuBar );
    mba^.SetFlags( mfBorder, 0 );
    mba^.AfterNotify := mbaNotify;
    Page^.Insert( mba );

    R.Assign( 10, 100, 110, 120 );
    lastText := new( PStaticText, Init( R, '' ));
    lastText^.Customize( ccStaticText );
    lastText^.SetFlags( stfBorder, 0 );
    Page^.Insert( lastText );

    R.Assign( 0, 0, 25, 13 );
    mpo := new( PMenuPopUp, Init( R, NewMenu(
      NewMenuItem( 'aaa','bbb',1,mifNotify,0,
      NewSubMenu( 'Sub-1', NewMenu(
        NewMenuItem( 'sub1','xx',2,mifNotify,0,
        NewMenuLine(
        NewMenuItem( 'sub2','yy',3,mifNotify,0,
        NewMenuItem( 'EndModal','Alt-X',0,mifEndModal, mrOk,
        nil
      ))))),
      NewMenuItem( 'ccc','ddd',4,mifNotify,0,
      NewMenuItem( 'eee','fff',5,mifNotify,0,
      NewMenuItem( 'EndModal','Alt-X',0, mifEndModal, mrOK,
      nil))))
    ))));
    mpo^.Customize( ccMenuBox );
    mpo^.AfterNotify := mbaNotify;
    Page^.Insert( mpo );
  end;

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


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

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

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


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

var
  teplota : word;
  tlak : real;
  prutok : word;
  rezistivita : word;
  jmeno : byte;

function MyLookUp( AValue: Longint ): PRString; far;
begin
  MyLookUp := @wishes[AValue+1].name;
end;



procedure CreateThirdPage;
var
  Page : PPage;
  R    : TRect;
  st   : PStaticText;
  nav  : PNavigator;
  le   : PListEdit;
  sb   : PScrollBar;
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( 302, 25, 317, 200 );
  sb := new( PScrollBar, Init( R ));
  sb^.Customize( ccScrollBar );
  page^.Insert( sb );

  R.Assign( 5, 25, 300, 200 );
  le := new( PListEdit, Init( r, sb,
         NewListColumn( 'Parametr', lcfLeft, 100,
         NewListColumn( 'Hodnota', lcfLeft or lcfSelectable, 100,
         NewListColumn( 'Jednotka',lcfLeft, 70,
         nil))
         ),
         NewListEditItem( 'Teplota','oC',lifInLineEdit , new( PWordValidator, Init(@Teplota, 0, 0, 9700 )),
         NewListEditItem( 'Tlak','Pa',lifInLineEdit , new( PRealValidator, Init(@Tlak, 0, 0, 2e8, 3, 1 )),
         NewListEditItem( 'Prutok','l/s', lifInLineEdit, new( PWordValidator, InitFxp(@Prutok, 0, 0, 33000, 5, 11 )),
         NewListEditItem( 'Rezistivita','Ohm*m',lifInLineEdit,new( PWordValidator, InitFxp( @Rezistivita, 0, 0, 65535, 2, 4 )),
         NewListEditItem( 'Jmeno','',lifInLineEnum, new( PByteEnumValidator, Init( @jmeno, 10, MyLookUp )),
         nil)))))
         )
        );
  le^.Customize( ccListEdit );
  le^.SetFlags( lvfHeader or lvfBorder or lvfHorzDiv or lvfVertDiv, 0 );
  Page^.Insert( le );

  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 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.