Scroom  0.14
gtk-helpers.hh
Go to the documentation of this file.
1 /*
2  * Scroom - Generic viewer for 2D data
3  * Copyright (C) 2009-2022 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(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  {
67  async_on_ui_thread_and_wait(std::move(f));
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 
85 inline 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 
90 inline bool operator==(const GdkPoint& left, const GdkPoint& right) { return left.x == right.x && left.y == right.y; }
91 
92 std::ostream& operator<<(std::ostream& os, cairo_rectangle_int_t const& r);
93 std::ostream& operator<<(std::ostream& os, const GdkPoint& p);
assertions.hh
Scroom::GtkHelpers::async_on_ui_thread
void async_on_ui_thread(T f)
Definition: gtk-helpers.hh:42
Scroom::GtkHelpers::Detail::gtkWrapper
static int gtkWrapper(gpointer data)
Definition: gtk-helpers.hh:24
Scroom::GtkHelpers::get_parent_window
GtkWindow * get_parent_window(GtkWidget *widget)
Definition: gtk-helpers.cc:17
Scroom::GtkHelpers
Definition: gtk-helpers.hh:19
require
#define require(expr)
Definition: assertions.hh:28
Scroom::GtkHelpers::sync_on_ui_thread
void sync_on_ui_thread(T f)
Definition: gtk-helpers.hh:59
Scroom::GtkHelpers::async_on_ui_thread_and_wait
void async_on_ui_thread_and_wait(T f)
Definition: gtk-helpers.hh:49
Scroom::GtkHelpers::on_ui_thread
bool on_ui_thread()
Definition: gtk-helpers.cc:15
operator==
bool operator==(cairo_rectangle_int_t const &left, cairo_rectangle_int_t const &right)
Definition: gtk-helpers.hh:85
Scroom::GtkHelpers::wrap
std::pair< GSourceFunc, gpointer > wrap(T f)
Definition: gtk-helpers.hh:34
Detail
Definition: async-deleter.hh:12
operator<<
std::ostream & operator<<(std::ostream &os, cairo_rectangle_int_t const &r)
Definition: rectangle.hh:309
Scroom::GtkHelpers::createCairoIntRectangle
cairo_rectangle_int_t createCairoIntRectangle(int x, int y, int width, int height)
Definition: gtk-helpers.hh:71