]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/cocoa/icon.mm | |
3 | // Purpose: wxIcon class | |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2003/08/11 | |
7 | // Copyright: (c) 2003 David Elliott | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #include "wx/icon.h" | |
14 | ||
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/bitmap.h" | |
17 | #endif //WX_PRECOMP | |
18 | ||
19 | #include "wx/cocoa/autorelease.h" | |
20 | ||
21 | #import <AppKit/NSImage.h> | |
22 | ||
23 | // ======================================================================== | |
24 | // wxIconRefData | |
25 | // ======================================================================== | |
26 | class wxIconRefData: public wxGDIRefData | |
27 | { | |
28 | friend class wxIcon; | |
29 | public: | |
30 | wxIconRefData(); | |
31 | wxIconRefData( const wxIconRefData& data ); | |
32 | virtual ~wxIconRefData(); | |
33 | ||
34 | virtual bool IsOk() const { return m_ok; } | |
35 | ||
36 | protected: | |
37 | int m_width; | |
38 | int m_height; | |
39 | int m_depth; | |
40 | bool m_ok; | |
41 | int m_numColors; | |
42 | int m_quality; | |
43 | WX_NSImage m_cocoaNSImage; | |
44 | }; | |
45 | ||
46 | #define M_ICONDATA ((wxIconRefData *)m_refData) | |
47 | ||
48 | wxIconRefData::wxIconRefData() | |
49 | { | |
50 | m_ok = false; | |
51 | m_width = 0; | |
52 | m_height = 0; | |
53 | m_depth = 0; | |
54 | m_quality = 0; | |
55 | m_numColors = 0; | |
56 | m_cocoaNSImage = nil; | |
57 | } | |
58 | ||
59 | wxIconRefData::wxIconRefData( const wxIconRefData& data) | |
60 | { | |
61 | m_width = data.m_width; | |
62 | m_height = data.m_height; | |
63 | m_depth = data.m_depth; | |
64 | m_ok = data.m_ok; | |
65 | m_numColors = data.m_numColors; | |
66 | m_quality = data.m_quality; | |
67 | m_cocoaNSImage = [data.m_cocoaNSImage copyWithZone:nil]; | |
68 | } | |
69 | ||
70 | wxIconRefData::~wxIconRefData() | |
71 | { | |
72 | wxAutoNSAutoreleasePool pool; | |
73 | [m_cocoaNSImage release]; | |
74 | m_cocoaNSImage = NULL; | |
75 | } | |
76 | ||
77 | ||
78 | // ======================================================================== | |
79 | // wxIcon | |
80 | // ======================================================================== | |
81 | IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject) | |
82 | ||
83 | wxIcon::wxIcon() | |
84 | { | |
85 | } | |
86 | ||
87 | wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height)) | |
88 | { | |
89 | } | |
90 | ||
91 | wxIcon::wxIcon(const wxString& icon_file, int flags, | |
92 | int desiredWidth, int desiredHeight) | |
93 | ||
94 | { | |
95 | LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight); | |
96 | } | |
97 | ||
98 | wxIcon::~wxIcon() | |
99 | { | |
100 | } | |
101 | ||
102 | wxGDIRefData *wxIcon::CreateGDIRefData() const | |
103 | { | |
104 | return new wxIconRefData; | |
105 | } | |
106 | ||
107 | wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const | |
108 | { | |
109 | return new wxIconRefData(*static_cast<const wxIconRefData *>(data)); | |
110 | } | |
111 | ||
112 | bool wxIcon::CreateFromXpm(const char* const* xpm) | |
113 | { | |
114 | wxBitmap bitmap(xpm); | |
115 | CopyFromBitmap(bitmap); | |
116 | return IsOk(); | |
117 | } | |
118 | ||
119 | bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type, | |
120 | int desiredWidth, int desiredHeight) | |
121 | { | |
122 | wxBitmap bitmap(filename, type); | |
123 | CopyFromBitmap(bitmap); | |
124 | return bitmap.IsOk(); | |
125 | } | |
126 | ||
127 | void wxIcon::CopyFromBitmap(const wxBitmap& bitmap) | |
128 | { | |
129 | UnRef(); | |
130 | m_refData = new wxIconRefData; | |
131 | M_ICONDATA->m_width = bitmap.GetWidth(); | |
132 | M_ICONDATA->m_height = bitmap.GetHeight(); | |
133 | wxAutoNSAutoreleasePool pool; | |
134 | M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain]; | |
135 | M_ICONDATA->m_ok = bitmap.IsOk(); | |
136 | M_ICONDATA->m_numColors = 0; | |
137 | M_ICONDATA->m_quality = 0; | |
138 | } | |
139 | ||
140 | int wxIcon::GetWidth() const | |
141 | { | |
142 | if(!m_refData) | |
143 | return 0; | |
144 | return M_ICONDATA->m_width; | |
145 | } | |
146 | ||
147 | int wxIcon::GetHeight() const | |
148 | { | |
149 | if(!m_refData) | |
150 | return 0; | |
151 | return M_ICONDATA->m_height; | |
152 | } | |
153 | ||
154 | WX_NSImage wxIcon::GetNSImage() const | |
155 | { | |
156 | if(!M_ICONDATA) | |
157 | return nil; | |
158 | return M_ICONDATA->m_cocoaNSImage; | |
159 | } |