# The GTK4 adapter: a GtkGLArea whose own framebuffer the plot is rendered into
# and driven from GTK's gestures and event controllers. GTK is a C library whose
# GL widget cannot be subclassed from C++ (`G_DEFINE_TYPE` declares the type's
# instance and class structs), so the adapter is a factory that builds an instance
# and wraps it -- the shape the header documents.
#
# Unlike GLFW and ImGui -- and like Qt -- this is not a fetched dependency: GTK4
# is a system SDK, and a project that enables this target is asking for the
# installed one. Discovery is pkg-config, and a missing GTK4 fails configuration
# with the fix in the message.
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
  pkg_check_modules(GTK4_PC QUIET IMPORTED_TARGET gtk4)
endif()

if(NOT TARGET PkgConfig::GTK4_PC)
  message(FATAL_ERROR
    "plot: PLOT_BUILD_GTK4 is ON but GTK4 was not found (pkg-config gtk4). Install the "
    "GTK4 development package -- on Arch, gtk4 -- or configure with -DPLOT_BUILD_GTK4=OFF, "
    "or point PKG_CONFIG_PATH at the GTK4 .pc file.")
endif()

add_library(plot_gtk4 STATIC gtk_plot_area.cpp gtk_plot_area.hpp gtk_input.hpp)
add_library(plot::gtk4 ALIAS plot_gtk4)
# The installed tree names it plot::gtk4, like the build tree (see plot_core's).
set_target_properties(plot_gtk4 PROPERTIES EXPORT_NAME gtk4)

# The public include root is `adapters/`, so the headers are reached as
# <gtk4/gtk_plot_area.hpp> and <gtk4/gtk_input.hpp> -- the toolkit's name first,
# like every other adapter's. The build interface is that root; the install one is
# the prefix's `include`, where both are installed under `gtk4/`, the same
# spelling.
target_include_directories(plot_gtk4 PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
  $<INSTALL_INTERFACE:include>)
# GTK is PUBLIC, unlike GLFW and ImGui: the adapter's factory hands out a
# GtkWidget, so a consumer that embeds the area needs GTK's headers and libraries
# to put it anywhere -- the widget *is* the toolkit's object -- and GTK's include
# directory is not one the compiler searches by default.
target_link_libraries(plot_gtk4 PUBLIC plot::core PkgConfig::GTK4_PC)
plot_apply_warnings(plot_gtk4)

# GTK came from pkg-config, which the installed config file can re-run: the
# snippet re-creates the exact PkgConfig::GTK4_PC target the adapter's public link
# interface names, so a consumer that links plot::gtk4 gets GTK's own include
# directories and libraries the same way this build did.
plot_record_adapter_export(plot_gtk4 TRUE "GTK4 was resolved by pkg-config"
  "find_dependency(PkgConfig)\npkg_check_modules(GTK4_PC REQUIRED IMPORTED_TARGET gtk4)\n")
if(PLOT_INSTALL)
  # gtk_input.hpp is installed with the public header: the demo and the test suite
  # reach it as <gtk4/gtk_input.hpp>, and it is a toolkit-free header a consumer
  # can use without GTK.
  install(FILES gtk_plot_area.hpp gtk_input.hpp DESTINATION include/gtk4)
endif()
