]> git.sesse.net Git - vlc/blob - modules/gui/macosx/MainWindowTitle.m
macosx: added some bits of documentation
[vlc] / modules / gui / macosx / MainWindowTitle.m
1 /*****************************************************************************
2  * MainWindowTitle.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2011 Felix Paul Kühne
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #import <vlc_common.h>
25 #import "intf.h"
26 #import "MainWindowTitle.h"
27 #import "CoreInteraction.h"
28
29 /*****************************************************************************
30  * VLCMainWindowTitleView
31  *
32  * this is our title bar, which can do anything a title should do
33  * it relies on the VLCWindowButtonCell to display the correct traffic light
34  * states, since we can't capture the mouse-moved events here correctly
35  *****************************************************************************/
36
37 @implementation VLCMainWindowTitleView
38
39 - (void)awakeFromNib
40 {
41     [self setImageScaling: NSScaleToFit];
42     [self setImageFrameStyle: NSImageFrameNone];
43     [self setImageAlignment: NSImageAlignCenter];
44     [self setImage: [NSImage imageNamed:@"bottom-background_dark"]];
45     [self setAutoresizesSubviews: YES];
46
47     [o_red_btn setImage: [NSImage imageNamed:@"window-close"]];
48     [o_red_btn setAlternateImage: [NSImage imageNamed:@"window-close-on"]];
49     [[o_red_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
50     [[o_red_btn cell] setTag: 0];
51     [o_yellow_btn setImage: [NSImage imageNamed:@"window-minimize"]];
52     [o_yellow_btn setAlternateImage: [NSImage imageNamed:@"window-minimize-on"]];
53     [[o_yellow_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
54     [[o_yellow_btn cell] setTag: 1];
55     [o_green_btn setImage: [NSImage imageNamed:@"window-zoom"]];
56     [o_green_btn setAlternateImage: [NSImage imageNamed:@"window-zoom-on"]];
57     [[o_green_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
58     [[o_green_btn cell] setTag: 2];
59     [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen"]];
60     [o_fullscreen_btn setAlternateImage: [NSImage imageNamed:@"window-fullscreen-on"]];
61     [[o_fullscreen_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
62     [[o_fullscreen_btn cell] setTag: 3];
63 }
64
65 - (BOOL)mouseDownCanMoveWindow
66 {
67     return YES;
68 }
69
70 - (IBAction)buttonAction:(id)sender
71 {
72     if (sender == o_red_btn)
73         [[self window] orderOut: sender];
74     else if (sender == o_yellow_btn)
75         [[self window] miniaturize: sender];
76     else if (sender == o_green_btn)
77         [[self window] performZoom: sender];
78     else if (sender == o_fullscreen_btn)
79         [[VLCCoreInteraction sharedInstance] toggleFullscreen];
80     else
81         msg_Err( VLCIntf, "unknown button action sender" );
82 }
83
84 - (void)setWindowTitle:(NSString *)title
85 {
86     [o_title_lbl setStringValue: title];
87 }
88
89 - (void)setFullscreenButtonHidden:(BOOL)b_value
90 {
91     [o_fullscreen_btn setHidden: b_value];
92 }
93
94 - (void)setWindowButtonOver:(BOOL)b_value
95 {
96     if( b_value )
97     {
98         [o_red_btn setImage: [NSImage imageNamed:@"window-close-over"]];
99         [o_yellow_btn setImage: [NSImage imageNamed:@"window-minimize-over"]];
100         [o_green_btn setImage: [NSImage imageNamed:@"window-zoom-over"]];
101     }
102     else
103     {
104         [o_red_btn setImage: [NSImage imageNamed:@"window-close"]];
105         [o_yellow_btn setImage: [NSImage imageNamed:@"window-minimize"]];
106         [o_green_btn setImage: [NSImage imageNamed:@"window-zoom"]];
107     }
108 }
109
110 - (void)setWindowFullscreenButtonOver:(BOOL)b_value
111 {
112     if (b_value)
113         [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen-over"]];
114     else
115         [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen"]];
116 }
117
118 @end
119
120 /*****************************************************************************
121  * VLCWindowButtonCell
122  *
123  * since the title bar cannot fetch these mouse events (the more top-level
124  * NSButton is unable fetch them as well), we are using a subclass of the
125  * button cell to do so. It's set in the nib for the respective objects.
126  *****************************************************************************/
127
128 @implementation VLCWindowButtonCell
129
130 - (void)mouseEntered:(NSEvent *)theEvent
131 {
132     if ([self tag] == 3)
133         [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: YES];
134     else
135         [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: YES];
136 }
137
138 - (void)mouseExited:(NSEvent *)theEvent
139 {
140     if ([self tag] == 3)
141         [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: NO];
142     else
143         [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: NO];
144 }
145
146 @end
147
148
149 /*****************************************************************************
150  * VLCResizeControl
151  *
152  * For Leopard and Snow Leopard, we need to emulate the resize control on the
153  * bottom right of the window, since it is gone by using the borderless window
154  * mask. A proper fix would be Lion-only.
155  *****************************************************************************/
156
157 @implementation VLCResizeControl
158
159 - (void)mouseDragged:(NSEvent *)theEvent
160 {
161     NSRect windowFrame = [[self window] frame];
162     CGFloat deltaX, deltaY, oldOriginY;
163     deltaX = [theEvent deltaX];
164     deltaY = [theEvent deltaY];
165     oldOriginY = windowFrame.origin.y;
166
167     windowFrame.origin.y = (oldOriginY + windowFrame.size.height) - (windowFrame.size.height + deltaY);
168     windowFrame.size.width += deltaX;
169     windowFrame.size.height += deltaY;
170
171     NSSize winMinSize = [self window].minSize;
172     if (windowFrame.size.width < winMinSize.width)
173         windowFrame.size.width = winMinSize.width;
174
175     if (windowFrame.size.height < winMinSize.height)
176     {
177         windowFrame.size.height = winMinSize.height;
178         windowFrame.origin.y = oldOriginY;
179     }
180
181     [[self window] setFrame: windowFrame display: YES animate: NO];
182 }
183
184 @end