aboutsummaryrefslogtreecommitdiff
path: root/backends/taskbar/macosx/macosx-taskbar.mm
blob: f6d3be55ceef7a14a689f4700b7e0cb86d37ab38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

// Disable symbol overrides so that we can use system headers
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include "common/scummsys.h"

#if defined(MACOSX) && defined(USE_TASKBAR)

#include "backends/taskbar/macosx/macosx-taskbar.h"
#include "common/config-manager.h"
#include "common/file.h"
#include "backends/platform/sdl/macosx/macosx-compat.h"
#include <AppKit/NSApplication.h>
#include <AppKit/NSImage.h>
#include <Foundation/NSString.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSUserDefaults.h>
#include <AppKit/NSImageView.h>
#include <AppKit/NSColor.h>
#include <AppKit/NSBezierPath.h>
#include <CoreFoundation/CFString.h>

// NSDockTile was introduced with Mac OS X 10.5.
// Try provide backward compatibility by avoiding NSDockTile symbols.
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
typedef id NSDockTilePtr;
#else
#include <AppKit/NSDockTile.h>
typedef NSDockTile * NSDockTilePtr;
#endif

NSDockTilePtr _dockTile;
NSImageView *_applicationIconView;
NSImageView *_overlayIconView;

// Using a NSProgressIndicator as a sub-view of the NSDockTile view does not work properly.
// The progress indicator is grayed out and display no progress. So instead the bar is drawn
// manually, which is a bit more work :(

MacOSXTaskbarManager::MacOSXTaskbarManager() : _progress(-1.0) {
	if ([NSApp respondsToSelector:@selector(dockTile)])
		_dockTile = [NSApp dockTile];
	_applicationIconView = nil;
	_overlayIconView = nil;
}

MacOSXTaskbarManager::~MacOSXTaskbarManager() {
	clearApplicationIconView();
}

void MacOSXTaskbarManager::initApplicationIconView() {
	if (_dockTile == nil)
		return;
	if (_applicationIconView == nil) {
		_applicationIconView = [[NSImageView alloc] init];
		[_applicationIconView setImage:[NSApp applicationIconImage]];
		[_dockTile performSelector:@selector(setContentView:) withObject:_applicationIconView];
	}
}

void MacOSXTaskbarManager::clearApplicationIconView() {
	if (_dockTile == nil)
		return;
	[_dockTile performSelector:@selector(setContentView:) withObject:nil];
	[_applicationIconView release];
	_applicationIconView = nil;
}

void MacOSXTaskbarManager::initOverlayIconView() {
	if (_dockTile == nil)
		return;
	if (_overlayIconView == nil) {
		const double overlaySize = 0.75;
		initApplicationIconView();
		NSSize size = [_applicationIconView frame].size;
		_overlayIconView = [[NSImageView alloc] initWithFrame:NSMakeRect(size.width * (1.0-overlaySize), 0.0f, size.width * overlaySize, size.height * overlaySize)];
		[_overlayIconView setImageAlignment:NSImageAlignBottomRight];
		[_applicationIconView addSubview:_overlayIconView];
		[_overlayIconView release];
	}
}

void MacOSXTaskbarManager::clearOverlayIconView() {
	if (_progress < 0.0)
		clearApplicationIconView();
	else
		[_overlayIconView removeFromSuperview];
	_overlayIconView = nil;
}

void MacOSXTaskbarManager::setOverlayIcon(const Common::String &name, const Common::String &description) {
	if (_dockTile == nil)
		return;

    if (name.empty()) {
		clearOverlayIconView();
		[_dockTile performSelector:@selector(display)];
		return;
	}

	Common::String path = getIconPath(name);
	if (path.empty())
		return;

	initOverlayIconView();

	CFStringRef imageFile = CFStringCreateWithCString(0, path.c_str(), kCFStringEncodingASCII);
	NSImage *image = [[NSImage alloc] initWithContentsOfFile:(NSString *)imageFile];
	[_overlayIconView setImage:image];
	[image release];
	CFRelease(imageFile);

	[_dockTile performSelector:@selector(display)];
}

void MacOSXTaskbarManager::setProgressValue(int completed, int total) {
	if (_dockTile == nil)
		return;

	if (total > 0)
		_progress = (double)completed / (double)total;
	else if (_progress < 0)
		_progress = 0.0;

	 NSImage *mainIcon = [[NSApp applicationIconImage] copy];
	double barSize = [mainIcon size].width;
	double progressSize = barSize * _progress;
	[mainIcon lockFocus];
	[[NSColor colorWithDeviceRed:(40.0/255.0) green:(120.0/255.0) blue:(255.0/255.0) alpha:0.78] set];
	[NSBezierPath fillRect:NSMakeRect(0, 0, progressSize, 11)];
	[[NSColor colorWithDeviceRed:(241.0/255.0) green:(241.0/255.0) blue:(241.0/255.0) alpha:0.78] set];
	[NSBezierPath fillRect:NSMakeRect(progressSize, 0, barSize-progressSize, 11)];
	[mainIcon unlockFocus];

	initApplicationIconView();
	[_applicationIconView setImage:mainIcon];
	[mainIcon release];

	[_dockTile performSelector:@selector(display)];
}

void MacOSXTaskbarManager::setProgressState(TaskbarProgressState state) {
	if (_dockTile == nil)
		return;

	// Only support two states: visible and not visible.
	if (state == kTaskbarNoProgress) {
		_progress = -1.0;
		if (_overlayIconView == nil)
			clearApplicationIconView();
		else if (_applicationIconView != nil)
			[_applicationIconView setImage:[NSApp applicationIconImage]];
		return;
	}

	setProgressValue(-1, -1);
}

void MacOSXTaskbarManager::setCount(int count) {
	if (_dockTile == nil)
		return;

	if (count > 0)
		[_dockTile performSelector:@selector(setBadgeLabel:) withObject:[NSString stringWithFormat:@"%d", count]];
	else
		[_dockTile performSelector:@selector(setBadgeLabel:) withObject:nil];
}

void MacOSXTaskbarManager::notifyError() {
	if (_dockTile == nil)
		return;

	// NSImageNameCaution was introduced in 10.6.
	// For compatibility with older systems we should use something else (e.g. overlay label
	// or our own icon).
	//initOverlayIconView();
	//[_overlayIconView setImage:[NSImage imageNamed:NSImageNameCaution]];
	//[_dockTile performSelector:@selector(display)];
}

void MacOSXTaskbarManager::clearError() {
	if (_dockTile == nil)
		return;

    clearOverlayIconView();
	[_dockTile performSelector:@selector(display)];
	return;
}

Common::String MacOSXTaskbarManager::getIconPath(const Common::String& target) {
	// We first try to look for a iconspath configuration variable then
	// fallback to the extra path
	//
	// Icons can be either in a subfolder named "icons" or directly in the path

	Common::String iconsPath = ConfMan.get("iconspath");
	Common::String extraPath = ConfMan.get("extrapath");

#define TRY_ICON_PATH(path) { \
Common::FSNode node((path)); \
if (node.exists()) \
return (path); \
}

	if (!iconsPath.empty()) {
		TRY_ICON_PATH(iconsPath + "/" + target + ".png");
		TRY_ICON_PATH(iconsPath + "/" + ConfMan.get("gameid") + ".png");
		TRY_ICON_PATH(iconsPath + "/icons/" + target + ".png");
		TRY_ICON_PATH(iconsPath + "/icons/" + ConfMan.get("gameid") + ".png");
	}

	if (!extraPath.empty()) {
		TRY_ICON_PATH(extraPath + "/" + target + ".png");
		TRY_ICON_PATH(extraPath + "/" + ConfMan.get("gameid") + ".png");
		TRY_ICON_PATH(extraPath + "/icons/" + target + ".png");
		TRY_ICON_PATH(extraPath + "/icons/" + ConfMan.get("gameid") + ".png");
	}

	return "";
}

void MacOSXTaskbarManager::addRecent(const Common::String &name, const Common::String &description) {
	//warning("[MacOSXTaskbarManager::addRecent] Adding recent list entry: %s (%s)", name.c_str(), description.c_str());

	if (_dockTile == nil)
		return;

	// Store the game, description and icon in user preferences.
	// The NSDockTilePlugin will retrieve them there to list them in the dock tile menu.

	CFStringRef gameName = CFStringCreateWithCString(0, name.c_str(), kCFStringEncodingASCII);
	CFStringRef desc = CFStringCreateWithCString(0, description.c_str(), kCFStringEncodingASCII);

	// First build the dictionary for this game.
	NSMutableDictionary *dict = [[NSMutableDictionary  alloc] init];
	[dict setObject:(NSString *)gameName forKey:@"game"];
	[dict setObject:(NSString *)desc forKey:@"description"];

	// Icon
	Common::String iconPath = getIconPath(name);
	if (!iconPath.empty()) {
		CFStringRef icon = CFStringCreateWithCString(0, iconPath.c_str(), kCFStringEncodingASCII);
		[dict setObject:(NSString *)icon forKey:@"icon"];
		CFRelease(icon);
	}

	// Retrieve the current list of recent items and update it.
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSArray *oldArray = [defaults arrayForKey:@"recentGames"];
	if (oldArray == nil) {
		[defaults setObject:[NSArray arrayWithObject:dict] forKey:@"recentGames"];
	} else {
		NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:oldArray];
		// Insert the new game at the start
		[newArray insertObject:dict atIndex:0];
		// If the game was already present in the array, remove it
		for (unsigned int i = 1 ; i < [newArray count] ; ++i) {
			NSDictionary *oldDict = [newArray objectAtIndex:i];
			if (oldDict == nil)
				continue;
			NSString *oldGame = [oldDict valueForKey:@"game"];
			if (oldGame != nil && [oldGame isEqualToString:(NSString*)gameName]) {
				[newArray removeObjectAtIndex:i];
				break;
			}
		}
		// And make sure we limit the size of the array to 5 games
		if ([newArray count] > 5)
			[newArray removeLastObject];
		[defaults setObject:newArray forKey:@"recentGames"];
		[newArray release];
	}

	// Finally release the dictionary
	[dict release];
	CFRelease(gameName);
	CFRelease(desc);
}



#endif