Files
YMS/yms/gtkutils.py
T

126 lines
4.1 KiB
Python

# SPDX-License-Identifier: WTFPL
from gi.repository import Gtk
def init_named_controls(window, builder):
ctrltype = {
'lbl': 'label',
'ent': 'entry',
'but': 'button',
'cmb': 'combo',
'tvw': 'tree',
'spi': 'spin',
'txt': 'text',
'chk': 'check',
'opt': 'radio',
'vbx': 'vbox',
'hbx': 'hbox',
'box': 'box',
'tbl': 'table',
'mnu': 'menu',
'img': 'image',
'drw': 'area',
'stk': 'stack'
}
for ctrl in ctrltype.values():
setattr(window, ctrl, {})
for widget in builder.get_objects():
if type(widget) in (Gtk.Adjustment, Gtk.CellRendererText, Gtk.TreeSelection):
continue
widgetname = Gtk.Buildable.get_name(widget)
nameparts = widgetname.partition('_')
if (not nameparts[0] in ctrltype.keys()) or (not nameparts[2]):
continue
var = getattr(window, ctrltype[nameparts[0]])
var[nameparts[2]] = widget
def messagebox(messagetext, symbol=None, title=None, parent=None):
if not symbol:
symbol = Gtk.MessageType.INFO
if parent:
flags = Gtk.DialogFlags.DESTROY_WITH_PARENT
else:
flags = 0
md = Gtk.MessageDialog(parent=parent, flags=flags, message_type=symbol, buttons=Gtk.ButtonsType.CLOSE)
md.set_markup(messagetext)
if title:
md.set_title(title)
if not parent:
md.set_position(Gtk.WindowPosition.CENTER)
md.run()
md.destroy()
def questionbox(messagetext, symbol=None, title=None, parent=None):
"""
The decision is returned as a value: True if agreed to, False if rejected
"""
if not symbol:
symbol = Gtk.MessageType.QUESTION
if parent:
flags = Gtk.DialogFlags.DESTROY_WITH_PARENT
else:
flags = 0
md = Gtk.MessageDialog(parent=parent, flags=flags, message_type=symbol, buttons=Gtk.ButtonsType.YES_NO)
md.set_markup(messagetext)
if title:
md.set_title(title)
if not parent:
md.set_position(Gtk.WindowPosition.CENTER)
response = md.run()
md.destroy()
return response == Gtk.ResponseType.YES
def get_text_contents(textview):
"""
Convenience function to get all contents from GtkTextView
"""
buf = textview.get_buffer()
return buf.get_text(buf.get_start_iter(), buf.get_end_iter(), True)
class TouchPopup():
"""
A pop-up window that can be closed again with another click.
The pop-up features a title in bold, as well as a list of labels and values.
"""
#TODO finish converting to GTK3
def __init__(self, parent, title, data, width=None):
self.window = win = gtk.Window(Gtk.WindowType.POPUP)
win.set_position(Gtk.WindowPosition.MOUSE)
win.set_transient_for(parent)
win.set_modal(True)
if width:
win.set_size_request(width, -1)
#win.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("#feffd0"))
win.override_background_color(
Gtk.StateFlags.NORMAL,
Gdk.RGBA(1.0, 1.0, 0.816, 1.0) # #feffd0
)
frame = Gtk.Frame()
frame.set_shadow_type(Gtk.ShadowType.ETCHED_OUT)
frame.set_border_width(2)
win.add(frame)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
frame.add(vbox)
# Title
label = Gtk.Label("<b>" + title + "</b>")
label.set_use_markup(True)
label.set_alignment(0.0, 0.5)
vbox.pack_start(label)
# Data
sg = Gtk.SizeGroup(Gtk.SizeGroupMode.HORIZONTAL)
for d in data:
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
vbox.pack_start(hbox)
label = Gtk.Label(d[0])
label.set_alignment(0.0, 0.5)
hbox.pack_start(label, False, False, 4)
sg.add_widget(label)
label = Gtk.Label(d[1])
label.set_alignment(0.0, 0.5)
hbox.pack_start(label)
win.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
win.connect('button-press-event', self.callback)
win.show_all()
def callback(self, window, event):
window.destroy()