2015-06-11 18:07:06 -05:00
|
|
|
// 11 june 2015
|
|
|
|
#include "uipriv_unix.h"
|
|
|
|
|
2015-08-28 08:56:41 -05:00
|
|
|
struct uiWindow {
|
|
|
|
uiUnixControl c;
|
2015-06-13 11:59:13 -05:00
|
|
|
|
2015-06-11 18:07:06 -05:00
|
|
|
GtkWidget *widget;
|
2015-06-13 11:59:13 -05:00
|
|
|
GtkContainer *container;
|
|
|
|
GtkWindow *window;
|
|
|
|
|
|
|
|
GtkWidget *vboxWidget;
|
|
|
|
GtkContainer *vboxContainer;
|
|
|
|
GtkBox *vbox;
|
|
|
|
|
2016-06-15 23:45:23 -05:00
|
|
|
GtkWidget *childHolderWidget;
|
|
|
|
GtkContainer *childHolderContainer;
|
|
|
|
|
2015-06-13 11:59:13 -05:00
|
|
|
GtkWidget *menubar;
|
|
|
|
|
2016-06-15 23:45:23 -05:00
|
|
|
uiControl *child;
|
2015-08-28 09:30:42 -05:00
|
|
|
int margined;
|
2015-06-29 10:48:27 -05:00
|
|
|
|
2015-06-11 18:07:06 -05:00
|
|
|
int (*onClosing)(uiWindow *, void *);
|
|
|
|
void *onClosingData;
|
2016-06-15 15:45:49 -05:00
|
|
|
void (*onPositionChanged)(uiWindow *, void *);
|
|
|
|
void *onPositionChangedData;
|
2016-06-15 19:45:10 -05:00
|
|
|
gboolean changingPosition;
|
2016-06-15 23:45:23 -05:00
|
|
|
void (*onContentSizeChanged)(uiWindow *, void *);
|
|
|
|
void *onContentSizeChangedData;
|
|
|
|
gboolean changingSize;
|
2016-06-16 09:03:35 -05:00
|
|
|
gboolean fullscreen;
|
2015-06-11 18:07:06 -05:00
|
|
|
};
|
|
|
|
|
2015-06-13 11:59:13 -05:00
|
|
|
static gboolean onClosing(GtkWidget *win, GdkEvent *e, gpointer data)
|
|
|
|
{
|
2015-08-28 08:56:41 -05:00
|
|
|
uiWindow *w = uiWindow(data);
|
2015-06-13 11:59:13 -05:00
|
|
|
|
|
|
|
// manually destroy the window ourselves; don't let the delete-event handler do it
|
2015-08-28 08:56:41 -05:00
|
|
|
if ((*(w->onClosing))(w, w->onClosingData))
|
2015-06-13 11:59:13 -05:00
|
|
|
uiControlDestroy(uiControl(w));
|
|
|
|
// don't continue to the default delete-event handler; we destroyed the window by now
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-06-15 19:45:10 -05:00
|
|
|
static gboolean onConfigure(GtkWidget *win, GdkEvent *e, gpointer data)
|
|
|
|
{
|
|
|
|
uiWindow *w = uiWindow(data);
|
|
|
|
|
|
|
|
// there doesn't seem to be a way to determine if only moving or only resizing is happening :/
|
|
|
|
if (w->changingPosition)
|
|
|
|
w->changingPosition = FALSE;
|
|
|
|
else
|
|
|
|
(*(w->onPositionChanged))(w, w->onPositionChangedData);
|
|
|
|
// always continue handling
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:45:23 -05:00
|
|
|
static void onSizeAllocate(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
|
|
|
|
{
|
|
|
|
uiWindow *w = uiWindow(data);
|
|
|
|
|
|
|
|
if (w->changingSize)
|
|
|
|
w->changingSize = FALSE;
|
|
|
|
else
|
|
|
|
(*(w->onContentSizeChanged))(w, w->onContentSizeChangedData);
|
|
|
|
}
|
|
|
|
|
2015-06-13 11:59:13 -05:00
|
|
|
static int defaultOnClosing(uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:45:23 -05:00
|
|
|
static void defaultOnPositionContentSizeChanged(uiWindow *w, void *data)
|
2016-06-15 15:45:49 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:15:02 -05:00
|
|
|
static void uiWindowDestroy(uiControl *c)
|
2015-06-13 11:59:13 -05:00
|
|
|
{
|
2016-04-25 19:15:02 -05:00
|
|
|
uiWindow *w = uiWindow(c);
|
|
|
|
|
2015-06-13 11:59:13 -05:00
|
|
|
// first hide ourselves
|
|
|
|
gtk_widget_hide(w->widget);
|
2015-06-30 00:16:35 -05:00
|
|
|
// now destroy the child
|
2016-06-15 23:45:23 -05:00
|
|
|
if (w->child != NULL) {
|
|
|
|
uiControlSetParent(w->child, NULL);
|
|
|
|
uiUnixControlSetContainer(uiUnixControl(w->child), w->childHolderContainer, TRUE);
|
|
|
|
uiControlDestroy(w->child);
|
|
|
|
}
|
2015-06-13 11:59:13 -05:00
|
|
|
// now destroy the menus, if any
|
|
|
|
if (w->menubar != NULL)
|
|
|
|
freeMenubar(w->menubar);
|
2016-06-15 23:45:23 -05:00
|
|
|
gtk_widget_destroy(w->childHolderWidget);
|
2015-08-28 08:56:41 -05:00
|
|
|
gtk_widget_destroy(w->vboxWidget);
|
2016-04-25 19:15:02 -05:00
|
|
|
// and finally free ourselves
|
|
|
|
g_object_unref(w->widget);
|
|
|
|
uiFreeControl(uiControl(w));
|
2015-06-11 18:07:06 -05:00
|
|
|
}
|
|
|
|
|
2016-04-25 19:15:02 -05:00
|
|
|
uiUnixControlDefaultHandle(uiWindow)
|
2016-05-22 19:02:47 -05:00
|
|
|
|
|
|
|
uiControl *uiWindowParent(uiControl *c)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowSetParent(uiControl *c, uiControl *parent)
|
|
|
|
{
|
|
|
|
uiUserBugCannotSetParentOnToplevel("uiWindow");
|
|
|
|
}
|
2016-04-25 19:15:02 -05:00
|
|
|
|
|
|
|
static int uiWindowToplevel(uiControl *c)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiUnixControlDefaultVisible(uiWindow)
|
|
|
|
|
|
|
|
static void uiWindowShow(uiControl *c)
|
2015-06-13 11:59:13 -05:00
|
|
|
{
|
2015-08-28 08:56:41 -05:00
|
|
|
uiWindow *w = uiWindow(c);
|
2015-06-13 11:59:13 -05:00
|
|
|
|
|
|
|
// don't use gtk_widget_show_all() as that will show all children, regardless of user settings
|
|
|
|
// don't use gtk_widget_show(); that doesn't bring to front or give keyboard focus
|
|
|
|
// (gtk_window_present() does call gtk_widget_show() though)
|
|
|
|
gtk_window_present(w->window);
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:15:02 -05:00
|
|
|
uiUnixControlDefaultHide(uiWindow)
|
|
|
|
uiUnixControlDefaultEnabled(uiWindow)
|
|
|
|
uiUnixControlDefaultEnable(uiWindow)
|
|
|
|
uiUnixControlDefaultDisable(uiWindow)
|
|
|
|
// TODO?
|
|
|
|
uiUnixControlDefaultSetContainer(uiWindow)
|
2015-06-11 18:07:06 -05:00
|
|
|
|
2015-08-28 08:56:41 -05:00
|
|
|
char *uiWindowTitle(uiWindow *w)
|
2015-06-11 18:07:06 -05:00
|
|
|
{
|
2015-06-13 11:59:13 -05:00
|
|
|
return uiUnixStrdupText(gtk_window_get_title(w->window));
|
2015-06-11 18:07:06 -05:00
|
|
|
}
|
|
|
|
|
2015-08-28 08:56:41 -05:00
|
|
|
void uiWindowSetTitle(uiWindow *w, const char *title)
|
2015-06-11 18:07:06 -05:00
|
|
|
{
|
2015-06-13 11:59:13 -05:00
|
|
|
gtk_window_set_title(w->window, title);
|
2015-06-11 18:07:06 -05:00
|
|
|
}
|
|
|
|
|
2016-06-15 15:45:49 -05:00
|
|
|
// TODO allow specifying either as NULL on all platforms
|
|
|
|
void uiWindowPosition(uiWindow *w, int *x, int *y)
|
|
|
|
{
|
|
|
|
gint rx, ry;
|
|
|
|
|
|
|
|
gtk_window_get_position(w->window, &rx, &ry);
|
|
|
|
*x = rx;
|
|
|
|
*y = ry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowSetPosition(uiWindow *w, int x, int y)
|
|
|
|
{
|
2016-06-15 19:45:10 -05:00
|
|
|
w->changingPosition = TRUE;
|
2016-06-15 15:45:49 -05:00
|
|
|
gtk_window_move(w->window, x, y);
|
2016-06-15 19:45:10 -05:00
|
|
|
// gtk_window_move() is asynchronous
|
|
|
|
// we need to wait for a configure-event
|
|
|
|
// thanks to hergertme in irc.gimp.net/#gtk+
|
|
|
|
while (w->changingPosition)
|
2016-06-17 08:16:30 -05:00
|
|
|
if (!uiMainStep(1))
|
|
|
|
break; // stop early if uiQuit() called
|
2016-06-15 15:45:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowCenter(uiWindow *w)
|
|
|
|
{
|
|
|
|
gint x, y;
|
|
|
|
GtkAllocation winalloc;
|
|
|
|
GdkWindow *gdkwin;
|
|
|
|
GdkScreen *screen;
|
|
|
|
GdkRectangle workarea;
|
|
|
|
|
|
|
|
gtk_widget_get_allocation(w->widget, &winalloc);
|
|
|
|
gdkwin = gtk_widget_get_window(w->widget);
|
|
|
|
screen = gdk_window_get_screen(gdkwin);
|
|
|
|
gdk_screen_get_monitor_workarea(screen,
|
|
|
|
gdk_screen_get_monitor_at_window(screen, gdkwin),
|
|
|
|
&workarea);
|
|
|
|
|
|
|
|
x = (workarea.width - winalloc.width) / 2;
|
|
|
|
y = (workarea.height - winalloc.height) / 2;
|
2016-06-15 19:45:10 -05:00
|
|
|
// TODO move up slightly? see what Mutter or GNOME Shell or GNOME Terminal do(es)?
|
|
|
|
uiWindowSetPosition(w, x, y);
|
2016-06-15 15:45:49 -05:00
|
|
|
}
|
|
|
|
|
2016-06-17 08:16:30 -05:00
|
|
|
// TODO this and size changed get set during uiWindowDestroy
|
2016-06-15 15:45:49 -05:00
|
|
|
void uiWindowOnPositionChanged(uiWindow *w, void (*f)(uiWindow *, void *), void *data)
|
|
|
|
{
|
|
|
|
w->onPositionChanged = f;
|
|
|
|
w->onPositionChangedData = data;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:45:23 -05:00
|
|
|
void uiWindowContentSize(uiWindow *w, int *width, int *height)
|
|
|
|
{
|
|
|
|
GtkAllocation allocation;
|
|
|
|
|
|
|
|
gtk_widget_get_allocation(w->childHolderWidget, &allocation);
|
|
|
|
*width = allocation.width;
|
|
|
|
*height = allocation.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO what happens if the size is already the current one?
|
|
|
|
// TODO a spurious size-allocate gets sent after this function returns
|
2016-06-17 08:16:30 -05:00
|
|
|
// TODO can't reduce the size this way
|
2016-06-15 23:45:23 -05:00
|
|
|
void uiWindowSetContentSize(uiWindow *w, int width, int height)
|
|
|
|
{
|
|
|
|
w->changingSize = TRUE;
|
|
|
|
gtk_widget_set_size_request(w->childHolderWidget, width, height);
|
|
|
|
while (w->changingSize)
|
2016-06-17 08:16:30 -05:00
|
|
|
if (!uiMainStep(1))
|
|
|
|
break; // stop early if uiQuit() called
|
2016-06-15 23:45:23 -05:00
|
|
|
gtk_widget_set_size_request(w->childHolderWidget, -1, -1);
|
|
|
|
}
|
|
|
|
|
2016-06-16 09:03:35 -05:00
|
|
|
int uiWindowFullscreen(uiWindow *w)
|
|
|
|
{
|
|
|
|
return w->fullscreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO use window-state-event to track
|
|
|
|
// TODO does this send an extra size changed?
|
|
|
|
void uiWindowSetFullscreen(uiWindow *w, int fullscreen)
|
|
|
|
{
|
|
|
|
w->fullscreen = fullscreen;
|
|
|
|
if (w->fullscreen)
|
|
|
|
gtk_window_fullscreen(w->window);
|
|
|
|
else
|
|
|
|
gtk_window_unfullscreen(w->window);
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:45:23 -05:00
|
|
|
void uiWindowOnContentSizeChanged(uiWindow *w, void (*f)(uiWindow *, void *), void *data)
|
|
|
|
{
|
|
|
|
w->onContentSizeChanged = f;
|
|
|
|
w->onContentSizeChangedData = data;
|
|
|
|
}
|
|
|
|
|
2015-08-28 08:56:41 -05:00
|
|
|
void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
|
2015-06-11 18:07:06 -05:00
|
|
|
{
|
|
|
|
w->onClosing = f;
|
|
|
|
w->onClosingData = data;
|
|
|
|
}
|
|
|
|
|
2016-06-16 10:34:19 -05:00
|
|
|
int uiWindowBorderless(uiWindow *w)
|
|
|
|
{
|
|
|
|
return gtk_window_get_decorated(w->window) == FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowSetBorderless(uiWindow *w, int borderless)
|
|
|
|
{
|
|
|
|
gtk_window_set_decorated(w->window, borderless == 0);
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:45:23 -05:00
|
|
|
// TODO save and restore expands and aligns
|
2015-08-28 08:56:41 -05:00
|
|
|
void uiWindowSetChild(uiWindow *w, uiControl *child)
|
2015-06-11 18:07:06 -05:00
|
|
|
{
|
2015-08-28 08:56:41 -05:00
|
|
|
if (w->child != NULL) {
|
2016-06-15 23:45:23 -05:00
|
|
|
uiControlSetParent(w->child, NULL);
|
|
|
|
uiUnixControlSetContainer(uiUnixControl(w->child), w->childHolderContainer, TRUE);
|
|
|
|
}
|
|
|
|
w->child = child;
|
|
|
|
if (w->child != NULL) {
|
|
|
|
uiControlSetParent(w->child, uiControl(w));
|
|
|
|
uiUnixControlSetContainer(uiUnixControl(w->child), w->childHolderContainer, FALSE);
|
2015-08-28 08:56:41 -05:00
|
|
|
}
|
2015-06-11 18:07:06 -05:00
|
|
|
}
|
|
|
|
|
2015-08-28 08:56:41 -05:00
|
|
|
int uiWindowMargined(uiWindow *w)
|
2015-06-11 18:07:06 -05:00
|
|
|
{
|
2015-08-28 08:56:41 -05:00
|
|
|
return w->margined;
|
2015-06-11 18:07:06 -05:00
|
|
|
}
|
|
|
|
|
2015-08-28 08:56:41 -05:00
|
|
|
void uiWindowSetMargined(uiWindow *w, int margined)
|
2015-06-11 18:07:06 -05:00
|
|
|
{
|
2015-08-28 08:56:41 -05:00
|
|
|
w->margined = margined;
|
2016-06-15 23:45:23 -05:00
|
|
|
setMargined(w->childHolderContainer, w->margined);
|
2015-06-11 18:07:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
|
|
|
|
{
|
2015-08-28 08:56:41 -05:00
|
|
|
uiWindow *w;
|
2015-06-11 18:07:06 -05:00
|
|
|
|
2016-04-25 19:15:02 -05:00
|
|
|
uiUnixNewControl(uiWindow, w);
|
2015-06-11 18:07:06 -05:00
|
|
|
|
2015-06-13 11:59:13 -05:00
|
|
|
w->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
|
|
w->container = GTK_CONTAINER(w->widget);
|
|
|
|
w->window = GTK_WINDOW(w->widget);
|
|
|
|
|
|
|
|
gtk_window_set_title(w->window, title);
|
|
|
|
gtk_window_resize(w->window, width, height);
|
|
|
|
|
|
|
|
w->vboxWidget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
w->vboxContainer = GTK_CONTAINER(w->vboxWidget);
|
|
|
|
w->vbox = GTK_BOX(w->vboxWidget);
|
|
|
|
|
|
|
|
// set the vbox as the GtkWindow child
|
|
|
|
gtk_container_add(w->container, w->vboxWidget);
|
|
|
|
|
|
|
|
if (hasMenubar) {
|
|
|
|
w->menubar = makeMenubar(uiWindow(w));
|
|
|
|
gtk_container_add(w->vboxContainer, w->menubar);
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:45:23 -05:00
|
|
|
w->childHolderWidget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
w->childHolderContainer = GTK_CONTAINER(w->childHolderWidget);
|
|
|
|
gtk_widget_set_hexpand(w->childHolderWidget, TRUE);
|
|
|
|
gtk_widget_set_halign(w->childHolderWidget, GTK_ALIGN_FILL);
|
|
|
|
gtk_widget_set_vexpand(w->childHolderWidget, TRUE);
|
|
|
|
gtk_widget_set_valign(w->childHolderWidget, GTK_ALIGN_FILL);
|
|
|
|
gtk_container_add(w->vboxContainer, w->childHolderWidget);
|
|
|
|
|
2015-06-13 11:59:13 -05:00
|
|
|
// show everything in the vbox, but not the GtkWindow itself
|
|
|
|
gtk_widget_show_all(w->vboxWidget);
|
|
|
|
|
2016-06-15 15:45:49 -05:00
|
|
|
// and connect our events
|
2015-06-13 11:59:13 -05:00
|
|
|
g_signal_connect(w->widget, "delete-event", G_CALLBACK(onClosing), w);
|
2016-06-15 19:45:10 -05:00
|
|
|
g_signal_connect(w->widget, "configure-event", G_CALLBACK(onConfigure), w);
|
2016-06-15 23:45:23 -05:00
|
|
|
g_signal_connect(w->childHolderWidget, "size-allocate", G_CALLBACK(onSizeAllocate), w);
|
2015-08-28 08:56:41 -05:00
|
|
|
uiWindowOnClosing(w, defaultOnClosing, NULL);
|
2016-06-15 23:45:23 -05:00
|
|
|
uiWindowOnPositionChanged(w, defaultOnPositionContentSizeChanged, NULL);
|
|
|
|
uiWindowOnContentSizeChanged(w, defaultOnPositionContentSizeChanged, NULL);
|
2015-06-11 18:07:06 -05:00
|
|
|
|
2016-05-21 21:36:21 -05:00
|
|
|
// normally it's SetParent() that does this, but we can't call SetParent() on a uiWindow
|
|
|
|
// TODO we really need to clean this up
|
|
|
|
g_object_ref(w->widget);
|
|
|
|
|
2015-08-28 08:56:41 -05:00
|
|
|
return w;
|
2015-06-11 18:07:06 -05:00
|
|
|
}
|