-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhostwindow.cpp
More file actions
295 lines (258 loc) · 8.79 KB
/
hostwindow.cpp
File metadata and controls
295 lines (258 loc) · 8.79 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
#include "hostwindow.h"
#include <TlHelp32.h>
#include <QResizeEvent>
#include <QDateTime>
#include <tchar.h>
#include <QApplication>
#include <QSettings>
#include <QFont>
#include <QDebug>
HostWindow::HostWindow(QWidget *parent) : QMainWindow(parent), timer(QDateTime::currentSecsSinceEpoch())
{
hostInstance = this;
// restore last pos/size
QSettings setting("TabSoft", "TestTab");
if(setting.contains("hostwindow/size")){
qDebug() << "restore last size..";
QSize lastSize = setting.value("hostwindow/size").toSize();
this->resize(lastSize);
}
else
this->resize(800, 400);
if(setting.contains("hostwindow/pos")){
QPoint lastPos = setting.value("hostwindow/pos").toPoint();
this->move(lastPos);
}
// init components
{
replaceContainer = new QWidget(this);
this->setCentralWidget(replaceContainer);// replace to set layout
verticalLayout = new QVBoxLayout();
verticalLayout->setMargin(0);
verticalLayout->setSpacing(0);
replaceContainer->setLayout(verticalLayout);
// init tabBarLayout
tabLayout = new QHBoxLayout();
tabLayout->setSpacing(0);
tabLayout->setMargin(0);
// init tabBar
tabBar = new MyTabBar(this);
tabLayout->addWidget(tabBar);
//init button
addTabButton = new QPushButton(this);
addTabButton->setIcon(QIcon(":/img/icons8-plus-math-40.png"));
addTabButton->setIconSize(QSize(30, 30));
addTabButton->setFlat(true);
addTabButton->setMinimumSize(QSize(40, 40));
addTabButton->setMaximumSize(QSize(40, 40));
tabLayout->addWidget(addTabButton);
// place tabBars in widget
tabWidget = new QWidget(this);
tabWidget->setMaximumHeight(40);
tabWidget->setMinimumHeight(40);
tabWidget->setLayout(tabLayout);
verticalLayout->addWidget(tabWidget);
tabContentContianer = new WidgetContainer(this);
verticalLayout->addWidget(tabContentContianer);
}
// tabBar resort event
QObject::connect(
tabBar,
&QTabBar::tabMoved,
[](int from, int to){
qDebug() << from << to;
});
// tabBar close event
QObject::connect(
tabBar,
&QTabBar::tabCloseRequested,
[this](int index){
qDebug() << "request close" << index;
if(index == tabBar->currentIndex())
delete tabContentContianer->DeleteWidget();
tabs.removeAt(index);
tabBar->removeTab(index);
if(tabs.length() == 0) QApplication::quit();
});
// select tab event
QObject::connect(
tabBar,
&QTabBar::currentChanged,
[this](int index){
qDebug() << "set current tab" << index;
if(index >= 0)
tabContentContianer->SetWidget(tabs[index]);
});
//drag&drop event
QObject::connect(
tabBar,
&MyTabBar::newTab,
[this](QString path){
this->AddExplorer(path);
});
// addTab event
QObject::connect(
addTabButton,
&QPushButton::clicked,
[this](){
this->AddExplorer();
});
}
void HostWindow::closeEvent(QCloseEvent *event)
{
hostInstance = nullptr;
tabContentContianer->DeleteWidget();
for(auto i = tabs.begin(); i != tabs.end(); i++)
{
delete (*i);
}
for (auto i = hooks.begin(); i != hooks.end(); i++) {
UnhookWinEvent((*i).titleHook);
}
qDebug() << "destroy";
if(!isMinimized() && !isMaximized()){
// save pos/size
QSettings setting("TabSoft", "TestTab");
setting.setValue("hostwindow/size", this->size());
setting.setValue("hostwindow/pos", this->pos());
}
event->accept();
}
void CALLBACK HandleWinEventName(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
LONG idObject, LONG idChild,
DWORD dwEventThread, DWORD dwmsEventTime)
{
if(hostInstance == nullptr || hwnd == nullptr) return;
hostInstance->notifyTitleChanged(hwnd);
}
void HostWindow::CatchWindow(HWND windowHandle, DWORD pId)
{
if(windowHandle == nullptr) exit(1);
WindowWraper* newTab = new WindowWraper(windowHandle);
tabs.push_back(newTab);
// get window Title
int titleLength = GetWindowTextLength(windowHandle) + 1; // including "\0"
wchar_t* title = new wchar_t[titleLength];
GetWindowText(windowHandle, title, titleLength);
QString titleQ = QString::fromWCharArray(title, titleLength);
delete[] title;
title = nullptr;
tabBar->addTab(titleQ);
tabBar->setCurrentIndex(tabs.length() - 1);
// TODO: hook window icon event
if(pId != 0 && !hooks.contains(pId)){
qDebug() << "try to hook title..";
HWINEVENTHOOK titlehook = SetWinEventHook(
EVENT_OBJECT_NAMECHANGE,
EVENT_OBJECT_NAMECHANGE,
nullptr,
HandleWinEventName,
pId,
0,
WINEVENT_OUTOFCONTEXT);
hooks.insert(pId, HookPair(titlehook));
}
}
BOOL CALLBACK EnumWindowsProcs(HWND hWnd,LPARAM lParam)
{
EnumArg* arg = reinterpret_cast<EnumArg*>(lParam);
LPCTSTR targetClassName = L"CabinetWClass\0";
LPTSTR className = new wchar_t[14];
GetClassName(hWnd, className, 14);
bool isValid =
GetParent(hWnd) == nullptr &&
IsWindowVisible(hWnd) &&
wcscmp(targetClassName, className) == 0;
if(isValid){
DWORD pId;
GetWindowThreadProcessId(hWnd, &pId);
if(arg->target.contains(pId)){
qDebug() << "Get you!" << pId << "/" << hWnd;
arg->callee->CatchWindow(hWnd, pId);
arg->finded = true;
return false; // stop enum
}
return true;
}
return true; // continue
}
bool HostWindow::AddExplorer(QString path)
{
// execute explorer
SHELLEXECUTEINFO SEI = {0};
SEI.cbSize = sizeof (SHELLEXECUTEINFO);
SEI.fMask = SEE_MASK_NOCLOSEPROCESS;
SEI.lpVerb = NULL;
SEI.lpFile = L"explorer.exe"; // Open an Explorer window at the 'Computer'
SEI.lpParameters = L","; // default path
if(path != ""){
path = path.right(path.length() - 8); // cut prefix file:///
path.replace("/", "\\");
path.push_front("/root,");
wchar_t* path_w = new wchar_t[path.length() + 1];
path.toWCharArray(path_w);
path_w[path.length()] = L'\0';
SEI.lpParameters = path_w;
}
SEI.lpDirectory = nullptr;
SEI.nShow = SW_MINIMIZE; // don't popup
SEI.hInstApp = nullptr;
if(ShellExecuteEx(&SEI)){
EnumArg* arg = new EnumArg();
arg->callee = this;
arg->finded = false;
PROCESSENTRY32 entry;
entry.dwSize = sizeof (PROCESSENTRY32);
timer = QDateTime::currentSecsSinceEpoch();
while(QDateTime::currentSecsSinceEpoch() - timer < 5){
arg->target.clear();
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if(Process32First(snapshot, &entry)){
while(Process32Next(snapshot, &entry)){
if(wcscmp(entry.szExeFile, L"explorer.exe") == 0){
arg->target.push_back(entry.th32ProcessID);
}
}
}
if(arg->target.length() == 0){
// shellExecute success
// but not found target porcess
return false;
}
qDebug() << "exec explorer" << arg->target;
// 5 seconds timeout
if(!EnumWindows(&EnumWindowsProcs, reinterpret_cast<LPARAM>(arg))){
delete arg;
arg = nullptr;
DWORD error = GetLastError();
if(error){
qDebug() << "enum window: result" << error;
break;
}
return true;
}
}
// not found
return false;
}
else{
qDebug() << GetLastError();
qDebug() << SEI.hInstApp;
return false;
}
}
void HostWindow::notifyTitleChanged(HWND changedWindow)
{
qDebug() << "get notified title changed " << changedWindow;
for (auto i = 0; i < tabs.length(); i++) {
if(tabs[i]->GetWindowHandle() == changedWindow){
int titleLength = GetWindowTextLength(changedWindow) + 1; // including "\0"
wchar_t* title = new wchar_t[titleLength];
GetWindowText(changedWindow, title, titleLength);
QString titleQ = QString::fromWCharArray(title, titleLength);
delete[] title;
title = nullptr;
tabBar->setTabText(i, titleQ);
}
}
}