comparison dwtx/ui/forms/widgets/ScrolledPageBook.d @ 75:5d489b9f966c

Fix continue porting
author Frank Benoit <benoit@tionex.de>
date Sat, 24 May 2008 05:11:16 +0200
parents
children 04b47443bb01
comparison
equal deleted inserted replaced
74:dad2e11b8ae4 75:5d489b9f966c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation 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.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.ui.forms.widgets.ScrolledPageBook;
14
15 import dwtx.ui.forms.widgets.SharedScrolledComposite;
16 import dwtx.ui.forms.widgets.LayoutComposite;
17
18 import dwt.DWT;
19 import dwt.graphics.Point;
20 import dwt.graphics.Rectangle;
21 import dwt.layout.GridLayout;
22 import dwt.widgets.Composite;
23 import dwt.widgets.Control;
24 import dwt.widgets.Event;
25 import dwt.widgets.Listener;
26 import dwtx.ui.internal.forms.widgets.WrappedPageBook;
27
28 import dwt.dwthelper.utils;
29 import tango.util.collection.HashMap;
30 /**
31 * ScrolledPageBook is a class that is capable of stacking several composites
32 * (pages), while showing one at a time. The content is scrolled if there is
33 * not enough space to fit it in the client area.
34 *
35 * @since 3.0
36 */
37 public class ScrolledPageBook : SharedScrolledComposite {
38 private WrappedPageBook pageBook;
39 private HashMap!(Object,Object) pages;
40 private Composite emptyPage;
41 private Control currentPage;
42 /**
43 * Creates a new instance in the provided parent
44 *
45 * @param parent
46 */
47 public this(Composite parent) {
48 this(parent, DWT.H_SCROLL | DWT.V_SCROLL);
49 }
50 /**
51 * Creates a new instance in the provided parent and with the provided
52 * style.
53 *
54 * @param parent
55 * the control parent
56 * @param style
57 * the style to use
58 */
59 public this(Composite parent, int style) {
60 super(parent, style);
61 pageBook = new WrappedPageBook(this, DWT.NULL);
62 setContent(pageBook);
63 pages = new HashMap!(Object,Object);
64 setExpandHorizontal(true);
65 setExpandVertical(true);
66 this.addListener(DWT.Traverse, dgListener( (Event e) {
67 switch (e.detail) {
68 case DWT.TRAVERSE_ESCAPE :
69 case DWT.TRAVERSE_RETURN :
70 case DWT.TRAVERSE_TAB_NEXT :
71 case DWT.TRAVERSE_TAB_PREVIOUS :
72 e.doit = true;
73 break;
74 }
75 }));
76 }
77 /**
78 * Removes the default size of the composite, allowing the control to
79 * shrink to the trim.
80 *
81 * @param wHint
82 * the width hint
83 * @param hHint
84 * the height hint
85 * @param changed
86 * if <code>true</code>, do not use cached values
87 */
88 public Point computeSize(int wHint, int hHint, bool changed) {
89 Rectangle trim = computeTrim(0, 0, 10, 10);
90 return new Point(trim.width, trim.height);
91 }
92 /**
93 * Tests if the page under the provided key is currently in the book.
94 *
95 * @param key
96 * the page key
97 * @return <code>true</code> if page exists, <code>false</code>
98 * otherwise.
99 */
100 public bool hasPage(Object key) {
101 return pages.containsKey(key);
102 }
103 /**
104 * Creates a new page for the provided key. Use the returned composite to
105 * create children in it.
106 *
107 * @param key
108 * the page key
109 * @return the newly created page composite
110 */
111 public Composite createPage(Object key) {
112 Composite page = createPage();
113 pages.add(key, page);
114 return page;
115 }
116 /**
117 * Returns the page book container.
118 *
119 * @return the page book container
120 */
121 public Composite getContainer() {
122 return pageBook;
123 }
124 /**
125 * Registers a page under the privided key to be managed by the page book.
126 * The page must be a direct child of the page book container.
127 *
128 * @param key
129 * the page key
130 * @param page
131 * the page composite to register
132 * @see #createPage(Object)
133 * @see #getContainer
134 */
135 public void registerPage(Object key, Control page) {
136 pages.add(key, page);
137 }
138 /**
139 * Removes the page under the provided key from the page book. Does nothing
140 * if page with that key does not exist.
141 *
142 * @param key
143 * the page key.
144 */
145 public void removePage(Object key) {
146 removePage(key, true);
147 }
148 /**
149 * Removes the page under the provided key from the page book. Does nothing
150 * if page with that key does not exist.
151 *
152 * @param key
153 * the page key.
154 * @param showEmptyPage
155 * if <code>true</code>, shows the empty page
156 * after page removal.
157 */
158 public void removePage(Object key, bool showEmptyPage_) {
159 Control page = cast(Control) pages.get(key);
160 if (page !is null) {
161 pages.remove(key);
162 page.dispose();
163 if (showEmptyPage_)
164 showEmptyPage();
165 }
166 }
167 /**
168 * Shows the page with the provided key and hides the page previously
169 * showing. Does nothing if the page with that key does not exist.
170 *
171 * @param key
172 * the page key
173 */
174 public void showPage(Object key) {
175 Control page = cast(Control) pages.get(key);
176 if (page !is null) {
177 pageBook.showPage(page);
178 if (currentPage !is null && currentPage !is page) {
179 // switching pages - force layout
180 if (null !is cast(Composite)page )
181 (cast(Composite) page).layout(false);
182 }
183 currentPage = page;
184 } else {
185 showEmptyPage();
186 }
187 reflow(true);
188 }
189 /**
190 * Shows a page with no children to be used if the desire is to not show
191 * any registered page.
192 */
193 public void showEmptyPage() {
194 if (emptyPage is null) {
195 emptyPage = createPage();
196 emptyPage.setLayout(new GridLayout());
197 }
198 pageBook.showPage(emptyPage);
199 currentPage = emptyPage;
200 reflow(true);
201 }
202 /**
203 * Sets focus on the current page if shown.
204 */
205 public bool setFocus() {
206 if (currentPage !is null)
207 return currentPage.setFocus();
208 return super.setFocus();
209 }
210 /**
211 * Returns the page currently showing.
212 *
213 * @return the current page
214 */
215 public Control getCurrentPage() {
216 return currentPage;
217 }
218 private Composite createPage() {
219 Composite page = new LayoutComposite(pageBook, DWT.NULL);
220 page.setBackground(getBackground());
221 page.setForeground(getForeground());
222 page.setMenu(pageBook.getMenu());
223 return page;
224 }
225 }