# Program: xf
# Description: handle photo widgets
#
# $Header: Photo[2.4] Wed Mar 10 11:57:26 1993 garfield@garfield frozen $

global xfDefaultConf
global photoList

set xfDefaultConf(photo) 4
if {![info exists photoList]} {
  set photoList(xfnone) ""
}

##########
# Procedure: XFAdd.Photo
# Description: add a photo
# Arguments: xfW - the widget
#            xfName - a optional name
#            xfType - add or config
# Returns: none
# Sideeffects: none
##########
proc XFAdd.Photo {xfW xfName xfType} {
  global xfConf
  global xfStatus

  XFEditSetStatus "Inserting Photo..."
  set xfName [XFMiscGetUniqueName $xfName photo]
  if {"$xfStatus(path)" == "."} {
    if {"$xfType" == "add"} {
      if {[catch "photo .$xfName" xfResult]} {
        XFProcError "$xfResult"
        XFEditSetStatus "Inserting Photo...aborted"
        return
      }
    } {
      if {[catch "photo .$xfName" xfResult]} {
        XFProcError "$xfResult"
        XFEditSetStatus "Inserting Photo...aborted"
        return
      }
    }

    XFMiscPositionWidget .$xfName
    XFMiscBindWidgetTree .$xfName
  } {
    if {"$xfType" == "add"} {
      if {[catch "photo $xfStatus(path).$xfName" xfResult]} {
        XFProcError "$xfResult"
        XFEditSetStatus "Inserting Photo...aborted"
        return
      }
    } {
      if {[catch "photo $xfStatus(path).$xfName" xfResult]} {
        XFProcError "$xfResult"
        XFEditSetStatus "Inserting Photo...aborted"
        return
      }
    }

    XFMiscPositionWidget $xfStatus(path).$xfName
    XFMiscBindWidgetTree $xfStatus(path).$xfName
  }

  incr xfStatus(elementCounter)
  XFEditSetPath $xfStatus(path)
  XFEditSetStatus "Inserting Photo...done"
}

##########
# Procedure: XFBind.Photo
# Description: default bindings for photo
# Arguments: xfW - the widget
# Returns: none
# Sideeffects: none
##########
proc XFBind.Photo {xfW} {

  if {"[info commands $xfW]" != ""} {
    bind $xfW <2> {%W scan mark %x %y}
    bind $xfW <B2-Motion> {%W scan dragto %x %y}
  }
}

##########
# Procedure: XFConfig.Photo4
# Description: configure a photo
# Arguments: xfW - the widget
#            xfType - config type (add config)
#            xfClass - the class we configure
#            xfLeader - the leading window
# Returns: none
# Sideeffects: none
##########
proc XFConfig.Photo4 {xfW xfType xfClass {xfLeader ""}} {
  global photoList
  global xfBind
  global xfMisc
  global xfStatus

  set xfMisc(photoDither) 0

  if {"$xfType" == "add"} {
    set xfName button$xfStatus(elementCounter)
  } {
    set xfName [XFMiscPathName $xfW]
  }
  XFEditSetStatus "Calling parameter setting for Photo..."

  # build widget structure
  XFTmpltToplevel .xf${xfClass}Config4 530x470 \
    "Photo parameters:[XFMiscPathTail $xfW]" $xfLeader

  XFElementInit $xfW .xf${xfClass}Config4 $xfType $xfClass XFPhotoSetPhoto4 \
    parameters $xfName 4
#  XFElementColor $xfW .xf${xfClass}Config4 $xfType $xfClass bg \
#    background Background "Background" XFPhotoSetPhoto4
  XFElementColor $xfW .xf${xfClass}Config4 $xfType $xfClass blank \
    blank Blank "Blank" XFPhotoSetPhoto4
#  XFElementScale $xfW .xf${xfClass}Config4 $xfType $xfClass borderwidth \
#    borderWidth BorderWidth "Border width" "pixels" 40 XFPhotoSetPhoto4

  XFTmpltFrame .xf${xfClass}Config4.params1.params2.frame2 0
  
  label .xf${xfClass}Config4.params1.params2.frame2.message1 \
    -anchor e \
    -width $xfStatus(elementWidth) \
    -text "Dither:"

  radiobutton .xf${xfClass}Config4.params1.params2.frame2.no \
    -text "No" \
    -value 0 \
    -variable xfMisc(photoDither) \
    -command "XFPhotoSetPhoto4 $xfW 0 $xfClass"

  radiobutton .xf${xfClass}Config4.params1.params2.frame2.request \
    -text "On request" \
    -value 1 \
    -variable xfMisc(photoDither) \
    -command "XFPhotoSetPhoto4 $xfW 0 $xfClass"

  radiobutton .xf${xfClass}Config4.params1.params2.frame2.always \
    -text "Always" \
    -value 2 \
    -variable xfMisc(photoDither) \
    -command "XFPhotoSetPhoto4 $xfW 0 $xfClass"

  pack append .xf${xfClass}Config4.params1.params2.frame2 \
              .xf${xfClass}Config4.params1.params2.frame2.message1 {left} \
              .xf${xfClass}Config4.params1.params2.frame2.no {left} \
              .xf${xfClass}Config4.params1.params2.frame2.request {left} \
              .xf${xfClass}Config4.params1.params2.frame2.always {left}
  pack append .xf${xfClass}Config4.params1.params2 \
              .xf${xfClass}Config4.params1.params2.frame2 {top fillx}

  XFElementGeometry $xfW .xf${xfClass}Config4 $xfType $xfClass \
    XFPhotoSetPhoto4
  XFElementScaleDouble $xfW .xf${xfClass}Config4 $xfType \
    $xfClass size "Image size" "Width" "Height" 2000 2000
  XFElementScale $xfW .xf${xfClass}Config4 $xfType $xfClass gamma \
    gamma Gamma "Gamma" "Gamma/10" 100 XFPhotoSetPhoto4

  XFTmpltFrame .xf${xfClass}Config4.params1.params2.frame3 0
  
  label .xf${xfClass}Config4.params1.params2.frame3.message1 \
    -anchor e \
    -width $xfStatus(elementWidth) \
    -text "Palette:"

  scale .xf${xfClass}Config4.params1.params2.frame3.bw \
    -from 0 \
    -label "Gray shades" \
    -orient horizontal \
    -sliderlength 15 \
    -to 256 \
    -width 8

  scale .xf${xfClass}Config4.params1.params2.frame3.red \
    -from 0 \
    -label "Red" \
    -orient horizontal \
    -sliderlength 15 \
    -to 256 \
    -width 8

  scale .xf${xfClass}Config4.params1.params2.frame3.green \
    -from 0 \
    -label "Green" \
    -orient horizontal \
    -sliderlength 15 \
    -to 256 \
    -width 8

  scale .xf${xfClass}Config4.params1.params2.frame3.blue \
    -from 0 \
    -label "Blue" \
    -orient horizontal \
    -sliderlength 15 \
    -to 256 \
    -width 8

  pack append .xf${xfClass}Config4.params1.params2.frame3 \
              .xf${xfClass}Config4.params1.params2.frame3.message1 {left} \
              .xf${xfClass}Config4.params1.params2.frame3.bw {left} \
              .xf${xfClass}Config4.params1.params2.frame3.red {left} \
              .xf${xfClass}Config4.params1.params2.frame3.green {left} \
              .xf${xfClass}Config4.params1.params2.frame3.blue {left}
  pack append .xf${xfClass}Config4.params1.params2 \
              .xf${xfClass}Config4.params1.params2.frame3 {top fillx}

#  XFElementRelief $xfW .xf${xfClass}Config4 $xfType $xfClass XFPhotoSetPhoto4
  XFElementText $xfW .xf${xfClass}Config4 $xfType $xfClass xscrollcommand \
    xscrollcommand XScrollCommand "X scroll command" XFPhotoSetPhoto4
  XFElementText $xfW .xf${xfClass}Config4 $xfType $xfClass yscrollcommand \
    yscrollcommand YScrollCommand "Y scroll command" XFPhotoSetPhoto4

  set xfTmpRetVal ""
  if {![info exists photoList]} {
    set photoList(xfnone) ""
  }
  foreach xfCounter [array names photoList] {
    if {"$xfCounter" == "$xfW"} {
      set xfArrayName ""
      append xfArrayName photoList ( $xfCounter )
      if {[catch "set \"$xfArrayName\"" xfTmpRetVal]} {
        set xfTmpRetVal ""
      }
    }
  }
  XFTmpltLabledEntry .xf${xfClass}Config4.params1.params2 picname \
    "Picture name:" $xfTmpRetVal
  XFMiscInitElement .xf${xfClass}Config4 picname
  pack append .xf${xfClass}Config4.params1.params2 \
              .xf${xfClass}Config4.params1.params2.picname {top fillx pady 6} \

  if {[llength [split [lindex [$xfW configure -palette] 4] /]] == 1} {
    .xf${xfClass}Config4.params1.params2.frame3.bw set \
      [lindex [$xfW configure -palette] 4]
  } {
    .xf${xfClass}Config4.params1.params2.frame3.red set \
      [lindex [split [lindex [$xfW configure -palette] 4] /] 0]
    .xf${xfClass}Config4.params1.params2.frame3.green set \
      [lindex [split [lindex [$xfW configure -palette] 4] /] 1]
    .xf${xfClass}Config4.params1.params2.frame3.blue set \
      [lindex [split [lindex [$xfW configure -palette] 4] /] 2]
  }

  if {"$xfType" == "add"} {
    .xf${xfClass}Config4.params1.params2.gamma.gamma set 10
  } {
    .xf${xfClass}Config4.params1.params2.gamma.gamma set \
      [lindex [split [expr [lindex [$xfW configure -gamma] 4]*10] .] 0]

    .xf${xfClass}Config4.leave.cancel configure \
      -command "
        global xfMisc
        if {\[info exists xfMisc($xfClass,saveValue)\]} {
          foreach xfCounter \$xfMisc($xfClass,saveValue) {
            catch \"$xfW config -\[lindex \$xfCounter 0\] \\\"\[lindex \$xfCounter 1\]\\\"\"
          }
        }
        if {\[info exists xfMisc($xfClass,saveWm)\]} {
          if {\"[string tolower $xfClass]\" == \"toplevel\"} {
            foreach xfCounter \$xfMisc($xfClass,saveWm) {
              catch \"wm \[lindex \$xfCounter 0\] $xfW \\\"\[lindex \$xfCounter 1\]\\\"\"
            }
          }
        }
        if {\"\[.xf${xfClass}Config4.params1.params2.picname.picname get\]\" == \"\"} {
          set xfArrayName \"\"
          append xfArrayName photoList ( $xfW )
          catch \"unset \\\"\$xfArrayName\\\"\"
        } {
          set xfArrayName \"\"
          append xfArrayName photoList ( $xfW )
          set \"\$xfArrayName\" \
            \[.xf${xfClass}Config4.params1.params2.picname.picname get\]
          $xfW blank
          readppm $xfW \[set \"\$xfArrayName\"\]
        }
        destroy .xf${xfClass}Config4"
  }

  XFMiscSetResource .xf${xfClass}Config4.params1.params2.gamma.gamma \
    command "XFPhotoSetPhoto4 $xfW 0 $xfClass"
  XFMiscSetResource .xf${xfClass}Config4.params1.params2.frame3.bw \
    command "XFPhotoSetPhoto4 $xfW 0 $xfClass"
  XFMiscSetResource .xf${xfClass}Config4.params1.params2.frame3.red \
    command "XFPhotoSetPhoto4 $xfW 0 $xfClass"
  XFMiscSetResource .xf${xfClass}Config4.params1.params2.frame3.green \
    command "XFPhotoSetPhoto4 $xfW 0 $xfClass"
  XFMiscSetResource .xf${xfClass}Config4.params1.params2.frame3.blue \
    command "XFPhotoSetPhoto4 $xfW 0 $xfClass"
  XFMiscSetResource .xf${xfClass}Config4.params1.params2.size.size1.size1 \
    command "XFPhotoSetPhoto4 $xfW 0 $xfClass"
  XFMiscSetResource .xf${xfClass}Config4.params1.params2.size.size2.size2 \
    command "XFPhotoSetPhoto4 $xfW 0 $xfClass"

  # save current parameter
#  XFElementSave $xfW $xfClass {background borderwidth blank ditherlevel imagesize gamma geometry palette xscrollcommand yscrollcommand}
  XFElementSave $xfW $xfClass {blank ditherlevel imagesize gamma geometry palette xscrollcommand yscrollcommand}

  bind .xf${xfClass}Config4.params1.params2.picname.picname $xfBind(configure) \
    "XFProcFSBoxFile {.xf${xfClass}Config4.params1.params2.picname.picname}"
  bind .xf${xfClass}Config4.params1.params2.picname.picname <Return> \
    "XFPhotoSetPhoto4 $xfW 0 $xfClass"

  # packing
  pack append .xf${xfClass}Config4.params1 \
              .xf${xfClass}Config4.params1.params2 {left fill expand}
  pack append .xf${xfClass}Config4 \
              .xf${xfClass}Config4.pathname {top fill frame center} \
              .xf${xfClass}Config4.leave {bottom fill} \
              .xf${xfClass}Config4.additional {bottom fill} \
              .xf${xfClass}Config4.params1 {top fill expand}

  XFBindFormConnect .xf${xfClass}Config4.params1.params2 \
    "XFPhotoSetPhoto4 $xfW 0 $xfClass"

  XFEditSetStatus "Calling parameter setting for Photo...done"
}

##########
# Procedure: XFSaveSpecial.Photo
# Description: save photo
# Arguments: xfW - the widget
# Returns: none
# Sideeffects: none
##########
proc XFSaveSpecial.Photo {xfW} {
  global photoList

  set xfResult ""
  if {![info exists photoList]} {
    set photoList(xfnone) ""
  }
  foreach xfCounter [array names photoList] {
    if {"$xfCounter" == "$xfW"} {
      set xfArrayName ""
      append xfArrayName photoList ( $xfCounter )
      if {![catch "set \"$xfArrayName\"" xfTmpRetVal]} {
        append xfResult "  $xfW blank\n"
        append xfResult "  readppm $xfW \"$xfTmpRetVal\"\n"
      }
    }
  }
  return $xfResult
}

##########
# Procedure: XFPhotoSetPhoto4
# Description: set photo parameters
# Arguments: xfW - the widget
#            xfType - the type of setting (1 = set always, 0 = set
#                     only if permanent apply is on)
#            xfClass - the class we configure
#            xfParam1 - ignored parameter
# Returns: none
# Sideeffects: none
##########
proc XFPhotoSetPhoto4 {xfW xfType xfClass {xfParam1 ""}} {
  global photoList
  global xfConf
  global xfMisc

  if {$xfType == 0 && !$xfConf(applyParameters)} {
    return
  }
  if {"[info commands $xfW]" == ""} {
    return
  }
  XFMiscSetSymbolicName $xfW \
    [.xf${xfClass}Config4.params1.params2.symname.symname get]

#  XFMiscSetResource $xfW background \
#    [.xf${xfClass}Config4.params1.params2.bg.bg get]
  if {"[.xf${xfClass}Config4.params1.params2.blank.blank get]" != ""} {
    XFMiscSetResource $xfW blank \
      [.xf${xfClass}Config4.params1.params2.blank.blank get]
  }
#  XFMiscSetResource $xfW borderwidth \
#    "[.xf${xfClass}Config4.params1.params2.borderwidth.borderwidth get]"
  XFMiscSetResource $xfW ditherlevel $xfMisc(photoDither)
  if {[.xf${xfClass}Config4.params1.params2.geo.geo1.geo1 get] > 0 &&
      [.xf${xfClass}Config4.params1.params2.geo.geo2.geo2 get] > 0} {
    XFMiscSetResource $xfW geometry \
      "[.xf${xfClass}Config4.params1.params2.geo.geo1.geo1 get]x[.xf${xfClass}Config4.params1.params2.geo.geo2.geo2 get]"
  }
  if {[.xf${xfClass}Config4.params1.params2.size.size1.size1 get] > 0 &&
      [.xf${xfClass}Config4.params1.params2.size.size2.size2 get] > 0} {
    XFMiscSetResource $xfW imagesize \
      "[.xf${xfClass}Config4.params1.params2.size.size1.size1 get]x[.xf${xfClass}Config4.params1.params2.size.size2.size2 get]"
  }
  XFMiscSetResource $xfW gamma \
    [expr [.xf${xfClass}Config4.params1.params2.gamma.gamma get]/10]
  if {[.xf${xfClass}Config4.params1.params2.frame3.bw get] > 0} {
    XFMiscSetResource $xfW palette \
      [.xf${xfClass}Config4.params1.params2.frame3.bw get]
  } {
    XFMiscSetResource $xfW palette \
      ".xf${xfClass}Config4.params1.params2.frame3.red get]/.xf${xfClass}Config4.params1.params2.frame3.green get]/.xf${xfClass}Config4.params1.params2.frame3.blue get]"
  }
  XFMiscSetResource $xfW xscrollcommand \
    [.xf${xfClass}Config4.params1.params2.xscrollcommand.xscrollcommand get]
  XFMiscSetResource $xfW yscrollcommand \
    [.xf${xfClass}Config4.params1.params2.yscrollcommand.yscrollcommand get]

  if {"[.xf${xfClass}Config4.params1.params2.picname.picname get]" ==""} {
    set xfArrayName ""
    append xfArrayName photoList ( $xfW )
    catch "unset \"$xfArrayName\""
  } {
    set xfArrayName ""
    append xfArrayName photoList ( $xfW )
    set "$xfArrayName" \
      [.xf${xfClass}Config4.params1.params2.picname.picname get]
    $xfW blank
    readppm $xfW [set "$xfArrayName"]
  }
}

# eof

