view src/indyvon/component.clj @ 154:ed36fcf061de

Removed the domain part from namespace names.
author Mikhail Kryshen <mikhail@kryshen.net>
date Mon, 14 Apr 2014 20:01:00 +0400
parents
children e0063c1d0f7f
line source
1 ;;
2 ;; Copyright 2010-2014 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 indyvon.component
21 "Integrating Indyvon into AWT and Swing components."
22 (:use
23 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- paint-component [^Component c ^Graphics g scene]
30 (let [w (.getWidth c)
31 h (.getHeight c)]
32 (.setColor g (:back-color *theme*))
33 (.fillRect g 0 0 w h)
34 (draw-scene! scene g w h)))
36 (defn- preferred-size [^Component c scene]
37 (let [geom (scene-geometry scene)]
38 (Dimension. (width geom) (height geom))))
40 (defn ^JPanel make-jpanel
41 ([view]
42 (make-jpanel view (root-event-dispatcher)))
43 ([view event-dispatcher]
44 (let [panel (proxy [JPanel] [])
45 scene (make-scene
46 view event-dispatcher panel
47 (.getDesktopProperty (java.awt.Toolkit/getDefaultToolkit)
48 "awt.font.desktophints"))]
49 (update-proxy
50 panel
51 {"paintComponent" #(paint-component %1 %2 scene)
52 "getPreferredSize" #(preferred-size % scene)})
53 (.setBackground panel (:back-color *theme*))
54 (add-observer panel scene (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 ^JFrame make-jframe [^String title view]
64 (doto (JFrame. title)
65 (.. (getContentPane) (add (make-jpanel view)))
66 (.pack)))
68 (defn message [m]
69 (JOptionPane/showMessageDialog (:component *scene*) m))