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

; pre-2.8 COMPATIBILITY
  (when (not (defined? 'gimp-image-insert-layer))
    (define (gimp-image-insert-layer image layer parent position)
      (gimp-image-add-layer image layer position) ))

(define (script-fu-sg-grid-layer image)
  ;; Create a physical instantiation of the image grid (as a layer)
  ;
  (let ((grid-fg-color (car (gimp-image-grid-get-foreground-color image)))
        (grid-bg-color (car (gimp-image-grid-get-background-color image)))
        (grid-offset (car (gimp-image-grid-get-offset image)))
        (grid-spacing (car (gimp-image-grid-get-spacing image)))
        (grid-style (car (gimp-image-grid-get-style image)))
        )
    (gimp-image-undo-group-start image)
    (let ((orig-sel (car (gimp-selection-save image)))
          (layer (car (gimp-layer-new image 
                                      (car (gimp-image-width image)) 
                                      (car (gimp-image-height image))
                                      (+ (* (car (gimp-image-base-type image)) 2) 1)
                                      "Grid"
                                      100
                                      NORMAL-MODE ))))
      (gimp-image-insert-layer image layer 0 0)
      (gimp-selection-none image)
      (gimp-drawable-fill layer TRANSPARENT-FILL)
      (if (= grid-style GRID-DOTS)
        (plug-in-grid RUN-NONINTERACTIVE image layer
                      0 grid-spacing grid-offset grid-fg-color 255
                      0 grid-spacing grid-offset grid-fg-color 255
                      1 0 1 grid-fg-color 255 ) 
        (if (= grid-style GRID-INTERSECTIONS)
          (plug-in-grid RUN-NONINTERACTIVE image layer
                        0 grid-spacing grid-offset grid-fg-color 255
                        0 grid-spacing grid-offset grid-fg-color 255
                        3 0 1 grid-fg-color 255 )
          (plug-in-grid RUN-NONINTERACTIVE image layer
                        1 grid-spacing grid-offset grid-fg-color 255
                        1 grid-spacing grid-offset grid-fg-color 255
                        0 grid-spacing grid-offset grid-fg-color 255 )))
      (gimp-selection-load orig-sel)
      (gimp-image-remove-channel image orig-sel) )
    (gimp-image-undo-group-end image) )
  (gimp-displays-flush)
  )
        
(script-fu-register "script-fu-sg-grid-layer"
  "New Layer From Image Grid"
  "Create a layer from current grid configuration."
  "Saul Goode"
  "Saul Goode"
  "July 2014"
  "*"
  SF-IMAGE    "Image"    0
  )

(script-fu-menu-register "script-fu-sg-grid-layer"
  "<Image>/Filters/Misc"
  )

