2009-10-08 19:27:49 +00:00
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""terminal_popup_menu.py - classes necessary to provide a terminal context
|
|
|
|
menu"""
|
|
|
|
|
2022-10-31 06:56:34 +00:00
|
|
|
from gi.repository import Gtk, Gdk
|
2009-10-08 19:27:49 +00:00
|
|
|
|
2018-04-24 18:22:10 +00:00
|
|
|
from .version import APP_NAME
|
|
|
|
from .translation import _
|
|
|
|
from .terminator import Terminator
|
2020-06-17 18:11:20 +00:00
|
|
|
from .util import err, dbg, spawn_new_terminator
|
2018-04-24 18:22:10 +00:00
|
|
|
from .config import Config
|
|
|
|
from .prefseditor import PrefsEditor
|
|
|
|
from . import plugin
|
2020-05-16 21:24:50 +00:00
|
|
|
from .layoutlauncher import LayoutLauncher
|
2009-10-08 19:27:49 +00:00
|
|
|
|
|
|
|
class TerminalPopupMenu(object):
|
|
|
|
"""Class implementing the Terminal context menu"""
|
|
|
|
terminal = None
|
2010-03-11 14:30:07 +00:00
|
|
|
terminator = None
|
2010-05-15 13:30:13 +00:00
|
|
|
config = None
|
2022-10-31 06:56:34 +00:00
|
|
|
accelgrp = None
|
2009-10-08 19:27:49 +00:00
|
|
|
|
|
|
|
def __init__(self, terminal):
|
|
|
|
"""Class initialiser"""
|
|
|
|
self.terminal = terminal
|
2010-03-11 14:30:07 +00:00
|
|
|
self.terminator = Terminator()
|
2010-05-15 13:30:13 +00:00
|
|
|
self.config = Config()
|
2022-10-31 06:56:34 +00:00
|
|
|
self.accelgrp = Gtk.AccelGroup()
|
|
|
|
|
|
|
|
def get_menu_item_mask(self, maskstr):
|
|
|
|
mask = 0
|
|
|
|
maskstr = maskstr.lower()
|
|
|
|
if maskstr.find('<Shift>'.lower()) >= 0:
|
|
|
|
mask = mask | Gdk.ModifierType.SHIFT_MASK
|
|
|
|
dbg("adding mask <Shift> %s" % mask)
|
|
|
|
|
|
|
|
if maskstr.find('<Control>'.lower()) >= 0:
|
|
|
|
mask = mask | Gdk.ModifierType.CONTROL_MASK
|
|
|
|
dbg("adding mask <Control> %s" % mask)
|
|
|
|
|
|
|
|
if maskstr.find('<Alt>'.lower()) >= 0:
|
|
|
|
mask = mask | Gdk.ModifierType.MOD1_MASK
|
|
|
|
dbg("adding mask <Alt> %s" % mask)
|
|
|
|
|
|
|
|
mask = Gdk.ModifierType(mask)
|
|
|
|
dbg("menu_item_mask :%d" % mask)
|
|
|
|
return mask
|
|
|
|
|
|
|
|
def menu_item(self, menutype, actstr, menustr):
|
|
|
|
act = self.config.base.get_item('keybindings', actstr)
|
|
|
|
maskstr = act[actstr] if actstr in act else ""
|
|
|
|
mask = self.get_menu_item_mask(maskstr)
|
|
|
|
|
|
|
|
accelchar = ""
|
|
|
|
pos = menustr.lower().find("_")
|
|
|
|
if (pos >= 0 and pos+1 < len(menustr)):
|
|
|
|
accelchar = menustr.lower()[pos+1]
|
|
|
|
|
|
|
|
dbg("action from config:%s for item:%s with shortcut accelchar:(%s)"
|
|
|
|
% (maskstr, menustr, accelchar))
|
|
|
|
item = menutype.new_with_mnemonic(_(menustr))
|
|
|
|
if mask:
|
|
|
|
item.add_accelerator("activate",
|
|
|
|
self.accelgrp,
|
|
|
|
Gdk.keyval_from_name(accelchar),
|
|
|
|
mask,
|
|
|
|
Gtk.AccelFlags.VISIBLE)
|
|
|
|
return item
|
2009-10-08 19:27:49 +00:00
|
|
|
|
|
|
|
def show(self, widget, event=None):
|
|
|
|
"""Display the context menu"""
|
|
|
|
terminal = self.terminal
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
menu = Gtk.Menu()
|
2015-07-02 19:40:53 +00:00
|
|
|
self.popup_menu = menu
|
2009-10-08 19:27:49 +00:00
|
|
|
url = None
|
|
|
|
button = None
|
|
|
|
time = None
|
|
|
|
|
2010-05-15 13:30:13 +00:00
|
|
|
self.config.set_profile(terminal.get_profile())
|
|
|
|
|
2009-10-08 19:27:49 +00:00
|
|
|
if event:
|
2015-11-29 22:43:45 +00:00
|
|
|
url = terminal.vte.match_check_event(event)
|
2009-10-08 19:27:49 +00:00
|
|
|
button = event.button
|
|
|
|
time = event.time
|
2010-01-25 12:26:32 +00:00
|
|
|
else:
|
|
|
|
time = 0
|
|
|
|
button = 3
|
2009-10-08 19:27:49 +00:00
|
|
|
|
2014-09-19 14:10:43 +00:00
|
|
|
if url and url[0]:
|
2012-01-14 20:09:25 +00:00
|
|
|
dbg("URL matches id: %d" % url[1])
|
2018-04-24 18:22:10 +00:00
|
|
|
if not url[1] in list(terminal.matches.values()):
|
2012-01-14 20:09:25 +00:00
|
|
|
err("Unknown URL match id: %d" % url[1])
|
|
|
|
dbg("Available matches: %s" % terminal.matches)
|
|
|
|
|
|
|
|
nameopen = None
|
|
|
|
namecopy = None
|
2009-10-08 19:27:49 +00:00
|
|
|
if url[1] == terminal.matches['email']:
|
|
|
|
nameopen = _('_Send email to...')
|
|
|
|
namecopy = _('_Copy email address')
|
|
|
|
elif url[1] == terminal.matches['voip']:
|
|
|
|
nameopen = _('Ca_ll VoIP address')
|
|
|
|
namecopy = _('_Copy VoIP address')
|
2018-04-24 18:22:10 +00:00
|
|
|
elif url[1] in list(terminal.matches.values()):
|
2012-01-14 20:09:25 +00:00
|
|
|
# This is a plugin match
|
|
|
|
for pluginname in terminal.matches:
|
|
|
|
if terminal.matches[pluginname] == url[1]:
|
|
|
|
break
|
|
|
|
|
|
|
|
dbg("Found match ID (%d) in terminal.matches plugin %s" %
|
|
|
|
(url[1], pluginname))
|
|
|
|
registry = plugin.PluginRegistry()
|
|
|
|
registry.load_plugins()
|
|
|
|
plugins = registry.get_plugins_by_capability('url_handler')
|
|
|
|
for urlplugin in plugins:
|
|
|
|
if urlplugin.handler_name == pluginname:
|
|
|
|
dbg("Identified matching plugin: %s" %
|
|
|
|
urlplugin.handler_name)
|
|
|
|
nameopen = _(urlplugin.nameopen)
|
|
|
|
namecopy = _(urlplugin.namecopy)
|
|
|
|
break
|
|
|
|
|
|
|
|
if not nameopen:
|
2009-10-08 19:27:49 +00:00
|
|
|
nameopen = _('_Open link')
|
2012-01-14 20:09:25 +00:00
|
|
|
if not namecopy:
|
2009-10-08 19:27:49 +00:00
|
|
|
namecopy = _('_Copy address')
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
icon = Gtk.Image.new_from_stock(Gtk.STOCK_JUMP_TO,
|
|
|
|
Gtk.IconSize.MENU)
|
2014-09-19 14:10:43 +00:00
|
|
|
item = Gtk.ImageMenuItem.new_with_mnemonic(nameopen)
|
2009-10-08 19:27:49 +00:00
|
|
|
item.set_property('image', icon)
|
|
|
|
item.connect('activate', lambda x: terminal.open_url(url, True))
|
|
|
|
menu.append(item)
|
|
|
|
|
2014-09-19 14:10:43 +00:00
|
|
|
item = Gtk.MenuItem.new_with_mnemonic(namecopy)
|
2009-10-08 19:27:49 +00:00
|
|
|
item.connect('activate',
|
2014-09-19 14:10:43 +00:00
|
|
|
lambda x: terminal.clipboard.set_text(terminal.prepare_url(url), len(terminal.prepare_url(url))))
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.append(item)
|
|
|
|
|
2017-01-18 08:17:15 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
2009-10-08 19:27:49 +00:00
|
|
|
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.ImageMenuItem, 'copy', '_Copy')
|
2009-10-08 19:27:49 +00:00
|
|
|
item.connect('activate', lambda x: terminal.vte.copy_clipboard())
|
|
|
|
item.set_sensitive(terminal.vte.get_has_selection())
|
2022-10-31 06:56:34 +00:00
|
|
|
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.append(item)
|
|
|
|
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.ImageMenuItem, 'paste', '_Paste')
|
2009-10-08 19:27:49 +00:00
|
|
|
item.connect('activate', lambda x: terminal.paste_clipboard())
|
|
|
|
menu.append(item)
|
|
|
|
|
2017-01-18 08:17:15 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
2009-10-08 19:27:49 +00:00
|
|
|
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.ImageMenuItem, 'edit_window_title',
|
|
|
|
'Set _Window Title')
|
2021-08-31 00:41:27 +00:00
|
|
|
item.connect('activate', lambda x: terminal.key_edit_window_title())
|
|
|
|
menu.append(item)
|
|
|
|
|
2009-10-08 19:27:49 +00:00
|
|
|
if not terminal.is_zoomed():
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.ImageMenuItem, 'split_horiz',
|
|
|
|
'Split H_orizontally')
|
2014-09-19 14:08:08 +00:00
|
|
|
image = Gtk.Image()
|
|
|
|
image.set_from_icon_name(APP_NAME + '_horiz', Gtk.IconSize.MENU)
|
2009-10-08 19:27:49 +00:00
|
|
|
item.set_image(image)
|
|
|
|
if hasattr(item, 'set_always_show_image'):
|
|
|
|
item.set_always_show_image(True)
|
2010-03-11 14:30:07 +00:00
|
|
|
item.connect('activate', lambda x: terminal.emit('split-horiz',
|
2014-09-19 14:10:43 +00:00
|
|
|
self.terminal.get_cwd()))
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.append(item)
|
|
|
|
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.ImageMenuItem, 'split_vert',
|
|
|
|
'Split V_ertically')
|
2014-09-19 14:08:08 +00:00
|
|
|
image = Gtk.Image()
|
|
|
|
image.set_from_icon_name(APP_NAME + '_vert', Gtk.IconSize.MENU)
|
2009-10-08 19:27:49 +00:00
|
|
|
item.set_image(image)
|
|
|
|
if hasattr(item, 'set_always_show_image'):
|
|
|
|
item.set_always_show_image(True)
|
2010-03-11 14:30:07 +00:00
|
|
|
item.connect('activate', lambda x: terminal.emit('split-vert',
|
2014-09-19 14:10:43 +00:00
|
|
|
self.terminal.get_cwd()))
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.append(item)
|
|
|
|
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.MenuItem, 'new_tab', 'Open _Tab')
|
2010-04-02 15:45:32 +00:00
|
|
|
item.connect('activate', lambda x: terminal.emit('tab-new', False,
|
|
|
|
terminal))
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.append(item)
|
|
|
|
|
2010-04-02 15:45:32 +00:00
|
|
|
if self.terminator.debug_address is not None:
|
2014-09-19 14:10:43 +00:00
|
|
|
item = Gtk.MenuItem.new_with_mnemonic(_('Open _Debug Tab'))
|
2010-03-19 22:16:08 +00:00
|
|
|
item.connect('activate', lambda x:
|
2010-04-02 15:45:32 +00:00
|
|
|
terminal.emit('tab-new', True, terminal))
|
2010-03-19 22:16:08 +00:00
|
|
|
menu.append(item)
|
|
|
|
|
2017-01-18 08:17:15 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
2009-10-08 19:27:49 +00:00
|
|
|
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.ImageMenuItem, 'close_term', '_Close')
|
2011-08-22 18:58:24 +00:00
|
|
|
item.connect('activate', lambda x: terminal.close())
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.append(item)
|
|
|
|
|
2017-01-18 08:17:15 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
2009-10-08 19:27:49 +00:00
|
|
|
|
|
|
|
if not terminal.is_zoomed():
|
2015-11-28 23:22:57 +00:00
|
|
|
sensitive = not terminal.get_toplevel() == terminal.get_parent()
|
|
|
|
|
2014-09-19 14:10:43 +00:00
|
|
|
item = Gtk.MenuItem.new_with_mnemonic(_('_Zoom terminal'))
|
2009-10-08 19:27:49 +00:00
|
|
|
item.connect('activate', terminal.zoom)
|
2015-11-28 23:22:57 +00:00
|
|
|
item.set_sensitive(sensitive)
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.append(item)
|
|
|
|
|
2015-11-28 23:39:22 +00:00
|
|
|
item = Gtk.MenuItem.new_with_mnemonic(_('Ma_ximize terminal'))
|
2009-10-08 19:27:49 +00:00
|
|
|
item.connect('activate', terminal.maximise)
|
2015-11-28 23:22:57 +00:00
|
|
|
item.set_sensitive(sensitive)
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.append(item)
|
2009-11-14 18:56:34 +00:00
|
|
|
|
2017-01-18 08:17:15 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
2009-11-14 18:56:34 +00:00
|
|
|
else:
|
2014-09-19 14:10:43 +00:00
|
|
|
item = Gtk.MenuItem.new_with_mnemonic(_('_Restore all terminals'))
|
2009-11-14 18:56:34 +00:00
|
|
|
item.connect('activate', terminal.unzoom)
|
|
|
|
menu.append(item)
|
2009-10-08 19:27:49 +00:00
|
|
|
|
2017-01-18 08:17:15 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
2009-10-08 19:27:49 +00:00
|
|
|
|
2010-05-15 13:30:13 +00:00
|
|
|
if self.config['show_titlebar'] == False:
|
2014-09-19 14:10:43 +00:00
|
|
|
item = Gtk.MenuItem.new_with_mnemonic(_('Grouping'))
|
2010-05-15 13:30:13 +00:00
|
|
|
submenu = self.terminal.populate_group_menu()
|
|
|
|
submenu.show_all()
|
|
|
|
item.set_submenu(submenu)
|
|
|
|
menu.append(item)
|
2017-01-18 08:17:15 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
2010-05-15 13:30:13 +00:00
|
|
|
|
2020-12-27 16:12:19 +00:00
|
|
|
if terminal.is_held_open:
|
|
|
|
item = Gtk.MenuItem.new_with_mnemonic(_('Relaunch Command'))
|
|
|
|
item.connect('activate', lambda x: terminal.spawn_child())
|
|
|
|
menu.append(item)
|
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
|
|
|
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.CheckMenuItem, 'toggle_scrollbar',
|
|
|
|
'Show _scrollbar')
|
2009-10-08 19:27:49 +00:00
|
|
|
item.set_active(terminal.scrollbar.get_property('visible'))
|
|
|
|
item.connect('toggled', lambda x: terminal.do_scrollbar_toggle())
|
|
|
|
menu.append(item)
|
|
|
|
|
2014-09-19 14:10:43 +00:00
|
|
|
if hasattr(Gtk, 'Builder'): # VERIFY FOR GTK3: is this ever false?
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.MenuItem, 'preferences',
|
|
|
|
'_Preferences')
|
2010-07-02 10:09:21 +00:00
|
|
|
item.connect('activate', lambda x: PrefsEditor(self.terminal))
|
|
|
|
menu.append(item)
|
2009-12-26 20:09:16 +00:00
|
|
|
|
2018-04-24 18:22:10 +00:00
|
|
|
profilelist = sorted(self.config.list_profiles(), key=str.lower)
|
2009-12-26 20:09:16 +00:00
|
|
|
|
2010-01-03 17:53:25 +00:00
|
|
|
if len(profilelist) > 1:
|
2014-09-19 14:10:43 +00:00
|
|
|
item = Gtk.MenuItem.new_with_mnemonic(_('Profiles'))
|
2014-09-19 14:08:08 +00:00
|
|
|
submenu = Gtk.Menu()
|
2010-01-03 17:53:25 +00:00
|
|
|
item.set_submenu(submenu)
|
|
|
|
menu.append(item)
|
2009-12-26 20:09:16 +00:00
|
|
|
|
2010-01-03 17:53:25 +00:00
|
|
|
current = terminal.get_profile()
|
2009-12-26 20:09:16 +00:00
|
|
|
|
2010-01-03 17:53:25 +00:00
|
|
|
group = None
|
2009-12-26 20:09:16 +00:00
|
|
|
|
2010-01-08 01:04:06 +00:00
|
|
|
for profile in profilelist:
|
2017-02-19 15:57:36 +00:00
|
|
|
profile_label = profile
|
|
|
|
if profile_label == 'default':
|
|
|
|
profile_label = profile.capitalize()
|
|
|
|
item = Gtk.RadioMenuItem(profile_label, group)
|
2010-01-03 17:53:25 +00:00
|
|
|
if profile == current:
|
|
|
|
item.set_active(True)
|
2010-05-15 13:51:20 +00:00
|
|
|
item.connect('activate', terminal.force_set_profile, profile)
|
2010-01-03 17:53:25 +00:00
|
|
|
submenu.append(item)
|
2009-10-08 20:27:00 +00:00
|
|
|
|
2020-04-26 17:40:48 +00:00
|
|
|
self.add_layout_launcher(menu)
|
2009-10-08 19:27:49 +00:00
|
|
|
|
2009-12-18 00:45:08 +00:00
|
|
|
try:
|
|
|
|
menuitems = []
|
|
|
|
registry = plugin.PluginRegistry()
|
|
|
|
registry.load_plugins()
|
|
|
|
plugins = registry.get_plugins_by_capability('terminal_menu')
|
|
|
|
for menuplugin in plugins:
|
|
|
|
menuplugin.callback(menuitems, menu, terminal)
|
2010-01-05 22:28:14 +00:00
|
|
|
|
|
|
|
if len(menuitems) > 0:
|
2017-01-18 08:17:15 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
2010-01-05 22:28:14 +00:00
|
|
|
|
2009-12-18 00:45:08 +00:00
|
|
|
for menuitem in menuitems:
|
|
|
|
menu.append(menuitem)
|
2018-04-24 18:22:10 +00:00
|
|
|
except Exception as ex:
|
2009-12-18 00:45:08 +00:00
|
|
|
err('TerminalPopupMenu::show: %s' % ex)
|
|
|
|
|
2009-10-08 19:27:49 +00:00
|
|
|
menu.show_all()
|
2020-12-28 18:59:34 +00:00
|
|
|
menu.popup_at_pointer(None)
|
2009-10-08 19:27:49 +00:00
|
|
|
|
|
|
|
return(True)
|
|
|
|
|
2020-04-26 17:40:48 +00:00
|
|
|
def add_layout_launcher(self, menu):
|
|
|
|
"""Add the layout list to the menu"""
|
2022-10-31 06:56:34 +00:00
|
|
|
item = self.menu_item(Gtk.MenuItem, 'layout_launcher', '_Layouts...')
|
2020-04-26 17:40:48 +00:00
|
|
|
menu.append(item)
|
2020-06-17 18:11:20 +00:00
|
|
|
submenu = Gtk.Menu()
|
|
|
|
item.set_submenu(submenu)
|
|
|
|
layouts = self.config.list_layouts()
|
|
|
|
for layout in layouts:
|
|
|
|
item = Gtk.MenuItem(layout)
|
2022-10-15 20:17:36 +00:00
|
|
|
item.connect('activate', lambda x: spawn_new_terminator(self.terminator.origcwd, ['-u', '-l', x.get_label()]))
|
2020-06-17 18:11:20 +00:00
|
|
|
submenu.append(item)
|