1 | /* $Id: UICocoaSpecialControls.mm 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UICocoaSpecialControls implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifdef VBOX_DARWIN_USE_NATIVE_CONTROLS
|
---|
29 |
|
---|
30 | /* Qt includes: */
|
---|
31 | #include <QMacCocoaViewContainer>
|
---|
32 |
|
---|
33 | /* GUI includes: */
|
---|
34 | #include "VBoxUtils-darwin.h"
|
---|
35 | #include "UICocoaSpecialControls.h"
|
---|
36 |
|
---|
37 | /* System includes: */
|
---|
38 | #import <AppKit/NSButton.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /** Private button-target interface. */
|
---|
42 | @interface UIButtonTargetPrivate : NSObject
|
---|
43 | {
|
---|
44 | UICocoaButton *m_pRealTarget;
|
---|
45 | }
|
---|
46 | // WORKAROUND:
|
---|
47 | // The next method used to be called initWithObject, but Xcode 4.1 preview 5
|
---|
48 | // cannot cope with that for some reason. Hope this doesn't break anything.
|
---|
49 | -(id)initWithObjectAndLionTrouble:(UICocoaButton*)pObject;
|
---|
50 | -(IBAction)clicked:(id)pSender;
|
---|
51 | @end
|
---|
52 |
|
---|
53 |
|
---|
54 | /*********************************************************************************************************************************
|
---|
55 | * Class UIButtonTargetPrivate implementation. *
|
---|
56 | *********************************************************************************************************************************/
|
---|
57 |
|
---|
58 | @implementation UIButtonTargetPrivate
|
---|
59 | -(id)initWithObjectAndLionTrouble:(UICocoaButton*)pObject
|
---|
60 | {
|
---|
61 | self = [super init];
|
---|
62 |
|
---|
63 | m_pRealTarget = pObject;
|
---|
64 |
|
---|
65 | return self;
|
---|
66 | }
|
---|
67 |
|
---|
68 | -(IBAction)clicked:(id)pSender
|
---|
69 | {
|
---|
70 | Q_UNUSED(pSender);
|
---|
71 | m_pRealTarget->onClicked();
|
---|
72 | }
|
---|
73 | @end
|
---|
74 |
|
---|
75 |
|
---|
76 | /*********************************************************************************************************************************
|
---|
77 | * Class UICocoaButton implementation. *
|
---|
78 | *********************************************************************************************************************************/
|
---|
79 |
|
---|
80 | UICocoaButton::UICocoaButton(QWidget *pParent, CocoaButtonType enmType)
|
---|
81 | : QMacCocoaViewContainer(0, pParent)
|
---|
82 | {
|
---|
83 | /* Prepare auto-release pool: */
|
---|
84 | NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
|
---|
85 |
|
---|
86 | /* Prepare native button reference: */
|
---|
87 | NativeNSButtonRef pNativeRef;
|
---|
88 | NSRect initFrame;
|
---|
89 |
|
---|
90 | /* Configure button: */
|
---|
91 | switch (enmType)
|
---|
92 | {
|
---|
93 | case HelpButton:
|
---|
94 | {
|
---|
95 | pNativeRef = [[NSButton alloc] init];
|
---|
96 | [pNativeRef setTitle: @""];
|
---|
97 | [pNativeRef setBezelStyle: NSHelpButtonBezelStyle];
|
---|
98 | [pNativeRef setBordered: YES];
|
---|
99 | [pNativeRef setAlignment: NSCenterTextAlignment];
|
---|
100 | [pNativeRef sizeToFit];
|
---|
101 | initFrame = [pNativeRef frame];
|
---|
102 | initFrame.size.width += 12; /* Margin */
|
---|
103 | [pNativeRef setFrame:initFrame];
|
---|
104 | break;
|
---|
105 | };
|
---|
106 | case CancelButton:
|
---|
107 | {
|
---|
108 | pNativeRef = [[NSButton alloc] initWithFrame: NSMakeRect(0, 0, 13, 13)];
|
---|
109 | [pNativeRef setTitle: @""];
|
---|
110 | [pNativeRef setBezelStyle:NSShadowlessSquareBezelStyle];
|
---|
111 | [pNativeRef setButtonType:NSMomentaryChangeButton];
|
---|
112 | [pNativeRef setImage: [NSImage imageNamed: NSImageNameStopProgressFreestandingTemplate]];
|
---|
113 | [pNativeRef setBordered: NO];
|
---|
114 | [[pNativeRef cell] setImageScaling: NSImageScaleProportionallyDown];
|
---|
115 | initFrame = [pNativeRef frame];
|
---|
116 | break;
|
---|
117 | }
|
---|
118 | case ResetButton:
|
---|
119 | {
|
---|
120 | pNativeRef = [[NSButton alloc] initWithFrame: NSMakeRect(0, 0, 13, 13)];
|
---|
121 | [pNativeRef setTitle: @""];
|
---|
122 | [pNativeRef setBezelStyle:NSShadowlessSquareBezelStyle];
|
---|
123 | [pNativeRef setButtonType:NSMomentaryChangeButton];
|
---|
124 | [pNativeRef setImage: [NSImage imageNamed: NSImageNameRefreshFreestandingTemplate]];
|
---|
125 | [pNativeRef setBordered: NO];
|
---|
126 | [[pNativeRef cell] setImageScaling: NSImageScaleProportionallyDown];
|
---|
127 | initFrame = [pNativeRef frame];
|
---|
128 | break;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* Install click listener: */
|
---|
133 | UIButtonTargetPrivate *bt = [[UIButtonTargetPrivate alloc] initWithObjectAndLionTrouble:this];
|
---|
134 | [pNativeRef setTarget:bt];
|
---|
135 | [pNativeRef setAction:@selector(clicked:)];
|
---|
136 |
|
---|
137 | /* Put the button to the QCocoaViewContainer: */
|
---|
138 | setCocoaView(pNativeRef);
|
---|
139 | /* Release our reference, since our super class
|
---|
140 | * takes ownership and we don't need it anymore. */
|
---|
141 | [pNativeRef release];
|
---|
142 |
|
---|
143 | /* Finally resize the widget: */
|
---|
144 | setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
---|
145 | setFixedSize(NSWidth(initFrame), NSHeight(initFrame));
|
---|
146 |
|
---|
147 | /* Cleanup auto-release pool: */
|
---|
148 | [pPool release];
|
---|
149 | }
|
---|
150 |
|
---|
151 | UICocoaButton::~UICocoaButton()
|
---|
152 | {
|
---|
153 | }
|
---|
154 |
|
---|
155 | QSize UICocoaButton::sizeHint() const
|
---|
156 | {
|
---|
157 | NSRect frame = [nativeRef() frame];
|
---|
158 | return QSize(frame.size.width, frame.size.height);
|
---|
159 | }
|
---|
160 |
|
---|
161 | void UICocoaButton::setText(const QString &strText)
|
---|
162 | {
|
---|
163 | QString s(strText);
|
---|
164 | /* Set it for accessibility reasons as alternative title: */
|
---|
165 | [nativeRef() setAlternateTitle: ::darwinQStringToNSString(s.remove('&'))];
|
---|
166 | }
|
---|
167 |
|
---|
168 | void UICocoaButton::setToolTip(const QString &strToolTip)
|
---|
169 | {
|
---|
170 | [nativeRef() setToolTip: ::darwinQStringToNSString(strToolTip)];
|
---|
171 | }
|
---|
172 |
|
---|
173 | void UICocoaButton::onClicked()
|
---|
174 | {
|
---|
175 | emit clicked(false);
|
---|
176 | }
|
---|
177 |
|
---|
178 | #endif /* VBOX_DARWIN_USE_NATIVE_CONTROLS */
|
---|
179 |
|
---|