From 318036d1552277439d0485277a0e81dcd2d4fcec Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 15 Aug 2014 21:43:24 -0400 Subject: [PATCH] Added Group and implemented it on GTK+. --- redo/basicctrls.go | 16 +++++++++ redo/group_unix.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++ redo/zz_test.go | 3 ++ 3 files changed, 105 insertions(+) create mode 100644 redo/group_unix.go diff --git a/redo/basicctrls.go b/redo/basicctrls.go index 94f5c8d..8894e7c 100644 --- a/redo/basicctrls.go +++ b/redo/basicctrls.go @@ -102,3 +102,19 @@ func NewLabel(text string) Label { func NewStandaloneLabel(text string) Label { return newStandaloneLabel(text) } + +// Group is a Control that holds a single Control; if that Control also contains other Controls, then the Controls will appear visually grouped together. +// The appearance of a Group varies from system to system; for the most part a Group consists of a thin frame. +// All Groups have a text label indicating what the Group is for. +type Group interface { + Control + + // Text and SetText get and set the Group's label text. + Text() string + SetText(text string) +} + +// NewGroup creates a new Group with the given text label and child Control. +func NewGroup(text string, control Control) Group { + return newGroup(text, control) +} diff --git a/redo/group_unix.go b/redo/group_unix.go new file mode 100644 index 0000000..7c4e73d --- /dev/null +++ b/redo/group_unix.go @@ -0,0 +1,86 @@ +// +build !windows,!darwin + +// 15 august 2014 + +package ui + +import ( + "unsafe" +) + +// #include "gtk_unix.h" +import "C" + +type group struct { + _widget *C.GtkWidget + gcontainer *C.GtkContainer + frame *C.GtkFrame + + *container +} + +func newGroup(text string, control Control) Group { + ctext := togstr(text) + defer freegstr(ctext) + widget := C.gtk_frame_new(ctext) + g := &group{ + _widget: widget, + gcontainer: (*C.GtkContainer)(unsafe.Pointer(widget)), + frame: (*C.GtkFrame)(unsafe.Pointer(widget)), + } + + // 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))) + // TODO confirm this boldness level against Glade + 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) + // TODO free either bold or boldlist? + + g.container = newContainer(control) + g.container.setParent(&controlParent{g.gcontainer}) + + 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) widget() *C.GtkWidget { + return g._widget +} + +func (g *group) setParent(p *controlParent) { + basesetParent(g, p) +} + +func (g *group) allocate(x int, y int, width int, height int, d *sizing) []*allocation { + return baseallocate(g, x, y, width, height, d) +} + +func (g *group) preferredSize(d *sizing) (width, height int) { + return basepreferredSize(g, d) +} + +func (g *group) commitResize(a *allocation, d *sizing) { + basecommitResize(g, a, d) +} + +func (g *group) getAuxResizeInfo(d *sizing) { + basegetAuxResizeInfo(g, d) +} diff --git a/redo/zz_test.go b/redo/zz_test.go index 85c0b4a..568f398 100644 --- a/redo/zz_test.go +++ b/redo/zz_test.go @@ -32,6 +32,7 @@ var ddata = []dtype{ type testwin struct { t Tab w Window + group Group grid Grid nt Tab a Area @@ -69,6 +70,8 @@ func (tw *testwin) make(done chan struct{}) { done <- struct{}{} return true }) + tw.group = NewGroup("Group", NewVerticalStack(NewCheckbox("Checkbox in Group"))) + tw.t.Append("Group", tw.group) tw.grid = NewGrid(3, NewLabel("0,0"), NewTextField(), NewLabel("0,2"), NewButton("1,0"), NewButton("1,1"), NewButton("1,2"),