Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions choc/gui/choc_DesktopWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ struct DesktopWindow

/// An optional callback that will be called when the parent window is resized
std::function<void()> windowResized;
/// An optional callback that will be called when the parent window is moved
std::function<void()> windowMoved;
/// An optional callback that will be called when the parent window is closed
std::function<void()> windowClosed;

Expand Down Expand Up @@ -170,6 +172,25 @@ struct choc::ui::DesktopWindow::Pimpl
static_cast<Pimpl*> (arg)->windowDestroyEvent();
}),
this);

configureHandlerID = g_signal_connect (G_OBJECT (window), "configure-event",
G_CALLBACK (+[](GtkWidget*, GdkEventConfigure* event, gpointer arg) -> gboolean
{
auto* p = static_cast<Pimpl*> (arg);

if (event->x != p->lastX || event->y != p->lastY)
{
p->lastX = event->x;
p->lastY = event->y;

if (p->owner.windowMoved)
p->owner.windowMoved();
}

return FALSE;
}),
this);

setBounds (bounds);
setVisible (true);
}
Expand Down Expand Up @@ -202,6 +223,9 @@ struct choc::ui::DesktopWindow::Pimpl

~Pimpl()
{
if (configureHandlerID != 0 && window != nullptr)
g_signal_handler_disconnect (G_OBJECT (window), configureHandlerID);

if (destroyHandlerID != 0 && window != nullptr)
g_signal_handler_disconnect (G_OBJECT (window), destroyHandlerID);

Expand All @@ -210,10 +234,10 @@ struct choc::ui::DesktopWindow::Pimpl

void windowDestroyEvent()
{
g_clear_object (&window);

if (owner.windowClosed != nullptr)
owner.windowClosed();

g_clear_object (&window);
}

void* getWindowHandle() const { return (void*) window; }
Expand Down Expand Up @@ -355,6 +379,8 @@ struct choc::ui::DesktopWindow::Pimpl
GtkWidget* window = {};
GtkWidget* content = {};
unsigned long destroyHandlerID = 0;
unsigned long configureHandlerID = 0;
int lastX = 0, lastY = 0;
FileDropCallback fileDropCallback;
};

Expand Down Expand Up @@ -615,11 +641,12 @@ struct DesktopWindow::Pimpl
{
CHOC_AUTORELEASE_BEGIN
auto& p = getPimplFromContext (self);
p.window = {};

if (auto callback = p.owner.windowClosed)
choc::messageloop::postMessage ([callback] { callback(); });

p.window = {};

CHOC_AUTORELEASE_END
return TRUE;
}),
Expand All @@ -637,6 +664,18 @@ struct DesktopWindow::Pimpl
}),
"v@:@");

class_addMethod (delegateClass, sel_registerName ("windowDidMove:"),
(IMP) (+[](id self, SEL, id)
{
CHOC_AUTORELEASE_BEGIN

if (auto callback = getPimplFromContext (self).owner.windowMoved)
callback();

CHOC_AUTORELEASE_END
}),
"v@:@");

class_addMethod (delegateClass, sel_registerName ("applicationShouldTerminateAfterLastWindowClosed:"),
(IMP) (+[](id, SEL, id) -> BOOL { return 0; }),
"c@:@");
Expand Down Expand Up @@ -1090,6 +1129,12 @@ struct DesktopWindow::Pimpl
owner.windowResized();
}

void handleMove()
{
if (owner.windowMoved != nullptr)
owner.windowMoved();
}

bool handleFileDrop (HANDLE hdrop)
{
typedef UINT (WINAPI *DragQueryFileWFunc)(HANDLE, UINT, LPWSTR, UINT);
Expand Down Expand Up @@ -1151,6 +1196,7 @@ struct DesktopWindow::Pimpl
{
case WM_NCCREATE: enableNonClientDPIScaling (h); break;
case WM_SIZE: if (auto w = getPimpl (h)) w->handleSizeChange(); break;
case WM_MOVE: if (auto w = getPimpl (h)) w->handleMove(); break;
case WM_CLOSE: if (auto w = getPimpl (h)) w->handleClose(); return 0;
case WM_GETMINMAXINFO: if (auto w = getPimpl (h)) w->getMinMaxInfo (*(LPMINMAXINFO) lp); return 0;
case WM_DROPFILES: if (auto w = getPimpl (h)) if (w->handleFileDrop ((HANDLE) wp)) return 0; break;
Expand Down