xref: /haiku/src/libs/print/libprint/PrintUtils.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*
2 
3 Preview printer driver.
4 
5 Copyright (c) 2001 - 2008 Haiku.
6 
7 Authors:
8 	Philippe Houdoin
9 	Simon Gauvin
10 	Michael Pfeiffer
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy of
13 this software and associated documentation files (the "Software"), to deal in
14 the Software without restriction, including without limitation the rights to
15 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is furnished to do
17 so, subject to the following conditions:
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 THE SOFTWARE.
29 
30 */
31 
32 #include "PrintUtils.h"
33 
34 
35 #include <Message.h>
36 #include <Window.h>
37 
38 
39 BRect
40 ScaleRect(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 
53 void
54 SetBool(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 
64 void
65 SetFloat(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 
75 void
76 SetInt32(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 
86 void
87 SetString(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 
97 void
98 SetRect(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 
108 void
109 SetString(BMessage* msg, const char* name, const BString& value)
110 {
111 	SetString(msg, name, value.String());
112 }
113 
114 
115 static
116 bool 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 
126 void
127 AddFields(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