From d5ce49c8b0a2a7c28e1794022424fddc2369f900 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 23 Feb 2014 20:04:33 -0500 Subject: [PATCH] Added the preferred widget size basework and its GTK+ implementation. --- gtkcalls_unix.go | 8 ++++++++ prefsize_unix.go | 21 +++++++++++++++++++++ sysdata.go | 3 +++ 3 files changed, 32 insertions(+) create mode 100644 prefsize_unix.go diff --git a/gtkcalls_unix.go b/gtkcalls_unix.go index 95605e7..7731212 100644 --- a/gtkcalls_unix.go +++ b/gtkcalls_unix.go @@ -181,3 +181,11 @@ func gtk_label_set_text(widget *gtkWidget, text string) { func gtk_label_get_text(widget *gtkWidget) string { return C.GoString(fromgchar(C.gtk_label_get_text(togtklabel(widget)))) } + +func gtk_widget_get_preferred_size(w *gtkWidget) (minWidth int, minHeight int, natWidth int, natHeight int) { + var minimum, natural C.GtkRequisition + + C.gtk_widget_get_preferred_size(togtkwidget(w), &minimum, &natural) + return int(minimum.width), int(minimum.height), + int(natural.width), int(natural.height) +} diff --git a/prefsize_unix.go b/prefsize_unix.go new file mode 100644 index 0000000..b9114fc --- /dev/null +++ b/prefsize_unix.go @@ -0,0 +1,21 @@ +// +build !windows,!darwin,!plan9 + +// 23 february 2014 +package ui + +import ( + // ... +) + +// GTK+ 3 makes this easy: controls can tell us what their preferred size is! +// ...actually, it tells us two things: the "minimum size" and the "natural size". +// The "minimum size" is the smallest size we /can/ display /anything/. The "natural size" is the smallest size we would /prefer/ to display. +// The difference? Minimum size takes into account things like truncation with ellipses: the minimum size of a label can allot just the ellipses! +// So we use the natural size instead, right? +// We could, but there's one snag: "Handle with care. Note that the natural height of a height-for-width widget will generally be a smaller size than the minimum height, since the required height for the natural width is generally smaller than the required height for the minimum width." +// This will have to be taken care of manually, so TODO; we'll just use the natural size for now + +func (s *sysData) preferredSize() (width int, height int) { + _, _, width, height = gtk_widget_get_preferred_size(s.widget) + return width, height +} diff --git a/sysdata.go b/sysdata.go index adef516..9659a1e 100644 --- a/sysdata.go +++ b/sysdata.go @@ -61,6 +61,9 @@ func (c *cSysData) setWindowSize(int, int) error { func (c *cSysData) delete(int) error { panic(runtime.GOOS + " sysData does not define delete()") } +func (c *cSysData) preferredSize() (int, int) { + panic(runtime.GOOS + " sysData does not define preferredSize()") +} // signal sends the event signal. This raise is done asynchronously to avoid deadlocking the UI task. // Thanks skelterjohn for this techinque: if we can't queue any more events, drop them