require 'fox'
require 'rouge/xul'

include Fox

module Rouge
	class Toolkit 
		include RougeToolkit

		def initialize
			@application = FXApp.new
			@container_stack = []
			@id_option = -1
			@radio_groups = {}
			@radio_count = {}
			@container = []
		end

		def run
			@application.run
		end

		def exit_widget
			if @in_menu
				end_menu(@in_menu)
				@in_menu = false
			end

			if @container.pop
				last = @container_stack.pop
				if last == @root
					@application.create
					window = @container_stack.pop
					window.create
					window.show
				end
			end
		end

		def window title, orient=:vertical
			@container << true
			if @root
				window = FXDialogBox.new( @application, title )
				mapping = { 'close'=>proc {|widget|
					@application.stopModal()
					widget.hide
				}, 'show'=>'execute' }
			else
				window = FXMainWindow.new( @application, title )
				mapping = { 'close'=>'close', 'show'=>'show' }
			end
			@container_stack.push( window )
			@root = box( orient )
			WidgetWrapper.new( window, mapping )
		end

		def box layout=:vertical
			@container << true
			box = nil
			if layout == :vertical
				box = FXVerticalFrame.new(@container_stack[-1],
				LAYOUT_SIDE_LEFT|LAYOUT_FILL_Y|PACK_UNIFORM_WIDTH)
			else
				box = FXHorizontalFrame.new( @container_stack[-1],
				LAYOUT_SIDE_LEFT|FRAME_NONE|LAYOUT_FILL_X|LAYOUT_FILL_Y|PACK_UNIFORM_WIDTH)
			end
			@container_stack.push( box )
			box
		end
				
		# image, align unsupported
		def button label, image=nil, align=nil, &block
			@container << false
			widget = FXButton.new( @container_stack[-1], label )
			if block_given?
				widget.connect( SEL_COMMAND, &block )
			end
			widget
		end

		def text label
			@container << false
			FXLabel.new(@container_stack[-1], label, nil,
				LAYOUT_CENTER_Y|LAYOUT_SIDE_LEFT|JUSTIFY_LEFT)
		end

		def textbox width=10
			@container << false
			WidgetWrapper.new( 
				FXTextField.new( @container_stack[-1], width ),
				{ "value"=>"text", "value="=>"text=" }
				)
			# (matrix, 10, @intTarget, FXDataTarget::ID_VALUE,
			#LAYOUT_CENTER_Y|LAYOUT_CENTER_X|FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_ROW)
		end
		
		def radiogroup groupname
			@container << false
			@radio_groups[groupname] = FXDataTarget.new(1)
			@radio_count[groupname] = FXDataTarget::ID_OPTION + 1
			@radio_groups[groupname]
		end

		def radio label, groupname
			@container << false
			group = @radio_groups[groupname]
			@radio_count[groupname] += 1
			FXRadioButton.new(@container_stack[-1],label,group,@radio_count[groupname])
		end

		def checkbox label
			@container << false
			FXCheckButton.new( @container_stack[-1], label )
		end

		def separator
			@container << false
			FXHorizontalSeparator.new(@container_stack[-1])
			#, LAYOUT_SIDE_TOP|SEPARATOR_GROOVE|LAYOUT_FILL_X)
		end

		def progressmeter
			@container << false
			bar = FXProgressBar.new(@container_stack[-1])
			bar.total = 100
			WidgetWrapper.new( bar, 
				{ 'value'=>'progress', 'value='=>'progress=' } 
				)
			#, @progressTarget, FXDataTarget::ID_VALUE, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|FRAME_SUNKEN|FRAME_THICK)
		end

		# menulist -> menupopup -> menuitem
		# menulist is equivalent to a button that takes the value of the selected
		# option (checked).  Menupopup is merely the popup window itself; the
		# menuitem is a list content.  Menupopup can contain any windowish item.
		# <menubutton label="regular menubutton">
		# <menupopup>
		#    <menuitem label="foo 1"/>
		#    <menuitem label="foo 2"/>
		#    <menuitem label="foo 3"/>
		#  </menupopup>
		# </menubutton>
		# <menulist label="disabled menulist" disabled="true">
		#  <menupopup>
		#    <menuitem label="foo 1"/>
		#    <menuitem label="foo 2"/>
		#    <menuitem label="foo 3"/>
		#  </menupopup>
		# </menulist>    
		def menubutton( label )
			@container << false
			@in_menu = label
		end

		def menupopup
		end

		def menuitem( label, &block )
			FXMenuCommand.new( menu, label, nil, self
		end

		def end_menu label
			FXMenuButton.new(@container[-1], label, nil, menu, (MENUBUTTON_DOWN|
      JUSTIFY_LEFT|LAYOUT_TOP|FRAME_RAISED|FRAME_THICK|ICON_AFTER_TEXT|
      LAYOUT_CENTER_X|LAYOUT_CENTER_Y))
		end

		def stub
			@container << false
		end

		def start
			@application.create()
			@application.run()
		end
	end
end
