view src/luan/modules/editor/window.luan @ 1905:f7649b3ebd22

use JTextField for Label
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 15 Apr 2025 16:53:03 -0600
parents ad1dc9f103b7
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local Math = require "luan:Math.luan"
local min = Math.min or error()
local String = require "luan:String.luan"
local sub_string = String.sub or error()
local replace = String.replace or error()
local starts_with = String.starts_with or error()
local new_text_area = require("luan:swing/Text_area.luan").new or error()
local new_frame = require("luan:swing/Frame.luan").new or error()
local new_panel = require("luan:swing/Component.luan").new_panel or error()
local Layout = require "luan:swing/Layout.luan"
local new_mig_layout = Layout.new_mig_layout or error()
local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error()
local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error()
local int_to_color = require("luan:swing/Color.luan").int_to_color or error()
local Border = require "luan:swing/Border.luan"
local create_empty_border = Border.create_empty_border or error()
local new_label = require("luan:swing/Label.luan").new or error()
local make_find_panel = require "luan:editor/find.luan"
local add_menu_bar = require "luan:editor/menu.luan"


local n_windows = 0
local documents = {}

local function new_window(file)
	local window = {}
	window.has_file = file~=nil and file.is_file()
	local text_area = new_text_area{
		wrap_style_word = true
		line_wrap = true
		tab_size = 4
		font = { family="Monospaced", size=13 }
	}
	window.text_area = text_area
	local title = file and file.canonical().to_string() or "new"
	if file ~= nil then
		local document = documents[title]
		if document == nil then
			documents[title] = text_area.document
		else
			text_area.document = document
		end
		if file.is_file() then
			text_area.text = file.read_text()
			text_area.document.clear_unedited()
		end
	end
	text_area.set_selection(0)
	local status_bar = new_label{
		constraints = "span,growx"
		text = " "
		border = create_empty_border(2,16,4,16)
	}
	window.status_bar = status_bar
	local find_panel = make_find_panel(window)
	local frame = new_frame{
		preferred_size = { width=700, height=700 }
		content_pane = new_panel{
			layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]")
			children = {
				new_scroll_pane{
					constraints = "grow"
					view = text_area
					row_header_view = new_text_area_line_numbers{
						text_area = text_area
						foreground_color = int_to_color(0x888888)
						border = create_empty_border(0,8,0,8)
					}
				}
				find_panel
				status_bar
			}
		}
	}
	window.frame = frame
	frame.add_close_listener(function()
		n_windows = n_windows - 1
		if n_windows == 0 then
			Luan.exit()
		end
	end)
	local function set_title()
		local s = title
		if not text_area.document.is_unedited() then
			s = s.." *"
		end
		frame.title = s
	end
	set_title()
	window.set_title = set_title  -- dont gc
	text_area.document.add_undo_listener(set_title)
	window.new = new_window
	function window.open()
		local file_chooser = frame.file_chooser_load()
		if file ~= nil then
			file_chooser.directory = file.parent()
		end
		file_chooser.visible = true
		local new_file = file_chooser.file
		if new_file ~= nil then
			new_window(new_file)
		end
	end
	function window.save()
		if file == nil then
			local file_chooser = frame.file_chooser_save()
			file_chooser.visible = true
			file = file_chooser.file
			if file == nil then
				return false
			end
			title = file.canonical().to_string()
			frame.title = title
			documents[title] = text_area.document
		end
		file.write_text(text_area.text)
		text_area.document.set_unedited()
		return true
	end
	function window.revert()
		local selection = text_area.get_selection()
		local text = file.read_text()
		text_area.text = text
		text_area.set_selection(min(selection,#text+1))
		text_area.document.set_unedited()
		status_bar.text = "Reverted"
	end
	local function selection_lines()
		local start_seletion, end_selection = text_area.get_selection()
		local end_ = end_selection == start_seletion and end_selection or end_selection - 1
		local start_line = text_area.get_line_from_position(start_seletion)
		local end_line = text_area.get_line_from_position(end_)
		local start_pos = text_area.get_line_start_position(start_line)
		local end_pos = text_area.get_line_end_position(end_line)
		local text = text_area.text
		text = sub_string(text,start_pos,end_pos-2)
		return {
			text = text
			start_pos = start_pos
			length = #text
			lines = end_line - start_line + 1
			start_seletion = start_seletion
			end_selection = end_selection
		}
	end
	function window.indent()
		local r = selection_lines()
		local text = r.text
		local start_pos = r.start_pos
		text = "\t"..replace(text,"\n","\n\t")
		text_area.replace(start_pos,r.length,text)
		--logger.info(stringify{text_area.get_selection()})
		text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines )
	end
	function window.unindent()
		local r = selection_lines()
		local text = r.text
		text = "\n"..text
		local start_seletion = r.start_seletion
		if starts_with(text,"\n\t") then
			start_seletion = start_seletion - 1
		end
		local len1 = #text
		text = replace(text,"\n\t","\n")
		local len2 = #text
		local end_selection = r.end_selection - (len1 - len2)
		text = sub_string(text,2)
		text_area.replace(r.start_pos,r.length,text)
		text_area.set_selection(start_seletion,end_selection)
	end
	function window.cursor_column()
		local cursor_pos = text_area.get_selection()
		local line = text_area.get_line_from_position(cursor_pos)
		local start_line_pos = text_area.get_line_start_position(line)
		return cursor_pos - start_line_pos + 1
	end
	function window.goto(line)
		local pos = text_area.get_line_start_position(line)
		text_area.set_selection(pos)
	end
	add_menu_bar(window)
	frame.pack()
	frame.visible = true
	text_area.request_focus_in_window()
	n_windows = n_windows + 1
end

return new_window