From ad810909f04613a441622ffa2cdea57bdc9cafe3 Mon Sep 17 00:00:00 2001 From: "erik%netscape.com" Date: Sat, 12 Jun 1999 21:09:16 +0000 Subject: [PATCH] Support for non-Latin-1 menus. We now convert from Unicode to the locale's encoding, and use a set of default fonts in the non-Latin-1 case only. git-svn-id: svn://10.0.0.236/trunk@35030 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/widget/src/gtk/nsMenuBar.cpp | 8 +-- mozilla/widget/src/gtk/nsMenuItem.cpp | 74 +++++++++++++++++++++++++-- mozilla/widget/src/gtk/nsMenuItem.h | 2 + 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/mozilla/widget/src/gtk/nsMenuBar.cpp b/mozilla/widget/src/gtk/nsMenuBar.cpp index bfcfddef8ba..026f1ac062d 100644 --- a/mozilla/widget/src/gtk/nsMenuBar.cpp +++ b/mozilla/widget/src/gtk/nsMenuBar.cpp @@ -19,6 +19,7 @@ #include #include "nsMenuBar.h" +#include "nsMenuItem.h" #include "nsIComponentManager.h" #include "nsIDOMNode.h" #include "nsIMenu.h" @@ -130,7 +131,6 @@ NS_METHOD nsMenuBar::AddMenu(nsIMenu * aMenu) { nsString Label; GtkWidget *widget, *nmenu; - char *labelStr; void *voidData; nsISupports * supports = nsnull; @@ -142,14 +142,10 @@ NS_METHOD nsMenuBar::AddMenu(nsIMenu * aMenu) aMenu->GetLabel(Label); - labelStr = Label.ToNewCString(); - - widget = gtk_menu_item_new_with_label (labelStr); + widget = nsMenuItem::CreateLocalized(Label); gtk_widget_show(widget); gtk_menu_bar_append (GTK_MENU_BAR (mMenuBar), widget); - delete[] labelStr; - aMenu->GetNativeData(&voidData); nmenu = GTK_WIDGET(voidData); diff --git a/mozilla/widget/src/gtk/nsMenuItem.cpp b/mozilla/widget/src/gtk/nsMenuItem.cpp index ad9bd43323c..6f0bafa959e 100644 --- a/mozilla/widget/src/gtk/nsMenuItem.cpp +++ b/mozilla/widget/src/gtk/nsMenuItem.cpp @@ -35,6 +35,9 @@ #include "nsIDocumentViewer.h" #include "nsIPresContext.h" #include "nsIWebShell.h" +#include "nsICharsetConverterManager.h" +#include "nsIPlatformCharset.h" +#include "nsIServiceManager.h" static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); @@ -157,6 +160,73 @@ nsIWidget * nsMenuItem::GetMenuBarParent(nsISupports * aParent) return nsnull; } +GtkWidget* +nsMenuItem::CreateLocalized(const nsString& aLabel) +{ + nsresult result; + static nsIUnicodeEncoder* converter = nsnull; + static int isLatin1 = 0; + static int initialized = 0; + if (!initialized) { + initialized = 1; + result = NS_ERROR_FAILURE; + NS_WITH_SERVICE(nsIPlatformCharset, platform, NS_PLATFORMCHARSET_PROGID, + &result); + if (platform && NS_SUCCEEDED(result)) { + nsAutoString charset(""); + result = platform->GetCharset(kPlatformCharsetSel_Menu, charset); + if (NS_SUCCEEDED(result) && (charset.Length() > 0)) { + if (!charset.Compare("iso-8859-1", PR_TRUE)) { + isLatin1 = 1; + } + NS_WITH_SERVICE(nsICharsetConverterManager, manager, + NS_CHARSETCONVERTERMANAGER_PROGID, &result); + if (manager && NS_SUCCEEDED(result)) { + result = manager->GetUnicodeEncoder(&charset, &converter); + if (NS_FAILED(result) && converter) { + NS_RELEASE(converter); + converter = nsnull; + } + else if (converter) { + result = converter->SetOutputErrorBehavior( + nsIUnicodeEncoder::kOnError_Replace, nsnull, '?'); + } + } + } + } + } + + GtkWidget* menuItem = nsnull; + + if (converter) { + char labelStr[128]; + PRInt32 srcLen = aLabel.Length(); + PRInt32 destLen = sizeof(labelStr) - 1; + result = converter->Convert(aLabel.GetUnicode(), &srcLen, labelStr, + &destLen); + if ((destLen > 0) && (destLen < ((PRInt32) sizeof(labelStr)))) { + labelStr[destLen] = 0; + menuItem = gtk_menu_item_new_with_label(labelStr); + if (menuItem && (!isLatin1)) { + GtkWidget* label = GTK_BIN(menuItem)->child; + gtk_widget_ensure_style(label); + GtkStyle* style = gtk_style_copy(label->style); + gdk_font_unref(style->font); + style->font = gdk_fontset_load("*"); + gtk_widget_set_style(label, style); + gtk_style_unref(style); + } + } + } + else { + char labelStr[128]; + aLabel.ToCString(labelStr, sizeof(labelStr)); + menuItem = gtk_menu_item_new_with_label(labelStr); + } + + return menuItem; +} + //------------------------------------------------------------------------- NS_METHOD nsMenuItem::Create(nsISupports *aParent, const nsString &aLabel, @@ -191,9 +261,7 @@ NS_METHOD nsMenuItem::Create(nsISupports *aParent, if(mIsSeparator) { mMenuItem = gtk_menu_item_new(); } else { - char *nameStr = aLabel.ToNewCString(); - mMenuItem = gtk_menu_item_new_with_label(nameStr); - delete[] nameStr; + mMenuItem = CreateLocalized(aLabel); } gtk_widget_show(mMenuItem); diff --git a/mozilla/widget/src/gtk/nsMenuItem.h b/mozilla/widget/src/gtk/nsMenuItem.h index ef1d300495f..18f839fdb6c 100644 --- a/mozilla/widget/src/gtk/nsMenuItem.h +++ b/mozilla/widget/src/gtk/nsMenuItem.h @@ -75,6 +75,8 @@ public: void * menuNode, void * aWebShell); nsEventStatus MenuDestruct(const nsMenuEvent & aMenuEvent); + + static GtkWidget* CreateLocalized(const nsString& aLabel); protected: nsIWidget *GetMenuBarParent(nsISupports * aParentSupports);