Tizen 2.1 base
[platform/upstream/hplip.git] / prnt / hpijs / pmselect.cpp
1 /*****************************************************************************\
2   pmselect.cpp : Implimentation for the ModeSet class
3
4   Copyright (c) 2001 - 2002, Hewlett-Packard Co.
5   All rights reserved.
6
7   Redistribution and use in source and binary forms, with or without
8   modification, are permitted provided that the following conditions
9   are met:
10   1. Redistributions of source code must retain the above copyright
11      notice, this list of conditions and the following disclaimer.
12   2. Redistributions in binary form must reproduce the above copyright
13      notice, this list of conditions and the following disclaimer in the
14      documentation and/or other materials provided with the distribution.
15   3. Neither the name of Hewlett-Packard nor the names of its
16      contributors may be used to endorse or promote products derived
17      from this software without specific prior written permission.
18
19   THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
22   NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24   TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25   OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 \*****************************************************************************/
30
31
32 #include "header.h"
33 #include "pmselect.h"
34
35 APDK_BEGIN_NAMESPACE
36
37 ModeSet::ModeSet
38 (
39     PrintMode* pPM
40 )
41 {
42     ASSERT(pPM);
43     m_ListHead = new MSNode;
44
45     if (m_ListHead != NULL)
46     {
47         m_ListHead->m_pNext = NULL;
48         m_ListHead->m_pPrintMode = pPM;
49     }
50     Reset();                //   m_Current = m_ListHead;
51 } //ModeSet
52
53
54 ModeSet::~ModeSet()
55 {
56     if (m_ListHead != NULL)
57     {
58         Reset();
59         while (m_Current != NULL)
60         {
61             MSNode* pDeleteable = m_Current;
62             Next();             //m_Current = m_Current->m_pNext;
63             delete pDeleteable;
64         }
65         m_ListHead = NULL;
66     }
67 } //~ModeSet
68
69 // note that copy constructor, Head() and all "Subset" methods make NEW COPIES that need deleting
70
71 // copy constructor
72 ModeSet::ModeSet
73 (
74     ModeSet* pSource
75 ) :
76     m_ListHead(NULL),
77     m_Current(NULL)
78 {
79     ASSERT(pSource);
80
81     MSNode* t_SrcNode = pSource->m_ListHead;
82
83     while (t_SrcNode)
84     {
85         Append(t_SrcNode->m_pPrintMode);
86         t_SrcNode = t_SrcNode->m_pNext;
87     }
88 } //ModeSet
89
90
91 BOOL ModeSet::Append(PrintMode* pPrM)   // return TRUE for memerr
92 {
93     ASSERT(pPrM);
94     if (m_ListHead == NULL)
95     {
96         m_ListHead = new MSNode;
97         if (m_ListHead == NULL)
98         {
99             return TRUE;            // memory error
100         }
101         m_ListHead->m_pPrintMode = pPrM;
102     }
103     else
104     {
105         MSNode* t_walk;
106         if (m_Current != NULL)      // try to be effiecient and not walk the whole list
107         {
108             t_walk = m_Current;
109         }
110         else
111         {
112             t_walk = m_ListHead;        // we know m_ListHead != NULL from above
113         }
114
115         while(t_walk->m_pNext != NULL)
116         {
117             t_walk = t_walk->m_pNext;
118         }
119
120         t_walk->m_pNext = new MSNode;
121         if (t_walk->m_pNext == NULL)
122         {
123             return TRUE;                // memory error
124         }
125
126         // update current pointer (possible to shorten next append
127         // i.e. current will point to the last item in the list after an append
128         m_Current = t_walk->m_pNext;
129         m_Current->m_pPrintMode = pPrM;       // finally!!
130     }
131
132     return FALSE;       // no error
133 } //Append
134
135
136 unsigned int ModeSet::Size() const
137 {
138     unsigned int uCount = 0;
139
140     MSNode* t_walk = m_ListHead;
141     while (t_walk != NULL)
142     {
143         uCount++;
144         t_walk = t_walk->m_pNext;
145     }
146
147     return uCount;
148 } //Size
149
150 // strange name...?  Create a new one entry list with the first entry of this list
151 ModeSet* ModeSet::Head()
152 {
153     ASSERT(m_ListHead);
154     ModeSet* pNew = new ModeSet(HeadPrintMode());
155     return pNew;
156 } //Head
157
158
159 ModeSet* ModeSet::FontCapableSubset()
160 {
161     ModeSet* resMS = new ModeSet;
162     if (resMS == NULL)
163     {
164         return NULL;
165     }
166
167     Reset();
168     while(m_Current)
169     {
170         // if we have set up the list properly we never have to worry about m_pPrintMode
171         // being NULL.  If it is NULL then we shouldn't even have a node.
172         ASSERT(CurrPrintMode());
173
174         if(CurrPrintMode()->bFontCapable)
175         {
176             if(resMS->Append(CurrPrintMode()))
177             {
178                 delete resMS;               // there was a memory error appending
179                 return NULL;
180             }
181         }
182         Next();             //m_Current = m_Current->m_pNext;
183     }
184     return resMS;
185 } //FontCapableSubset
186
187
188 ModeSet* ModeSet::PenCompatibleSubset(PEN_TYPE pens)
189 {
190     ModeSet* resMS = new ModeSet;
191     if (resMS == NULL)
192     {
193         return NULL;
194     }
195
196     Reset();
197     while(m_Current)
198     {
199         ASSERT(CurrPrintMode());            // see comments above
200
201         if(CurrPrintMode()->Compatible(pens))
202         {
203             if(resMS->Append(CurrPrintMode()))
204             {
205                 delete resMS;                       // memory error
206                 return NULL;
207             }
208         }
209         Next();
210     }
211     return resMS;
212 } //PenCompatibleSubset
213
214
215 ModeSet* ModeSet::ColorCompatibleSubset(COLORMODE color)
216 {
217     ModeSet* resMS = new ModeSet;
218     if (resMS == NULL)
219     {
220         return NULL;
221     }
222
223     Reset();
224     while(m_Current)
225     {
226         ASSERT(CurrPrintMode());            // see comments above
227
228         if (CurrPrintMode()->ColorCompatible(color))
229         {
230             if(resMS->Append(CurrPrintMode()))
231             {
232                 delete resMS;                       // memory error
233                 return NULL;
234             }
235         }
236         Next();
237     }
238     return resMS;
239 } //ColorCompatibleSubset
240
241
242 ModeSet* ModeSet::QualitySubset(QUALITY_MODE eQuality)
243 {
244     ModeSet* resMS = new ModeSet;
245     if (resMS == NULL)
246     {
247         return NULL;
248     }
249
250     Reset();
251     while(m_Current)
252     {
253         ASSERT(CurrPrintMode());            // see comments above
254
255         if (CurrPrintMode()->QualityCompatible(eQuality))
256         {
257             if(resMS->Append(CurrPrintMode()))
258             {
259                 delete resMS;                       // memory error
260                 return NULL;
261             }
262         }
263         Next();
264     }
265     return resMS;
266 } //QualitySubset
267
268
269 ModeSet* ModeSet::MediaSubset(MEDIATYPE eMedia)
270 {
271     ModeSet* resMS = new ModeSet;
272     if (resMS == NULL)
273     {
274         return NULL;
275     }
276
277     Reset();
278     while(m_Current)
279     {
280         ASSERT(CurrPrintMode());            // see comments above
281
282         if (CurrPrintMode()->MediaCompatible(eMedia))
283         {
284             if(resMS->Append(CurrPrintMode()))
285             {
286                 delete resMS;                       // memory error
287                 return NULL;
288             }
289         }
290         Next();
291     }
292     return resMS;
293 } //MediaSubset
294
295 APDK_END_NAMESPACE