Skip to content

Commit

Permalink
Fix garbled text displaying unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 authored and 375gnu committed Nov 26, 2020
1 parent eb4e1a6 commit 2f80474
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 155 deletions.
134 changes: 70 additions & 64 deletions src/celestia/winbookmarks.cpp

Large diffs are not rendered by default.

75 changes: 35 additions & 40 deletions src/celestia/wineclipses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ char* MonthNames[12] =

bool InitEclipseFinderColumns(HWND listView)
{
LVCOLUMN lvc;
LVCOLUMN columns[5];
LVCOLUMNW lvc;
LVCOLUMNW columns[5];

lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_CENTER;
lvc.pszText = "";
lvc.pszText = L"";

int nColumns = sizeof(columns) / sizeof(columns[0]);
int i;
Expand All @@ -55,22 +55,28 @@ bool InitEclipseFinderColumns(HWND listView)
columns[i] = lvc;

bind_textdomain_codeset("celestia", CurrentCP());
columns[0].pszText = _("Planet");
wstring header0 = CurrentCPToWide(_("Planet"));
wstring header1 = CurrentCPToWide(_("Satellite"));
wstring header2 = CurrentCPToWide(_("Date"));
wstring header3 = CurrentCPToWide(_("Start"));
wstring header4 = CurrentCPToWide(_("Duration"));

columns[0].pszText = const_cast<wchar_t*>(header0.c_str());
columns[0].cx = 50;
columns[1].pszText = _("Satellite");
columns[1].pszText = const_cast<wchar_t*>(header1.c_str());
columns[1].cx = 65;
columns[2].pszText = _("Date");
columns[2].pszText = const_cast<wchar_t*>(header2.c_str());
columns[2].cx = 80;
columns[3].pszText = _("Start");
columns[3].pszText = const_cast<wchar_t*>(header3.c_str());
columns[3].cx = 55;
columns[4].pszText = _("Duration");
columns[4].pszText = const_cast<wchar_t*>(header4.c_str());
columns[4].cx = 55;
bind_textdomain_codeset("celestia", "UTF8");

for (i = 0; i < nColumns; i++)
{
columns[i].iSubItem = i;
if (ListView_InsertColumn(listView, i, &columns[i]) == -1)
if (SendMessage(listView, LVM_INSERTCOLUMNW, (WPARAM)i, (LPARAM)&columns[i]) == -1)
return false;
}

Expand Down Expand Up @@ -99,38 +105,33 @@ bool InitEclipseFinderItems(HWND listView, const vector<Eclipse>& eclipses)
}


static char callbackScratch[256];
void EclipseFinderDisplayItem(LPNMLVDISPINFOA nm)
static wchar_t callbackScratch[256];
void EclipseFinderDisplayItem(LPNMLVDISPINFOW nm)
{

Eclipse* eclipse = reinterpret_cast<Eclipse*>(nm->item.lParam);
if (eclipse == NULL)
{
nm->item.pszText = "";
nm->item.pszText = L"";
return;
}

size_t maxCount = sizeof(callbackScratch) / sizeof(wchar_t) - 1;
switch (nm->item.iSubItem)
{
case 0:
{
strncpy(callbackScratch, UTF8ToCurrentCP(_(eclipse->planete.c_str())).c_str(), sizeof(callbackScratch) - 1);
wcsncpy(callbackScratch, UTF8ToWide(_(eclipse->planete.c_str())).c_str(), maxCount);
nm->item.pszText = callbackScratch;
}
break;

case 1:
{
if (!strcmp(eclipse->planete.c_str(),"None"))
{
sprintf(callbackScratch,"");
nm->item.pszText = callbackScratch;
}
wcsncpy(callbackScratch, L"", maxCount);
else
{
strncpy(callbackScratch, UTF8ToCurrentCP(_(eclipse->sattelite.c_str())).c_str(), sizeof(callbackScratch) - 1);
nm->item.pszText = callbackScratch;
}
wcsncpy(callbackScratch, UTF8ToWide(_(eclipse->sattelite.c_str())).c_str(), maxCount);
}
break;

Expand All @@ -139,13 +140,12 @@ void EclipseFinderDisplayItem(LPNMLVDISPINFOA nm)
bind_textdomain_codeset("celestia", CurrentCP());
astro::Date startDate(eclipse->startTime);
if (!strcmp(eclipse->planete.c_str(),"None"))
sprintf(callbackScratch,"");
wcsncpy(callbackScratch, L"", maxCount);
else
sprintf(callbackScratch, "%2d %s %4d",
startDate.day,
_(MonthNames[startDate.month - 1]),
startDate.year);
nm->item.pszText = callbackScratch;
swprintf(callbackScratch, L"%2d %s %4d",
startDate.day,
CurrentCPToWide(_(MonthNames[startDate.month - 1])).c_str(),
startDate.year);
bind_textdomain_codeset("celestia", "UTF8");
}
break;
Expand All @@ -154,33 +154,27 @@ void EclipseFinderDisplayItem(LPNMLVDISPINFOA nm)
{
astro::Date startDate(eclipse->startTime);
if (!strcmp(eclipse->planete.c_str(),"None"))
sprintf(callbackScratch,"");
wcsncpy(callbackScratch, L"", maxCount);
else
{
sprintf(callbackScratch, "%02d:%02d",
swprintf(callbackScratch, L"%02d:%02d",
startDate.hour, startDate.minute);
}
nm->item.pszText = callbackScratch;
}
break;

case 4:
{
if (!strcmp(eclipse->planete.c_str(),"None"))
{
sprintf(callbackScratch,"");
nm->item.pszText = callbackScratch;
}
wcsncpy(callbackScratch, L"", maxCount);
else
{
int minutes = (int) ((eclipse->endTime - eclipse->startTime) *
24 * 60);
sprintf(callbackScratch, "%02d:%02d", minutes / 60, minutes % 60);
nm->item.pszText = callbackScratch;
swprintf(callbackScratch, L"%02d:%02d", minutes / 60, minutes % 60);
}
}
break;
}
nm->item.pszText = callbackScratch;
}


Expand Down Expand Up @@ -308,6 +302,7 @@ BOOL APIENTRY EclipseFinderProc(HWND hDlg,
//SetWindowLong(hDlg, DWL_USER, lParam);
SetWindowLongPtr(hDlg, DWLP_USER, lParam);
HWND hwnd = GetDlgItem(hDlg, IDC_ECLIPSES_LIST);
ListView_SetUnicodeFormat(hwnd, TRUE);
InitEclipseFinderColumns(hwnd);
SendDlgItemMessage(hDlg, IDC_ECLIPSES_LIST, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);

Expand Down Expand Up @@ -446,8 +441,8 @@ BOOL APIENTRY EclipseFinderProc(HWND hDlg,
{
switch(hdr->code)
{
case LVN_GETDISPINFO:
EclipseFinderDisplayItem((LPNMLVDISPINFOA) lParam);
case LVN_GETDISPINFOW:
EclipseFinderDisplayItem((LPNMLVDISPINFOW) lParam);
break;
case LVN_ITEMCHANGED:
{
Expand Down
11 changes: 6 additions & 5 deletions src/celestia/winmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,14 +1241,15 @@ BOOL APIENTRY OrganizeBookmarksProc(HWND hDlg,
if (hBookmarkTree = GetDlgItem(hDlg, IDC_ORGANIZE_BOOKMARK_TREE))
{
HTREEITEM hItem;
TVITEM tvItem;
TVITEMW tvItem;
if (hItem = TreeView_GetSelection(hBookmarkTree))
{
tvItem.hItem = hItem;
tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
tvItem.pszText = bookmarkName;
wstring itemName = CurrentCPToWide(bookmarkName);
tvItem.pszText = const_cast<wchar_t*>(itemName.c_str());
tvItem.cchTextMax = sizeof(bookmarkName);
if (TreeView_GetItem(hBookmarkTree, &tvItem))
if (SendMessage(hBookmarkTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
{
DialogBox(hRes,
MAKEINTRESOURCE(IDD_RENAME_BOOKMARK),
Expand Down Expand Up @@ -1299,15 +1300,15 @@ BOOL APIENTRY OrganizeBookmarksProc(HWND hDlg,
{
//Do not allow folders to be dragged
HWND hTree;
TVITEM tvItem;
TVITEMW tvItem;
LPNMTREEVIEW nm = (LPNMTREEVIEW)lParam;
HTREEITEM hItem = nm->itemNew.hItem;

if (hTree = GetDlgItem(hDlg, IDC_ORGANIZE_BOOKMARK_TREE))
{
tvItem.hItem = hItem;
tvItem.mask = TVIF_PARAM | TVIF_HANDLE;
if (TreeView_GetItem(hTree, &tvItem))
if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
{
if(tvItem.lParam != 1)
{
Expand Down
18 changes: 9 additions & 9 deletions src/celestia/winssbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ using namespace std;
HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel, void* data,
HTREEITEM parent)
{
TVITEM tvi;
TVINSERTSTRUCT tvins;
static HTREEITEM hPrev = (HTREEITEM) TVI_FIRST;
TVITEMW tvi;
TVINSERTSTRUCTW tvins;
static HTREEITEM hPrev = (HTREEITEM) TVI_FIRST;

#if 0
tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
#endif
tvi.mask = TVIF_TEXT | TVIF_PARAM;

// Set the text of the item.
tvi.pszText = lpszItem;
// Set the text of the item.
wstring itemText = CurrentCPToWide(lpszItem);
tvi.pszText = const_cast<wchar_t *>(itemText.c_str());
tvi.cchTextMax = lstrlen(lpszItem);

// Save the heading level in the item's application-defined
Expand All @@ -48,19 +49,18 @@ HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel, void* data,
tvins.hParent = parent;

// Add the item to the tree view control.
hPrev = (HTREEITEM) SendMessage(hwndTV, TVM_INSERTITEM, 0,
(LPARAM) (LPTVINSERTSTRUCT) &tvins);
hPrev = (HTREEITEM) SendMessage(hwndTV, TVM_INSERTITEMW, 0, (LPARAM)&tvins);

#if 0
// The new item is a child item. Give the parent item a
// closed folder bitmap to indicate it now has child items.
if (nLevel > 1)
{
hti = TreeView_GetParent(hwndTV, hPrev);
tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE; s
tvi.hItem = hti;
// tvi.iImage = g_nClosed;
// tvi.iSelectedImage = g_nClosed;
// tvi.iSelectedImage = g_nClosed; i
TreeView_SetItem(hwndTV, &tvi);
}
#endif
Expand Down
Loading

0 comments on commit 2f80474

Please sign in to comment.