Exemplo n.º 1
0
def buttons(app):
    """constructor for the headerbar buttons

    Returns:
        [NamedTupple] -- tupple of Gtk.Buttons
    """

    Button = namedtuple("Button", "new open_recent save search menu")
    btn = Button(
        Gtk.Button().new_with_label(_("New")),
        Gtk.Box().new(0, 0),
        Gtk.Button().new_with_label(_("Save")),
        Gtk.Button().new_from_icon_name("system-search-symbolic",
                                        Gtk.IconSize.BUTTON),
        Gtk.MenuButton().new())

    builder_window_menu = get_builder('Menu')
    model = builder_window_menu.get_object("Menu")

    open_button = Gtk.Button().new_with_label(_("Open"))
    open_button.set_action_name("app.open")

    recents_builder = get_builder('Recents')
    recents = recents_builder.get_object("recent_md_popover")

    recents_treeview = get_descendant(recents, "recent_view", level=0)
    recents_treeview.set_activate_on_single_click(True)

    recents_wd = recents_builder.get_object("recent_md_widget")
    recents_wd.connect('item-activated', app.on_open_recent)

    recents_button = Gtk.MenuButton().new()
    recents_button.set_image(
        Gtk.Image.new_from_icon_name("pan-down-symbolic", Gtk.IconSize.BUTTON))
    recents_button.set_tooltip_text(_("Open Recent"))
    recents_button.set_popover(recents)

    btn.open_recent.get_style_context().add_class("linked")
    btn.open_recent.pack_start(open_button, False, False, 0)
    btn.open_recent.pack_end(recents_button, False, False, 0)

    btn.search.set_tooltip_text(_("Search and replace"))
    btn.menu.set_tooltip_text(_("Menu"))
    btn.menu.set_image(
        Gtk.Image.new_from_icon_name("open-menu-symbolic",
                                     Gtk.IconSize.BUTTON))
    btn.menu.set_use_popover(True)
    btn.menu.set_menu_model(model)
    btn.new.set_action_name("app.new")
    btn.save.set_action_name("app.save")
    btn.search.set_action_name("app.search")

    return btn
Exemplo n.º 2
0
def buttons(app):
    """constructor for the headerbar buttons

    Returns:
        [NamedTupple] -- tupple of Gtk.Buttons
    """

    builder_window_menu = get_builder('Menu')
    model = builder_window_menu.get_object("Menu")
    recents_builder = get_builder('Recents')
    recents = recents_builder.get_object("recent_md_popover")
    recents_wd = recents_builder.get_object("recent_md_widget")
    recents_wd.connect('item-activated', app.on_open_recent)

    Button = namedtuple("Button", "new open recent save search menu")
    btn = Button(
        Gtk.Button().new_with_label(_("New")),
        Gtk.Button().new_with_label(_("Open")),
        Gtk.MenuButton().new(),
        Gtk.Button().new_with_label(_("Save")),
        Gtk.Button().new_from_icon_name("system-search-symbolic",
                                        Gtk.IconSize.BUTTON),
        Gtk.MenuButton().new())

    btn.recent.set_image(
        Gtk.Image.new_from_icon_name("go-down-symbolic", Gtk.IconSize.BUTTON))
    btn.recent.set_tooltip_text(_("Open Recent"))
    btn.recent.set_popover(recents)
    btn.search.set_tooltip_text(_("Search and replace"))
    btn.menu.set_tooltip_text(_("Menu"))
    btn.menu.set_image(
        Gtk.Image.new_from_icon_name("open-menu-symbolic",
                                     Gtk.IconSize.BUTTON))
    btn.menu.set_use_popover(True)
    btn.menu.set_menu_model(model)
    btn.new.set_action_name("app.new")
    btn.open.set_action_name("app.open")
    btn.save.set_action_name("app.save")
    btn.search.set_action_name("app.search")

    return btn
    def __init__(self, filename):
        """Set up the about dialog"""
        self.builder = get_builder('Export')
        self.dialog = self.builder.get_object("Export")
        self.stack = self.builder.get_object("export_stack")
        self.stack_switcher = self.builder.get_object("format_switcher")

        stack_pdf_disabled = self.builder.get_object("pdf_disabled")
        filename = filename or _("Untitled document.md")

        self.filechoosers = {export_format:self.stack.get_child_by_name(export_format)\
                   for export_format in ["pdf", "html", "odt", "advanced"]}
        for export_format, filechooser in self.filechoosers.items():
            filechooser.set_do_overwrite_confirmation(True)
            filechooser.set_current_folder(os.path.dirname(filename))
            if export_format == "advanced":
                self.adv_export_name = self.builder.get_object(
                    "advanced_export_name")
                self.adv_export_name.set_text(os.path.basename(filename)[:-3])
            else:
                filechooser.set_current_name(
                    os.path.basename(filename)[:-2] + export_format)

        # Disable pdf if Texlive not installed
        texlive_installed = helpers.exist_executable("pdftex")

        if not texlive_installed:
            self.filechoosers["pdf"].set_visible(False)
            stack_pdf_disabled.set_visible(True)
            stack_pdf_disabled.set_text(disabled_text())
            stack_pdf_disabled.set_justify(Gtk.Justification.CENTER)
            self.stack.connect('notify', self.allow_export,
                               'visible_child_name')

        self.builder.get_object("highlight_style").set_active(0)

        format_store = Gtk.ListStore(int, str)
        for fmt_id in self.formats_dict:
            format_store.append([fmt_id, self.formats_dict[fmt_id]["name"]])
        self.format_field = self.builder.get_object('choose_format')
        self.format_field.set_model(format_store)

        format_renderer = Gtk.CellRendererText()
        self.format_field.pack_start(format_renderer, True)
        self.format_field.add_attribute(format_renderer, "text", 1)
        self.format_field.set_active(0)
Exemplo n.º 4
0
    def finish_initializing(self, builder):  # pylint: disable=E1002
        """Set up the main window"""

        super(UberwriterWindow, self).finish_initializing(builder)

        # preferences
        self.settings = Settings.new()
        self.builder = builder

        self.connect('save-file', self.save_document)
        self.connect('save-file-as', self.save_document_as)
        self.connect('new-file', self.new_document)
        self.connect('open-file', self.open_document)
        self.connect('close-window', self.on_mnu_close_activate)
        self.scroll_adjusted = False

        # Code for other initialization actions should be added here.

        # Texlive checker
        self.texlive_installed = False

        self.set_name('UberwriterWindow')

        # Headerbars
        self.hb_container = Gtk.Frame(name='titlebar_container')
        self.hb_container.set_shadow_type(Gtk.ShadowType.NONE)
        self.hb_revealer = Gtk.Revealer(name='titlebar_revealer')
        self.hb = Gtk.HeaderBar()
        self.hb_revealer.add(self.hb)
        self.hb_revealer.props.transition_duration = 1000
        self.hb_revealer.props.transition_type = Gtk.RevealerTransitionType.CROSSFADE
        self.hb.props.show_close_button = True
        self.hb.get_style_context().add_class("titlebar")
        self.hb_container.add(self.hb_revealer)
        self.hb_container.show()
        self.set_titlebar(self.hb_container)
        self.hb_revealer.show()
        self.hb_revealer.set_reveal_child(True)
        self.hb.show()

        btn_new = Gtk.Button().new_with_label(_("New"))
        btn_open = Gtk.Button().new_with_label(_("Open"))
        btn_recent = Gtk.MenuButton().new()
        btn_recent.set_image(
            Gtk.Image.new_from_icon_name("go-down-symbolic",
                                         Gtk.IconSize.BUTTON))
        btn_recent.set_tooltip_text(_("Open Recent"))
        btn_recent.set_popup(self.generate_recent_files_menu())
        btn_save = Gtk.Button().new_with_label(_("Save"))
        btn_search = Gtk.Button().new_from_icon_name("system-search-symbolic",
                                                     Gtk.IconSize.BUTTON)
        btn_search.set_tooltip_text(_("Search and replace"))
        btn_menu = Gtk.MenuButton().new()
        btn_menu.set_tooltip_text(_("Menu"))
        btn_menu.set_image(
            Gtk.Image.new_from_icon_name("open-menu-symbolic",
                                         Gtk.IconSize.BUTTON))

        btn_new.set_action_name("app.new")
        btn_open.set_action_name("app.open")
        btn_recent.set_action_name("app.open_recent")
        btn_save.set_action_name("app.save")
        btn_search.set_action_name("app.search")

        btn_menu.set_use_popover(True)
        self.builder_window_menu = get_builder('Menu')
        self.model = self.builder_window_menu.get_object("Menu")
        btn_menu.set_menu_model(self.model)

        self.hb.pack_start(btn_new)
        self.hb.pack_start(btn_open)
        self.hb.pack_start(btn_recent)
        self.hb.pack_end(btn_menu)
        self.hb.pack_end(btn_search)
        self.hb.pack_end(btn_save)
        self.hb.show_all()

        # same for fullscreen headerbar
        # TODO: Refactorice: this is duplicated code!

        self.fullscr_events = self.builder.get_object("FullscreenEventbox")
        self.fullscr_hb_revealer = self.builder.get_object(
            "FullscreenHbPlaceholder")
        self.fullscr_hb = self.builder.get_object("FullscreenHeaderbar")
        self.fullscr_hb.get_style_context().add_class("titlebar")
        self.fullscr_hb_revealer.show()
        self.fullscr_hb_revealer.set_reveal_child(False)
        self.fullscr_hb.show()
        self.fullscr_events.hide()

        fs_btn_new = Gtk.Button().new_with_label(_("New"))
        fs_btn_open = Gtk.Button().new_with_label(_("Open"))
        self.fs_btn_recent = Gtk.MenuButton().new()
        self.fs_btn_recent.set_tooltip_text(_("Open Recent"))
        self.fs_btn_recent.set_image(
            Gtk.Image.new_from_icon_name("go-down-symbolic",
                                         Gtk.IconSize.BUTTON))
        self.fs_btn_recent.set_tooltip_text(_("Open Recent"))
        self.fs_btn_recent.set_popup(self.generate_recent_files_menu())
        fs_btn_save = Gtk.Button().new_with_label(_("Save"))
        fs_btn_search = Gtk.Button().new_from_icon_name(
            "system-search-symbolic", Gtk.IconSize.BUTTON)
        fs_btn_search.set_tooltip_text(_("Search and replace"))
        self.fs_btn_menu = Gtk.MenuButton().new()
        self.fs_btn_menu.set_tooltip_text(_("Menu"))
        self.fs_btn_menu.set_image(
            Gtk.Image.new_from_icon_name("open-menu-symbolic",
                                         Gtk.IconSize.BUTTON))
        fs_btn_exit = Gtk.Button().new_from_icon_name("view-restore-symbolic",
                                                      Gtk.IconSize.BUTTON)
        fs_btn_exit.set_tooltip_text(_("Exit Fullscreen"))

        fs_btn_new.set_action_name("app.new")
        fs_btn_open.set_action_name("app.open")
        self.fs_btn_recent.set_action_name("app.open_recent")
        fs_btn_save.set_action_name("app.save")
        fs_btn_search.set_action_name("app.search")
        fs_btn_exit.set_action_name("app.fullscreen")

        self.fs_btn_menu.set_use_popover(True)
        self.builder_window_menu = get_builder('Menu')
        self.model = self.builder_window_menu.get_object("Menu")
        self.fs_btn_menu.set_menu_model(self.model)

        self.fullscr_hb.pack_start(fs_btn_new)
        self.fullscr_hb.pack_start(fs_btn_open)
        self.fullscr_hb.pack_start(self.fs_btn_recent)
        self.fullscr_hb.pack_end(fs_btn_exit)
        self.fullscr_hb.pack_end(self.fs_btn_menu)
        self.fullscr_hb.pack_end(fs_btn_search)
        self.fullscr_hb.pack_end(fs_btn_save)
        self.fullscr_hb.show_all()
        # this is a little tricky
        # we show hb when the cursor enters an area of 1 px at the top of the window
        # as the hb is shown the height of the eventbox grows to accomodate it
        self.fullscr_events.connect('enter_notify_event', self.show_fs_hb)
        self.fullscr_events.connect('leave_notify_event', self.hide_fs_hb)
        self.fs_btn_menu.get_popover().connect('closed', self.hide_fs_hb)

        self.title_end = "  –  UberWriter"
        self.set_headerbar_title("New File" + self.title_end)

        self.focusmode = False

        self.word_count = builder.get_object('word_count')
        self.char_count = builder.get_object('char_count')

        # Setup status bar hide after 3 seconds

        self.status_bar = builder.get_object('status_bar_box')
        self.statusbar_revealer = builder.get_object('status_bar_revealer')
        self.status_bar.get_style_context().add_class('status_bar_box')
        self.status_bar_visible = True
        self.was_motion = True
        self.buffer_modified_for_status_bar = False
        self.connect("motion-notify-event", self.on_motion_notify)
        GObject.timeout_add(3000, self.poll_for_motion)

        self.accel_group = Gtk.AccelGroup()
        self.add_accel_group(self.accel_group)

        # Setup light background
        self.TextEditor = TextEditor()
        self.TextEditor.set_name('UberwriterEditor')
        self.get_style_context().add_class('uberwriter_window')

        base_leftmargin = 100
        self.TextEditor.set_left_margin(base_leftmargin)
        self.TextEditor.set_left_margin(40)
        self.TextEditor.set_top_margin(80)
        self.TextEditor.props.width_request = 600
        self.TextEditor.props.halign = Gtk.Align.CENTER
        self.TextEditor.set_vadjustment(builder.get_object('vadjustment1'))
        self.TextEditor.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
        self.TextEditor.connect('focus-out-event', self.focus_out)
        self.TextEditor.get_style_context().connect('changed',
                                                    self.style_changed)

        # self.TextEditor.install_style_property_parser

        self.TextEditor.show()
        self.TextEditor.grab_focus()

        self.EditorAlignment = builder.get_object('editor_alignment')
        self.ScrolledWindow = builder.get_object('editor_scrolledwindow')
        self.ScrolledWindow.props.width_request = 600
        self.ScrolledWindow.add(self.TextEditor)
        self.alignment_padding = 40
        self.EditorViewport = builder.get_object('editor_viewport')
        self.ScrolledWindow.connect_after("draw", self.draw_gradient)

        self.smooth_scroll_starttime = 0
        self.smooth_scroll_endtime = 0
        self.smooth_scroll_acttarget = 0
        self.smooth_scroll_data = {
            'target_pos': -1,
            'source_pos': -1,
            'duration': 0
        }
        self.smooth_scroll_tickid = -1

        self.PreviewPane = builder.get_object('preview_scrolledwindow')

        self.TextEditor.set_top_margin(80)
        self.TextEditor.set_bottom_margin(16)

        self.TextEditor.set_pixels_above_lines(4)
        self.TextEditor.set_pixels_below_lines(4)
        self.TextEditor.set_pixels_inside_wrap(8)

        tab_array = Pango.TabArray.new(1, True)
        tab_array.set_tab(0, Pango.TabAlign.LEFT, 20)
        self.TextEditor.set_tabs(tab_array)

        self.TextBuffer = self.TextEditor.get_buffer()
        self.TextBuffer.set_text('')

        # Init Window height for top/bottom padding
        self.window_height = self.get_size()[1]

        self.text_change_event = self.TextBuffer.connect(
            'changed', self.text_changed)

        # Init file name with None
        self.set_filename()

        self.style_provider = Gtk.CssProvider()
        self.style_provider.load_from_path(helpers.get_media_path('style.css'))

        Gtk.StyleContext.add_provider_for_screen(
            Gdk.Screen.get_default(), self.style_provider,
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

        # Markup and Shortcuts for the TextBuffer
        self.MarkupBuffer = MarkupBuffer(self, self.TextBuffer,
                                         base_leftmargin)
        self.MarkupBuffer.markup_buffer()

        # Setup dark mode if so
        if self.settings.get_value("dark-mode"):
            self.dark_mode_toggled(True)

        # Scrolling -> Dark or not?
        self.textchange = False
        self.scroll_count = 0
        self.timestamp_last_mouse_motion = 0
        self.TextBuffer.connect_after('mark-set', self.mark_set)

        # Drag and drop

        # self.TextEditor.drag_dest_unset()
        # self.TextEditor.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)
        self.target_list = Gtk.TargetList.new([])
        self.target_list.add_uri_targets(1)
        self.target_list.add_text_targets(2)

        self.TextEditor.drag_dest_set_target_list(self.target_list)
        self.TextEditor.connect_after('drag-data-received',
                                      self.on_drag_data_received)

        def on_drop(widget, *args):
            print("drop")

        self.TextEditor.connect('drag-drop', on_drop)

        self.TextBuffer.connect('paste-done', self.paste_done)
        # self.connect('key-press-event', self.alt_mod)

        # Events for Typewriter mode

        # Setting up inline preview
        self.InlinePreview = UberwriterInlinePreview(self.TextEditor,
                                                     self.TextBuffer)

        # Vertical scrolling
        self.vadjustment = self.ScrolledWindow.get_vadjustment()
        self.vadjustment.connect('value-changed', self.scrolled)

        # Setting up spellcheck
        self.auto_correct = None
        try:
            self.SpellChecker = SpellChecker(self.TextEditor,
                                             locale.getdefaultlocale()[0],
                                             collapse=False)
            if self.auto_correct:
                self.auto_correct.set_language(self.SpellChecker.language)
                self.SpellChecker.connect_language_change(
                    self.auto_correct.set_language)

            self.spellcheck = True
        except:
            self.SpellChecker = None
            self.spellcheck = False

        if self.spellcheck:
            self.SpellChecker.append_filter('[#*]+', SpellChecker.FILTER_WORD)

        self.did_change = False

        ###
        #   Sidebar initialization test
        ###
        self.paned_window = builder.get_object("main_pained")
        self.sidebar_box = builder.get_object("sidebar_box")
        self.sidebar = UberwriterSidebar(self)
        self.sidebar_box.hide()

        ###
        #   Search and replace initialization
        #   Same interface as Sidebar ;)
        ###
        self.searchreplace = UberwriterSearchAndReplace(self)

        # Window resize
        self.window_resize(self)
        self.connect("configure-event", self.window_resize)
        self.connect("delete-event", self.on_delete_called)
Exemplo n.º 5
0
    def __init__(self, app):
        """Set up the main window"""

        Gtk.ApplicationWindow.__init__(
            self,
            application=Gio.Application.get_default(),
            title="Uberwriter")

        self.builder = get_builder('UberwriterWindow')
        self.add(self.builder.get_object("FullscreenOverlay"))

        self.set_default_size(850, 500)

        # preferences
        self.settings = Settings.new()

        self.set_name('UberwriterWindow')

        # Headerbars
        self.headerbar = headerbars.MainHeaderbar(app)
        self.set_titlebar(self.headerbar.hb_container)
        self.fs_headerbar = headerbars.FsHeaderbar(self.builder, app)

        self.title_end = "  –  UberWriter"
        self.set_headerbar_title("New File" + self.title_end)

        self.focusmode = False

        self.word_count = self.builder.get_object('word_count')
        self.char_count = self.builder.get_object('char_count')

        # Setup status bar hide after 3 seconds

        self.status_bar = self.builder.get_object('status_bar_box')
        self.statusbar_revealer = self.builder.get_object(
            'status_bar_revealer')
        self.status_bar.get_style_context().add_class('status_bar_box')
        self.status_bar_visible = True
        self.was_motion = True
        self.buffer_modified_for_status_bar = False

        if self.settings.get_value("poll-motion"):
            self.connect("motion-notify-event", self.on_motion_notify)
            GObject.timeout_add(3000, self.poll_for_motion)

        self.accel_group = Gtk.AccelGroup()
        self.add_accel_group(self.accel_group)

        # Setup light background
        self.text_editor = TextEditor()
        self.text_editor.set_name('UberwriterEditor')
        self.get_style_context().add_class('uberwriter_window')

        base_leftmargin = 100
        self.text_editor.set_left_margin(base_leftmargin)
        self.text_editor.set_left_margin(40)
        self.text_editor.set_top_margin(80)
        self.text_editor.props.width_request = 600
        self.text_editor.props.halign = Gtk.Align.CENTER
        self.text_editor.set_vadjustment(
            self.builder.get_object('vadjustment1'))
        self.text_editor.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
        self.text_editor.connect('focus-out-event', self.focus_out)
        self.text_editor.get_style_context().connect('changed',
                                                     self.style_changed)

        self.text_editor.set_top_margin(80)
        self.text_editor.set_bottom_margin(16)

        self.text_editor.set_pixels_above_lines(4)
        self.text_editor.set_pixels_below_lines(4)
        self.text_editor.set_pixels_inside_wrap(8)

        tab_array = Pango.TabArray.new(1, True)
        tab_array.set_tab(0, Pango.TabAlign.LEFT, 20)
        self.text_editor.set_tabs(tab_array)

        self.text_editor.show()
        self.text_editor.grab_focus()

        self.editor_alignment = self.builder.get_object('editor_alignment')
        self.scrolled_window = self.builder.get_object('editor_scrolledwindow')
        self.scrolled_window.props.width_request = 600
        self.scrolled_window.add(self.text_editor)
        self.alignment_padding = 40
        self.editor_viewport = self.builder.get_object('editor_viewport')

        # some people seems to have performance problems with the overlay.
        # Let them disable it

        if self.settings.get_value("gradient-overlay"):
            self.overlay = self.scrolled_window.connect_after(
                "draw", self.draw_gradient)

        self.smooth_scroll_starttime = 0
        self.smooth_scroll_endtime = 0
        self.smooth_scroll_acttarget = 0
        self.smooth_scroll_data = {
            'target_pos': -1,
            'source_pos': -1,
            'duration': 0
        }
        self.smooth_scroll_tickid = -1

        self.text_buffer = self.text_editor.get_buffer()
        self.text_buffer.set_text('')

        # Init Window height for top/bottom padding
        self.window_height = self.get_size()[1]

        self.text_change_event = self.text_buffer.connect(
            'changed', self.text_changed)

        # Init file name with None
        self.set_filename()

        # self.style_provider = Gtk.CssProvider()
        # self.style_provider.load_from_path(helpers.get_media_path('arc_style.css'))

        # Gtk.StyleContext.add_provider_for_screen(
        #     Gdk.Screen.get_default(), self.style_provider,
        #     Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
        # )

        # Markup and Shortcuts for the TextBuffer
        self.markup_buffer = MarkupBuffer(self, self.text_buffer,
                                          base_leftmargin)
        self.markup_buffer.markup_buffer()

        # Setup dark mode if so
        self.toggle_dark_mode(self.settings.get_value("dark-mode"))

        # Scrolling -> Dark or not?
        self.textchange = False
        self.scroll_count = 0
        self.timestamp_last_mouse_motion = 0
        self.text_buffer.connect_after('mark-set', self.mark_set)

        # Drag and drop

        # self.TextEditor.drag_dest_unset()
        # self.TextEditor.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)
        self.target_list = Gtk.TargetList.new([])
        self.target_list.add_uri_targets(1)
        self.target_list.add_text_targets(2)

        self.text_editor.drag_dest_set_target_list(self.target_list)
        self.text_editor.connect_after('drag-data-received',
                                       self.on_drag_data_received)

        def on_drop(_widget, *_args):
            print("drop")

        self.text_editor.connect('drag-drop', on_drop)

        self.text_buffer.connect('paste-done', self.paste_done)
        # self.connect('key-press-event', self.alt_mod)

        # Events for Typewriter mode

        # Setting up inline preview
        self.inline_preview = UberwriterInlinePreview(self.text_editor,
                                                      self.text_buffer)

        # Vertical scrolling
        self.vadjustment = self.scrolled_window.get_vadjustment()
        self.vadjustment.connect('value-changed', self.scrolled)

        # Setting up spellcheck
        self.auto_correct = None
        self.toggle_spellcheck(self.settings.get_value("spellcheck"))
        self.did_change = False

        ###
        #   Sidebar initialization test
        ###
        self.paned_window = self.builder.get_object("main_pained")
        self.sidebar_box = self.builder.get_object("sidebar_box")
        self.sidebar = UberwriterSidebar(self)
        self.sidebar_box.hide()

        ###
        #   Search and replace initialization
        #   Same interface as Sidebar ;)
        ###
        self.searchreplace = UberwriterSearchAndReplace(self)

        # Window resize
        self.window_resize(self)
        self.connect("configure-event", self.window_resize)
        self.connect("delete-event", self.on_delete_called)