2015-04-06 11:20:51 -05:00
|
|
|
// 6 april 2015
|
2015-04-06 23:26:27 -05:00
|
|
|
#include "uipriv_unix.h"
|
2015-04-06 11:20:51 -05:00
|
|
|
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window {
|
|
|
|
uiWindow w;
|
2015-04-06 11:20:51 -05:00
|
|
|
GtkWidget *widget;
|
2015-04-16 22:31:32 -05:00
|
|
|
GtkContainer *container;
|
|
|
|
GtkWindow *window;
|
2015-04-13 11:05:14 -05:00
|
|
|
uiParent *content;
|
2015-04-06 11:20:51 -05:00
|
|
|
int (*onClosing)(uiWindow *, void *);
|
|
|
|
void *onClosingData;
|
2015-04-13 11:05:14 -05:00
|
|
|
int margined;
|
2015-04-18 16:46:37 -05:00
|
|
|
gulong destroyBlocker;
|
2015-04-06 11:20:51 -05:00
|
|
|
};
|
|
|
|
|
2015-04-10 20:48:50 -05:00
|
|
|
static gboolean onClosing(GtkWidget *win, GdkEvent *e, gpointer data)
|
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) data;
|
2015-04-10 20:48:50 -05:00
|
|
|
|
2015-04-18 11:22:15 -05:00
|
|
|
// manually destroy the window ourselves; don't let the delete-event handler do it
|
2015-04-15 22:14:36 -05:00
|
|
|
if ((*(w->onClosing))(uiWindow(w), w->onClosingData))
|
2015-04-18 11:22:15 -05:00
|
|
|
uiWindowDestroy(uiWindow(w));
|
|
|
|
return TRUE; // don't continue to the default delete-event handler; we destroyed the window by now
|
2015-04-10 20:48:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int defaultOnClosing(uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:37 -05:00
|
|
|
static void destroyBlocker(GtkWidget *widget, gpointer data)
|
2015-04-07 21:46:15 -05:00
|
|
|
{
|
2015-04-18 16:46:37 -05:00
|
|
|
complain("attempt to dispose uiWindow at %p before uiWindowDestroy()", data);
|
2015-04-07 21:46:15 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 11:22:15 -05:00
|
|
|
// TODO should we change the GtkWindow's child first?
|
2015-04-15 21:26:27 -05:00
|
|
|
static void windowDestroy(uiWindow *ww)
|
2015-04-06 11:20:51 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
2015-04-06 11:20:51 -05:00
|
|
|
|
2015-04-18 11:22:15 -05:00
|
|
|
// first, hide the window to prevent our cleanup from being noticed
|
|
|
|
gtk_widget_hide(w->widget);
|
|
|
|
// next, destroy the content uiParent
|
|
|
|
uiParentDestroy(w->content);
|
|
|
|
// now that we cleaned up properly, we can mark our window as ready to be destroyed
|
2015-04-18 16:46:37 -05:00
|
|
|
g_signal_handler_disconnect(w->widget, w->destroyBlocker);
|
2015-04-18 11:22:15 -05:00
|
|
|
// finally, destroy the window
|
2015-04-06 11:20:51 -05:00
|
|
|
gtk_widget_destroy(w->widget);
|
2015-04-18 16:46:37 -05:00
|
|
|
// and free ourselves
|
|
|
|
uiFree(w);
|
2015-04-06 11:20:51 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static uintptr_t windowHandle(uiWindow *ww)
|
2015-04-06 11:20:51 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-06 11:20:51 -05:00
|
|
|
return (uintptr_t) (w->widget);
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static char *windowTitle(uiWindow *ww)
|
2015-04-09 01:56:51 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
return g_strdup(gtk_window_get_title(w->window));
|
2015-04-09 01:56:51 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static void windowSetTitle(uiWindow *ww, const char *title)
|
2015-04-09 01:56:51 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
gtk_window_set_title(w->window, title);
|
2015-04-09 01:56:51 -05:00
|
|
|
}
|
2015-04-06 11:20:51 -05:00
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static void windowShow(uiWindow *ww)
|
2015-04-06 11:20:51 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-11 23:59:32 -05:00
|
|
|
// don't use gtk_widget_show_all(); that will override user hidden settings
|
|
|
|
gtk_widget_show(w->widget);
|
2015-04-06 11:20:51 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static void windowHide(uiWindow *ww)
|
2015-04-06 11:20:51 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
2015-04-06 11:20:51 -05:00
|
|
|
gtk_widget_hide(w->widget);
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static void windowOnClosing(uiWindow *ww, int (*f)(uiWindow *, void *), void *data)
|
2015-04-06 11:20:51 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-06 11:20:51 -05:00
|
|
|
w->onClosing = f;
|
|
|
|
w->onClosingData = data;
|
|
|
|
}
|
2015-04-07 12:22:46 -05:00
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static void windowSetChild(uiWindow *ww, uiControl *c)
|
2015-04-07 12:22:46 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-17 12:43:01 -05:00
|
|
|
uiParentSetMainControl(w->content, c);
|
2015-04-13 11:05:14 -05:00
|
|
|
uiParentUpdate(w->content);
|
2015-04-07 12:22:46 -05:00
|
|
|
}
|
2015-04-09 14:18:18 -05:00
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static int windowMargined(uiWindow *ww)
|
2015-04-09 20:11:56 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-13 11:05:14 -05:00
|
|
|
return w->margined;
|
2015-04-09 20:11:56 -05:00
|
|
|
}
|
2015-04-09 14:18:18 -05:00
|
|
|
|
2015-04-16 22:31:32 -05:00
|
|
|
static void windowSetMargined(uiWindow *ww, int margined)
|
2015-04-09 14:18:18 -05:00
|
|
|
{
|
2015-04-15 21:26:27 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-13 11:05:14 -05:00
|
|
|
w->margined = margined;
|
|
|
|
if (w->margined)
|
|
|
|
uiParentSetMargins(w->content, gtkXMargin, gtkYMargin, gtkXMargin, gtkYMargin);
|
|
|
|
else
|
|
|
|
uiParentSetMargins(w->content, 0, 0, 0, 0);
|
|
|
|
uiParentUpdate(w->content);
|
2015-04-09 14:18:18 -05:00
|
|
|
}
|
2015-04-15 21:26:27 -05:00
|
|
|
|
2015-04-20 17:34:51 -05:00
|
|
|
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
|
2015-04-15 21:26:27 -05:00
|
|
|
{
|
|
|
|
struct window *w;
|
2015-04-20 17:34:51 -05:00
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *contentWidget;
|
2015-04-15 21:26:27 -05:00
|
|
|
|
|
|
|
w = uiNew(struct window);
|
2015-04-16 22:31:32 -05:00
|
|
|
|
2015-04-15 21:26:27 -05:00
|
|
|
w->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
2015-04-16 22:31:32 -05:00
|
|
|
w->container = GTK_CONTAINER(w->widget);
|
|
|
|
w->window = GTK_WINDOW(w->widget);
|
|
|
|
|
|
|
|
gtk_window_set_title(w->window, title);
|
2015-04-20 17:34:51 -05:00
|
|
|
// TODO this does not take menus or CSD into account
|
2015-04-16 22:31:32 -05:00
|
|
|
gtk_window_resize(w->window, width, height);
|
|
|
|
|
2015-04-15 21:26:27 -05:00
|
|
|
g_signal_connect(w->widget, "delete-event", G_CALLBACK(onClosing), w);
|
2015-04-18 16:46:37 -05:00
|
|
|
w->destroyBlocker = g_signal_connect(w->widget, "destroy", G_CALLBACK(destroyBlocker), w);
|
2015-04-16 22:31:32 -05:00
|
|
|
|
2015-04-20 17:34:51 -05:00
|
|
|
if (hasMenubar) {
|
|
|
|
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
gtk_widget_show_all(vbox);
|
|
|
|
gtk_container_add(w->container, vbox);
|
|
|
|
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), makeMenubar());
|
|
|
|
|
|
|
|
w->content = uiNewParent((uintptr_t) GTK_CONTAINER(vbox));
|
|
|
|
contentWidget = GTK_WIDGET(uiParentHandle(w->content));
|
|
|
|
gtk_widget_set_hexpand(contentWidget, TRUE);
|
|
|
|
gtk_widget_set_halign(contentWidget, GTK_ALIGN_FILL);
|
|
|
|
gtk_widget_set_vexpand(contentWidget, TRUE);
|
|
|
|
gtk_widget_set_valign(contentWidget, GTK_ALIGN_FILL);
|
|
|
|
} else
|
|
|
|
w->content = uiNewParent((uintptr_t) (w->container));
|
|
|
|
|
2015-04-15 21:26:27 -05:00
|
|
|
w->onClosing = defaultOnClosing;
|
|
|
|
|
|
|
|
uiWindow(w)->Destroy = windowDestroy;
|
2015-04-16 22:31:32 -05:00
|
|
|
uiWindow(w)->Handle = windowHandle;
|
|
|
|
uiWindow(w)->Title = windowTitle;
|
|
|
|
uiWindow(w)->SetTitle = windowSetTitle;
|
|
|
|
uiWindow(w)->Show = windowShow;
|
|
|
|
uiWindow(w)->Hide = windowHide;
|
|
|
|
uiWindow(w)->OnClosing = windowOnClosing;
|
|
|
|
uiWindow(w)->SetChild = windowSetChild;
|
|
|
|
uiWindow(w)->Margined = windowMargined;
|
|
|
|
uiWindow(w)->SetMargined = windowSetMargined;
|
2015-04-15 21:26:27 -05:00
|
|
|
|
|
|
|
return uiWindow(w);
|
|
|
|
}
|