Skip to content

Commit 2f80474

Browse files
levinli303375gnu
authored andcommitted
Fix garbled text displaying unicode
1 parent eb4e1a6 commit 2f80474

7 files changed

Lines changed: 209 additions & 155 deletions

File tree

src/celestia/winbookmarks.cpp

Lines changed: 70 additions & 64 deletions
Large diffs are not rendered by default.

src/celestia/wineclipses.cpp

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ char* MonthNames[12] =
4141

4242
bool InitEclipseFinderColumns(HWND listView)
4343
{
44-
LVCOLUMN lvc;
45-
LVCOLUMN columns[5];
44+
LVCOLUMNW lvc;
45+
LVCOLUMNW columns[5];
4646

4747
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
4848
lvc.fmt = LVCFMT_CENTER;
49-
lvc.pszText = "";
49+
lvc.pszText = L"";
5050

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

5757
bind_textdomain_codeset("celestia", CurrentCP());
58-
columns[0].pszText = _("Planet");
58+
wstring header0 = CurrentCPToWide(_("Planet"));
59+
wstring header1 = CurrentCPToWide(_("Satellite"));
60+
wstring header2 = CurrentCPToWide(_("Date"));
61+
wstring header3 = CurrentCPToWide(_("Start"));
62+
wstring header4 = CurrentCPToWide(_("Duration"));
63+
64+
columns[0].pszText = const_cast<wchar_t*>(header0.c_str());
5965
columns[0].cx = 50;
60-
columns[1].pszText = _("Satellite");
66+
columns[1].pszText = const_cast<wchar_t*>(header1.c_str());
6167
columns[1].cx = 65;
62-
columns[2].pszText = _("Date");
68+
columns[2].pszText = const_cast<wchar_t*>(header2.c_str());
6369
columns[2].cx = 80;
64-
columns[3].pszText = _("Start");
70+
columns[3].pszText = const_cast<wchar_t*>(header3.c_str());
6571
columns[3].cx = 55;
66-
columns[4].pszText = _("Duration");
72+
columns[4].pszText = const_cast<wchar_t*>(header4.c_str());
6773
columns[4].cx = 55;
6874
bind_textdomain_codeset("celestia", "UTF8");
6975

7076
for (i = 0; i < nColumns; i++)
7177
{
7278
columns[i].iSubItem = i;
73-
if (ListView_InsertColumn(listView, i, &columns[i]) == -1)
79+
if (SendMessage(listView, LVM_INSERTCOLUMNW, (WPARAM)i, (LPARAM)&columns[i]) == -1)
7480
return false;
7581
}
7682

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

101107

102-
static char callbackScratch[256];
103-
void EclipseFinderDisplayItem(LPNMLVDISPINFOA nm)
108+
static wchar_t callbackScratch[256];
109+
void EclipseFinderDisplayItem(LPNMLVDISPINFOW nm)
104110
{
105111

106112
Eclipse* eclipse = reinterpret_cast<Eclipse*>(nm->item.lParam);
107113
if (eclipse == NULL)
108114
{
109-
nm->item.pszText = "";
115+
nm->item.pszText = L"";
110116
return;
111117
}
112118

119+
size_t maxCount = sizeof(callbackScratch) / sizeof(wchar_t) - 1;
113120
switch (nm->item.iSubItem)
114121
{
115122
case 0:
116123
{
117-
strncpy(callbackScratch, UTF8ToCurrentCP(_(eclipse->planete.c_str())).c_str(), sizeof(callbackScratch) - 1);
124+
wcsncpy(callbackScratch, UTF8ToWide(_(eclipse->planete.c_str())).c_str(), maxCount);
118125
nm->item.pszText = callbackScratch;
119126
}
120127
break;
121128

122129
case 1:
123130
{
124131
if (!strcmp(eclipse->planete.c_str(),"None"))
125-
{
126-
sprintf(callbackScratch,"");
127-
nm->item.pszText = callbackScratch;
128-
}
132+
wcsncpy(callbackScratch, L"", maxCount);
129133
else
130-
{
131-
strncpy(callbackScratch, UTF8ToCurrentCP(_(eclipse->sattelite.c_str())).c_str(), sizeof(callbackScratch) - 1);
132-
nm->item.pszText = callbackScratch;
133-
}
134+
wcsncpy(callbackScratch, UTF8ToWide(_(eclipse->sattelite.c_str())).c_str(), maxCount);
134135
}
135136
break;
136137

@@ -139,13 +140,12 @@ void EclipseFinderDisplayItem(LPNMLVDISPINFOA nm)
139140
bind_textdomain_codeset("celestia", CurrentCP());
140141
astro::Date startDate(eclipse->startTime);
141142
if (!strcmp(eclipse->planete.c_str(),"None"))
142-
sprintf(callbackScratch,"");
143+
wcsncpy(callbackScratch, L"", maxCount);
143144
else
144-
sprintf(callbackScratch, "%2d %s %4d",
145-
startDate.day,
146-
_(MonthNames[startDate.month - 1]),
147-
startDate.year);
148-
nm->item.pszText = callbackScratch;
145+
swprintf(callbackScratch, L"%2d %s %4d",
146+
startDate.day,
147+
CurrentCPToWide(_(MonthNames[startDate.month - 1])).c_str(),
148+
startDate.year);
149149
bind_textdomain_codeset("celestia", "UTF8");
150150
}
151151
break;
@@ -154,33 +154,27 @@ void EclipseFinderDisplayItem(LPNMLVDISPINFOA nm)
154154
{
155155
astro::Date startDate(eclipse->startTime);
156156
if (!strcmp(eclipse->planete.c_str(),"None"))
157-
sprintf(callbackScratch,"");
157+
wcsncpy(callbackScratch, L"", maxCount);
158158
else
159-
{
160-
sprintf(callbackScratch, "%02d:%02d",
159+
swprintf(callbackScratch, L"%02d:%02d",
161160
startDate.hour, startDate.minute);
162-
}
163-
nm->item.pszText = callbackScratch;
164161
}
165162
break;
166163

167164
case 4:
168165
{
169166
if (!strcmp(eclipse->planete.c_str(),"None"))
170-
{
171-
sprintf(callbackScratch,"");
172-
nm->item.pszText = callbackScratch;
173-
}
167+
wcsncpy(callbackScratch, L"", maxCount);
174168
else
175169
{
176170
int minutes = (int) ((eclipse->endTime - eclipse->startTime) *
177171
24 * 60);
178-
sprintf(callbackScratch, "%02d:%02d", minutes / 60, minutes % 60);
179-
nm->item.pszText = callbackScratch;
172+
swprintf(callbackScratch, L"%02d:%02d", minutes / 60, minutes % 60);
180173
}
181174
}
182175
break;
183176
}
177+
nm->item.pszText = callbackScratch;
184178
}
185179

186180

@@ -308,6 +302,7 @@ BOOL APIENTRY EclipseFinderProc(HWND hDlg,
308302
//SetWindowLong(hDlg, DWL_USER, lParam);
309303
SetWindowLongPtr(hDlg, DWLP_USER, lParam);
310304
HWND hwnd = GetDlgItem(hDlg, IDC_ECLIPSES_LIST);
305+
ListView_SetUnicodeFormat(hwnd, TRUE);
311306
InitEclipseFinderColumns(hwnd);
312307
SendDlgItemMessage(hDlg, IDC_ECLIPSES_LIST, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
313308

@@ -446,8 +441,8 @@ BOOL APIENTRY EclipseFinderProc(HWND hDlg,
446441
{
447442
switch(hdr->code)
448443
{
449-
case LVN_GETDISPINFO:
450-
EclipseFinderDisplayItem((LPNMLVDISPINFOA) lParam);
444+
case LVN_GETDISPINFOW:
445+
EclipseFinderDisplayItem((LPNMLVDISPINFOW) lParam);
451446
break;
452447
case LVN_ITEMCHANGED:
453448
{

src/celestia/winmain.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,14 +1241,15 @@ BOOL APIENTRY OrganizeBookmarksProc(HWND hDlg,
12411241
if (hBookmarkTree = GetDlgItem(hDlg, IDC_ORGANIZE_BOOKMARK_TREE))
12421242
{
12431243
HTREEITEM hItem;
1244-
TVITEM tvItem;
1244+
TVITEMW tvItem;
12451245
if (hItem = TreeView_GetSelection(hBookmarkTree))
12461246
{
12471247
tvItem.hItem = hItem;
12481248
tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
1249-
tvItem.pszText = bookmarkName;
1249+
wstring itemName = CurrentCPToWide(bookmarkName);
1250+
tvItem.pszText = const_cast<wchar_t*>(itemName.c_str());
12501251
tvItem.cchTextMax = sizeof(bookmarkName);
1251-
if (TreeView_GetItem(hBookmarkTree, &tvItem))
1252+
if (SendMessage(hBookmarkTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
12521253
{
12531254
DialogBox(hRes,
12541255
MAKEINTRESOURCE(IDD_RENAME_BOOKMARK),
@@ -1299,15 +1300,15 @@ BOOL APIENTRY OrganizeBookmarksProc(HWND hDlg,
12991300
{
13001301
//Do not allow folders to be dragged
13011302
HWND hTree;
1302-
TVITEM tvItem;
1303+
TVITEMW tvItem;
13031304
LPNMTREEVIEW nm = (LPNMTREEVIEW)lParam;
13041305
HTREEITEM hItem = nm->itemNew.hItem;
13051306

13061307
if (hTree = GetDlgItem(hDlg, IDC_ORGANIZE_BOOKMARK_TREE))
13071308
{
13081309
tvItem.hItem = hItem;
13091310
tvItem.mask = TVIF_PARAM | TVIF_HANDLE;
1310-
if (TreeView_GetItem(hTree, &tvItem))
1311+
if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
13111312
{
13121313
if(tvItem.lParam != 1)
13131314
{

src/celestia/winssbrowser.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ using namespace std;
2626
HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel, void* data,
2727
HTREEITEM parent)
2828
{
29-
TVITEM tvi;
30-
TVINSERTSTRUCT tvins;
31-
static HTREEITEM hPrev = (HTREEITEM) TVI_FIRST;
29+
TVITEMW tvi;
30+
TVINSERTSTRUCTW tvins;
31+
static HTREEITEM hPrev = (HTREEITEM) TVI_FIRST;
3232

3333
#if 0
3434
tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
3535
#endif
3636
tvi.mask = TVIF_TEXT | TVIF_PARAM;
3737

38-
// Set the text of the item.
39-
tvi.pszText = lpszItem;
38+
// Set the text of the item.
39+
wstring itemText = CurrentCPToWide(lpszItem);
40+
tvi.pszText = const_cast<wchar_t *>(itemText.c_str());
4041
tvi.cchTextMax = lstrlen(lpszItem);
4142

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

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

5454
#if 0
5555
// The new item is a child item. Give the parent item a
5656
// closed folder bitmap to indicate it now has child items.
5757
if (nLevel > 1)
5858
{
5959
hti = TreeView_GetParent(hwndTV, hPrev);
60-
tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
60+
tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE; s
6161
tvi.hItem = hti;
6262
// tvi.iImage = g_nClosed;
63-
// tvi.iSelectedImage = g_nClosed;
63+
// tvi.iSelectedImage = g_nClosed; i
6464
TreeView_SetItem(hwndTV, &tvi);
6565
}
6666
#endif

0 commit comments

Comments
 (0)