OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 #include "C4Include.h"
17 #include "graphics/C4DrawGL.h"
18 #include "platform/C4Window.h"
19 #include "C4Version.h"
20 #include "game/C4Application.h"
21 #include "lib/C4Rect.h"
22 #include "game/C4FullScreen.h"
23 #include "editor/C4Console.h"
25 
26 #import <AppKit/AppKit.h>
28 #import "graphics/C4DrawGLMac.h"
29 
30 #ifdef USE_COCOA
31 
32 #define ctrler (this->objectiveCObject<C4WindowController>())
33 
35  Active(false),
36  pSurface(0)
37 {}
38 
40 
41 static NSString* windowXibNameForWindowKind(C4Window::WindowKind kind)
42 {
43  switch (kind)
44  {
47  return @"EditorGUIWindow";
49  return @"FullScreen";
51  return @"EditorViewport";
52  default:
53  return nil;
54  }
55 }
56 
57 C4Window * C4Window::Init(C4Window::WindowKind windowKind, C4AbstractApp * pApp, const char * Title, const C4Rect * size)
58 {
59  Active = true;
60 
61 #ifdef WITH_QT_EDITOR
62  // embed into editor: Viewport widget creation handled by C4ConsoleQt
63  ::Console.AddViewport(static_cast<C4ViewportWindow *>(this));
64  return this;
65 #else
66 
67  // Create window
68  C4WindowController* controller = [C4WindowController new];
69  setObjectiveCObject(controller);
70  [NSBundle loadNibNamed:windowXibNameForWindowKind(windowKind) owner:controller];
71  [controller setStdWindow:this];
72  if (windowKind != W_GuiWindow && windowKind != W_Console)
73  {
74  if (lionAndBeyond())
75  [controller.window setCollectionBehavior:[controller.window collectionBehavior] | NSWindowCollectionBehaviorFullScreenPrimary];
76  }
77  SetTitle(Title);
78  eKind = windowKind;
79  return this;
80 #endif
81 }
82 
83 void C4Window::Clear()
84 {
85  // Destroy window
86  C4WindowController* controller = ctrler;
87  if (controller)
88  {
89  [controller.openGLView setNeedsDisplay:NO];
90  [controller.openGLView removeFromSuperview];
91  [controller setStdWindow:NULL];
92  [controller close];
93  setObjectiveCObject(nil);
94  }
95 }
96 
97 bool C4Window::StorePosition(const char *szWindowName, const char *szSubKey, bool fStoreSize)
98 {
99  [ctrler setWindowFrameAutosaveName:[NSString stringWithFormat:@"%s_%s", szWindowName, szSubKey]];
100  return true;
101 }
102 
103 bool C4Window::RestorePosition(const char *szWindowName, const char *szSubKey, bool fHidden)
104 {
105  StorePosition(szWindowName, szSubKey, true);
106  if (fHidden)
107  [ctrler.window orderOut:ctrler];
108  else
109  [ctrler.window orderFront:ctrler];
110  return true;
111 }
112 
113 void C4Window::SetTitle(const char *szToTitle)
114 {
115  C4WindowController* controller;
116  if ((controller = ctrler) && controller.window)
117  [controller.window setTitle:[NSString stringWithUTF8String:szToTitle ? szToTitle : ""]];
118 }
119 
120 bool C4Window::GetSize(C4Rect * pRect)
121 {
122  if (this == &::FullScreen)
123  {
124  pRect->x = 0;
125  pRect->y = 0;
126  pRect->Wdt = ActualFullscreenX;
127  pRect->Hgt = ActualFullscreenY;
128  }
129  else
130  {
131  C4WindowController* controller = ctrler;
132  NSView* view = controller.openGLView ? controller.openGLView : controller.window.contentView;
133  NSRect r = [view frame];
134  pRect->x = 0;
135  pRect->y = 0;
136  pRect->Wdt = r.size.width;
137  pRect->Hgt = r.size.height;
138  }
139  return true;
140 }
141 
142 void C4Window::SetSize(unsigned int cx, unsigned int cy)
143 {
144  C4WindowController* controller = ctrler;
145  [controller setContentSize:NSMakeSize(cx, cy)];
146 }
147 
148 void C4Window::GrabMouse(bool grab)
149 {
150  // TODO
151 }
152 
154 {
155  [ctrler.openGLView display];
156 }
157 
159 {
160  return true;
161 }
162 
163 C4KeyCode K_SHIFT_L = 56 + CocoaKeycodeOffset;
164 C4KeyCode K_SHIFT_R = 60 + CocoaKeycodeOffset;
165 C4KeyCode K_CONTROL_L = 0x3b + CocoaKeycodeOffset;
166 C4KeyCode K_CONTROL_R = 0x3e + CocoaKeycodeOffset;
167 C4KeyCode K_ALT_L = 58 + CocoaKeycodeOffset;
168 C4KeyCode K_ALT_R = 61 + CocoaKeycodeOffset;
169 C4KeyCode K_COMMAND_L = 55 + CocoaKeycodeOffset;
170 C4KeyCode K_COMMAND_R = 54 + CocoaKeycodeOffset;
171 C4KeyCode K_F1 = 122 + CocoaKeycodeOffset;
172 C4KeyCode K_F2 = 120 + CocoaKeycodeOffset;
173 C4KeyCode K_F3 = 99 + CocoaKeycodeOffset;
174 C4KeyCode K_F4 = 118 + CocoaKeycodeOffset;
175 C4KeyCode K_F5 = 96 + CocoaKeycodeOffset;
176 C4KeyCode K_F6 = 97 + CocoaKeycodeOffset;
177 C4KeyCode K_F7 = 98 + CocoaKeycodeOffset;
178 C4KeyCode K_F8 = 100 + CocoaKeycodeOffset;
179 C4KeyCode K_F9 = 101 + CocoaKeycodeOffset;
180 C4KeyCode K_F10 = 109 + CocoaKeycodeOffset;
181 C4KeyCode K_F11 = 103 + CocoaKeycodeOffset;
182 C4KeyCode K_F12 = 111 + CocoaKeycodeOffset;
183 C4KeyCode K_ADD = 69 + CocoaKeycodeOffset;
184 C4KeyCode K_SUBTRACT = 78 + CocoaKeycodeOffset;
185 C4KeyCode K_MULTIPLY = 67 + CocoaKeycodeOffset;
186 C4KeyCode K_ESCAPE = 53 + CocoaKeycodeOffset;
187 C4KeyCode K_PAUSE = NSPauseFunctionKey + CocoaKeycodeOffset;
188 C4KeyCode K_TAB = 48 + CocoaKeycodeOffset;
189 C4KeyCode K_RETURN = 36 + CocoaKeycodeOffset;
190 C4KeyCode K_DELETE = 117 + CocoaKeycodeOffset;
191 C4KeyCode K_INSERT = 125125125 + CocoaKeycodeOffset;
192 C4KeyCode K_BACK = 51 + CocoaKeycodeOffset;
193 C4KeyCode K_SPACE = 49 + CocoaKeycodeOffset;
194 C4KeyCode K_UP = 126 + CocoaKeycodeOffset;
195 C4KeyCode K_DOWN = 125 + CocoaKeycodeOffset;
196 C4KeyCode K_LEFT = 123 + CocoaKeycodeOffset;
197 C4KeyCode K_RIGHT = 124 + CocoaKeycodeOffset;
198 C4KeyCode K_HOME = 115 + CocoaKeycodeOffset;
199 C4KeyCode K_END = 119 + CocoaKeycodeOffset;
200 C4KeyCode K_SCROLL = 1000 + CocoaKeycodeOffset;
201 C4KeyCode K_MENU = 1000 + CocoaKeycodeOffset;
202 C4KeyCode K_PAGEUP = 116 + CocoaKeycodeOffset;
203 C4KeyCode K_PAGEDOWN = 121 + CocoaKeycodeOffset;
204 C4KeyCode K_M = 46 + CocoaKeycodeOffset;
205 C4KeyCode K_T = 17 + CocoaKeycodeOffset;
206 C4KeyCode K_W = 13 + CocoaKeycodeOffset;
207 C4KeyCode K_I = 34 + CocoaKeycodeOffset;
208 C4KeyCode K_C = 8 + CocoaKeycodeOffset;
209 C4KeyCode K_V = 9 + CocoaKeycodeOffset;
210 C4KeyCode K_X = 7 + CocoaKeycodeOffset;
211 C4KeyCode K_A = 0 + CocoaKeycodeOffset;
212 
213 // FIXME
214 C4KeyCode K_PRINT = -100;
215 C4KeyCode K_CENTER = -101;
216 C4KeyCode K_NUM = -102;
217 
218 int MK_SHIFT = NSShiftKeyMask;
219 int MK_CONTROL = NSControlKeyMask;
220 int MK_ALT = NSAlternateKeyMask;
221 
222 #endif
bool StorePosition(const char *szWindowName, const char *szSubKey, bool fStoreSize=true)
void GrabMouse(bool grab)
Definition: C4AppT.cpp:105
bool GetSize(C4Rect *pRect)
Definition: C4AppT.cpp:106
C4OpenGLView * openGLView
C4Console Console
Definition: C4Globals.cpp:45
void SetTitle(const char *Title)
Definition: C4AppT.cpp:112
C4FullScreen FullScreen
Definition: C4Globals.cpp:46
Definition: C4Rect.h:29
void setContentSize:(NSSize size)
virtual bool ReInit(C4AbstractApp *pApp)
Definition: C4AppT.cpp:108
int32_t Wdt
Definition: C4Rect.h:32
void SetSize(unsigned int cx, unsigned int cy)
Definition: C4AppT.cpp:111
int32_t y
Definition: C4Rect.h:32
void AddViewport(C4ViewportWindow *cvp)
Definition: C4ConsoleGUI.h:111
virtual ~C4Window()
Definition: C4AppT.cpp:102
WindowKind eKind
Definition: C4Window.h:280
bool lionAndBeyond()
bool RestorePosition(const char *szWindowName, const char *szSubKey, bool fHidden=false)
Definition: C4AppT.cpp:109
int32_t x
Definition: C4Rect.h:32
virtual void RequestUpdate()
Definition: C4AppT.cpp:110
bool Active
Definition: C4Window.h:278
virtual C4Window * Init(WindowKind windowKind, C4AbstractApp *pApp, const char *Title, const C4Rect *size)
Definition: C4AppT.cpp:107
int32_t Hgt
Definition: C4Rect.h:32
virtual void Clear()
Definition: C4AppT.cpp:100
unsigned long C4KeyCode
C4Window()
Definition: C4AppT.cpp:101