view src/net/kryshen/indyvon/component.clj @ 101:9874107e3e96

Clojure 1.3 compatibility, mouse wheel support, scalable viewport, additional layer implementations.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 18 May 2011 20:50:49 +0400
parents bf4a46f80851
children 491152048c89 f42e2b9e1ad9
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 (net.kryshen.indyvon.core Size Bounds)
26 (java.awt Graphics Component Dimension Color)
27 (java.awt.geom Rectangle2D$Double)
28 (javax.swing JFrame JPanel JOptionPane)))
30 (defn font-context [^Component component]
31 (.getFontRenderContext (.getFontMetrics component (.getFont component))))
33 (defmacro with-component [component & body]
34 `(let [c# ~component]
35 (binding [*target* c#
36 *font-context* (font-context c#)]
37 ~@body)))
39 (defn ^JPanel make-jpanel
40 ([layer]
41 (make-jpanel layer (root-event-dispatcher)))
42 ([layer event-dispatcher]
43 (let [panel
44 (proxy [JPanel] []
45 (paintComponent [^Graphics g]
46 (let [size (.getSize ^Component this)]
47 (.setColor g (:back-color *theme*))
48 (.fillRect g 0 0 (.width size) (.height size))
49 (draw-root! layer g (.width size) (.height size)
50 event-dispatcher this)))
51 (getPreferredSize []
52 (let [s (root-size layer (font-context this) this)]
53 (Dimension. (:width s) (:height s)))))]
54 (.setBackground panel (:back-color *theme*))
55 (add-observer panel layer (fn [w _]
56 ;; Use the first observer argument
57 ;; instead of closing over panel to
58 ;; allow the panel and associated
59 ;; observer to be gc'd.
60 (.repaint ^Component w)))
61 (listen! event-dispatcher panel)
62 panel)))
64 (defn make-jframe [title layer]
65 (doto (JFrame. title)
66 (.. (getContentPane) (add (make-jpanel layer)))
67 (.pack)))
69 (defn message [m]
70 (JOptionPane/showMessageDialog *target* m))