Skip to content

Commit

Permalink
Hack around Glib::ustring::compose() bug (actually Glib::ustring::For…
Browse files Browse the repository at this point in the history
…matStream::to_string() one)

Remove WIN32_COMPOSE_BUG diversions
  • Loading branch information
BastienDurel committed May 19, 2011
1 parent f8c2f80 commit 1645716
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
42 changes: 33 additions & 9 deletions src/main.cc
Expand Up @@ -28,6 +28,36 @@
#include <giomm/file.h>
#include <glibmm/i18n.h>

#if defined WIN32 && defined WIN32_FORMATSTREAM_BUG_WORKAROUND
/*
* There's a bug in the windows glibmm ustring::FormatStream::to_string()
* The compile-time options make use of g_convert, which cannot convert
* from WCHAR_T to UTF-8. Then we change ustring.h to make to_string()
* calling to_string2(), and implement to_string2() with g_utf16_to_utf8().
*
* This is a ugly hack to avoid recompiling glibmm.
*/
namespace Glib {
ustring ustring::FormatStream::to_string2() const
{
GError* error = 0;

const std::wstring str = stream_.str();

// Avoid going through iconv if wchar_t always contains UTF-16.
glong n_bytes = 0;
const ScopedPtr<char> buf (g_utf16_to_utf8(reinterpret_cast<const gunichar2*>(str.data()),
str.size(), 0, &n_bytes, &error));

if (error)
{
Glib::Error::throw_exception(error);
}

return ustring(buf.get(), buf.get() + n_bytes);
}
}
#endif

#ifdef ENABLE_NLS
# include <libintl.h>
Expand All @@ -53,14 +83,15 @@ main (int argc, char *argv[])
Gtk::Main kit(argc, argv);

#if defined WIN32
std::locale::global(std::locale(""));
SetEnvironmentVariable(L"LANGUAGE", L"C");
SetEnvironmentVariable(L"LC_ALL", L"en");
std::locale::global(std::locale("C"));
#endif

bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);

//Load the Glade file and instiate its widgets:
try
{
NrDeckbuilder NR(kit);
Expand Down Expand Up @@ -518,15 +549,8 @@ void NrDeckbuilder::onTextExportClick()
for (NrCardList::iterator it = currentDeck.begin();
it != currentDeck.end(); ++it)
{
#if defined WIN32_COMPOSE_BUG
char tmp[1024] = { 0 };
snprintf(tmp, 1023, "%d\t%s\t%s\n", it->instanceNum,
it->GetName().raw().c_str(), it->print ? "*" : "");
lOut->write(tmp);
#else
lOut->write(Glib::ustring::compose("%1\t%2\t%3\n", it->instanceNum,
it->GetName(), it->print ? "*" : ""));
#endif
}
}
catch (const Glib::Exception& ex)
Expand Down
6 changes: 0 additions & 6 deletions src/pdf.cc
Expand Up @@ -348,13 +348,7 @@ void ComposePDF(NrCardList& list, HPDF& pdf, const Glib::ustring& name)
currow = 0;
++curpage;
pdf.AddPage();
#if defined WIN32_COMPOSE_BUG
char tmp[1024] = { 0 };
snprintf(tmp, 1023, "%s - Page %d", name.raw().c_str(), curpage);
Glib::ustring title = tmp;
#else
Glib::ustring title = Glib::ustring::compose(_("%1 - Page %2"), name, curpage);
#endif
pdf.WriteText(title, 0, 0, -1, HPDF::TITLE_MARGIN, HPDF::center, 12);
}
}
Expand Down
Binary file modified win32/NR-deckbuilder.suo
Binary file not shown.
5 changes: 4 additions & 1 deletion win32/stdafx.h
Expand Up @@ -15,11 +15,14 @@
#include <giomm/memoryinputstream.h>
#include <giomm/file.h>
#include <glibmm/i18n.h>
#include <windows.h>
#undef min

#define PACKAGE_DATA_DIR ".."
#define UI_FILE PACKAGE_DATA_DIR "/src/nr_deckbuilder.ui"
#define GETTEXT_PACKAGE "nr_deckbuilder"

#define ENABLE_NLS
#define HAVE_HPDF
#define WIN32_COMPOSE_BUG
//#define WIN32_COMPOSE_BUG

0 comments on commit 1645716

Please sign in to comment.