← 都来码 / Dolaima← 例程索引ada/app/calendar.adb▶ 在浏览器里试玩 原文

ada/app/calendar.adb · 7836 字节 · 下载原文

▶ 在浏览器里试玩

-- 日历 —— 用 **Ada** 写的手机小应用(日期 / 月历)
-- Calendar -- a small phone app written in **Ada** (dates / month view)
--
-- 功能:一屏一个月历;← → 切月,↑ ↓ 跳年;今天那一格高亮;ESC 退出。
--
-- ## 本语言的实测约束(见 Examples/ada/app/stopwatch.adb 文件头)
--   · 库函数 `K := ui_...(…)`;数组 1 基;`Integer'Image` 带一个前导空格;
--   · `and/or/not`,不等 `/=`;`while … loop … end loop;`。
--
-- 跑法 —— 手机:vml run examples/ada/calendar.adb
-- 跑法 —— 桌面:vmlcli Examples/ada/calendar.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure Calendar is
   type Mdays_Arr is array (1 .. 12) of Integer;
   Mdays : Mdays_Arr;
   K : Integer := 0;
   W : Integer := 0;
   H : Integer := 0;
   Cell : Integer := 50;
   CellW : Integer := 0;
   CellH : Integer := 0;
   Avail : Integer := 0;
   Ox : Integer := 10;
   Oy : Integer := 120;
   Hty : Integer := 36;
   Hwk : Integer := 78;
   Hgy : Integer := 120;
   Hbot : Integer := 24;
   Tfs : Integer := 20;
   Wfs : Integer := 12;
   Dfs : Integer := 10;
   Hfs : Integer := 11;
   Year : Integer := 2026;
   Month : Integer := 11;
   Ty : Integer := 2026;
   Tm : Integer := 11;
   Td : Integer := 14;
   Days : Integer := 0;
   Start : Integer := 0;
   Count : Integer := 0;
   Frames : Integer := 0;
   I : Integer := 0;
   Y : Integer := 0;
   M : Integer := 0;
   Col : Integer := 0;
   Row : Integer := 0;
   T : Integer := 0;
   Tid : Integer := 0;
   Leap : Integer := 0;
   Lang : Integer := 0;
begin
   Mdays(1) := 31;
   Mdays(2) := 28;
   Mdays(3) := 31;
   Mdays(4) := 30;
   Mdays(5) := 31;
   Mdays(6) := 30;
   Mdays(7) := 31;
   Mdays(8) := 31;
   Mdays(9) := 30;
   Mdays(10) := 31;
   Mdays(11) := 30;
   Mdays(12) := 31;

   Lang := ui_scr_get_language();
   K := ui_win_open(0, 0, 0);
   K := ui_scr_set_keep_on(1);
   W := ui_scr_get_w();
   H := ui_scr_get_h();
   if W <= 0 then
      W := 360;
   end if;
   if H <= 0 then
      H := 620;
   end if;
   Hty := H / 24;
   if Hty < 10 then
      Hty := 10;
   end if;
   Hwk := H / 10;
   if Hwk < 42 then
      Hwk := 42;
   end if;
   Hgy := Hwk + 18;
   Hbot := H / 16;
   if Hbot < 22 then
      Hbot := 22;
   end if;
   Avail := H - Hgy - Hbot - 4;
   CellW := (W - 12) / 7;
   CellH := Avail / 6;
   Cell := CellW;
   if CellH < Cell then
      Cell := CellH;
   end if;
   if Cell < 6 then
      Cell := 6;
   end if;
   Ox := (W - 7 * Cell) / 2;
   Oy := Hgy + (Avail - 6 * Cell) / 2;
   Tfs := H / 20;
   if Tfs < 12 then
      Tfs := 12;
   end if;
   if Tfs > 24 then
      Tfs := 24;
   end if;
   Wfs := H / 32;
   if Wfs < 8 then
      Wfs := 8;
   end if;
   if Wfs > 14 then
      Wfs := 14;
   end if;
   Dfs := Cell / 2;
   if Dfs < 7 then
      Dfs := 7;
   end if;
   if Dfs > 28 then
      Dfs := 28;
   end if;
   Hfs := H / 48;
   if Hfs < 9 then
      Hfs := 9;
   end if;
   if Hfs > 13 then
      Hfs := 13;
   end if;

   Tid := ui_timer_set(200, 0);

   while ui_win_is_closed() = 0 loop
      Days := Mdays(Month);
      Leap := 0;
      if Year mod 4 = 0 and Year mod 100 /= 0 then
         Leap := 1;
      end if;
      if Year mod 400 = 0 then
         Leap := 1;
      end if;
      if Month = 2 and Leap = 1 then
         Days := 29;
      end if;

      Count := 0;
      Y := 1900;
      while Y < Year loop
         Count := Count + 365;
         if Y mod 4 = 0 and Y mod 100 /= 0 then
            Count := Count + 1;
         end if;
         if Y mod 400 = 0 then
            Count := Count + 1;
         end if;
         Y := Y + 1;
      end loop;
      M := 1;
      while M < Month loop
         if M = 2 then
            if Leap = 1 then
               Count := Count + 29;
            else
               Count := Count + 28;
            end if;
         else
            Count := Count + Mdays(M);
         end if;
         M := M + 1;
      end loop;
      Start := Count mod 7;

      K := ui_2d_clear(-15724520);
      K := ui_2d_text(Ox, Hty, Integer'Image(Year), -1513232, Tfs, 0);
      if Lang = 0 then
         K := ui_2d_text(Ox + Tfs * 2 + 8, Hty, "年", -6643536, Tfs * 3 / 4, 0);
      end if;
      K := ui_2d_text(Ox + Tfs * 5, Hty, Integer'Image(Month), -1513232, Tfs, 0);
      if Lang = 0 then
         K := ui_2d_text(Ox + Tfs * 6 + 8, Hty, "月", -6643536, Tfs * 3 / 4, 0);
      end if;

      I := 0;
      while I < 7 loop
         if Lang = 0 then
            if I = 0 then
               K := ui_2d_text(Ox + I * Cell + Cell / 2, Hwk, "一", -6643536, Wfs, 1);
            elsif I = 1 then
               K := ui_2d_text(Ox + I * Cell + Cell / 2, Hwk, "二", -6643536, Wfs, 1);
            elsif I = 2 then
               K := ui_2d_text(Ox + I * Cell + Cell / 2, Hwk, "三", -6643536, Wfs, 1);
            elsif I = 3 then
               K := ui_2d_text(Ox + I * Cell + Cell / 2, Hwk, "四", -6643536, Wfs, 1);
            elsif I = 4 then
               K := ui_2d_text(Ox + I * Cell + Cell / 2, Hwk, "五", -6643536, Wfs, 1);
            elsif I = 5 then
               K := ui_2d_text(Ox + I * Cell + Cell / 2, Hwk, "六", -6643536, Wfs, 1);
            else
               K := ui_2d_text(Ox + I * Cell + Cell / 2, Hwk, "日", -6643536, Wfs, 1);
            end if;
         end if;
         I := I + 1;
      end loop;

      I := 0;
      while I < Days loop
         Col := (Start + I) mod 7;
         Row := (Start + I) / 7;
         if Year = Ty and Month = Tm and I + 1 = Td then
            K := ui_2d_rect(Ox + Col * Cell + 1, Oy + Row * Cell + 1, Cell - 2, Cell - 2, -11409298, 1, 0, 2);
            K := ui_2d_text(Ox + Col * Cell + Cell / 2, Oy + Row * Cell + (Cell - Dfs) / 2, Integer'Image(I + 1), -15724520, Dfs, 1);
         else
            K := ui_2d_rect(Ox + Col * Cell + 1, Oy + Row * Cell + 1, Cell - 2, Cell - 2, -15066588, 1, 0, 2);
            K := ui_2d_text(Ox + Col * Cell + Cell / 2, Oy + Row * Cell + (Cell - Dfs) / 2, Integer'Image(I + 1), -1513232, Dfs, 1);
         end if;
         I := I + 1;
      end loop;

      K := ui_2d_text(W / 2, H - Hbot + 2, "← → 切月 · ↑ ↓ 跳年 · ESC 退出", -6643536, Hfs, 1);
      K := ui_2d_present();

      T := ui_msg_fetch_wait(0);
      if T = 10 then
         exit;
      end if;

      if T = 1 then
         I := ui_msg_get_a();
         if I = 27 then
            exit;
         end if;
         if I = 37 then
            Month := Month - 1;
            if Month < 1 then
               Month := 12;
               Year := Year - 1;
            end if;
         elsif I = 39 then
            Month := Month + 1;
            if Month > 12 then
               Month := 1;
               Year := Year + 1;
            end if;
         elsif I = 38 then
            Year := Year + 1;
         elsif I = 40 then
            Year := Year - 1;
         end if;
         if Year < 1900 then
            Year := 1900;
         end if;
         if Year > 2100 then
            Year := 2100;
         end if;
      end if;

      if T = 6 or T = 4 then
         if ui_msg_get_a() < W / 2 then
            Month := Month - 1;
            if Month < 1 then
               Month := 12;
               Year := Year - 1;
            end if;
         else
            Month := Month + 1;
            if Month > 12 then
               Month := 1;
               Year := Year + 1;
            end if;
         end if;
         if Year < 1900 then
            Year := 1900;
         end if;
         if Year > 2100 then
            Year := 2100;
         end if;
      end if;

      if T = 9 then
         Frames := Frames + 1;
         if Frames >= 20000 then
            exit;
         end if;
      end if;
   end loop;

   K := ui_timer_kill(Tid);
   K := ui_scr_set_keep_on(0);
   K := ui_win_close();
end Calendar;