// info  nam="SAMPLE02A"  tit="カレンダー"  dsc="説明用サンプル"  pro=3

BOOL ReportMain(HREP rp, int msg, int pg, UINT pa)
{ // カレンダー
  int i, j, sr, yo, dm, dy, tco, re;
  CELL cel;
  GRID grd;
  HBUF rb;
  static int sr0;
  static COLI ciCAL = { TID_YMN,  0, "sr0", NULL, "開始年月(&M)", };
  static COLS csCAL = { &ciCAL, 1 }; 
  static TBLI tiCAL = { &csCAL, };
  static BGRC mc[12] = {
    0x004488, 0x006666, 0x008844, 0x00cc00, 0x448800, 0x666600,
    0x884400, 0xcc0000, 0x880044, 0x660066, 0x440088, 0x0000cc,
  };
  static char wk[7][4] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };

  if (msg == REPM_SETUP) {

    sr0 = date_serial(year(today()), month(today()), 1);
    rb = CreateRowBufX(&sr0, &tiCAL);
    re = EditRowDlgBox(hWND(rp), rb, "カレンダー");
    DeleteRowBuf(rb);
    if (!re) return FALSE;

  } else if (msg == REPM_PAGESETUP) {

    SetNPages(rp, 12);

  } else if (msg == REPM_DRAWPAGE) {

    sr = date_serial(year(sr0), month(sr0) + pg - 1, 1);
    SetCell(&cel, 80.0, 40.0, 130.0, 90.0);
    FillCell(rp, &cel, RST_NUL, mc[month(sr) - 1]);
    tco = 0xffffff;
    SetCell(&cel, 80.0, 45.0, 130.0, 55.0);
    SetTextFont(rp, NULL, 9.0, 0, tco);
    CellText(rp, &cel, format("%d", year(sr)), DT_CEN);
    SetCell(&cel, 80.0, 55.0, 130.0, 85.0);
    SetTextFont(rp, NULL, 30.0, 0, tco);
    CellText(rp, &cel, format("%d", month(sr)), DT_CEN);
    SetGrid0(&grd, 21.0, 100.0, 7, 6, 24.0, 20.0);
    yo = youbi(sr);
    dm = days_of_month(year(sr), month(sr));
    dy = 0;
    for (i = 0; i < grd.nr; i++) {
      for (j = 0; j < grd.nc; j++) {
        tco = (j == 0)? 0x0000ff: (j == 6)? 0xff0000: 0;
        if (i == 0) {
          SetTextFont(rp, NULL, 8.0, 0, tco);
          GridCellText(rp, &grd, i, j, RST_R_M, wk[j], DT_CEN);
        } else {
          SetTextFont(rp, NULL, 12.0, 0, tco);
          if (i == 1 && j == yo) dy = 1;
          if (dy > 0 && dy <= dm) {
            if (i == grd.nr - 1 && dy + 7 <= dm) {
              SetTextFont(rp, NULL, 10.0, 0, tco);
              GridCellText(rp, &grd, i, j, RST_R_M, "／", DT_CEN);
              MoveCell(GridCell(&cel, &grd, i, j), 1.0, -5.0);
              CellText(rp, &cel, format("%d", dy), DT_LFT);
              MoveCell(GridCell(&cel, &grd, i, j), -1.0, 5.0);
              CellText(rp, &cel, format("%d", dy + 7), DT_RGT);
            } else {
              GridCellText(rp, &grd, i, j, RST_R_M, format("%d", dy), DT_CEN);
            }
            dy++;
          } else {
            GridCellText(rp, &grd, i, j, RST_R_M, "*", DT_CEN);
          }
        }
      }
    }

  }
  return TRUE;
}
