comparison dwtx/novocode/ishell/internal/CustomDrawnButton.d @ 188:e3780acbbf80

Added ported sources from Novocode, thanks to WasserDragoon
author Frank Benoit <benoit@tionex.de>
date Sun, 26 Oct 2008 14:54:39 +0100
parents
children df4e66472aff
comparison
equal deleted inserted replaced
187:293a2f22f944 188:e3780acbbf80
1 /*******************************************************************************
2 * Copyright (c) 2005 Stefan Zeiger and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.novocode.com/legal/epl-v10.html
7 *
8 * Contributors:
9 * Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
10 *******************************************************************************/
11
12 module dwtx.novocode.ishell.internal.CustomDrawnButton;
13
14 import dwt.DWT;
15 import dwt.graphics.Point;
16 import dwt.widgets.Canvas;
17 import dwt.widgets.Composite;
18 import dwt.widgets.Display;
19 import dwt.widgets.Event;
20 import dwt.widgets.Listener;
21
22
23 /**
24 * A simple button control which needs to be subclassed to draw a specific
25 * kind of button. This base class provides the event handling.
26 *
27 * @author Stefan Zeiger (szeiger@novocode.com)
28 * @since Jan 30, 2005
29 * @version $Id: CustomDrawnButton.java 320 2005-02-26 13:37:02 +0000 (Sat, 26 Feb 2005) szeiger $
30 */
31
32 class CustomDrawnButton : Canvas
33 {
34 private bool pressed;
35 private Display display;
36 private bool drawnMouseIn = false;
37
38
39 this(Composite parent, int style)
40 {
41 super(parent, style);
42 this.display = getDisplay();
43
44 addListener(DWT.Paint, dgListener(&paintListener));
45
46 addListener(DWT.MouseDown, dgListener(&onMouseDown));
47
48 addListener(DWT.MouseUp, dgListener(&onMouseUp));
49
50 addListener(DWT.MouseMove, dgListener(&onMouseMove));
51 }
52
53
54 private void paintListener(Event event)
55 {
56 bool mouseIn = mouseIn();
57 onPaint(event, pressed && mouseIn);
58 drawnMouseIn = mouseIn;
59 }
60
61
62 private void onMouseDown(Event event)
63 {
64 if(event.button is 1)
65 {
66 pressed = true;
67 redraw();
68 }
69 else if(event.button is 3 && (event.stateMask & DWT.BUTTON1) !is 0) // chord click
70 {
71 pressed = false;
72 redraw();
73 }
74 }
75
76
77 private void onMouseUp(Event event)
78 {
79 if(pressed && (event.stateMask & DWT.BUTTON1) !is 0)
80 {
81 pressed = false;
82 if(mouseIn())
83 {
84 Event selectionEvent = new Event();
85 notifyListeners(DWT.Selection, selectionEvent);
86 }
87 if(!isDisposed()) redraw();
88 }
89 }
90
91
92 private void onMouseMove(Event event)
93 {
94 if(!pressed) return;
95 bool mouseIn = mouseIn();
96 if(mouseIn is drawnMouseIn) return;
97 redraw();
98 }
99
100
101 private bool mouseIn()
102 {
103 Point p = toControl(display.getCursorLocation());
104 if(p.x < -1 || p.y < -1) return false;
105 Point size = getSize();
106 return p.x <= size.x+1 && p.y <= size.y+1;
107 }
108
109
110 public Point computeSize(int wHint, int hHint, bool changed)
111 {
112 checkWidget();
113 if(wHint is DWT.DEFAULT) wHint = 0;
114 if(hHint is DWT.DEFAULT) hHint = 0;
115 return new Point(wHint, hHint);
116 }
117
118
119 public bool setFocus()
120 {
121 checkWidget();
122 return false;
123 }
124
125
126 public bool isReparentable ()
127 {
128 checkWidget();
129 return false;
130 }
131
132
133 protected abstract void onPaint(Event event, bool pressed);
134 }