Several unicode fixes related to wxScintilla, spellcheck and thesaurus

Originally committed to SVN as r617.
This commit is contained in:
Rodrigo Braz Monteiro 2006-12-26 01:08:46 +00:00
parent 518c4d3766
commit 8b055489eb
8 changed files with 60 additions and 15 deletions

View File

@ -40,6 +40,7 @@
#include <errno.h> #include <errno.h>
#include <wx/tokenzr.h> #include <wx/tokenzr.h>
#include <wx/choicdlg.h> #include <wx/choicdlg.h>
#include <wx/filename.h>
#include "mkv_wrap.h" #include "mkv_wrap.h"
#include "dialog_progress.h" #include "dialog_progress.h"
#include "ass_file.h" #include "ass_file.h"
@ -548,7 +549,8 @@ MkvStdIO::MkvStdIO(wxString filename) {
memrealloc = StdIoRealloc; memrealloc = StdIoRealloc;
memfree = StdIoFree; memfree = StdIoFree;
progress = StdIoProgress; progress = StdIoProgress;
fp = fopen(filename.mb_str(),"rb"); wxFileName fname(filename);
fp = fopen(fname.GetShortPath().mb_str(wxConvUTF8),"rb");
if (fp) { if (fp) {
setvbuf(fp, NULL, _IOFBF, CACHESIZE); setvbuf(fp, NULL, _IOFBF, CACHESIZE);
} }

View File

@ -51,7 +51,9 @@ public:
SpellChecker() {} SpellChecker() {}
virtual ~SpellChecker() {} virtual ~SpellChecker() {}
virtual void AddWord(wxString word)=0; virtual void AddWord(wxString word) {}
virtual bool CanAddWord(wxString word) { return false; }
virtual bool CheckWord(wxString word)=0; virtual bool CheckWord(wxString word)=0;
virtual wxArrayString GetSuggestions(wxString word)=0; virtual wxArrayString GetSuggestions(wxString word)=0;

View File

@ -64,6 +64,14 @@ HunspellSpellChecker::~HunspellSpellChecker() {
} }
//////////////////////////
// Can add to dictionary?
bool HunspellSpellChecker::CanAddWord(wxString word) {
if (!hunspell) return false;
return (word.mb_str(*conv) != NULL);
}
////////////////////////// //////////////////////////
// Add word to dictionary // Add word to dictionary
void HunspellSpellChecker::AddWord(wxString word) { void HunspellSpellChecker::AddWord(wxString word) {
@ -75,7 +83,9 @@ void HunspellSpellChecker::AddWord(wxString word) {
// Check if the word is valid // Check if the word is valid
bool HunspellSpellChecker::CheckWord(wxString word) { bool HunspellSpellChecker::CheckWord(wxString word) {
if (!hunspell) return true; if (!hunspell) return true;
return (hunspell->spell(word.mb_str(*conv)) == 1); wxCharBuffer buf = word.mb_str(*conv);
if (buf) return (hunspell->spell(buf) == 1);
return false;
} }
@ -85,11 +95,15 @@ wxArrayString HunspellSpellChecker::GetSuggestions(wxString word) {
// Array // Array
wxArrayString suggestions; wxArrayString suggestions;
// Word
wxCharBuffer buf = word.mb_str(*conv);
if (!buf) return suggestions;
// Get suggestions // Get suggestions
if (hunspell) { if (hunspell) {
// Grab raw from Hunspell // Grab raw from Hunspell
char **results; char **results;
int n = hunspell->suggest(&results,word.mb_str(*conv)); int n = hunspell->suggest(&results,buf);
// Convert each // Convert each
for (int i=0;i<n;i++) { for (int i=0;i<n;i++) {

View File

@ -59,6 +59,8 @@ public:
~HunspellSpellChecker(); ~HunspellSpellChecker();
void AddWord(wxString word); void AddWord(wxString word);
bool CanAddWord(wxString word);
bool CheckWord(wxString word); bool CheckWord(wxString word);
wxArrayString GetSuggestions(wxString word); wxArrayString GetSuggestions(wxString word);

View File

@ -251,14 +251,34 @@ void SubsTextEditCtrl::UpdateStyle(int start, int _length) {
} }
///////////////////////////////////
// Get unicode-compatible position
int SubsTextEditCtrl::GetUnicodePosition(int pos) {
wxString string = GetText().Left(pos);
wxCharBuffer buffer = string.mb_str(wxConvUTF8);
return strlen(buffer);
}
///////////////////////////////////////
// Reverse unicode-compatible position
int SubsTextEditCtrl::GetReverseUnicodePosition(int pos) {
wxCharBuffer buffer = GetText().mb_str(wxConvUTF8);
char *buf2 = new char[pos+1];
memcpy(buf2,buffer,pos);
buf2[pos] = 0;
wxString buf3(buf2,wxConvUTF8);
return buf3.Length();
}
//////////////////////// ////////////////////////
// Unicode-safe styling // Unicode-safe styling
void SubsTextEditCtrl::SetUnicodeStyling(int start,int length,int style) { void SubsTextEditCtrl::SetUnicodeStyling(int start,int length,int style) {
// Get the real length // Get the real length
wxString string = GetText().Mid(start,length); wxString string = GetText().Mid(start,length);
wxCharBuffer buffer = string.mb_str(wxConvUTF8); wxCharBuffer buffer = string.mb_str(wxConvUTF8);
const char* utf8str = buffer; int len = strlen(buffer);
int len = strlen(utf8str);
// Set styling // Set styling
SetStyling(len,style); SetStyling(len,style);
@ -345,10 +365,7 @@ void SubsTextEditCtrl::StyleSpellCheck(int start, int len) {
// Check if it's valid // Check if it's valid
if (!spellchecker->CheckWord(curWord)) { if (!spellchecker->CheckWord(curWord)) {
// Get length before it // Get length before it
wxString string = GetText().Left(startPos[i]); int utf8len = GetUnicodePosition(startPos[i]);
wxCharBuffer buffer = string.mb_str(wxConvUTF8);
const char* utf8str = buffer;
int utf8len = strlen(utf8str);
// Set styling // Set styling
StartStyling(utf8len,32); StartStyling(utf8len,32);
@ -432,6 +449,7 @@ void SubsTextEditCtrl::ShowPopupMenu(int activePos) {
// Position // Position
if (activePos == -1) activePos = GetCurrentPos(); if (activePos == -1) activePos = GetCurrentPos();
activePos = GetReverseUnicodePosition(activePos);
// Get current word under cursor // Get current word under cursor
currentWord = GetWordAtPosition(activePos); currentWord = GetWordAtPosition(activePos);
@ -461,7 +479,7 @@ void SubsTextEditCtrl::ShowPopupMenu(int activePos) {
for (int i=0;i<nSugs;i++) menu.Append(EDIT_MENU_SUGGESTIONS+i,sugs[i])->SetFont(font); for (int i=0;i<nSugs;i++) menu.Append(EDIT_MENU_SUGGESTIONS+i,sugs[i])->SetFont(font);
// Append "add word" // Append "add word"
menu.Append(EDIT_MENU_ADD_TO_DICT,wxString::Format(_("Add \"%s\" to dictionary"),currentWord.c_str())); menu.Append(EDIT_MENU_ADD_TO_DICT,wxString::Format(_("Add \"%s\" to dictionary"),currentWord.c_str()))->Enable(spellchecker->CanAddWord(currentWord));
menu.AppendSeparator(); menu.AppendSeparator();
} }
@ -715,7 +733,7 @@ void SubsTextEditCtrl::OnUseSuggestion(wxCommandEvent &event) {
SetText(text.Left(MAX(0,start)) + suggestion + text.Mid(end+1)); SetText(text.Left(MAX(0,start)) + suggestion + text.Mid(end+1));
// Set selection // Set selection
SetSelection(start,start+suggestion.Length()); SetSelection(GetUnicodePosition(start),GetUnicodePosition(start+suggestion.Length()));
SetFocus(); SetFocus();
} }
@ -742,6 +760,6 @@ void SubsTextEditCtrl::OnUseThesaurusSuggestion(wxCommandEvent &event) {
SetText(text.Left(MAX(0,start)) + suggestion + text.Mid(end+1)); SetText(text.Left(MAX(0,start)) + suggestion + text.Mid(end+1));
// Set selection // Set selection
SetSelection(start,start+suggestion.Length()); SetSelection(GetUnicodePosition(start),GetUnicodePosition(start+suggestion.Length()));
SetFocus(); SetFocus();
} }

View File

@ -67,6 +67,8 @@ private:
wxString GetWordAtPosition(int pos); wxString GetWordAtPosition(int pos);
void GetBoundsOfWordAtPosition(int pos,int &start,int &end); void GetBoundsOfWordAtPosition(int pos,int &start,int &end);
int GetUnicodePosition(int pos);
int GetReverseUnicodePosition(int pos);
void SetUnicodeStyling(int start,int length,int style); void SetUnicodeStyling(int start,int length,int style);
void ShowPopupMenu(int activePos=-1); void ShowPopupMenu(int activePos=-1);

View File

@ -71,6 +71,7 @@ void MySpellThesaurus::Lookup(wxString word,ThesaurusEntryArray &result) {
// Grab raw from MyThes // Grab raw from MyThes
mentry *me; mentry *me;
wxCharBuffer buf = word.Lower().mb_str(*conv); wxCharBuffer buf = word.Lower().mb_str(*conv);
if (!buf) return;
int n = mythes->Lookup(buf,strlen(buf),&me); int n = mythes->Lookup(buf,strlen(buf),&me);
// Each entry // Each entry

View File

@ -35,6 +35,7 @@
#include <wx/filename.h> #include <wx/filename.h>
#include <wx/msw/registry.h> #include <wx/msw/registry.h>
#include <wx/filename.h>
#include "video_provider_avs.h" #include "video_provider_avs.h"
#include "options.h" #include "options.h"
#include "main.h" #include "main.h"
@ -157,7 +158,9 @@ PClip AvisynthVideoProvider::OpenVideo(wxString _filename, bool mpeg2dec3_priori
try { try {
// Prepare filename // Prepare filename
char *videoFilename = env->SaveString(_filename.mb_str(wxConvLocal)); //char *videoFilename = env->SaveString(_filename.mb_str(wxConvLocal));
wxFileName fname(_filename);
char *videoFilename = env->SaveString(fname.GetShortPath().mb_str(wxConvLocal));
// Avisynth file, just import it // Avisynth file, just import it
if (extension == _T(".avs")) { if (extension == _T(".avs")) {
@ -275,7 +278,8 @@ PClip AvisynthVideoProvider::ApplySubtitles(wxString _filename, PClip videosourc
// Insert subs // Insert subs
AVSValue script; AVSValue script;
char temp[512]; char temp[512];
strcpy(temp,_filename.mb_str(wxConvLocal)); wxFileName fname(_filename);
strcpy(temp,fname.GetShortPath().mb_str(wxConvLocal));
AVSValue args[2] = { videosource, temp }; AVSValue args[2] = { videosource, temp };
try { try {