comparison dwt/widgets/Canvas.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents 642f460a0908
children cfa563df4fdd
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
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 * 10 *
11 * Port to the D programming language: 11 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com> 12 * Jacob Carlborg <doob@me.com>
13 *******************************************************************************/ 13 *******************************************************************************/
14 module dwt.widgets.Canvas; 14 module dwt.widgets.Canvas;
15 15
16 16
17 import dwt.DWT; 17 import dwt.DWT;
20 import dwt.graphics.GC; 20 import dwt.graphics.GC;
21 import dwt.graphics.Image; 21 import dwt.graphics.Image;
22 import dwt.graphics.Rectangle; 22 import dwt.graphics.Rectangle;
23 import dwt.internal.cocoa.NSBezierPath; 23 import dwt.internal.cocoa.NSBezierPath;
24 import dwt.internal.cocoa.NSColor; 24 import dwt.internal.cocoa.NSColor;
25 import dwt.internal.cocoa.NSCursor;
26 import dwt.internal.cocoa.NSEvent;
25 import dwt.internal.cocoa.NSGraphicsContext; 27 import dwt.internal.cocoa.NSGraphicsContext;
26 import dwt.internal.cocoa.NSPoint; 28 import dwt.internal.cocoa.NSPoint;
29 import dwt.internal.cocoa.NSRange;
27 import dwt.internal.cocoa.NSRect; 30 import dwt.internal.cocoa.NSRect;
28 import dwt.internal.cocoa.NSSize; 31 import dwt.internal.cocoa.NSSize;
29 import dwt.internal.cocoa.OS; 32 import dwt.internal.cocoa.OS;
30 33
31 import dwt.dwthelper.utils; 34 import dwt.dwthelper.utils;
51 * painted using DWT graphics calls or are handled by native 54 * painted using DWT graphics calls or are handled by native
52 * methods. 55 * methods.
53 * </p> 56 * </p>
54 * 57 *
55 * @see Composite 58 * @see Composite
59 * @see <a href="http://www.eclipse.org/swt/snippets/#canvas">Canvas snippets</a>
60 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a>
61 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
56 */ 62 */
57 public class Canvas : Composite { 63 public class Canvas : Composite {
58 Caret caret; 64 Caret caret;
59 IME ime; 65 IME ime;
60 66
61 this () { 67 this () {
62 /* Do nothing */ 68 /* Do nothing */
63 } 69 }
64 70
65 bool becomeFirstResponder () { 71 int /*long*/ attributedSubstringFromRange (int /*long*/ id, int /*long*/ sel, int /*long*/ range) {
72 if (ime !is null) return ime.attributedSubstringFromRange (id, sel, range);
73 return super.attributedSubstringFromRange(id, sel, range);
74 }
75
76 bool becomeFirstResponder (int /*long*/ id, int /*long*/ sel) {
66 if (caret !is null) caret.setFocus (); 77 if (caret !is null) caret.setFocus ();
67 return super.becomeFirstResponder(); 78 return super.becomeFirstResponder(id, sel);
68 } 79 }
69 80
70 bool resignFirstResponder () { 81 bool resignFirstResponder (int /*long*/ id, int /*long*/ sel) {
71 if (caret !is null) caret.killFocus (); 82 if (caret !is null) caret.killFocus ();
72 return super.resignFirstResponder(); 83 return super.resignFirstResponder(id, sel);
73 } 84 }
74 85
75 86
76 /** 87 /**
77 * Constructs a new instance of this class given its parent 88 * Constructs a new instance of this class given its parent
102 */ 113 */
103 public this (Composite parent, int style) { 114 public this (Composite parent, int style) {
104 super (parent, style); 115 super (parent, style);
105 } 116 }
106 117
118 int /*long*/ characterIndexForPoint (int /*long*/ id, int /*long*/ sel, int /*long*/ point) {
119 if (ime !is null) return ime.characterIndexForPoint (id, sel, point);
120 return super.characterIndexForPoint (id, sel, point);
121 }
122
107 /** 123 /**
108 * Fills the interior of the rectangle specified by the arguments, 124 * Fills the interior of the rectangle specified by the arguments,
109 * with the receiver's background. 125 * with the receiver's background.
110 * 126 *
111 * @param gc the gc where the rectangle is to be filled 127 * @param gc the gc where the rectangle is to be filled
129 checkWidget (); 145 checkWidget ();
130 if (gc is null) error (DWT.ERROR_NULL_ARGUMENT); 146 if (gc is null) error (DWT.ERROR_NULL_ARGUMENT);
131 if (gc.isDisposed ()) error (DWT.ERROR_INVALID_ARGUMENT); 147 if (gc.isDisposed ()) error (DWT.ERROR_INVALID_ARGUMENT);
132 Control control = findBackgroundControl (); 148 Control control = findBackgroundControl ();
133 if (control !is null) { 149 if (control !is null) {
134 // control.fillBackground (handle, gc.handle, new Rectangle (x, y, width, height)); 150 NSRect rect = new NSRect();
151 rect.x = x;
152 rect.y = y;
153 rect.width = width;
154 rect.height = height;
155 control.fillBackground (view, gc.handle, rect);
135 } else { 156 } else {
136 gc.fillRectangle (x, y, width, height); 157 gc.fillRectangle (x, y, width, height);
137 } 158 }
138 } 159 }
139 160
140 void drawRect(objc.id id, NSRect rect) { 161 void drawWidget (objc.id id_, NSRect rect) {
141 super.drawRect(id, rect); 162 super.drawWidget (id, rect);
142 if (caret is null) return; 163 if (caret is null) return;
143 if (caret.isShowing) { 164 if (caret.isShowing) {
144 NSGraphicsContext context = NSGraphicsContext.currentContext(); 165 NSGraphicsContext context = NSGraphicsContext.currentContext();
145 166
146 Image image = caret.image; 167 Image image = caret.image;
168 context.restoreGraphicsState(); 189 context.restoreGraphicsState();
169 } 190 }
170 } 191 }
171 } 192 }
172 193
194 NSRect firstRectForCharacterRange (int /*long*/ id, int /*long*/ sel, int /*long*/ range) {
195 if (ime !is null) return ime.firstRectForCharacterRange (id, sel, range);
196 return super.firstRectForCharacterRange (id, sel, range);
197 }
198
173 /** 199 /**
174 * Returns the caret. 200 * Returns the caret.
175 * <p> 201 * <p>
176 * The caret for the control is automatically hidden 202 * The caret for the control is automatically hidden
177 * and shown when the control is painted or resized, 203 * and shown when the control is painted or resized,
179 * is scrolled. To avoid drawing on top of the caret, 205 * is scrolled. To avoid drawing on top of the caret,
180 * the programmer must hide and show the caret when 206 * the programmer must hide and show the caret when
181 * drawing in the window any other time. 207 * drawing in the window any other time.
182 * </p> 208 * </p>
183 * 209 *
184 * @return the caret 210 * @return the caret for the receiver, may be null
185 * 211 *
186 * @exception DWTException <ul> 212 * @exception DWTException <ul>
187 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 213 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
188 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 214 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
189 * </ul> 215 * </ul>
191 public Caret getCaret () { 217 public Caret getCaret () {
192 checkWidget(); 218 checkWidget();
193 return caret; 219 return caret;
194 } 220 }
195 221
222 /**
223 * Returns the IME.
224 *
225 * @return the IME
226 *
227 * @exception DWTException <ul>
228 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
229 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
230 * </ul>
231 *
232 * @since 3.4
233 */
196 public IME getIME () { 234 public IME getIME () {
197 checkWidget(); 235 checkWidget();
198 return ime; 236 return ime;
237 }
238
239 bool hasMarkedText (int /*long*/ id, int /*long*/ sel) {
240 if (ime !is null) return ime.hasMarkedText (id, sel);
241 return super.hasMarkedText (id, sel);
242 }
243
244 bool insertText (int /*long*/ id, int /*long*/ sel, int /*long*/ string) {
245 if (ime !is null) {
246 if (!ime.insertText (id, sel, string)) return false;
247 }
248 return super.insertText (id, sel, string);
249 }
250
251 NSRange markedRange (int /*long*/ id, int /*long*/ sel) {
252 if (ime !is null) return ime.markedRange (id, sel);
253 return super.markedRange (id, sel);
199 } 254 }
200 255
201 //void redrawWidget (int control, bool children) { 256 //void redrawWidget (int control, bool children) {
202 // bool isFocus = OS.VERSION < 0x1040 && caret !is null && caret.isFocusCaret (); 257 // bool isFocus = OS.VERSION < 0x1040 && caret !is null && caret.isFocusCaret ();
203 // if (isFocus) caret.killFocus (); 258 // if (isFocus) caret.killFocus ();
289 } 344 }
290 } 345 }
291 if (isFocus) caret.setFocus (); 346 if (isFocus) caret.setFocus ();
292 } 347 }
293 348
349 NSRange selectedRange (int /*long*/ id, int /*long*/ sel) {
350 if (ime !is null) return ime.selectedRange (id, sel);
351 return super.selectedRange (id, sel);
352 }
353
354 bool sendKeyEvent (NSEvent nsEvent, int type) {
355 if (caret !is null) NSCursor.setHiddenUntilMouseMoves (true);
356 return super.sendKeyEvent (nsEvent, type);
357 }
358
294 /** 359 /**
295 * Sets the receiver's caret. 360 * Sets the receiver's caret.
296 * <p> 361 * <p>
297 * The caret for the control is automatically hidden 362 * The caret for the control is automatically hidden
298 * and shown when the control is painted or resized, 363 * and shown when the control is painted or resized,
329 checkWidget (); 394 checkWidget ();
330 if (caret !is null) caret.setFont (font); 395 if (caret !is null) caret.setFont (font);
331 super.setFont (font); 396 super.setFont (font);
332 } 397 }
333 398
399 /**
400 * Sets the receiver's IME.
401 *
402 * @param ime the new IME for the receiver, may be null
403 *
404 * @exception IllegalArgumentException <ul>
405 * <li>ERROR_INVALID_ARGUMENT - if the IME has been disposed</li>
406 * </ul>
407 * @exception DWTException <ul>
408 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
409 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
410 * </ul>
411 *
412 * @since 3.4
413 */
334 public void setIME (IME ime) { 414 public void setIME (IME ime) {
335 checkWidget (); 415 checkWidget ();
336 if (ime !is null && ime.isDisposed()) error(DWT.ERROR_INVALID_ARGUMENT); 416 if (ime !is null && ime.isDisposed()) error(DWT.ERROR_INVALID_ARGUMENT);
337 this.ime = ime; 417 this.ime = ime;
338 } 418 }
339 419
340 } 420 bool setMarkedText_selectedRange (int /*long*/ id, int /*long*/ sel, int /*long*/ string, int /*long*/ range) {
421 if (ime !is null) {
422 if (!ime.setMarkedText_selectedRange (id, sel, string, range)) return false;
423 }
424 return super.setMarkedText_selectedRange (id, sel, string, range);
425 }
426
427 int /*long*/ validAttributesForMarkedText (int /*long*/ id, int /*long*/ sel) {
428 if (ime !is null) return ime.validAttributesForMarkedText (id, sel);
429 return super.validAttributesForMarkedText(id, sel);
430 }
431
432 }