# Matt666 Juil. 2008

# Permission to use, copy, modify, and distribute this software for 
# any purpose and without fee is hereby granted, provided that the above
# copyright notice appear in all copies.

#Create a toolbar that easily toggle units 

#Thanks to CadFather for the icons !! ^^
#
# Oct, 2009 jim.foltz@gmail.com
#  - wrapped code in modules Matt666::ToggleUnits
#  - added require 'langhandler'
#  - added Strings handling
#  - added UI.refresh_inspectors to update Model Info window on change
#  - added upper and lower precision validation procs for toolbar buttons
#
require 'sketchup'
require 'langhandler'

module Matt666
    module ToggleUnits
	module_function

	strings = LanguageHandler.new("toggle_units.strings")
	Strings = {} 
	Strings["UnitsOptions"] = strings.GetString("UnitsOptions")
	Strings["LengthFormat"] = strings.GetString("LengthFormat")
	Strings["LengthPrecision"] = strings.GetString("LengthPrecision")
	Strings["LengthUnit"] = strings.GetString("LengthUnit")
	Strings["LengthSnapEnabled"] = strings.GetString("LengthSnapEnabled")
	Strings["LengthSnapLength"] = strings.GetString("LengthSnapLength")
	Strings["AnglePrecision"] = strings.GetString("AnglePrecision")
	Strings["AngleSnapEnabled"] = strings.GetString("AngleSnapEnabled")
	Strings["SnapAngle"] = strings.GetString("SnapAngle")
	Strings["SuppressUnitsDisplay"] = strings.GetString("SuppressUnitsDisplay")
	Strings["ForceInchDisplay"] = strings.GetString("ForceInchDisplay")

	Strings["Increase precision"] = strings.GetString("Increase precision")
	Strings["Decrease precision"] = strings.GetString("Decrease precision")
	Strings["Display format"] = strings.GetString("Display format")
	Strings["Decimal"] = strings.GetString("Decimal")
	Strings["Architectural"] = strings.GetString("Architectural")
	Strings["Engineering"] = strings.GetString("Engineering")
	Strings["Fractional"] = strings.GetString("Fractional")
	Strings["Inch"] = strings.GetString("Inch")
	Strings["Feet"] = strings.GetString("Feet")
	Strings["Millimeter"] = strings.GetString("Engineering")
	Strings["Centimeter"] = strings.GetString("Centimeter")
	Strings["Meter"] = strings.GetString("Meter")

	def toggle_units(int)
	    model = Sketchup.active_model
	    case int
	    when 0,1,2,3,4
		model.options[Strings['UnitsOptions']][2] = int

	    when 5,6,7,8
		result = int - 5
		case result
		when 1;model.options[Strings['UnitsOptions']][Strings['LengthUnit']] = 1
		when 2;model.options[Strings['UnitsOptions']][Strings['LengthUnit']] = 0
		when 3;model.options[Strings['UnitsOptions']][Strings['LengthUnit']] = 1
		end
		model.options[Strings['UnitsOptions']][Strings['LengthFormat']] = result
	    end
	    model.active_view.invalidate
	    UI.refresh_inspectors
	    return nil
	end

	def toggle_units_validation_proc(int)
	    case int
	    when 0,1,2,3,4
		if Sketchup.active_model.options[Strings['UnitsOptions']][Strings['LengthFormat']] != 0
		    MF_GRAYED
		else
		    if Sketchup.active_model.options[Strings['UnitsOptions']][Strings['LengthUnit']] == int
			MF_CHECKED
		    else
			MF_UNCHECKED
		    end
		end
	    when 5,6,7,8
		result = int - 5
		if Sketchup.active_model.options[Strings['UnitsOptions']][Strings['LengthFormat']] == result
		    MF_CHECKED
		else
		    MF_UNCHECKED
		end
	    end
	end

	def toggle_Length_Precision(int)
	    model = Sketchup.active_model
	    return if not [1,-1].index(int)
	    lp = model.options[Strings["UnitsOptions"]][Strings["LengthPrecision"]]
	    if 0 <= (lp + int) && (lp + int) <= 10
		model.options[Strings["UnitsOptions"]][Strings["LengthPrecision"]] = lp + int
		puts "Drawing precision = " + (lp + int).to_s
		f = "Units Precision: %.#{lp+int}f (#{lp+int})"
		str = f % [0]
		Sketchup.status_text = str
	    end
	    model.active_view.invalidate
	    UI.refresh_inspectors
	end

	def length_precision_validation_proc_upper
	    lp = Sketchup.active_model.options[Strings["UnitsOptions"]][Strings["LengthPrecision"]]
	    if lp  >= 10 
		MF_GRAYED
	    end
	end

	def length_precision_validation_proc_lower
	    lp = Sketchup.active_model.options[Strings["UnitsOptions"]][Strings["LengthPrecision"]]
	    if lp <= 0 
		MF_GRAYED
	    end
	end

	def display_unit_format
	    model = Sketchup.active_model
	    if model.options[Strings["UnitsOptions"]][Strings["SuppressUnitsDisplay"]] == true
		model.options[Strings["UnitsOptions"]][Strings["SuppressUnitsDisplay"]]= false
	    else
		model.options[Strings["UnitsOptions"]][Strings["SuppressUnitsDisplay"]]= true
	    end
	    model.active_view.invalidate
	    UI.refresh_inspectors
	end
	def display_unit_format_validation_proc
	    if Sketchup.active_model.options[Strings["UnitsOptions"]][Strings["SuppressUnitsDisplay"]] == true
		MF_UNCHECKED
	    else
		MF_CHECKED
	    end
	end


	if not file_loaded?("toggle_units.rb")

	    u_menu = UI.menu("Plugins").add_submenu("Toggle units")
	    u_menu.add_item(Strings["Decimal"]) { toggle_units(5) }
	    u_menu.add_item(Strings["Architectural"]) { toggle_units(6) }
	    u_menu.add_item(Strings["Fractional"]) { toggle_units(7) }
	    u_menu.add_item(Strings["Engineering"]) { toggle_units(8) }
	    u_menu.add_item(Strings["Feet"]) { toggle_units(1) }
	    u_menu.add_item(Strings["Inch"]) { toggle_units(0) }
	    u_menu.add_item(Strings["Millimeter"]) { toggle_units(2) }
	    u_menu.add_item(Strings["Centimeter"]) { toggle_units(3) }
	    u_menu.add_item(Strings["Meter"]) { toggle_units(4) }
	    u_menu.add_separator
	    u_menu.add_item(Strings["Decrease precision"]) { toggle_Length_Precision(-1) }
	    u_menu.add_item(Strings["Increase precision"]) { toggle_Length_Precision(1) }
	    u_menu.add_separator
	    u_menu.add_item(Strings["Display format"]) {display_unit_format }

	    tb = UI::Toolbar.new("Toggle units")
	    ext = ".png"
	    path = File.join("Plugins","toggle_units_icons")

	    cmd = UI::Command.new(Strings["Decimal"]) { toggle_units(5) }
	    cmd.small_icon = Sketchup.find_support_file(("tudec_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tudec_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Decimal"
	    cmd.set_validation_proc { toggle_units_validation_proc(5) }
	    tb.add_item cmd

	    cmd = UI::Command.new(Strings["Architectural"]) { toggle_units(6) }
	    cmd.small_icon = Sketchup.find_support_file(("tuarc_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tuarc_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Architectural"
	    cmd.set_validation_proc { toggle_units_validation_proc(6) }
	    tb.add_item cmd

	    cmd = UI::Command.new(Strings["Fractionnal"]) { toggle_units(8) }
	    cmd.small_icon = Sketchup.find_support_file(("tufra_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tufra_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Fractionnal"
	    cmd.set_validation_proc { toggle_units_validation_proc(8) }
	    tb.add_item cmd

	    cmd = UI::Command.new(Strings["Engineering"]) { toggle_units(7) }
	    cmd.small_icon = Sketchup.find_support_file(("tuing_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tuing_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Ingeniery"
	    cmd.set_validation_proc { toggle_units_validation_proc(7) }
	    tb.add_item cmd

	    tb.add_separator
	    tb.add_separator

	    cmd = UI::Command.new(Strings["Inch"]) { toggle_units(0) }
	    cmd.small_icon = Sketchup.find_support_file(("tuin_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tuin_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Inch"
	    cmd.set_validation_proc { toggle_units_validation_proc(0) }
	    tb.add_item cmd

	    cmd = UI::Command.new("Feet") { toggle_units(1) }
	    cmd.small_icon = Sketchup.find_support_file(("tufe_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tufe_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Feet"
	    cmd.set_validation_proc { toggle_units_validation_proc(1) }
	    tb.add_item cmd

	    tb.add_separator

	    cmd = UI::Command.new(Strings["Millimeter"]) { toggle_units(2) }
	    cmd.small_icon = Sketchup.find_support_file(("tumm_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tumm_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Millimeter"
	    cmd.set_validation_proc { toggle_units_validation_proc(2) }
	    tb.add_item cmd

	    cmd = UI::Command.new(Strings["Centimeter"]) { toggle_units(3) }
	    cmd.small_icon = Sketchup.find_support_file(("tucm_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tucm_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Centimeter"
	    cmd.set_validation_proc { toggle_units_validation_proc(3) }
	    tb.add_item cmd

	    cmd = UI::Command.new(Strings["Meter"]) { toggle_units(4) }
	    cmd.small_icon = Sketchup.find_support_file(("tume_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tume_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = "Drawing unit -> Meter"
	    cmd.set_validation_proc { toggle_units_validation_proc(4) }
	    tb.add_item cmd

	    tb.add_separator
	    tb.add_separator

	    cmd = UI::Command.new(Strings["Decrease precision"]) { toggle_Length_Precision(-1) }
	    cmd.small_icon = Sketchup.find_support_file(("tumoins_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tumoins_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = Strings["Decrease precision"]
	    cmd.set_validation_proc { length_precision_validation_proc_lower }
	    tb.add_item cmd

	    cmd = UI::Command.new(Strings["Increase precision"]) { toggle_Length_Precision(1) }
	    cmd.small_icon = Sketchup.find_support_file(("tuplus_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("tuplus_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = Strings["Increase precision"]
	    cmd.set_validation_proc { length_precision_validation_proc_upper }
	    tb.add_item cmd

	    tb.add_separator

	    cmd = UI::Command.new(Strings["Display format"]) { display_unit_format }
	    cmd.small_icon = Sketchup.find_support_file(("duf_16" + ext),path)
	    cmd.large_icon = Sketchup.find_support_file(("duf_24" + ext),path)
	    cmd.tooltip = cmd.status_bar_text = Strings["Display format"]
	    cmd.set_validation_proc { display_unit_format_validation_proc }
	    tb.add_item cmd

	    state = tb.get_last_state

	    if(state == TB_NEVER_SHOWN)
		tb.show 
	    elsif (state == TB_VISIBLE)
		tb.restore
	    end
	    file_loaded("toggle_units.rb")
	end
    end # module Matt666
end
