xref: /AOO41X/main/writerperfect/source/filter/TableStyle.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /* TableStyle: Stores (and writes) table-based information that is
2  * needed at the head of an OO document.
3  *
4  * Copyright (C) 2002-2004 William Lachance (william.lachance@sympatico.ca)
5  * Copyright (C) 2004 Net Integration Technologies, Inc. (http://www.net-itech.com)
6  * Copyright (C) 2004 Fridrich Strba (fridrich.strba@bluewin.ch)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  * For further information visit http://libwpd.sourceforge.net
23  *
24  */
25 
26 /* "This product is not manufactured, approved, or supported by
27  * Corel Corporation or Corel Corporation Limited."
28  */
29 #include <math.h>
30 #include <string.h>
31 #include "FilterInternal.hxx"
32 #include "TableStyle.hxx"
33 #include "DocumentElement.hxx"
34 
35 #ifdef _MSC_VER
36 #include <minmax.h>
37 #endif
38 
TableCellStyle(const WPXPropertyList & xPropList,const char * psName)39 TableCellStyle::TableCellStyle(const WPXPropertyList &xPropList, const char *psName) :
40     Style(psName),
41         mPropList(xPropList)
42 {
43 }
44 
write(DocumentHandler * pHandler) const45 void TableCellStyle::write(DocumentHandler *pHandler) const
46 {
47     TagOpenElement styleOpen("style:style");
48     styleOpen.addAttribute("style:name", getName());
49     styleOpen.addAttribute("style:family", "table-cell");
50     styleOpen.write(pHandler);
51 
52         // WLACH_REFACTORING: Only temporary.. a much better solution is to
53         // generalize this sort of thing into the "Style" superclass
54         WPXPropertyList stylePropList;
55         WPXPropertyList::Iter i(mPropList);
56         for (i.rewind(); i.next();)
57         {
58                 if (strlen(i.key()) > 2 && strncmp(i.key(), "fo", 2) == 0)
59                         stylePropList.insert(i.key(), i()->clone());
60         }
61         stylePropList.insert("fo:padding", "0.0382inch");
62         pHandler->startElement("style:properties", stylePropList);
63     pHandler->endElement("style:properties");
64 
65     pHandler->endElement("style:style");
66 }
67 
TableRowStyle(const WPXPropertyList & propList,const char * psName)68 TableRowStyle::TableRowStyle(const WPXPropertyList &propList, const char *psName) :
69     Style(psName),
70         mPropList(propList)
71 {
72 }
73 
write(DocumentHandler * pHandler) const74 void TableRowStyle::write(DocumentHandler *pHandler) const
75 {
76     TagOpenElement styleOpen("style:style");
77     styleOpen.addAttribute("style:name", getName());
78     styleOpen.addAttribute("style:family", "table-row");
79     styleOpen.write(pHandler);
80 
81         TagOpenElement stylePropertiesOpen("style:properties");
82         if (mPropList["style:min-row-height"])
83                 stylePropertiesOpen.addAttribute("style:min-row-height", mPropList["style:min-row-height"]->getStr());
84         else if (mPropList["style:row-height"])
85                 stylePropertiesOpen.addAttribute("style:row-height", mPropList["style:row-height"]->getStr());
86         stylePropertiesOpen.write(pHandler);
87         pHandler->endElement("style:properties");
88 
89     pHandler->endElement("style:style");
90 }
91 
92 
TableStyle(const WPXPropertyList & xPropList,const WPXPropertyListVector & columns,const char * psName)93 TableStyle::TableStyle(const WPXPropertyList &xPropList, const WPXPropertyListVector &columns, const char *psName) :
94     Style(psName),
95         mPropList(xPropList),
96         mColumns(columns)
97 {
98 }
99 
~TableStyle()100 TableStyle::~TableStyle()
101 {
102     typedef std::vector<TableCellStyle *>::iterator TCSVIter;
103     typedef std::vector<TableRowStyle *>::iterator TRSVIter;
104     for (TCSVIter iterTableCellStyles = mTableCellStyles.begin() ; iterTableCellStyles != mTableCellStyles.end(); iterTableCellStyles++)
105         delete(*iterTableCellStyles);
106     for (TRSVIter iterTableRowStyles = mTableRowStyles.begin() ; iterTableRowStyles != mTableRowStyles.end(); iterTableRowStyles++)
107         delete(*iterTableRowStyles);
108 
109 }
110 
write(DocumentHandler * pHandler) const111 void TableStyle::write(DocumentHandler *pHandler) const
112 {
113     TagOpenElement styleOpen("style:style");
114     styleOpen.addAttribute("style:name", getName());
115     styleOpen.addAttribute("style:family", "table");
116     if (getMasterPageName())
117         styleOpen.addAttribute("style:master-page-name", getMasterPageName()->cstr());
118     styleOpen.write(pHandler);
119 
120     TagOpenElement stylePropertiesOpen("style:properties");
121         if (mPropList["table:align"])
122                 stylePropertiesOpen.addAttribute("table:align", mPropList["table:align"]->getStr());
123     if (mPropList["fo:margin-left"])
124         stylePropertiesOpen.addAttribute("fo:margin-left", mPropList["fo:margin-left"]->getStr());
125     if (mPropList["fo:margin-right"])
126         stylePropertiesOpen.addAttribute("fo:margin-right", mPropList["fo:margin-right"]->getStr());
127     if (mPropList["style:width"])
128         stylePropertiesOpen.addAttribute("style:width", mPropList["style:width"]->getStr());
129     if (mPropList["fo:break-before"])
130         stylePropertiesOpen.addAttribute("fo:break-before", mPropList["fo:break-before"]->getStr());
131     stylePropertiesOpen.write(pHandler);
132 
133     pHandler->endElement("style:properties");
134 
135     pHandler->endElement("style:style");
136 
137     int i=1;
138         WPXPropertyListVector::Iter j(mColumns);
139     for (j.rewind(); j.next();)
140     {
141         TagOpenElement styleNestedOpen("style:style");
142         WPXString sColumnName;
143         sColumnName.sprintf("%s.Column%i", getName().cstr(), i);
144         styleNestedOpen.addAttribute("style:name", sColumnName);
145         styleNestedOpen.addAttribute("style:family", "table-column");
146         styleNestedOpen.write(pHandler);
147 
148                 pHandler->startElement("style:properties", j());
149         pHandler->endElement("style:properties");
150 
151         pHandler->endElement("style:style");
152 
153         i++;
154     }
155 
156     typedef std::vector<TableRowStyle *>::const_iterator TRSVIter;
157     for (TRSVIter iterTableRow = mTableRowStyles.begin() ; iterTableRow != mTableRowStyles.end(); iterTableRow++)
158         (*iterTableRow)->write(pHandler);
159 
160     typedef std::vector<TableCellStyle *>::const_iterator TCSVIter;
161     for (TCSVIter iterTableCell = mTableCellStyles.begin() ; iterTableCell != mTableCellStyles.end(); iterTableCell++)
162         (*iterTableCell)->write(pHandler);
163 }
164