Figured out what to do about containers in GTK+ and applied it to Group.

This commit is contained in:
Pietro Gagliardi 2014-10-17 22:28:23 -04:00
parent 00438732d1
commit b060d992ff
3 changed files with 201 additions and 7 deletions

102
newctrl/container_unix.c Normal file
View File

@ -0,0 +1,102 @@
// +build !windows,!darwin
// 13 august 2014
#include "gtk_unix.h"
#include "_cgo_export.h"
#define GOCONTAINER_TYPE (goContainer_get_type())
#define GOCONTAINER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GOCONTAINER_TYPE, goContainer))
#define IS_GOCONTAINER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GOCONTAINER_TYPE))
#define GOCONTAINER_CLASS(class) (G_TYPE_CHECK_CLASS_CAST((class), GOCONTAINER_TYPE, goContainerClass))
#define IS_GOCONTAINER_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE((class), GOCONTAINER_TYPE))
#define GOCONTAINER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GOCONTAINER_TYPE, goContainerClass))
typedef struct goContainer goContainer;
typedef struct goContainerClass goContainerClass;
struct goContainer {
GtkContainer parent_instance;
void *gocontainer;
GPtrArray *children; // for forall()
};
struct goContainerClass {
GtkContainerClass parent_class;
};
G_DEFINE_TYPE(goContainer, goContainer, GTK_TYPE_CONTAINER)
static void goContainer_init(goContainer *c)
{
c->children = g_ptr_array_new();
gtk_widget_set_has_window(GTK_WIDGET(c), FALSE);
}
static void goContainer_dispose(GObject *obj)
{
g_ptr_array_unref(GOCONTAINER(obj)->children);
G_OBJECT_CLASS(goContainer_parent_class)->dispose(obj);
}
static void goContainer_finalize(GObject *obj)
{
G_OBJECT_CLASS(goContainer_parent_class)->finalize(obj);
}
static void goContainer_add(GtkContainer *container, GtkWidget *widget)
{
gtk_widget_set_parent(widget, GTK_WIDGET(container));
g_ptr_array_add(GOCONTAINER(container)->children, widget);
}
static void goContainer_remove(GtkContainer *container, GtkWidget *widget)
{
gtk_widget_unparent(widget);
g_ptr_array_remove(GOCONTAINER(container)->children, widget);
}
static void goContainer_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
gtk_widget_set_allocation(widget, allocation);
}
struct forall {
GtkCallback callback;
gpointer data;
};
static void doforall(gpointer obj, gpointer data)
{
struct forall *s = (struct forall *) data;
(*(s->callback))(GTK_WIDGET(obj), s->data);
}
static void goContainer_forall(GtkContainer *container, gboolean includeInternals, GtkCallback callback, gpointer data)
{
struct forall s;
s.callback = callback;
s.data = data;
g_ptr_array_foreach(GOCONTAINER(container)->children, doforall, &s);
}
static void goContainer_class_init(goContainerClass *class)
{
G_OBJECT_CLASS(class)->dispose = goContainer_dispose;
G_OBJECT_CLASS(class)->finalize = goContainer_finalize;
GTK_WIDGET_CLASS(class)->size_allocate = goContainer_size_allocate;
GTK_CONTAINER_CLASS(class)->add = goContainer_add;
GTK_CONTAINER_CLASS(class)->remove = goContainer_remove;
GTK_CONTAINER_CLASS(class)->forall = goContainer_forall;
}
GtkWidget *newContainer(void *gocontainer)
{
goContainer *c;
c = (goContainer *) g_object_new(GOCONTAINER_TYPE, NULL);
c->gocontainer = gocontainer;
return GTK_WIDGET(c);
}

View File

@ -37,10 +37,17 @@ func (c *container) parent() *controlParent {
return &controlParent{c.container}
}
//export containerResizing
func containerResizing(data unsafe.Pointer, r *C.GtkAllocation) {
c := (*container)(data)
c.resize(int(r.x), int(r.y), int(r.width), int(r.height))
func (c *container) allocation(margined bool) C.GtkAllocation {
var a C.GtkAllocation
C.gtk_widget_get_allocation(c.widget, &a)
if margined {
a.x += C.gint(gtkXMargin)
a.y += C.gint(gtkYMargin)
a.width -= C.gint(gtkXMargin) * 2
a.height -= C.gint(gtkYMargin) * 2
}
return a
}
const (
@ -53,9 +60,6 @@ const (
func (w *window) beginResize() (d *sizing) {
d = new(sizing)
if spaced {
d.xmargin = gtkXMargin
d.ymargintop = gtkYMargin
d.ymarginbottom = d.ymargintop
d.xpadding = gtkXPadding
d.ypadding = gtkYPadding
}

88
newctrl/group_unix.go Normal file
View File

@ -0,0 +1,88 @@
// +build !windows,!darwin
// 15 august 2014
package ui
import (
"unsafe"
)
// #include "gtk_unix.h"
import "C"
type group struct {
*controlSingleWidget
gcontainer *C.GtkContainer
frame *C.GtkFrame
child Control
container *container
margined bool
}
func newGroup(text string, control Control) Group {
ctext := togstr(text)
defer freegstr(ctext)
widget := C.gtk_frame_new(ctext)
g := &group{
controlSingleWidget: newControlSingleWidget(widget),
gcontainer: (*C.GtkContainer)(unsafe.Pointer(widget)),
frame: (*C.GtkFrame)(unsafe.Pointer(widget)),
child: control,
}
// with GTK+, groupboxes by default have frames and slightly x-offset regular text
// they should have no frame and fully left-justified, bold text
var yalign C.gfloat
// preserve default y-alignment
C.gtk_frame_get_label_align(g.frame, nil, &yalign)
C.gtk_frame_set_label_align(g.frame, 0, yalign)
C.gtk_frame_set_shadow_type(g.frame, C.GTK_SHADOW_NONE)
label := (*C.GtkLabel)(unsafe.Pointer(C.gtk_frame_get_label_widget(g.frame)))
// this is the boldness level used by GtkPrintUnixDialog
// (it technically uses "bold" but see pango's pango-enum-types.c for the name conversion; GType is weird)
bold := C.pango_attr_weight_new(C.PANGO_WEIGHT_BOLD)
boldlist := C.pango_attr_list_new()
C.pango_attr_list_insert(boldlist, bold)
C.gtk_label_set_attributes(label, boldlist)
C.pango_attr_list_unref(boldlist) // thanks baedert in irc.gimp.net/#gtk+
g.container = newContainer(control)
g.child.setParent(g.container.parent())
g.container.setParent(&controlParent{g.gcontainer})
g.fresize = g.resize
return g
}
func (g *group) Text() string {
return fromgstr(C.gtk_frame_get_label(g.frame))
}
func (g *group) SetText(text string) {
ctext := togstr(text)
defer freegstr(ctext)
C.gtk_frame_set_label(g.frame, ctext)
}
func (g *group) Margined() bool {
return g.margined
}
func (g *group) SetMargined(margined bool) {
g.margined = margined
}
func (g *group) resize(x int, y int, width int, height int, d *sizing) {
// first, chain up to change the GtkFrame and its child container
// TODO use a variable for this
g.containerSingleWidget.resize(x, y, width, height, d)
// now that the container has the correct size, we can resize the child
a := g.container.allocation(g.margined)
g.child.resize(int(a.x), int(a.y), int(a.width), int(a.height), d)
}