Scroom 0.14-49-gb7ae7a6d
Loading...
Searching...
No Matches
gtk-helpers.hh
Go to the documentation of this file.
1/*
2 * Scroom - Generic viewer for 2D data
3 * Copyright (C) 2009-2026 Kees-Jan Dijkzeul
4 *
5 * SPDX-License-Identifier: LGPL-2.1
6 */
7
8#pragma once
9
10#include <future>
11#include <ostream>
12
13#include <boost/function.hpp>
14
15#include <gtk/gtk.h>
16
17#include "assertions.hh"
18
20{
21 namespace Detail
22 {
23 template <typename T>
24 static int gtkWrapper(gpointer data)
25 {
26 auto* w = reinterpret_cast<T*>(data);
27 (*w)();
28 delete w;
29 return false;
30 }
31 } // namespace Detail
32
33 template <typename T>
34 std::pair<GSourceFunc, gpointer> wrap(T f)
35 {
36 return std::make_pair<GSourceFunc, gpointer>(Detail::gtkWrapper<T>, new T(std::move(f)));
37 }
38
39 bool on_ui_thread();
40
41 template <typename T>
43 {
44 const auto [function, data] = wrap(std::move(f));
45 gdk_threads_add_idle(function, data);
46 }
47
48 template <typename T>
50 {
52 std::packaged_task<void(void)> t(std::move(f));
53 std::future<void> const future = t.get_future();
54 async_on_ui_thread(std::move(t));
55 future.wait();
56 }
57
58 template <typename T>
60 {
61 if(on_ui_thread())
62 {
63 f();
64 }
65 else
66 {
68 }
69 }
70
71 inline cairo_rectangle_int_t createCairoIntRectangle(int x, int y, int width, int height)
72 {
73 cairo_rectangle_int_t rect;
74 rect.x = x;
75 rect.y = y;
76 rect.width = width;
77 rect.height = height;
78 return rect;
79 }
80
81 GtkWindow* get_parent_window(GtkWidget* widget);
82
83} // namespace Scroom::GtkHelpers
84
85inline bool operator==(cairo_rectangle_int_t const& left, cairo_rectangle_int_t const& right)
86{
87 return left.x == right.x && left.y == right.y && left.width == right.width && left.height == right.height;
88}
89
90inline bool operator==(const GdkPoint& left, const GdkPoint& right) { return left.x == right.x && left.y == right.y; }
91
92std::ostream& operator<<(std::ostream& os, cairo_rectangle_int_t const& r);
93std::ostream& operator<<(std::ostream& os, const GdkPoint& p);
#define require(expr)
Definition assertions.hh:30
uint8_t data
Definition blob-tests.cc:36
PresentationInterfaceStub::Ptr const p
Definition determine-size-test.cc:172
() void(clear(nullptr)+(5 *clear(nullptr)) *5)
bool operator==(cairo_rectangle_int_t const &left, cairo_rectangle_int_t const &right)
Definition gtk-helpers.hh:85
std::ostream & operator<<(std::ostream &os, cairo_rectangle_int_t const &r)
Definition gtk-helpers.cc:29
f
Definition gtkhelper-tests.cc:43
std::pair< GSourceFunc, gpointer > const w
Definition gtkhelper-tests.cc:42
Definition color.hh:26
static int gtkWrapper(gpointer data)
Definition gtk-helpers.hh:24
Definition gtk-helpers.hh:20
bool on_ui_thread()
Definition gtk-helpers.cc:15
void async_on_ui_thread_and_wait(T f)
Definition gtk-helpers.hh:49
void sync_on_ui_thread(T f)
Definition gtk-helpers.hh:59
cairo_rectangle_int_t createCairoIntRectangle(int x, int y, int width, int height)
Definition gtk-helpers.hh:71
GtkWindow * get_parent_window(GtkWidget *widget)
Definition gtk-helpers.cc:17
void async_on_ui_thread(T f)
Definition gtk-helpers.hh:42
std::pair< GSourceFunc, gpointer > wrap(T f)
Definition gtk-helpers.hh:34
Segment< int > const right(3, 7)
const auto rect
Definition rectangletests.cc:335
ThreadPool t(0)
Scroom::Utils::Rectangle< double > const r
Definition transformpresentation_test.cc:65