Minsky: 3.17.0
windowInformation.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2021
3  @author Janak Porwal
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef WINDOW_INFORMATION_H
21 #define WINDOW_INFORMATION_H
22 
23 #include <cairoSurfaceImage.h>
24 #if defined(CAIRO_HAS_WIN32_SURFACE) && !defined(__CYGWIN__)
25 #define USE_WIN32_SURFACE
26 #include <windows.h>
27 #include <wingdi.h>
28 #elif defined(MAC_OSX_TK)
29 #include "getContext.h"
30 #elif defined(CAIRO_HAS_XLIB_SURFACE)
31 #define USE_X11
32 #include <cairo/cairo-xlib.h>
33 #include <X11/Xlib.h>
34 #endif
35 
36 #include <atomic>
37 #include <functional>
38 #include <mutex>
39 #include <future>
40 #include <thread>
41 
42 namespace minsky
43 {
44  struct Winfo
45  {
46 #ifdef USE_WIN32_SURFACE
47  HWND parentWindowId, childWindowId;
48  HBITMAP hbmMem; // backing buffer pixmap
49  HDC hdcMem; // backing buffer bitmap device context
50  HANDLE hOld; //
51 #elif defined(MAC_OSX_TK)
52  NSContext nsContext;
53  std::shared_ptr<std::lock_guard<std::mutex>> lock;
54  Winfo(NSContext&& nsContext): nsContext(std::move(nsContext)) {}
55 #elif defined(USE_X11)
56  Window parentWindowId;
57  Window childWindowId, bufferWindowId;
58 
59  Display* display; // Weak reference, returned by system
60  GC graphicsContext;
61  XWindowAttributes wAttr;
62 #endif
66  int offsetTop;
67  };
68 
69  class WindowInformation: public Winfo
70  {
71  friend class RenderNativeWindow;
72 #if defined(MAC_OSX_TK)
73  friend struct NSContext;
74 #elif defined(USE_X11)
75  struct EventThread: public Winfo, public std::thread
76  {
77  void run();
78  EventThread(const Winfo& w): Winfo(w), thread([this]{run();}) {}
79  ~EventThread() {XDestroyWindow(display, childWindowId); join();}
80  };
81 
82  std::unique_ptr<EventThread> eventThread;
83 #endif
84 
85  public:
86 
87  const bool hasScrollBars=false;
88  ecolab::cairo::SurfacePtr bufferSurface;
89  std::function<void()> draw;
90  void copyBufferToMain();
91 
92  public:
94  // TODO refactor all these classes to avoid this dependency inversion
95  WindowInformation(uint64_t parentWin, int left, int top, int cWidth, int cHeight,double scalingFactor,bool hasScrollBars,const std::function<void()>& draw);
96 
97  const ecolab::cairo::SurfacePtr& getBufferSurface() const;
98  void requestRedraw();
99 
100  WindowInformation(const WindowInformation&)=delete;
101  void operator=(const WindowInformation&)=delete;
102  };
103 } // namespace minsky
104 
105 #endif
const ecolab::cairo::SurfacePtr & getBufferSurface() const
ecolab::cairo::SurfacePtr bufferSurface
WindowInformation(uint64_t parentWin, int left, int top, int cWidth, int cHeight, double scalingFactor, bool hasScrollBars, const std::function< void()> &draw)
void operator=(const WindowInformation &)=delete
std::function< void()> draw