Source code: com/anotherbigidea/flash/movie/EditField.java
1 /****************************************************************
2 * Copyright (c) 2001, David N. Main, All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the
6 * following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or
18 * promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ****************************************************************/
34 package com.anotherbigidea.flash.movie;
35
36 import java.io.*;
37 import java.util.*;
38 import com.anotherbigidea.flash.interfaces.*;
39 import com.anotherbigidea.flash.writers.*;
40 import com.anotherbigidea.flash.readers.*;
41 import com.anotherbigidea.flash.structs.*;
42 import com.anotherbigidea.flash.SWFConstants;
43
44 /**
45 * An Edit Field Symbol.
46 *
47 * In order to limit the chars in the field use a separate font and only
48 * load those glyphs that are required.
49 */
50 public class EditField extends Symbol
51 {
52 protected AlphaColor textColor;
53 protected int alignment;
54 protected int charLimit;
55 protected double leftMargin;
56 protected double rightMargin;
57 protected double indentation;
58 protected double lineSpacing;
59
60 protected String fieldName;
61 protected String initialText;
62 protected Font font;
63 protected double fontSize;
64 protected double minX, minY, maxX, maxY;
65
66 protected boolean isSelectable = true;
67 protected boolean hasBorder = true;
68 protected boolean isHtml;
69 protected boolean usesSystemFont;
70 protected boolean hasWordWrap;
71 protected boolean isMultiline;
72 protected boolean isPassword;
73 protected boolean isEditable = true;
74
75 public boolean isSelectable() { return isSelectable; }
76 public boolean hasBorder() { return hasBorder; }
77 public boolean isHtml() { return isHtml; }
78 public boolean usesSystemFont() { return usesSystemFont; }
79 public boolean hasWordWrap() { return hasWordWrap; }
80 public boolean isMultiline() { return isMultiline; }
81 public boolean isPassword() { return isPassword; }
82 public boolean isEditable() { return isEditable; }
83
84 public void setProperties( boolean isSelectable, boolean hasBorder,
85 boolean isHtml, boolean usesSystemFont,
86 boolean hasWordWrap, boolean isMultiline,
87 boolean isPassword, boolean isEditable )
88 {
89 this.isSelectable = isSelectable;
90 this.hasBorder = hasBorder;
91 this.isHtml = isHtml;
92 this.usesSystemFont = usesSystemFont;
93 this.hasWordWrap = hasWordWrap;
94 this.isMultiline = isMultiline;
95 this.isPassword = isPassword;
96 this.isEditable = isEditable;
97 }
98
99 public AlphaColor getTextColor() { return textColor; }
100 public int getAlignment() { return alignment; }
101 public int getCharLimit() { return charLimit; }
102 public double getLeftMargin() { return leftMargin; }
103 public double getRightMargin() { return rightMargin; }
104 public double getIndentation() { return indentation; }
105 public double getLineSpacing() { return lineSpacing; }
106
107 public String getFieldName() { return fieldName; }
108 public String getInitialText() { return initialText; }
109 public Font getFont() { return font; }
110 public double getFontSize() { return fontSize; }
111 public double getMinX() { return minX; }
112 public double getMinY() { return minY; }
113 public double getMaxX() { return maxX; }
114 public double getMaxY() { return maxY; }
115
116 public void setTextColor ( AlphaColor color ) { this.textColor = color; }
117 public void setAlignment ( int alignment ) { this.alignment = alignment; }
118 public void setCharLimit ( int charLimit ) { this.charLimit = charLimit; }
119 public void setLeftMargin ( double leftMargin ) { this.leftMargin = leftMargin; }
120 public void setRightMargin( double rightMargin ) { this.rightMargin = rightMargin; }
121 public void setIndentation( double indentation ) { this.indentation = indentation; }
122 public void setLineSpacing( double lineSpacing ) { this.lineSpacing = lineSpacing; }
123
124 public void setFieldName ( String name ) { this.fieldName = name; }
125 public void setInitialText( String text ) { this.initialText = text; }
126 public void setFont ( Font font ) { this.font = font; }
127 public void setFontSize( double fontSize ) { this.fontSize = fontSize; }
128 public void setMinX ( double minX ) { this.minX = minX; }
129 public void setMinY ( double minY ) { this.minY = minY; }
130 public void setMaxX ( double maxX ) { this.maxX = maxX; }
131 public void setMaxY ( double maxY ) { this.maxY = maxY; }
132
133 /**
134 * Create an Edit Field with black text and default settings
135 *
136 * @param fieldName may be null
137 * @param intialText may be null
138 */
139 public EditField( String fieldName, String initialText,
140 Font font, double fontSize,
141 double minX, double minY, double maxX, double maxY )
142 {
143 this.fieldName = fieldName;
144 this.initialText = initialText;
145 this.font = font;
146 this.fontSize = fontSize;
147
148 this.minX = minX;
149 this.minY = minY;
150 this.maxX = maxX;
151 this.maxY = maxY;
152
153 this.textColor = new AlphaColor(0,0,0,255);
154 }
155
156
157 protected int defineSymbol( Movie movie,
158 SWFTagTypes timelineWriter,
159 SWFTagTypes definitionWriter )
160 throws IOException
161 {
162 int id = getNextId( movie );
163
164 //--Make sure that the font is defined.
165 int fontId = font.define( false, movie, definitionWriter );
166
167 Rect bounds = new Rect( (int)(minX*SWFConstants.TWIPS),
168 (int)(minY*SWFConstants.TWIPS),
169 (int)(maxX*SWFConstants.TWIPS),
170 (int)(maxY*SWFConstants.TWIPS));
171
172 int flags = 0;
173
174 if( ! isSelectable ) flags |= SWFConstants.TEXTFIELD_NO_SELECTION;
175 if( hasBorder ) flags |= SWFConstants.TEXTFIELD_DRAW_BORDER;
176 if( isHtml ) flags |= SWFConstants.TEXTFIELD_HTML;
177 if( ! usesSystemFont ) flags |= SWFConstants.TEXTFIELD_FONT_GLYPHS;
178 if( hasWordWrap ) flags |= SWFConstants.TEXTFIELD_WORD_WRAP;
179 if( isMultiline ) flags |= SWFConstants.TEXTFIELD_IS_MULTILINE;
180 if( isPassword ) flags |= SWFConstants.TEXTFIELD_IS_PASSWORD;
181 if( ! isEditable ) flags |= SWFConstants.TEXTFIELD_DISABLE_EDIT;
182
183 if( initialText != null && initialText.length() > 0 )
184 flags |= SWFConstants.TEXTFIELD_HAS_TEXT;
185
186 if( charLimit > 0 ) flags |= SWFConstants.TEXTFIELD_LIMIT_CHARS;
187
188 //--define the edit field
189 definitionWriter.tagDefineTextField(
190 id, fieldName, initialText, bounds, flags,
191 textColor, alignment, fontId,
192 (int)(fontSize * SWFConstants.TWIPS),
193 charLimit,
194 (int)(leftMargin * SWFConstants.TWIPS),
195 (int)(rightMargin * SWFConstants.TWIPS),
196 (int)(indentation * SWFConstants.TWIPS),
197 (int)(lineSpacing * SWFConstants.TWIPS) );
198
199 return id;
200 }
201
202 }