Heute habe ich mir von ChatGPT einen einfachen Texteditor mit Ruby und GTK3 programmieren lassen:
Das hat überraschend gut funktioniert.
Auf meinem Debian-System musste ich noch das Paket ruby-gtk3 installieren.
Hier ist der Code:
Code
require 'gtk3'
def cut_text
clipboard = Gtk::Clipboard.get(Gdk::SELECTION_CLIPBOARD)
text = @text_buffer.get_text(@text_buffer.get_selection_bounds[0], @text_buffer.get_selection_bounds[1], true)
clipboard.text = text
@text_buffer.delete_selection(true, true)
end
def copy_text
clipboard = Gtk::Clipboard.get(Gdk::SELECTION_CLIPBOARD)
text = @text_buffer.get_text(@text_buffer.get_selection_bounds[0], @text_buffer.get_selection_bounds[1], true)
clipboard.text = text
end
def paste_text
clipboard = Gtk::Clipboard.get(Gdk::SELECTION_CLIPBOARD)
text = clipboard.wait_for_text
@text_buffer.insert_clipboard(clipboard, nil, true)
end
def select_all_text
@text_buffer.select_range(@text_buffer.start_iter, @text_buffer.end_iter)
end
def open_file
dialog = Gtk::FileChooserDialog.new(
title: 'Datei öffnen',
action: :open,
buttons: [
[Gtk::Stock::CANCEL, :cancel],
[Gtk::Stock::OPEN, :accept]
]
)
filter = Gtk::FileFilter.new
filter.set_name('Textdateien')
filter.add_pattern('*.txt')
dialog.add_filter(filter)
if dialog.run == :accept
filename = dialog.filename
text = File.read(filename)
@text_buffer.text = text
# Setze den Dateinamen als Titel
@window.set_title(File.basename(filename))
end
dialog.destroy
end
def save_file
dialog = Gtk::FileChooserDialog.new(
title: 'Datei speichern',
action: :save,
buttons: [
[Gtk::Stock::CANCEL, :cancel],
[Gtk::Stock::SAVE, :accept]
]
)
filter = Gtk::FileFilter.new
filter.set_name('Textdateien')
filter.add_pattern('*.txt')
dialog.add_filter(filter)
if dialog.run == :accept
filename = dialog.filename
text = @text_buffer.text
File.write(filename, text)
# Setze den Dateinamen als Titel
@window.set_title(File.basename(filename))
end
dialog.destroy
end
def new_file
@text_buffer.text = ''
@window.set_title('Unbenannt')
end
def update_word_count
text = @text_buffer.text
words = text.split(/\s+/).count
@word_count_label.text = "#{words} Wörter"
end
def set_font_size(text_view, font_size)
font_desc = Pango::FontDescription.new("Georgia #{font_size}")
text_view.override_font(font_desc)
end
def print_text
text = @text_buffer.text
IO.popen('lp', 'w') do |io|
io.puts text
end
end
def set_text_color(text_view)
text_view.override_color(Gtk::StateFlags::NORMAL, Gdk::RGBA.new(0.071, 0.349, 0.569, 1))
end
def highlight_word(word)
start_iter = @text_buffer.start_iter
match_start = start_iter.forward_search(word, Gtk::TextIter::SEARCH_TEXT_ONLY, nil)
while match_start
match_end = match_start[1]
@text_buffer.apply_tag('bold', match_start[0], match_end)
start_iter = match_end
match_start = start_iter.forward_search(word, Gtk::TextIter::SEARCH_TEXT_ONLY, nil)
end
end
def remove_highlight
start_iter = @text_buffer.start_iter
end_iter = @text_buffer.end_iter
@text_buffer.remove_tag_by_name('bold', start_iter, end_iter)
end
app = Gtk::Application.new('org.gtk.example', :flags_none)
app.signal_connect 'activate' do |app|
@window = Gtk::ApplicationWindow.new(app)
@window.set_title 'Texteditor'
@window.set_default_size(1200, 900)
@window.signal_connect('key-press-event') do |_, event|
case [event.state, event.keyval]
when [[Gdk::ModifierType::CONTROL_MASK], Gdk::Keyval::GDK_KEY_c]
copy_text
when [[Gdk::ModifierType::CONTROL_MASK], Gdk::Keyval::GDK_KEY_v]
paste_text
when [[Gdk::ModifierType::CONTROL_MASK], Gdk::Keyval::GDK_KEY_a]
select_all_text
when [[Gdk::ModifierType::CONTROL_MASK], Gdk::Keyval::GDK_KEY_x]
cut_text
end
end
header_bar = Gtk::HeaderBar.new
header_bar.show_close_button = true
header_bar.title = '[ TEXT-WUNDER ]'
new_file_button = Gtk::Button.new(label: 'Neue Datei')
new_file_button.signal_connect('clicked') { new_file }
header_bar.pack_start(new_file_button)
open_file_button = Gtk::Button.new(label: 'Datei öffnen')
open_file_button.signal_connect('clicked') { open_file }
header_bar.pack_start(open_file_button)
save_file_button = Gtk::Button.new(label: 'Datei speichern')
save_file_button.signal_connect('clicked') { save_file }
header_bar.pack_start(save_file_button)
print_button = Gtk::Button.new(label: 'Drucken')
print_button.signal_connect('clicked') { print_text }
header_bar.pack_start(print_button)
@window.titlebar = header_bar
text_view = Gtk::TextView.new
text_view.set_wrap_mode(Gtk::WrapMode::WORD)
set_font_size(text_view, 17)
set_text_color(text_view)
@text_buffer = text_view.buffer
scrolled_window = Gtk::ScrolledWindow.new
scrolled_window.set_policy(:automatic, :automatic)
scrolled_window.add(text_view)
box = Gtk::Box.new(:vertical, 10)
box.margin = 10
box.pack_start(scrolled_window, expand: true, fill: true, padding: 10)
search_box = Gtk::Box.new(:horizontal, 5)
search_entry = Gtk::Entry.new
search_entry.set_placeholder_text('Suche...')
search_box.pack_start(search_entry, expand: true, fill: true, padding: 5)
search_button = Gtk::Button.new(label: 'Suchen')
search_button.signal_connect('clicked') do
remove_highlight
word_to_find = search_entry.text
highlight_word(word_to_find)
end
search_box.pack_start(search_button, expand: false, fill: false, padding: 5)
bold_tag = @text_buffer.create_tag('bold', 'weight' => Pango::Weight::BOLD)
box.pack_start(search_box, expand: false, fill: false, padding: 5)
@window.add(box)
@word_count_label = Gtk::Label.new
update_word_count
box.pack_start(@word_count_label, expand: false, fill: false, padding: 5)
text_view.buffer.signal_connect('changed') { update_word_count }
@window.show_all
end
app.run
Display More