; paste-as-new-centered.scm
; by Rob Antonishen
; http://ffaat.pointclark.net

; Version 1.1 (20100604)

; Description
;
; pastes the current buffer as a new layer centered on the current selection, 
; above the current active layer, or centered in the canvas if there is no active layer.

; Changes
; 1.1 -Added a check that the active layer is NOT a flaoting selection (thanks Gino D!)

; License:
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version. 
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html


(define (script-fu-paste-as-new-centered img drawable)

  (let* ((temp-image (car (gimp-edit-paste-as-new)))
         (active-layer (car (gimp-image-get-active-layer img)))
         (selection-info (gimp-selection-bounds img))
         (new-layer 0)
         (buffer-width 0)
         (buffer-height 0)
         (offset-x 0)
         (offset-y 0)
         (saved-selection 0))

    (cond 
      ((= FALSE (car (gimp-image-is-valid temp-image)))
       (gimp-message "There is no image data in the clipboard to paste."))
       
      ((= TRUE (car (gimp-layer-is-floating-sel active-layer)))
       (gimp-message "The script doesn't affect floating selections."))
       
      (else
        (gimp-image-undo-group-start img)
        (set! buffer-width (car (gimp-image-width temp-image)))
        (set! buffer-height (car (gimp-image-height temp-image)))
        (gimp-image-delete temp-image)
        (set! new-layer (car (gimp-layer-new img buffer-width buffer-height (+ (* (car (gimp-image-base-type img)) 2) 1) "Clipboard" 100 NORMAL-MODE)))
        (gimp-image-add-layer img new-layer -1)
        (if (= (car selection-info) FALSE) ; no selection
          (begin 
            (gimp-floating-sel-anchor (car (gimp-edit-paste new-layer TRUE)))
            (if (= active-layer -1)
              (begin 
                (set! offset-x (round (/ (- (car (gimp-image-width img)) buffer-width) 2)))
                (set! offset-y (round (/ (- (car (gimp-image-height img)) buffer-height) 2))))
              (begin 
                (set! offset-x (+ (round (/ (- (car (gimp-drawable-width active-layer)) buffer-width) 2)) (car (gimp-drawable-offsets active-layer))))
                (set! offset-y (+ (round (/ (- (car (gimp-drawable-height active-layer)) buffer-height) 2)) (cadr (gimp-drawable-offsets active-layer)))))
            )
          )
          (begin  ;else selection
            (set! saved-selection (car (gimp-selection-save img)))
            (gimp-selection-none img)
            (gimp-floating-sel-anchor (car (gimp-edit-paste new-layer TRUE)))
            (set! offset-x (+ (round (/ (- (- (list-ref selection-info 3) (list-ref selection-info 1)) buffer-width) 2)) (list-ref selection-info 1)))
            (set! offset-y (+ (round (/ (- (- (list-ref selection-info 4) (list-ref selection-info 2)) buffer-height) 2)) (list-ref selection-info 2)))
            (gimp-selection-load saved-selection)
            (gimp-image-remove-channel img saved-selection)
          )
        )
        (gimp-layer-translate new-layer offset-x offset-y)
        (gimp-displays-flush)
        (gimp-image-undo-group-end img)
      )
    )
  )
)

(script-fu-register "script-fu-paste-as-new-centered"
  "New _Centered Layer"
  "Paste the clipboard contents into a new centered layer"
  "Rob Antonishen"
  "Rob Antonishen"
  "2010-06"
  "*"
  SF-IMAGE      "image"      0
  SF-DRAWABLE   "drawable"   0
)

(script-fu-menu-register "script-fu-paste-as-new-centered"
                         "<Image>/Edit/Paste as")
