Implement creation of wxFont from wxNativeFontInfo.
[wxWidgets.git] / src / cocoa / font.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/font.cpp
3 // Purpose:     wxFont class
4 // Author:      AUTHOR
5 // Modified by:
6 // Created:     ??/??/98
7 // RCS-ID:      $Id$
8 // Copyright:   (c) AUTHOR
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/font.h"
15
16 #ifndef WX_PRECOMP
17     #include "wx/string.h"
18     #include "wx/gdicmn.h"
19 #endif
20
21 #include "wx/fontutil.h"
22 #include "wx/encinfo.h"
23
24 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
25 {
26     friend class WXDLLIMPEXP_FWD_CORE wxFont;
27 public:
28     wxFontRefData()
29     :   m_fontId(0)
30     {
31         Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE,
32              wxT("Geneva"), wxFONTENCODING_DEFAULT);
33     }
34
35     wxFontRefData(const wxFontRefData& data)
36     :   wxGDIRefData()
37     ,   m_fontId(data.m_fontId)
38     ,   m_info(data.m_info)
39     {
40     }
41
42     wxFontRefData(const wxNativeFontInfo& info)
43     :   wxGDIRefData()
44     ,   m_fontId(0)
45     ,   m_info(info)
46     {}
47
48     wxFontRefData(int size,
49                   int family,
50                   int style,
51                   int weight,
52                   bool underlined,
53                   const wxString& faceName,
54                   wxFontEncoding encoding)
55     :   m_fontId(0)
56     {
57         Init(size, family, style, weight, underlined, faceName, encoding);
58     }
59
60     virtual ~wxFontRefData();
61 protected:
62     // common part of all ctors
63     void Init(int size,
64               int family,
65               int style,
66               int weight,
67               bool underlined,
68               const wxString& faceName,
69               wxFontEncoding encoding);
70
71     // font characterstics
72     int            m_fontId;
73     wxNativeFontInfo m_info;
74
75 public:
76 };
77
78 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
79
80 void wxFontRefData::Init(int size, int family, int style, int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding)
81 {
82     m_info.pointSize = size;
83     m_info.family = static_cast<wxFontFamily>(family);
84     m_info.style = static_cast<wxFontStyle>(style);
85     m_info.weight = static_cast<wxFontWeight>(weight);
86     m_info.underlined = underlined;
87     m_info.faceName = faceName;
88     m_info.encoding = encoding;
89 }
90
91 wxFontRefData::~wxFontRefData()
92 {
93     // TODO: delete font data
94 }
95
96 #define M_FONTDATA ((wxFontRefData*)m_refData)
97
98 bool wxFont::Create(const wxNativeFontInfo& nativeFontInfo)
99 {
100     UnRef();
101     m_refData = new wxFontRefData(nativeFontInfo);
102     
103     return true;
104 }
105
106 void wxFont::SetEncoding(wxFontEncoding)
107 {
108 }
109
110 wxFontEncoding wxFont::GetEncoding() const
111 {
112     return wxFontEncoding();
113 }
114
115 int wxFont::GetPointSize() const
116 {
117     wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
118     return M_FONTDATA->m_info.pointSize;
119 }
120
121 bool wxFont::GetUnderlined() const
122 {
123     if(M_FONTDATA)
124         return M_FONTDATA->m_info.underlined;
125     else
126         return false;
127 }
128
129 int wxFont::GetStyle() const
130 {
131     wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
132     return M_FONTDATA->m_info.style;
133 }
134
135 int wxFont::GetFamily() const
136 {
137     wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
138     return M_FONTDATA->m_info.family;
139 }
140
141 int wxFont::GetWeight() const
142 {
143     wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
144     return M_FONTDATA->m_info.weight;
145 }
146
147 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
148 {
149     wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
150     return &M_FONTDATA->m_info;
151 }
152
153 bool wxFont::Create(int pointSize, int family, int style, int weight, bool underlined, const wxString& faceName, wxFontEncoding encoding)
154 {
155     UnRef();
156     m_refData = new wxFontRefData(pointSize, family, style, weight, underlined, faceName, encoding);
157
158     RealizeResource();
159
160     return true;
161 }
162
163 wxFont::~wxFont()
164 {
165 }
166
167 bool wxFont::RealizeResource()
168 {
169     // TODO: create the font (if there is a native font object)
170     return false;
171 }
172
173 void wxFont::Unshare()
174 {
175     // Don't change shared data
176     if (!m_refData)
177     {
178         m_refData = new wxFontRefData();
179     }
180     else
181     {
182         wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
183         UnRef();
184         m_refData = ref;
185     }
186 }
187
188 void wxFont::SetPointSize(int pointSize)
189 {
190     Unshare();
191
192     M_FONTDATA->m_info.pointSize = pointSize;
193
194     RealizeResource();
195 }
196
197 void wxFont::SetFamily(int family)
198 {
199     Unshare();
200
201     M_FONTDATA->m_info.family = static_cast<wxFontFamily>(family);
202
203     RealizeResource();
204 }
205
206 void wxFont::SetStyle(int style)
207 {
208     Unshare();
209
210     M_FONTDATA->m_info.style = static_cast<wxFontStyle>(style);
211
212     RealizeResource();
213 }
214
215 void wxFont::SetWeight(int weight)
216 {
217     Unshare();
218
219     M_FONTDATA->m_info.weight = static_cast<wxFontWeight>(weight);
220
221     RealizeResource();
222 }
223
224 bool wxFont::SetFaceName(const wxString& faceName)
225 {
226     Unshare();
227
228     M_FONTDATA->m_info.faceName = faceName;
229
230     RealizeResource();
231
232     return wxFontBase::SetFaceName(faceName);
233 }
234
235 void wxFont::SetUnderlined(bool underlined)
236 {
237     Unshare();
238
239     M_FONTDATA->m_info.underlined = underlined;
240
241     RealizeResource();
242 }
243
244 /* New font system */
245 wxString wxFont::GetFaceName() const
246 {
247     wxString str;
248     if (M_FONTDATA)
249         str = M_FONTDATA->m_info.faceName;
250     return str;
251 }
252
253 // vim:sts=4:sw=4:et