| 1 |
namespace phoenix { |
| 2 |
|
| 3 |
void pWindow::append(Layout& layout) { |
| 4 |
Geometry geometry = window.state.geometry; |
| 5 |
geometry.x = geometry.y = 0; |
| 6 |
layout.setGeometry(geometry); |
| 7 |
} |
| 8 |
|
| 9 |
void pWindow::append(Menu& menu) { |
| 10 |
if(window.state.menuFont != "") menu.p.setFont(window.state.menuFont); |
| 11 |
else menu.p.setFont(Font::sans(8)); |
| 12 |
qtMenu->addMenu(menu.p.qtMenu); |
| 13 |
} |
| 14 |
|
| 15 |
void pWindow::append(Widget& widget) { |
| 16 |
if(widget.font().empty() && !window.state.widgetFont.empty()) { |
| 17 |
widget.setFont(window.state.widgetFont); |
| 18 |
} |
| 19 |
if(widget.font().empty()) widget.p.setFont(Font::sans(8)); |
| 20 |
if(GetParentWidget(&widget)) { |
| 21 |
widget.p.qtWidget->setParent(GetParentWidget(&widget)->p.container(widget)); |
| 22 |
} else { |
| 23 |
widget.p.qtWidget->setParent(qtContainer); |
| 24 |
} |
| 25 |
widget.setVisible(widget.visible()); |
| 26 |
} |
| 27 |
|
| 28 |
Geometry pWindow::frameMargin() { |
| 29 |
unsigned menuHeight = window.state.menuVisible ? settings->geometry.menuHeight : 0; |
| 30 |
unsigned statusHeight = window.state.statusVisible ? settings->geometry.statusHeight : 0; |
| 31 |
if(window.state.fullScreen) return {0, menuHeight, 0, menuHeight + statusHeight}; |
| 32 |
return { |
| 33 |
settings->geometry.frameX, |
| 34 |
settings->geometry.frameY + menuHeight, |
| 35 |
settings->geometry.frameWidth, |
| 36 |
settings->geometry.frameHeight + menuHeight + statusHeight |
| 37 |
}; |
| 38 |
} |
| 39 |
|
| 40 |
bool pWindow::focused() { |
| 41 |
return qtWindow->isActiveWindow() && !qtWindow->isMinimized(); |
| 42 |
} |
| 43 |
|
| 44 |
Geometry pWindow::geometry() { |
| 45 |
if(window.state.fullScreen) { |
| 46 |
unsigned menuHeight = window.state.menuVisible ? qtMenu->height() : 0; |
| 47 |
unsigned statusHeight = window.state.statusVisible ? qtStatus->height() : 0; |
| 48 |
QRect geometry = qtWindow->geometry(); //frameGeometry() includes frame even though it's not visible in fullscreen mode |
| 49 |
return {geometry.x(), geometry.y() + menuHeight, geometry.width(), geometry.height() - menuHeight - statusHeight}; |
| 50 |
} |
| 51 |
return window.state.geometry; |
| 52 |
} |
| 53 |
|
| 54 |
void pWindow::remove(Layout& layout) { |
| 55 |
} |
| 56 |
|
| 57 |
void pWindow::remove(Menu& menu) { |
| 58 |
//QMenuBar::removeMenu() does not exist |
| 59 |
qtMenu->clear(); |
| 60 |
for(auto& menu : window.state.menu) append(menu); |
| 61 |
} |
| 62 |
|
| 63 |
void pWindow::remove(Widget& widget) { |
| 64 |
//orphan() destroys and recreates widgets (to disassociate them from their parent); |
| 65 |
//attempting to create widget again after QApplication::quit() crashes libQtGui |
| 66 |
if(qtApplication) widget.p.orphan(); |
| 67 |
} |
| 68 |
|
| 69 |
void pWindow::setBackgroundColor(Color color) { |
| 70 |
QPalette palette; |
| 71 |
palette.setColor(QPalette::Background, QColor(color.red, color.green, color.blue /*, color.alpha */)); |
| 72 |
qtContainer->setPalette(palette); |
| 73 |
qtContainer->setAutoFillBackground(true); |
| 74 |
//translucency results are very unpleasant without a compositor; so disable for now |
| 75 |
//qtWindow->setAttribute(Qt::WA_TranslucentBackground, color.alpha != 255); |
| 76 |
} |
| 77 |
|
| 78 |
void pWindow::setDroppable(bool droppable) { |
| 79 |
qtWindow->setAcceptDrops(droppable); |
| 80 |
} |
| 81 |
|
| 82 |
void pWindow::setFocused() { |
| 83 |
qtWindow->raise(); |
| 84 |
qtWindow->activateWindow(); |
| 85 |
} |
| 86 |
|
| 87 |
void pWindow::setFullScreen(bool fullScreen) { |
| 88 |
if(fullScreen == false) { |
| 89 |
setResizable(window.state.resizable); |
| 90 |
qtWindow->showNormal(); |
| 91 |
qtWindow->adjustSize(); |
| 92 |
} else { |
| 93 |
qtLayout->setSizeConstraint(QLayout::SetDefaultConstraint); |
| 94 |
qtContainer->setFixedSize(Desktop::size().width - frameMargin().width, Desktop::size().height - frameMargin().height); |
| 95 |
qtWindow->showFullScreen(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void pWindow::setGeometry(Geometry geometry) { |
| 100 |
locked = true; |
| 101 |
Application::processEvents(); |
| 102 |
QApplication::syncX(); |
| 103 |
Geometry margin = frameMargin(); |
| 104 |
|
| 105 |
setResizable(window.state.resizable); |
| 106 |
qtWindow->move(geometry.x - frameMargin().x, geometry.y - frameMargin().y); |
| 107 |
//qtWindow->adjustSize() fails if larger than 2/3rds screen size |
| 108 |
qtWindow->resize(qtWindow->sizeHint()); |
| 109 |
if(window.state.resizable) { |
| 110 |
//required to allow shrinking window from default size |
| 111 |
qtWindow->setMinimumSize(1, 1); |
| 112 |
qtContainer->setMinimumSize(1, 1); |
| 113 |
} |
| 114 |
|
| 115 |
for(auto& layout : window.state.layout) { |
| 116 |
geometry.x = geometry.y = 0; |
| 117 |
layout.setGeometry(geometry); |
| 118 |
} |
| 119 |
locked = false; |
| 120 |
} |
| 121 |
|
| 122 |
void pWindow::setMenuFont(string font) { |
| 123 |
qtMenu->setFont(pFont::create(font)); |
| 124 |
for(auto& item : window.state.menu) item.p.setFont(font); |
| 125 |
} |
| 126 |
|
| 127 |
void pWindow::setMenuVisible(bool visible) { |
| 128 |
qtMenu->setVisible(visible); |
| 129 |
setGeometry(window.state.geometry); |
| 130 |
} |
| 131 |
|
| 132 |
void pWindow::setModal(bool modal) { |
| 133 |
if(modal == true) { |
| 134 |
//windowModality can only be enabled while window is invisible |
| 135 |
setVisible(false); |
| 136 |
qtWindow->setWindowModality(Qt::ApplicationModal); |
| 137 |
setVisible(true); |
| 138 |
while(window.state.modal) { |
| 139 |
Application::processEvents(); |
| 140 |
usleep(20 * 1000); |
| 141 |
} |
| 142 |
qtWindow->setWindowModality(Qt::NonModal); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
void pWindow::setResizable(bool resizable) { |
| 147 |
if(resizable) { |
| 148 |
qtLayout->setSizeConstraint(QLayout::SetDefaultConstraint); |
| 149 |
qtContainer->setMinimumSize(window.state.geometry.width, window.state.geometry.height); |
| 150 |
} else { |
| 151 |
qtLayout->setSizeConstraint(QLayout::SetFixedSize); |
| 152 |
qtContainer->setFixedSize(window.state.geometry.width, window.state.geometry.height); |
| 153 |
} |
| 154 |
qtStatus->setSizeGripEnabled(resizable); |
| 155 |
} |
| 156 |
|
| 157 |
void pWindow::setStatusFont(string font) { |
| 158 |
qtStatus->setFont(pFont::create(font)); |
| 159 |
} |
| 160 |
|
| 161 |
void pWindow::setStatusText(string text) { |
| 162 |
qtStatus->showMessage(QString::fromUtf8(text), 0); |
| 163 |
} |
| 164 |
|
| 165 |
void pWindow::setStatusVisible(bool visible) { |
| 166 |
qtStatus->setVisible(visible); |
| 167 |
setGeometry(window.state.geometry); |
| 168 |
} |
| 169 |
|
| 170 |
void pWindow::setTitle(string text) { |
| 171 |
qtWindow->setWindowTitle(QString::fromUtf8(text)); |
| 172 |
} |
| 173 |
|
| 174 |
void pWindow::setVisible(bool visible) { |
| 175 |
locked = true; |
| 176 |
qtWindow->setVisible(visible); |
| 177 |
if(visible) { |
| 178 |
updateFrameGeometry(); |
| 179 |
setGeometry(window.state.geometry); |
| 180 |
} |
| 181 |
locked = false; |
| 182 |
} |
| 183 |
|
| 184 |
void pWindow::setWidgetFont(string font) { |
| 185 |
} |
| 186 |
|
| 187 |
void pWindow::constructor() { |
| 188 |
qtWindow = new QtWindow(*this); |
| 189 |
qtWindow->setWindowTitle(" "); |
| 190 |
|
| 191 |
//if program was given a name, try and set the window taskbar icon to a matching pixmap image |
| 192 |
if(applicationState.name.empty() == false) { |
| 193 |
if(file::exists({"/usr/share/pixmaps/", applicationState.name, ".png"})) { |
| 194 |
qtWindow->setWindowIcon(QIcon(string{"/usr/share/pixmaps/", applicationState.name, ".png"})); |
| 195 |
} else if(file::exists({"/usr/local/share/pixmaps/", applicationState.name, ".png"})) { |
| 196 |
qtWindow->setWindowIcon(QIcon(string{"/usr/local/share/pixmaps/", applicationState.name, ".png"})); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
qtLayout = new QVBoxLayout(qtWindow); |
| 201 |
qtLayout->setMargin(0); |
| 202 |
qtLayout->setSpacing(0); |
| 203 |
qtWindow->setLayout(qtLayout); |
| 204 |
|
| 205 |
qtMenu = new QMenuBar(qtWindow); |
| 206 |
qtMenu->setVisible(false); |
| 207 |
qtLayout->addWidget(qtMenu); |
| 208 |
|
| 209 |
qtContainer = new QWidget(qtWindow); |
| 210 |
qtContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 211 |
qtContainer->setVisible(true); |
| 212 |
qtLayout->addWidget(qtContainer); |
| 213 |
|
| 214 |
qtStatus = new QStatusBar(qtWindow); |
| 215 |
qtStatus->setSizeGripEnabled(true); |
| 216 |
qtStatus->setVisible(false); |
| 217 |
qtLayout->addWidget(qtStatus); |
| 218 |
|
| 219 |
setGeometry(window.state.geometry); |
| 220 |
setMenuFont(Font::sans(8)); |
| 221 |
setStatusFont(Font::sans(8)); |
| 222 |
|
| 223 |
QColor color = qtWindow->palette().color(QPalette::ColorRole::Window); |
| 224 |
window.state.backgroundColor = Color((uint8_t)color.red(), (uint8_t)color.green(), (uint8_t)color.blue(), (uint8_t)color.alpha()); |
| 225 |
} |
| 226 |
|
| 227 |
void pWindow::destructor() { |
| 228 |
delete qtStatus; |
| 229 |
delete qtContainer; |
| 230 |
delete qtMenu; |
| 231 |
delete qtLayout; |
| 232 |
delete qtWindow; |
| 233 |
} |
| 234 |
|
| 235 |
void pWindow::updateFrameGeometry() { |
| 236 |
pApplication::syncX(); |
| 237 |
QRect border = qtWindow->frameGeometry(); |
| 238 |
QRect client = qtWindow->geometry(); |
| 239 |
|
| 240 |
settings->geometry.frameX = client.x() - border.x(); |
| 241 |
settings->geometry.frameY = client.y() - border.y(); |
| 242 |
settings->geometry.frameWidth = border.width() - client.width(); |
| 243 |
settings->geometry.frameHeight = border.height() - client.height(); |
| 244 |
|
| 245 |
if(window.state.menuVisible) { |
| 246 |
pApplication::syncX(); |
| 247 |
settings->geometry.menuHeight = qtMenu->height(); |
| 248 |
} |
| 249 |
|
| 250 |
if(window.state.statusVisible) { |
| 251 |
pApplication::syncX(); |
| 252 |
settings->geometry.statusHeight = qtStatus->height(); |
| 253 |
} |
| 254 |
|
| 255 |
settings->save(); |
| 256 |
} |
| 257 |
|
| 258 |
void pWindow::QtWindow::closeEvent(QCloseEvent* event) { |
| 259 |
event->ignore(); |
| 260 |
if(self.window.onClose) self.window.onClose(); |
| 261 |
else self.window.setVisible(false); |
| 262 |
if(self.window.state.modal && !self.window.visible()) self.window.setModal(false); |
| 263 |
} |
| 264 |
|
| 265 |
void pWindow::QtWindow::moveEvent(QMoveEvent* event) { |
| 266 |
if(self.locked == false && self.window.state.fullScreen == false && self.qtWindow->isVisible() == true) { |
| 267 |
self.window.state.geometry.x += event->pos().x() - event->oldPos().x(); |
| 268 |
self.window.state.geometry.y += event->pos().y() - event->oldPos().y(); |
| 269 |
} |
| 270 |
|
| 271 |
if(self.locked == false) { |
| 272 |
if(self.window.onMove) self.window.onMove(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
void pWindow::QtWindow::dragEnterEvent(QDragEnterEvent* event) { |
| 277 |
if(event->mimeData()->hasUrls()) { |
| 278 |
event->acceptProposedAction(); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
void pWindow::QtWindow::dropEvent(QDropEvent* event) { |
| 283 |
lstring paths = DropPaths(event); |
| 284 |
if(paths.empty()) return; |
| 285 |
if(self.window.onDrop) self.window.onDrop(paths); |
| 286 |
} |
| 287 |
|
| 288 |
void pWindow::QtWindow::keyPressEvent(QKeyEvent* event) { |
| 289 |
Keyboard::Keycode sym = Keysym(event->nativeVirtualKey()); |
| 290 |
if(sym != Keyboard::Keycode::None && self.window.onKeyPress) self.window.onKeyPress(sym); |
| 291 |
} |
| 292 |
|
| 293 |
void pWindow::QtWindow::keyReleaseEvent(QKeyEvent* event) { |
| 294 |
Keyboard::Keycode sym = Keysym(event->nativeVirtualKey()); |
| 295 |
if(sym != Keyboard::Keycode::None && self.window.onKeyRelease) self.window.onKeyRelease(sym); |
| 296 |
} |
| 297 |
|
| 298 |
void pWindow::QtWindow::resizeEvent(QResizeEvent*) { |
| 299 |
if(self.locked == false && self.window.state.fullScreen == false && self.qtWindow->isVisible() == true) { |
| 300 |
self.window.state.geometry.width = self.qtContainer->geometry().width(); |
| 301 |
self.window.state.geometry.height = self.qtContainer->geometry().height(); |
| 302 |
} |
| 303 |
|
| 304 |
for(auto& layout : self.window.state.layout) { |
| 305 |
Geometry geometry = self.geometry(); |
| 306 |
geometry.x = geometry.y = 0; |
| 307 |
layout.setGeometry(geometry); |
| 308 |
} |
| 309 |
|
| 310 |
if(self.locked == false) { |
| 311 |
if(self.window.onSize) self.window.onSize(); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
QSize pWindow::QtWindow::sizeHint() const { |
| 316 |
unsigned width = self.window.state.geometry.width; |
| 317 |
unsigned height = self.window.state.geometry.height; |
| 318 |
if(self.window.state.menuVisible) height += settings->geometry.menuHeight; |
| 319 |
if(self.window.state.statusVisible) height += settings->geometry.statusHeight; |
| 320 |
return QSize(width, height); |
| 321 |
} |
| 322 |
|
| 323 |
} |