<commit_before># Copyright 2008-2009 <NAME>
#
# This file is part of Reinteract and distributed under the terms
# of the BSD license. See the file COPYING in the Reinteract
# distribution for full details.
#
########################################################################

import os
import re
import sys

import gtk

from application import application
from base_window import BaseWindow
from gc_utils import gc_at_idle
from library_editor import LibraryEditor
from notebook import LibraryFile, NotebookFile, WorksheetFile
from window_builder import WindowBuilder
from worksheet_editor import WorksheetEditor

class BaseNotebookWindow(BaseWindow):
    def __init__(self, notebook):
        BaseWindow.__init__(self, notebook)

        self.state = application.state.get_notebook_state(notebook.folder)
        # We'll call window.set_default_size() later with an appropriate
        # default size for the BaseNotebookWindow subclass. The size set by
        # window.resize() takes precedence.
        (width, height) = self.state.get_size()
        if width != -1 and height != -1:
            self.window.resize(width, height)
        self.window.connect('configure-event', self.on_configure_event)

        self.path = notebook.folder

        self.editors = []

        self.nb_widget = gtk.Notebook()
        self.nb_widget.connect_after('switch-page', self.on_page_switched)
        self.nb_widget.connect('page-reordered', self.on_page_reordered)

        self._fill_content()

        self.main_vbox.show_all()

        self.__initial_editor = None

        open_file_paths = self.state.get_open_files()
        current_file = self.state.get_current_file()

        for path in open_file_paths:
            if not path in self.notebook.files:
                continue

            file = self.notebook.files[path]
            self.open_file(file)

        current_file_editor = None
        if current_file is not None:
            filename = os.path.join(notebook.folder, current_file)
            for editor in self.editors:
                if editor.filename == filename:
                    current_file_editor = editor

        if current_file_editor is None and len(self.editors) > 0:
            current_file_editor = self.editors[0]

        if current_file_editor is not None:
            self._make_editor_current(current_file_editor)
            current_file_editor.view.grab_focus()

        self.__update_title()

    #######################################################
    # Implemented by subclasses
    #######################################################

    def _fill_contents(self, editor):
        raise NotImplementedError()

    def _add_editor(self, editor):
        self.editors.append(editor)
        self.nb_widget.add(editor.widget)
        editor.widget._notebook_window_editor = editor
        editor.connect('notify::title', self.on_editor_notify_title)
        editor.connect('filename-changed', self.on_editor_filename_changed)
        editor.connect('notify::modified', self.on_editor_notify_modified)
        editor.connect('notify::state', self.on_editor_notify_state)

        self._update_editor_title(editor)
        self._update_editor_state(editor)
        self._update_open_files()

    def _close_editor(self, editor):
        if not editor.confirm_discard():
            return

        if editor == self.current_editor:
            # Either we'll switch page and a new editor will be set, or we have no pages left
            self.current_editor = None

        if editor == self.__initial_editor:
            self.__initial_editor = None

        self.editors.remove(editor)
        editor.widget._notebook_window_editor = None
        editor.close()
        self.__update_title()
        self._update_open_files()
        self.update_sensitivity()

        gc_at_idle()

    def _update_editor_state(self, editor):
        self.update_sensitivity()

    def _update_editor_title(self, editor):
        if editor == self.current_editor:
            self.__update_title()

    #######################################################
    # Overrides
    #######################################################

    def _add_actions(self, action_group):
        BaseWindow._add_actions(self, action_group)

        action_group.add_actions([
            ('notebook-properties', gtk.STOCK_PROPERTIES, "Notebook Prop_erties", None,         None, self.on_notebook_properties),
            ('new-worksheet',       gtk.STOCK_NEW,        "_New Worksheet",       "<control>n", None, self.on_new_worksheet),
            ('new-library',         gtk.STOCK_NEW,        "New _Library",         "",           None, self.on_new_library),
            ('calculate-all',       gtk.STOCK_REFRESH,    "Calculate _All",       "<control><alt>Return",  None, self.on_calculate_all),
            ('switch-tab-1',        None,                 None,                   "<alt>1",     None, self.on_switch_tab),
            ('switch-tab-2',        None,                 None,                   "<alt>2",     None, self.on_switch_tab),
            ('switch-tab-3',        None,                 None,                   "<alt>3",     None, self.on_switch_tab),
            ('switch-tab-4',        None,                 None,                   "<alt>4",     None, self.on_switch_tab),
            ('switch-tab-5',        None,                 None,                   "<alt>5",     None, self.on_switch_tab),
            ('switch-tab-6',        None,                 None,                   "<alt>6",     None, self.on_switch_tab),
            ('switch-tab-7',        None,                 None,                   "<alt>7",     None, self.on_switch_tab),
            ('switch-tab-8',        None,                 None,                   "<alt>8",     None, self.on_switch_tab),
            ('switch-tab-9',        None,                 None,                   "<alt>9",     None, self.on_switch_tab),
            ('switch-tab-10',       None,                 None,                   "<alt>0",     None, self.on_switch_tab)
        ])

    def _close_current(self):
        if self.current_editor:
            self._close_editor(self.current_editor)

    def _close_window(self, confirm_discard):
        if confirm_discard and not self._confirm_discard():
            return

        # Prevent visual artifacts by hiding first
        self.window.hide()
        for editor in self.editors:
            editor.close()

        BaseWindow._close_window(self, confirm_discard)

    #######################################################
    # Utility
    #######################################################

    def _make_editor_current(self, editor):
        self.nb_widget.set_current_page(self.nb_widget.page_num(editor.widget))

    def __close_initial_editor(self):
        if self.__initial_editor and not self.__initial_editor.filename and not self.__initial_editor.modified:
            self._close_editor(self.__initial_editor)
            self.__initial_editor = None

    def __new_worksheet(self):<commit_msg>Fix for closing editors<commit_after># Copyright 2008-2009 <NAME>
#
# This file is part of Reinteract and distributed under the terms
# of the BSD license. See the file COPYING in the Reinteract
# distribution for full details.
#
########################################################################

import os
import re
import sys

import gtk

from application import application
from base_window import BaseWindow
from gc_utils import gc_at_idle
from library_editor import LibraryEditor
from notebook import LibraryFile, NotebookFile, WorksheetFile
from window_builder import WindowBuilder
from worksheet_editor import WorksheetEditor

class BaseNotebookWindow(BaseWindow):
    def __init__(self, notebook):
        BaseWindow.__init__(self, notebook)

        self.state = application.state.get_notebook_state(notebook.folder)
        # We'll call window.set_default_size() later with an appropriate
        # default size for the BaseNotebookWindow subclass. The size set by
        # window.resize() takes precedence.
        (width, height) = self.state.get_size()
        if width != -1 and height != -1:
            self.window.resize(width, height)
        self.window.connect('configure-event', self.on_configure_event)

        self.path = notebook.folder

        self.editors = []

        self.nb_widget = gtk.Notebook()
        self.nb_widget.connect_after('switch-page', self.on_page_switched)
        self.nb_widget.connect('page-reordered', self.on_page_reordered)

        self._fill_content()

        self.main_vbox.show_all()

        self.__initial_editor = None

        open_file_paths = self.state.get_open_files()
        current_file = self.state.get_current_file()

        for path in open_file_paths:
            if not path in self.notebook.files:
                continue

            file = self.notebook.files[path]
            self.open_file(file)

        current_file_editor = None
        if current_file is not None:
            filename = os.path.join(notebook.folder, current_file)
            for editor in self.editors:
                if editor.filename == filename:
                    current_file_editor = editor

        if current_file_editor is None and len(self.editors) > 0:
            current_file_editor = self.editors[0]

        if current_file_editor is not None:
            self._make_editor_current(current_file_editor)
            current_file_editor.view.grab_focus()

        self.__update_title()

    #######################################################
    # Implemented by subclasses
    #######################################################

    def _fill_contents(self, editor):
        raise NotImplementedError()

    def _add_editor(self, editor):
        self.editors.append(editor)
        self.nb_widget.add(editor.widget)
        editor.widget._notebook_window_editor = editor
        editor.connect('notify::title', self.on_editor_notify_title)
        editor.connect('filename-changed', self.on_editor_filename_changed)
        editor.connect('notify::modified', self.on_editor_notify_modified)
        editor.connect('notify::state', self.on_editor_notify_state)

        self._update_editor_title(editor)
        self._update_editor_state(editor)
        self._update_open_files()

    def _close_editor(self, editor):
        if not editor.confirm_discard():
            return

        if editor == self.current_editor:
            # Either we'll switch page and a new editor will be set, or we have no pages left
            self.current_editor = None

        if editor == self.__initial_editor:
            self.__initial_editor = None

        self.editors.remove(editor)
        editor.widget._notebook_window_editor = None
        editor.close()
        self.__update_title()
        self._update_open_files()
        self.update_sensitivity()

        gc_at_idle()

    def _update_editor_state(self, editor):
        self.update_sensitivity()

    def _update_editor_title(self, editor):
        if editor == self.current_editor:
            self.__update_title()

    #######################################################
    # Overrides
    #######################################################

    def _add_actions(self, action_group):
        BaseWindow._add_actions(self, action_group)

        action_group.add_actions([
            ('notebook-properties', gtk.STOCK_PROPERTIES, "Notebook Prop_erties", None,         None, self.on_notebook_properties),
            ('new-worksheet',       gtk.STOCK_NEW,        "_New Worksheet",       "<control>n", None, self.on_new_worksheet),
            ('new-library',         gtk.STOCK_NEW,        "New _Library",         "",           None, self.on_new_library),
            ('calculate-all',       gtk.STOCK_REFRESH,    "Calculate _All",       "<control><alt>Return",  None, self.on_calculate_all),
            ('switch-tab-1',        None,                 None,                   "<alt>1",     None, self.on_switch_tab),
            ('switch-tab-2',        None,                 None,                   "<alt>2",     None, self.on_switch_tab),
            ('switch-tab-3',        None,                 None,                   "<alt>3",     None, self.on_switch_tab),
            ('switch-tab-4',        None,                 None,                   "<alt>4",     None, self.on_switch_tab),
            ('switch-tab-5',        None,                 None,                   "<alt>5",     None, self.on_switch_tab),
            ('switch-tab-6',        None,                 None,                   "<alt>6",     None, self.on_switch_tab),
            ('switch-tab-7',        None,                 None,                   "<alt>7",     None, self.on_switch_tab),
            ('switch-tab-8',        None,                 None,                   "<alt>8",     None, self.on_switch_tab),
            ('switch-tab-9',        None,                 None,                   "<alt>9",     None, self.on_switch_tab),
            ('switch-tab-10',       None,                 None,                   "<alt>0",     None, self.on_switch_tab)
        ])

    def _close_current(self):
        if self.current_editor:
            self._close_editor(self.current_editor)

    def _close_window(self, confirm_discard):
        if confirm_discard and not self._confirm_discard():
            return

        # Prevent visual artifacts by hiding first
        self.window.hide()
        for editor in self.editors:
            editor.widget._notebook_window_editor = None
            editor.close()

        BaseWindow._close_window(self, confirm_discard)

    #######################################################
    # Utility
    #######################################################

    def _make_editor_current(self, editor):
        self.nb_widget.set_current_page(self.nb_widget.page_num(editor.widget))

    def __close_initial_editor(self):
        if self.__initial_editor and not self.__initial_editor.filename and not self.__initial_editor.modified:
            self._close_editor(self.__initial_editor)
            self.__initial_editor = None

    def __new_worksheet(self):
{'fraction_non_alphanumeric': 0.14418670239018544, 'fraction_numerical': 0.004653458365649016, 'mean_word_length': 2.2599402436221556, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1870486', 'n_tokens_mistral': 3879, 'n_tokens_neox': 3749, 'n_words': 798}