diff --git a/src/apps/terminal/CodeConv.cpp b/src/apps/terminal/CodeConv.cpp index 181b354f53..0c16ab943b 100644 --- a/src/apps/terminal/CodeConv.cpp +++ b/src/apps/terminal/CodeConv.cpp @@ -22,7 +22,7 @@ UTF8 ************************************************************************/ -#include +#include #include #include "CodeConv.h" @@ -62,17 +62,15 @@ CodeConv::ConvertFromInternal(const char *src, int32 srclen, char *dst, int codi return srclen; } - int theCoding = coding_translation_table[coding]; - int32 dstlen = srclen * 256; long state = 0; - convert_from_utf8(theCoding, (char *)src, &srclen, + convert_from_utf8(coding, (char *)src, &srclen, (char *)dst, &dstlen, &state, '?'); // 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) // 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 = ""; strncpy((char *)dst + dstlen, end_of_jis, 3); dstlen += 3; @@ -113,10 +111,9 @@ CodeConv::ConvertToInternal(const char *src, int32 srclen, char *dst, int coding #endif #endif - int theCoding = coding_translation_table[coding]; int32 dstlen = 4; long state = 0; - convert_to_utf8(theCoding, (char *)src, &srclen, + convert_to_utf8(coding, (char *)src, &srclen, (char *)dst, &dstlen, &state, '?'); dst[dstlen] = '\0'; diff --git a/src/apps/terminal/Coding.cpp b/src/apps/terminal/Coding.cpp index 5f8dc71cee..8b8fdb2641 100644 --- a/src/apps/terminal/Coding.cpp +++ b/src/apps/terminal/Coding.cpp @@ -15,21 +15,22 @@ struct etable { const static etable kEncodingTable[] = { {"UTF-8", "UTF8", 'U', M_UTF8}, - {"ISO-8859-1", "8859-1", '1', M_ISO_8859_1}, - {"ISO-8859-2", "8859-2", '2', M_ISO_8859_2}, - {"ISO-8859-3", "8859-3", '3', M_ISO_8859_3}, - {"ISO-8859-4", "8859-4", '4', M_ISO_8859_4}, - {"ISO-8859-5", "8859-5", '5', M_ISO_8859_5}, - {"ISO-8859-6", "8859-6", '6', M_ISO_8859_6}, - {"ISO-8859-7", "8859-7", '7', M_ISO_8859_7}, - {"ISO-8859-8", "8859-8", '8', M_ISO_8859_8}, - {"ISO-8859-9", "8859-9", '9', M_ISO_8859_9}, - {"ISO-8859-10", "8859-10", '0', M_ISO_8859_10}, - {"MacRoman", "MacRoman",'M', M_MAC_ROMAN}, - {"JIS", "JIS", 'J', M_ISO_2022_JP}, - {"Shift-JIS", "SJIS", 'S', M_SJIS}, - {"EUC-jp", "EUCJ", 'E', M_EUC_JP}, - {"EUC-kr", "EUCK", 'K', M_EUC_KR}, + {"ISO-8859-1", "8859-1", '1', B_ISO1_CONVERSION}, + {"ISO-8859-2", "8859-2", '2', B_ISO2_CONVERSION}, + {"ISO-8859-3", "8859-3", '3', B_ISO3_CONVERSION}, + {"ISO-8859-4", "8859-4", '4', B_ISO4_CONVERSION}, + {"ISO-8859-5", "8859-5", '5', B_ISO5_CONVERSION}, + {"ISO-8859-6", "8859-6", '6', B_ISO6_CONVERSION}, + {"ISO-8859-7", "8859-7", '7', B_ISO7_CONVERSION}, + {"ISO-8859-8", "8859-8", '8', B_ISO8_CONVERSION}, + {"ISO-8859-9", "8859-9", '9', B_ISO9_CONVERSION}, + {"ISO-8859-10", "8859-10", '0', B_ISO10_CONVERSION}, + {"MacRoman", "MacRoman",'M', B_MAC_ROMAN_CONVERSION}, + {"JIS", "JIS", 'J', B_JIS_CONVERSION}, + {"Shift-JIS", "SJIS", 'S', B_SJIS_CONVERSION}, + {"EUC-jp", "EUCJ", 'E', B_EUC_CONVERSION}, + {"EUC-kr", "EUCK", 'K', B_EUC_KR_CONVERSION}, + /* Not Implement. {"EUC-tw", "EUCT", "T", M_EUC_TW}, @@ -42,8 +43,6 @@ const static etable kEncodingTable[] = }; -static int sCurrentEncoding = M_UTF8; - status_t get_nth_encoding(int i, int *id) @@ -51,7 +50,7 @@ get_nth_encoding(int i, int *id) if (id == NULL) 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; *id = kEncodingTable[i].id; @@ -97,27 +96,23 @@ longname2shortname(const char *longname) const char * id2longname(int id) { - return kEncodingTable[id].name; + const etable *p = kEncodingTable; + while (p->name) { + if (id == p->id) + return p->name; + p++; + } } const char id2shortcut(int id) { - return kEncodingTable[id].shortcut; -} - - -void -SetEncoding(int encoding) -{ - sCurrentEncoding = encoding; -} - - -int -GetEncoding() -{ - return sCurrentEncoding; + const etable *p = kEncodingTable; + while (p->name) { + if (id == p->id) + return p->shortcut; + p++; + } } diff --git a/src/apps/terminal/Coding.h b/src/apps/terminal/Coding.h index b09acb9832..70f78d8cb9 100644 --- a/src/apps/terminal/Coding.h +++ b/src/apps/terminal/Coding.h @@ -32,56 +32,10 @@ #ifndef _CODING__H_ #define _CODING__H_ +#include #include -enum { - 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 */ -}; - +#define M_UTF8 (uint32)(-1) 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 id2shortcut(int op); -void SetEncoding(int encoding); -int GetEncoding(); #endif /* _CODING_H_ */ diff --git a/src/apps/terminal/MenuUtil.cpp b/src/apps/terminal/MenuUtil.cpp index 299f8baf85..79fc9f4d33 100644 --- a/src/apps/terminal/MenuUtil.cpp +++ b/src/apps/terminal/MenuUtil.cpp @@ -41,7 +41,7 @@ MakeMenu(ulong msg, const char **items, const char *defaultItemName) void -MakeEncodingMenu(BMenu *eMenu, int marked, bool flag) +MakeEncodingMenu(BMenu *eMenu, bool flag) { int encoding; int i = 0; @@ -53,9 +53,6 @@ MakeEncodingMenu(BMenu *eMenu, int marked, bool flag) else eMenu->AddItem(new BMenuItem(id2longname(encoding), msg)); - if (i == marked) - eMenu->ItemAt(i)->SetMarked(true); - i++; } } diff --git a/src/apps/terminal/MenuUtil.h b/src/apps/terminal/MenuUtil.h index fcdf876682..1da1e10e9c 100644 --- a/src/apps/terminal/MenuUtil.h +++ b/src/apps/terminal/MenuUtil.h @@ -20,7 +20,7 @@ class PrefHandler; BPopUpMenu * MakeMenu(ulong msg, const char **items, const char *defaultItemName); -void MakeEncodingMenu(BMenu *eMenu, int coding, bool flag); +void MakeEncodingMenu(BMenu *eMenu, bool flag); void LoadLocaleFile (PrefHandler *); diff --git a/src/apps/terminal/TermParse.cpp b/src/apps/terminal/TermParse.cpp index 64c4092b68..1eec44c92b 100644 --- a/src/apps/terminal/TermParse.cpp +++ b/src/apps/terminal/TermParse.cpp @@ -288,37 +288,38 @@ TermParse::EscParse() if (GetReaderBuf(c) < B_OK) break; - if (now_coding != GetEncoding()) { + if (now_coding != fView->Encoding()) { /* * Change coding, change parse table. */ - switch (GetEncoding()) { - case M_UTF8: - groundtable = utf8_groundtable; - break; - case M_ISO_8859_1: - case M_ISO_8859_2: - case M_ISO_8859_3: - case M_ISO_8859_4: - case M_ISO_8859_5: - case M_ISO_8859_6: - case M_ISO_8859_7: - case M_ISO_8859_8: - case M_ISO_8859_9: - case M_ISO_8859_10: + switch (fView->Encoding()) { + case B_ISO1_CONVERSION: + case B_ISO2_CONVERSION: + case B_ISO3_CONVERSION: + case B_ISO4_CONVERSION: + case B_ISO5_CONVERSION: + case B_ISO6_CONVERSION: + case B_ISO7_CONVERSION: + case B_ISO8_CONVERSION: + case B_ISO9_CONVERSION: + case B_ISO10_CONVERSION: groundtable = iso8859_groundtable; break; - case M_SJIS: + case B_SJIS_CONVERSION: groundtable = sjis_groundtable; break; - case M_EUC_JP: - case M_EUC_KR: - case M_ISO_2022_JP: + case B_EUC_CONVERSION: + case B_EUC_KR_CONVERSION: + case B_JIS_CONVERSION: groundtable = iso8859_groundtable; break; + case M_UTF8: + default: + groundtable = utf8_groundtable; + break; } parsestate = groundtable; - now_coding = GetEncoding(); + now_coding = fView->Encoding(); } switch (parsestate[c]) { @@ -332,8 +333,8 @@ TermParse::EscParse() case CASE_PRINT_GR: /* case iso8859 gr character, or euc */ ptr = cbuf; - if (now_coding == M_EUC_JP || now_coding == M_EUC_KR - || now_coding == M_ISO_2022_JP) { + if (now_coding == B_EUC_CONVERSION || now_coding == B_EUC_KR_CONVERSION + || now_coding == B_JIS_CONVERSION) { switch (parsestate[curess]) { case CASE_SS2: /* JIS X 0201 */ *ptr++ = curess; @@ -366,10 +367,10 @@ TermParse::EscParse() width = 1; } - if (now_coding != M_ISO_2022_JP) + if (now_coding != B_JIS_CONVERSION) CodeConv::ConvertToInternal((char*)cbuf, -1, (char*)dstbuf, now_coding); 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); break; @@ -380,7 +381,7 @@ TermParse::EscParse() cbuf[1] |= 0x80; cbuf[2] = 0; 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); break; diff --git a/src/apps/terminal/TermView.cpp b/src/apps/terminal/TermView.cpp index 14ea6503a8..1f354f9ebe 100644 --- a/src/apps/terminal/TermView.cpp +++ b/src/apps/terminal/TermView.cpp @@ -133,6 +133,7 @@ TermView::TermView(BRect frame, const char *command) fBufferStartPos(-1), fTermRows(PrefHandler::Default()->getInt32(PREF_ROWS)), fTermColumns(PrefHandler::Default()->getInt32(PREF_COLS)), + fEncoding(M_UTF8), fTop(0), fTextBuffer(new (nothrow) TermBuffer(fTermRows, fTermColumns)), 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 void TermView::SetTermFont(const BFont *halfFont, const BFont *fullFont) @@ -1429,10 +1444,10 @@ TermView::KeyDown(const char *bytes, int32 numBytes) } } else { // input multibyte character - if (GetEncoding() != M_UTF8) { + if (fEncoding != M_UTF8) { uchar dstbuf[1024]; int cnum = CodeConv::ConvertFromInternal(bytes, numBytes, - (char *)dstbuf, GetEncoding()); + (char *)dstbuf, fEncoding); fShell->Write(dstbuf, cnum); } } @@ -1679,10 +1694,10 @@ TermView::DoClearAll(void) void TermView::WritePTY(const uchar *text, int numBytes) { - if (GetEncoding() != M_UTF8) { + if (fEncoding != M_UTF8) { uchar *destBuffer = (uchar *)malloc(numBytes * 3); numBytes = CodeConv::ConvertFromInternal((char*)text, numBytes, - (char*)destBuffer, GetEncoding()); + (char*)destBuffer, fEncoding); fShell->Write(destBuffer, numBytes); free(destBuffer); } else { @@ -1839,12 +1854,14 @@ TermView::Select(CurPos start, CurPos end) if (fTextBuffer->GetChar(start.y, start.x, buf, &attr) == IN_STRING) { 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) { end.x++; - if (end.x >= fTermColumns) end.x = fTermColumns; + if (end.x >= fTermColumns) + end.x = fTermColumns; } fSelStart = start; diff --git a/src/apps/terminal/TermView.h b/src/apps/terminal/TermView.h index 77fa08acb1..830a97eedd 100644 --- a/src/apps/terminal/TermView.h +++ b/src/apps/terminal/TermView.h @@ -68,6 +68,9 @@ public: BRect SetTermSize(int rows, int cols, bool flag); void SetTermColor(); + int Encoding() const; + void SetEncoding(int encoding); + void SetMouseCursor(); // void SetIMAware (bool); void SetScrollBar(BScrollBar *scrbar); @@ -235,6 +238,8 @@ private: int fTermRows; int fTermColumns; + int fEncoding; + // Terminal view pointer. int fTop; diff --git a/src/apps/terminal/TermWindow.cpp b/src/apps/terminal/TermWindow.cpp index 671c3752f6..ba82130a90 100644 --- a/src/apps/terminal/TermWindow.cpp +++ b/src/apps/terminal/TermWindow.cpp @@ -144,6 +144,7 @@ TermWindow::_InitWindow(const char *command) textframe.top = fMenubar->Bounds().bottom + 1.0; fTermView = new TermView(textframe, command); + fTermView->SetEncoding(longname2id(PrefHandler::Default()->getString(PREF_TEXT_ENCODING))); // Initialize TermView. (font, size and color) fTermView->SetTermFont(&halfFont, &fullFont); @@ -187,7 +188,9 @@ void TermWindow::MenusBeginning() { // 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(); } @@ -261,7 +264,7 @@ TermWindow::_SetupMenu() fEncodingmenu = new BMenu("Font Encoding"); fEncodingmenu->SetRadioMode(true); - MakeEncodingMenu(fEncodingmenu, GetEncoding(), true); + MakeEncodingMenu(fEncodingmenu, true); fHelpmenu->AddItem(fWindowSizeMenu); fHelpmenu->AddItem(fEncodingmenu); // fHelpmenu->AddItem(fNewFontMenu); @@ -382,7 +385,7 @@ TermWindow::MessageReceived(BMessage *message) case MENU_ENCODING: { message->FindInt32 ("op", &coding_id); - SetEncoding(coding_id); + fTermView->SetEncoding(coding_id); break; } // Extended B_SET_PROPERTY. Dispatch this message, @@ -393,7 +396,7 @@ TermWindow::MessageReceived(BMessage *message) message->GetCurrentSpecifier(&i, &spe); if (!strcmp("encode", spe.FindString("property", i))){ message->FindInt32 ("data", &coding_id); - SetEncoding (coding_id); + fTermView->SetEncoding (coding_id); message->SendReply(B_REPLY); } else { @@ -409,7 +412,7 @@ TermWindow::MessageReceived(BMessage *message) message->GetCurrentSpecifier(&i, &spe); if (!strcmp("encode", spe.FindString("property", i))){ BMessage reply(B_REPLY); - reply.AddInt32("result", GetEncoding()); + reply.AddInt32("result", fTermView->Encoding()); message->SendReply(&reply); } else if (!strcmp("tty", spe.FindString("property", i))) {