Skip to content

Commit

Permalink
Add NewDialogWithFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Oct 17, 2021
1 parent c54e8c3 commit fe15f63
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 deletions.
47 changes: 44 additions & 3 deletions gir/cmd/gir_generate/gendata/gendata.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,54 @@ func GtkTextBufferInsert(nsgen *girgen.NamespaceGenerator) error {
return nil
}

const cGTKDialogNew2 = `
GtkWidget* _gotk4_gtk_dialog_new2(const gchar* title, GtkWindow* parent, GtkDialogFlags flags) {
return gtk_dialog_new_with_buttons(title, parent, flags, NULL, NULL);
}
`

func GtkNewDialog(nsgen *girgen.NamespaceGenerator) error {
fg, ok := nsgen.Files["gtkdialog.go"]
if !ok {
return nil
}

h := fg.Header()
h.Import("runtime")
h.AddCBlock(cGTKDialogNew2)

p := fg.Pen()
p.Line(`
// NewDialogWithFlags is a slightly more advanced version of NewDialog,
// allowing the user to construct a new dialog with the given
// constructor-only dialog flags.
//
// It is a wrapper around Gtk.Dialog.new_with_buttons in C.
func NewDialogWithFlags(title string, parent *gtk.Window, flags gtk.DialogFlags) *gtk.Dialog {
ctitle := C.CString(title)
defer C.free(unsafe.Pointer(ctitle))
w := C._gotk4_gtk_dialog_new2(
(*C.gchar)(ctitle),
(*C.GtkWindow)(parent.Native()),
(C.GtkDialogFlags)(flags),
)
runtime.KeepAlive(parent)
return wrapDialog(externglib.Take(unsafe.Pointer(c)))
}
`)

return nil
}

// Postprocessors is similar to Append, except the caller can mutate the package
// in a more flexible manner.
var Postprocessors = map[string][]girgen.Postprocessor{
"GLib-2": {GioArrayUseBytes, GLibAliases, GLibLogs, GLibDateTime},
"GLib-2": {ImportGError, GioArrayUseBytes, GLibAliases, GLibLogs, GLibDateTime},
"Gio-2": {ImportGError},
"Gtk-3": {ImportGError},
"Gtk-4": {ImportGError}, // for the marshaler
"Gtk-3": {ImportGError, GtkNewDialog},
"Gtk-4": {ImportGError, GtkNewDialog},
}

// Appends contains the contents of files that are appended into generated
Expand Down
10 changes: 9 additions & 1 deletion gir/girgen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (f *FileGenerator) Generate() ([]byte, error) {

if len(f.header.Callbacks) > 0 {
for _, callback := range f.header.SortedCallbackHeaders() {
fpen.Words("//", callback)
fpen.Words(makeComment(callback))
}
}

Expand Down Expand Up @@ -160,6 +160,14 @@ func (f *FileGenerator) Generate() ([]byte, error) {
return b, nil
}

func makeComment(block string) string {
lines := strings.Split(block, "\n")
for i := range lines {
lines[i] = "// " + lines[i]
}
return strings.Join(lines, "\n")
}

func makeImport(importPath, alias string) string {
pathBase := path.Base(importPath)

Expand Down
30 changes: 30 additions & 0 deletions gir/girgen/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,36 @@ func CallbackCHeader(source *gir.NamespaceFindResult, callback *gir.Callback) st
)
}

// AddCBlock adds a block of C code into the Cgo preamble.
func (h *Header) AddCBlock(block string) {
if block == "" {
// Bound check to prevent panic in line logic.
return
}

block = strings.TrimSpace(block)

// Guess the indentation: grab the last line and count its leading tabs.
lines := strings.Split(block, "\n")
lastLine := lines[len(lines)-1]

var tabs int
for tabs < len(lastLine) && lastLine[tabs] == '\t' {
tabs++
}

// Trim all lines except for the first, since TrimSpace will take care of
// that.
indent := strings.Repeat("\t", tabs)
for i := 1; i < len(lines); i++ {
lines[i] = strings.TrimPrefix(lines[i], indent)
}

// Rejoin and add.
block = strings.Join(lines, "\n")
h.AddCallbackHeader(block)
}

// AddCallbackHeader adds a callback header raw.
func (h *Header) AddCallbackHeader(header string) {
if h.stop {
Expand Down
22 changes: 22 additions & 0 deletions pkg/gtk/v3/gtkdialog.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions pkg/gtk/v4/gtkdialog.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fe15f63

Please sign in to comment.