def get_combobox_by_label(self, text): label = [ label for label in get_widgets_by_type(Gtk.Label, self.dialog) if label.get_label() == text ][0] return label.get_parent().get_children()[1]
def get_switch(self, label, gtk_type): """ Return the switch widget identified by ``label`` and belonging to the given ``gtk_type``. Here is a simple example showing how to get the widget associated to a given switch: # open the editor first editor = GPS.BuildTargetsEditor() yield editor.open_and_yield() # select the page containing the switch editor.select_page("Build All") # get the widget associated to the 'Compile only' # switch switch_widget = editor.get_switch("Compile only", Gtk.ToggleButton) """ switches = get_widgets_by_type( gtk_type, self.get_current_page_widget()) result = [switch for switch in switches if BuildTargetsEditor.get_switch_label( switch, gtk_type) == label and switch.get_mapped()] if result: return result[0] else: return None
def get_switch(self, label, gtk_type): """ Return the switch widget identified by ``label`` and belonging to the given ``gtk_type``. Here is a simple example showing how to get the widget associated to a given switch: # open the editor first editor = GPS.Project_Properties() yield editor.open_and_yield() # select the page containing the switch editor.select_page("Build/Switches/Builder") # get the widget associated to the 'Compile only' # switch switch_widget = editor.get_switch("Compile only", Gtk.ToggleButton) """ model, iter = self.treeview.get_selection().get_selected() page_widget = model[iter][Project_Properties_Editor.COLUMN_WIDGET] switches = get_widgets_by_type(gtk_type, page_widget) result = [switch for switch in switches if BuildTargetsEditor.get_switch_label( switch, gtk_type) == label and switch.get_mapped()] if result: return result[0] else: return None
def get_switch(self, label, gtk_type): """ Return the switch widget identified by ``label`` and belonging to the given ``gtk_type``. Here is a simple example showing how to get the widget associated to a given switch: # open the editor first editor = GPS.BuildTargetsEditor() yield editor.open_and_yield() # select the page containing the switch editor.select_page("Build All") # get the widget associated to the 'Compile only' # switch switch_widget = editor.get_switch("Compile only", Gtk.ToggleButton) """ switches = get_widgets_by_type(gtk_type, self.dialog) result = [switch for switch in switches if BuildTargetsEditor.get_switch_label( switch, gtk_type) == label and switch.get_mapped()] if result: return result[0] else: return None
def open_and_yield(self, wait_scan=False): """ Compatible with run_test_driver, to be used in a yield statement editor = Project_Properties_Editor() yield editor.open_and_yield() ... if :param wait_scan: is True, this will wait for GPRconfig to complete its scanning before returning. """ yield self._open_and_yield('open project properties') # Wait for the GPRconfig scan to complete before editing # and/or saving the Project Properties editor if wait_scan: # FIXME: we are ignoring git task for now, they can create a fake # deadlock when executing a test yield wait_tasks(["git"]) self.treeview = get_widget_by_name('Project Properties Tree', self.dialogs) scenario_selector = get_widget_by_name( 'Project Properties Scenario Selector', self.dialogs) self.scenario_selector_tree = get_widgets_by_type( Gtk.TreeView, scenario_selector)[0]
def driver(): GPS.BuildTarget("Build All").execute() yield wait_tasks() b = GPS.EditorBuffer.get(GPS.File("pr.adb")) v = b.current_view() v.goto(b.at(4, 4)) GPS.execute_action("goto declaration") yield wait_tasks() bla_ads_buf = GPS.EditorBuffer.get() gps_assert(os.path.basename(bla_ads_buf.file().name()), "bla.ads", "Goto declaration didn't jump to the right file") bla_ads_view = bla_ads_buf.current_view() text_view = get_widgets_by_type(Gtk.TextView, bla_ads_view.pywidget())[0] adj_value = text_view.get_vadjustment().get_value() adj_max = text_view.get_vadjustment().get_upper() # Verify that we're scrolled vertically gps_assert(adj_value > VADJ_LOWER_BOUND, True, "The text is not scrolled after jumping to declaration") GPS.execute_action("goto body") yield wait_tasks() bla_adb_buf = GPS.EditorBuffer.get() gps_assert(os.path.basename(bla_adb_buf.file().name()), "bla.adb", "Goto body didn't jump to the right file") bla_adb_view = bla_ads_buf.current_view() text_view = get_widgets_by_type(Gtk.TextView, bla_adb_view.pywidget())[0] adj_value = text_view.get_vadjustment().get_value() adj_max = text_view.get_vadjustment().get_upper() # Verify again that we're scrolled vertically gps_assert(adj_value > VADJ_LOWER_BOUND, True, "The text is not scrolled after jumping to body")
def yield_click_on_item(self, label_text, button=1, events=pygps.single_click_events): """ Clicks on the Learn view's item identified with the given ``label_text``. """ children = get_widgets_by_type(Gtk.FlowBoxChild, self.dialog) for child in children: label = get_widgets_by_type(Gtk.Label, child)[0] if label.get_label() == label_text: if isinstance(child.get_parent(), Gtk.FlowBox): click_in_widget(child.get_window(), -1, -1, events=events) yield wait_idle() return
def set_use_exec_dir(self, value): """ Check or uncheck "Use exec dir" :param value: True or False """ check = [w for w in get_widgets_by_type(Gtk.CheckButton, self.dialogs) if w.get_label().startswith("Use exec dir")][0] check.set_active(value)
def on_file_edited(hn, f): def key_pressed_proxy(view, event): return view.vim_state.on_key_pressed(view, event) buffer = GPS.EditorBuffer.get(f) view = buffer.current_view() gtk_view = get_widgets_by_type(Gtk.TextView, view.pywidget())[0] gtk_view.vim_state = VimState(view, gtk_view) buffer.vim_state = gtk_view.vim_state gtk_view.connect("key-press-event", key_pressed_proxy)
def get_switch_label(switch, gtk_type): if (gtk_type == Gtk.ToggleButton or gtk_type == Gtk.Button or gtk_type == Gtk.RadioButton): return switch.get_label() else: labels = get_widgets_by_type(Gtk.Label, switch.get_parent()) if labels: return labels[0].get_label() else: return ""
def toggle_editor_wrapping(): """Toggle word wrapping in the current editor""" buffer = GPS.EditorBuffer.get () v = buffer.current_view() from pygps import get_widgets_by_type text_view = get_widgets_by_type(gtk.TextView, v.pywidget())[0] if text_view.get_wrap_mode() == gtk.WRAP_NONE: text_view.set_wrap_mode(gtk.WRAP_WORD) else: text_view.set_wrap_mode(gtk.WRAP_NONE)
def toggle_editor_wrapping(): """Toggle word wrapping in the current editor""" buffer = GPS.EditorBuffer.get() v = buffer.current_view() from pygps import get_widgets_by_type text_view = get_widgets_by_type(Gtk.TextView, v.pywidget())[0] if text_view.get_wrap_mode() == Gtk.WrapMode(0): text_view.set_wrap_mode(Gtk.WrapMode(2)) else: text_view.set_wrap_mode(Gtk.WrapMode(0))
def yield_click_on_item(self, label_text, button=1, events=pygps.single_click_events): """ Clicks on the Learn view's item identified with the given ``label_text``. Raises an exception when node corresponding item is not found. """ children = get_widgets_by_type(Gtk.FlowBoxChild, self.dialog) for child in children: label = get_widgets_by_type(Gtk.Label, child)[0] if label.get_label() == label_text: if isinstance(child.get_parent(), Gtk.FlowBox): alloc = label.get_allocation() click_in_widget( child.get_window(), alloc.x, alloc.y, events=events) yield wait_idle() return raise Exception("'%s' item not found in the Learn view" % (label_text))
def open_and_yield(self, docked=False): """ Open the search dialog, and returns a handle to it. This is compatible with run_test_driver: editor = GPS.Search() yield editor.open_and_yield() """ yield self._open_and_yield("Search") if docked: self.dialog = GPS.MDI.get("Search").pywidget() else: self.dialog = get_window_by_prefix('GNAT Studio - Search -') if self.dialog: combos = get_widgets_by_type(Gtk.ComboBox, self.dialog) self.find = get_button_from_label("Find", self.dialog) self.find_all = get_button_from_label("Find All", self.dialog) self.next = self.find # This is in fact the same button self.replace = get_button_from_label("Replace", self.dialog) self.replace_all = get_button_from_label("Replace All", self.dialog) self.close = get_button_from_label("Close", self.dialog) self.replace_and_find = get_button_from_label( "Replace & Find", self.dialog) self.scope = get_widget_by_name("search scope combo", self.dialog) self.pattern = combos[0].get_child() self.replace_text = combos[1].get_child() self.look_in = combos[3] if len(combos) >= 4 else combos[2] self.previous = get_button_from_label("Previous", self.dialog) toggle_buttons = get_widgets_by_type(Gtk.ToggleButton, self.dialog) self.regexp = toggle_buttons[0] self.case = toggle_buttons[1] self.whole_word = toggle_buttons[2] # Disable confirmation dialog for 'Replace all' button GPS.Preference("Ask-Confirmation-For-Replace-All").set(False)
def open_and_yield(self): """ Open the build targets editor dialog, and returns a handle to it. This is compatible with run_test_driver: editor = GPS.BuildTargetsEditor() yield editor.open_and_yield() """ yield self._open_and_yield('/Build/Settings/Targets') self.dialog = get_window_by_prefix('Target Configuration') self.tree = get_widgets_by_type(Gtk.TreeView, self.dialog)[0]
def open_and_yield(self): """ Compatible with run_test_driver, to be used in a yield statement view = Variables_View() yield view.open_and_yield() ... """ yield self._open_and_yield("open debugger variables window") GPS.execute_action("open debugger variables window") self.view = GPS.MDI.get("Variables") gps_not_null(self.view, "Variables view not found") self.tree = get_widgets_by_type(Gtk.TreeView, self.view.pywidget()) self.tree = self.tree[0]
def select_template(self, template): """ Select the given template in the assistant's left tree view and click on the 'Next' button. :param template: The label of the template to select """ tree = get_widgets_by_type(Gtk.TreeView, self.__assistant)[0] path = pygps.tree.find_in_tree(tree, 0, template) pygps.tree.click_in_tree(tree, path) get_button_from_label("Next", self.__assistant).clicked()
def open_and_yield(self): """ Compatible with run_test_driver, to be used in a yield statement view = Variables_View() yield view.open_and_yield() ... """ yield self._open_and_yield("open debugger variables window") GPS.execute_action("open debugger variables window") self.view = GPS.MDI.get("Debugger Variables") gps_not_null(self.view, "Variables view not found") self.tree = get_widgets_by_type(Gtk.TreeView, self.view.pywidget()) self.tree = self.tree[0]
def open_and_yield(self): preferences_dialog = Preferences() yield preferences_dialog.open_and_yield() preferences_dialog.select_page("Key Shortcuts") self.editor = get_widget_by_name('Key shortcuts') if self.editor: self.modify_button = get_button_from_label('Add', self.editor) self.remove_button = get_button_from_label('Remove', self.editor) self.close_button = get_button_from_label('Close') self.key_theme_combo = get_widgets_by_type(Gtk.ComboBoxText, self.editor)[0]
def _on_view_button_press(self, _, event): """React to a button_press on the view.""" if event.button == 3: gtkmenu = get_widgets_by_type(Gtk.MenuItem, None) item = None for l in gtkmenu: if l.get_label() == MENU_NAME: # ??? good way to find this item = l t = item.get_submenu() # On this button, raise the contextual menu t.popup(None, None, None, None, 3, 0) return False return False
def select_template(self, template): """ Select the given template in the assistant's left tree view and click on the 'Next' button. :param template: The label of the template to select """ tree = get_widgets_by_type(Gtk.TreeView, self.__assistant)[0] model = tree.get_model() path = pygps.tree.find_in_tree(tree, 0, template) pygps.tree.click_in_tree(tree, path) get_button_from_label("Next", self.__assistant).clicked()
def open_and_yield(self): """ Open the search dialog, and returns a handle to it. This is compatible with run_test_driver: editor = GPS.Search() yield editor.open_and_yield() """ yield self._open_and_yield("Search") self.dialog = get_window_by_prefix('GPS - Search -') if self.dialog: combos = get_widgets_by_type(Gtk.ComboBox, self.dialog) self.find = get_button_from_label("Find", self.dialog) self.find_all = get_button_from_label("Find All", self.dialog) self.next = self.find # This is in fact the same button self.replace = get_button_from_label("Replace", self.dialog) self.replace_all = get_button_from_label( "Replace All", self.dialog) self.close = get_button_from_label("Close", self.dialog) self.replace_and_find = get_button_from_label( "Replace & Find", self.dialog) self.scope = get_widget_by_name( "search scope combo", self.dialog) self.pattern = combos[0].get_child() self.replace_text = combos[1].get_child() self.look_in = combos[3] if len(combos) >= 4 else combos[2] self.previous = get_button_from_label("Previous", self.dialog) toggle_buttons = get_widgets_by_type(Gtk.ToggleButton, self.dialog) self.regexp = toggle_buttons[0] self.case = toggle_buttons[1] self.whole_word = toggle_buttons[2]
def open_and_yield(self): """ Open the build targets editor dialog, and returns a handle to it. This is compatible with run_test_driver: editor = GPS.BuildTargetsEditor() yield editor.open_and_yield() """ preferences_dialog = Preferences() yield preferences_dialog.open_and_yield() preferences_dialog.select_page("Build Targets") self.editor = get_widget_by_name("Build Targets Editor") self.notebook = pygps.get_widgets_by_type(Gtk.Notebook, self.editor)[0] self.close_button = get_button_from_label("Close", preferences_dialog.dialog) self.apply_button = get_button_from_label("Apply", preferences_dialog.dialog) self.tree = get_widgets_by_type(Gtk.TreeView, self.editor)[0]
def driver(): # Execute build & run GPS.execute_action("Build & Run Number 1") window = None # Wait until the main is running in the output window while not window: yield timeout(100) window = GPS.MDI.get("Run: hello" + (".exe" if sys.platform == "win32" else "")) view = pygps.get_widgets_by_type(Gtk.TextView, window.pywidget())[0] # Interrupt the task [t for t in GPS.Task.list() if t.name() == "Run Main"][0].interrupt() # Send a couple of Enter keys view.grab_focus() yield timeout(100) send_key_event(GDK_RETURN) yield timeout(100) send_key_event(GDK_RETURN)
def open_and_yield(self): yield self._open_and_yield(self.action_name) fields = get_widgets_by_type(Gtk.Entry, self.dialogs) self.filename = fields[0] self.line = fields[1]
def _goto_line_bound(beginning, extend_selection): """ Move the cursor in the current focus widget to various places in the line: * if beginning is True, move to the beginning of the line If the cursor is already in column 1, move to the first non-blank character on the line when in a GPS code editor. * else move to the end of the line """ from pygps import get_widgets_by_type widget = get_focused_widget() # When in a standard Gtk_Entry field: if isinstance(widget, Gtk.Entry): bounds = widget.get_selection_bounds() cursor_pos = widget.get_position() if bounds: start_pos = bounds[0] if bounds[1] == cursor_pos else bounds[1] else: start_pos = widget.get_position() end_pos = 0 if beginning else widget.get_text_length() if extend_selection: widget.select_region(start_pos, end_pos) else: widget.set_position(end_pos) return elif not isinstance(widget, Gtk.TextView): # We don't know how to handle these other widgets return else: # We are in a GPS code editor or standard Gtk.TextView ed = GPS.EditorBuffer.get(open=False) if ed: gtk_ed_view = get_widgets_by_type( Gtk.TextView, ed.current_view().pywidget())[0] if not ed or gtk_ed_view != widget: # in a Gtk.TextView, but not a GPS code editor b = widget.get_buffer() it = b.get_iter_at_mark(b.get_mark("insert")) if beginning: if it.get_line_index() == 0: # Already at beginning ? move to first non blank while it.get_char() in (u' ', u'\t'): it.forward_char() else: b.place_cursor(b.get_iter_at_line_offset(it.get_line(), 0)) else: it.forward_to_line_end() b.place_cursor(it) else: for c in ed.cursors(): d = c.mark().location() if beginning: if d.column() == 1: # Already at beginning ? move to first non blank while d.get_char() in (u' ', u'\t'): d = d.forward_char(1) else: d = d.beginning_of_line() else: d = d.end_of_line() c.move(d, extend_selection or ed.extend_existing_selection)
def open_and_yield(self): yield self._open_and_yield('/Edit/Preferences...') self.dialog = get_window_by_prefix('GNAT Studio - Preferences -') self.tree = get_widgets_by_type(Gtk.TreeView, self.dialog)[0]
def __init__(self): GPS.execute_action("open breakpoints editor") self.view = GPS.MDI.get("Breakpoints") self.list = get_widgets_by_type(Gtk.TreeView, self.view.pywidget()) gps_not_null(self.list, 'List of breakpoints not found') self.list = self.list[0]
def get_checkbox(self, name): return [ w for w in get_widgets_by_type(Gtk.CheckButton, self.dialogs) if w.get_label().startswith(name) ][0]
def text_view_from_location(loc): """Creates a Gtk.TextView from an EditorLocation""" return pygps.get_widgets_by_type( Gtk.TextView, loc.buffer().current_view().pywidget())[0]
def text_view_from_location(loc): """Creates a Gtk.TextView from an EditorLocation""" return pygps.get_widgets_by_type(Gtk.TextView, loc.buffer().current_view().pywidget())[0]
def open_and_yield(self): yield self._open_and_yield('open Commits') self.view = GPS.MDI.get("Commits") self.tree = get_widgets_by_type(Gtk.TreeView, self.view.pywidget())[0] self.msg = get_widgets_by_type(Gtk.TextView, self.view.pywidget())[0]
def open_and_yield(self): yield self._open_and_yield('/Edit/Preferences...') self.dialog = get_window_by_prefix('GPS - Preferences -') self.tree = get_widgets_by_type(Gtk.TreeView, self.dialog)[0]
def open_and_yield(self): GPS.execute_action("open Learn") self.dialog = GPS.MDI.get('Learn').pywidget() self.paned_view = get_widgets_by_type(Gtk.Paned, self.dialog)[0] self.doc_label = get_widgets_by_type(Gtk.Label, self.paned_view.get_child2())[0]
def _goto_line_bound(beginning, extend_selection): """ Move the cursor in the current focus widget to various places in the line: * if beginning is True, move to the beginning of the line If the cursor is already in column 1, move to the first non-blank character on the line when in a GPS code editor. * else move to the end of the line """ from pygps import get_widgets_by_type widget = get_focused_widget() # When in a standard Gtk_Entry field: if isinstance(widget, Gtk.Entry): bounds = widget.get_selection_bounds() cursor_pos = widget.get_position() if bounds: start_pos = bounds[0] if bounds[1] == cursor_pos else bounds[1] else: start_pos = widget.get_position() end_pos = 0 if beginning else widget.get_text_length() if extend_selection: widget.select_region(start_pos, end_pos) else: widget.set_position(end_pos) return elif not isinstance(widget, Gtk.TextView): # We don't know how to handle these other widgets return else: # We are in a GPS code editor or standard Gtk.TextView ed = GPS.EditorBuffer.get(open=False) if ed: gtk_ed_view = get_widgets_by_type(Gtk.TextView, ed.current_view().pywidget())[0] if not ed or gtk_ed_view != widget: # in a Gtk.TextView, but not a GPS code editor b = widget.get_buffer() it = b.get_iter_at_mark(b.get_mark("insert")) if beginning: if it.get_line_index() == 0: # Already at beginning ? move to first non blank while it.get_char() in (u' ', u'\t'): it.forward_char() else: b.place_cursor(b.get_iter_at_line_offset(it.get_line(), 0)) else: it.forward_to_line_end() b.place_cursor(it) else: for c in ed.cursors(): d = c.mark().location() if beginning: if d.column() == 1: # Already at beginning ? move to first non blank while d.get_char() in (u' ', u'\t'): d = d.forward_char(1) else: d = d.beginning_of_line() else: d = d.end_of_line() c.move(d, extend_selection or ed.extend_existing_selection)
def get_combobox_by_label(self, text): label = [label for label in get_widgets_by_type(Gtk.Label, self.dialog) if label.get_label() == text][0] return label.get_parent().get_children()[1]
def get_command_line_entry(self): return get_widgets_by_type(Gtk.Entry, self.dialog)[0]
def open_and_yield(self): yield self._open_and_yield('save as') labels = get_widgets_by_type(Gtk.Label, self.dialogs[0]) label = [x for x in labels if x.get_label() == '_Name:'][0] self.entry = label.get_parent().get_children()[0]