Permalink
Browse files

sketch out basic widget layout

  • Loading branch information...
1 parent 07c6ce5 commit 1810b30c3aceb80ae67e5669a65855bb518fd8b1 @mental committed May 29, 2012
Showing with 134 additions and 2 deletions.
  1. +2 −1 project.clj
  2. +132 −1 src/org/inkly/minicomic/core.clj
View
@@ -1,3 +1,4 @@
(defproject org.inkly.minicomic "1.0.0-SNAPSHOT"
:description "FIXME: write description"
- :dependencies [[org.clojure/clojure "1.3.0"]])
+ :dependencies [[org.clojure/clojure "1.3.0"]]
+ :main org.inkly.minicomic.core)
@@ -1 +1,132 @@
-(ns org.inkly.minicomic.core)
+(ns org.inkly.minicomic.core
+ (:import (java.awt BorderLayout
+ Color
+ GridBagConstraints
+ GridBagLayout)
+ (javax.swing Box
+ JComboBox
+ JFrame
+ JLabel
+ JPanel
+ JSpinner
+ JTextField
+ SwingUtilities)))
+
+(defn bind-unit-selector []
+ (doto (JComboBox.)
+ (.addItem "in")
+ (.addItem "cm")))
+
+(defn bind-measurement []
+ (JTextField. 4))
+
+(defn bind-small-count []
+ (JSpinner.))
+
+(def MAIN-BINDINGS
+ {:standard-paper-size
+ (fn []
+ (doto (JComboBox.)
+ (.addItem "US Letter")
+ (.addItem "US Legal")
+ (.addItem "A4")
+ (.addItem "Custom")))
+ :paper-units bind-unit-selector
+ :paper-width bind-measurement
+ :paper-height bind-measurement
+ :paper-up bind-small-count
+ :creep-increment bind-measurement
+ :creep-units bind-unit-selector
+ :creep-sheets bind-small-count
+ :preview (fn []
+ (doto (JPanel.)
+ (.setBackground Color/BLACK)))})
+
+(def MAIN-LAYOUT
+ [[:standard-paper-size
+ [:paper-width "×" :paper-height :paper-units]
+ :paper-aspect
+ ["÷" :paper-up "up"]
+ [:creep-increment :creep-units "creep"]
+ ["per" :creep-sheets "sheets"]]
+ ^{:expand true}
+ [^{:expand true} {:bind :preview}
+ :spreads
+ :current-spread]])
+
+(defn flip-axis [axis]
+ (case axis
+ :horizontal :vertical
+ :vertical :horizontal))
+
+(defn axis-to-vector [axis]
+ (case axis
+ :horizontal [1 0]
+ :vertical [0 1]))
+
+(defmacro setf! [obj field value]
+ `(set! (~field ~obj) ~value))
+
+(declare build-container-layout)
+
+(defn build-layout
+ ([layout bindings axis]
+ (cond
+ (vector? layout) (build-container-layout layout bindings axis)
+ (keyword? layout) (build-layout {:bind layout} bindings axis)
+ (string? layout) (JLabel. layout)
+ (map? layout) ((get bindings
+ (:bind layout)
+ #(JLabel. "[unbound]")))
+ :else (JLabel. (str "[unknown: " layout "]"))))
+ ([layout bindings]
+ (build-layout layout bindings :horizontal)))
+
+(defn make-constraints
+ ([axis]
+ (make-constraints axis false))
+ ([axis expand?]
+ (let [[row-inc col-inc] (axis-to-vector axis)
+ weight (if expand? 1 0)]
+ (doto (GridBagConstraints.)
+ (setf! .gridx (* row-inc GridBagConstraints/RELATIVE))
+ (setf! .gridy (* col-inc GridBagConstraints/RELATIVE))
+ (setf! .fill (if expand?
+ GridBagConstraints/BOTH
+ (case axis
+ :horizontal GridBagConstraints/VERTICAL
+ :vertical GridBagConstraints/HORIZONTAL)))
+ (setf! .weightx weight)
+ (setf! .weighty weight)))))
+
+(defn expanding? [layout]
+ (if (or (map? layout)
+ (vector? layout))
+ (:expand (meta layout))
+ false))
+
+(defn build-container-layout [sublayouts bindings axis]
+ (let [panel (JPanel. (GridBagLayout.))
+ no-expanding (not (some expanding? sublayouts))]
+ (doseq [sublayout sublayouts]
+ (.add panel
+ (build-layout sublayout bindings (flip-axis axis))
+ (make-constraints axis (expanding? sublayout))))
+ (when no-expanding
+ (.add panel
+ (Box/createGlue)
+ (make-constraints axis true)))
+ panel))
+
+(defn -main [& args]
+ (SwingUtilities/invokeLater
+ (fn []
+ (let [frame (JFrame. "Minicomic")]
+ (doto (.getContentPane frame)
+ (.add (build-layout MAIN-LAYOUT MAIN-BINDINGS)
+ BorderLayout/CENTER))
+ (doto frame
+ (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
+ (.pack)
+ (.setVisible true)
+ (.setExtendedState JFrame/MAXIMIZED_BOTH))))))

0 comments on commit 1810b30

Please sign in to comment.