Mercurial > projects > dynamin
annotate dynamin/gui/notebook.d @ 78:651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 06 Feb 2010 15:12:52 -0600 |
parents | 6580fabb7dce |
children | 3cfc83a99cbc |
rev | line source |
---|---|
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: | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
71 List!(TabPage, true) _tabPages; |
0 | 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 } | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
94 void whenTabPagesChanged(TabPage page, int) { |
0 | 95 if(_tabPages.count == 0) |
96 selectedIndex = -1; | |
97 else if(selectedIndex == -1) | |
98 selectedIndex = 0; | |
99 layout(); | |
100 } | |
101 public: | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
9
diff
changeset
|
102 /// Override this method in a subclass to handle the selectionChanged event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
103 protected void whenSelectionChanged(EventArgs e) { |
0 | 104 if(_content !is null) |
105 _children.remove(_content); | |
106 _content = null; | |
107 if(_selectedIndex >= 0) { | |
108 _content = selectedTabPage.content; | |
109 add(_content); | |
110 } | |
111 layout(); | |
112 } | |
9
682fa50ab831
Fix stupid mistake in last commit.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
113 /// This event occurs after a different tab is selected. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
9
diff
changeset
|
114 Event!(whenSelectionChanged) selectionChanged; |
0 | 115 |
116 this() { | |
78
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
68
diff
changeset
|
117 selectionChanged.setUp(&whenSelectionChanged); |
0 | 118 |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
119 _tabPages = new List!(TabPage, true)(&whenTabPagesChanged, &whenTabPagesChanged); |
0 | 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 */ | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
146 List!(TabPage, true) tabPages() { return _tabPages; } |
0 | 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) { | |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
154 if(_selectedIndex == index) |
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
155 return; |
0 | 156 if(index < -1) |
157 throw new IllegalArgumentException("index cannot be less than -1"); | |
158 _selectedIndex = index; | |
159 selectionChanged(new EventArgs); | |
160 } | |
161 | |
162 /** | |
163 * Gets or sets the selected tab using its tab page. | |
164 * A value of null means there is no selected tab. | |
165 * A specified tab page must be in the TabPages list. | |
166 */ | |
167 TabPage selectedTabPage() { | |
168 if(_selectedIndex == -1) | |
169 return null; | |
170 else | |
171 return _tabPages[_selectedIndex]; | |
172 } | |
173 /// ditto | |
174 void selectedTabPage(TabPage p) { | |
175 if(p is null) { | |
176 selectedIndex = -1; | |
177 return; | |
178 } | |
179 foreach(i, page; _tabPages) { | |
180 if(p is page) { | |
181 selectedIndex = i; | |
182 break; | |
183 } | |
184 } | |
185 // if here, do nothing | |
186 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
187 |
0 | 188 bool multipleLines() { return _multipleLines; } |
189 void multipleLines(bool b) { | |
190 if(b == false) | |
191 throw new Exception("sorry, MultipleLines = false not implemented"); | |
192 _multipleLines = b; | |
193 } | |
194 } | |
195 |