Merge pull request #3916 from techee/symtree_icons
[geany-mirror.git] / scintilla / gtk / Wrappers.h
bloba22605a0300f0ee231cd1ef1a38c419a818a6e0b
1 // Scintilla source code edit control
2 // Wrappers.h - Encapsulation of GLib, GObject, Pango, Cairo, GTK, and GDK types
3 // Copyright 2022 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
6 #ifndef WRAPPERS_H
7 #define WRAPPERS_H
9 namespace Scintilla::Internal {
11 // GLib
13 struct GFreeReleaser {
14 template <class T>
15 void operator()(T *object) noexcept {
16 g_free(object);
20 using UniqueStr = std::unique_ptr<gchar, GFreeReleaser>;
22 // GObject
24 struct GObjectReleaser {
25 // Called by unique_ptr to destroy/free the object
26 template <class T>
27 void operator()(T *object) noexcept {
28 g_object_unref(object);
32 // Pango
34 using UniquePangoContext = std::unique_ptr<PangoContext, GObjectReleaser>;
35 using UniquePangoLayout = std::unique_ptr<PangoLayout, GObjectReleaser>;
36 using UniquePangoFontMap = std::unique_ptr<PangoFontMap, GObjectReleaser>;
38 struct FontDescriptionReleaser {
39 void operator()(PangoFontDescription *fontDescription) noexcept {
40 pango_font_description_free(fontDescription);
44 using UniquePangoFontDescription = std::unique_ptr<PangoFontDescription, FontDescriptionReleaser>;
46 struct FontMetricsReleaser {
47 void operator()(PangoFontMetrics *metrics) noexcept {
48 pango_font_metrics_unref(metrics);
52 using UniquePangoFontMetrics = std::unique_ptr<PangoFontMetrics, FontMetricsReleaser>;
54 struct LayoutIterReleaser {
55 // Called by unique_ptr to destroy/free the object
56 void operator()(PangoLayoutIter *iter) noexcept {
57 pango_layout_iter_free(iter);
61 using UniquePangoLayoutIter = std::unique_ptr<PangoLayoutIter, LayoutIterReleaser>;
63 // Cairo
65 struct CairoReleaser {
66 void operator()(cairo_t *context) noexcept {
67 cairo_destroy(context);
71 using UniqueCairo = std::unique_ptr<cairo_t, CairoReleaser>;
73 struct CairoSurfaceReleaser {
74 void operator()(cairo_surface_t *psurf) noexcept {
75 cairo_surface_destroy(psurf);
79 using UniqueCairoSurface = std::unique_ptr<cairo_surface_t, CairoSurfaceReleaser>;
81 // GTK
83 using UniqueIMContext = std::unique_ptr<GtkIMContext, GObjectReleaser>;
85 // GDK
87 struct GdkEventReleaser {
88 void operator()(GdkEvent *ev) noexcept {
89 gdk_event_free(ev);
93 using UniqueGdkEvent = std::unique_ptr<GdkEvent, GdkEventReleaser>;
95 inline void UnRefCursor(GdkCursor *cursor) noexcept {
96 #if GTK_CHECK_VERSION(3,0,0)
97 g_object_unref(cursor);
98 #else
99 gdk_cursor_unref(cursor);
100 #endif
103 [[nodiscard]] inline GdkWindow *WindowFromWidget(GtkWidget *w) noexcept {
104 return gtk_widget_get_window(w);
109 #endif