DrOli
DrOli

Reputation: 1083

Gtk absolute/relative/ or "no" path (Windows)

Using Gtk+, we introduce some of the icons into the app via gtk_image_new_from_file(). We found that if the icon file is directly in the apps Dir, then it all works well with "no path", eg.

FString255 = "Icon_Charts.png"
IconImage_Ptr = gtk_image_new_from_file( Trim(FString255)//c_Null_Char )

However, when we tried to move the icons to a sub-Dir (what a surprise, called "Icons"), we could not get Gtk to recognise the png's. We tried every permutation we could think of using absolute and relative variations (e.g. with ".\" with "./", with "double slashes", with "C:.....\Icons..." etc.) ... no joy.

Does anybody know the syntax Gtk expects for relative path, e.g. something like:

FString255 = ".\Icons\Icon_Charts.png"

???

Or perhaps is there something "special" about gtk_image_new_from_file() and perhaps it can ONLY accept "no path" file-names?

We get the feeling it must be something super simple that we missed.

Upvotes: 1

Views: 605

Answers (2)

DrOli
DrOli

Reputation: 1083

OK, sussed it, and we are feeling extremely stupid and embarrassed. As our OP suspected:

" We get the feeling it must be something super simple that we missed."

... well, it was :-(.

Before the "actual answer", TingPing's answer would be a possibility, if the real issue was not something else entirely. However, even then, if we were to go that route, we would use something like (keeping our submissions "Fortran-consistent" with the OP):

Temp_cPtr = g_get_current_dir_utf8 ()
!
If( c_Associated(Temp_cPtr) ) Then
    !
    !
    n = c_StrLen( Temp_cPtr )
    !
    FString255Path = ""
    !
    Call C_F_String( Temp_cPtr, FString255Path(1:n) )
    !
End if
!
!
FString255 = FString255Path(1:n)//"\Icons\Icon_Charts.png"
IconImage_Ptr(j) = gtk_image_new_from_file( Trim(FString255)//c_Null_Char )

or set "\Icons" as var and adjust the path variable at the outset, eg.

FString255Path = FString255Path(1:n)//"\Icons\"
FString255 = Trim(FString255Path)//"Icon_Charts.png"

... yes, we could have used Allocatable strings also, there is a reason why we used fixed len strings here.

In the event, the usual "simple" thing does actually work, for example, the desired relative path approach really is, as initially thought:

FString255 = ".\Icons\Icon_Charts.png"
IconImage_Ptr(j) = gtk_image_new_from_file( Trim(FString255)//c_Null_Char )

OK, now for the "actual answer", and our "monument to stupidity du jour"; in fact this Gtk app is quite large, with some parts of the front end created with Glade and "builder", while other parts are written explicitly in code. Some of the same icons are used by both the Glade/builder bits, as well as the explicit code bits. As it happens, we had used the correct (relative) path at the outset ... unfortunately, we looked for the results in a part of the GUI that is generated by the Glade/builder bits, and which of course have their own independent mechanism for loading icons (even if the same icons are re-used in the "code"), and so no amount of fiddling with the "code/paths" would make any difference there.

... this is rather a "bush-league" mistake on our part, and would feel better if the entire question/post was deleted ... but perhaps we should "honour our monument to stupidity" :-).

... our apologies for wasting anybody's time.

Upvotes: 0

TingPing
TingPing

Reputation: 2302

To avoid any non-obvious behavior you should always use absolute paths.

GLib provides the g_win32_get_package_installation_directory_of_module() function to allow getting the path of the current project (assuming standard directory layout). For example:

char *path, *package_dir;

package_dir = g_win32_get_package_installation_directory_of_module (NULL);
g_assert (package_dir != NULL);
path = g_build_filename (package_dir, "Icons", "Icon_Charts.png", NULL);

g_free (package_dir);

Upvotes: 1

Related Questions