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