GTK/C and GtkBuilder to make a single executable - gtk3

In my project I call gtk_builder_add_from_file function to load an xml file with the ui objects designed with Glade previously. So, I have my binary program and (in the same folder) the xml file.
What is the best way to pack all into a single executable? should I use a self-extracting script? or there is something else to compile all together?
Thanks to all

You can use the GResource API available in GIO. GResources work by defining the assets you wish to ship with your application inside an XML file, similar to this:
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/example/YourApp">
<file preprocess="xml-stripblanks">your-app.ui</file>
<file>some-image.png</file>
</gresource>
</gresources>
Take note of the prefix attribute, because it will be used later.
Once you've added your assets, you use the glib-compile-resources binary shipped by GLib to generate a C file that includes all your assets, encoded as byte arrays. The generated code will also use the global constructor functionality exposed by various compilers so that the resources are loaded once your application is loaded (and before main is called), or, in case of a shared object, once the library is loaded by the linker. An example of glib-compiler-resources invocation in a Makefile is:
GLIB_COMPILE_RESOURCES = $(shell $(PKGCONFIG) --variable=glib_compile_resources gio-2.0)
resources = $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=. --generate-dependencies your-app.gresource.xml
your-app-resources.c: your-app.gresource.xml $(resources)
$(GLIB_COMPILE_RESOURCES) your-app.gresource.xml --target=$0 --sourcedir=. --geneate-source
Then you have to add the your-app-resources.c to your build.
In order to access your assets you should use the from_resource() function that is exposed in various classes; for instance, to load a UI description in GtkBuilder, you should use gtk_builder_add_from_resource(). The path used is a combination of the prefix you defined in the GResource XML file and the file name, e.g.: /com/example/YourApp/your-app.ui. You can also use the resource:// URI when loading from a GFile.
You can find more information on the GResources API reference page.

Related

GSettings, glib-compile-schemas and Eclipse

I am building this Gtkmm3 application in Ubuntu and wanted to explore GSettings. All was going well while following the instructions at the 'Using GSettings' page and then it was time to configure the make files. I use Eclipse 2019-12 IDE with CDT (V9.10) and 'GNU Make Builder' as the builder. I'm totally perplexed as to how to introduce the macros listed in the GNOME page into the make files. I even tried changing the project to a 'C/C++ Autotools Project' using Eclipse but still the necessary make files to add the macros were missing. Creating a new project with GNU Autotools does create the necessary make files but I was not able to get pkg-config to work with it.
Can anyone point me to some resource which explains how to compile the schema and how & where to load the resultant binary file (externally if necessary). I'll consider myself blessed if someone has already made a Gtkmm3 C++ application with GSettings support using Eclipse IDE in Linux and can share the details.
Finally I figured. Thought I'll share my findings here. Actually some one out there had explained this for python (link below).
Using GSettings with Python/PyGObject
Creating the schema
For the developer the work starts with defining a schema for the settings. A schema is an XML file that looks something like this.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE schemalist SYSTEM "gio_gschema.dtd" >
<schemalist>
<schema id="org.gtk.skanray.emlibrary"
path="/org/skanray/emlibrary/" gettext-domain="emlibrary">
<key name="wave-pressure-ptrach-visible" type="b">
<default>true</default>
<summary>Set visibility of 'Ptrach' trace in pressure waveform.</summary>
<description>The pressure waveform shows multiple traces where 'PAW' is always enabled and additionally 'Ptrach' can be displayed. This settings affects the visibility of the trachial pressure trace shown in this waveform channel.</description></key>
</schema>
</schemalist>
The file name has to have a ‘.gschema.xml’ suffix. The schema file should be in the project path, only so that it gets pushed to SVN.
Best would be to use an XML editor (e.g. Eclipse) that supports design of XML files from a DTD file. Use following DTD file.
gschema.dtd
It is possible to store anything derived from GVariant into GSettings. Refer to following page to understand the basic types and the ‘type’ attribute to be used in the schema.
GVariant Format Strings
Compiling the schema
With the schema ready, (sudo) copy it into /usr/share/glib-2.0/schemas/ then run,
> sudo glib-compile-schemas /usr/share/glib-2.0/schemas/
At this point, the newly added settings can be seen / modified using dconf editor.
Accessing GSettings from the application
Coming to the main event of the show, this is how an application can read ( and / or write) settings. It is not necessary that one needs to bind property of an object to a ‘key’ in GSettings, it may be queried and used as well. Refer to GSettings API reference for details.
Glib::RefPtr <Gio::Settings> refSettings = Gio::Settings::create(“org.gtk.skanray.emlibrary”);
CLineTrace * pTrace = NULL; // CLineTrace is derived from Gtk::Widget
…
pTrace = …
…
if(refSettings)
{
refSettings->bind("wave-pressure-ptrach-visible",
pTrace,
"visible",
Gio::SETTINGS_BIND_DEFAULT);
}
Now you can fire up dconf editor and test the settings.
NOTE
Bindings are usually preferred to be made in class constructors. However binding to ‘visible’ property of a widget could be a bit tricky. Typically the top level window does a show_all() as the last line in its constructor. However constructors of the children of top level window would have completed executing including making the bindings. If there were settings that had stored ‘visibility’ as false then the top level window’s call to show_all() would mess up with that setting. In such cases it is advised to perform the bind one time in the on_map() handler of the respective class.

Why does DITA Open Toolkit PDF plugin rename image href attributes?

I'm sorry if this doesn't have enough information. I don't typically ask for help online like this.
I'm using DITA Open Toolkit 3.4 on Windows. I generated a plugin called "vcr2" using Jarno's (very excellent and helpful) PDF Plugin Generator and then made a handful of customizations. The plugin uses the pdf2 plugin as a base. When I try to use the vcr2 plugin, my images are not working. I've tracked the problem down to malformed image filenames in the image's href attribute.
For example:
In my source file (a DITA Task), the markup for one of my images looks like this:
<image href="MyRemindersChooseReminder.png"/>
If I run a transform with the pdf2 plugin, the images work fine. In the merged stage1.xml file in the Temp folder, the XML for that same image looks like this:
<image class="- topic/image " href="df2d132af27436c59c5c8c4282e112d62bec8201.png" placement="inline" xtrc="image:1;10:66" xtrf="file:/V:/Vasont/Extract/t12340879-minimal/t12340879.xml"/>
It is processed into a file Topic.fo, and looks like this:
<fo:external-graphic
 src="url('file:/V:/Vasont/Extract/t12340879-minimal/MyRemindersChooseReminder.png')"/>
Everything works fine and the image looks fine.
If I run the same file through my 'vcr2' plugin, which just calls the same pdf2 plugin with some overrides, all the images get broken:
stage1.xml
<image class="- topic/image " href="df2d132af27436c59c5c8c4282e112d62bec8201.png" placement="inline" xtrc="image:1;10:66" xtrf="file:/V:/Vasont/Extract/t12340879-minimal/t12340879.xml"/>
Topic.fo
<fo:external-graphic
 src="url('file:/V:/Vasont/Extract/t12340879-minimal/df2d132af27436c59c5c8c4282e112d62bec8201.png')"
/>
As I track this down further, it appears that somewhere in the map-reader Ant task, this filename gets changed to that cryptic string of pseudo-hexadecimal. I think later on it's supposed to be changed back or resolved to a complete URI or something.
So, the two-part question is: Why does Open Toolkit change my filenames, and what's supposed to change them back?
DITA-OT's preprocess uses hashes for temporary filenames because it allows the code to not deal with directory structures. This enables preprocess to work in so-called "map-first" mode, where it first processes all DITA map resources and only then starts to process DITA topic and image resources.
The preprocess has a step called clean-preprocess that can rewrite the temporary file names to match source resource files names. However, this rewrite operation is disabled for PDF output because the original file names are not used for anything in that output type.

"How to embed resources" or "How to access a Resource"

I am struggling with embedded resources or resources in general with Dynamics365. My goal is to add a xml-file as resource to a model and use that resource in some testcode.
I tried to add the xml as resource-element but it seems this does not embedd the xml into the compiled dll so i don't know how to pick up that xml-file in my testcode. Currently my testcode loads the xml from "C:\Temp\test.xml" where i copied my xml to, but thats not a viable solution and i thought adding the xml as resource would be ok. Or is there a better approach to this scenario ?
You can use class SysResource to interact with resources. I used the following code in one of my unit tests to load the content of a file resource into a file and create a CommaStreamIo instance from that file. You should be able to modify that to do your stuff with an xml file.
ResourceNode textFileResourceNode = SysResource::getResourceNode(resourceStr(MyTextFileResourceName));
str textFilename = SysResource::saveToTempFile(textFileResourceNode);
CommaStreamIo commaStreamIo = CommaStreamIo::constructForRead(File::UseFileFromURL(textFilename));
Also take a look at reading a resource into a string.
You could also take a look at how some of the standard resources are used. For example, there are several .xslt file resources that are used to transform bank statement formats.

"Two output file names resolved to the same output path" error when nesting more than one .resx file within form in .NET application

I have a Windows Forms .NET application in Visual Studio. Making a form "Localizable" adds a Form1.resx file nested below the form. I also want to add a separate .resx file for each form (Form1Resources.resx). This is used for custom form-specific resources, e.g. messages generated using the code behind.
This is set up as follows:
It would be tidier to nest the custom .resx file beneath the form (see this question for details about nest how to do this), as follows:
However, this results in the following error when I build the application:
Two output file names resolved to the same output path:
"obj\Debug\WindowsFormsApp1.Form1.resources" WindowsFormsApp1
I'm guessing that MSBuild uses some logic to find nested .resx files and generate .resources file based on its parent. Is there any way that this can be resolved?
Note that it is not possible to add custom messages to the Form1.resx file - this is for design-specific resources only and any resources that you add get overwritten when you save changes in design mode.
The error comes from the GenerateResource task because the 2 resx files (EmbeddedResource items in msbuild) passed both have the same ManifestResourceName metadata value. That values gets created by the CreateManifestResourceNames task and assumingly when it sees an EmbeddedResource which has the DependentUpon metadata set (to Form1.cs in your case) it always generates something of the form '$(RootNamespace).%(DependentUpon)': both your resx files end up with WindowsFormsApp1.Form1 as ManifestResourceName. Which could arguably be treated as the reason why having all resx files under Form1 is not tidier: it's not meant for it, requires extra fiddling, moreover it could be confusing for others since they'd typcially expect to contain the resx fils placed beneath a form to contain what it always does.
Anyway: there's at least 2 ways to work around this:
there's a Target called CreateCustomManifestResourceNames which is meant to be used for custom ManifestResourceName creation. A bit too much work for your case probably, just mentioning it for completeness
manually declare a ManifestResourceName yourself which doesn't clash with the other(s); if the metadata is already present it won't get overwritten by
Generic code sample:
<EmbeddedResource Include="Form1Resources.resx">
<DependentUpon>Form1.cs</DependentUpon>
<ManifestResourceName>$(RootNamespace).%(FileName)</ManifestResourceName>
...
</EmbeddedResource>

How to check if a file is already included?

I have a procedure structure like this:
<someprocedure.p>
<randomCode>
<INCLUDE standardIncludeFile.i>
</someprocedure>
The standardIncludeFile.i-include file can be used with any procedure files. However, it requires other include files to work. F.ex. stantarderror.i and standardconstants.i.
If the someprocedure.p already has included these 2 files, they shouldn't be included in the standardIncludeFile.i. If they're not, they should be included in the standardIncludeFile.i.
Can I use DEFINED inside the standardIncludeFile.i to check if those .i-files are already included in the someprocedure.p?
If I use INCLUDE anyway without any conditions, the Eclipse Open-Edge editor gives me the setting to include once, but I'm not sure if that's a good way. The files are compiled on the server for production, anyway.
Currently stantarderror.i or standardconstants.i do not contain any GLOBAL-DEFINED constants, so I cannot check them that way with DEFINED.
Within your incldue file, do something like this
&IF DEFINED (stantarderror) EQ 0 &THEN
GLOBAL-DEFINE stantarderror stantarderror
// actual code here
&ENDIF