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