Signaling

In a traditional GTK+ application, it would be possible, even recommended, to make use of the built-in signal handling capabilities. A new signal could be created, which would then be emitted by the application at an appropriate time; components could connect to this signal and respond appropriately. However, the ability to create signals is not exposed through the Go-GTK API and so we will use callbacks like the previous examples.

To load our test server, we first update the main() function to set up a server and pass it to the user interface creation code. We then set the content to show the current message from our test server:

func main() {
server := client.NewTestServer()
main := new(mainUI)
main.showMain(server)
main.setEmail(server.CurrentMessage())

gtk.Main()
}

This makes use of a new helper function that will set the content of the email detail panel. We will call this from our list selection code later as well:

func (m *mainUI) setEmail(message *client.EmailMessage) {
m.subject.SetText(message.Subject)
m.to.SetText(message.ToEmailString())
m.from.SetText(message.FromEmailString())
m.date.SetText(message.DateString())

m.content.GetBuffer().SetText(message.Content)
}

To set the content of the email list, we store the iterator and the model in our application struct when created, so they can be referenced later. The following helper function handles the details of prepending an item to the email list. This function is called on each message in server.ListMessages() to set up the initial list:

func (m *mainUI) prependEmail(message *client.EmailMessage) {
m.listModel.Prepend(&m.listIter)
m.listModel.SetValue(&m.listIter, 0, message.Subject)
}

The last part of the basic communication with the user interface is to handle the selection of items in the tree view. To handle this, our application must implement gtk.GtkTreeSelecter, which has a single Select() function. The following implementation will suit our needs. Firstly, note that this can be called for selection and deselection, so we need to check that the item is not currently selected. Then, we use the path specified when the callback is invoked to determine the row that was clicked. This row number is used to get the email from the server list of messages. We can then call our helpful setEmail() function:

func (m *mainUI) Select(selection *gtk.TreeSelection, model *gtk.TreeModel, path *gtk.TreePath, selected bool) bool {
if selected { // already selected, just return
return true
}

row := path.GetIndices()[0]
email := m.server.ListMessages()[row]

m.setEmail(email)
return true
}

For the select handler to be called, we must register it on gtk.ListView when it is created:

var selecter gtk.GtkTreeSelecter
selecter = mainUI
list.GetSelection().SetSelectFunction(&selecter)

Now, the user interface should be complete. We need to handle background updates when new emails arrive.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.156.251