Debian Codegraph @

src/af/xap/cocoa/xap_CocoaToolbarWindow.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Program Utilities
4 * Copyright (C) 2002-2004 Hubert Figuiere
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_debugmsg.h"
23
24 #include "ev_CocoaToolbar.h"
25
26 #include "xap_CocoaFrameImpl.h"
27 #include "xap_CocoaToolbarWindow.h"
28
29 static XAP_CocoaToolbarWindow_Controller * s_pSharedToolbar = nil;
30
31 @interface XAP_CocoaToolbarWindow : NSPanel
32 {
33 //
34 }
35 - (id)initWithContentRect:(NSRect)frame;
36 - (BOOL)canBecomeKeyWindow;
37 @end
38
39 @implementation XAP_CocoaToolbarWindow
40
41 - (id)initWithContentRect:(NSRect)windowFrame
42 {
43 if (![super initWithContentRect:windowFrame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES]) {
44 return nil;
45 }
46 [self setBecomesKeyOnlyIfNeeded:YES];
47 [self setHidesOnDeactivate:YES];
48 [self setReleasedWhenClosed:YES]; // ??
49 [self setExcludedFromWindowsMenu:YES];
50 [self setCanHide:YES];
51 return self;
52 }
53
54 - (BOOL)canBecomeKeyWindow
55 {
56 return YES;
57 }
58
59 @end
60
61 @implementation XAP_CocoaToolbarWindow_Controller
62
63 + (XAP_CocoaToolbarWindow_Controller *)sharedToolbar
64 {
65 if (!s_pSharedToolbar)
66 {
67 /* no toolbar created. create one and show it:
68 */
69 s_pSharedToolbar = [[XAP_CocoaToolbarWindow_Controller alloc] initWithWindow:nil];
70
71 // TODO!! [s_pSharedToolbar showWindow:s_pSharedToolbar];
72 xxx_UT_DEBUGMSG (("Toolbar is visible ? : %d\n", [myWindow isVisible]));
73 }
74 return s_pSharedToolbar;
75 }
76
77
78 - (id)initWithWindow:(NSWindow *)window
79 {
80 if (![super initWithWindow:window])
81 {
82 return nil;
83 }
84 m_windows = [[NSMutableArray alloc] initWithCapacity:4];
85 if (!m_windows)
86 {
87 [self release];
88 return nil;
89 }
90 m_SummaryID = @"";
91 [m_SummaryID retain];
92
93 m_bounds.size.height = 0;
94 NSNotificationCenter * NC = [NSNotificationCenter defaultCenter];
95 [NC addObserver:self selector:@selector(showToolbarNotification:) name:(XAP_CocoaFrameImpl::XAP_FrameNeedToolbar) object:nil];
96 [NC addObserver:self selector:@selector(hideToolbarNotification:) name:(XAP_CocoaFrameImpl::XAP_FrameReleaseToolbar) object:nil];
97 return self;
98 }
99
100 - (void)dealloc
101 {
102 if (m_windows)
103 {
104 [self removeAllToolbars];
105
106 [m_windows release];
107 m_windows = 0;
108 }
109 [m_SummaryID release];
110
111 [[NSNotificationCenter defaultCenter] removeObserver:self];
112 [super dealloc];
113 }
114
115 - (void)removeAllToolbars
116 {
117 [m_SummaryID release];
118 m_SummaryID = @"";
119 [m_SummaryID retain];
120
121 m_bounds.size.height = 0;
122
123 unsigned count = [m_windows count];
124
125 for (unsigned i = 0; i < count; i++)
126 {
127 NSWindowController * window = (NSWindowController *) [m_windows objectAtIndex:i];
128
129 [window close];
130 [window release];
131 }
132 [m_windows removeAllObjects];
133 }
134
135 - (void)autoResize
136 {
137 // ??
138 }
139
140 - (void)redisplayToolbars:(XAP_CocoaFrameController *)frame
141 {
142 if (!m_lock)
143 {
144 [self _showAllToolbars:frame];
145 }
146 }
147
148 - (void)_showAllToolbars:(XAP_CocoaFrameController *)wframe
149 {
150 UT_ASSERT(wframe);
151 if (!wframe)
152 {
153 [self removeAllToolbars];
154 return;
155 }
156
157 bool bNewConfiguration = true;
158
159 NSString * SummaryID = [wframe getToolbarSummaryID];
160 if ([m_SummaryID isEqualToString:SummaryID])
161 {
162 XAP_CocoaFrameImpl::setToolbarRect(m_bounds);
163 bNewConfiguration = false;
164 }
165 if (bNewConfiguration)
166 {
167 [self removeAllToolbars];
168
169 [m_SummaryID release];
170 m_SummaryID = [SummaryID retain];
171 }
172
173 NSArray * toolbars = [wframe getToolbars];
174
175 unsigned count = [toolbars count];
176 if (!count)
177 return;
178
179 NSRect visibleFrame = [[NSScreen mainScreen] visibleFrame];
180
181 float offset_x = 0;
182 float offset_y = 0;
183
184 for (unsigned i = 0; i < count; i++)
185 {
186 NSView * view = [toolbars objectAtIndex:i];
187
188 if (!bNewConfiguration)
189 {
190 /* A different frame is being used but with the same configuration of toolbars, so
191 * don't create new windows, just replace the views...
192 */
193 NSWindowController * controller = (NSWindowController *) [m_windows objectAtIndex:i];
194 NSWindow * window = [controller window];
195 NSView * contentView = [window contentView];
196 NSArray * subviews = [contentView subviews];
197 while ([subviews count])
198 {
199 NSView * subview = (NSView *) [subviews objectAtIndex:0];
200 [subview removeFromSuperview];
201 }
202 [contentView addSubview:view];
203 [window orderFront:self];
204 continue;
205 }
206
207 NSRect frame = [view frame];
208
209 bool bNewToolbarRow = true;
210
211 if (i > 0)
212 if (offset_x + frame.size.width < visibleFrame.origin.x + visibleFrame.size.width)
213 bNewToolbarRow = false;
214
215 if (bNewToolbarRow)
216 {
217 offset_x = visibleFrame.origin.x;
218 offset_y += frame.size.height;
219 }
220 frame.origin.x = offset_x;
221 frame.origin.y = visibleFrame.origin.y + visibleFrame.size.height - offset_y;
222
223 XAP_CocoaToolbarWindow * window = [[XAP_CocoaToolbarWindow alloc] initWithContentRect:frame];
224 if (window)
225 {
226 NSWindowController * controller = [[NSWindowController alloc] initWithWindow:window];
227 [m_windows addObject:controller];
228 [controller showWindow:self];
229
230 [window orderFront:self];
231 [window release]; // ??
232 }
233 offset_x += frame.size.width;
234
235 frame.origin.x = 0;
236 frame.origin.y = 0;
237
238 [[window contentView] addSubview:view];
239 }
240 if (bNewConfiguration)
241 {
242 visibleFrame.origin.y = visibleFrame.origin.y + visibleFrame.size.height - offset_y;
243 visibleFrame.size.height = offset_y;
244
245 m_bounds = visibleFrame;
246
247 XAP_CocoaFrameImpl::setToolbarRect(m_bounds);
248 }
249 }
250
251 - (float)height
252 {
253 return m_bounds.size.height;
254 }
255
256 - (void)lock
257 {
258 UT_ASSERT(m_lock == NO);
259 m_lock = YES;
260 }
261
262 - (void)unlock
263 {
264 UT_ASSERT(m_lock);
265 m_lock = NO;
266 }
267
268 - (void)showToolbarNotification:(NSNotification*)notif
269 {
270 UT_DEBUGMSG(("received showToolbarNotification:\n"));
271 XAP_CocoaFrameController * frame = [notif object];
272
273 if (frame == m_current)
274 {
275 UT_DEBUGMSG(("already shown\n"));
276 return;
277 }
278 m_current = frame;
279
280 [self _showAllToolbars:frame];
281 #if 0
282 unsigned count = [m_windows count];
283 if (!count)
284 return;
285
286 for (unsigned i = 0; i < count; i++)
287 {
288 NSWindowController * controller = (NSWindowController *) [m_windows objectAtIndex:i];
289 [[controller window] orderFront:self];
290 }
291 #endif
292 }
293
294 - (void)hideToolbarNotification:(NSNotification*)notif
295 {
296 UT_DEBUGMSG(("received hideToolbarNotification:\n"));
297 if (m_current != [notif object]) {
298 NSLog(@"attempt to hide toolbar for a different frame.");
299 return;
300 }
301 m_current = nil;
302
303 unsigned count = [m_windows count];
304 if (!count)
305 return;
306
307 for (unsigned i = 0; i < count; i++)
308 {
309 NSWindowController * controller = (NSWindowController *) [m_windows objectAtIndex:i];
310 [[controller window] orderOut:self];
311 }
312 }
313
314 @end