root/branches/0.2/controlexample/canvastab.d

Revision 46, 9.0 kB (checked in by JJR, 4 years ago)

* 0.2 Branch added: New working DWT version submitted by Shawn Liu.

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 module controlexample.canvastab;
12
13 private {
14 import swt.all;
15 import controlexample.tab;
16 import controlexample.controlexample;
17 }
18
19 class CanvasTab : Tab {
20     static int colors [] = [
21         SWT.COLOR_RED,
22         SWT.COLOR_GREEN,
23         SWT.COLOR_BLUE,
24         SWT.COLOR_MAGENTA,
25         SWT.COLOR_YELLOW,
26         SWT.COLOR_CYAN,
27         SWT.COLOR_DARK_RED,
28         SWT.COLOR_DARK_GREEN,
29         SWT.COLOR_DARK_BLUE,
30         SWT.COLOR_DARK_MAGENTA,
31         SWT.COLOR_DARK_YELLOW,
32         SWT.COLOR_DARK_CYAN
33     ];
34    
35     /* Example widgets and groups that contain them */
36     Canvas canvas;
37     Group canvasGroup;
38
39     /* Style widgets added to the "Style" group */
40     Button horizontalButton, verticalButton, noBackgroundButton, noFocusButton, noMergePaintsButton, noRedrawResizeButton;
41
42     /* Other widgets added to the "Other" group */
43     Button caretButton, fillDamageButton;
44
45     int paintCount;
46     int cx, cy;
47     int maxX, maxY;
48    
49     /**
50      * Creates the Tab within a given instance_ren of ControlExample.
51      */
52     this(ControlExample instance_ren) {
53         super(instance_ren);
54     }
55    
56     /**
57      * Creates a bitmap image.
58      */
59     Image createBitmapImage (Display display, String name) {
60         ImageData source = new ImageData(name ~ ".bmp");
61         ImageData mask = new ImageData(name ~ "_mask.bmp");
62         return new Image (display, source, mask);
63     }
64
65     /**
66      * Creates the "Other" group.
67      */
68     void createOtherGroup () {
69         super.createOtherGroup ();
70    
71         /* Create display controls specific to this example */
72         caretButton = new Button (otherGroup, SWT.CHECK);
73         caretButton.setText (ControlExample.getResourceString("Caret"));
74         fillDamageButton = new Button (otherGroup, SWT.CHECK);
75         fillDamageButton.setText (ControlExample.getResourceString("FillDamage"));
76            
77         /* Add the listeners */
78         caretButton.handleEvent(this, SWT.Selection, &setCaret);
79     }
80    
81     /**
82      * Creates the "Example" group.
83      */
84     void createExampleGroup () {
85         super.createExampleGroup ();
86        
87         /* Create a group for the canvas widget */
88         canvasGroup = new Group (exampleGroup, SWT.NONE);
89         canvasGroup.setLayout (new GridLayout ());
90         canvasGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
91         canvasGroup.setText ("Canvas");
92     }
93    
94     /**
95      * Creates the "Example" widgets.
96      */
97     void createExampleWidgets () {
98        
99         /* Compute the widget style */
100         int style = getDefaultStyle();
101         if (horizontalButton.getSelection ()) style |= SWT.H_SCROLL;
102         if (verticalButton.getSelection ()) style |= SWT.V_SCROLL;
103         if (borderButton.getSelection ()) style |= SWT.BORDER;
104         if (noBackgroundButton.getSelection ()) style |= SWT.NO_BACKGROUND;
105         if (noFocusButton.getSelection ()) style |= SWT.NO_FOCUS;
106         if (noMergePaintsButton.getSelection ()) style |= SWT.NO_MERGE_PAINTS;
107         if (noRedrawResizeButton.getSelection ()) style |= SWT.NO_REDRAW_RESIZE;
108
109         /* Create the example widgets */
110         paintCount = 0; cx = 0; cy = 0;
111         canvas = new Canvas (canvasGroup, style);
112         canvas.handlePaint(this, delegate(PaintEvent e) {
113             CanvasTab tab = cast(CanvasTab)e.cData;
114             tab.paintCount++;
115             GC gc = e.gc;
116             if (tab.fillDamageButton.getSelection ()) {
117                 Color color = e.display.getSystemColor (tab.colors [tab.paintCount % cast(int)(tab.colors.length)]);
118                 gc.setBackground(color);
119                 gc.fillRectangle(e.x, e.y, e.width, e.height);
120             }
121             Point size = tab.canvas.getSize ();
122             gc.drawArc(tab.cx + 1, tab.cy + 1, size.x - 2, size.y - 2, 0, 360);
123             gc.drawRectangle(tab.cx + (size.x - 10) / 2, tab.cy + (size.y - 10) / 2, 10, 10);
124         });
125
126         canvas.handleMove(this, delegate(ControlEvent event) {
127             CanvasTab tab = cast(CanvasTab)event.cData;
128             Point size = tab.canvas.getSize ();
129             tab.maxX = size.x * 3 / 2;
130             tab.maxY = size.y * 3 / 2;
131             tab.resizeScrollBars ();
132         });
133         ScrollBar bar = canvas.getHorizontalBar();
134         if (bar !== null) {
135             bar.handleSelection(this, delegate(SelectionEvent event) {
136                 CanvasTab tab = cast(CanvasTab)event.cData;
137                 tab.scrollHorizontal (cast(ScrollBar)event.widget);
138             });
139         }
140         bar = canvas.getVerticalBar();
141         if (bar !== null) {
142             bar.handleSelection(this, delegate(SelectionEvent event) {
143                 CanvasTab tab = cast(CanvasTab)event.cData;
144                 tab.scrollVertical (cast(ScrollBar)event.widget);
145             });
146         }
147         resizeScrollBars ();
148     }
149    
150     /**
151      * Creates the "Style" group.
152      */
153     void createStyleGroup() {
154         super.createStyleGroup();
155    
156         /* Create the extra widgets */
157         horizontalButton = new Button (styleGroup, SWT.CHECK);
158         horizontalButton.setText ("SWT.H_SCROLL");
159         horizontalButton.setSelection(true);
160         verticalButton = new Button (styleGroup, SWT.CHECK);
161         verticalButton.setText ("SWT.V_SCROLL");
162         verticalButton.setSelection(true);
163         borderButton = new Button (styleGroup, SWT.CHECK);
164         borderButton.setText ("SWT.BORDER");
165         noBackgroundButton = new Button (styleGroup, SWT.CHECK);
166         noBackgroundButton.setText ("SWT.NO_BACKGROUND");
167         noFocusButton = new Button (styleGroup, SWT.CHECK);
168         noFocusButton.setText ("SWT.NO_FOCUS");
169         noMergePaintsButton = new Button (styleGroup, SWT.CHECK);
170         noMergePaintsButton.setText ("SWT.NO_MERGE_PAINTS");
171         noRedrawResizeButton = new Button (styleGroup, SWT.CHECK);
172         noRedrawResizeButton.setText ("SWT.NO_REDRAW_RESIZE");
173     }
174
175     /**
176      * Creates the tab folder page.
177      *
178      * @param tabFolder org.eclipse.swt.widgets.TabFolder
179      * @return the new page for the tab folder
180      */
181     Composite createTabFolderPage (TabFolder tabFolder) {
182         super.createTabFolderPage (tabFolder);
183
184         /*
185          * Add a resize listener to the tabFolderPage so that
186          * if the user types into the example widget to change
187          * its preferred size, and then resizes the shell, we
188          * recalculate the preferred size correctly.
189          */
190         tabFolderPage.handleEvent(this, SWT.Resize, &setExampleWidgetSize);
191        
192         return tabFolderPage;
193     }
194
195     /**
196      * Gets the "Example" widget children.
197      */
198     Control [] getExampleWidgets () {
199         Control[] result;
200         result ~= canvas;
201         return result;
202     }
203    
204     /**
205      * Gets the text for the tab folder item.
206      */
207     String getTabText () {
208         return "Canvas";
209     }
210    
211     /**
212      * Resizes the maximum and thumb of both scrollbars.
213      */
214     void resizeScrollBars () {
215         Rectangle clientArea = canvas.getClientArea();
216         ScrollBar bar = canvas.getHorizontalBar();
217         if (bar !== null) {
218             bar.setMaximum(maxX);
219             bar.setThumb(clientArea.width);
220             bar.setPageIncrement(clientArea.width);
221         }
222         bar = canvas.getVerticalBar();
223         if (bar !== null) {
224             bar.setMaximum(maxY);
225             bar.setThumb(clientArea.height);
226             bar.setPageIncrement(clientArea.height);
227         }
228     }
229
230     /**
231      * Scrolls the canvas horizontally.
232      *
233      * @param scrollBar
234      */
235     void scrollHorizontal (ScrollBar scrollBar) {
236         assert(scrollBar);
237         Rectangle bounds = canvas.getClientArea();
238         int x = -scrollBar.getSelection();
239         if (x + maxX < bounds.width) {
240             x = bounds.width - maxX;
241         }
242         canvas.scroll(x, cy, cx, cy, maxX, maxY, false);
243         cx = x;
244     }
245
246     /**
247      * Scrolls the canvas vertically.
248      *
249      * @param scrollBar
250      */
251     void scrollVertical (ScrollBar scrollBar) {
252         assert(scrollBar);
253         Rectangle bounds = canvas.getClientArea();
254         int y = -scrollBar.getSelection();
255         if (y + maxY < bounds.height) {
256             y = bounds.height - maxY;
257         }
258         canvas.scroll(cx, y, cx, cy, maxX, maxY, false);
259         cy = y;
260     }
261
262     /**
263      * Sets or clears the caret in the "Example" widget.
264      */
265     void setCaret () {
266         Caret oldCaret = canvas.getCaret ();
267         if (caretButton.getSelection ()) {
268             Caret newCaret = new Caret(canvas, SWT.NONE);
269             Font font = canvas.getFont();
270             newCaret.setFont(font);
271             GC gc = new GC(canvas);
272             gc.setFont(font);
273             newCaret.setBounds(1, 1, 1, gc.getFontMetrics().getHeight());
274             gc.dispose();
275             canvas.setCaret (newCaret);
276             canvas.setFocus();
277         } else {
278             canvas.setCaret (null);
279         }
280         if (oldCaret !== null) oldCaret.dispose ();
281     }
282    
283     /**
284      * Sets the state of the "Example" widgets.
285      */
286     void setExampleWidgetState () {
287         super.setExampleWidgetState ();
288         horizontalButton.setSelection ((canvas.getStyle () & SWT.H_SCROLL) != 0);
289         verticalButton.setSelection ((canvas.getStyle () & SWT.V_SCROLL) != 0);
290         borderButton.setSelection ((canvas.getStyle () & SWT.BORDER) != 0);
291         noBackgroundButton.setSelection ((canvas.getStyle () & SWT.NO_BACKGROUND) != 0);
292         noFocusButton.setSelection ((canvas.getStyle () & SWT.NO_FOCUS) != 0);
293         noMergePaintsButton.setSelection ((canvas.getStyle () & SWT.NO_MERGE_PAINTS) != 0);
294         noRedrawResizeButton.setSelection ((canvas.getStyle () & SWT.NO_REDRAW_RESIZE) != 0);
295         setCaret ();
296     }
297 }
Note: See TracBrowser for help on using the browser.