forked from OpenKore/openkore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockNotebook.pm
More file actions
299 lines (254 loc) · 7.31 KB
/
DockNotebook.pm
File metadata and controls
299 lines (254 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#########################################################################
# OpenKore - WxWidgets Interface
#
# Copyright (c) 2005,2007 OpenKore development team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# $Revision$
# $Id$
#
#########################################################################
##
# MODULE DESCRIPTION: Notebook control which supports undocking of its children
#
# This is a special notebook control. It hides the tab when there's only one child.
# All children in this notebook control will have a title bar, and can be detached
# into a dialog.
#
# This control is designed to only contain one instance for each type of child
# controls. For example, in OpenKore you can only have one Advanced Configuration
# panel.
package Interface::Wx::DockNotebook;
use strict;
use Wx ':everything';
use Wx::Event qw(EVT_NOTEBOOK_PAGE_CHANGING EVT_TIMER);
use base qw(Wx::Panel);
use Interface::Wx::DockNotebook::Page;
##############################
### CATEGORY: Constructor
##############################
##
# Interface::Wx::DockNotebook->new(parent, id)
# parent: The parent window. Must not be undef.
# id: The window identifier.
# Returns: a new Interface::Wx::DockNotebook control.
#
# Creates a new Interface::Wx::DockNotebook control.
# You can add pages by using $docknotebook->newPage()
sub new {
my ($class, $parent, $id) = @_;
my $self = $class->SUPER::new($parent, $id);
my $sizer = $self->{sizer} = new Wx::BoxSizer(wxVERTICAL);
$self->{dialogs} = [];
$self->SetSizer($sizer);
EVT_NOTEBOOK_PAGE_CHANGING($self, $id, \&onPageChanging);
return $self;
}
##############################
### CATEGORY: Methods
##############################
##
# $docknotebook->newPage(show_buttons, title, [select = 1])
# show_buttons: Whether this page should have detach/close buttons.
# title: a title for this tab.
# Returns: a Interface::Wx::DockNotebook::Page control.
#
# Adds a new page to the notebook.
#
# See also: Interface::Wx::DockNotebook::Page->set()
#
# Example:
# my $page;
#
# $page = $docknotebook->newPage(1, "Tab 1");
# $page->set(new Wx::Button($page, -1, "Click Me!"));
#
# $page = $docknotebook->newPage(1, "Tab 2");
# $page->set(new Wx::Button($page, -1, "Hello World"));
sub newPage {
my ($self, $show_buttons, $title, $select) = @_;
my $page;
$select = 1 if (!defined $select);
if (!$self->{page} && !$self->{notebook}) {
# This is the first child
$page = new Interface::Wx::DockNotebook::Page($self, $show_buttons, $title);
$self->{sizer}->Add($page, 1, wxGROW);
$self->{page} = $page;
} else {
# We have multiple children.
if (!$self->{notebook}) {
# Create a notebook if we haven't done so already
$self->{notebook} = new Wx::Notebook($self, wxID_ANY);
$self->{sizer}->Add($self->{notebook}, 1, wxGROW);
# Reparent the first page
$self->{sizer}->Detach($self->{page});
$self->{page}->Reparent($self->{notebook});
$self->{notebook}->AddPage($self->{page}, $self->{page}{title});
$self->Layout;
}
# Finally, add the new page
$page = new Interface::Wx::DockNotebook::Page($self->{notebook}, $show_buttons, $title);
$self->{notebook}->AddPage($page, $title, $select);
delete $self->{page};
}
return $page;
}
##
# $docknotebook->closePage(page)
# page: Either an Interface::Wx::DockNotebook::Page object, or the title of a page.
#
# Close a page. If the page has been detached to a dialog, then the dialog will be closed.
sub closePage {
my ($self, $page) = @_;
my $notebook = $self->{notebook};
if (!ref($page)) {
$page = $self->hasPage($page);
return if (!$page);
if ($page->{dialog}) {
$page->{dialog}->Close;
return;
}
}
if ($notebook) {
my $n = $notebook->GetPageCount;
for (my $i = 0; $i < $n; $i++) {
if ($notebook->GetPage($i) == $page) {
$notebook->DeletePage($i);
last;
}
}
if ($n == 2) {
# The notebook only has 1 item left; we want to
# reparent the page and then get rid of the notebook
# I cannot reparent the page itself due to a bug in WxWidgets,
# so create a new page and reparent the old page's child
$page = $notebook->GetPage(0);
my $page2 = new Interface::Wx::DockNotebook::Page($self, $page->{show_buttons}, $page->{title});
$page->{child}->Reparent($page2);
$page2->set($page->{child});
$self->{sizer}->Add($page2, 1, wxGROW);
$notebook->DeletePage(0);
$self->{sizer}->Detach($notebook);
$notebook->Destroy;
delete $self->{notebook};
$self->Layout;
$self->{page} = $page2;
}
} else {
$self->{sizer}->Detach($page);
$page->Destroy;
delete $self->{page};
$self->Layout;
}
}
##
# $docknotebook->hasPage(title)
# Returns: the Interface::Wx::DockNotebook::Page object which has the same title, or undef or nothing found.
#
# Check whether the notebook contains a page with title $title.
sub hasPage {
my ($self, $title) = @_;
my $notebook = $self->{notebook};
foreach (@{$self->{dialogs}}) {
next if (!$_);
if ($_->{title} eq $title) {
return $_;
}
}
if (!$notebook) {
if ($self->{page} && $self->{page}{title} eq $title) {
return $self->{page};
} else {
return;
}
}
my $n = $notebook->GetPageCount;
for (my $i = 0; $i < $n; $i++) {
if ($notebook->GetPage($i)->{title} eq $title) {
return $notebook->GetPage($i);
}
}
return;
}
##
# $docknotebook->hasPage(title)
# Returns: 1 on success, 0 on failure.
#
# Make sure the page with title $title is visible. If the page has been
# detached to a dialog, then that dialog will be raised.
sub switchPage {
my ($self, $title) = @_;
my $notebook = $self->{notebook};
foreach (@{$self->{dialogs}}) {
next if (!$_);
if ($_->{title} eq $title) {
$_->{dialog}->Raise;
return 1;
}
}
if (!$notebook) {
if ($self->{page} && $self->{page}{title} eq $title) {
return 1;
} else {
return 0;
}
}
my $n = $notebook->GetPageCount;
for (my $i = 0; $i < $n; $i++) {
if ($notebook->GetPage($i)->{title} eq $title) {
$notebook->SetSelection($i);
return 1;
}
}
return 0;
}
sub nextPage {
my ($self) = @_;
return unless ($self->{notebook});
my $i = $self->{notebook}->GetSelection;
$self->{notebook}->SetSelection($i + 1) if ($i < $self->{notebook}->GetPageCount);
}
sub prevPage {
my ($self) = @_;
return unless ($self->{notebook});
my $i = $self->{notebook}->GetSelection;
$self->{notebook}->SetSelection($i - 1) if ($i > 0);
}
####################
# Private
####################
sub onPageChanging {
my ($self, $event) = @_;
my $focus = Wx::Window::FindFocus;
my $sub;
if (!$focus) {
my $page = $self->{notebook}->GetPage($event->GetSelection);
$sub = sub {
$page->{child}->SetFocus;
} if ($page && $page->{child});
} elsif ($focus->isa('Interface::Wx::Input')) {
$sub = sub {
my ($from, $to) = $focus->GetSelection;
$focus->SetFocus;
$focus->SetSelection($from, $to);
};
} else {
$event->Skip;
return;
}
if ($sub) {
my $timer = new Wx::Timer($self, 1300);
EVT_TIMER($self, 1300, $sub);
$timer->Start(10, 1);
}
}
1;