clipbox/src/main.py
2020-12-14 15:20:40 -08:00

98 lines
3.1 KiB
Python

# main.py
#
# Copyright 2020 Morgan McMillian
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, Gio
gi.require_version('Handy', '1')
from gi.repository import Handy
Handy.init()
class Application(Gtk.Application):
def __init__(self):
super().__init__(application_id='dev.thrrgilag.clipbox',
flags=Gio.ApplicationFlags.FLAGS_NONE)
def do_activate(self):
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
win = self.props.active_window
if not win:
win = Gtk.ApplicationWindow(application=self)
win.set_default_size(320, 480)
title_bar = Handy.TitleBar()
win.set_titlebar(title_bar)
header = Handy.HeaderBar(title='Clipboard', show_close_button=True)
title_bar.add(header)
vbox = Gtk.Box(orientation='vertical')
scroller = Gtk.ScrolledWindow(halign='fill')
textarea = Gtk.TextView(
left_margin=8,
right_margin=8,
top_margin=8,
bottom_margin=8
)
textarea.set_wrap_mode(Gtk.WrapMode.WORD)
scroller.add(textarea)
self.buffer = textarea.get_buffer()
actionbar = Gtk.ActionBar()
paste_button = Gtk.Button.new_with_label("Paste Text")
copy_button = Gtk.Button.new_with_label("Copy Text")
clear_button = Gtk.Button.new_with_label("Clear")
actionbar.pack_end(copy_button)
actionbar.pack_end(paste_button)
actionbar.pack_end(clear_button)
paste_button.connect('clicked', self.paste_text)
copy_button.connect('clicked', self.copy_text)
clear_button.connect('clicked', self.clear_text)
vbox.pack_start(scroller, True, True, 0)
vbox.pack_end(actionbar, False, False, 0)
win.add(vbox)
win.show_all()
win.present()
def copy_text(self, widget):
start = self.buffer.get_start_iter()
end = self.buffer.get_end_iter()
text = self.buffer.get_text(start, end, False)
self.clipboard.set_text(text, -1)
def paste_text(self, widget):
text = self.clipboard.wait_for_text()
if text is not None:
self.buffer.set_text(text, -1)
def clear_text(self, widget):
start = self.buffer.get_start_iter()
end = self.buffer.get_end_iter()
self.buffer.delete(start, end)
def main(version):
app = Application()
return app.run(sys.argv)