view src/net/kryshen/indyvon/component.clj @ 106:f42e2b9e1ad9

Removed Anchored protocol, "layer-size" function in Layer replaced with "geometry" which returns a structure describing both layer size and anchor point. Indyvon now requires Clojure 1.3.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 21 Sep 2011 02:27:11 +0300
parents 9874107e3e96
children f3dedece38f3
line source
1 ;;
2 ;; Copyright 2010, 2011 Mikhail Kryshen <mikhail@kryshen.net>
3 ;;
4 ;; This file is part of Indyvon.
5 ;;
6 ;; Indyvon is free software: you can redistribute it and/or modify it
7 ;; under the terms of the GNU Lesser General Public License version 3
8 ;; only, as published by the Free Software Foundation.
9 ;;
10 ;; Indyvon is distributed in the hope that it will be useful, but
11 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with Indyvon. If not, see
17 ;; <http://www.gnu.org/licenses/>.
18 ;;
20 (ns net.kryshen.indyvon.component
21 "Integrating Indyvon into AWT and Swing components."
22 (:use
23 net.kryshen.indyvon.core)
24 (:import
25 (java.awt Graphics Component Dimension Color)
26 (java.awt.geom Rectangle2D$Double)
27 (javax.swing JFrame JPanel JOptionPane)))
29 (defn font-context [^Component component]
30 (.getFontRenderContext (.getFontMetrics component (.getFont component))))
32 (defmacro with-component [component & body]
33 `(let [c# ~component]
34 (binding [*target* c#
35 *font-context* (font-context c#)]
36 ~@body)))
38 (defn ^JPanel make-jpanel
39 ([layer]
40 (make-jpanel layer (root-event-dispatcher)))
41 ([layer event-dispatcher]
42 (let [panel
43 (proxy [JPanel] []
44 (paintComponent [^Graphics g]
45 (let [size (.getSize ^Component this)]
46 (.setColor g (:back-color *theme*))
47 (.fillRect g 0 0 (.width size) (.height size))
48 (draw-root! layer g (.width size) (.height size)
49 event-dispatcher this)))
50 (getPreferredSize []
51 (let [geom (root-geometry layer (font-context this) this)]
52 (Dimension. (width geom) (height geom)))))]
53 (.setBackground panel (:back-color *theme*))
54 (add-observer panel layer (fn [w _]
55 ;; Use the first observer argument
56 ;; instead of closing over panel to
57 ;; allow the panel and associated
58 ;; observer to be gc'd.
59 (.repaint ^Component w)))
60 (listen! event-dispatcher panel)
61 panel)))
63 (defn make-jframe [title layer]
64 (doto (JFrame. title)
65 (.. (getContentPane) (add (make-jpanel layer)))
66 (.pack)))
68 (defn message [m]
69 (JOptionPane/showMessageDialog *target* m))