summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm
diff options
context:
space:
mode:
authorGabriel de Dietrich <gabriel.dedietrich@qt.io>2016-10-24 14:07:33 -0700
committerTimur Pocheptsov <timur.pocheptsov@qt.io>2016-11-16 05:19:44 +0000
commit27aeeac6eeb11150b505f8ffde6e87e157c22c90 (patch)
treec1515bc75eec952567eac8aeecbfedeab09d73b7 /src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm
parent1a43199fcea1bcec1ebf1a1a12cd3dcb942d67b4 (diff)
Cocoa Dialog Helpers: Refactor OK-Cancel buttons view
Since virtually all the logic is shared between QNSColorPanelDelegate and QNSFontPanelDelegate, we extract the added buttons and layouting logic and move it into its own class. This requires the two afore mentioned Objective C classes to satisfy the QNSPanelDelegate protocol. Change-Id: Ie26e758f5db71920896d930a4f3644b51a1ce3fa Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm')
-rw-r--r--src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm126
1 files changed, 12 insertions, 114 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm b/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm
index 0be931b54f..e565f3b5b1 100644
--- a/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm
+++ b/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm
@@ -58,33 +58,6 @@ typedef float CGFloat; // Should only not be defined on 32-bit platforms
QT_USE_NAMESPACE
-// should a priori be kept in sync with qcolordialog_mac.mm
-const CGFloat ButtonMinWidth = 78.0;
-const CGFloat ButtonMinHeight = 32.0;
-const CGFloat ButtonSpacing = 0.0;
-const CGFloat ButtonTopMargin = 0.0;
-const CGFloat ButtonBottomMargin = 7.0;
-const CGFloat ButtonSideMargin = 9.0;
-
-// looks better with some margins
-const CGFloat DialogTopMargin = 7.0;
-const CGFloat DialogSideMargin = 9.0;
-
-static NSButton *macCreateButton(const char *text, NSView *superview)
-{
- static const NSRect buttonFrameRect = { { 0.0, 0.0 }, { 0.0, 0.0 } };
-
- NSButton *button = [[NSButton alloc] initWithFrame:buttonFrameRect];
- [button setButtonType:NSMomentaryLightButton];
- [button setBezelStyle:NSRoundedBezelStyle];
- [button setTitle:(NSString*)(CFStringRef)QCFString(
- QPlatformTheme::removeMnemonics(QCoreApplication::translate("QDialogButtonBox", text)))];
- [[button cell] setFont:[NSFont systemFontOfSize:
- [NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
- [superview addSubview:button];
- return button;
-}
-
static QFont qfontForCocoaFont(NSFont *cocoaFont, const QFont &resolveFont)
{
QFont newFont;
@@ -103,22 +76,19 @@ static QFont qfontForCocoaFont(NSFont *cocoaFont, const QFont &resolveFont)
@class QT_MANGLE_NAMESPACE(QNSFontPanelDelegate);
-@interface QT_MANGLE_NAMESPACE(QNSFontPanelDelegate) : NSObject<NSWindowDelegate>
+@interface QT_MANGLE_NAMESPACE(QNSFontPanelDelegate) : NSObject<NSWindowDelegate, QT_MANGLE_NAMESPACE(QNSPanelDelegate)>
{
@public
NSFontPanel *mFontPanel;
QCocoaFontDialogHelper *mHelper;
NSView *mStolenContentView;
- NSButton *mOkButton;
- NSButton *mCancelButton;
+ QNSPanelContentsWrapper *mPanelButtons;
QFont mQtFont;
NSInteger mResultCode;
BOOL mDialogIsExecuting;
BOOL mResultSet;
};
- (void)restoreOriginalContentView;
-- (void)relayout;
-- (void)relayoutToContentSize:(NSSize)frameSize;
- (void)updateQtFont;
- (void)changeFont:(id)sender;
- (void)finishOffWithCode:(NSInteger)code;
@@ -134,8 +104,7 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSFontPanelDelegate);
mFontPanel = [NSFontPanel sharedFontPanel];
mHelper = 0;
mStolenContentView = 0;
- mOkButton = 0;
- mCancelButton = 0;
+ mPanelButtons = 0;
mResultCode = NSCancelButton;
mDialogIsExecuting = false;
mResultSet = false;
@@ -173,23 +142,11 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSFontPanelDelegate);
[mFontPanel setContentView:0];
// create a new content view and add the stolen one as a subview
- NSRect frameRect = { { 0.0, 0.0 }, { 0.0, 0.0 } };
- NSView *ourContentView = [[NSView alloc] initWithFrame:frameRect];
- [ourContentView addSubview:mStolenContentView];
-
- // create OK and Cancel buttons and add these as subviews
- mOkButton = macCreateButton("&OK", ourContentView);
- mCancelButton = macCreateButton("Cancel", ourContentView);
-
- [mFontPanel setContentView:ourContentView];
- [mFontPanel setDefaultButtonCell:[mOkButton cell]];
- [self relayoutToContentSize:[[mStolenContentView superview] frame].size];
-
- [mOkButton setAction:@selector(onOkClicked)];
- [mOkButton setTarget:self];
-
- [mCancelButton setAction:@selector(onCancelClicked)];
- [mCancelButton setTarget:self];
+ mPanelButtons = [[QNSPanelContentsWrapper alloc] initWithPanelDelegate:self];
+ [mPanelButtons addSubview:mStolenContentView];
+ mPanelButtons.panelContentsMargins = NSEdgeInsetsMake(0, 0, 7, 0);
+ mFontPanel.contentView = mPanelButtons;
+ mFontPanel.defaultButtonCell = mPanelButtons.okButton.cell;
}
}
@@ -198,77 +155,18 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSFontPanelDelegate);
[mFontPanel close];
}
-- (void)windowDidResize:(NSNotification *)notification
-{
- Q_UNUSED(notification);
- [self relayout];
-}
-
- (void)restoreOriginalContentView
{
if (mStolenContentView) {
- NSView *ourContentView = [mFontPanel contentView];
-
// return stolen stuff to its rightful owner
[mStolenContentView removeFromSuperview];
[mFontPanel setContentView:mStolenContentView];
- [mOkButton release];
- [mCancelButton release];
- [ourContentView release];
- mOkButton = 0;
- mCancelButton = 0;
mStolenContentView = 0;
+ [mPanelButtons release];
+ mPanelButtons = nil;
}
}
-- (void)relayout
-{
- if (!mOkButton)
- return;
-
- [self relayoutToContentSize:[[mStolenContentView superview] frame].size];
-}
-
-- (void)relayoutToContentSize:(NSSize)frameSize
-{
- Q_ASSERT(mOkButton);
-
- [mOkButton sizeToFit];
- NSSize okSizeHint = [mOkButton frame].size;
-
- [mCancelButton sizeToFit];
- NSSize cancelSizeHint = [mCancelButton frame].size;
-
- const CGFloat ButtonWidth = qMin(qMax(ButtonMinWidth,
- qMax(okSizeHint.width, cancelSizeHint.width)),
- CGFloat((frameSize.width - 2.0 * ButtonSideMargin - ButtonSpacing) * 0.5));
- const CGFloat ButtonHeight = qMax(ButtonMinHeight,
- qMax(okSizeHint.height, cancelSizeHint.height));
-
- const CGFloat X = DialogSideMargin;
- const CGFloat Y = ButtonBottomMargin + ButtonHeight + ButtonTopMargin;
-
- NSRect okRect = { { frameSize.width - ButtonSideMargin - ButtonWidth,
- ButtonBottomMargin },
- { ButtonWidth, ButtonHeight } };
- [mOkButton setFrame:okRect];
- [mOkButton setNeedsDisplay:YES];
-
- NSRect cancelRect = { { okRect.origin.x - ButtonSpacing - ButtonWidth,
- ButtonBottomMargin },
- { ButtonWidth, ButtonHeight } };
- [mCancelButton setFrame:cancelRect];
- [mCancelButton setNeedsDisplay:YES];
-
- NSRect stolenCVRect = { { X, Y },
- { frameSize.width - X - X, frameSize.height - Y - DialogTopMargin } };
- [mStolenContentView setFrame:stolenCVRect];
- [mStolenContentView setNeedsDisplay:YES];
-
- [[mStolenContentView superview] setNeedsDisplay:YES];
-}
-
-
- (void)onOkClicked
{
[mFontPanel close];
@@ -277,7 +175,7 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSFontPanelDelegate);
- (void)onCancelClicked
{
- if (mOkButton) {
+ if (mPanelButtons) {
[mFontPanel close];
mQtFont = QFont();
[self finishOffWithCode:NSCancelButton];
@@ -332,7 +230,7 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QNSFontPanelDelegate);
- (BOOL)windowShouldClose:(id)window
{
Q_UNUSED(window);
- if (!mOkButton)
+ if (!mPanelButtons)
[self updateQtFont];
if (mDialogIsExecuting) {
[self finishOffWithCode:NSCancelButton];