Exemple #1
0
func main() {
	gtk3.Init()

	var infoBuf, sourceBuf *gtk3.TextBuffer

	window := gtk3.NewWindow(gtk3.GtkWindowType.TOPLEVEL)
	window.SetTitle("Gtk+ Code Demos")
	window.Connect("destroy", gtk3.MainQuit)

	hbox := gtk3.NewHBox(0)
	window.Add(hbox)

	// Create tree
	tree := CreateTree()
	hbox.PackStart(tree, false, false, 0)

	notebook := gtk3.NewNotebook()
	hbox.PackStart(notebook, true, true, 0)

	notebook.AppendPage(CreateText(&infoBuf, false), gtk3.NewLabelWithMnemonic("_Info"))

	infoBuf.CreateTag("title", gtk3.P{"font": "Sans 18"})

	notebook.AppendPage(CreateText(&sourceBuf, true), gtk3.NewLabelWithMnemonic("_Source"))

	sourceBuf.CreateTag("comment", gtk3.P{"foreground": "DodgerBlue"})
	window.SetDefaultSize(600, 400)
	window.ShowAll()

	gtk3.Main()
}
Exemple #2
0
func CreateTree() gtk3.WidgetLike {
	model := gtk3.NewTreeStore([]gobject.GType{gobject.G_TYPE_STRING, gobject.G_TYPE_INT})

	treeView := gtk3.NewTreeViewWithModel(model)
	selection := treeView.GetSelection()
	if selection == nil {
		panic("Selection object nil")
	}
	selection.SetMode(gtk3.GtkSelectionMode.BROWSE)

	var iter gtk3.TreeIter

	for _, demo := range Demos {
		model.Append(&iter, nil)
		model.SetValues(&iter, gtk3.V{TitleColumn: demo.Title, StyleColumn: pango.PangoStyle.NORMAL})

		if len(demo.Children) > 0 {
			var childIter gtk3.TreeIter
			for _, childDemo := range demo.Children {
				model.Append(&childIter, &iter)
				model.SetValues(&childIter, gtk3.V{TitleColumn: childDemo.Title, StyleColumn: pango.PangoStyle.NORMAL})
			}
		}
	}

	cell := gtk3.NewCellRendererText()
	column := gtk3.NewTreeViewColumnWithAttributes("Widget (double click for demo)",
		cell,
		gtk3.A{{"text", TitleColumn}, {"style", StyleColumn}})

	treeView.AppendColumn(column)

	model.GetIterFirst(&iter)
	selection.SelectIter(&iter)

	treeView.Connect("row_activated", rowActivated, model)

	treeView.CollapseAll()
	treeView.SetHeadersVisible(false)

	sw := gtk3.NewScrolledWindow(nil, nil)
	sw.SetPolicy(gtk3.GtkPolicy.AUTOMATIC, gtk3.GtkPolicy.AUTOMATIC)
	sw.Add(treeView)

	label := gtk3.NewLabel("Widget (double click for demo)")

	box := gtk3.NewNotebook()
	box.AppendPage(sw, label)

	treeView.GrabFocus()

	return box
}
Exemple #3
0
func DoSearchEntry(w gtk3.WidgetLike) gtk3.WidgetLike {
	if window == nil {
		var searchProgressId, finishSearchId uint

		window = gtk3.NewDialogWithButtons("Search Entry", w.(*gtk3.Window),
			0, gtk3.B{{gtk3.GtkStock.CLOSE, gtk3.GtkResponse.NONE}})
		window.SetResizable(false)
		window.Connect("response", func() { window.Destroy() })
		window.Connect("destroy", func() {
			if finishSearchId != 0 {
				glib.GSourceRemove(finishSearchId)
			}
			if searchProgressId != 0 {
				glib.GSourceRemove(searchProgressId)
			}

			window = nil
		})

		contentArea := window.GetContentArea().(*gtk3.Box)

		vbox := gtk3.NewVBox(5)
		contentArea.PackStart(vbox, true, true, 0)
		vbox.SetBorderWidth(5)

		label := gtk3.NewLabel("")
		label.SetMarkup("Search entry demo")
		vbox.PackStart(label, false, false, 0)

		hbox := gtk3.NewHBox(10)
		vbox.PackStart(hbox, true, true, 0)
		hbox.SetBorderWidth(0)

		// Create our entry
		entry := gtk3.NewEntry()
		hbox.PackStart(entry, false, false, 0)

		// Create the find and cancel buttons
		notebook := gtk3.NewNotebook()
		notebook.SetShowTabs(false)
		notebook.SetShowBorder(false)
		hbox.PackStart(notebook, false, false, 0)

		startSearchFunc := func() { entry.ProgressPulse() }

		finishSearchFunc := func() bool {
			notebook.SetCurrentPage(0)
			glib.GSourceRemove(searchProgressId)
			searchProgressId = 0
			entry.SetProgressFraction(0.0)
			return false
		}

		findButton := gtk3.NewButtonWithLabel("Find")
		findButton.Connect("clicked", func() {
			notebook.SetCurrentPage(1)

			searchProgressId = glib.GTimeoutAddFull(glib.GPriority.DEFAULT, 100, startSearchFunc)

			finishSearchId = glib.GTimeoutAddSecondsFull(glib.GPriority.DEFAULT, 15, finishSearchFunc)
		})
		notebook.AppendPage(findButton, nil)
		findButton.Show()

		cancelButton := gtk3.NewButtonWithLabel("Cancel")
		cancelButton.Connect("clicked", func() {
			glib.GSourceRemove(finishSearchId)
			finishSearchFunc()
		})
		notebook.AppendPage(cancelButton, nil)
		cancelButton.Show()

		// Set up the search icon
		entry.SetIconFromStock(gtk3.GtkEntryIconPosition.PRIMARY, gtk3.GtkStock.FIND)
		entry.SetIconTooltipText(gtk3.GtkEntryIconPosition.PRIMARY,
			"Search by name\nClick here to change the search type")
		entry.SetPlaceholderText("name")

		// Set up the clear icon
		entry.SetIconFromStock(gtk3.GtkEntryIconPosition.SECONDARY, gtk3.GtkStock.CLEAR)

		menu := createSearchMenu(entry)
		menu.AttachToWidget(entry, nil)

		textChangedFunc := func() {
			hasText := entry.GetTextLength() > 0
			entry.SetIconSensitive(gtk3.GtkEntryIconPosition.SECONDARY, hasText)
			findButton.SetSensitive(hasText)
		}

		textChangedFunc()

		entry.Connect("icon-press", iconPressCallback, menu)
		entry.Connect("notify::text", textChangedFunc)

		entry.Connect("activate", func() {
			if searchProgressId != 0 {
				return
			}
			notebook.SetCurrentPage(1)

			searchProgressId = glib.GTimeoutAddFull(glib.GPriority.DEFAULT, 100, startSearchFunc)

			finishSearchId = glib.GTimeoutAddSecondsFull(glib.GPriority.DEFAULT, 15, finishSearchFunc)

		})

		entry.Connect("populate-popup", entryPopulatePopup)

		button := window.GetWidgetForResponse(gtk3.GtkResponse.NONE)
		button.W().GrabFocus()
	}

	if !window.GetVisible() {
		window.ShowAll()
	} else {
		window.Destroy()
		window = nil
		return nil
	}

	return window
}