! gtaxisdefault.f90 - 軸操作
! Copyright (C) GFD Dennou Club, 2000.  All rights reserved

subroutine GTAxisOpen(this, base_var, dimno, vertical)
    use gtgraph_types, only: GT_AXIS
    use gtdata_types, only: GT_VARIABLE
    use gtdata_generic, only: Open, get, Inquire, get_attr
    use dc_string
    type(GT_AXIS), intent(out):: this
    type(GT_VARIABLE), intent(in):: base_var
    integer, intent(in):: dimno
    logical, intent(in), optional:: vertical
    real, pointer:: values(:)
continue
    call Open(this%var, base_var, dimno)
    ! 縦軸か横軸かを決定
    this%vertical = .FALSE.
    if (present(vertical)) this%vertical = vertical
    ! 軸の上下限推奨値、ここではとりあえず最大最小とし、
    ! 切りのよい値への変換は DclScalingPoint にゆだねる。
    call get(this%var, values)
    this%upper = maxval(values)
    this%lower = minval(values)
    deallocate(values)
    ! 文字属性を取得
    call get_attr(this%var, 'long_name', this%title)
    if (this%title == '') call Inquire(this%var, name=this%title)
    ! 単位に関してはいずれ GT_VARIABLE が
    ! インターフェイスを持つべきである
    call get_attr(this%var, 'units', this%units)
    if (this%units == '') then
        call get_attr(this%var, 'unit', this%units, &
            default="units missing")
    endif
    ! 二重描画を防止するためのフラグ
    this%drawn = .FALSE.
    ! 二重セーブを防止するためのフラグ
    this%saved = ""
end subroutine

! サブルーチン名 Axes は Axis の複数であることに注意。
! 現在のところ必ず座標軸は 2 つ対で描画されなければならない。

subroutine GTAxesDraw(h_axis, v_axis, parent, set_space)
    use gtgraph_types, only: GT_AXIS, GT_FIGURE
    use gtdata_generic, only: Get
    use dc_string
    use dc_error
    use dcl
    implicit none
    type(GT_AXIS), intent(inout):: h_axis, v_axis
    type(GT_FIGURE), intent(in):: parent
    logical, intent(in), optional:: set_space
    logical:: set_space_mode
    real, pointer:: value(:)
    type(VSTRING):: xtitle, ytitle, xunits, yunits
continue
    ! それはないだろう
    if (.not. associated(parent%h_axis)) return
    if (.not. associated(parent%v_axis)) return
    ! オプション処理
    set_space_mode = .FALSE.
    if (present(set_space)) set_space_mode = set_space
    ! 軸を描くということは空間設定でもある。
    call DclSetWindow(h_axis%lower, h_axis%upper, &
        v_axis%lower, v_axis%upper)
    call DclSetTransFunction()
    if (.not. (h_axis%drawn .or. v_axis%drawn)) then
        xtitle = GTStringQuoteForDcl(h_axis%title)
        ytitle = GTStringQuoteForDcl(v_axis%title)
        xunits = GTStringQuoteForDcl(h_axis%units)
        yunits = GTStringQuoteForDcl(v_axis%units)
        call DclSetTitle( &
            & vchar(xtitle, len(xtitle)), &
            & vchar(ytitle, len(ytitle)), &
            & vchar(xunits, len(xunits)), &
            & vchar(yunits, len(yunits)))
        ! 外向き目盛り
        call DclSetParm('AXIS:INNER', -1)
        call DclDrawScaledAxis()
    else if (.not. v_axis%drawn) then
        ytitle = GTStringQuoteForDcl(v_axis%title)
        yunits = GTStringQuoteForDcl(v_axis%units)
        call DclSetTitle("", vchar(ytitle, len(ytitle)), &
            & "", vchar(yunits, len(yunits)))
        call DclDrawScaledAxis(side="rl")
    else if (.not. h_axis%drawn) then
        xtitle = GTStringQuoteForDcl(h_axis%title)
        xunits = GTStringQuoteForDcl(h_axis%units)
        call DclSetTitle(vchar(xtitle, len(xtitle)), "", &
            & vchar(xunits, len(xunits)), "")
        call DclDrawScaledAxis(side="tb")
    endif
    if (set_space_mode) then
        call get(h_axis%var, value)
        call DclSetXGrid(value)
        deallocate(value)
        call get(v_axis%var, value)
        call DclSetYGrid(value)
        deallocate(value)
    endif
    h_axis%drawn = .TRUE.
    v_axis%drawn = .TRUE.
end subroutine

subroutine GTAxisClose(axis)
    use gtgraph_types, only: GT_AXIS
    use gtdata_generic, only: Close
    type(GT_AXIS), intent(inout):: axis
continue
    ! 判定が面倒なので var を多重 close してしまう。
    call Close(axis%var)
end subroutine
