Mercurial > projects > dynamin
annotate dynamin/gui/notebook.d @ 103:73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 22:06:02 -0500 |
parents | 3cfc83a99cbc |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
5 * This Source Code Form is subject to the terms of the Mozilla Public |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
6 * License, v. 2.0. If a copy of the MPL was not distributed with this |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
83
diff
changeset
|
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.gui.notebook; | |
12 | |
13 import dynamin.core.event; | |
14 import dynamin.core.list; | |
15 import dynamin.all_gui; | |
16 import dynamin.all_painting; | |
17 import dynamin.core.string; | |
18 import tango.core.Exception; | |
19 | |
20 /// | |
21 class TabPage { | |
22 protected: | |
23 string _text; | |
24 Control _content; | |
25 Point _tabLocation; | |
26 Size _tabSize; | |
27 public: | |
28 /** | |
29 * Gets or sets the text displayed on the tab of this tab page. | |
30 */ | |
31 string text() { return _text; } | |
32 /// ditto | |
33 void text(string str) { _text = str; } | |
34 /** | |
35 * Gets or sets the control that is shown in this tab page. | |
36 */ | |
37 Control content() { return _content; } | |
38 /// ditto | |
39 void content(Control c) { _content = c; } | |
40 Point tabLocation() { return _tabLocation; } | |
41 void tabLocation(Point pt) { _tabLocation = pt; } | |
42 Size tabSize() { return _tabSize; } | |
43 void tabSize(Size sz) { _tabSize = sz; } | |
44 } | |
45 | |
46 /** | |
47 * A notebook is a control that has tabs and changes what it displays | |
48 * depending upon which tab is selected. | |
49 * | |
50 * The appearance of a notebook with Windows Classic: | |
51 * | |
52 * $(IMAGE ../web/example_notebook.png) | |
53 */ | |
54 class Notebook : Container { | |
55 protected: | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
56 List!(TabPage, true) _tabPages; |
0 | 57 int _selectedIndex = -1; |
58 bool _multipleLines = true; | |
59 Control _content; | |
60 package int _tabAreaSize; | |
61 override void whenMouseDown(MouseEventArgs e) { | |
62 if(e.button != MouseButton.Left) | |
63 return; | |
64 foreach(i, page; _tabPages) { | |
65 if((page.tabLocation+page.tabSize).contains(e.location)) | |
66 selectedIndex = i; | |
67 } | |
68 } | |
69 override void whenPainting(PaintingEventArgs e) { | |
70 Theme.current.Notebook_paint(this, e.graphics); | |
71 foreach(page; _tabPages) { | |
72 if(page is selectedTabPage) | |
73 continue; | |
74 | |
75 Theme.current.Tab_paint(page, this, e.graphics); | |
76 } | |
77 Theme.current.Tab_paint(selectedTabPage, this, e.graphics); | |
78 } | |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
79 void whenTabPagesChanged(ListChangeType, TabPage oldPage, TabPage newPage, uint) { |
0 | 80 if(_tabPages.count == 0) |
81 selectedIndex = -1; | |
82 else if(selectedIndex == -1) | |
83 selectedIndex = 0; | |
84 layout(); | |
85 } | |
86 public: | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
9
diff
changeset
|
87 /// 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
|
88 protected void whenSelectionChanged(EventArgs e) { |
0 | 89 if(_content !is null) |
90 _children.remove(_content); | |
91 _content = null; | |
92 if(_selectedIndex >= 0) { | |
93 _content = selectedTabPage.content; | |
94 add(_content); | |
95 } | |
96 layout(); | |
97 } | |
9
682fa50ab831
Fix stupid mistake in last commit.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
98 /// 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
|
99 Event!(whenSelectionChanged) selectionChanged; |
0 | 100 |
101 this() { | |
78
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
68
diff
changeset
|
102 selectionChanged.setUp(&whenSelectionChanged); |
0 | 103 |
83
3cfc83a99cbc
Add List.opIndexAssign and switch to one callback for change notification.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
104 _tabPages = new List!(TabPage, true)(&whenTabPagesChanged); |
0 | 105 _focusable = true; |
106 } | |
107 override void layout() { | |
108 _tabAreaSize = 20; | |
109 int x = 2; | |
110 foreach(page; _tabPages) { | |
111 page.tabLocation = Point(x, 2); | |
112 page.tabSize = Size(70, 18); | |
113 x += 70; | |
114 } | |
115 if(_content) { | |
116 auto border = Theme.current.Notebook_borderSize(this); | |
117 _content.location = [border.left, _tabAreaSize+border.top]; | |
118 _content.size = [width-border.left-border.right, height-border.top-border.bottom-_tabAreaSize]; | |
119 } | |
120 } | |
121 /** | |
122 * Gets the tab pages displayed in this notebook. | |
123 * Examples: | |
124 * ----- | |
125 * TabPage advancedPage = new TabPage; | |
126 * advancedPage.text = "Advanced"; | |
127 * advancedPage.content = advancedPanel; // a previously created Panel | |
128 * tabbedView.TabPages.Add(advancedPage); | |
129 * ----- | |
130 */ | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
131 List!(TabPage, true) tabPages() { return _tabPages; } |
0 | 132 /** |
133 * Gets or sets the selected tab using its index. An index of -1 means | |
134 * there is no selected tab. | |
135 */ | |
136 int selectedIndex() { return _selectedIndex; } | |
137 /// ditto | |
138 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
|
139 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
|
140 return; |
0 | 141 if(index < -1) |
142 throw new IllegalArgumentException("index cannot be less than -1"); | |
143 _selectedIndex = index; | |
144 selectionChanged(new EventArgs); | |
145 } | |
146 | |
147 /** | |
148 * Gets or sets the selected tab using its tab page. | |
149 * A value of null means there is no selected tab. | |
150 * A specified tab page must be in the TabPages list. | |
151 */ | |
152 TabPage selectedTabPage() { | |
153 if(_selectedIndex == -1) | |
154 return null; | |
155 else | |
156 return _tabPages[_selectedIndex]; | |
157 } | |
158 /// ditto | |
159 void selectedTabPage(TabPage p) { | |
160 if(p is null) { | |
161 selectedIndex = -1; | |
162 return; | |
163 } | |
164 foreach(i, page; _tabPages) { | |
165 if(p is page) { | |
166 selectedIndex = i; | |
167 break; | |
168 } | |
169 } | |
170 // if here, do nothing | |
171 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
172 |
0 | 173 bool multipleLines() { return _multipleLines; } |
174 void multipleLines(bool b) { | |
175 if(b == false) | |
176 throw new Exception("sorry, MultipleLines = false not implemented"); | |
177 _multipleLines = b; | |
178 } | |
179 } | |
180 |