Ejemplo n.º 1
0
class DictionaryFrame(BookFrame):
    id = N_("Dictionary")

    def __init__(self, parent, book):
        self.dict = wx.Panel(parent)
        self.dictsplitter = wx.SplitterWindow(self.dict)

        super(DictionaryFrame, self).__init__(self.dictsplitter)
        self.SetBook(book)
        self.book.observers += self.dictionary_version_changed
        parent.on_close += lambda: \
         self.book.observers.remove(
          self.dictionary_version_changed
         )

        self.dictionary_list = DictionarySelector(self.dictsplitter, book)
        self.dictionary_list_index = -1
        self.dictionary_list.item_changed_observers += self.list_item_changed
        s = wx.BoxSizer(wx.HORIZONTAL)
        self.dictsplitter.SetSashGravity(1)
        self.dictsplitter.SplitVertically(self, self.dictionary_list)

        self.dictsplitter.SetMinimumPaneSize(100)

        def set_splitter():
            self.dictsplitter.SetSashPosition(self.dictsplitter.Size[0] - 130)

        wx.CallAfter(set_splitter)

        s.Add(self.dictsplitter, 1, wx.GROW)
        self.dict.SetSizer(s)

    def list_item_changed(self, event=None, suppress_reference_change=False):
        # Only force everything to reload if the list index has changed.
        dictionary_list_index = self.dictionary_list.list.GetFirstSelected()
        index_changed = (dictionary_list_index != self.dictionary_list_index)
        self.dictionary_list_index = dictionary_list_index
        if index_changed and not suppress_reference_change:
            self.UpdateUI()
            # If the user is in the middle of typing, then we don't want to steal focus from them.
            # Otherwise, we need to steal focus to fix the list problem.
            self.ForceKillFocus()
            if self.dictionary_list.item_to_focus_on is not self.dictionary_list.text_entry:
                self.SetFocus()
            #if self.dictionary_list.item_to_focus_on:
            #	self.ForceKillFocus()

    def chapter_move(self, amount):
        mod = self.book.mod
        if not mod:
            return

        key = mod.getKey()
        key.Persist(1)
        key.setText(to_str(self.reference, mod))
        mod.setKey(key)
        mod.increment(amount)
        ref = to_unicode(mod.getKeyText(), mod)
        self.notify(ref, source=events.CHAPTER_MOVE)

    def update_title(self, shown=None):
        m = guiconfig.mainfrm
        p = m.get_pane_for_frame(self)
        version = self.book.version
        ref = self.reference
        ref = self.format_ref(self.book.mod, ref)

        text = u"%s - %s (%s)" % (self.title, ref, version)
        m.set_pane_title(p.name, text)

    def format_ref(self, module, ref):
        return self.book.format_ref(module, ref)

    def get_window(self):
        return self.dict

    def dictionary_version_changed(self, newversion):
        freeze_ui = guiutil.FreezeUI(self.dictionary_list)
        self.dictionary_list.set_book(self.book)
        self.dictionary_list_index = -1

    def notify(self, ref, source=None, settings_changed=False):
        self.UpdateUI(ref)

    def UpdateUI(self, ref=""):
        if not ref:
            ref = self.dictionary_list.GetValue().upper()
        else:
            self.dictionary_list.choose_item(ref, update_text_entry_value=True)

        self.SetReference(ref)

    @guiutil.frozen
    def SetReference(self, ref, settings_changed=False):
        if not ref:
            # Initially, we start with no reference.
            # Since this causes an exception in the protocol handler,
            # we just force it to be the first available topic.
            topics = self.book.GetTopics()
            if topics:
                ref = topics[0].upper()

        super(DictionaryFrame,
              self).SetReference(ref, settings_changed=settings_changed)

    def SetReference_from_string(self, string):
        wx.CallAfter(self.UpdateUI, string)

    def get_reference_textbox(self):
        return self.dictionary_list.text_entry

    def current_segment_changed(self, new_segment_ref):
        # If the current segment changes as a result of the user typing in
        # the text box, we want to leave the text as they typed it rather
        # than replacing it with the full name of the current topic.
        if wx.Window.FindFocus() == self.dictionary_list.text_entry.text:
            return

        self.dictionary_list.choose_item(new_segment_ref,
                                         update_text_entry_value=True,
                                         suppress_reference_change=True)
        self.reference = new_segment_ref
        self.update_title()