0
|
1 // Written in the D programming language
|
|
2 // www.digitalmars.com/d/
|
|
3
|
|
4 /*
|
|
5 * The contents of this file are subject to the Mozilla Public License Version
|
|
6 * 1.1 (the "License"); you may not use this file except in compliance with
|
|
7 * the License. You may obtain a copy of the License at
|
|
8 * http://www.mozilla.org/MPL/
|
|
9 *
|
|
10 * Software distributed under the License is distributed on an "AS IS" basis,
|
|
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
12 * for the specific language governing rights and limitations under the
|
|
13 * License.
|
|
14 *
|
|
15 * The Original Code is the Dynamin library.
|
|
16 *
|
|
17 * The Initial Developer of the Original Code is Jordan Miner.
|
|
18 * Portions created by the Initial Developer are Copyright (C) 2007-2009
|
|
19 * the Initial Developer. All Rights Reserved.
|
|
20 *
|
|
21 * Contributor(s):
|
|
22 * Jordan Miner <jminer7@gmail.com>
|
|
23 *
|
|
24 */
|
|
25
|
|
26 module dynamin.gui.notebook;
|
|
27
|
|
28 import dynamin.core.event;
|
|
29 import dynamin.core.list;
|
|
30 import dynamin.all_gui;
|
|
31 import dynamin.all_painting;
|
|
32 import dynamin.core.string;
|
|
33 import tango.core.Exception;
|
|
34
|
|
35 ///
|
|
36 class TabPage {
|
|
37 protected:
|
|
38 string _text;
|
|
39 Control _content;
|
|
40 Point _tabLocation;
|
|
41 Size _tabSize;
|
|
42 public:
|
|
43 /**
|
|
44 * Gets or sets the text displayed on the tab of this tab page.
|
|
45 */
|
|
46 string text() { return _text; }
|
|
47 /// ditto
|
|
48 void text(string str) { _text = str; }
|
|
49 /**
|
|
50 * Gets or sets the control that is shown in this tab page.
|
|
51 */
|
|
52 Control content() { return _content; }
|
|
53 /// ditto
|
|
54 void content(Control c) { _content = c; }
|
|
55 Point tabLocation() { return _tabLocation; }
|
|
56 void tabLocation(Point pt) { _tabLocation = pt; }
|
|
57 Size tabSize() { return _tabSize; }
|
|
58 void tabSize(Size sz) { _tabSize = sz; }
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * A notebook is a control that has tabs and changes what it displays
|
|
63 * depending upon which tab is selected.
|
|
64 *
|
|
65 * The appearance of a notebook with Windows Classic:
|
|
66 *
|
|
67 * $(IMAGE ../web/example_notebook.png)
|
|
68 */
|
|
69 class Notebook : Container {
|
|
70 protected:
|
|
71 List!(TabPage) _tabPages;
|
|
72 int _selectedIndex = -1;
|
|
73 bool _multipleLines = true;
|
|
74 Control _content;
|
|
75 package int _tabAreaSize;
|
|
76 override void whenMouseDown(MouseEventArgs e) {
|
|
77 if(e.button != MouseButton.Left)
|
|
78 return;
|
|
79 foreach(i, page; _tabPages) {
|
|
80 if((page.tabLocation+page.tabSize).contains(e.location))
|
|
81 selectedIndex = i;
|
|
82 }
|
|
83 }
|
|
84 override void whenPainting(PaintingEventArgs e) {
|
|
85 Theme.current.Notebook_paint(this, e.graphics);
|
|
86 foreach(page; _tabPages) {
|
|
87 if(page is selectedTabPage)
|
|
88 continue;
|
|
89
|
|
90 Theme.current.Tab_paint(page, this, e.graphics);
|
|
91 }
|
|
92 Theme.current.Tab_paint(selectedTabPage, this, e.graphics);
|
|
93 }
|
|
94 void whenTabPagesChanged() {
|
|
95 if(_tabPages.count == 0)
|
|
96 selectedIndex = -1;
|
|
97 else if(selectedIndex == -1)
|
|
98 selectedIndex = 0;
|
|
99 layout();
|
|
100 }
|
|
101 public:
|
|
102 /// This event occurs after a different tab is selected.
|
|
103 Event!() selectionChanged;
|
|
104 /// Override this method in a subclass to handle the SelectionChanged event.
|
|
105 protected void whenSelectionChanged(EventArgs e) {
|
|
106 if(_content !is null)
|
|
107 _children.remove(_content);
|
|
108 _content = null;
|
|
109 if(_selectedIndex >= 0) {
|
|
110 _content = selectedTabPage.content;
|
|
111 add(_content);
|
|
112 }
|
|
113 layout();
|
|
114 }
|
|
115
|
|
116 this() {
|
|
117 selectionChanged = new Event!()(&whenSelectionChanged);
|
|
118
|
|
119 _tabPages = new List!(TabPage)(&whenTabPagesChanged);
|
|
120 _focusable = true;
|
|
121 }
|
|
122 override void layout() {
|
|
123 _tabAreaSize = 20;
|
|
124 int x = 2;
|
|
125 foreach(page; _tabPages) {
|
|
126 page.tabLocation = Point(x, 2);
|
|
127 page.tabSize = Size(70, 18);
|
|
128 x += 70;
|
|
129 }
|
|
130 if(_content) {
|
|
131 auto border = Theme.current.Notebook_borderSize(this);
|
|
132 _content.location = [border.left, _tabAreaSize+border.top];
|
|
133 _content.size = [width-border.left-border.right, height-border.top-border.bottom-_tabAreaSize];
|
|
134 }
|
|
135 }
|
|
136 /**
|
|
137 * Gets the tab pages displayed in this notebook.
|
|
138 * Examples:
|
|
139 * -----
|
|
140 * TabPage advancedPage = new TabPage;
|
|
141 * advancedPage.text = "Advanced";
|
|
142 * advancedPage.content = advancedPanel; // a previously created Panel
|
|
143 * tabbedView.TabPages.Add(advancedPage);
|
|
144 * -----
|
|
145 */
|
|
146 List!(TabPage) tabPages() { return _tabPages; }
|
|
147 /**
|
|
148 * Gets or sets the selected tab using its index. An index of -1 means
|
|
149 * there is no selected tab.
|
|
150 */
|
|
151 int selectedIndex() { return _selectedIndex; }
|
|
152 /// ditto
|
|
153 void selectedIndex(int index) {
|
|
154 if(index < -1)
|
|
155 throw new IllegalArgumentException("index cannot be less than -1");
|
|
156 _selectedIndex = index;
|
|
157 selectionChanged(new EventArgs);
|
|
158 }
|
|
159
|
|
160 /**
|
|
161 * Gets or sets the selected tab using its tab page.
|
|
162 * A value of null means there is no selected tab.
|
|
163 * A specified tab page must be in the TabPages list.
|
|
164 */
|
|
165 TabPage selectedTabPage() {
|
|
166 if(_selectedIndex == -1)
|
|
167 return null;
|
|
168 else
|
|
169 return _tabPages[_selectedIndex];
|
|
170 }
|
|
171 /// ditto
|
|
172 void selectedTabPage(TabPage p) {
|
|
173 if(p is null) {
|
|
174 selectedIndex = -1;
|
|
175 return;
|
|
176 }
|
|
177 foreach(i, page; _tabPages) {
|
|
178 if(p is page) {
|
|
179 selectedIndex = i;
|
|
180 break;
|
|
181 }
|
|
182 }
|
|
183 // if here, do nothing
|
|
184 }
|
|
185 bool multipleLines() { return _multipleLines; }
|
|
186 void multipleLines(bool b) {
|
|
187 if(b == false)
|
|
188 throw new Exception("sorry, MultipleLines = false not implemented");
|
|
189 _multipleLines = b;
|
|
190 }
|
|
191 }
|
|
192
|