comparison dwt/graphics/TextLayout.d @ 36:db5a898b2119

Fixed a lot of compile errors
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Tue, 07 Oct 2008 12:56:18 +0200
parents e831403a80a9
children 642f460a0908
comparison
equal deleted inserted replaced
35:7d135fe0caf2 36:db5a898b2119
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 *
11 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com>
10 *******************************************************************************/ 13 *******************************************************************************/
11 module dwt.graphics.TextLayout; 14 module dwt.graphics.TextLayout;
12
13 import dwt.dwthelper.utils;
14 15
15 import dwt.DWT; 16 import dwt.DWT;
16 import dwt.DWTException; 17 import dwt.DWTException;
17 import dwt.internal.cocoa.NSColor; 18 import dwt.internal.cocoa.NSColor;
18 import dwt.internal.cocoa.NSFont; 19 import dwt.internal.cocoa.NSFont;
26 import dwt.internal.cocoa.NSString; 27 import dwt.internal.cocoa.NSString;
27 import dwt.internal.cocoa.NSTextContainer; 28 import dwt.internal.cocoa.NSTextContainer;
28 import dwt.internal.cocoa.NSTextStorage; 29 import dwt.internal.cocoa.NSTextStorage;
29 import dwt.internal.cocoa.OS; 30 import dwt.internal.cocoa.OS;
30 31
32 import dwt.dwthelper.utils;
33 import dwt.graphics.Color;
34 import dwt.graphics.Device;
35 import dwt.graphics.Font;
36 import dwt.graphics.FontMetrics;
37 import dwt.graphics.GC;
38 import dwt.graphics.GCData;
39 import dwt.graphics.GlyphMetrics;
40 import dwt.graphics.Point;
41 import dwt.graphics.Rectangle;
42 import dwt.graphics.Region;
43 import dwt.graphics.Resource;
44 import dwt.graphics.TextStyle;
45
31 /** 46 /**
32 * <code>TextLayout</code> is a graphic object that represents 47 * <code>TextLayout</code> is a graphic object that represents
33 * styled text. 48 * styled text.
34 * <p> 49 * <p>
35 * Instances of this class provide support for drawing, cursor 50 * Instances of this class provide support for drawing, cursor
42 * </p> 57 * </p>
43 * 58 *
44 * @since 3.0 59 * @since 3.0
45 */ 60 */
46 public final class TextLayout : Resource { 61 public final class TextLayout : Resource {
62
63 alias Resource.init_ init_;
47 64
48 NSTextStorage textStorage; 65 NSTextStorage textStorage;
49 NSLayoutManager layoutManager; 66 NSLayoutManager layoutManager;
50 NSTextContainer textContainer; 67 NSTextContainer textContainer;
51 Font font; 68 Font font;
99 orientation = DWT.LEFT_TO_RIGHT; 116 orientation = DWT.LEFT_TO_RIGHT;
100 text = ""; 117 text = "";
101 styles = new StyleItem[2]; 118 styles = new StyleItem[2];
102 styles[0] = new StyleItem(); 119 styles[0] = new StyleItem();
103 styles[1] = new StyleItem(); 120 styles[1] = new StyleItem();
104 init(); 121 init_();
105 } 122 }
106 123
107 void checkLayout() { 124 void checkLayout() {
108 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 125 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
109 } 126 }
110 127
111 void computeRuns() { 128 void computeRuns() {
112 if (textStorage !is null) return; 129 if (textStorage !is null) return;
113 NSString str = NSString.stringWith(text); 130 NSString str = NSString.stringWith(text);
114 textStorage = (cast(NSTextStorage)new NSTextStorage().alloc()); 131 textStorage = (cast(NSTextStorage)(new NSTextStorage()).alloc());
115 textStorage.initWithString_(str); 132 textStorage.initWithString_(str);
116 layoutManager = cast(NSLayoutManager)new NSLayoutManager().alloc().init(); 133 layoutManager = cast(NSLayoutManager)(new NSLayoutManager()).alloc().init();
117 textContainer = cast(NSTextContainer)new NSTextContainer().alloc(); 134 textContainer = cast(NSTextContainer)(new NSTextContainer()).alloc();
118 NSSize size = new NSSize(); 135 NSSize size = new NSSize();
119 size.width = wrapWidth !is -1 ? wrapWidth : Float.MAX_VALUE; 136 size.width = wrapWidth !is -1 ? wrapWidth : Float.MAX_VALUE;
120 size.height = Float.MAX_VALUE; 137 size.height = Float.MAX_VALUE;
121 textContainer.initWithContainerSize(size); 138 textContainer.initWithContainerSize(size);
122 textStorage.addLayoutManager(layoutManager); 139 textStorage.addLayoutManager(layoutManager);
126 Font defaultFont = font !is null ? font : device.systemFont; 143 Font defaultFont = font !is null ? font : device.systemFont;
127 NSRange range = new NSRange(); 144 NSRange range = new NSRange();
128 range.length = str.length(); 145 range.length = str.length();
129 textStorage.addAttribute(OS.NSFontAttributeName(), defaultFont.handle, range); 146 textStorage.addAttribute(OS.NSFontAttributeName(), defaultFont.handle, range);
130 147
131 NSMutableParagraphStyle paragraph = cast(NSMutableParagraphStyle)new NSMutableParagraphStyle().alloc().init(); 148 NSMutableParagraphStyle paragraph = cast(NSMutableParagraphStyle)(new NSMutableParagraphStyle()).alloc().init();
132 int align = OS.NSLeftTextAlignment; 149 int align_ = OS.NSLeftTextAlignment;
133 if (justify) { 150 if (justify) {
134 align = OS.NSJustifiedTextAlignment; 151 align_ = OS.NSJustifiedTextAlignment;
135 } else { 152 } else {
136 switch (alignment) { 153 switch (alignment) {
137 case DWT.CENTER: 154 case DWT.CENTER:
138 align = OS.NSCenterTextAlignment; 155 align_ = OS.NSCenterTextAlignment;
139 break; 156 break;
140 case DWT.RIGHT: 157 case DWT.RIGHT:
141 align = OS.NSRightTextAlignment; 158 align_ = OS.NSRightTextAlignment;
142 } 159 }
143 } 160 }
144 paragraph.setAlignment(align); 161 paragraph.setAlignment(align_);
145 paragraph.setLineSpacing(spacing); 162 paragraph.setLineSpacing(spacing);
146 paragraph.setFirstLineHeadIndent(indent); 163 paragraph.setFirstLineHeadIndent(indent);
147 164
148 //TODO tabs ascend descent wrap 165 //TODO tabs ascend descent wrap
149 166
1227 * </ul> 1244 * </ul>
1228 */ 1245 */
1229 public void setSegments(int[] segments) { 1246 public void setSegments(int[] segments) {
1230 checkLayout(); 1247 checkLayout();
1231 if (this.segments is null && segments is null) return; 1248 if (this.segments is null && segments is null) return;
1232 if (this.segments !is null && segments !isnull) { 1249 if (this.segments !is null && segments !is null) {
1233 if (this.segments.length is segments.length) { 1250 if (this.segments.length is segments.length) {
1234 int i; 1251 int i;
1235 for (i = 0; i <segments.length; i++) { 1252 for (i = 0; i <segments.length; i++) {
1236 if (this.segments[i] !is segments[i]) break; 1253 if (this.segments[i] !is segments[i]) break;
1237 } 1254 }
1359 * </ul> 1376 * </ul>
1360 */ 1377 */
1361 public void setTabs(int[] tabs) { 1378 public void setTabs(int[] tabs) {
1362 checkLayout(); 1379 checkLayout();
1363 if (this.tabs is null && tabs is null) return; 1380 if (this.tabs is null && tabs is null) return;
1364 if (this.tabs !is null && tabs !isnull) { 1381 if (this.tabs !is null && tabs !is null) {
1365 if (this.tabs.length is tabs.length) { 1382 if (this.tabs.length is tabs.length) {
1366 int i; 1383 int i;
1367 for (i = 0; i < tabs.length; i++) { 1384 for (i = 0; i < tabs.length; i++) {
1368 if (this.tabs[i] !is tabs[i]) break; 1385 if (this.tabs[i] !is tabs[i]) break;
1369 } 1386 }