Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make wxMSW date/time picker controls locale aware #23965

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/wx/msw/datetimectrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class WXDLLIMPEXP_ADV wxDateTimePickerCtrl : public wxDateTimePickerCtrlBase
#if wxUSE_INTL
// Override to return the date/time format used by this control.
virtual wxLocaleInfo MSWGetFormat() const = 0;
void MSWSetTimeFormat(wxLocaleInfo index);
#endif // wxUSE_INTL

// Override to indicate whether we can have no date at all.
Expand Down
2 changes: 2 additions & 0 deletions include/wx/msw/private/uilocale.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@

WXDLLIMPEXP_BASE wxString wxTranslateFromUnicodeFormat(const wxString& fmt);

WXDLLIMPEXP_BASE wxString wxGetMSWDateTimeFormat(wxLocaleInfo index);

#endif // _WX_MSW_PRIVATE_UILOCALE_H_
16 changes: 13 additions & 3 deletions include/wx/msw/timectrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#ifndef _WX_MSW_TIMECTRL_H_
#define _WX_MSW_TIMECTRL_H_

#include "wx/uilocale.h"
#include "wx/msw/private/uilocale.h"

// ----------------------------------------------------------------------------
// wxTimePickerCtrl
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -41,9 +44,16 @@ class WXDLLIMPEXP_ADV wxTimePickerCtrl : public wxTimePickerCtrlBase
const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxTimePickerCtrlNameStr)
{
return MSWCreateDateTimePicker(parent, id, dt,
pos, size, style,
validator, name);
bool ok = MSWCreateDateTimePicker(parent, id, dt,
pos, size, style,
validator, name);
#if wxUSE_INTL
if (ok)
{
MSWSetTimeFormat(wxLOCALE_TIME_FMT);
}
#endif
return ok;
}

// Override MSW-specific functions used during control creation.
Expand Down
15 changes: 12 additions & 3 deletions src/msw/datectrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#include "wx/datectrl.h"
#include "wx/dateevt.h"
#include "wx/uilocale.h"
#include "wx/msw/private/uilocale.h"

wxIMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl);

Expand All @@ -58,9 +60,16 @@ wxDatePickerCtrl::Create(wxWindow *parent,
if ( !(style & wxDP_DROPDOWN) )
style |= wxDP_SPIN;

return MSWCreateDateTimePicker(parent, id, dt,
pos, size, style,
validator, name);
bool ok = MSWCreateDateTimePicker(parent, id, dt,
pos, size, style,
validator, name);
#if wxUSE_INTL
if (ok)
{
MSWSetTimeFormat(wxLOCALE_SHORT_DATE_FMT);
}
#endif
return ok;
}

WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
Expand Down
11 changes: 11 additions & 0 deletions src/msw/datetimectrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#endif // WX_PRECOMP

#include "wx/msw/private/datecontrols.h"
#include "wx/msw/private/uilocale.h"

// apparently some versions of mingw define these macros erroneously
#ifndef DateTime_GetSystemtime
Expand Down Expand Up @@ -122,6 +123,16 @@ wxDateTimePickerCtrl::MSWCreateDateTimePicker(wxWindow *parent,
return true;
}

void wxDateTimePickerCtrl::MSWSetTimeFormat(wxLocaleInfo index)
{
wxString formatStr = wxGetMSWDateTimeFormat(index);
if (!formatStr.empty())
{
const TCHAR* format = formatStr.t_str();
DateTime_SetFormat(GetHwnd(), format);
}
}

void wxDateTimePickerCtrl::SetValue(const wxDateTime& dt)
{
wxCHECK_RET( dt.IsValid() || MSWAllowsNone(),
Expand Down
21 changes: 21 additions & 0 deletions src/msw/uilocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ LCTYPE wxGetLCTYPEFormatFromLocalInfo(wxLocaleInfo index)
return 0;
}

WXDLLIMPEXP_BASE wxString wxGetMSWDateTimeFormat(wxLocaleInfo index)
{
wxString format;
wxString localeName = wxUILocale::GetCurrent().GetName();
const wchar_t* name = localeName.wc_str();
LCTYPE lctype = wxGetLCTYPEFormatFromLocalInfo(index);
if (lctype != 0)
{
wchar_t buf[256];
if (::GetLocaleInfoEx(name, lctype, buf, WXSIZEOF(buf)))
{
format = buf;
}
else
{
wxLogLastError(wxT("GetLocaleInfoEx"));
}
}
return format;
}

// ----------------------------------------------------------------------------
// wxLocaleIdent::GetName() implementation for MSW
// ----------------------------------------------------------------------------
Expand Down
Loading