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

fortran/app/calendar.f90 · 5597 字节 · 下载原文

▶ 在浏览器里试玩

! 日历 —— 用 **Fortran** 写的手机小应用(日期 / 月历)
! Calendar -- a small phone app written in **Fortran** (dates / month view)
!
! 功能:一屏一个月历;← → 切月,↑ ↓ 跳年;今天那一格高亮;ESC 退出。
!
! 本前端没有数字转字符串 ⇒ **日期不画数字**:每格是一个方块,位置本身就是日期
! (第 1 行第 1 格 = 1 号),今天用绿色高亮;表头画 7 个短横线。
!
! ◆ 写法要求(见 Examples/fortran/app/sokoban.f90 文件头)
!   · `k = ui_2d_rect(...)` 函数调用表达式;`!=` 写 `/=`
!
! 跑法 —— 手机:vml run examples/fortran/calendar.f90
! 跑法 —— 桌面:vmlcli Examples/fortran/calendar.f90 --screen 480x640 --frames /tmp/fr

program calendar
  implicit none
  integer :: mdays(12)
  integer :: w
  integer :: h
  integer :: cell
  integer :: ox
  integer :: oy
  integer :: year
  integer :: month
  integer :: ty
  integer :: tm
  integer :: td
  integer :: days
  integer :: start
  integer :: count
  integer :: i
  integer :: k
  integer :: y
  integer :: col
  integer :: row
  integer :: frames
  integer :: t
  integer :: tid
  integer :: lang
  integer :: key
  integer :: leap
  integer :: px
  integer :: py
  integer :: fs

  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()
  if (lang == 0) k = ui_win_open('日历', 0, 0)
  if (lang /= 0) k = ui_win_open('Calendar', 0, 0)

  w = ui_scr_get_w()
  h = ui_scr_get_h()
  if (w <= 0) w = 360
  if (h <= 0) h = 620
  ! 最多 6 行日期 ⇒ 宽度与高度各算一次取小,小屏也能完整放下
  oy = 84
  cell = (w - 20) / 7
  k = (h - oy - 6) / 6
  if (k < cell) cell = k
  if (cell < 10) cell = 10
  ox = (w - 7 * cell) / 2

  year = 2026
  month = 11
  ty = 2026
  tm = 11
  td = 14
  frames = 0

  k = ui_scr_set_keep_on(1)
  tid = ui_timer_set(200, 0)

  do while (ui_win_is_closed() == 0)

    ! 这个月有几天
    days = mdays(month)
    if (month == 2) then
      leap = 0
      if (mod(year, 4) == 0) then
        if (mod(year, 100) /= 0) leap = 1
      end if
      if (mod(year, 400) == 0) leap = 1
      if (leap == 1) days = 29
    end if

    ! 1 号是星期几:从 1900-01-01(星期一)累加天数
    count = 0
    i = 1900
    do while (i < year)
      count = count + 365
      if (mod(i, 4) == 0) then
        if (mod(i, 100) /= 0) count = count + 1
      end if
      if (mod(i, 400) == 0) count = count + 1
      i = i + 1
    end do
    i = 1
    do while (i < month)
      if (i == 2) then
        if (mod(year, 4) == 0) then
          if (mod(year, 100) /= 0) then
            count = count + 29
          else
            count = count + 28
          end if
        else
          count = count + 28
        end if
      else
        count = count + mdays(i)
      end if
      i = i + 1
    end do
    start = mod(count, 7)

    ! ── draw ──
    ! 布局随画布现算(含 resize):网格最多 6 行,宽高各算一次取小
    oy = 84
    cell = (w - 20) / 7
    k = (h - oy - 6) / 6
    if (k < cell) cell = k
    if (cell < 10) cell = 10
    ox = (w - 7 * cell) / 2

    k = ui_2d_clear(-15724520)

    ! 表头:7 条短横线
    i = 0
    do while (i < 7)
      k = ui_2d_rect(ox + i * cell + cell / 4, oy - 20, cell / 2, 4, -6643536, 1, 0, 1)
      i = i + 1
    end do

    ! 日期格(位置即日期;今天高亮)
    i = 0
    do while (i < days)
      col = mod(start + i, 7)
      row = (start + i) / 7
      px = ox + col * cell
      py = oy + row * cell
      if (year == ty .and. month == tm .and. i + 1 == td) then
        k = ui_2d_rect(px + 2, py + 2, cell - 4, cell - 4, -11409298, 1, 0, 2)
      else
        k = ui_2d_rect(px + 2, py + 2, cell - 4, cell - 4, -15066588, 1, 0, 2)
      end if
      i = i + 1
    end do

    fs = 11
    if (w < 360) fs = 10
    if (lang == 0) k = ui_2d_text(8, 44, '← → 切月 · ↑ ↓ 跳年 · ESC 退出', -6643536, fs, 0)
    if (lang /= 0) k = ui_2d_text(8, 44, 'Left/Right month - Up/Down year - ESC quits', -6643536, fs, 0)
    k = ui_2d_present()

    t = ui_msg_fetch_wait(0)
    if (t == 10) exit

    if (t == 1) then
      key = ui_msg_get_a()
      if (key == 27) exit
      if (key == 37) then
        month = month - 1
        if (month < 1) then
          month = 12
          year = year - 1
        end if
      end if
      if (key == 39) then
        month = month + 1
        if (month > 12) then
          month = 1
          year = year + 1
        end if
      end if
      if (key == 38) year = year + 1
      if (key == 40) year = year - 1
      if (year < 1900) year = 1900
      if (year > 2100) year = 2100
    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) year = 1900
      if (year > 2100) year = 2100
    end if

    if (t == 11) then
      w = ui_scr_get_w()
      h = ui_scr_get_h()
      if (w <= 0) w = 360
      if (h <= 0) h = 620
    end if

    if (t == 9) then
      frames = frames + 1
      if (frames >= 20000) exit
    end if
  end do

  k = ui_timer_kill(tid)
  k = ui_scr_set_keep_on(0)
  k = ui_win_close()
end program calendar