Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * The Contents of this file are made available subject to the terms of
5 : * either of the following licenses
6 : *
7 : * - GNU Lesser General Public License Version 2.1
8 : * - Sun Industry Standards Source License Version 1.1
9 : *
10 : * Sun Microsystems Inc., October, 2000
11 : *
12 : * GNU Lesser General Public License Version 2.1
13 : * =============================================
14 : * Copyright 2000 by Sun Microsystems, Inc.
15 : * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 : *
17 : * This library is free software; you can redistribute it and/or
18 : * modify it under the terms of the GNU Lesser General Public
19 : * License version 2.1, as published by the Free Software Foundation.
20 : *
21 : * This library is distributed in the hope that it will be useful,
22 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : * Lesser General Public License for more details.
25 : *
26 : * You should have received a copy of the GNU Lesser General Public
27 : * License along with this library; if not, write to the Free Software
28 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 : * MA 02111-1307 USA
30 : *
31 : *
32 : * Sun Industry Standards Source License Version 1.1
33 : * =================================================
34 : * The contents of this file are subject to the Sun Industry Standards
35 : * Source License Version 1.1 (the "License"); You may not use this file
36 : * except in compliance with the License. You may obtain a copy of the
37 : * License at http://www.openoffice.org/license.html.
38 : *
39 : * Software provided under this License is provided on an "AS IS" basis,
40 : * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 : * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 : * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 : * See the License for the specific provisions governing your rights and
44 : * obligations concerning the Software.
45 : *
46 : * The Initial Developer of the Original Code is: IBM Corporation
47 : *
48 : * Copyright: 2008 by IBM Corporation
49 : *
50 : * All Rights Reserved.
51 : *
52 : * Contributor(s): _______________________________________
53 : *
54 : *
55 : ************************************************************************/
56 : /*************************************************************************
57 : * @file
58 : * Table object.
59 : ************************************************************************/
60 : #include "xftable.hxx"
61 : #include "xfrow.hxx"
62 : #include "xfcolstyle.hxx"
63 : #include "xfstylemanager.hxx"
64 : #include <cassert>
65 :
66 1 : XFTable::XFTable()
67 : {
68 1 : m_strName = XFGlobal::GenTableName();
69 1 : m_bSubTable = sal_False;
70 1 : m_pOwnerCell = NULL;
71 1 : }
72 :
73 0 : XFTable::XFTable(const XFTable& other):XFContent(other)
74 : {
75 0 : m_strName = other.m_strName;
76 0 : m_bSubTable = other.m_bSubTable;
77 0 : m_pOwnerCell = NULL;
78 0 : for( int i=1; i<=other.m_aHeaderRows.GetCount(); i++ )
79 : {
80 0 : XFRow *pRow = (XFRow*)other.m_aHeaderRows.GetContent(i+1);
81 0 : if( pRow )
82 : {
83 0 : m_aHeaderRows.Add( new XFRow(*pRow) );
84 : }
85 : }
86 :
87 0 : m_aColumns = other.m_aColumns;
88 0 : m_strDefCellStyle = other.m_strDefCellStyle;
89 0 : m_strDefRowStyle = other.m_strDefRowStyle;
90 0 : m_strDefColStyle = other.m_strDefColStyle;
91 0 : }
92 :
93 0 : XFTable& XFTable::operator =(const XFTable& other)
94 : {
95 0 : m_strName = other.m_strName;
96 0 : m_bSubTable = other.m_bSubTable;
97 0 : m_pOwnerCell = NULL;
98 0 : for( int i=1; i<=other.m_aHeaderRows.GetCount(); i++ )
99 : {
100 0 : XFRow *pRow = (XFRow*)other.m_aHeaderRows.GetContent(i);
101 0 : if( pRow )
102 : {
103 0 : m_aHeaderRows.Add( new XFRow(*pRow) );
104 : }
105 : }
106 :
107 0 : m_aColumns = other.m_aColumns;
108 0 : m_strDefCellStyle = other.m_strDefCellStyle;
109 0 : m_strDefRowStyle = other.m_strDefRowStyle;
110 0 : m_strDefColStyle = other.m_strDefColStyle;
111 0 : return *this;
112 : }
113 :
114 3 : XFTable::~XFTable()
115 : {
116 1 : std::map<sal_uInt16, XFRow*>::iterator it;
117 6 : for( it=m_aRows.begin(); it!=m_aRows.end(); ++it )
118 : {
119 5 : XFRow *pRow = (*it).second;
120 5 : delete pRow;
121 : }
122 1 : m_aRows.clear();
123 1 : m_aColumns.clear();
124 2 : }
125 :
126 4 : void XFTable::SetColumnStyle(sal_Int32 col, const OUString& style)
127 : {
128 4 : m_aColumns[col] = style;
129 4 : }
130 :
131 5 : void XFTable::AddRow(XFRow *pRow)
132 : {
133 : assert(pRow);
134 5 : if( !pRow )
135 5 : return;
136 :
137 5 : int row = pRow->GetRow();
138 :
139 5 : if( row<1 )
140 5 : pRow->SetRow(m_aRows.size()+1);
141 :
142 5 : row = pRow->GetRow();
143 5 : if( m_aRows.find(row) != m_aRows.end() )
144 0 : delete m_aRows[row];
145 :
146 5 : pRow->SetOwnerTable(this);
147 5 : m_aRows[row] = pRow;
148 : }
149 :
150 0 : void XFTable::AddHeaderRow(XFRow *pRow)
151 : {
152 0 : if( !pRow )
153 0 : return;
154 0 : m_aHeaderRows.Add(pRow);
155 : }
156 :
157 0 : OUString XFTable::GetTableName()
158 : {
159 0 : if( m_bSubTable )
160 : {
161 0 : return m_pOwnerCell->GetCellName();
162 : }
163 : else
164 0 : return m_strName;
165 : }
166 :
167 0 : sal_uInt16 XFTable::GetRowCount()
168 : {
169 0 : sal_uInt16 rowMax = 0;
170 0 : std::map<sal_uInt16, XFRow*>::iterator it;
171 0 : for( it=m_aRows.begin(); it!=m_aRows.end(); ++it )
172 : {
173 0 : if (it->first > rowMax)
174 0 : rowMax = it->first;
175 : }
176 :
177 0 : return rowMax;
178 : }
179 :
180 0 : XFRow* XFTable::GetRow(sal_Int32 row)
181 : {
182 0 : return m_aRows[row];
183 : }
184 :
185 0 : sal_Int32 XFTable::GetColumnCount()
186 : {
187 0 : int colMax = -1;
188 0 : std::map<sal_Int32,OUString>::iterator it;
189 0 : for( it=m_aColumns.begin(); it!=m_aColumns.end(); ++it )
190 : {
191 0 : if( it->first>colMax )
192 0 : colMax = it->first;
193 : }
194 0 : return colMax;
195 : }
196 :
197 0 : enumXFContent XFTable::GetContentType()
198 : {
199 0 : return enumXFContentTable;
200 : }
201 :
202 1 : void XFTable::ToXml(IXFStream *pStrm)
203 : {
204 1 : IXFAttrList *pAttrList = pStrm->GetAttrList();
205 :
206 1 : pAttrList->Clear();
207 : //sub table shouldn't use table name.
208 1 : if( !m_bSubTable )
209 1 : pAttrList->AddAttribute( "table:name", m_strName);
210 :
211 1 : if( !GetStyleName().isEmpty() )
212 1 : pAttrList->AddAttribute( "table:style-name", GetStyleName() );
213 :
214 1 : if( m_bSubTable )
215 0 : pStrm->StartElement( "table:sub-table" );
216 : else
217 1 : pStrm->StartElement( "table:table" );
218 :
219 : //output columns:
220 : {
221 1 : int lastCol = 0;
222 1 : std::map<sal_Int32,OUString>::iterator it;
223 5 : for( it=m_aColumns.begin(); it!=m_aColumns.end(); ++it )
224 : {
225 4 : sal_Int32 col = (*it).first;
226 4 : OUString style = m_aColumns[col];
227 :
228 : //default col repeated:
229 4 : if( col >lastCol+1 )
230 : {
231 0 : if( col > lastCol + 2 )
232 : {
233 0 : if( !m_strDefColStyle.isEmpty() )
234 : {
235 0 : pAttrList->AddAttribute( "table:style-name", m_strDefColStyle );
236 : }
237 0 : pAttrList->AddAttribute( "table:number-columns-repeated", OUString::number(col-lastCol-1) );
238 : }
239 0 : pStrm->StartElement( "table:table-column" );
240 0 : pStrm->EndElement( "table:table-column" );
241 : }
242 :
243 4 : if( !style.isEmpty() )
244 : {
245 4 : pAttrList->AddAttribute( "table:style-name", style );
246 : }
247 4 : pStrm->StartElement( "table:table-column" );
248 4 : pStrm->EndElement( "table:table-column" );
249 4 : lastCol = col;
250 4 : }
251 : }
252 :
253 1 : if( m_aHeaderRows.GetCount()>0 )
254 : {
255 0 : pStrm->StartElement( "table:table-header-rows" );
256 0 : m_aHeaderRows.ToXml(pStrm);
257 0 : pStrm->EndElement( "table:table-header-rows" );
258 : }
259 : //output rows:
260 : {
261 1 : int lastRow = 0;
262 :
263 1 : std::map<sal_uInt16, XFRow* >::iterator it = m_aRows.begin();
264 6 : for( ; it!=m_aRows.end(); ++it )
265 : {
266 5 : int row = (*it).first;
267 5 : XFRow *pRow = (*it).second;
268 :
269 : //null row repeated:
270 5 : if( row>lastRow+1 )
271 : {
272 0 : XFRow *pNullRow = new XFRow();
273 0 : pNullRow->SetStyleName(m_strDefRowStyle);
274 0 : if( row>lastRow+2)
275 0 : pNullRow->SetRepeated(row-lastRow-1);
276 0 : XFCell *pCell = new XFCell();
277 0 : pCell->SetStyleName(m_strDefCellStyle);
278 0 : pNullRow->AddCell(pCell);
279 0 : pNullRow->ToXml(pStrm);
280 : }
281 5 : pRow->ToXml(pStrm);
282 5 : lastRow = row;
283 : }
284 : }
285 :
286 1 : if( m_bSubTable )
287 0 : pStrm->EndElement( "table:sub-table" );
288 : else
289 1 : pStrm->EndElement( "table:table" );
290 1 : }
291 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|