1/*
2 * PrinterCap.cpp
3 * Copyright 1999-2000 Y.Takagi. All Rights Reserved.
4 */
5
6#include "PrinterCap.h"
7#include "PrinterData.h"
8
9BaseCap::BaseCap(const string &label)
10	:
11	fLabel(label)
12{
13}
14
15
16BaseCap::~BaseCap()
17{
18}
19
20
21const char*
22BaseCap::Label() const
23{
24	return fLabel.c_str();
25}
26
27
28EnumCap::EnumCap(const string &label, bool isDefault)
29	:
30	BaseCap(label),
31	fIsDefault(isDefault)
32{
33}
34
35
36const char*
37EnumCap::Key() const
38{
39	return fKey.c_str();
40}
41
42
43PaperCap::PaperCap(const string &label, bool isDefault, JobData::Paper paper,
44	const BRect &paperRect, const BRect &physicalRect)
45	:
46	EnumCap(label, isDefault),
47	fPaper(paper),
48	fPaperRect(paperRect),
49	fPhysicalRect(physicalRect)
50{
51}
52
53
54int32
55PaperCap::ID() const
56{
57	return fPaper;
58}
59
60
61PaperSourceCap::PaperSourceCap(const string &label, bool isDefault,
62	JobData::PaperSource paperSource)
63	:
64	EnumCap(label, isDefault),
65	fPaperSource(paperSource)
66{
67}
68
69
70int32
71PaperSourceCap::ID() const
72{
73	return fPaperSource;
74}
75
76
77ResolutionCap::ResolutionCap(const string &label, bool isDefault,
78	int32 id, int xResolution, int yResolution)
79	:
80	EnumCap(label, isDefault),
81	fID(id),
82	fXResolution(xResolution),
83	fYResolution(yResolution)
84{
85}
86
87
88int32
89ResolutionCap::ID() const
90{
91	return fID;
92}
93
94
95OrientationCap::OrientationCap(const string &label, bool isDefault,
96	JobData::Orientation orientation)
97	:
98	EnumCap(label, isDefault),
99	fOrientation(orientation)
100{
101}
102
103
104int32
105OrientationCap::ID() const
106{
107	return fOrientation;
108}
109
110
111PrintStyleCap::PrintStyleCap(const string &label, bool isDefault,
112	JobData::PrintStyle printStyle)
113	:
114	EnumCap(label, isDefault),
115	fPrintStyle(printStyle)
116{
117}
118
119
120int32
121PrintStyleCap::ID() const
122{
123	return fPrintStyle;
124}
125
126
127BindingLocationCap::BindingLocationCap(const string &label, bool isDefault,
128	JobData::BindingLocation bindingLocation)
129	:
130	EnumCap(label, isDefault),
131	fBindingLocation(bindingLocation)
132{
133}
134
135
136int32
137BindingLocationCap::ID() const
138{
139	return fBindingLocation;
140}
141
142
143ColorCap::ColorCap(const string &label, bool isDefault, JobData::Color color)
144	:
145	EnumCap(label, isDefault),
146	fColor(color)
147{
148}
149
150
151int32
152ColorCap::ID() const
153{
154	return fColor;
155}
156
157
158ProtocolClassCap::ProtocolClassCap(const string &label, bool isDefault,
159	int32 protocolClass, const string &description)
160	:
161	EnumCap(label, isDefault),
162	fProtocolClass(protocolClass),
163	fDescription(description)
164{
165}
166
167
168int32
169ProtocolClassCap::ID() const
170{
171	return fProtocolClass;
172}
173
174
175DriverSpecificCap::DriverSpecificCap(const string& label, int32 category,
176	Type type)
177	:
178	EnumCap(label, false),
179	fCategory(category),
180	fType(type)
181{
182}
183
184
185int32
186DriverSpecificCap::ID() const
187{
188	return fCategory;
189}
190
191
192ListItemCap::ListItemCap(const string& label, bool isDefault, int32 id)
193	:
194	EnumCap(label, isDefault),
195	fID(id)
196{
197}
198
199
200int32
201ListItemCap::ID() const
202{
203	return fID;
204}
205
206
207BooleanCap::BooleanCap(const string& label, bool defaultValue)
208	:
209	BaseCap(label),
210	fDefaultValue(defaultValue)
211{
212}
213
214
215bool
216BooleanCap::DefaultValue() const
217{
218	return fDefaultValue;
219}
220
221
222IntRangeCap::IntRangeCap(const string& label, int lower, int upper,
223	int defaultValue)
224	:
225	BaseCap(label),
226	fLower(lower),
227	fUpper(upper),
228	fDefaultValue(defaultValue)
229{
230}
231
232
233int32
234IntRangeCap::Lower() const
235{
236	return fLower;
237}
238
239
240int32
241IntRangeCap::Upper() const
242{
243	return fUpper;
244}
245
246
247int32
248IntRangeCap::DefaultValue() const
249{
250	return fDefaultValue;
251}
252
253
254DoubleRangeCap::DoubleRangeCap(const string& label, double lower, double upper,
255	double defaultValue)
256	:
257	BaseCap(label),
258	fLower(lower),
259	fUpper(upper),
260	fDefaultValue(defaultValue)
261{
262}
263
264
265double
266DoubleRangeCap::Lower() const
267{
268	return fLower;
269}
270
271
272double
273DoubleRangeCap::Upper() const
274{
275	return fUpper;
276}
277
278
279double
280DoubleRangeCap::DefaultValue() const
281{
282	return fDefaultValue;
283}
284
285
286PrinterCap::PrinterCap(const PrinterData *printer_data)
287	:
288	fPrinterData(printer_data)
289{
290}
291
292
293PrinterCap::~PrinterCap()
294{
295}
296
297
298const EnumCap*
299PrinterCap::GetDefaultCap(CapID category) const
300{
301	int count = CountCap(category);
302	if (count <= 0)
303		return NULL;
304
305	const BaseCap **base_cap = GetCaps(category);
306	while (count--) {
307		const EnumCap* enumCap = dynamic_cast<const EnumCap*>(*base_cap);
308		if (enumCap == NULL)
309			return NULL;
310
311		if (enumCap->fIsDefault) {
312			return enumCap;
313		}
314
315		base_cap++;
316	}
317
318	return static_cast<const EnumCap*>(GetCaps(category)[0]);
319}
320
321
322template<typename Predicate>
323const BaseCap*
324PrinterCap::FindCap(CapID category, Predicate& predicate) const
325{
326	int count = CountCap(category);
327	if (count <= 0)
328		return NULL;
329
330	const BaseCap **base_cap = GetCaps(category);
331	while (count--) {
332		if (predicate(*base_cap)) {
333			return *base_cap;
334		}
335		base_cap++;
336	}
337	return NULL;
338
339}
340
341const EnumCap*
342PrinterCap::FindCap(CapID category, int id) const
343{
344	IDPredicate predicate(id);
345	return static_cast<const EnumCap*>(FindCap(category, predicate));
346}
347
348
349const BaseCap*
350PrinterCap::FindCap(CapID category, const char* label) const
351{
352	LabelPredicate predicate(label);
353	return FindCap(category, predicate);
354}
355
356
357const EnumCap*
358PrinterCap::FindCapWithKey(CapID category, const char* key) const
359{
360	KeyPredicate predicate(key);
361	return static_cast<const EnumCap*>(FindCap(category, predicate));
362}
363
364
365const BooleanCap*
366PrinterCap::FindBooleanCap(CapID category) const
367{
368	if (CountCap(category) != 1)
369		return NULL;
370	return dynamic_cast<const BooleanCap*>(GetCaps(category)[0]);
371}
372
373
374const IntRangeCap*
375PrinterCap::FindIntRangeCap(CapID category) const
376{
377	if (CountCap(category) != 1)
378		return NULL;
379	return dynamic_cast<const IntRangeCap*>(GetCaps(category)[0]);
380}
381
382
383const DoubleRangeCap*
384PrinterCap::FindDoubleRangeCap(CapID category) const
385{
386	if (CountCap(category) != 1)
387		return NULL;
388	return dynamic_cast<const DoubleRangeCap*>(GetCaps(category)[0]);
389}
390
391
392int
393PrinterCap::GetProtocolClass() const {
394	return fPrinterData->GetProtocolClass();
395}
396
397
398const PrinterData*
399PrinterCap::GetPrinterData() const
400{
401	return fPrinterData;
402}
403