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