2015-04-07 11:17:05 -05:00
|
|
|
// 7 april 2015
|
|
|
|
#include "uipriv_unix.h"
|
|
|
|
|
2015-04-09 17:17:04 -05:00
|
|
|
struct singleWidget {
|
2015-04-07 11:17:05 -05:00
|
|
|
GtkWidget *widget;
|
|
|
|
GtkWidget *scrolledWindow;
|
|
|
|
GtkWidget *immediate; // the widget that is added to the parent container; either widget or scrolledWindow
|
2015-04-28 20:30:38 -05:00
|
|
|
uiContainer *parent;
|
|
|
|
int hidden;
|
2015-04-18 16:14:19 -05:00
|
|
|
void (*onDestroy)(void *);
|
|
|
|
void *onDestroyData;
|
2015-04-07 11:17:05 -05:00
|
|
|
};
|
|
|
|
|
2015-04-08 00:16:22 -05:00
|
|
|
static void singleDestroy(uiControl *c)
|
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-09 17:17:04 -05:00
|
|
|
|
2015-04-19 21:52:51 -05:00
|
|
|
if (s->parent != NULL)
|
2015-04-28 20:30:38 -05:00
|
|
|
complain("attempt to destroy a uiControl at %p while it still has a parent", c);
|
2015-04-18 12:38:19 -05:00
|
|
|
// first call the widget's own destruction code
|
2015-04-18 16:14:19 -05:00
|
|
|
(*(s->onDestroy))(s->onDestroyData);
|
2015-05-01 16:10:36 -05:00
|
|
|
// then actually destroy
|
2015-04-28 20:30:38 -05:00
|
|
|
g_object_unref(s->immediate);
|
2015-04-18 16:46:37 -05:00
|
|
|
// and free ourselves
|
|
|
|
uiFree(s);
|
2015-04-08 00:16:22 -05:00
|
|
|
}
|
|
|
|
|
2015-04-07 11:17:05 -05:00
|
|
|
static uintptr_t singleHandle(uiControl *c)
|
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-09 17:17:04 -05:00
|
|
|
|
|
|
|
return (uintptr_t) (s->widget);
|
2015-04-07 11:17:05 -05:00
|
|
|
}
|
|
|
|
|
2015-04-28 20:30:38 -05:00
|
|
|
static void singleSetParent(uiControl *c, uiContainer *parent)
|
2015-04-09 15:21:09 -05:00
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-28 20:30:38 -05:00
|
|
|
uiContainer *oldparent;
|
|
|
|
GtkContainer *oldcontainer;
|
|
|
|
GtkContainer *newcontainer;
|
2015-04-09 15:21:09 -05:00
|
|
|
|
|
|
|
oldparent = s->parent;
|
2015-04-14 11:41:25 -05:00
|
|
|
s->parent = parent;
|
|
|
|
if (oldparent != NULL) {
|
2015-04-28 20:30:38 -05:00
|
|
|
oldcontainer = GTK_CONTAINER(uiControlHandle(uiControl(oldparent)));
|
|
|
|
gtk_container_remove(oldcontainer, s->immediate);
|
2015-04-14 11:41:25 -05:00
|
|
|
}
|
|
|
|
if (s->parent != NULL) {
|
2015-04-28 20:30:38 -05:00
|
|
|
newcontainer = GTK_CONTAINER(uiControlHandle(uiControl(s->parent)));
|
|
|
|
gtk_container_add(newcontainer, s->immediate);
|
2015-04-14 11:41:25 -05:00
|
|
|
}
|
2015-04-28 20:30:38 -05:00
|
|
|
if (oldparent != NULL)
|
|
|
|
uiContainerUpdate(oldparent);
|
|
|
|
if (s->parent != NULL)
|
|
|
|
uiContainerUpdate(s->parent);
|
2015-04-09 15:21:09 -05:00
|
|
|
}
|
|
|
|
|
2015-04-09 17:17:04 -05:00
|
|
|
static void singlePreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
|
2015-04-07 11:17:05 -05:00
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-07 11:17:05 -05:00
|
|
|
GtkRequisition natural;
|
|
|
|
|
|
|
|
// use the natural size as the minimum size is an *absolute* minimum
|
|
|
|
// for example, if a label has ellipsizing on, it can be the width of the ellipses, not the text
|
|
|
|
// there is a warning about height-for-width sizing, but in my tests this isn't an issue
|
2015-04-09 17:17:04 -05:00
|
|
|
gtk_widget_get_preferred_size(s->widget, NULL, &natural);
|
|
|
|
*width = natural.width;
|
|
|
|
*height = natural.height;
|
2015-04-07 11:17:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
|
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-07 11:17:05 -05:00
|
|
|
GtkAllocation a;
|
|
|
|
|
|
|
|
a.x = x;
|
|
|
|
a.y = y;
|
|
|
|
a.width = width;
|
|
|
|
a.height = height;
|
2015-04-09 17:17:04 -05:00
|
|
|
gtk_widget_size_allocate(s->immediate, &a);
|
2015-04-07 11:17:05 -05:00
|
|
|
}
|
|
|
|
|
2015-04-11 13:21:58 -05:00
|
|
|
static int singleVisible(uiControl *c)
|
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-11 13:21:58 -05:00
|
|
|
|
2015-04-29 09:40:50 -05:00
|
|
|
return !s->hidden;
|
2015-04-11 13:21:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void singleShow(uiControl *c)
|
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-11 13:21:58 -05:00
|
|
|
|
2015-04-28 20:30:38 -05:00
|
|
|
gtk_widget_show_all(s->immediate);
|
2015-04-29 13:25:34 -05:00
|
|
|
s->hidden = 0;
|
2015-04-13 11:05:14 -05:00
|
|
|
if (s->parent != NULL)
|
2015-04-28 20:30:38 -05:00
|
|
|
uiContainerUpdate(s->parent);
|
2015-04-11 13:21:58 -05:00
|
|
|
}
|
|
|
|
|
2015-04-28 20:30:38 -05:00
|
|
|
static void singleHide(uiControl *c)
|
2015-04-11 13:21:58 -05:00
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-11 13:21:58 -05:00
|
|
|
|
|
|
|
gtk_widget_hide(s->immediate);
|
2015-04-29 13:25:34 -05:00
|
|
|
s->hidden = 1;
|
2015-04-13 11:05:14 -05:00
|
|
|
if (s->parent != NULL)
|
2015-04-28 20:30:38 -05:00
|
|
|
uiContainerUpdate(s->parent);
|
2015-04-11 13:21:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void singleEnable(uiControl *c)
|
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-11 13:21:58 -05:00
|
|
|
|
2015-04-28 20:30:38 -05:00
|
|
|
gtk_widget_set_sensitive(s->immediate, TRUE);
|
2015-04-11 13:21:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void singleDisable(uiControl *c)
|
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s = (struct singleWidget *) (c->Internal);
|
2015-04-11 13:21:58 -05:00
|
|
|
|
|
|
|
gtk_widget_set_sensitive(s->immediate, FALSE);
|
|
|
|
}
|
|
|
|
|
2015-05-02 19:51:00 -05:00
|
|
|
void uiUnixMakeControl(uiControl *c, GType type, gboolean inScrolledWindow, gboolean scrolledWindowHasBorder, void (*onDestroy)(void *), void *onDestroyData, const char *firstProperty, ...)
|
2015-04-07 11:17:05 -05:00
|
|
|
{
|
2015-05-04 14:51:23 -05:00
|
|
|
struct singleWidget *s;
|
2015-04-07 18:47:44 -05:00
|
|
|
va_list ap;
|
2015-04-07 11:17:05 -05:00
|
|
|
|
2015-05-04 14:51:23 -05:00
|
|
|
s = uiNew(struct singleWidget);
|
2015-04-07 18:47:44 -05:00
|
|
|
|
|
|
|
va_start(ap, firstProperty);
|
2015-04-09 17:17:04 -05:00
|
|
|
s->widget = GTK_WIDGET(g_object_new_valist(type, firstProperty, ap));
|
2015-04-07 18:47:44 -05:00
|
|
|
va_end(ap);
|
2015-04-09 17:17:04 -05:00
|
|
|
s->immediate = s->widget;
|
2015-04-07 11:17:05 -05:00
|
|
|
|
|
|
|
if (inScrolledWindow) {
|
2015-04-09 17:17:04 -05:00
|
|
|
s->scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
|
|
|
|
if (!GTK_IS_SCROLLABLE(s->widget))
|
|
|
|
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(s->scrolledWindow), s->widget);
|
2015-04-07 11:17:05 -05:00
|
|
|
else
|
2015-04-09 17:17:04 -05:00
|
|
|
gtk_container_add(GTK_CONTAINER(s->scrolledWindow), s->widget);
|
2015-04-07 11:17:05 -05:00
|
|
|
if (scrolledWindowHasBorder)
|
2015-04-09 17:17:04 -05:00
|
|
|
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(s->scrolledWindow), GTK_SHADOW_IN);
|
|
|
|
s->immediate = s->scrolledWindow;
|
2015-04-07 11:17:05 -05:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:16:22 -05:00
|
|
|
// we need to keep an extra reference on the immediate widget
|
2015-04-30 16:57:54 -05:00
|
|
|
// this is so we can reparent the control freely
|
2015-04-09 17:17:04 -05:00
|
|
|
g_object_ref_sink(s->immediate);
|
|
|
|
|
2015-04-18 16:36:12 -05:00
|
|
|
s->onDestroy = onDestroy;
|
2015-04-18 16:14:19 -05:00
|
|
|
s->onDestroyData = onDestroyData;
|
2015-04-18 11:30:25 -05:00
|
|
|
|
2015-04-28 20:30:38 -05:00
|
|
|
// finally, call gtk_widget_show_all() here to set the initial visibility of the widget
|
|
|
|
gtk_widget_show_all(s->immediate);
|
|
|
|
|
2015-05-03 13:44:46 -05:00
|
|
|
uiControl(c)->Internal = s;
|
|
|
|
uiControl(c)->Destroy = singleDestroy;
|
|
|
|
uiControl(c)->Handle = singleHandle;
|
|
|
|
uiControl(c)->SetParent = singleSetParent;
|
|
|
|
uiControl(c)->PreferredSize = singlePreferredSize;
|
|
|
|
uiControl(c)->Resize = singleResize;
|
|
|
|
uiControl(c)->Visible = singleVisible;
|
|
|
|
uiControl(c)->Show = singleShow;
|
|
|
|
uiControl(c)->Hide = singleHide;
|
|
|
|
uiControl(c)->Enable = singleEnable;
|
|
|
|
uiControl(c)->Disable = singleDisable;
|
2015-04-07 11:17:05 -05:00
|
|
|
}
|