; STPshiny/theme.jl

(let*
  (
    (font-colors (list "dimgrey" "black"))
    
    (title-width
      (lambda (w)
        (let
          ((w-width (car (window-dimensions w))))
        (max 0 (+ 20 (min (- w-width 55) (text-width (window-name w) font)))))))
          
    (update-title-width
      (lambda (w)
        (if (eq (window-get w 'current-frame-style) 'STPshiny) 
          (rebuild-frame w))))
    
    (title-images (make-image "title_mid.png"))
    (title-shade-images (make-image "title_mid_shade.png"))
    (left-title-images (make-image "title_left.png"))
    (left-title-shade-images (make-image "title_left_shade.png"))
    (right-title-images (list (make-image "title_right_off.png")
                              (make-image "title_right.png")))
    (right-title-shade-images (list (make-image "title_right_shade_off.png")
                                    (make-image "title_right_shade.png")))
    (close-images (list (make-image "button_close_off.png") 
                        (make-image "button_close.png") nil
                        (make-image "button_close_in.png")))
    (iconify-images (list (make-image "button_min_off.png")
                          (make-image "button_min.png") nil
                          (make-image "button_min_in.png")))
    (maximize-images (list (make-image "button_max_off.png")
                           (make-image "button_max.png") nil
                           (make-image "button_max_in.png")))
    (bottom-image (make-image "border_bottom.png"))
    (top-image (make-image "border_top.png"))
    (top-shade-image (make-image "border_top_shade.png"))
    (top-left-image (make-image "border_topleft.png"))
    (top-left-shade-image (make-image "border_topleft_shade.png"))
    (top-right-image (list (make-image "border_topright_off.png")
                           (make-image "border_topright.png")))
    (top-right-shade-image (list (make-image "border_topright_shade_off.png")
                                 (make-image "border_topright_shade.png")))
    (bottom-left-image (make-image "border_bottomleft.png"))
    (bottom-right-image (list (make-image "border_bottomright_off.png")
                              (make-image "border_bottomright.png")))
    (left-image (make-image "border_left.png"))
    (right-image (list (make-image "border_right_off.png")
                       (make-image "border_right.png")))

    (frame `(
        ((background . ,left-image)
         (left-edge . -6)
         (top-edge . 0)
         (bottom-edge . 0)
         (class . left-border))
        ((background . ,right-image)
         (right-edge . -6)
         (top-edge . 0)
         (bottom-edge . 0)
         (class . right-border))
        ((background . ,bottom-image)
         (left-edge . 0)
         (right-edge . 0)
         (bottom-edge . -6)
         (class . bottom-border))
        ((background . ,bottom-left-image)
         (left-edge . -6)
         (bottom-edge . -6)
         (class . bottom-left-corner))
        ((background . ,bottom-right-image)
         (right-edge . -6)
         (bottom-edge . -6)
         (class . bottom-right-corner))
        ((background . ,title-images)
         (foreground . ,font-colors)
         (text . ,window-name)
         (top-edge . -20)
         (left-edge . 0)
         (right-edge . 25)
	 (y-justify . 3)
         (x-justify . 0)
         (cursor . left_ptr)
         (class . title))
        ((background . ,left-title-images)
         (left-edge . -6)
         (top-edge . -20)
         (cursor . left_ptr)
         (class . title))
        ((background . ,right-title-images)
         (right-edge . -6)
         (top-edge . -20)
         (cursor . left_ptr)
         (class . title))
        ((background . ,close-images)
         (right-edge . 29)
         (top-edge . -15)
         (cursor . left_ptr)
         (class . close-button))
        ((background . ,iconify-images)
         (right-edge . 12)
         (top-edge . -15)
         (cursor . left_ptr)
         (class . iconify-button))
        ((background . ,maximize-images)
         (right-edge . -1)
         (top-edge . -15)
         (cursor . left_ptr)
         (class . maximize-button))
      ))
      
      (shaded-frame `(
        ((background . ,title-shade-images)
         (foreground . ,font-colors)
         (text . ,window-name)
         (top-edge . -20)
         (left-edge . 0)
         (right-edge . 25)
         (y-justify . 3)
         (x-justify . 0)
         (cursor . left_ptr)
         (class . title))
        ((background . ,left-title-shade-images)
         (left-edge . -6)
         (top-edge . -20)
         (cursor . left_ptr)
         (class . title))
        ((background . ,right-title-shade-images)
         (right-edge . -6)
         (top-edge . -20)
         (cursor . left_ptr)
         (class . title))
        ((background . ,close-images)
         (right-edge . 29)
         (top-edge . -15)
         (cursor . left_ptr)
         (class . close-button))
        ((background . ,iconify-images)
         (right-edge . 12)
         (top-edge . -15)
         (cursor . left_ptr)
         (class . iconify-button))
        ((background . ,maximize-images)
         (right-edge . -1)
         (top-edge . -15)
         (cursor . left_ptr)
         (class . maximize-button))
      ))

      ;; modal dialogs should have no controls
      (border-only-frame `(
        ((background . ,top-image)
         (top-edge . -6)
         (left-edge . 0)
         (right-edge . 0)
         (cursor . left_ptr)
         (class . title))
        ((background . ,left-image)
         (left-edge . -6)
         (top-edge . 0)
         (bottom-edge . 0)
         (cursor . left_ptr)
         (class . title))
        ((background . ,top-left-image)
         (left-edge . -6)
         (top-edge . -6)
         (cursor . left_ptr)
         (class . title))
        ((background . ,top-right-image)
         (right-edge . -6)
         (top-edge . -6)
         (cursor . left_ptr)
         (class . title))
        ((background . ,right-image)
         (right-edge . -6)
         (top-edge . 0)
         (bottom-edge . 0)
         (cursor . left_ptr)
         (class . title))
        ((background . ,bottom-image)
         (left-edge . 0)
         (right-edge . 0)
         (bottom-edge . -6)
         (cursor . left_ptr)
         (class . title))
        ((background . ,bottom-left-image)
         (left-edge . -6)
         (bottom-edge . -6)
         (cursor . left_ptr)
         (class . title))
        ((background . ,bottom-right-image)
         (right-edge . -6)
         (bottom-edge . -6)
         (cursor . left_ptr)
         (class . title))
      ))

     (shaded-transient-frame `(
        ((background . ,top-shade-image)
         (top-edge . -6)
         (left-edge . 0)
         (right-edge . 0)
         (cursor . left_ptr)
         (class . title))
        ((background . ,top-left-shade-image)
         (left-edge . -6)
         (top-edge . -6)
         (cursor . left_ptr)
         (class . title))
        ((background . ,top-right-shade-image)
         (right-edge . -6)
         (top-edge . -6)
         (cursor . left_ptr)
         (class . title))
      ))
    )

  (add-frame-style 'STPshiny
    (lambda (w type)
      (cond 
            ((eq type 'shaped) shaded-frame)
            ((eq type 'shaded) shaded-frame)
            ((eq type 'transient) border-only-frame)
            ((eq type 'shaped-transient) shaded-transient-frame)
            ((eq type 'shaded-transient) shaded-transient-frame)
            ((eq type 'unframed) nil-frame)
            (t frame))))
            
  (call-after-property-changed 'WM_NAME update-title-width)
)
