; 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.

; Requires GIMP 2.8 or later

(define (script-fu-sg-dissolve-group image item)
  (when (= (car (gimp-item-is-group item)) TRUE)
    (gimp-image-undo-group-start image)
    (let ((parent (car (gimp-item-get-parent item)))
          (pos (car (gimp-image-get-item-position image item))) )
      (let loop ((layers (reverse (vector->list (cadr (gimp-item-get-children item))))))
        (unless (null? layers)
          (let ((new-layer (car (gimp-layer-copy (car layers) (car (gimp-drawable-has-alpha item)))))
                (name (car (gimp-item-get-name (car layers)))) )
            (gimp-image-insert-layer image 
                                     new-layer 
                                     (if (= parent -1) 
                                       0 parent )
                                     pos )
            (gimp-image-remove-layer image (car layers))
            (gimp-item-set-name new-layer name) )
          (loop (cdr layers)) )))
    (gimp-image-remove-layer image item)
    (gimp-image-undo-group-end image)
    (gimp-displays-flush)
    )
  )
        
(script-fu-register "script-fu-sg-dissolve-group"
  "Dissolve Group"
  "Move layers from inside a group to its parent."
  "Saul Goode"
  "Saul Goode"
  "July 2013"
  "*"
  SF-IMAGE    "Image"    0
  SF-DRAWABLE "Layer" 0
  )

(script-fu-menu-register "script-fu-sg-dissolve-group"
 "<Layers>"
 )


