Minsky
getContext.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2021
3  @author Russell Standish
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 
21 #include "getContext.h"
22 #include "windowInformation.h"
23 #include <Appkit/NSGraphicsContext.h>
24 #include <Appkit/NSWindow.h>
25 #include <AppKit/NSView.h>
26 #include <cairo/cairo-quartz.h>
27 #include <iostream>
28 #include <exception>
29 #include "minsky_epilogue.h"
30 
31 using namespace std;
32 
33 @interface CairoView: NSView
34 {
36 }
37 -(void) drawRect: (NSRect)rect;
38 // required to prevent the subview from stealing mouse events
39 -(NSView*) hitTest: (NSPoint)aPoint;
40 -(void) setWinfo: (minsky::WindowInformation*)winfo;
41 @end
42 
43 namespace minsky
44 {
45 
46  struct ViewImpl
47  {
49  ViewImpl(): cairoView([[CairoView alloc] init]) {[cairoView retain];}
50  ~ViewImpl() {[cairoView removeFromSuperview]; [cairoView release];}
51  ViewImpl(const ViewImpl&)=delete;
52  void operator=(const ViewImpl&)=delete;
53  };
54 
55  NSContext::NSContext(NSContext&&)=default;
56 
57 
58  namespace
59  {
60  struct SetSignal
61  {
62  sig_t segvHandler;
63  SetSignal() {segvHandler=signal(SIGSEGV,signal_caught);}
64  ~SetSignal() {signal(SIGSEGV,segvHandler);}
65  static void signal_caught(int) {
66  throw runtime_error("Different process space not supported on MacOSX");
67  }
68  };
69  }
70 
71  NSContext::NSContext(void* nativeHandle,int xoffs,int yoffs,int width,int height,
72  WindowInformation& winfo)
73 
74  {
75  NSView* view=reinterpret_cast<NSView*>(nativeHandle);
76  // check that we are passed a valid NSView pointer (must be an in-process call).
77  @try {
78  SetSignal sigHandler;
79  [view class];
80  }
81  @catch (NSException*)
82  {
83  throw runtime_error("Different process space not supported on MacOSX");
84  }
85 
86  impl=make_unique<ViewImpl>();
87 
88  // do not overwrite scrollbar
89  if (winfo.hasScrollBars)
90  {
91  width-=20;
92  }
93  [impl->cairoView setFrameSize: NSMakeSize(width,height)];
94  [impl->cairoView setWinfo: &winfo];
95  [view addSubview: impl->cairoView];
96  }
97 
98  NSContext::~NSContext()=default;
99 
100  void NSContext::requestRedraw()
101  {
102  [impl->cairoView setNeedsDisplay: true];
103  }
104 
105 }
106 
107 @implementation CairoView
108 -(void) drawRect: (NSRect)rect
109 {
110  auto context = [[NSGraphicsContext currentContext] CGContext];
111  auto frame=[self frame];
112  CGContextTranslateCTM(context,winfo->offsetLeft,winfo->childHeight+(winfo->hasScrollBars?20:0));
113  CGContextScaleCTM(context,1,-1); //CoreGraphics's y dimension is opposite to everybody else's
114  winfo->bufferSurface=make_shared<ecolab::cairo::Surface>(cairo_quartz_surface_create_for_cg_context(context, NSWidth(frame), NSHeight(frame)));
115  if (winfo->hasScrollBars)
116  cairo_surface_set_device_offset(winfo->bufferSurface->surface(), 0, 20);
117  winfo->draw();
118  winfo->bufferSurface.reset();
119 }
120 - (NSView *) hitTest: (NSPoint) aPoint
121 {
122  return nil;
123 }
124 
125 -(void) setWinfo: (minsky::WindowInformation*)w
126 {
127  winfo=w;
128 }
129 @end
ecolab::cairo::SurfacePtr bufferSurface
CairoView * cairoView
Definition: getContext.cc:48
STL namespace.
#define SIGSEGV
Definition: tclmain.cc:50
struct TCLcmd::trap::init_t init
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
minsky::WindowInformation * winfo
Definition: getContext.cc:35
std::unique_ptr< ViewImpl > impl
Definition: getContext.h:33
std::function< void()> draw