OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
codepages.h
Go to the documentation of this file.
1//
2/// \file codepages.h
3/// Utilities for accessing and setting code page information
4//
5// Part of OWLNext - the next generation Object Windows Library
6// Copyright (c) 2014 by Joe Slater
7//
8// For more information, including license details, see
9// http://owlnext.sourceforge.net
10//
11
12#if !defined(OWL_CODEPAGES_H)
13#define OWL_CODEPAGES_H
14
15#include <owl/private/defs.h>
16#if defined(BI_HAS_PRAGMA_ONCE)
17# pragma once
18#endif
19
20#if defined(__WINCE__)
21#define OWL_SET_OEMCP_(idCodePage) static_cast<bool>(::SetOEMCP(static_cast<UINT>(idCodePage)))
22#define OWL_SET_ANSICP_(idCodePage) static_cast<bool>(::SetACP(static_cast<UINT>(idCodePage)))
23#else
24#define OWL_SET_OEMCP_(idCodePage) false
25#define OWL_SET_ANSICP_(idCodePage) false
26#endif
27
28#include <vector>
29#include <mbctype.h>
30
31namespace owl
32{
33
34//
35/// Encapsulates Windows code page functionality.
36//
37/// TCodePages is a static class and instantiation is not allowed.
38//
39/// The code page list does not get loaded until accessed using `GetCodePageList`
40/// or `GetIndex`. Loading is performed only once and is persistent until the
41/// application terminates.
42//
43/// The following sample code illustrates loading the code page list in a TComboBox
44/// control, retrieving the current code page identifier, initializing the selection of
45/// the control to the currently selected code page identifier, and changing the
46/// current code page to the user selection.
47//
48/// \code
49/// TComboBox* cbox = new TComboBox(/*...*/);
50/// // ...
51/// for (auto &cp : TCodePages::GetCodePageList())
52/// cbox->AddString(cp.Name);
53/// auto iCur = TCodePages::GetIndex(TCodePages::GetAnsiCodePageId());
54/// if (iCur != -1) cbox->SetSelIndex(iCur);
55/// // ...
56/// auto iSel = cbox->GetSelIndex();
57/// if (iSel != -1) TCodePages::SetAnsiCodePageId(TCodePages::GetCodePageList().at(iSel).CodePageId);
58/// \endcode
59//
61{
62
63 public:
64 //
65 /// Define the code page, code page object, and container types.
66 //
68 using TCodePage = struct { TCodePageId CodePageId; bool Installed; tstring Name; };
69 using TCodePageList = std::vector<TCodePage>;
70
71 //
72 /// \name Code Page List Operations
73 /// @{
74
75 static auto GetCodePageList() -> const TCodePageList&;
76 static auto GetIndex(TCodePageId idCodePage) -> int;
77
78 /// @}
79
80 //
81 /// \name Code Page WINAPI Wrapper Operations
82 /// @{
83
84 //
85 /// Retrieves the current OEM code page identifier.
86 //
87 /// Wrapper function for the Windows API `GetOEMCP`.
88 //
89 /// \return current OEM code page identifier.
90 //
91 /// \sa http://msdn.microsoft.com/en-us/library/windows/desktop/dd318114.aspx
92 //
94 { return static_cast<TCodePageId>(::GetOEMCP()); }
95
96 //
97 /// Sets the current OEM code page identifier.
98 //
99 /// Wrapper function for the Windows API `SetOEMCP`.
100 //
101 /// @param[in] idCodePage is the code page identifier to set.
102 //
103 /// \note Avaiable for WINCE only.
104 //
105 /// \return true if successful.
106 //
107 /// \sa http://msdn.microsoft.com/en-us/library/gg155426.aspx
108 //
110 { return OWL_SET_OEMCP_(idCodePage); }
111
112 //
113 /// Retrieves the current ANSI code page identifier.
114 //
115 /// Wrapper function for the Windows API `GetACP`.
116 //
117 /// \return current ANSI code page identifier.
118 //
119 /// \sa http://msdn.microsoft.com/en-us/library/windows/desktop/dd318070.aspx
120 //
122 { return static_cast<TCodePageId>(::GetACP()); }
123
124 //
125 /// Sets the current ANSI code page identifier.
126 //
127 /// Wrapper function for the Windows API `SetACP`.
128 //
129 /// @param[in] idCodePage is the code page identifier to set.
130 //
131 /// \note Avaiable for WINCE only.
132 //
133 /// \return true if successful.
134 //
135 /// \sa http://msdn.microsoft.com/en-us/library/gg155975.aspx
136 //
139
140 //
141 /// Retieves a CPINFOEX structure.
142 //
143 /// Wrapper function for the Windows API `GetCPInfoEx`.
144 //
145 /// @param[in] idCodePage is the code page identifier to use.
146 //
147 /// \return the CPINFOEX structure.
148 //
149 /// \sa http://msdn.microsoft.com/en-us/library/windows/desktop/dd318081.aspx
150 /// \sa http://msdn.microsoft.com/en-us/library/windows/desktop/dd317781.aspx
151 //
153 {
154 CPINFOEX cpInfoEx = { 0 };
156 return cpInfoEx;
157 }
158
159 //
160 /// Retieves a CPINFO structure.
161 //
162 /// Wrapper function for the Windows API `GetCPInfo`.
163 //
164 /// @param[in] idCodePage is the code page identifier to use.
165 //
166 /// \return the CPINFO structure.
167 //
168 /// \sa http://msdn.microsoft.com/en-us/library/windows/desktop/dd318078.aspx
169 /// \sa http://msdn.microsoft.com/en-us/library/windows/desktop/dd317780.aspx
170 //
172 {
173 CPINFO cpInfo = { 0 };
175 return cpInfo;
176 }
177
178 //
179 /// Retieves the multi-byte code page identifier.
180 //
181 /// Wrapper function for the Windows API `_getmbcp`.
182 //
183 /// \return the multi-byte code page identifier.
184 //
185 /// \sa http://msdn.microsoft.com/en-us/library/4115fwez.aspx
186 //
188 { return static_cast<TCodePageId>(::_getmbcp()); }
189
190 //
191 /// Sets the multi-byte code page identifier.
192 //
193 /// Wrapper function for the Windows API `_setmbcp`.
194 //
195 /// @param[in] idCodePage is the code page identifier to set.
196 //
197 /// \return true if successful.
198 //
199 /// \sa http://msdn.microsoft.com/en-us/library/883tf19a.aspx
200 //
202 { return !::_setmbcp(static_cast<int>(idCodePage)); }
203
204 /// @}
205
206 private:
207 //
208 /// \name Code Page Internal Operations
209 /// @{
210
211 //
212 /// Internally implements the Meyers Singleton technique for building the code page list once, thread-safely.
213 //
214 static auto GetInstance() -> const TCodePages&
215 {
216 static TCodePages c;
217 return c;
218 }
219
220 TCodePages();
221
222 /// @}
223
224}; // class TCodePages
225
226} // owl namespace
227
228// Release macro definitions; they need not persist.
229//
230#undef OWL_SET_OEMCP_
231#undef OWL_SET_ANSICP_
232
233#endif // OWL_CODEPAGES_H
Encapsulates Windows code page functionality.
Definition codepages.h:61
static auto GetIndex(TCodePageId idCodePage) -> int
Searches the code page list for a code page identifier.
Definition codepages.cpp:86
static auto SetMultiByteCodePage(TCodePageId idCodePage) -> bool
Sets the multi-byte code page identifier.
Definition codepages.h:201
std::vector< TCodePage > TCodePageList
Definition codepages.h:69
static auto SetOemCodePageId(TCodePageId idCodePage) -> bool
Sets the current OEM code page identifier.
Definition codepages.h:109
static auto SetAnsiCodePageId(TCodePageId idCodePage) -> bool
Sets the current ANSI code page identifier.
Definition codepages.h:137
static auto GetOemCodePageId() -> TCodePageId
Retrieves the current OEM code page identifier.
Definition codepages.h:93
static auto GetAnsiCodePageId() -> TCodePageId
Retrieves the current ANSI code page identifier.
Definition codepages.h:121
static auto GetMultiByteCodePage() -> TCodePageId
Retieves the multi-byte code page identifier.
Definition codepages.h:187
static auto GetCodePageInfo(TCodePageId idCodePage) -> CPINFO
Retieves a CPINFO structure.
Definition codepages.h:171
static auto GetCodePageList() -> const TCodePageList &
Retrieves the code page list.
Definition codepages.cpp:98
uint TCodePageId
Define the code page, code page object, and container types.
Definition codepages.h:67
static auto GetCodePageInfoEx(TCodePageId idCodePage) -> CPINFOEX
Retieves a CPINFOEX structure.
Definition codepages.h:152
#define OWL_SET_OEMCP_(idCodePage)
Definition codepages.h:24
#define OWL_SET_ANSICP_(idCodePage)
Definition codepages.h:25
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25