view src/indyvon/component.clj @ 185:83241889daac

BorderBox: properly wrap Geometry of the center view.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 21 Nov 2017 19:06:25 +0300
parents 8769a7b50b4f
children
line source
1 ;;
2 ;; Copyright 2010-2015 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 EventQueue)
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 (theme-get :back-color))
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 "Creates a JPanel to display the specified View."
42 ([view]
43 (make-jpanel view (root-event-dispatcher)))
44 ([view event-dispatcher]
45 (let [panel (proxy [JPanel] [])
46 scene (make-scene
47 view event-dispatcher panel
48 (.getDesktopProperty (java.awt.Toolkit/getDefaultToolkit)
49 "awt.font.desktophints"))]
50 (update-proxy
51 panel
52 {"paintComponent" #(paint-component %1 %2 scene)
53 "getPreferredSize" #(preferred-size % scene)})
54 (.setBackground panel (theme-get :back-color))
55 (add-observer! panel scene (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 ^JFrame make-jframe
65 "Creates a JFrame to display the specified View."
66 [^String title view]
67 (doto (JFrame. title)
68 (.. (getContentPane) (add (make-jpanel view)))
69 (.pack)))
71 (defn ^JFrame show-view!
72 "Creates and shows a JFrame with the specified View."
73 ([view]
74 (show-view! "Indyvon" view))
75 ([title view]
76 (let [f (atom nil)]
77 (EventQueue/invokeAndWait
78 #(doto ^JFrame (reset! f (make-jframe title view))
79 (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
80 (.setVisible true)))
81 @f)))
83 (defn message [m]
84 (JOptionPane/showMessageDialog (:component *scene*) m))