kipp_action Subroutine

public subroutine kipp_action(ch, x, y, done)

Apply one cursor key ch at world position (x, y).

Shared by interactive loop and scripted REPL key command (KIPP_SCRIPT). Each view change except u pushes old view onto view history.

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(in) :: ch

Key or mouse event character, as giza reports it.

real(kind=real64), intent(in) :: x

Cursor x, in world coords.

real(kind=real64), intent(in) :: y

Cursor y, in world coords.

logical, intent(out) :: done

Set when ch asks to leave cursor mode.


Source Code

   subroutine kipp_action(ch, x, y, done)
      !! Apply one cursor key `ch` at world position (`x`, `y`).
      !!
      !! Shared by interactive loop and scripted REPL `key` command (`KIPP_SCRIPT`). Each view
      !! change except `u` pushes old view onto view history.
      ! Arguments
      character(len=1), intent(in) :: ch
         !! Key or mouse event character, as giza reports it.
      real(real64), intent(in) :: x
         !! Cursor x, in world coords.
      real(real64), intent(in) :: y
         !! Cursor y, in world coords.
      logical, intent(out) :: done
         !! Set when `ch` asks to leave cursor mode.
      real(real64) :: xa, ya, x2, y2
      character(len=1) :: ch2
      character(len=80) :: snap
      character(len=512) :: cmd
      integer :: ierr, exitstat, cmdstat, tmpunit
      type(view_t) :: old, new

      done = .false.
      xa = x; ya = y
      if (.not. allocated(views)) allocate (views(0))
      old = view_now()
      select case (ch)
      case ('q', K_ESC, K_CTRLC)               ! back to the REPL
         done = .true.
      case (K_LCLICK)                          ! drag-rectangle zoom
         x2 = xa; y2 = ya; ch2 = ' '
         ierr = giza_band(giza_band_rectangle, 0, xa, ya, x2, y2, ch2)
         if (ierr == 0 .and. ch2 /= K_ESC .and. ch2 /= K_CTRLC .and. ch2 /= 'q') then
            call set_xrange_w(xa, x2)
            call set_yrange_w(ya, y2)
            call kipp_render()
         end if
      case ('x')                               ! select an x range
         x2 = xa; y2 = ya; ch2 = ' '
         ierr = giza_band(giza_band_vertlines, 0, xa, ya, x2, y2, ch2)
         if (ierr == 0 .and. ch2 /= K_ESC .and. ch2 /= K_CTRLC .and. ch2 /= 'q') then
            call set_xrange_w(xa, x2)
            call kipp_render()
         end if
      case ('y')                               ! select a y range
         x2 = xa; y2 = ya; ch2 = ' '
         ierr = giza_band(giza_band_horzlines, 0, xa, ya, x2, y2, ch2)
         if (ierr == 0 .and. ch2 /= K_ESC .and. ch2 /= K_CTRLC .and. ch2 /= 'q') then
            call set_yrange_w(ya, y2)
            call kipp_render()
         end if
      case (K_SCRUP, 'z', '+')                 ! zoom in about cursor
         call zoom_at(xa, ya, 0.5d0); call kipp_render()
      case (K_SCRDN, K_RCLICK, 'Z', '-')       ! zoom out about cursor
         call zoom_at(xa, ya, 2.0d0); call kipp_render()
      case (K_SCRLT, 'h')
         call pan_frac(-0.25d0, 0.d0); call kipp_render()
      case (K_SCRRT, 'l')
         call pan_frac(0.25d0, 0.d0); call kipp_render()
      case ('j')
         call pan_frac(0.d0, -0.25d0); call kipp_render()
      case ('k')
         call pan_frac(0.d0, 0.25d0); call kipp_render()
      case (K_MCLICK, 'c')                     ! centre view on cursor
         call center_at(xa, ya); call kipp_render()
      case ('r', 'a', '0')                     ! reset to full view
         st%xauto = .true.; st%yauto = .true.
         call kipp_autoscale(); call kipp_render()
      case ('s')                               ! numbered png snapshot
         nsnap = nsnap + 1
         write (snap, '(a,a,i4.4)') trim(st%prefix), '_', nsnap
         call kipp_save(trim(snap))
      case ('p')                               ! copy png to clipboard
         write (snap, '(a,i0,a)') '/tmp/kippy-', c_getpid(), '-clip'
         call kipp_save(trim(snap))
         ! the forked clipboard owner must not hold our stdout/stderr: if
         ! those are a pipe it dies when the pipe closes and the copy is lost.
         ! macos pbcopy only takes text, so the png goes to NSPasteboard via jxa
         cmd = "{ wl-copy --type image/png < "//trim(snap)//".png || "// &
               'osascript -l JavaScript -e ''ObjC.import("AppKit"); '// &
               'var pb = $.NSPasteboard.generalPasteboard; pb.clearContents; '// &
               'if (!pb.setDataForType($.NSData.dataWithContentsOfFile("'//trim(snap)//'.png"), '// &
               '$.NSPasteboardTypePNG)) throw "copy failed"'' || '// &
               "xclip -selection clipboard -t image/png -i "//trim(snap)//".png; } "// &
               ">/dev/null 2>&1"
         exitstat = 1
         cmdstat = 1
         call execute_command_line(trim(cmd), wait=.true., exitstat=exitstat, cmdstat=cmdstat)
         if (cmdstat == 0 .and. exitstat == 0) then
            print '(a)', '[kipp] copied png to clipboard'
         else
            print '(a)', '[kipp] could not copy png: wl-copy, osascript and xclip missing or failed'
         end if
         open (newunit=tmpunit, file=trim(snap)//'.png', status='old', iostat=ierr)
         if (ierr == 0) close (tmpunit, status='delete')
      case ('S')                               ! numbered pdf snapshot
         nsnap = nsnap + 1
         write (snap, '(a,a,i4.4)') trim(st%prefix), '_', nsnap
         call kipp_save(trim(snap)//'.pdf')
      case ('g')                               ! toggle the cell/time grid
         st%showgrid = .not. st%showgrid; call kipp_render()
      case ('m')                               ! toggle the model-number strip
         st%showmodels = .not. st%showmodels; call kipp_render()
      case ('v')                               ! print the cell under the cursor
         call print_cell(xa, ya)
      case ('u')                               ! back to the previous view
         if (size(views) == 0) then
            print '(a)', '[kipp] no earlier view'
         else
            call set_view(views(size(views)))
            views = views(:size(views) - 1)
            call kipp_render()
         end if
      case ('?')
         call cursor_help()
      case default
         ! ignore anything else (shift-click, unmapped keys)
      end select
      ! compared after the render, so a cancelled selection or a pan stopped
      ! by clamp_view at the data edge records nothing
      new = view_now()
      if (ch /= 'u' .and. any(new%lim /= old%lim)) views = [views, old]
   end subroutine kipp_action