Debian Codegraph @

src/af/xap/cocoa/xap_CocoaWindow.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Program Utilities
4 * Copyright (C) 2002 Francis James Franklin <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA.
20 */
21
22 #include "ut_assert.h"
23 #include "ut_debugmsg.h"
24
25 #include "xap_CocoaWindow.h"
26
27 static float s_ToolbarHeight = 0.0;
28
29 @interface XAP_CocoaWindowDelegate : NSWindowController
30 {
31 XAP_CocoaWindow * m_window;
32 }
33 - (id)initWithWindow:(NSWindow *)window withXAPWindow:(XAP_CocoaWindow *)xap;
34 - (void)windowDidResize:(NSNotification *)aNotification;
35 @end
36
37 @implementation XAP_CocoaWindowDelegate
38
39 - (id)initWithWindow:(NSWindow *)window withXAPWindow:(XAP_CocoaWindow *)xap
40 {
41 if(![super initWithWindow:window]) {
42 return nil;
43 }
44 m_window = xap;
45 return self;
46 }
47
48 - (void)windowDidResize:(NSNotification *)aNotification
49 {
50 UT_UNUSED(aNotification);
51 if (m_window)
52 m_window->_windowResized ();
53 }
54
55 @end
56
57
58 XAP_CocoaWindow::XAP_CocoaWindow (WindowStyle ws, const NSRect & frameRect) :
59 m_styleMask(NSBorderlessWindowMask),
60 m_backingType(NSBackingStoreBuffered),
61 m_controller(0),
62 m_window(0),
63 m_isToolbar(false)
64 {
65 NSRect screenFrame = [[NSScreen mainScreen] visibleFrame];
66
67 float diff_width = screenFrame.size.width - frameRect.size.width;
68 float diff_height = screenFrame.size.height - frameRect.size.height;
69
70 m_frame = NSMakeRect (screenFrame.origin.x + diff_width / 2, screenFrame.origin.y + diff_height / 2,
71 frameRect.size.width, frameRect.size.height);
72 _init (ws);
73 }
74
75 XAP_CocoaWindow::XAP_CocoaWindow () :
76 m_styleMask(NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask),
77 m_backingType(NSBackingStoreBuffered),
78 m_controller(0),
79 m_window(0),
80 m_isToolbar(false)
81 {
82 NSRect frame = [[NSScreen mainScreen] visibleFrame];
83
84 float width = frame.size.width;
85 float height = frame.size.height - s_ToolbarHeight;
86
87 float x = width / 16.0;
88 float y = height / 16.0;
89
90 width -= width / 8.0;
91 height -= height / 8.0;
92
93 m_frame = NSMakeRect(x, y + height, width, height);
94
95 _init (ws_Frame);
96 }
97
98 XAP_CocoaWindow::XAP_CocoaWindow (float height) :
99 m_styleMask(NSBorderlessWindowMask),
100 m_backingType(NSBackingStoreBuffered),
101 m_controller(0),
102 m_window(0),
103 m_isToolbar(true)
104 {
105 m_frame = [[NSScreen mainScreen] visibleFrame];
106
107 m_frame.origin.y = m_frame.size.height - height;
108 m_frame.size.height = height;
109
110 _init (ws_Raw);
111
112 s_ToolbarHeight = height;
113 }
114
115 void XAP_CocoaWindow::_init (WindowStyle ws)
116 {
117 switch (ws)
118 {
119 case ws_Raw:
120 m_styleMask = NSBorderlessWindowMask;
121 break;
122
123 case ws_Normal:
124 default: // ??
125 m_styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
126 break;
127 }
128 m_window = [[NSWindow alloc] initWithContentRect:m_frame styleMask:m_styleMask backing:m_backingType defer:YES];
129 UT_ASSERT (m_window);;
130
131 m_controller = [[XAP_CocoaWindowDelegate alloc] initWithWindow:m_window withXAPWindow:this];
132 [m_window setDelegate:m_controller];
133 }
134
135 XAP_CocoaWindow::~XAP_CocoaWindow ()
136 {
137 if (m_controller) {
138 [m_controller close];
139 }
140 [m_controller release];
141 [m_window release];
142
143
144 if (m_isToolbar)
145 s_ToolbarHeight = 0.0;
146 }
147
148 void XAP_CocoaWindow::_show ()
149 {
150 UT_ASSERT(m_controller);
151 if (m_controller == 0)
152 return;
153
154 [m_controller showWindow:m_controller]; // what object should we really be passing?
155
156 [m_window setFrame:m_frame display:YES];
157 }
158
159 /* assume that window position is top-left, not bottom-left */
160 void XAP_CocoaWindow::_moveto (const NSPoint & position)
161 {
162 [m_window setFrameTopLeftPoint:position];
163 }
164
165 void XAP_CocoaWindow::_resize (const NSSize & size)
166 {
167 [m_window setContentSize:size];
168 }
169
170 void XAP_CocoaWindow::_resize (float height) // special case for toolbar
171 {
172 if (!m_isToolbar)
173 return;
174
175 UT_ASSERT(m_window);
176 if (m_window == 0)
177 return;
178
179 m_frame = [[NSScreen mainScreen] visibleFrame];
180
181 m_frame.origin.y = m_frame.size.height - height;
182 m_frame.size.height = height;
183
184 [m_window setContentSize:m_frame.size];
185
186 [m_window setFrame:m_frame display:YES];
187
188 s_ToolbarHeight = height;
189 }
190
191 /* callback notification of main-window resize
192 */
193 void XAP_CocoaWindow::_windowResized ()
194 {
195 //
196 }