Exemplo n.º 1
0
    def _HandleEncodingError(self, control):
        """Handle trying to reload the file the file with a different encoding
        Until it succeeds or gives up.
        @param control: stc
        @return: bool

        """
        # Loop while the load fails prompting to try a new encoding
        tried = None
        fname = control.GetFileName().strip(os.sep)
        fname = fname.split(os.sep)[-1]
        while True:
            doc = control.GetDocument()
            doc.ClearLastError()
            if tried is None:
                enc = doc.GetEncoding()
                if enc is None:
                    enc = ed_txt.DEFAULT_ENCODING
            else:
                enc = tried

            msg = _("The correct encoding of '%s' could not be determined.\n\n"
                    "Choose an encoding and select Ok to open the file with the chosen encoding.\n"
                    "Click Cancel to abort opening the file") % fname

            # On some systems it seems that default encoding ends up being
            # None so default to utf-8 for choices.
            if enc is None:
                enc = 'utf_8'

            dlg = eclib.EncodingDialog(self, msg=msg,
                                        title=_("Choose an Encoding"),
                                        default=enc)
            bmp = wx.ArtProvider.GetBitmap(str(ed_glob.ID_DOCPROP),
                                           wx.ART_OTHER)
            if bmp.IsOk():
                dlg.SetBitmap(bmp)
            dlg.CenterOnParent()
            result = dlg.ShowModal()
            if dlg:
                enc = dlg.GetEncoding()
                dlg.Destroy()
            else:
                result = wx.ID_CANCEL

            # Don't want to open it in another encoding
            if result == wx.ID_CANCEL:
                return False
            else:
                control.SetEncoding(enc)
                tried = enc
                ok = control.LoadFile(control.GetFileName())
                if ok:
                    return True
                else:
                    # Artificially add a short pause, because if its not there
                    # the dialog will be shown again so fast it wont seem
                    # like reloading the file was even tried.
                    wx.Sleep(1)
Exemplo n.º 2
0
 def OnShowDialogBtn(self, evt):
     """Show one of the test dialogs"""
     e_obj = evt.GetEventObject()
     if e_obj == self.btnDlg:
         dlg = eclib.EncodingDialog(self, msg="Select an encoding",
                                    title="Encoding Dialog",
                                    elist=eclib.GetAllEncodings(),
                                    default="utf-8")
         dlg.SetBitmap(IconFile.Home.GetBitmap())
         dlg.CenterOnParent()
         if dlg.ShowModal() == wx.ID_OK:
             enc = dlg.GetEncoding()
             self.log.write("Got Encoding: %s" % enc)
         dlg.Destroy()