Gotk4 NewMessageDialog doesn't take the message_format argument

Hi, I’ve a problem with GOTK4. You can find the question on StackOverflow, and if possible, I would prefer you answer on it.

I use github.com/diamondburned/gotk4/pkg/gtk/v4. I’d like to create a modal dialog, like this:

dialog := gtk.NewMessageDialog(
	&window.Window,
	gtk.DialogModal|gtk.DialogDestroyWithParent,
	gtk.MessageInfo,
	gtk.ButtonsOK,
	"Some info I want to let the user know",
)
dialog.SetTitle("Information")
dialog.SetVisible(true)

Only, NewMessageDialog() is declared like this:

func gtk.NewMessageDialog(parent *gtk.Window, flags gtk.DialogFlags, typ gtk.MessageType, buttons gtk.ButtonsType) *gtk.MessageDialog

Unkike the C function gtk_message_dialog_new(), the Go one doesn’t have the message_format argument, nor the ... (parameters for message_format) one.

My question is: How to create a information dialog in Go, with just a title, a body and one button?

package main

import (
	"os"

	"github.com/diamondburned/gotk4/pkg/gio/v2"
	"github.com/diamondburned/gotk4/pkg/gtk/v4"
)

func main() {
	var flags gio.ApplicationFlags
	app := gtk.NewApplication("com.example.dialog", flags)

	app.ConnectActivate(func() {
		window := gtk.NewApplicationWindow(app)
		window.SetTitle("MyApp")
		window.SetDefaultSize(400, 200)

		button := gtk.NewButtonWithLabel("Show messagebox")
		button.ConnectClicked(func() {
			showInfoDialog(&window.Window)
		})

		window.SetChild(button)
		window.SetVisible(true)
	})

	os.Exit(app.Run(os.Args))
}

func showInfoDialog(parent *gtk.Window) {
	var dialogFlags gtk.DialogFlags
	dialogFlags |= gtk.DialogModal
	dialogFlags |= gtk.DialogDestroyWithParent

	var msgType gtk.MessageType = gtk.MessageType(gtk.MessageInfo)
	var btnType gtk.ButtonsType = gtk.ButtonsType(gtk.ButtonsOK)

	dialog := gtk.NewMessageDialog(
		parent,
		dialogFlags,
		msgType,
		btnType,
	)
	dialog.SetTitle("Info")
	dialog.SetMarkup("Your message...")

	area := dialog.MessageArea()
	if box, ok := area.(*gtk.Box); ok {
		lbl := gtk.NewLabel("Click OK to continue...")
		lbl.SetWrap(true)
		box.Append(lbl)
	}

	dialog.ConnectResponse(func(responseID int) {
		dialog.Destroy()
	})

	dialog.SetVisible(true)
}

You could try setting the MessageDialog’s text property.

This is completely untested. I’m just going from gtk and gobject documentation, along with gotk4 docs on pkg.go.dev.

dialog := gtk.NewMessageDialog(
	&window.Window,
	gtk.DialogModal|gtk.DialogDestroyWithParent,
	gtk.MessageInfo,
	gtk.ButtonsOK,
)

var textValue glib.Value
textValue.InitValue(glib.TypeString)
textValue.SetString("Some info I want to let the user know")

dialog.SetObjectProperty("text", textValue)


Hi,

Your solution also works, but I prefer the @Vio’s one, for these reasons:

  • His own does not use SetObjectProperty(), that I keep for cases where I can’t do otherwise.
  • Your one contains an error: I must not create a nil *glib.Value, then set it a type and a value (by the way, the InitValue doesn’t exists), I just have to run glib.NewValue("...") and it automatically infers type.

Thanks you for answering