mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:17:35 +00:00
Very hacky support for dragging a window around.
This commit is contained in:
parent
22721e6729
commit
64127e0637
7 changed files with 80 additions and 14 deletions
|
@ -54,6 +54,25 @@ void Painter::drawRect(const Rect& rect, Color color)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Painter::xorRect(const Rect& rect, Color color)
|
||||||
|
{
|
||||||
|
Rect r = rect;
|
||||||
|
r.moveBy(m_widget.x(), m_widget.y());
|
||||||
|
|
||||||
|
for (int y = r.top(); y < r.bottom(); ++y) {
|
||||||
|
dword* bits = scanline(y);
|
||||||
|
if (y == r.top() || y == (r.bottom() - 1)) {
|
||||||
|
for (int x = r.left(); x < r.right(); ++x) {
|
||||||
|
bits[x] ^= color.value();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bits[r.left()] ^= color.value();
|
||||||
|
bits[r.right() - 1] ^= color.value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, const Color& color)
|
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, const Color& color)
|
||||||
{
|
{
|
||||||
Point point;
|
Point point;
|
||||||
|
|
|
@ -17,6 +17,8 @@ public:
|
||||||
void drawRect(const Rect&, Color);
|
void drawRect(const Rect&, Color);
|
||||||
void drawText(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, const Color& = Color());
|
void drawText(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, const Color& = Color());
|
||||||
|
|
||||||
|
void xorRect(const Rect&, Color);
|
||||||
|
|
||||||
const Font& font() const;
|
const Font& font() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -12,6 +12,11 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isEmpty() const
|
||||||
|
{
|
||||||
|
return width() == 0 || height() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
void moveBy(int dx, int dy)
|
void moveBy(int dx, int dy)
|
||||||
{
|
{
|
||||||
m_location.moveBy(dx, dy);
|
m_location.moveBy(dx, dy);
|
||||||
|
|
|
@ -49,7 +49,7 @@ void Widget::event(Event& event)
|
||||||
|
|
||||||
void Widget::onPaint(PaintEvent& event)
|
void Widget::onPaint(PaintEvent& event)
|
||||||
{
|
{
|
||||||
printf("Widget::onPaint :)\n");
|
//printf("Widget::onPaint :)\n");
|
||||||
for (auto* ch : children()) {
|
for (auto* ch : children()) {
|
||||||
auto* child = (Widget*)ch;
|
auto* child = (Widget*)ch;
|
||||||
child->onPaint(event);
|
child->onPaint(event);
|
||||||
|
|
|
@ -22,6 +22,9 @@ public:
|
||||||
const Rect& rect() const { return m_rect; }
|
const Rect& rect() const { return m_rect; }
|
||||||
void setRect(const Rect&);
|
void setRect(const Rect&);
|
||||||
|
|
||||||
|
Point position() const { return m_rect.location(); }
|
||||||
|
void setPosition(const Point& position) { setRect({ position.x(), position.y(), width(), height() }); }
|
||||||
|
|
||||||
Widget* mainWidget() { return m_mainWidget; }
|
Widget* mainWidget() { return m_mainWidget; }
|
||||||
const Widget* mainWidget() const { return m_mainWidget; }
|
const Widget* mainWidget() const { return m_mainWidget; }
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ void WindowManager::paintWindowFrame(Window& window)
|
||||||
{
|
{
|
||||||
Painter p(*m_rootWidget);
|
Painter p(*m_rootWidget);
|
||||||
|
|
||||||
printf("WM: paintWindowFrame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
|
//printf("[WM] paintWindowFrame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
|
||||||
|
|
||||||
Rect topRect = titleBarRectForWindow(window);
|
Rect topRect = titleBarRectForWindow(window);
|
||||||
|
|
||||||
|
@ -78,6 +78,18 @@ void WindowManager::paintWindowFrame(Window& window)
|
||||||
windowFrameWidth + windowTitleBarHeight + window.rect().height() + 4
|
windowFrameWidth + windowTitleBarHeight + window.rect().height() + 4
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (!m_lastDragRect.isEmpty()) {
|
||||||
|
p.xorRect(m_lastDragRect, Color(255, 0, 0));
|
||||||
|
m_lastDragRect = Rect();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_dragWindow == &window) {
|
||||||
|
borderRect.inflate(2, 2);
|
||||||
|
p.xorRect(borderRect, Color(255, 0, 0));
|
||||||
|
m_lastDragRect = borderRect;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
p.drawRect(borderRect, Color(255, 255, 255));
|
p.drawRect(borderRect, Color(255, 255, 255));
|
||||||
borderRect.inflate(2, 2);
|
borderRect.inflate(2, 2);
|
||||||
p.drawRect(borderRect, m_windowBorderColor);
|
p.drawRect(borderRect, m_windowBorderColor);
|
||||||
|
@ -97,34 +109,54 @@ void WindowManager::addWindow(Window& window)
|
||||||
|
|
||||||
void WindowManager::notifyTitleChanged(Window& window)
|
void WindowManager::notifyTitleChanged(Window& window)
|
||||||
{
|
{
|
||||||
printf("[WM] ]Window{%p} title set to '%s'\n", &window, window.title().characters());
|
//printf("[WM] Window{%p} title set to '%s'\n", &window, window.title().characters());
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::notifyRectChanged(Window& window, const Rect& oldRect, const Rect& newRect)
|
void WindowManager::notifyRectChanged(Window& window, const Rect& oldRect, const Rect& newRect)
|
||||||
{
|
{
|
||||||
printf("[WM] Window %p rect changed (%d,%d %dx%d) -> (%d,%d %dx%d)\n",
|
//printf("[WM] Window %p rect changed (%d,%d %dx%d) -> (%d,%d %dx%d)\n", &window, oldRect.x(), oldRect.y(), oldRect.width(), oldRect.height(), newRect.x(), newRect.y(), newRect.width(), newRect.height());
|
||||||
&window,
|
|
||||||
oldRect.x(),
|
|
||||||
oldRect.y(),
|
|
||||||
oldRect.width(),
|
|
||||||
oldRect.height(),
|
|
||||||
newRect.x(),
|
|
||||||
newRect.y(),
|
|
||||||
newRect.width(),
|
|
||||||
newRect.height());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
|
void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
|
||||||
{
|
{
|
||||||
|
if (event.type() == Event::MouseDown) {
|
||||||
|
printf("[WM] Begin dragging Window{%p}\n", &window);
|
||||||
|
m_dragWindow = &window;
|
||||||
|
m_dragOrigin = event.position();
|
||||||
|
m_dragWindowOrigin = window.position();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#if 0
|
||||||
byte r = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
byte r = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
||||||
byte g = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
byte g = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
||||||
byte b = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
byte b = (((double)rand()) / (double)RAND_MAX) * 255.0;
|
||||||
m_windowBorderColor = Color(r, g, b);
|
m_windowBorderColor = Color(r, g, b);
|
||||||
paintWindowFrame(window);
|
paintWindowFrame(window);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::processMouseEvent(MouseEvent& event)
|
void WindowManager::processMouseEvent(MouseEvent& event)
|
||||||
{
|
{
|
||||||
|
if (event.type() == Event::MouseUp) {
|
||||||
|
if (m_dragWindow) {
|
||||||
|
printf("[WM] Finish dragging Window{%p}\n", m_dragWindow);
|
||||||
|
m_dragWindow = nullptr;
|
||||||
|
EventLoop::main().postEvent(this, make<PaintEvent>());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.type() == Event::MouseMove) {
|
||||||
|
if (m_dragWindow) {
|
||||||
|
Point pos = m_dragWindowOrigin;
|
||||||
|
printf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_dragOrigin.x(), m_dragOrigin.y(), event.x(), event.y());
|
||||||
|
pos.moveBy(event.x() - m_dragOrigin.x(), event.y() - m_dragOrigin.y());
|
||||||
|
m_dragWindow->setPosition(pos);
|
||||||
|
paintWindowFrame(*m_dragWindow);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: Respect z-order of windows...
|
// FIXME: Respect z-order of windows...
|
||||||
for (auto* window : m_windows) {
|
for (auto* window : m_windows) {
|
||||||
if (titleBarRectForWindow(*window).contains(event.position())) {
|
if (titleBarRectForWindow(*window).contains(event.position())) {
|
||||||
|
@ -150,7 +182,7 @@ void WindowManager::processMouseEvent(MouseEvent& event)
|
||||||
|
|
||||||
void WindowManager::handlePaintEvent(PaintEvent& event)
|
void WindowManager::handlePaintEvent(PaintEvent& event)
|
||||||
{
|
{
|
||||||
printf("[WM] paint event\n");
|
//printf("[WM] paint event\n");
|
||||||
m_rootWidget->event(event);
|
m_rootWidget->event(event);
|
||||||
paintWindowFrames();
|
paintWindowFrames();
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,4 +38,9 @@ private:
|
||||||
void paintWindowFrame(Window&);
|
void paintWindowFrame(Window&);
|
||||||
HashTable<Window*> m_windows;
|
HashTable<Window*> m_windows;
|
||||||
Widget* m_rootWidget { nullptr };
|
Widget* m_rootWidget { nullptr };
|
||||||
|
|
||||||
|
Window* m_dragWindow { nullptr };
|
||||||
|
Point m_dragOrigin;
|
||||||
|
Point m_dragWindowOrigin;
|
||||||
|
Rect m_lastDragRect;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue