view src/net/kryshen/indyvon/component.clj @ 85:e718a69f7d99

Observers: use weak keys, renamed some fns.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 01 Oct 2010 18:45:17 +0400
parents 5d2153e8a28d
children dd7b8dbb20bc
line wrap: on
line source

;;
;; Copyright 2010 Mikhail Kryshen <mikhail@kryshen.net>
;;
;; This file is part of Indyvon.
;;
;; Indyvon is free software: you can redistribute it and/or modify it
;; under the terms of the GNU Lesser General Public License version 3
;; only, as published by the Free Software Foundation.
;;
;; Indyvon 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
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with Indyvon.  If not, see
;; <http://www.gnu.org/licenses/>.
;;

(ns net.kryshen.indyvon.component
  "Integrating Indyvon into AWT and Swing components."
  (:use
   net.kryshen.indyvon.core)
  (:import
   (net.kryshen.indyvon.core Size Bounds)
   (java.awt Graphics2D Component Dimension Color)
   (java.awt.geom Rectangle2D$Double)
   (javax.swing JFrame JPanel)))

(defn- font-context [^Component component]
  (.getFontRenderContext (.getFontMetrics component (.getFont component))))

(defn make-jpanel
  ([layer]
     (make-jpanel layer (root-event-dispatcher)))
  ([layer event-dispatcher]
     (let [panel
           (proxy [JPanel] []
             (paintComponent [g]
               (let [size (.getSize ^Component this)]
                 (draw-root! layer g (.width size) (.height size)
                             event-dispatcher this)))
             (getPreferredSize []
               (let [s (root-size layer (font-context this) this)]
                 (Dimension. (:width s) (:height s)))))]
       (.setBackground panel (:back-color *theme*))
       (add-observer panel layer (fn [w _]
                                   ;; Use the first observer argument
                                   ;; instead of closing over panel to
                                   ;; allow the panel and associated
                                   ;; observer to be gc'd.
                                   (.repaint ^Component w)))
       (listen! event-dispatcher panel)
       panel)))

(defn make-jframe [title layer]
  (doto (JFrame. title)
    (.. (getContentPane) (add (make-jpanel layer)))
    (.pack)))