Encoding is now a property of TermView. Changed a lot of code to fix the

problems caused by this change. MakeEncodingMenu doesn't mark the 
current encoding anymore, it was already done in TermWindow::MenusBeginning().
Removed custom enums for encodings, just use the ones provided in UTF8.h.
I'm more and more convinced we should drop the custom conversion routines
and use the system ones.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21704 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2007-07-26 14:52:24 +00:00
parent f4ac9a9693
commit feaebcb571
9 changed files with 99 additions and 132 deletions
+4 -7
View File
@@ -22,7 +22,7 @@ UTF8
************************************************************************/ ************************************************************************/
#include <support/UTF8.h> #include <UTF8.h>
#include <string.h> #include <string.h>
#include "CodeConv.h" #include "CodeConv.h"
@@ -62,17 +62,15 @@ CodeConv::ConvertFromInternal(const char *src, int32 srclen, char *dst, int codi
return srclen; return srclen;
} }
int theCoding = coding_translation_table[coding];
int32 dstlen = srclen * 256; int32 dstlen = srclen * 256;
long state = 0; long state = 0;
convert_from_utf8(theCoding, (char *)src, &srclen, convert_from_utf8(coding, (char *)src, &srclen,
(char *)dst, &dstlen, &state, '?'); (char *)dst, &dstlen, &state, '?');
// TODO: Apart from this particular case, looks like we could use the // TODO: Apart from this particular case, looks like we could use the
// system api for code conversion... check if this (which looks a lot like a workaround) // system api for code conversion... check if this (which looks a lot like a workaround)
// applies to haiku, and if not, get rid of this class and just use the system api directly. // applies to haiku, and if not, get rid of this class and just use the system api directly.
if (coding == M_ISO_2022_JP && state != 0) { if (coding == B_EUC_CONVERSION && state != 0) {
const char *end_of_jis = "(B"; const char *end_of_jis = "(B";
strncpy((char *)dst + dstlen, end_of_jis, 3); strncpy((char *)dst + dstlen, end_of_jis, 3);
dstlen += 3; dstlen += 3;
@@ -113,10 +111,9 @@ CodeConv::ConvertToInternal(const char *src, int32 srclen, char *dst, int coding
#endif #endif
#endif #endif
int theCoding = coding_translation_table[coding];
int32 dstlen = 4; int32 dstlen = 4;
long state = 0; long state = 0;
convert_to_utf8(theCoding, (char *)src, &srclen, convert_to_utf8(coding, (char *)src, &srclen,
(char *)dst, &dstlen, &state, '?'); (char *)dst, &dstlen, &state, '?');
dst[dstlen] = '\0'; dst[dstlen] = '\0';
+29 -34
View File
@@ -15,21 +15,22 @@ struct etable {
const static etable kEncodingTable[] = const static etable kEncodingTable[] =
{ {
{"UTF-8", "UTF8", 'U', M_UTF8}, {"UTF-8", "UTF8", 'U', M_UTF8},
{"ISO-8859-1", "8859-1", '1', M_ISO_8859_1}, {"ISO-8859-1", "8859-1", '1', B_ISO1_CONVERSION},
{"ISO-8859-2", "8859-2", '2', M_ISO_8859_2}, {"ISO-8859-2", "8859-2", '2', B_ISO2_CONVERSION},
{"ISO-8859-3", "8859-3", '3', M_ISO_8859_3}, {"ISO-8859-3", "8859-3", '3', B_ISO3_CONVERSION},
{"ISO-8859-4", "8859-4", '4', M_ISO_8859_4}, {"ISO-8859-4", "8859-4", '4', B_ISO4_CONVERSION},
{"ISO-8859-5", "8859-5", '5', M_ISO_8859_5}, {"ISO-8859-5", "8859-5", '5', B_ISO5_CONVERSION},
{"ISO-8859-6", "8859-6", '6', M_ISO_8859_6}, {"ISO-8859-6", "8859-6", '6', B_ISO6_CONVERSION},
{"ISO-8859-7", "8859-7", '7', M_ISO_8859_7}, {"ISO-8859-7", "8859-7", '7', B_ISO7_CONVERSION},
{"ISO-8859-8", "8859-8", '8', M_ISO_8859_8}, {"ISO-8859-8", "8859-8", '8', B_ISO8_CONVERSION},
{"ISO-8859-9", "8859-9", '9', M_ISO_8859_9}, {"ISO-8859-9", "8859-9", '9', B_ISO9_CONVERSION},
{"ISO-8859-10", "8859-10", '0', M_ISO_8859_10}, {"ISO-8859-10", "8859-10", '0', B_ISO10_CONVERSION},
{"MacRoman", "MacRoman",'M', M_MAC_ROMAN}, {"MacRoman", "MacRoman",'M', B_MAC_ROMAN_CONVERSION},
{"JIS", "JIS", 'J', M_ISO_2022_JP}, {"JIS", "JIS", 'J', B_JIS_CONVERSION},
{"Shift-JIS", "SJIS", 'S', M_SJIS}, {"Shift-JIS", "SJIS", 'S', B_SJIS_CONVERSION},
{"EUC-jp", "EUCJ", 'E', M_EUC_JP}, {"EUC-jp", "EUCJ", 'E', B_EUC_CONVERSION},
{"EUC-kr", "EUCK", 'K', M_EUC_KR}, {"EUC-kr", "EUCK", 'K', B_EUC_KR_CONVERSION},
/* Not Implement. /* Not Implement.
{"EUC-tw", "EUCT", "T", M_EUC_TW}, {"EUC-tw", "EUCT", "T", M_EUC_TW},
@@ -42,8 +43,6 @@ const static etable kEncodingTable[] =
}; };
static int sCurrentEncoding = M_UTF8;
status_t status_t
get_nth_encoding(int i, int *id) get_nth_encoding(int i, int *id)
@@ -51,7 +50,7 @@ get_nth_encoding(int i, int *id)
if (id == NULL) if (id == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
if (i < 0 || i >= (int)(sizeof(kEncodingTable) / sizeof(etable))) if (i < 0 || i >= (int)(sizeof(kEncodingTable) / sizeof(etable)) - 1)
return B_BAD_INDEX; return B_BAD_INDEX;
*id = kEncodingTable[i].id; *id = kEncodingTable[i].id;
@@ -97,27 +96,23 @@ longname2shortname(const char *longname)
const char * const char *
id2longname(int id) id2longname(int id)
{ {
return kEncodingTable[id].name; const etable *p = kEncodingTable;
while (p->name) {
if (id == p->id)
return p->name;
p++;
}
} }
const char const char
id2shortcut(int id) id2shortcut(int id)
{ {
return kEncodingTable[id].shortcut; const etable *p = kEncodingTable;
} while (p->name) {
if (id == p->id)
return p->shortcut;
void p++;
SetEncoding(int encoding) }
{
sCurrentEncoding = encoding;
}
int
GetEncoding()
{
return sCurrentEncoding;
} }
+2 -50
View File
@@ -32,56 +32,10 @@
#ifndef _CODING__H_ #ifndef _CODING__H_
#define _CODING__H_ #define _CODING__H_
#include <SupportDefs.h>
#include <UTF8.h> #include <UTF8.h>
enum { #define M_UTF8 (uint32)(-1)
M_UTF8, /* UTF-8 */
M_ISO_8859_1, /* ISO-8859 */
M_ISO_8859_2,
M_ISO_8859_3,
M_ISO_8859_4,
M_ISO_8859_5,
M_ISO_8859_6,
M_ISO_8859_7,
M_ISO_8859_8,
M_ISO_8859_9,
M_ISO_8859_10,
M_MAC_ROMAN, /* Macintosh Roman */
M_ISO_2022_JP,
M_SJIS, /* Japanese */
M_EUC_JP,
M_EUC_KR
// M_EUC_TW, /* Chinese */
// M_BIG5,
// M_ISO_2022_CN,
// M_EUC_KR, /* Koeran */
// M_ISO_2022_KR,
};
const uint32 coding_translation_table[] = {
0,
B_ISO1_CONVERSION, /* ISO 8859-1 */
B_ISO2_CONVERSION, /* ISO 8859-2 */
B_ISO3_CONVERSION, /* ISO 8859-3 */
B_ISO4_CONVERSION, /* ISO 8859-4 */
B_ISO5_CONVERSION, /* ISO 8859-5 */
B_ISO6_CONVERSION, /* ISO 8859-6 */
B_ISO7_CONVERSION, /* ISO 8859-7 */
B_ISO8_CONVERSION, /* ISO 8859-8 */
B_ISO9_CONVERSION, /* ISO 8859-9 */
B_ISO10_CONVERSION, /* ISO 8859-10 */
B_MAC_ROMAN_CONVERSION, /* Macintosh Roman */
B_JIS_CONVERSION, /* JIS X 0208-1990 */
B_SJIS_CONVERSION, /* Shift-JIS */
B_EUC_CONVERSION, /* EUC Packed Japanese */
B_EUC_KR_CONVERSION /* EUC Korean */
};
status_t get_nth_encoding(int i, int *id); status_t get_nth_encoding(int i, int *id);
@@ -90,7 +44,5 @@ const char * longname2shortname(const char *longname);
const char * id2longname(int op); const char * id2longname(int op);
const char id2shortcut(int op); const char id2shortcut(int op);
void SetEncoding(int encoding);
int GetEncoding();
#endif /* _CODING_H_ */ #endif /* _CODING_H_ */
+1 -4
View File
@@ -41,7 +41,7 @@ MakeMenu(ulong msg, const char **items, const char *defaultItemName)
void void
MakeEncodingMenu(BMenu *eMenu, int marked, bool flag) MakeEncodingMenu(BMenu *eMenu, bool flag)
{ {
int encoding; int encoding;
int i = 0; int i = 0;
@@ -53,9 +53,6 @@ MakeEncodingMenu(BMenu *eMenu, int marked, bool flag)
else else
eMenu->AddItem(new BMenuItem(id2longname(encoding), msg)); eMenu->AddItem(new BMenuItem(id2longname(encoding), msg));
if (i == marked)
eMenu->ItemAt(i)->SetMarked(true);
i++; i++;
} }
} }
+1 -1
View File
@@ -20,7 +20,7 @@ class PrefHandler;
BPopUpMenu * MakeMenu(ulong msg, const char **items, BPopUpMenu * MakeMenu(ulong msg, const char **items,
const char *defaultItemName); const char *defaultItemName);
void MakeEncodingMenu(BMenu *eMenu, int coding, bool flag); void MakeEncodingMenu(BMenu *eMenu, bool flag);
void LoadLocaleFile (PrefHandler *); void LoadLocaleFile (PrefHandler *);
+26 -25
View File
@@ -288,37 +288,38 @@ TermParse::EscParse()
if (GetReaderBuf(c) < B_OK) if (GetReaderBuf(c) < B_OK)
break; break;
if (now_coding != GetEncoding()) { if (now_coding != fView->Encoding()) {
/* /*
* Change coding, change parse table. * Change coding, change parse table.
*/ */
switch (GetEncoding()) { switch (fView->Encoding()) {
case M_UTF8: case B_ISO1_CONVERSION:
groundtable = utf8_groundtable; case B_ISO2_CONVERSION:
break; case B_ISO3_CONVERSION:
case M_ISO_8859_1: case B_ISO4_CONVERSION:
case M_ISO_8859_2: case B_ISO5_CONVERSION:
case M_ISO_8859_3: case B_ISO6_CONVERSION:
case M_ISO_8859_4: case B_ISO7_CONVERSION:
case M_ISO_8859_5: case B_ISO8_CONVERSION:
case M_ISO_8859_6: case B_ISO9_CONVERSION:
case M_ISO_8859_7: case B_ISO10_CONVERSION:
case M_ISO_8859_8:
case M_ISO_8859_9:
case M_ISO_8859_10:
groundtable = iso8859_groundtable; groundtable = iso8859_groundtable;
break; break;
case M_SJIS: case B_SJIS_CONVERSION:
groundtable = sjis_groundtable; groundtable = sjis_groundtable;
break; break;
case M_EUC_JP: case B_EUC_CONVERSION:
case M_EUC_KR: case B_EUC_KR_CONVERSION:
case M_ISO_2022_JP: case B_JIS_CONVERSION:
groundtable = iso8859_groundtable; groundtable = iso8859_groundtable;
break; break;
case M_UTF8:
default:
groundtable = utf8_groundtable;
break;
} }
parsestate = groundtable; parsestate = groundtable;
now_coding = GetEncoding(); now_coding = fView->Encoding();
} }
switch (parsestate[c]) { switch (parsestate[c]) {
@@ -332,8 +333,8 @@ TermParse::EscParse()
case CASE_PRINT_GR: case CASE_PRINT_GR:
/* case iso8859 gr character, or euc */ /* case iso8859 gr character, or euc */
ptr = cbuf; ptr = cbuf;
if (now_coding == M_EUC_JP || now_coding == M_EUC_KR if (now_coding == B_EUC_CONVERSION || now_coding == B_EUC_KR_CONVERSION
|| now_coding == M_ISO_2022_JP) { || now_coding == B_JIS_CONVERSION) {
switch (parsestate[curess]) { switch (parsestate[curess]) {
case CASE_SS2: /* JIS X 0201 */ case CASE_SS2: /* JIS X 0201 */
*ptr++ = curess; *ptr++ = curess;
@@ -366,10 +367,10 @@ TermParse::EscParse()
width = 1; width = 1;
} }
if (now_coding != M_ISO_2022_JP) if (now_coding != B_JIS_CONVERSION)
CodeConv::ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, now_coding); CodeConv::ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, now_coding);
else else
CodeConv::ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, M_EUC_JP); CodeConv::ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, B_EUC_CONVERSION);
fView->PutChar(dstbuf, attr, width); fView->PutChar(dstbuf, attr, width);
break; break;
@@ -380,7 +381,7 @@ TermParse::EscParse()
cbuf[1] |= 0x80; cbuf[1] |= 0x80;
cbuf[2] = 0; cbuf[2] = 0;
width = 2; width = 2;
CodeConv::ConvertToInternal((char*)cbuf, 2, (char*)dstbuf, M_EUC_JP); CodeConv::ConvertToInternal((char*)cbuf, 2, (char*)dstbuf, B_EUC_CONVERSION);
fView->PutChar(dstbuf, attr, width); fView->PutChar(dstbuf, attr, width);
break; break;
+23 -6
View File
@@ -133,6 +133,7 @@ TermView::TermView(BRect frame, const char *command)
fBufferStartPos(-1), fBufferStartPos(-1),
fTermRows(PrefHandler::Default()->getInt32(PREF_ROWS)), fTermRows(PrefHandler::Default()->getInt32(PREF_ROWS)),
fTermColumns(PrefHandler::Default()->getInt32(PREF_COLS)), fTermColumns(PrefHandler::Default()->getInt32(PREF_COLS)),
fEncoding(M_UTF8),
fTop(0), fTop(0),
fTextBuffer(new (nothrow) TermBuffer(fTermRows, fTermColumns)), fTextBuffer(new (nothrow) TermBuffer(fTermRows, fTermColumns)),
fScrollBar(NULL), fScrollBar(NULL),
@@ -294,6 +295,20 @@ TermView::SetTermColor()
} }
int
TermView::Encoding() const
{
return fEncoding;
}
void
TermView::SetEncoding(int encoding)
{
fEncoding = encoding;
}
//! Sets half and full fonts for terminal //! Sets half and full fonts for terminal
void void
TermView::SetTermFont(const BFont *halfFont, const BFont *fullFont) TermView::SetTermFont(const BFont *halfFont, const BFont *fullFont)
@@ -1429,10 +1444,10 @@ TermView::KeyDown(const char *bytes, int32 numBytes)
} }
} else { } else {
// input multibyte character // input multibyte character
if (GetEncoding() != M_UTF8) { if (fEncoding != M_UTF8) {
uchar dstbuf[1024]; uchar dstbuf[1024];
int cnum = CodeConv::ConvertFromInternal(bytes, numBytes, int cnum = CodeConv::ConvertFromInternal(bytes, numBytes,
(char *)dstbuf, GetEncoding()); (char *)dstbuf, fEncoding);
fShell->Write(dstbuf, cnum); fShell->Write(dstbuf, cnum);
} }
} }
@@ -1679,10 +1694,10 @@ TermView::DoClearAll(void)
void void
TermView::WritePTY(const uchar *text, int numBytes) TermView::WritePTY(const uchar *text, int numBytes)
{ {
if (GetEncoding() != M_UTF8) { if (fEncoding != M_UTF8) {
uchar *destBuffer = (uchar *)malloc(numBytes * 3); uchar *destBuffer = (uchar *)malloc(numBytes * 3);
numBytes = CodeConv::ConvertFromInternal((char*)text, numBytes, numBytes = CodeConv::ConvertFromInternal((char*)text, numBytes,
(char*)destBuffer, GetEncoding()); (char*)destBuffer, fEncoding);
fShell->Write(destBuffer, numBytes); fShell->Write(destBuffer, numBytes);
free(destBuffer); free(destBuffer);
} else { } else {
@@ -1839,12 +1854,14 @@ TermView::Select(CurPos start, CurPos end)
if (fTextBuffer->GetChar(start.y, start.x, buf, &attr) == IN_STRING) { if (fTextBuffer->GetChar(start.y, start.x, buf, &attr) == IN_STRING) {
start.x--; start.x--;
if (start.x < 0) start.x = 0; if (start.x < 0)
start.x = 0;
} }
if (fTextBuffer->GetChar(end.y, end.x, buf, &attr) == IN_STRING) { if (fTextBuffer->GetChar(end.y, end.x, buf, &attr) == IN_STRING) {
end.x++; end.x++;
if (end.x >= fTermColumns) end.x = fTermColumns; if (end.x >= fTermColumns)
end.x = fTermColumns;
} }
fSelStart = start; fSelStart = start;
+5
View File
@@ -68,6 +68,9 @@ public:
BRect SetTermSize(int rows, int cols, bool flag); BRect SetTermSize(int rows, int cols, bool flag);
void SetTermColor(); void SetTermColor();
int Encoding() const;
void SetEncoding(int encoding);
void SetMouseCursor(); void SetMouseCursor();
// void SetIMAware (bool); // void SetIMAware (bool);
void SetScrollBar(BScrollBar *scrbar); void SetScrollBar(BScrollBar *scrbar);
@@ -235,6 +238,8 @@ private:
int fTermRows; int fTermRows;
int fTermColumns; int fTermColumns;
int fEncoding;
// Terminal view pointer. // Terminal view pointer.
int fTop; int fTop;
+8 -5
View File
@@ -144,6 +144,7 @@ TermWindow::_InitWindow(const char *command)
textframe.top = fMenubar->Bounds().bottom + 1.0; textframe.top = fMenubar->Bounds().bottom + 1.0;
fTermView = new TermView(textframe, command); fTermView = new TermView(textframe, command);
fTermView->SetEncoding(longname2id(PrefHandler::Default()->getString(PREF_TEXT_ENCODING)));
// Initialize TermView. (font, size and color) // Initialize TermView. (font, size and color)
fTermView->SetTermFont(&halfFont, &fullFont); fTermView->SetTermFont(&halfFont, &fullFont);
@@ -187,7 +188,9 @@ void
TermWindow::MenusBeginning() TermWindow::MenusBeginning()
{ {
// Syncronize Encode Menu Pop-up menu and Preference. // Syncronize Encode Menu Pop-up menu and Preference.
(fEncodingmenu->FindItem(id2longname(GetEncoding())))->SetMarked(true); BMenuItem *item = fEncodingmenu->FindItem(id2longname(fTermView->Encoding()));
if (item != NULL)
item->SetMarked(true);
BWindow::MenusBeginning(); BWindow::MenusBeginning();
} }
@@ -261,7 +264,7 @@ TermWindow::_SetupMenu()
fEncodingmenu = new BMenu("Font Encoding"); fEncodingmenu = new BMenu("Font Encoding");
fEncodingmenu->SetRadioMode(true); fEncodingmenu->SetRadioMode(true);
MakeEncodingMenu(fEncodingmenu, GetEncoding(), true); MakeEncodingMenu(fEncodingmenu, true);
fHelpmenu->AddItem(fWindowSizeMenu); fHelpmenu->AddItem(fWindowSizeMenu);
fHelpmenu->AddItem(fEncodingmenu); fHelpmenu->AddItem(fEncodingmenu);
// fHelpmenu->AddItem(fNewFontMenu); // fHelpmenu->AddItem(fNewFontMenu);
@@ -382,7 +385,7 @@ TermWindow::MessageReceived(BMessage *message)
case MENU_ENCODING: { case MENU_ENCODING: {
message->FindInt32 ("op", &coding_id); message->FindInt32 ("op", &coding_id);
SetEncoding(coding_id); fTermView->SetEncoding(coding_id);
break; break;
} }
// Extended B_SET_PROPERTY. Dispatch this message, // Extended B_SET_PROPERTY. Dispatch this message,
@@ -393,7 +396,7 @@ TermWindow::MessageReceived(BMessage *message)
message->GetCurrentSpecifier(&i, &spe); message->GetCurrentSpecifier(&i, &spe);
if (!strcmp("encode", spe.FindString("property", i))){ if (!strcmp("encode", spe.FindString("property", i))){
message->FindInt32 ("data", &coding_id); message->FindInt32 ("data", &coding_id);
SetEncoding (coding_id); fTermView->SetEncoding (coding_id);
message->SendReply(B_REPLY); message->SendReply(B_REPLY);
} else { } else {
@@ -409,7 +412,7 @@ TermWindow::MessageReceived(BMessage *message)
message->GetCurrentSpecifier(&i, &spe); message->GetCurrentSpecifier(&i, &spe);
if (!strcmp("encode", spe.FindString("property", i))){ if (!strcmp("encode", spe.FindString("property", i))){
BMessage reply(B_REPLY); BMessage reply(B_REPLY);
reply.AddInt32("result", GetEncoding()); reply.AddInt32("result", fTermView->Encoding());
message->SendReply(&reply); message->SendReply(&reply);
} }
else if (!strcmp("tty", spe.FindString("property", i))) { else if (!strcmp("tty", spe.FindString("property", i))) {