OpenClonk
C4WindowMac.mm
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #define GL_SILENCE_DEPRECATION
17 #include "C4Include.h"
18 #include "graphics/C4DrawGL.h"
19 #include "platform/C4Window.h"
20 #include "C4Version.h"
21 #include "game/C4Application.h"
22 #include "lib/C4Rect.h"
23 #include "game/C4FullScreen.h"
24 #include "editor/C4Console.h"
26 
27 #import <AppKit/AppKit.h>
29 #import "graphics/C4DrawGLMac.h"
30 
31 #ifdef USE_COCOA
32 
33 #define ctrler (this->objectiveCObject<C4WindowController>())
34 
36  Active(false),
37  pSurface(0)
38 {}
39 
41 
42 #ifndef WITH_QT_EDITOR
43 static NSString* windowXibNameForWindowKind(C4Window::WindowKind kind)
44 {
45  switch (kind)
46  {
49  return @"EditorGUIWindow";
51  return @"FullScreen";
53  return @"EditorViewport";
54  default:
55  return nil;
56  }
57 }
58 #endif
59 
60 C4Window * C4Window::Init(C4Window::WindowKind windowKind, C4AbstractApp * pApp, const char * Title, const C4Rect * size)
61 {
62  Active = true;
63 
64 #ifdef WITH_QT_EDITOR
65  // embed into editor: Viewport widget creation handled by C4ConsoleQt
66  ::Console.AddViewport(static_cast<C4ViewportWindow *>(this));
67  return this;
68 #else
69 
70  // Create window
71  C4WindowController* controller = [C4WindowController new];
72  setObjectiveCObject(controller);
73  [NSBundle loadNibNamed:windowXibNameForWindowKind(windowKind) owner:controller];
74  [controller setStdWindow:this];
75  if (windowKind != W_GuiWindow && windowKind != W_Console)
76  {
77  if (lionAndBeyond())
78  [controller.window setCollectionBehavior:[controller.window collectionBehavior] | NSWindowCollectionBehaviorFullScreenPrimary];
79  }
80  SetTitle(Title);
81  eKind = windowKind;
82  return this;
83 #endif
84 }
85 
86 void C4Window::Clear()
87 {
88  // Destroy window
89  C4WindowController* controller = ctrler;
90  if (controller)
91  {
92  [controller.openGLView setNeedsDisplay:NO];
93  [controller.openGLView removeFromSuperview];
94  [controller setStdWindow:NULL];
95  [controller close];
96  setObjectiveCObject(nil);
97  }
98 }
99 
100 bool C4Window::StorePosition(const char *szWindowName, const char *szSubKey, bool fStoreSize)
101 {
102  [ctrler setWindowFrameAutosaveName:[NSString stringWithFormat:@"%s_%s", szWindowName, szSubKey]];
103  return true;
104 }
105 
106 bool C4Window::RestorePosition(const char *szWindowName, const char *szSubKey, bool fHidden)
107 {
108  StorePosition(szWindowName, szSubKey, true);
109  if (fHidden)
110  [ctrler.window orderOut:ctrler];
111  else
112  [ctrler.window orderFront:ctrler];
113  return true;
114 }
115 
116 void C4Window::SetTitle(const char *szToTitle)
117 {
118  C4WindowController* controller;
119  if ((controller = ctrler) && controller.window)
120  [controller.window setTitle:[NSString stringWithUTF8String:szToTitle ? szToTitle : ""]];
121 }
122 
123 bool C4Window::GetSize(C4Rect * pRect)
124 {
125  if (this == &::FullScreen)
126  {
127  pRect->x = 0;
128  pRect->y = 0;
129  pRect->Wdt = ActualFullscreenX;
130  pRect->Hgt = ActualFullscreenY;
131  }
132  else
133  {
134  C4WindowController* controller = ctrler;
135  NSView* view = controller.openGLView ? controller.openGLView : controller.window.contentView;
136  NSRect r = [view frame];
137  pRect->x = 0;
138  pRect->y = 0;
139  pRect->Wdt = r.size.width;
140  pRect->Hgt = r.size.height;
141  }
142  return true;
143 }
144 
145 void C4Window::SetSize(unsigned int cx, unsigned int cy)
146 {
147  C4WindowController* controller = ctrler;
148  [controller setContentSize:NSMakeSize(cx, cy)];
149 }
150 
151 void C4Window::GrabMouse(bool grab)
152 {
153  // TODO
154 }
155 
157 {
158  [ctrler.openGLView display];
159 }
160 
162 {
163  return true;
164 }
165 
166 C4KeyCode K_SHIFT_L = 56 + CocoaKeycodeOffset;
167 C4KeyCode K_SHIFT_R = 60 + CocoaKeycodeOffset;
168 C4KeyCode K_CONTROL_L = 0x3b + CocoaKeycodeOffset;
169 C4KeyCode K_CONTROL_R = 0x3e + CocoaKeycodeOffset;
170 C4KeyCode K_ALT_L = 58 + CocoaKeycodeOffset;
171 C4KeyCode K_ALT_R = 61 + CocoaKeycodeOffset;
172 C4KeyCode K_COMMAND_L = 55 + CocoaKeycodeOffset;
173 C4KeyCode K_COMMAND_R = 54 + CocoaKeycodeOffset;
174 C4KeyCode K_F1 = 122 + CocoaKeycodeOffset;
175 C4KeyCode K_F2 = 120 + CocoaKeycodeOffset;
176 C4KeyCode K_F3 = 99 + CocoaKeycodeOffset;
177 C4KeyCode K_F4 = 118 + CocoaKeycodeOffset;
178 C4KeyCode K_F5 = 96 + CocoaKeycodeOffset;
179 C4KeyCode K_F6 = 97 + CocoaKeycodeOffset;
180 C4KeyCode K_F7 = 98 + CocoaKeycodeOffset;
181 C4KeyCode K_F8 = 100 + CocoaKeycodeOffset;
182 C4KeyCode K_F9 = 101 + CocoaKeycodeOffset;
183 C4KeyCode K_F10 = 109 + CocoaKeycodeOffset;
184 C4KeyCode K_F11 = 103 + CocoaKeycodeOffset;
185 C4KeyCode K_F12 = 111 + CocoaKeycodeOffset;
186 C4KeyCode K_ADD = 69 + CocoaKeycodeOffset;
187 C4KeyCode K_SUBTRACT = 78 + CocoaKeycodeOffset;
188 C4KeyCode K_MULTIPLY = 67 + CocoaKeycodeOffset;
189 C4KeyCode K_ESCAPE = 53 + CocoaKeycodeOffset;
190 C4KeyCode K_PAUSE = NSPauseFunctionKey + CocoaKeycodeOffset;
191 C4KeyCode K_TAB = 48 + CocoaKeycodeOffset;
192 C4KeyCode K_RETURN = 36 + CocoaKeycodeOffset;
193 C4KeyCode K_DELETE = 117 + CocoaKeycodeOffset;
194 C4KeyCode K_INSERT = 125125125 + CocoaKeycodeOffset;
195 C4KeyCode K_BACK = 51 + CocoaKeycodeOffset;
196 C4KeyCode K_SPACE = 49 + CocoaKeycodeOffset;
197 C4KeyCode K_UP = 126 + CocoaKeycodeOffset;
198 C4KeyCode K_DOWN = 125 + CocoaKeycodeOffset;
199 C4KeyCode K_LEFT = 123 + CocoaKeycodeOffset;
200 C4KeyCode K_RIGHT = 124 + CocoaKeycodeOffset;
201 C4KeyCode K_HOME = 115 + CocoaKeycodeOffset;
202 C4KeyCode K_END = 119 + CocoaKeycodeOffset;
203 C4KeyCode K_SCROLL = 1000 + CocoaKeycodeOffset;
204 C4KeyCode K_MENU = 1000 + CocoaKeycodeOffset;
205 C4KeyCode K_PAGEUP = 116 + CocoaKeycodeOffset;
206 C4KeyCode K_PAGEDOWN = 121 + CocoaKeycodeOffset;
207 C4KeyCode K_M = 46 + CocoaKeycodeOffset;
208 C4KeyCode K_T = 17 + CocoaKeycodeOffset;
209 C4KeyCode K_W = 13 + CocoaKeycodeOffset;
210 C4KeyCode K_I = 34 + CocoaKeycodeOffset;
211 C4KeyCode K_C = 8 + CocoaKeycodeOffset;
212 C4KeyCode K_V = 9 + CocoaKeycodeOffset;
213 C4KeyCode K_X = 7 + CocoaKeycodeOffset;
214 C4KeyCode K_A = 0 + CocoaKeycodeOffset;
215 C4KeyCode K_S = 1 + CocoaKeycodeOffset;
216 
217 // FIXME
218 C4KeyCode K_PRINT = -100;
219 C4KeyCode K_CENTER = -101;
220 C4KeyCode K_NUM = -102;
221 
222 int MK_SHIFT = NSEventModifierFlagShift;
223 int MK_CONTROL = NSEventModifierFlagControl;
224 int MK_ALT = NSEventModifierFlagOption;
225 
226 #endif
C4Console Console
Definition: C4Globals.cpp:45
C4FullScreen FullScreen
Definition: C4Globals.cpp:46
unsigned long C4KeyCode
bool lionAndBeyond()
void AddViewport(C4ViewportWindow *cvp)
Definition: C4ConsoleGUI.h:110
Definition: C4Rect.h:28
int32_t y
Definition: C4Rect.h:30
int32_t Hgt
Definition: C4Rect.h:30
int32_t Wdt
Definition: C4Rect.h:30
int32_t x
Definition: C4Rect.h:30
virtual C4Window * Init(WindowKind windowKind, C4AbstractApp *pApp, const char *Title, const C4Rect *size)
Definition: C4AppT.cpp:109
bool GetSize(C4Rect *pRect)
Definition: C4AppT.cpp:108
virtual bool ReInit(C4AbstractApp *pApp)
Definition: C4AppT.cpp:110
virtual ~C4Window()
Definition: C4WindowSDL.cpp:46
void SetTitle(const char *Title)
Definition: C4AppT.cpp:114
bool RestorePosition(const char *szWindowName, const char *szSubKey, bool fHidden=false)
Definition: C4AppT.cpp:111
virtual void Clear()
Definition: C4AppT.cpp:102
void SetSize(unsigned int cx, unsigned int cy)
Definition: C4AppT.cpp:113
WindowKind eKind
Definition: C4Window.h:276
virtual void RequestUpdate()
Definition: C4AppT.cpp:112
bool StorePosition(const char *szWindowName, const char *szSubKey, bool fStoreSize=true)
bool Active
Definition: C4Window.h:274
@ W_Fullscreen
Definition: C4Window.h:268
@ W_GuiWindow
Definition: C4Window.h:265
@ W_Viewport
Definition: C4Window.h:267
@ W_Console
Definition: C4Window.h:266
void GrabMouse(bool grab)
Definition: C4AppT.cpp:107
C4OpenGLView * openGLView