
(defaults-to q-borderwidth 0)

(: iconify-fsm
   (fsm-make 
    (: original-state
       (state-make 
	(on (buttonpress any any) () iconify-state)))
    (: iconify-state 
       (state-make
	(on (buttonrelease 1 any) (iconify-window) original-state)
	(on (buttonrelease 2 any) (restore-window) original-state)
	(on (buttonrelease 3 any) 
	    (if (= (# 'zoomed window-property) 1)
		(normalize-window) (zoom-window)) 
	    original-state)
	))))

(: icon-pixmap (pixmap-make 'iconify))

(: icon-plug '(with (borderwidth 1 background white foreground black
;				 cursor plug-cursor
				 borderpixel background fsm  iconify-fsm)
	(plug-make icon-pixmap)))   ;;;; Need to make these better pixmaps!!!

(: function-fsm
   (fsm-make 
    (: original-state
       (state-make 
	(on (buttonpress any any) () function-state)))
    (: function-state 
       (state-make
	(on (buttonrelease 1 any) 
	    (if (= (# 'zoomed window-property) 1)
		(normalize-window) (zoom-window 'horizontally)) 
	    original-state)
	(on (buttonrelease 2 any) (bounce-window) original-state)
	(on (buttonrelease 3 any) 
	    (if (= (# 'zoomed window-property) 1)
		(normalize-window) (zoom-window 'vertically)) 
	    original-state)
	))))

(: function-plug '(with (borderwidth 1 background white foreground black
;				     cursor plug-cursor
				     borderpixel background fsm function-fsm)
			(plug-make (pixmap-make 'iconify))))

(: edit-fsm  
   (fsm-make 
    (: q.edit-fsm.normal 
       (state-make
	(on (double-button any any) 
	    (progn
	      (set-focus wob)
	      (wob-cursor edit-cursor)
	      (wob-background black)
	      (with (foreground white)
		    (wob-tile 
		     (active-label-make
		      (# 'title wob) title-font))))
	    q.edit-fsm.editable)
	(on (button any (together with-alt with-control))
	    (progn
	      (set-focus wob)
	      (wob-background black)
	      (with (foreground white)
		    (wob-tile 
		     (active-label-make
		      (# 'title wob) title-font))))
	    q.edit-fsm.editable)
	(on (user-event 'name-change)
	    (progn
	      (## 'title wob (: s window-name))
	      (with (foreground black)
		    (wob-tile (active-label-make s title-font)))
	      (send-user-event 'get-s (window-icon))
	      ))
	standard-title-behavior  
	))
    (: q.edit-fsm.editable
       (state-make
	(on (keypress (key-make "Return") any)
	    (q.edit-fsm.de-edit)
	    q.edit-fsm.normal)
	(on (double-button any any)
	    (q.edit-fsm.de-edit)
	    q.edit-fsm.normal)
	(on (keypress "BackSpace" any)
	    (progn
	      (## 'title wob 
			 (: s (match "\\(.*\\)."
				     (# 'title wob) 1)))
	      (with (foreground white)
		    (wob-tile (active-label-make s title-font)))
	      (send-user-event 'get-s (window-icon))
	      ))
	(on (keypress "Delete" any)
	    (progn
	      (## 'title wob (: s ""))
	      (with (foreground white)
		    (wob-tile (active-label-make s title-font)))
	      (send-user-event 'get-s (window-icon))
	      ))
	(on (keypress any any)
	    (progn
	      (## 'title wob 
			 (: s (+ (# 'title wob) (last-key))))
	      (with (foreground white)
		    (wob-tile (active-label-make s title-font)))
	      (send-user-event 'get-s (window-icon))
	      ))
	(on (user-event 'name-change) 
	    (progn
	      (## 'title wob (: s window-name))
	      (with (foreground white)
		    (wob-tile (active-label-make s title-font)))
	      (send-user-event 'get-s (window-icon))
	      ))
	(on focus-out
	    (q.edit-fsm.de-edit)
	    q.edit-fsm.normal)
	standard-title-behavior
	))
    ))

(de q.edit-fsm.de-edit ()
;    (wob-cursor plug-cursor)
    (wob-background white)
    (with (foreground black)
	  (wob-tile 
	   (active-label-make
	    (# 'title wob) title-font)))
    (set-focus))

(: titlebar-fsm 
    (fsm-make
	(state-make
	    (on (user-event 'focus-in) (wob-background black))
	    (on (user-event 'focus-out) (wob-background white))
	    standard-title-behavior)))
    
(: editable-plug '(with (borderwidth 1 background white foreground black
				     border-pixel black
				     font title-font ; cursor plug-cursor
				     property (list 'title window-name) 
				     fsm edit-fsm)
			(plug-make (label-make window-name))))

(: titlebar (: my-title-bar 
	       (with (borderwidth 1 background black fsm titlebar-fsm
			 borderpixel black ;cursor title-cursor
			 bar-min-width 16 bar-max-width 22)
		     (bar-make editable-plug () function-plug icon-plug ))))

(: q-window-fsm
   (fsm-make
    (state-make
     (on focus-in
	 (progn (send-user-event 'focus-in)
		(wob-borderpixel black)))
     (on focus-out
	 (progn (send-user-event 'focus-out)
		(wob-borderpixel white)))
     standard-window-behavior
     )))


(: q-win.result '(with (inner-borderwidth 1 fsm q-window-fsm 
					  borderwidth q-borderwidth
					  borderpixel white)
		       (window-make titlebar () () () ())))

(defname 'q-win.data screen. ())
(df q-win () (if q-win.data q-win.data
		 (: q-win.data (eval q-win.result))))

