Menu.pas
Un article de Wikipedia.
// librairie de creation de menus // // methodes // initmenu(var m:menu) // titlemenu(var m:menu;t:string) // addmenuitem(var m:menu;e:string) // selectmenu(m:menu):integer // sizemenu(var m:menu):integer // // definitions a inclure // {$i PalmAPI2.pas} const maxmenu = 10; type plistitem = ^listitem; listitem = record etiquette : string; next : plistitem; end; menu = record title : string; next : plistitem; end; //var m : menu; function sizemenu(var m:menu):integer; var tm:plistitem; count:integer; begin if m.next = nil then sizemenu := 0 else begin tm := m.next; count:=1; while tm^.next <> nil do begin count:=count+1; tm := tm^.next; end; sizemenu := count; end; end; procedure initmenu(var m:menu); begin m.title := ''; m.next := nil; end; procedure titlemenu(var m:menu;t:string); begin m.title := t; end; procedure addmenuitem(var m:menu;e:string); var tm:plistitem; begin if m.next = nil then begin new(m.next); with m.next^ do begin etiquette := e; next := nil; end; end else begin tm:=m.next; while tm^.next <> nil do tm := tm^.next; new(tm^.next); with tm^.next^ do begin etiquette := e; next := nil; end; end; end; function selectmenu(m:menu):integer; var tm:plistitem; page,count,n,maxpages,sel,selecteditem:integer; selection:set of 1..20; navigation:set of 'A'..'z'; s:string; quit:boolean; maxmenuitems:integer; function itemnumber(n,max:integer):integer; begin itemnumber:=((n-1) mod max)+1; end; begin //bug dans la multiplication de constante maxmenuitems:=maxmenu; page:=0; quit:=false; selecteditem:=0; maxpages:=(sizemenu(m)-1) div maxmenuitems; repeat clrscr; count:=1; selection:=[]; navigation:=['q','Q']; writeln; writeln(' ':5,m.title); write(' ':5); for n:=1 to length(m.title) do begin write('-'); end; writeln; writeln; if m.next <> nil then begin tm:=m.next; while tm^.next <> nil do begin if (count > maxmenuitems*page) and (count <= maxmenuitems*(page+1)) then begin writeln(itemnumber(count,maxmenuitems):3,'. ',tm^.etiquette); selection:=selection+[itemnumber(count,maxmenuitems)]; end; tm:=tm^.next; count:=count+1; end; if (count > maxmenuitems*page) and (count <= maxmenuitems*(page+1)) then begin writeln(itemnumber(count,maxmenuitems):3,'. ',tm^.etiquette); selection:=selection+[itemnumber(count,maxmenuitems)]; end; end; writeln; if page > 0 then begin writeln('p':3,'. Precedent'); navigation:=navigation+['p','P']; end; if page < maxpages then begin writeln('s':3,'. Suivant'); navigation:=navigation+['s','S']; end; writeln('q':3,'. Quitter'); writeln; write(' ':5,'Selection : '); read(s); if 'p' in navigation then if (s = 'p') or (s = 'P') then page:=page-1; if 's' in navigation then if (s = 's') or (s = 'S') then page:=page+1; if (s = 'q') or (s = 'Q') then quit:=true; sel:=StrAtoI(s); if sel in selection then begin selecteditem:=sel+maxmenuitems*page; quit:=true; end; until quit = true; selectmenu:=selecteditem; end;
Catégories: Source | Logiciel | Pascal | Palm