1 /*
2  * SPDX-FileCopyrightText: 2021~2021 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _GTK3_UTILS_H_
8 #define _GTK3_UTILS_H_
9 
10 #include <cairo.h>
11 #include <glib-object.h>
12 #include <memory>
13 #include <utility>
14 
15 namespace fcitx::gtk {
16 
17 bool unescape(std::string &str);
18 
19 template <auto FreeFunction>
20 struct FunctionDeleter {
21     template <typename T>
operatorFunctionDeleter22     void operator()(T *p) const {
23         if (p) {
24             FreeFunction(const_cast<std::remove_const_t<T> *>(p));
25         }
26     }
27 };
28 template <typename T, auto FreeFunction = std::free>
29 using UniqueCPtr = std::unique_ptr<T, FunctionDeleter<FreeFunction>>;
30 
31 template <typename T>
32 using GObjectUniquePtr = UniqueCPtr<T, g_object_unref>;
33 
rectContains(cairo_rectangle_int_t rect1,cairo_rectangle_int_t rect2)34 static inline bool rectContains(cairo_rectangle_int_t rect1,
35                                 cairo_rectangle_int_t rect2) {
36     return (rect1.x <= rect2.x && rect1.y <= rect2.y &&
37             rect1.x + rect1.width >= rect2.x + rect2.width &&
38             rect1.y + rect1.height >= rect2.y + rect2.height);
39 }
40 
rectContains(cairo_rectangle_int_t rect,int x,int y)41 static inline bool rectContains(cairo_rectangle_int_t rect, int x, int y) {
42     return x >= rect.x && y >= rect.y && x <= rect.x + rect.width &&
43            y <= rect.y + rect.height;
44 }
45 
46 } // namespace fcitx::gtk
47 
48 #endif // _GTK3_UTILS_H_
49