indexing
	description	: "Main window for this WEL Application"
	author		: "Generated by the New WEL Application Wizard"
	date		: "$Date: 2004/05/05 23:31:53 $"
	revision	: "$Revision: 1.14 $"

class
	MAIN_WINDOW

inherit
	WEL_FRAME_WINDOW
		redefine
			class_icon,
			on_menu_command,
			on_control_id_command,
			on_accelerator_command,
			on_size,
			class_background,
			on_notify,
			closeable,
			on_menu_select
		end

	APPLICATION_IDS
		export
			{NONE} all
		end

	WEL_COLOR_CONSTANTS
		export
			{NONE} all
		end

	WEL_ID_CONSTANTS
		export
			{NONE} all
		end

	WEL_TTN_CONSTANTS
		export
			{NONE} all
		end

	WEL_LIST_VIEW_CONSTANTS
		export
			{NONE} all
		end

create
	make

feature {NONE} -- Initialization

	make is
			-- Make the main window
		do
			make_top ("Battle.Net Server Editor")
			create bnet_list.make_with_values(1001, 0)
			tool_bar := build_toolbar
			data_saved := true;

			-- Serverliste Aufbauen
			create list.make(Current, 120, 25, 100, 82, -1)

			-- Spiele liste
			create game.make(Current, 2, 25, 116, 82, -1)

			set_menu(main_menu)
			
			-- Create a status window
			create status_window.make (Current, -1)
			status_window.set_multiple_mode
			status_window.set_parts (<<300, 350, -1>>)
			
			-- Dis-/Enable Einträge
			main_menu.enable_item (Idm_server_deleteall)
			set_availability (Idm_edit_options, false)
			set_availability (Idm_file_set, not data_saved)
	
			if game_id < Idb_starcraft then
				-- Disable einträge
				set_availability (Idm_file_get, false)
				set_availability (Idm_file_import, false)
				set_availability (Idm_file_export, false)
				set_availability (Idm_server_add, false)
				set_availability (Idm_server_edit, false)
				set_availability (Idm_server_delete, false)
				set_availability (Idm_server_reset, false)
				game_id := -1
			end
			resize(465, 320)			
		ensure
			list_exists: list.exists
			game_exists: game.exists
			menu_exists: main_menu.exists
			tool_bar: tool_bar.exists
		end

feature {NONE} -- Messages

	on_size (size_type, a_width, a_height: INTEGER) is
			-- Resizen vom Fensterinhalt
		do
			--Statuswindow
			if status_window /= Void then
				status_window.reposition
			end

			-- Serverlisten größe
			list.resize(a_width-123, a_height-50)
			-- Server-Column breite
			list.set_column_width (110,0)
			list.set_column_width (21,1)
			list.set_column_width (39,2)
			if list.width > 150 then
				list.set_column_width (list.width-170, 3)
			else
				list.set_column_width (0, 3)
			end
			
			-- Gameliste
			game.set_height (a_height-50)
			-- Gamecolumn
			game.set_column_width (116, 0)
		end

	on_menu_command (menu_id: INTEGER) is
			-- Handle Menu Events
		local
			msg_box: WEL_MSG_BOX
			server_prop: SERVERPROP_DIALOG
			about: ABOUT_DIALOG
		do
			create msg_box.make
			
			inspect
				menu_id
			when Idm_file_get then
				if is_selected then get_bnet_server(game_id) end
			when Idm_file_set then
				if is_selected then set_bnet_server(bnet_list) end
			when Idm_file_import then
				open_file_dialog.activate (Current)
				if open_file_dialog.selected then
					if get_servers_from_file(open_file_dialog.file_name) then
						list.insert_server(bnet_list)
						data_saved := false
					else
						status_window.set_text_part (0, "Error with Importfile!")
					end
				end
			when Idm_file_export then
				save_file_dialog.set_file_name("Export")
				save_file_dialog.activate (Current)
				if save_file_dialog.selected then
					inspect
						save_file_dialog.filter_index	
					when 2 then
						set_servers_to_reg_file(save_file_dialog.file_name)						
					else
						set_servers_to_file(save_file_dialog.file_name)												
					end
				end
			when Idm_file_exit then
				if closeable then
					destroy
				end
			when Idm_edit_options then
				msg_box.warning_message_box (Current, "Idm_edit_options", "Not Implemented!")
			when Idm_server_default then
				if list.selected_count > 0 then
					bnet_list.set_default (list.selected_item + 1)
					list.insert_server(bnet_list)
					data_saved := false;
				end
			when Idm_server_add then
				if game_id >= Idb_starcraft then
					create server_prop.make_by_id(Current, Idd_serverprop)
					server_prop.activate
					if server_prop.ok_pushed then
						bnet_list.extend(server_prop.get_server)
						list.insert_server(bnet_list)
						data_saved := false;
					end
				end
			when Idm_server_edit then
				if list.selected_count > 0 then
					create server_prop.make_by_id(Current, Idd_serverprop)
					server_prop.set_server(bnet_list @ (list.selected_item + 1))
					server_prop.activate
					if server_prop.ok_pushed then
						bnet_list.go_i_th(list.selected_item + 1)
						bnet_list.replace(server_prop.get_server)
						list.insert_server(bnet_list)
						data_saved := false;
					end
				end
			when Idm_server_delete then
				if list.selected_count > 0 then
					bnet_list.go_i_th(list.selected_item + 1)
					bnet_list.remove
					list.insert_server(bnet_list)
					data_saved := false;
				end
			when Idm_server_deleteall then
				bnet_list.wipe_out
				if is_selected then list.insert_server(bnet_list) end
				data_saved := false;
			when Idm_server_reset then
				-- liste leeren
				bnet_list.wipe_out
				-- liste mit standartservern füllen
				set_standard_server
				
				if is_selected then list.insert_server(bnet_list) end
				data_saved := false;
			when Idm_help_about then
				create about.make_by_id (Current, Idd_about)
				about.activate
			else
				msg_box.error_message_box (Current, "Unknown control/menu Id!", "ERROR")
			end
			set_availability(Idm_file_set, not data_saved)
		end

	on_menu_select (menu_item: INTEGER; flags: INTEGER; a_menu: WEL_MENU) is
			-- Display a message in the status window corresponding
			-- to the selected menu_item.
		do
			status_window.set_text_part (0,
				resource_string_id (menu_item))
		end

	on_control_id_command (control_id: INTEGER) is
			-- Perform the corresponding menu command
		do
			on_menu_command (control_id)
		end

	on_accelerator_command (accelerator_id: INTEGER) is
			-- Perform the corresponding menu command
		do
			on_menu_command (accelerator_id)
		end

	on_notify (control_id: INTEGER; info: WEL_NMHDR) is
			-- Draw the tooltips -> Toolbar.
		local
			tt: WEL_TOOLTIP_TEXT
		do
			if info.code = Ttn_needtext then
				create tt.make_by_nmhdr (info)
				-- Set resource string id.
				tt.set_text_id (tt.hdr.id_from)
			end
		end

feature {CHECK_SERVER} -- Messages

	send_command (control_id: INTEGER) is
			-- Perform the corresponding menu command
		do
			on_menu_command (control_id)
		end

feature {CHECK_GAMES} -- Access

	get_bnet_server(prog: INTEGER) is
			-- Get Server from Registry
		require
			prog_existiert: prog > 0
		local
			reg_key		: MY_WEL_REGISTRY
			reg_value	: MY_WEL_REGISTRY_KEY_VALUE
			reg_val		: LINKED_LIST[STRING]
		do
			game_id := prog
			create reg_key

			-- Einstellungen holen
			inspect
				game_id
			when Idb_starcraft then
				reg_value ?= reg_key.open_key_value("HKEY_CURRENT_USER\SOFTWARE\BATTLE.NET\CONFIGURATION", "Battle.net gateways")
			when Idb_diablo then
				reg_value ?= reg_key.open_key_value("HKEY_CURRENT_USER\SOFTWARE\BATTLE.NET\CONFIGURATION", "Battle.net gateways")
			when Idb_diablo2 then
				reg_value ?= reg_key.open_key_value("HKEY_CURRENT_USER\SOFTWARE\BATTLE.NET\CONFIGURATION", "Diablo II Battle.net gateways")
			when Idb_w2bne then
				reg_value ?= reg_key.open_key_value("HKEY_CURRENT_USER\SOFTWARE\BATTLE.NET\CONFIGURATION", "Battle.net gateways")
			when Idb_war3 then
				reg_value ?= reg_key.open_key_value("HKEY_CURRENT_USER\SOFTWARE\Blizzard Entertainment\Warcraft III", "Battle.net gateways")
			end
			
			if reg_value /= Void then
				reg_val := reg_value.value
				
				bnet_list.wipe_out
				if not bnet_list.parse_bnet_string(reg_val, game_id) then
					--set_standard_server
				end
				list.insert_server(bnet_list)
				data_saved := true
				set_availability(Idm_file_set, not data_saved)
			else
				bnet_list.wipe_out
				set_standard_server
				list.insert_server(bnet_list)
				data_saved := false
				set_availability(Idm_file_set, not data_saved)
			end
		end

	closeable: BOOLEAN is
		local
			msg_box: WEL_MSG_BOX
		do
			Result := false
			if not data_saved then
				create msg_box.make
				msg_box.question_message_box (Current, "Save changes?", "Warning")
				if msg_box.message_box_result = Idyes then
					set_bnet_server(bnet_list)
					data_saved := true
				end
			end
			Result := true
		end

feature {NONE} -- File I/O

	get_servers_from_file(datei: STRING):BOOLEAN is
			-- Lädt Server aus `datei'
		require
			datei_nicht_leer: datei /= ""
			datei_existiert: datei /= Void
		local
			d: PLAIN_TEXT_FILE
			liste: LINKED_LIST[STRING]
			temp: STRING
		do
			create d.make_open_read(datei)
			create liste.make
			create temp.make_empty
			
			-- Datei in String Liste
			if not d.is_empty then
				from
					temp.wipe_out
					d.start	
					d.read_character
				until
					d.end_of_file or temp.count > 100
				loop
					if d.last_character.code = 0 then
						liste.extend(deep_clone(temp))
						temp.wipe_out
					else
						temp.extend(d.last_character)
					end
					d.read_character
				end
			end
			d.close
			
			-- Stringliste in Bnet_server_list
			Result := bnet_list.parse_bnet_string(liste, game_id)
		end
		
	set_servers_to_file(datei: STRING) is
			-- Speichert Server in `datei'
		require
			datei_nicht_leer: datei /= ""
			datei_existiert: datei /= Void
		local
			d: PLAIN_TEXT_FILE
		do
			create d.make_create_read_write(datei)
			
			d.put_string(bnet_list.to_bnet_string)
			d.close
		end

	set_servers_to_reg_file(datei:STRING) is
			-- Speichert Server in `datei' als *.reg-Fortmat Datei
		require
			datei_nicht_leer: datei /= ""
			datei_existiert: datei /= Void
		local
			d: PLAIN_TEXT_FILE
			temp, data, zeile: STRING
			data_index: INTEGER
		do
			create d.make_create_read_write(datei)
			create zeile.make_empty
			
			-- Reg start
			d.put_string("Windows Registry Editor Version 5.00")
			d.put_new_line
			d.put_new_line
			d.put_string("[")			
			
			-- Reg path
			inspect
				game_id
			when Idb_starcraft then
				d.put_string("HKEY_CURRENT_USER\SOFTWARE\BATTLE.NET\CONFIGURATION]")
				d.put_new_line
				zeile.append("%"Battle.net gateways%"=hex(7):")
			when Idb_diablo then
				d.put_string("HKEY_CURRENT_USER\SOFTWARE\BATTLE.NET\CONFIGURATION]")
				d.put_new_line
				zeile.append("%"Battle.net gateways%"=hex(7):")
			when Idb_diablo2 then
				d.put_string("HKEY_CURRENT_USER\SOFTWARE\BATTLE.NET\CONFIGURATION]")
				d.put_new_line
				zeile.append("%"Diablo II Battle.net gateways%"=hex(7):")
			when Idb_w2bne then
				d.put_string("HKEY_CURRENT_USER\SOFTWARE\BATTLE.NET\CONFIGURATION]")
				d.put_new_line
				zeile.append("%"Battle.net gateways%"=hex(7):")
			when Idb_war3 then
				d.put_string("HKEY_CURRENT_USER\SOFTWARE\Blizzard Entertainment\Warcraft III]")
				d.put_new_line
				zeile.append("%"Battle.net gateways%"=hex(7):")
			end

			-- Reg data
			from
				data_index := 1
				data := bnet_list.to_bnet_string
			invariant
				data.count - data_index + 1 >= 0
			until
				data_index > data.count
			loop
				from
				invariant
					data.count - data_index + 1 >= 0
				until
					zeile.count > 75 or data_index > data.count
				loop
					temp := (data @ data_index).code.to_hex_string
					temp.keep_tail(2)
					zeile.append(temp)
					zeile.append(",00,")
					data_index := data_index + 1 
				end
				if not (data_index > data.count) then
					zeile.append("\")
				else
					zeile.remove_tail(1)
				end
				d.put_string(zeile)
				d.put_new_line
				zeile.wipe_out
			end
			
			-- Reg end
			d.put_new_line
			d.close
		end

	open_file_dialog: WEL_OPEN_FILE_DIALOG is
			-- Dialog box to open a file.
		local
			ofn: WEL_OFN_CONSTANTS
		once
			create ofn
			create Result.make
			Result.set_default_extension ("bnet_se")
			Result.set_filter (<<"BNet_SE file (*.bnet_se)", "All files (*.*)">>,
								<<"*.bnet_se", "*.*">>)
			Result.add_flag (ofn.Ofn_filemustexist)
			Result.set_title("Import Server from File")
		ensure
			result_not_void: Result /= Void
		end

	save_file_dialog: WEL_SAVE_FILE_DIALOG is
			-- Dialog box to save a file.
		once
			create Result.make
			Result.set_default_extension ("bnet_se")
			Result.set_filter (<<"BNet_SE file (*.bnet_se)", "Registry file (*.reg)", "All files (*.*)">>,
								<<"*.bnet_se", "*.reg", "*.*">>)
			Result.set_title("Export Server to File")
		ensure
			result_not_void: Result /= Void
		end

feature {CHECK_GAMES, CHECK_SERVER} -- Implementation

	set_availability(an_id: INTEGER; available: BOOLEAN) is
			-- Setzt einträge in Menu und Toolbar auf en-/disabled
		require
			id_gt_null: an_id > 0
		do
			if available then
				main_menu.enable_item (an_id)
				if not (an_id = Idm_server_default) then tool_bar.enable_button (an_id) end
			else
				main_menu.disable_item (an_id)
				if not (an_id = Idm_server_default) then tool_bar.disable_button (an_id) end
			end

			-- Modified flag in Statuswindow
			if status_window /= Void then
				if data_saved then
					status_window.set_text_part (1,	"")
				else
					status_window.set_text_part (1,	"Modified")
				end
			end
		end		
		
	set_bnet_server(bnl: BNET_SERVER_LIST) is
			-- Set Server to Registry
		require
			bnl_not_void: bnl /= Void
		local
			new_sl: WEL_REGISTRY_KEY_VALUE
			c: WEL_REGISTRY_KEY_VALUE_TYPE
			c2: WEL_REGISTRY_ACCESS_MODE
			new_reg: WEL_REGISTRY
			path: POINTER
			mesgbx: WEL_MSG_BOX
		do
			create c
			create c2
			create new_reg

			if game_id = Idb_war3 then
				bnet_list.set_default(bnet_list.get_default - 1)
				path := new_reg.open_key_with_access("HKEY_CURRENT_USER\SOFTWARE\Blizzard Entertainment\Warcraft III", c2.key_set_value)				
			else
				path := new_reg.open_key_with_access("HKEY_CURRENT_USER\SOFTWARE\Battle.net\Configuration", c2.key_set_value)
			end
			
			create new_sl.make(c.Reg_multi_sz, bnet_list.to_bnet_string)
			if new_reg.last_call_successful then
				if game_id = Idb_diablo2 then
					new_reg.set_key_value(path, "Diablo II Battle.net gateways", new_sl)
				else
					new_reg.set_key_value(path, "Battle.net gateways", new_sl)
				end
				if game_id = Idb_war3 then
					bnet_list.set_default(bnet_list.get_default + 1)
				end
			data_saved := true
			set_availability(Idm_file_set, not data_saved)
			else	
				create mesgbx.make
				mesgbx.error_message_box (Current, "No write-access to Registrykey.", "No Access")
			end
		end

feature {NONE} -- Implementation

	set_standard_server is
			-- liste mit standartservern füllen
		do
			if game_id = Idb_war3 then
				bnet_list.set_version(1001)
				bnet_list.set_default(4)
				bnet_list.append_server("Lordaeron (U.S. West)", 8, "uswest.battle.net")
				bnet_list.append_server("Azeroth (U.S. East)", 6, "useast.battle.net")
				bnet_list.append_server("Kalimdor (Asia)", -9, "asia.battle.net")
				bnet_list.append_server("Northrend (Europe)", -1, "europe.battle.net")
			else
				bnet_list.set_version(1001)
				bnet_list.set_default(4)
				bnet_list.append_server("U.S. West", 8, "uswest.battle.net")
				bnet_list.append_server("U.S. East", 6, "useast.battle.net")
				bnet_list.append_server("Asia", -9, "asia.battle.net")
				bnet_list.append_server("Europe", -1, "europe.battle.net")					
			end
		end

	is_selected: BOOLEAN is
			-- Game selected?
		do
			Result := game_id > -1
		end

	game_id	:INTEGER

	list: CHECK_SERVER

	game: CHECK_GAMES

	data_saved: BOOLEAN

	bnet_list: BNET_SERVER_LIST
	
	status_window: WEL_STATUS_WINDOW

feature {NONE} -- Initial implementation

	class_background: WEL_BRUSH is
			-- White background
		once
			create Result.make_by_sys_color (Color_btnface + 1)
		end

	class_icon: WEL_ICON is
			-- Window's icon
		once
			create Result.make_by_id (Idr_mainframe)
		end

	main_menu: WEL_MENU is
			-- Menü
		once
			create Result.make_by_id (Idm_mainframe)
		ensure
			result_not_void: Result /= Void
			result_exists: Result.exists
		end

	tool_bar: WEL_IMAGELIST_TOOL_BAR
			-- Window's tool bar

	build_toolbar: WEL_IMAGELIST_TOOL_BAR is
			-- erzeugt Toolbar des Fensters
		local
			tool_bar_bitmap: WEL_TOOL_BAR_BITMAP
			tool_bar_button1, tool_bar_button2,	tool_bar_button3,
			tool_bar_button4, tool_bar_button5,	tool_bar_button6,
			tool_bar_button7, tool_bar_button8,	tool_bar_button9,
			tool_bar_button10, tool_bar_button11: WEL_TOOL_BAR_BUTTON
			bitmap_index: INTEGER
		once
			-- Create Toolbar and Bitmaps
			create Result.make(Current, -1)
			create tool_bar_bitmap.make (Idb_toolbar)

			Result.add_bitmaps (tool_bar_bitmap, 1)
			bitmap_index := Result.last_bitmap_index
			
			-- Create and Add Buttons
			create tool_bar_button1.make_button (bitmap_index, Idm_file_get)
			create tool_bar_button2.make_button (bitmap_index + 1, Idm_file_set)
			create tool_bar_button3.make_separator
			create tool_bar_button4.make_button (bitmap_index + 2, Idm_server_add)
			create tool_bar_button5.make_button (bitmap_index + 3, Idm_server_delete)
			create tool_bar_button6.make_button (bitmap_index + 4, Idm_server_edit)
			create tool_bar_button7.make_button (bitmap_index + 5, Idm_server_reset)
			create tool_bar_button8.make_separator
			create tool_bar_button9.make_button (bitmap_index + 6, Idm_edit_options)
			create tool_bar_button10.make_separator
			create tool_bar_button11.make_button (bitmap_index + 7, Idm_help_about)

			Result.add_buttons (<<
				tool_bar_button1,
				tool_bar_button2,
				tool_bar_button3,
				tool_bar_button4,
				tool_bar_button5,
				tool_bar_button6,
				tool_bar_button7,
				tool_bar_button8,
				tool_bar_button9,
				tool_bar_button10,
				tool_bar_button11>>)
		ensure
			toolbar_not_void: Result /= Void
			toolbar_exists: Result.exists
		end
		
end -- class MAIN_WINDOW
