[inherit('sys$library:starlet','lib$spawn','smgdefs','smg$repaint_line',
'BB.PEN')]
module write_message_directory;

[global] 
procedure write_message_directory;

{Write the message directory beginning with the message marked with
 dir_start pointer.}

var
  date_string : packed array [1..11] of char;
  message_count_string : string;
  class_string : string;
  message_class : integer;   {1=read,2=new,3=skipped}

begin

  {Write header info for chosen topic.}
  dir_display := true;
  smg$begin_pasteboard_update(pasteboard);
  smg$erase_display(header_display);
  smg$put_chars(header_display,current_topic,1,35,,smg$m_bold);
  smg$put_chars(header_display,'#',3,4,,
                 smg$m_bold+smg$m_underline+smg$m_reverse);
  smg$put_chars(header_display,'Subject',3,7,,
                 smg$m_bold+smg$m_underline+smg$m_reverse);
  smg$put_chars(header_display,'Poster',3,37,,
                 smg$m_bold+smg$m_underline+smg$m_reverse);
  smg$put_chars(header_display,'Date added',3,59,,
                 smg$m_bold+smg$m_underline+smg$m_reverse);
  smg$put_chars(header_display,'Type',3,72,,
                 smg$m_bold+smg$m_underline+smg$m_reverse);

  {Handle display when no messages exist.}
  if (dir_root^.next = nil) then
    begin
      smg$erase_display(main_display);
      smg$put_chars(main_display,' No messages exist.',10,31,,smg$m_reverse);
      smg$set_physical_cursor(pasteboard,23,39);
    end
  else
    begin
      dir_start := dir_curr;

      if (dir_start^.previous <> nil) then
        dir_start := dir_start^.previous;
    end;

  {Write lines for message menu.}
  if (dir_start^.next <> nil) then
    begin
      smg$erase_display(main_display);
      dir_list := dir_start;
      count := 1;
      repeat
        dir_list := dir_list^.next;
	with dir_list^ do
          begin
            writev(message_count_string,abs(number):4);
            $asctim(timbuf:=date_string,timadr:=invert(date));
            case class of
              1 : class_string:='READ   ';
              2 : class_string:='NEW    ';
              3 : class_string:='SKIPPED';
              otherwise class_string := '       ';
	    end;  {case}
            if (number > 0) then
              begin
                smg$put_line(main_display,pad(message_count_string,' ',6)+
                             substr(subject,1,28)+'  '+substr(poster,1,20)+'  '+
                             date_string+'  '+class_string);
                smg$set_physical_cursor(pasteboard,23,79);
              end
            else
              smg$put_line(main_display,pad(message_count_string,' ',6)+
                          '(Deleted)');
          end;  {with}
        count := count + 1;
      until (count > display_height) or (dir_list^.next = nil);
      if (dir_list^.next <> nil) then 
        begin
          smg$put_chars(main_display,' More messages ',18,33,,smg$m_reverse);
          smg$set_physical_cursor(pasteboard,23,79);
        end;
    end;

  smg$end_pasteboard_update(pasteboard);
end;       

end.
