This tutorial will guide you through the sample application for custom file handlers.
It demonstrates reading resources from an archive using zlib. In this case the resources are a web page and a couple of images. The web page is then rendered on the whole screen.
The sample solution is already configured and you only have to compile and run it.
It uses the sample application framework described in the previous sections (Base Application Framework, Sample Application Framework).
The output will be in the Coherent/Samples/UI/bin directory.
Coherent::UI::FileHandler
that handles I/O requests.CreateViewContext
.Start by including the necessary headers for the sample application framework:
and the ones for Coherent UI:
To use a custom file handler, additional includes are required (URLParse.h is needed for parsing the URL string supplied in a callback, more on that in a second):
The ViewEventListener
class is the same as the one supplied in the sample framework with the exception that it calls SetFocus in OnViewCreated on its corresponding view.
The ContextEventListener
class creates a single view when the view context signals for readiness (the OnContextReady method). The URL of the view has a special scheme - coui://. This special scheme is used by Coherent UI to call a user-supplied I/O handler. When using the scheme without supplying your own handler, a default one is used, which reads files from the filesystem.
We will create a new class, MyFileHandler
, which is derived from Coherent::UI::FileHandler
. Coherent::UI::FileHandler
requires you to implement 2 methods:
The ResourceResponse
and ResourceData
interfaces are used for data manipulation and signaling Coherent UI for either success or failure of the I/O operation.
The url
parameter of the 2 methods should be parsed to obtain the resource path. Coherent UI supplies a default parser that you can use by including URLParse.h and calling CoherentGetURLParser().Parse(...)
.
In this sample we only need reading, so the WriteFile
method is easy:
The skeleton of a ReadFile
method is illustrated as follows:
Here we assume that get_file_size is a method that retrieves the size of a specified resource and sets the flag there_was_some_error to true if there was an error while performing that operation; read_file_into is a method that reads a specified resource into a buffer in memory.
To use the custom file handler, the only thing you have to do is create an instance of the MyFileHandler class and pass it to the CreateViewContext
method:
That's it! Now resource requests with the coui:// scheme are redirected to your handler.