Skip to content

Commit 4c8b6be

Browse files
committed
Add GTK4 support
1 parent 4d3d271 commit 4c8b6be

File tree

5 files changed

+285
-30
lines changed

5 files changed

+285
-30
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55

66
# Mac OS X rubbish
77
.DS_Store
8+
9+
# CMake build directory
10+
/build/

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ set (CMAKE_CXX_STANDARD 17)
2626
add_subdirectory(src)
2727

2828
option(NFD_BUILD_TESTS "Build tests for nfd" OFF)
29-
if(${NFD_BUILD_TESTS})
29+
if(NFD_BUILD_TESTS)
3030
add_subdirectory(test)
3131
endif()

src/CMakeLists.txt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,27 @@ endif()
1212

1313
if(nfd_PLATFORM STREQUAL PLATFORM_LINUX)
1414
find_package(PkgConfig REQUIRED)
15-
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
16-
message("Using GTK version: ${GTK3_VERSION}")
15+
set(NFD_GTK_VERSION "" CACHE STRING "GTK version for Linux builds ('3' or '4')")
16+
set_property(CACHE NFD_GTK_VERSION PROPERTY STRINGS "" 3 4)
17+
# For Linux, we support both GTK3 and GTK4.
18+
# If NFD_GTK_VERSION is not explicitly set, then we take one that is available.
19+
# Otherwise, we find the version that the user wants.
20+
if(NFD_GTK_VERSION STREQUAL "")
21+
pkg_search_module(GTK REQUIRED gtk+-3.0 gtk4)
22+
if(DEFINED GTK_gtk+-3.0_VERSION)
23+
set(GTK_VERSION ${GTK_gtk+-3.0_VERSION})
24+
elseif(DEFINED GTK_gtk4_VERSION)
25+
set(GTK_VERSION ${GTK_gtk4_VERSION})
26+
endif()
27+
elseif(NFD_GTK_VERSION STREQUAL 3)
28+
pkg_check_modules(GTK REQUIRED gtk+-3.0)
29+
elseif(NFD_GTK_VERSION STREQUAL 4)
30+
pkg_check_modules(GTK REQUIRED gtk4)
31+
else()
32+
message(FATAL_ERROR "Unsupported GTK version: ${NFD_GTK_VERSION}")
33+
endif()
34+
35+
message("Using GTK version: ${GTK_VERSION}")
1736
list(APPEND SOURCE_FILES nfd_gtk.cpp)
1837
endif()
1938

@@ -32,9 +51,12 @@ target_include_directories(${TARGET_NAME}
3251

3352
if(nfd_PLATFORM STREQUAL PLATFORM_LINUX)
3453
target_include_directories(${TARGET_NAME}
35-
PRIVATE ${GTK3_INCLUDE_DIRS})
54+
PRIVATE ${GTK_INCLUDE_DIRS})
3655
target_link_libraries(${TARGET_NAME}
37-
PRIVATE ${GTK3_LIBRARIES})
56+
PRIVATE ${GTK_LIBRARIES})
57+
string(REPLACE "." ";" GTK_VERSION_LIST ${GTK_VERSION})
58+
list(GET GTK_VERSION_LIST 0 GTK_VERSION_MAJOR)
59+
target_compile_definitions(${TARGET_NAME} PUBLIC NFD_GTK_VERSION=${GTK_VERSION_MAJOR})
3860
endif()
3961

4062
if(nfd_PLATFORM STREQUAL PLATFORM_MACOS)

src/include/nfd.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ typedef char nfdnchar_t;
2828
typedef void nfdpathset_t;
2929
typedef struct {
3030
void* ptr;
31+
#if NFD_GTK_VERSION == 4
32+
unsigned int next_index;
33+
#endif
3134
} nfdpathsetenum_t;
3235

3336
typedef unsigned int nfdfiltersize_t;
@@ -43,6 +46,10 @@ typedef struct {
4346
const nfdnchar_t* spec;
4447
} nfdnfilteritem_t;
4548

49+
/* Conventions:
50+
* Output parameter (typically outPath/outPaths), if any, will not be modified if the function
51+
* returns something other than NFD_OKAY. This applies everywhere, including helper functions. */
52+
4653
/* nfd_<targetplatform>.c */
4754

4855
/* free a file path that was returned by the dialogs */

0 commit comments

Comments
 (0)