1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.hssf.record;
57   
58   import org.apache.poi.util.BitField;
59   import org.apache.poi.util.LittleEndian;
60   
61   /**
62    * Title:        Window1 Record<P>
63    * Description:  Stores the attributes of the workbook window.  This is basically
64    *               so the gui knows how big to make the window holding the spreadsheet
65    *               document.<P>
66    * REFERENCE:  PG 421 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
67    * @author Andrew C. Oliver (acoliver at apache dot org)
68    * @version 2.0-pre
69    */
70   
71   public class WindowOneRecord
72       extends Record
73   {
74       public final static short     sid = 0x3d;
75   
76       // our variable names stolen from old TV sets.
77       private short                 field_1_h_hold;                  // horizontal position
78       private short                 field_2_v_hold;                  // vertical position
79       private short                 field_3_width;
80       private short                 field_4_height;
81       private short                 field_5_options;
82       static final private BitField hidden   =
83           new BitField(0x01);                                        // is this window is hidden
84       static final private BitField iconic   =
85           new BitField(0x02);                                        // is this window is an icon
86       static final private BitField reserved = new BitField(0x04);   // reserved
87       static final private BitField hscroll  =
88           new BitField(0x08);                                        // display horizontal scrollbar
89       static final private BitField vscroll  =
90           new BitField(0x10);                                        // display vertical scrollbar
91       static final private BitField tabs     =
92           new BitField(0x20);                                        // display tabs at the bottom
93   
94       // all the rest are "reserved"
95       private short                 field_6_selected_tab;
96       private short                 field_7_displayed_tab;
97       private short                 field_8_num_selected_tabs;
98       private short                 field_9_tab_width_ratio;
99   
100      public WindowOneRecord()
101      {
102      }
103  
104      /**
105       * Constructs a WindowOne record and sets its fields appropriately.
106       *
107       * @param id     id must be 0x3d or an exception will be throw upon validation
108       * @param size  the size of the data area of the record
109       * @param data  data of the record (should not contain sid/len)
110       */
111  
112      public WindowOneRecord(short id, short size, byte [] data)
113      {
114          super(id, size, data);
115      }
116  
117      /**
118       * Constructs a WindowOne record and sets its fields appropriately.
119       *
120       * @param id     id must be 0x3d or an exception will be throw upon validation
121       * @param size  the size of the data area of the record
122       * @param data  data of the record (should not contain sid/len)
123       */
124  
125      public WindowOneRecord(short id, short size, byte [] data, int offset)
126      {
127          super(id, size, data, offset);
128      }
129  
130      protected void validateSid(short id)
131      {
132          if (id != sid)
133          {
134              throw new RecordFormatException("NOT A WINDOW1 RECORD");
135          }
136      }
137  
138      protected void fillFields(byte [] data, short size, int offset)
139      {
140          field_1_h_hold            = LittleEndian.getShort(data, 0 + offset);
141          field_2_v_hold            = LittleEndian.getShort(data, 2 + offset);
142          field_3_width             = LittleEndian.getShort(data, 4 + offset);
143          field_4_height            = LittleEndian.getShort(data, 6 + offset);
144          field_5_options           = LittleEndian.getShort(data, 8 + offset);
145          field_6_selected_tab      = LittleEndian.getShort(data, 10 + offset);
146          field_7_displayed_tab     = LittleEndian.getShort(data, 12 + offset);
147          field_8_num_selected_tabs = LittleEndian.getShort(data, 14 + offset);
148          field_9_tab_width_ratio   = LittleEndian.getShort(data, 16 + offset);
149      }
150  
151      /**
152       * set the horizontal position of the window (in 1/20ths of a point)
153       * @param h - horizontal location
154       */
155  
156      public void setHorizontalHold(short h)
157      {
158          field_1_h_hold = h;
159      }
160  
161      /**
162       * set the vertical position of the window (in 1/20ths of a point)
163       * @param v - vertical location
164       */
165  
166      public void setVerticalHold(short v)
167      {
168          field_2_v_hold = v;
169      }
170  
171      /**
172       * set the width of the window
173       * @param w  width
174       */
175  
176      public void setWidth(short w)
177      {
178          field_3_width = w;
179      }
180  
181      /**
182       * set teh height of the window
183       * @param h  height
184       */
185  
186      public void setHeight(short h)
187      {
188          field_4_height = h;
189      }
190  
191      /**
192       * set the options bitmask (see bit setters)
193       *
194       * @param o - the bitmask
195       */
196  
197      public void setOptions(short o)
198      {
199          field_5_options = o;
200      }
201  
202      // bitfields for options
203  
204      /**
205       * set whether the window is hidden or not
206       * @param ishidden or not
207       */
208  
209      public void setHidden(boolean ishidden)
210      {
211          field_5_options = hidden.setShortBoolean(field_5_options, ishidden);
212      }
213  
214      /**
215       * set whether the window has been iconized or not
216       * @param isiconic  iconize  or not
217       */
218  
219      public void setIconic(boolean isiconic)
220      {
221          field_5_options = iconic.setShortBoolean(field_5_options, isiconic);
222      }
223  
224      /**
225       * set whether to display the horizontal scrollbar or not
226       * @param scroll display or not
227       */
228  
229      public void setDisplayHorizonalScrollbar(boolean scroll)
230      {
231          field_5_options = hscroll.setShortBoolean(field_5_options, scroll);
232      }
233  
234      /**
235       * set whether to display the vertical scrollbar or not
236       * @param scroll  display or not
237       */
238  
239      public void setDisplayVerticalScrollbar(boolean scroll)
240      {
241          field_5_options = vscroll.setShortBoolean(field_5_options, scroll);
242      }
243  
244      /**
245       * set whether to display the tabs or not
246       * @param disptabs  display or not
247       */
248  
249      public void setDisplayTabs(boolean disptabs)
250      {
251          field_5_options = tabs.setShortBoolean(field_5_options, disptabs);
252      }
253  
254      // end bitfields
255  
256      /**
257       * set the selected tab number
258       * @param s  tab number
259       */
260  
261      public void setSelectedTab(short s)
262      {
263          field_6_selected_tab = s;
264      }
265  
266      /**
267       * set the displayed tab number
268       * @param t  tab number
269       */
270  
271      public void setDisplayedTab(short t)
272      {
273          field_7_displayed_tab = t;
274      }
275  
276      /**
277       * set the number of selected tabs
278       * @param n  number of tabs
279       */
280  
281      public void setNumSelectedTabs(short n)
282      {
283          field_8_num_selected_tabs = n;
284      }
285  
286      /**
287       * ratio of the width of the tabs to the horizontal scrollbar
288       * @param r  ratio
289       */
290  
291      public void setTabWidthRatio(short r)
292      {
293          field_9_tab_width_ratio = r;
294      }
295  
296      /**
297       * get the horizontal position of the window (in 1/20ths of a point)
298       * @return h - horizontal location
299       */
300  
301      public short getHorizontalHold()
302      {
303          return field_1_h_hold;
304      }
305  
306      /**
307       * get the vertical position of the window (in 1/20ths of a point)
308       * @return v - vertical location
309       */
310  
311      public short getVerticalHold()
312      {
313          return field_2_v_hold;
314      }
315  
316      /**
317       * get the width of the window
318       * @return width
319       */
320  
321      public short getWidth()
322      {
323          return field_3_width;
324      }
325  
326      /**
327       * get the height of the window
328       * @return height
329       */
330  
331      public short getHeight()
332      {
333          return field_4_height;
334      }
335  
336      /**
337       * get the options bitmask (see bit setters)
338       *
339       * @return o - the bitmask
340       */
341  
342      public short getOptions()
343      {
344          return field_5_options;
345      }
346  
347      // bitfields for options
348  
349      /**
350       * get whether the window is hidden or not
351       * @return ishidden or not
352       */
353  
354      public boolean getHidden()
355      {
356          return hidden.isSet(field_5_options);
357      }
358  
359      /**
360       * get whether the window has been iconized or not
361       * @return iconize  or not
362       */
363  
364      public boolean getIconic()
365      {
366          return iconic.isSet(field_5_options);
367      }
368  
369      /**
370       * get whether to display the horizontal scrollbar or not
371       * @return display or not
372       */
373  
374      public boolean getDisplayHorizontalScrollbar()
375      {
376          return hscroll.isSet(field_5_options);
377      }
378  
379      /**
380       * get whether to display the vertical scrollbar or not
381       * @return display or not
382       */
383  
384      public boolean getDisplayVerticalScrollbar()
385      {
386          return vscroll.isSet(field_5_options);
387      }
388  
389      /**
390       * get whether to display the tabs or not
391       * @return display or not
392       */
393  
394      public boolean getDisplayTabs()
395      {
396          return tabs.isSet(field_5_options);
397      }
398  
399      // end options bitfields
400  
401      /**
402       * get the selected tab number
403       * @return Tab number
404       */
405  
406      public short getSelectedTab()
407      {
408          return field_6_selected_tab;
409      }
410  
411      /**
412       * get the displayed tab number
413       * @return Tab number
414       */
415  
416      public short getDisplayedTab()
417      {
418          return field_7_displayed_tab;
419      }
420  
421      /**
422       * get the number of selected tabs
423       * @return number of tabs
424       */
425  
426      public short getNumSelectedTabs()
427      {
428          return field_8_num_selected_tabs;
429      }
430  
431      /**
432       * ratio of the width of the tabs to the horizontal scrollbar
433       * @return ratio
434       */
435  
436      public short getTabWidthRatio()
437      {
438          return field_9_tab_width_ratio;
439      }
440  
441      public String toString()
442      {
443          StringBuffer buffer = new StringBuffer();
444  
445          buffer.append("[WINDOW1]\n");
446          buffer.append("    .h_hold          = ")
447              .append(Integer.toHexString(getHorizontalHold())).append("\n");
448          buffer.append("    .v_hold          = ")
449              .append(Integer.toHexString(getVerticalHold())).append("\n");
450          buffer.append("    .width           = ")
451              .append(Integer.toHexString(getWidth())).append("\n");
452          buffer.append("    .height          = ")
453              .append(Integer.toHexString(getHeight())).append("\n");
454          buffer.append("    .options         = ")
455              .append(Integer.toHexString(getOptions())).append("\n");
456          buffer.append("        .hidden      = ").append(getHidden())
457              .append("\n");
458          buffer.append("        .iconic      = ").append(getIconic())
459              .append("\n");
460          buffer.append("        .hscroll     = ")
461              .append(getDisplayHorizontalScrollbar()).append("\n");
462          buffer.append("        .vscroll     = ")
463              .append(getDisplayVerticalScrollbar()).append("\n");
464          buffer.append("        .tabs        = ").append(getDisplayTabs())
465              .append("\n");
466          buffer.append("    .selectedtab     = ")
467              .append(Integer.toHexString(getSelectedTab())).append("\n");
468          buffer.append("    .displayedtab    = ")
469              .append(Integer.toHexString(getDisplayedTab())).append("\n");
470          buffer.append("    .numselectedtabs = ")
471              .append(Integer.toHexString(getNumSelectedTabs())).append("\n");
472          buffer.append("    .tabwidthratio   = ")
473              .append(Integer.toHexString(getTabWidthRatio())).append("\n");
474          buffer.append("[/WINDOW1]\n");
475          return buffer.toString();
476      }
477  
478      public int serialize(int offset, byte [] data)
479      {
480          LittleEndian.putShort(data, 0 + offset, sid);
481          LittleEndian.putShort(data, 2 + offset,
482                                (( short ) 0x12));   // 18 bytes (22 total)
483          LittleEndian.putShort(data, 4 + offset, getHorizontalHold());
484          LittleEndian.putShort(data, 6 + offset, getVerticalHold());
485          LittleEndian.putShort(data, 8 + offset, getWidth());
486          LittleEndian.putShort(data, 10 + offset, getHeight());
487          LittleEndian.putShort(data, 12 + offset, getOptions());
488          LittleEndian.putShort(data, 14 + offset, getSelectedTab());
489          LittleEndian.putShort(data, 16 + offset, getDisplayedTab());
490          LittleEndian.putShort(data, 18 + offset, getNumSelectedTabs());
491          LittleEndian.putShort(data, 20 + offset, getTabWidthRatio());
492          return getRecordSize();
493      }
494  
495      public int getRecordSize()
496      {
497          return 22;
498      }
499  
500      public short getSid()
501      {
502          return this.sid;
503      }
504  }
505