def rotate(self, widget, clockwise): """Rotate children in this window""" self.set_pos_by_ratio = True maker = Factory() child = self.get_child() # If our child is a Notebook, reset to work from its visible child if maker.isinstance(child, 'Notebook'): pagenum = child.get_current_page() child = child.get_nth_page(pagenum) if maker.isinstance(child, 'Paned'): parent = child.get_parent() # Need to get the allocation before we remove the child, # otherwise _sometimes_ we get incorrect values. alloc = child.get_allocation() parent.remove(child) child.rotate_recursive(parent, alloc.width, alloc.height, clockwise) self.show_all() while Gtk.events_pending(): Gtk.main_iteration_do(False) widget.grab_focus() self.set_pos_by_ratio = False
def tab_change(self, widget, num=None): """Change to a specific tab""" if num is None: err('must specify a tab to change to') maker = Factory() child = self.get_child() if not maker.isinstance(child, 'Notebook'): dbg('child is not a notebook, nothing to change to') return if num == -1: # Go to the next tab cur = child.get_current_page() pages = child.get_n_pages() if cur == pages - 1: num = 0 else: num = cur + 1 elif num == -2: # Go to the previous tab cur = child.get_current_page() if cur > 0: num = cur - 1 else: num = child.get_n_pages() - 1 child.set_current_page(num) # Work around strange bug in gtk-2.12.11 and pygtk-2.12.1 # Without it, the selection changes, but the displayed page doesn't # change child.set_current_page(child.get_current_page())
def move_tab(self, widget, direction): """Handle a keyboard shortcut for moving tab positions""" maker = Factory() notebook = self.get_child() if not maker.isinstance(notebook, 'Notebook'): dbg('not in a notebook, refusing to move tab %s' % direction) return dbg('moving tab %s' % direction) numpages = notebook.get_n_pages() page = notebook.get_current_page() child = notebook.get_nth_page(page) if direction == 'left': if page == 0: page = numpages else: page = page - 1 elif direction == 'right': if page == numpages - 1: page = 0 else: page = page + 1 else: err('unknown direction: %s' % direction) return notebook.reorder_child(child, page)
def add(self, widget, metadata=None): """Add a widget to the window by way of Gtk.Window.add()""" maker = Factory() Gtk.Window.add(self, widget) if maker.isinstance(widget, 'Terminal'): signals = { 'close-term': self.closeterm, 'title-change': self.title.set_title, 'split-horiz': self.split_horiz, 'split-vert': self.split_vert, 'unzoom': self.unzoom, 'tab-change': self.tab_change, 'group-all': self.group_all, 'group-all-toggle': self.group_all_toggle, 'ungroup-all': self.ungroup_all, 'group-tab': self.group_tab, 'group-tab-toggle': self.group_tab_toggle, 'ungroup-tab': self.ungroup_tab, 'move-tab': self.move_tab, 'tab-new': [self.tab_new, widget], 'navigate': self.navigate_terminal } for signal in signals: args = [] handler = signals[signal] if isinstance(handler, list): args = handler[1:] handler = handler[0] self.connect_child(widget, signal, handler, *args) widget.grab_focus()
def split_axis(self, widget, vertical=True, cwd=None, sibling=None, widgetfirst=True): """Split the axis of a terminal inside us""" dbg('called for widget: %s' % widget) order = None page_num = self.page_num(widget) if page_num == -1: err('Notebook::split_axis: %s not found in Notebook' % widget) return label = self.get_tab_label(widget) self.remove(widget) maker = Factory() if vertical: container = maker.make('vpaned') else: container = maker.make('hpaned') self.get_toplevel().set_pos_by_ratio = True if not sibling: sibling = maker.make('terminal') sibling.set_cwd(cwd) if self.config['always_split_with_profile']: sibling.force_set_profile(None, widget.get_profile()) sibling.spawn_child() if widget.group and self.config['split_to_group']: sibling.set_group(None, widget.group) elif self.config['always_split_with_profile']: sibling.force_set_profile(None, widget.get_profile()) self.insert_page(container, None, page_num) self.child_set_property(container, 'tab-expand', True) self.child_set_property(container, 'tab-fill', True) self.set_tab_reorderable(container, True) self.set_tab_label(container, label) self.show_all() order = [widget, sibling] if widgetfirst is False: order.reverse() for terminal in order: container.add(terminal) self.set_current_page(page_num) self.show_all() while Gtk.events_pending(): Gtk.main_iteration_do(False) self.get_toplevel().set_pos_by_ratio = False GLib.idle_add(terminal.ensure_visible_and_focussed)
def __init__(self): """Class initialiser""" self.terminator = Terminator() self.maker = Factory() Container.__init__(self) self.signals.append({'name' : 'resize-term', 'flags' : GObject.SignalFlags.RUN_LAST, 'return_type': None, 'param_types': (GObject.TYPE_STRING,)})
def set_autoresize(self, autoresize): """Must be called on the highest ancestor in one given orientation""" """TODO write some better doc :)""" maker = Factory() children = self.get_children() self.child_set_property(children[0], 'resize', False) self.child_set_property(children[1], 'resize', not autoresize) for child in children: if maker.type(child) == maker.type(self): child.set_autoresize(autoresize)
def ungroup_tab(self, widget): """Ungroup all terminals in the current tab""" maker = Factory() notebook = self.get_child() if not maker.isinstance(notebook, 'Notebook'): dbg('note in a notebook, refusing to ungroup tab') return self.set_groups(None, self.get_visible_terminals())
def on_delete_event(self, window, event, data=None): """Handle a window close request""" maker = Factory() if maker.isinstance(self.get_child(), 'Terminal'): if self.get_property('term_zoomed') == True: return self.confirm_close(window, _('window')) else: dbg('Window::on_delete_event: Only one child, closing is fine') return False elif maker.isinstance(self.get_child(), 'Container'): return self.confirm_close(window, _('window')) else: dbg('unknown child: %s' % self.get_child())
def get_tab_title(self, uuid=None): """Return the title of a parent tab of a given terminal""" maker = Factory() terminal = self.terminator.find_terminal_by_uuid(uuid) window = terminal.get_toplevel() root_widget = window.get_children()[0] if maker.isinstance(root_widget, "Notebook"): for tab_child in root_widget.get_children(): terms = [tab_child] if not maker.isinstance(terms[0], "Terminal"): terms = enumerate_descendants(tab_child)[1] if terminal in terms: return root_widget.get_tab_label(tab_child).get_label()
def propagate_title_change(self, widget, title): """Pass a title change up the widget stack""" maker = Factory() parent = self.get_parent() title = widget.get_window_title() if maker.isinstance(self, 'Notebook'): self.update_tab_label_text(widget, title) elif maker.isinstance(self, 'Window'): self.title.set_title(widget, title) if maker.isinstance(parent, 'Container'): parent.propagate_title_change(widget, title)
def closetab(self, widget, label): """Close a tab""" tabnum = None try: nb = widget.notebook except AttributeError: err('TabLabel::closetab: called on non-Notebook: %s' % widget) return for i in range(0, nb.get_n_pages() + 1): if label == nb.get_tab_label(nb.get_nth_page(i)): tabnum = i break if tabnum is None: err('TabLabel::closetab: %s not in %s. Bailing.' % (label, nb)) return maker = Factory() child = nb.get_nth_page(tabnum) if maker.isinstance(child, 'Terminal'): dbg('Notebook::closetab: child is a single Terminal') del nb.last_active_term[child] child.close() # FIXME: We only do this del and return here to avoid removing the # page below, which child.close() implicitly does del(label) return elif maker.isinstance(child, 'Container'): dbg('Notebook::closetab: child is a Container') result = self.construct_confirm_close(self.window, _('tab')) if result == Gtk.ResponseType.ACCEPT: containers = None objects = None containers, objects = enumerate_descendants(child) while len(objects) > 0: descendant = objects.pop() descendant.close() while Gtk.events_pending(): Gtk.main_iteration() return else: dbg('Notebook::closetab: user cancelled request') return else: err('Notebook::closetab: child is unknown type %s' % child) return
def new_window(self, cwd=None, profile=None): """Create a window with a Terminal in it""" maker = Factory() window = maker.make('Window') terminal = maker.make('Terminal') if cwd: terminal.set_cwd(cwd) if profile and self.config['always_split_with_profile']: terminal.force_set_profile(None, profile) window.add(terminal) window.show(True) terminal.spawn_child() return (window, terminal)
def split_axis(self, widget, vertical=True, cwd=None, sibling=None, widgetfirst=True): """Split the window""" if self.get_property('term_zoomed') == True: err("You can't split while a terminal is maximised/zoomed") return order = None maker = Factory() self.remove(widget) if vertical: container = maker.make('VPaned') else: container = maker.make('HPaned') self.set_pos_by_ratio = True if not sibling: sibling = maker.make('Terminal') sibling.set_cwd(cwd) if self.config['always_split_with_profile']: sibling.force_set_profile(None, widget.get_profile()) sibling.spawn_child() if widget.group and self.config['split_to_group']: sibling.set_group(None, widget.group) elif self.config['always_split_with_profile']: sibling.force_set_profile(None, widget.get_profile()) self.add(container) container.show_all() order = [widget, sibling] if widgetfirst is False: order.reverse() for term in order: container.add(term) container.show_all() while Gtk.events_pending(): Gtk.main_iteration_do(False) sibling.grab_focus() self.set_pos_by_ratio = False
def get_tab(self, uuid=None): """Return the UUID of the parent tab of a given terminal""" maker = Factory() terminal = self.terminator.find_terminal_by_uuid(uuid) window = terminal.get_toplevel() root_widget = window.get_children()[0] if maker.isinstance(root_widget, 'Notebook'): #return root_widget.uuid.urn for tab_child in root_widget.get_children(): terms = [tab_child] if not maker.isinstance(terms[0], "Terminal"): terms = enumerate_descendants(tab_child)[1] if terminal in terms: # FIXME: There are no uuid's assigned to the the notebook, or the actual tabs! # This would fail: return root_widget.uuid.urn return ""
def group_tab(self, widget): """Group all terminals in the current tab""" maker = Factory() notebook = self.get_child() if not maker.isinstance(notebook, 'Notebook'): dbg('not in a notebook, refusing to group tab') return pagenum = notebook.get_current_page() while True: group = _('Tab %d') % pagenum if group not in self.terminator.groups: break pagenum += 1 self.set_groups(group, self.get_visible_terminals())
def get_visible_terminals(self): """Walk down the widget tree to find all of the visible terminals. Mostly using Container::get_visible_terminals()""" terminals = {} if not hasattr(self, 'cached_maker'): self.cached_maker = Factory() maker = self.cached_maker child = self.get_child() if not child: return [] # If our child is a Notebook, reset to work from its visible child if maker.isinstance(child, 'Notebook'): pagenum = child.get_current_page() child = child.get_nth_page(pagenum) if maker.isinstance(child, 'Container'): terminals.update(child.get_visible_terminals()) elif maker.isinstance(child, 'Terminal'): terminals[child] = child.get_allocation() else: err('Unknown child type %s' % type(child)) return terminals
def rotate_recursive(self, parent, w, h, clockwise): """ Recursively rotate "self" into a new paned that'll have "w" x "h" size. Attach it to "parent". As discussed in LP#1522542, we should build up the new layout (including the separator positions) in a single step. We can't rely on Gtk+ computing the allocation sizes yet, so we have to do the computation ourselves and carry the resulting paned sizes all the way down the widget tree. """ maker = Factory() handle_size = self.get_handlesize() if isinstance(self, HPaned): container = VPaned() reverse = not clockwise else: container = HPaned() reverse = clockwise container.ratio = self.ratio children = self.get_children() if reverse: container.ratio = 1 - container.ratio children.reverse() if isinstance(self, HPaned): w1 = w2 = w h1 = pos = self.position_by_ratio(h, handle_size, container.ratio) h2 = max(h - h1 - handle_size, 0) else: h1 = h2 = h w1 = pos = self.position_by_ratio(w, handle_size, container.ratio) w2 = max(w - w1 - handle_size, 0) container.set_pos(pos) parent.add(container) if maker.isinstance(children[0], 'Terminal'): children[0].get_parent().remove(children[0]) container.add(children[0]) else: children[0].rotate_recursive(container, w1, h1, clockwise) if maker.isinstance(children[1], 'Terminal'): children[1].get_parent().remove(children[1]) container.add(children[1]) else: children[1].rotate_recursive(container, w2, h2, clockwise)
def _do_redistribute(self, recurse_up=False, recurse_down=False): maker = Factory() #2 Make a list of self + all children of same type tree = [self, [], 0, None] toproc = [tree] number_splits = 1 while toproc: curr = toproc.pop(0) for child in curr[0].get_children(): if type(child) == type(curr[0]): childset = [child, [], 0, curr] curr[1].append(childset) toproc.append(childset) number_splits = number_splits + 1 else: curr[1].append([None, [], 1, None]) p = curr while p: p[2] = p[2] + 1 p = p[3] # (1c) If Shift modifier, redistribute lower sections too if recurse_down and \ (maker.isinstance(child, 'VPaned') or \ maker.isinstance(child, 'HPaned')): child.do_redistribute(False, True) #3 Get ancestor x/y => a, and handle size => hs avail_pixels = self.get_length() handle_size = self.get_handlesize() #4 Math! eek (a - (n * hs)) / (n + 1) = single size => s single_size = (avail_pixels - (number_splits * handle_size)) / (number_splits + 1) arr_sizes = [single_size] * (number_splits + 1) for i in range(avail_pixels % (number_splits + 1)): arr_sizes[i] = arr_sizes[i] + 1 #5 Descend down setting the handle position to s # (Has to handle nesting properly) toproc = [tree] while toproc: curr = toproc.pop(0) for child in curr[1]: toproc.append(child) if curr[1].index(child) == 0: curr[0].set_position((child[2] * single_size) + ((child[2] - 1) * handle_size))
def tab_new(self, widget=None, debugtab=False, _param1=None, _param2=None): """Make a new tab""" cwd = None profile = None if self.get_property('term_zoomed') == True: err("You can't create a tab while a terminal is maximised/zoomed") return if widget: cwd = widget.get_cwd() profile = widget.get_profile() maker = Factory() if not self.is_child_notebook(): dbg('Making a new Notebook') notebook = maker.make('Notebook', window=self) self.show() self.present() return self.get_child().newtab(debugtab, cwd=cwd, profile=profile)
def do_redistribute(self, recurse_up=False, recurse_down=False): """Evenly divide available space between sibling panes""" maker = Factory() #1 Find highest ancestor of the same type => ha highest_ancestor = self while type(highest_ancestor.get_parent()) == type(highest_ancestor): highest_ancestor = highest_ancestor.get_parent() highest_ancestor.set_autoresize(False) # (1b) If Super modifier, redistribute higher sections too if recurse_up: grandfather = highest_ancestor.get_parent() if maker.isinstance(grandfather, 'VPaned') or \ maker.isinstance(grandfather, 'HPaned') : grandfather.do_redistribute(recurse_up, recurse_down) highest_ancestor._do_redistribute(recurse_up, recurse_down) GLib.idle_add(highest_ancestor.set_autoresize, True)
def set_rough_geometry_hints(self): """Walk all the terminals along the top and left edges to fake up how many columns/rows we sort of have""" if self.ismaximised == True: return if not hasattr(self, 'cached_maker'): self.cached_maker = Factory() maker = self.cached_maker if maker.isinstance(self.get_child(), 'Notebook'): dbg("We don't currently support geometry hinting with tabs") return terminals = self.get_visible_terminals() column_sum = 0 row_sum = 0 for terminal in terminals: rect = terminal.get_allocation() if rect.x == 0: cols, rows = terminal.get_size() row_sum = row_sum + rows if rect.y == 0: cols, rows = terminal.get_size() column_sum = column_sum + cols if column_sum == 0 or row_sum == 0: dbg('column_sum=%s,row_sum=%s. No terminals found in >=1 axis' % (column_sum, row_sum)) return # FIXME: I don't think we should just use whatever font size info is on # the last terminal we inspected. Looking up the default profile font # size and calculating its character sizes would be rather expensive # though. font_width, font_height = terminal.get_font_size() total_font_width = font_width * column_sum total_font_height = font_height * row_sum win_width, win_height = self.get_size() extra_width = win_width - total_font_width extra_height = win_height - total_font_height dbg('setting geometry hints: (ewidth:%s)(eheight:%s),\ (fwidth:%s)(fheight:%s)' % (extra_width, extra_height, font_width, font_height)) geometry = Gdk.Geometry() geometry.base_width = extra_width geometry.base_height = extra_height geometry.width_inc = font_width geometry.height_inc = font_height self.set_geometry_hints( self, geometry, Gdk.WindowHints.BASE_SIZE | Gdk.WindowHints.RESIZE_INC)
def enumerate_descendants(parent): """Walk all our children and build up a list of containers and terminals""" # FIXME: Does having to import this here mean we should move this function # back to Container? from terminatorlib.factory import Factory containerstmp = [] containers = [] terminals = [] maker = Factory() if parent is None: err('no parent widget specified') return for descendant in parent.get_children(): if maker.isinstance(descendant, 'Container'): containerstmp.append(descendant) elif maker.isinstance(descendant, 'Terminal'): terminals.append(descendant) while len(containerstmp) > 0: child = containerstmp.pop(0) for descendant in child.get_children(): if maker.isinstance(descendant, 'Container'): containerstmp.append(descendant) elif maker.isinstance(descendant, 'Terminal'): terminals.append(descendant) containers.append(child) dbg(f'{len(containers)} containers and {len(terminals)} terminals fall beneath {parent}' ) return containers, terminals
def get_visible_terminals(self): """Walk the widget tree to find all of the visible terminals. That is, any terminals which are not hidden in another Notebook pane""" if not hasattr(self, 'cached_maker'): self.cached_maker = Factory() maker = self.cached_maker terminals = {} for child in self.get_offspring(): if not child: continue if maker.isinstance(child, 'Terminal'): terminals[child] = child.get_allocation() elif maker.isinstance(child, 'Container'): terminals.update(child.get_visible_terminals()) else: err('Unknown child type %s' % type(child)) return (terminals)
def on_key_press(self, window, event): """Handle a keyboard event""" maker = Factory() self.set_urgency_hint(False) mapping = self.terminator.keybindings.lookup(event) if mapping: dbg('Window::on_key_press: looked up %r' % mapping) if mapping == 'full_screen': self.set_fullscreen(not self.isfullscreen) elif mapping == 'close_window': if not self.on_delete_event( window, Gdk.Event.new(Gdk.EventType.DELETE)): self.on_destroy_event(window, Gdk.Event.new(Gdk.EventType.DESTROY)) else: return False return True
def describe_layout(self, count, parent, global_layout, child_order): """Describe our current layout""" layout = {} maker = Factory() mytype = maker.type(self) if not mytype: err('unable to detemine own type. %s' % self) return ({}) layout['type'] = mytype layout['parent'] = parent layout['order'] = child_order if hasattr(self, 'get_position'): position = self.get_position() if hasattr(position, '__iter__'): position = ':'.join([str(x) for x in position]) layout['position'] = position if hasattr(self, 'ismaximised'): layout['maximised'] = self.ismaximised if hasattr(self, 'isfullscreen'): layout['fullscreen'] = self.isfullscreen if hasattr(self, 'ratio'): layout['ratio'] = self.ratio if hasattr(self, 'get_size'): layout['size'] = self.get_size() if hasattr(self, 'title'): layout['title'] = self.title.text if mytype == 'Notebook': labels = [] last_active_term = [] for tabnum in xrange(0, self.get_n_pages()): page = self.get_nth_page(tabnum) label = self.get_tab_label(page) labels.append(label.get_custom_label()) last_active_term.append( self.last_active_term[self.get_nth_page(tabnum)]) layout['labels'] = labels layout['last_active_term'] = last_active_term layout['active_page'] = self.get_current_page() else: if hasattr( self, 'last_active_term') and self.last_active_term is not None: layout['last_active_term'] = self.last_active_term if mytype == 'Window': if self.uuid == self.terminator.last_active_window: layout['last_active_window'] = True else: layout['last_active_window'] = False name = 'child%d' % count count = count + 1 global_layout[name] = layout child_order = 0 for child in self.get_children(): if hasattr(child, 'describe_layout'): count = child.describe_layout(count, name, global_layout, child_order) child_order = child_order + 1 return (count)
def reconfigure(self): """Update configuration for the whole application""" if self.style_providers != []: for style_provider in self.style_providers: Gtk.StyleContext.remove_provider_for_screen( Gdk.Screen.get_default(), style_provider) self.style_providers = [] # Force the window background to be transparent for newer versions of # GTK3. We then have to fix all the widget backgrounds because the # widgets theming may not render it's own background. css = """ .terminator-terminal-window { background-color: alpha(@theme_bg_color,0); } .terminator-terminal-window .notebook.header, .terminator-terminal-window notebook header { background-color: @theme_bg_color; } .terminator-terminal-window .pane-separator { background-color: @theme_bg_color; } .terminator-terminal-window .terminator-terminal-searchbar { background-color: @theme_bg_color; } """ # Fix several themes that put a borders, corners, or backgrounds around # viewports, making the titlebar look bad. css += """ .terminator-terminal-window GtkViewport, .terminator-terminal-window viewport { border-width: 0px; border-radius: 0px; background-color: transparent; } """ # Add per profile snippets for setting the background of the HBox template = """ .terminator-profile-%s { background-color: alpha(%s, %s); } """ profiles = self.config.base.profiles for profile in profiles.keys(): if profiles[profile]['use_theme_colors']: # Create a dummy window/vte and realise it so it has correct # values to read from tmp_win = Gtk.Window() tmp_vte = Vte.Terminal() tmp_win.add(tmp_vte) tmp_win.realize() bgcolor = tmp_vte.get_style_context().get_background_color( Gtk.StateType.NORMAL) bgcolor = "#{0:02x}{1:02x}{2:02x}".format( int(bgcolor.red * 255), int(bgcolor.green * 255), int(bgcolor.blue * 255)) tmp_win.remove(tmp_vte) del tmp_vte del tmp_win else: bgcolor = Gdk.RGBA() bgcolor = profiles[profile]['background_color'] if profiles[profile]['background_type'] == 'transparent': bgalpha = profiles[profile]['background_darkness'] else: bgalpha = "1" munged_profile = "".join( [c if c.isalnum() else "-" for c in profile]) css += template % (munged_profile, bgcolor, bgalpha) style_provider = Gtk.CssProvider() style_provider.load_from_data(css.encode('utf8')) self.style_providers.append(style_provider) # Attempt to load some theme specific stylistic tweaks for appearances usr_theme_dir = os.path.expanduser('~/.local/share/themes') (head, _tail) = os.path.split(borg.__file__) app_theme_dir = os.path.join(head, 'themes') theme_name = self.gtk_settings.get_property('gtk-theme-name') theme_part_list = ['terminator.css'] if self.config[ 'extra_styling']: # checkbox_style - needs adding to prefs theme_part_list.append('terminator_styling.css') for theme_part_file in theme_part_list: for theme_dir in [usr_theme_dir, app_theme_dir]: path_to_theme_specific_css = os.path.join( theme_dir, theme_name, 'gtk-3.0/apps', theme_part_file) if os.path.isfile(path_to_theme_specific_css): style_provider = Gtk.CssProvider() style_provider.connect('parsing-error', self.on_css_parsing_error) try: style_provider.load_from_path( path_to_theme_specific_css) except GError: # Hmmm. Should we try to provide GTK version specific files here on failure? gtk_version_string = '.'.join([ str(Gtk.get_major_version()), str(Gtk.get_minor_version()), str(Gtk.get_micro_version()) ]) err('Error(s) loading css from %s into Gtk %s' % (path_to_theme_specific_css, gtk_version_string)) self.style_providers.append(style_provider) break # Size the GtkPaned splitter handle size. if self.config['handle_size'] in range(0, 21): css = '''.terminator-terminal-window GtkPaned, .terminator-terminal-window paned {{ min-width: {0}px; min-height: {0}px; }} '''.format(self.config['handle_size']) style_provider = Gtk.CssProvider() style_provider.load_from_data(css.encode()) self.style_providers.append(style_provider) # Apply the providers, incrementing priority so they don't cancel out # each other for idx in range(0, len(self.style_providers)): Gtk.StyleContext.add_provider_for_screen( Gdk.Screen.get_default(), self.style_providers[idx], Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION + idx) # Cause all the terminals to reconfigure for terminal in self.terminals: terminal.reconfigure() # Reparse our keybindings self.keybindings.configure(self.config['keybindings']) # Update tab position if appropriate maker = Factory() for window in self.windows: child = window.get_child() if maker.isinstance(child, 'Notebook'): child.configure()
def layout_done(self): """Layout operations have finished, record that fact""" self.doing_layout = False maker = Factory() window_last_active_term_mapping = {} for window in self.windows: if window.is_child_notebook(): source = window.get_toplevel().get_children()[0] else: source = window window_last_active_term_mapping[window] = copy.copy( source.last_active_term) for terminal in self.terminals: if not terminal.pid: terminal.spawn_child() for window in self.windows: if window.is_child_notebook(): # For windows with a notebook notebook = window.get_toplevel().get_children()[0] # Cycle through pages by number for page in range(0, notebook.get_n_pages()): # Try and get the entry in the previously saved mapping mapping = window_last_active_term_mapping[window] page_last_active_term = mapping.get( notebook.get_nth_page(page), None) if page_last_active_term is None: # Couldn't find entry, so we find the first child of type Terminal children = notebook.get_nth_page(page).get_children() for page_last_active_term in children: if maker.isinstance(page_last_active_term, 'Terminal'): page_last_active_term = page_last_active_term.uuid break else: err('Should never reach here!') page_last_active_term = None if page_last_active_term is None: # Bail on this tab as we're having no luck here, continue with the next continue # Set the notebook entry, then ensure Terminal is visible and focussed urn = page_last_active_term.urn notebook.last_active_term[notebook.get_nth_page( page)] = page_last_active_term if urn: term = self.find_terminal_by_uuid(urn) if term: term.ensure_visible_and_focussed() else: # For windows without a notebook ensure Terminal is visible and focussed if window_last_active_term_mapping[window]: term = self.find_terminal_by_uuid( window_last_active_term_mapping[window].urn) term.ensure_visible_and_focussed() # Build list of new windows using prelayout list new_win_list = [] for window in self.windows: if window not in self.prelayout_windows: new_win_list.append(window) # Make sure all new windows get bumped to the top for window in new_win_list: window.show() window.grab_focus() try: t = GdkX11.x11_get_server_time(window.get_window()) except (TypeError, AttributeError): t = 0 window.get_window().focus(t) # Awful workaround to be sure that the last focused window is actually the one focused. # Don't ask, don't tell policy on this. Even this is not 100% if self.last_active_window: window = self.find_window_by_uuid(self.last_active_window.urn) count = 0 while count < 1000 and Gtk.events_pending(): count += 1 Gtk.main_iteration_do(False) window.show() window.grab_focus() try: t = GdkX11.x11_get_server_time(window.get_window()) except (TypeError, AttributeError): t = 0 window.get_window().focus(t) self.prelayout_windows = None
def newtab(self, debugtab=False, widget=None, cwd=None, metadata=None, profile=None): """Add a new tab, optionally supplying a child widget""" dbg('making a new tab') maker = Factory() top_window = self.get_toplevel() if not widget: widget = maker.make('Terminal') if cwd: widget.set_cwd(cwd) if profile and self.config['always_split_with_profile']: widget.force_set_profile(None, profile) widget.spawn_child(debugserver=debugtab) elif profile and self.config['always_split_with_profile']: widget.force_set_profile(None, profile) signals = {'close-term': self.wrapcloseterm, 'split-horiz': self.split_horiz, 'split-vert': self.split_vert, 'title-change': self.propagate_title_change, 'unzoom': self.unzoom, 'tab-change': top_window.tab_change, 'group-all': top_window.group_all, 'group-all-toggle': top_window.group_all_toggle, 'ungroup-all': top_window.ungroup_all, 'group-tab': top_window.group_tab, 'group-tab-toggle': top_window.group_tab_toggle, 'ungroup-tab': top_window.ungroup_tab, 'move-tab': top_window.move_tab, 'tab-new': [top_window.tab_new, widget], 'navigate': top_window.navigate_terminal} if maker.isinstance(widget, 'Terminal'): for signal in signals: args = [] handler = signals[signal] if isinstance(handler, list): args = handler[1:] handler = handler[0] self.connect_child(widget, signal, handler, *args) if metadata and metadata.has_key('tabnum'): tabpos = metadata['tabnum'] else: tabpos = -1 label = TabLabel(self.window.get_title(), self) if metadata and metadata.has_key('label'): dbg('creating TabLabel with text: %s' % metadata['label']) label.set_custom_label(metadata['label']) label.connect('close-clicked', self.closetab) label.show_all() widget.show_all() dbg('inserting page at position: %s' % tabpos) self.insert_page(widget, None, tabpos) if maker.isinstance(widget, 'Terminal'): containers, objects = ([], [widget]) else: containers, objects = enumerate_descendants(widget) term_widget = None for term_widget in objects: if maker.isinstance(term_widget, 'Terminal'): self.set_last_active_term(term_widget.uuid) break self.set_tab_label(widget, label) self.child_set_property(widget, 'tab-expand', True) self.child_set_property(widget, 'tab-fill', True) self.set_tab_reorderable(widget, True) self.set_current_page(tabpos) self.show_all() if maker.isinstance(term_widget, 'Terminal'): widget.grab_focus()
def is_child_notebook(self): """Returns True if this Window's child is a Notebook""" maker = Factory() return maker.isinstance(self.get_child(), 'Notebook')