-
Notifications
You must be signed in to change notification settings - Fork 0
/
TabsContainer.cpp
170 lines (141 loc) · 3.32 KB
/
TabsContainer.cpp
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
/*
* Copyright 2024, Andrea Anzani <andrea.anzani@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "TabsContainer.h"
#include "TabView.h"
#include "GTabView.h"
#include <cassert>
class Filler : public BView
{
public:
Filler():BView("filler", B_WILL_DRAW /*| B_FULL_UPDATE_ON_RESIZE*/) { }
void Draw(BRect rect)
{
BRect bounds(Bounds());
TabViewTools::DrawTabBackground(this, bounds, rect);
}
};
TabsContainer::TabsContainer(GTabView* tabView, BMessage* message):
BGroupView(B_HORIZONTAL, 0.0f),
BInvoker(message, nullptr, nullptr),
fSelectedTab(nullptr),
fGTabView(tabView),
fTabShift(0)
{
SetFlags(Flags()|B_FRAME_EVENTS);
GroupLayout()->AddView(0, new Filler());
SetExplicitMinSize(BSize(100, TabViewTools::DefaultTabHeigh()));
SetExplicitAlignment(BAlignment(B_ALIGN_LEFT, B_ALIGN_VERTICAL_CENTER));
}
void
TabsContainer::AddTab(TabView* tab)
{
BLayoutItem* item = GroupLayout()->AddView(CountTabs(), tab);
tab->SetLayoutItem (item);
if (CountTabs() == 1) {
//printf("Adding %s\n", tab->Label());
tab->SetIsFront(true);
fSelectedTab = tab;
} else {
TabView* prev = TabAt(CountTabs()-2);
//printf("Prev: %s\n", prev->Label());
prev->SetIsFront(prev == fSelectedTab);
tab->SetIsFront(false);
}
}
int32
TabsContainer::CountTabs()
{
return GroupLayout()->CountItems() - 1; //exclude the Filler.
}
TabView*
TabsContainer::TabAt(int32 index)
{
if (index < 0 || index >= CountTabs())
return nullptr;
return dynamic_cast<TabView*>(GroupLayout()->ItemAt(index)->View());
}
int32
TabsContainer::IndexOfTab(TabView* tab)
{
assert(tab);
return GroupLayout()->IndexOfItem(tab->LayoutItem());
}
void
TabsContainer::ShiftTabs(int32 delta)
{
//printf("1) ShiftTabs %d\n", fTabShift);
int32 newShift = fTabShift + delta;
if (newShift < 0)
newShift = 0;
int32 max = std::max(newShift, fTabShift);
for (int32 i=0;i<max;i++) {
TabView* tab = TabAt(i);
if (i < newShift) {
if (tab->IsHidden() == false)
tab->Hide();
} else {
if (tab->IsHidden() == true)
tab->Show();
}
}
fTabShift = newShift;
_UpdateScrolls();
//printf("2) ShiftTabs %d\n", fTabShift);
}
void
TabsContainer::MouseDown(TabView* tab, BPoint where)
{
if (tab != fSelectedTab) {
int32 index = IndexOfTab(tab);
if (fSelectedTab != nullptr)
fSelectedTab->SetIsFront(false);
tab->SetIsFront(true);
fSelectedTab = tab;
if (Message() && Target()) {
BMessage msg = *Message();
msg.AddPointer("tab", tab);
msg.AddInt32("index", index);
Invoke(&msg);
}
}
OnMouseDown(where);
}
void
TabsContainer::FrameResized(float w, float h)
{
//Auto-scroll:
if (fTabShift > 0) {
//ts 1 -> 0
//ts 2 -> 0-1
int32 tox = 0;
TabView* last = TabAt(CountTabs()-1);
float right = last->Frame().right;
for (int32 i=fTabShift - 1;i>=0;i--){
//int32 idx = fTabShift - i - 1;
// printf("IDX %d\n", i);
TabView* tab = TabAt(i);
right = right + tab->Frame().Width();
if (right < w)
tox--;
else
break;
}
if (tox != 0)
ShiftTabs(tox);
}
//end
_UpdateScrolls();
BGroupView::FrameResized(w,h);
}
void
TabsContainer::_UpdateScrolls()
{
if (CountTabs() > 0) {
GroupLayout()->Relayout(true);
TabView* last = TabAt(CountTabs()-1);
if(fGTabView != nullptr)
fGTabView->UpdateScrollButtons(fTabShift != 0, last->Frame().right > Bounds().right);
}
}