1/*
2
3Preview printer driver.
4
5Copyright (c) 2001 - 2008 Haiku.
6
7Authors:
8	Philippe Houdoin
9	Simon Gauvin
10	Michael Pfeiffer
11
12Permission is hereby granted, free of charge, to any person obtaining a copy of
13this software and associated documentation files (the "Software"), to deal in
14the Software without restriction, including without limitation the rights to
15use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
16of the Software, and to permit persons to whom the Software is furnished to do
17so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28THE SOFTWARE.
29
30*/
31
32#include "PrintUtils.h"
33
34
35#include <Message.h>
36#include <Window.h>
37
38
39BRect
40ScaleRect(const BRect& rect, float scale)
41{
42	BRect scaleRect(rect);
43
44	scaleRect.left *= scale;
45	scaleRect.right *= scale;
46	scaleRect.top *= scale;
47	scaleRect.bottom *= scale;
48
49	return scaleRect;
50}
51
52
53void
54SetBool(BMessage* msg, const char* name, bool value)
55{
56	if (msg->HasBool(name)) {
57		msg->ReplaceBool(name, value);
58	} else {
59		msg->AddBool(name, value);
60	}
61}
62
63
64void
65SetFloat(BMessage* msg, const char* name, float value)
66{
67	if (msg->HasFloat(name)) {
68		msg->ReplaceFloat(name, value);
69	} else {
70		msg->AddFloat(name, value);
71	}
72}
73
74
75void
76SetInt32(BMessage* msg, const char* name, int32 value)
77{
78	if (msg->HasInt32(name)) {
79		msg->ReplaceInt32(name, value);
80	} else {
81		msg->AddInt32(name, value);
82	}
83}
84
85
86void
87SetString(BMessage* msg, const char* name, const char* value)
88{
89	if (msg->HasString(name, 0)) {
90		msg->ReplaceString(name, value);
91	} else {
92		msg->AddString(name, value);
93	}
94}
95
96
97void
98SetRect(BMessage* msg, const char* name, const BRect& rect)
99{
100	if (msg->HasRect(name)) {
101		msg->ReplaceRect(name, rect);
102	} else {
103		msg->AddRect(name, rect);
104	}
105}
106
107
108void
109SetString(BMessage* msg, const char* name, const BString& value)
110{
111	SetString(msg, name, value.String());
112}
113
114
115static
116bool InList(const char* list[], const char* name)
117{
118	for (int i = 0; list[i] != NULL; ++i) {
119		if (strcmp(list[i], name) == 0)
120			return true;
121	}
122	return false;
123}
124
125
126void
127AddFields(BMessage* to, const BMessage* from, const char* excludeList[],
128	const char* includeList[], bool overwrite)
129{
130	if (to == from)
131		return;
132	char* name;
133	type_code type;
134	int32 count;
135	for (int32 i = 0; from->GetInfo(B_ANY_TYPE, i, &name, &type, &count)
136		== B_OK; ++i) {
137		if (excludeList && InList(excludeList, name))
138			continue;
139
140		if (includeList && !InList(includeList, name))
141			continue;
142
143		ssize_t size;
144		const void* data;
145		if (!overwrite && to->FindData(name, type, 0, &data, &size) == B_OK)
146			continue;
147
148		// replace existing data
149		to->RemoveName(name);
150
151		for (int32 j = 0; j < count; ++j) {
152			if (from->FindData(name, type, j, &data, &size) == B_OK) {
153				if (type == B_STRING_TYPE) {
154					to->AddString(name, (const char*)data);
155				} else if (type == B_MESSAGE_TYPE) {
156					BMessage m;
157					from->FindMessage(name, j, &m);
158					to->AddMessage(name, &m);
159				} else {
160					to->AddData(name, type, data, size);
161				}
162			}
163		}
164	}
165}
166