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