calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / fpicker / source / aqua / SalAquaPicker.mm
blobc2cf497f5771d6ccb5fdcabae790e2ed3e280df7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <cppuhelper/interfacecontainer.h>
26 #include <osl/diagnose.h>
27 #include <osl/mutex.hxx>
28 #include <vcl/svapp.hxx>
29 #include "SalAquaPicker.hxx"
30 #include <osl/file.hxx>
31 #include "NSString_OOoAdditions.hxx"
33 #include "NSURL_OOoAdditions.hxx"
35 #include "SalAquaFilePicker.hxx"
37 #include <stdio.h>
39 #pragma mark DEFINES
40 #define kSetHideExtensionStateKey @"NSNavLastUserSetHideExtensionButtonState"
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::uno;
46 SalAquaPicker::SalAquaPicker()
47 : m_pDialog(nullptr)
48 , m_pControlHelper(new ControlHelper())
52 SalAquaPicker::~SalAquaPicker()
54     SolarMutexGuard aGuard;
56     NSAutoreleasePool *pool = [NSAutoreleasePool new];
58     if (nullptr != m_pControlHelper)
59         delete m_pControlHelper;
61     if (nullptr != m_pDialog)
62         [m_pDialog release];
64     [pool release];
67 void SalAquaPicker::implInitialize()
69     SolarMutexGuard aGuard;
71     if (m_pDialog != nil) {
72         return;
73     }
75     switch (m_nDialogType)
76     {
77         case NAVIGATIONSERVICES_OPEN:
78             m_pDialog = [NSOpenPanel openPanel];
79             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:NO];
80             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:YES];
81             break;
83         case NAVIGATIONSERVICES_SAVE:
84             m_pDialog = [NSSavePanel savePanel];
85             [m_pDialog setCanSelectHiddenExtension:NO]; //changed for issue #102102
86             /* I would have loved to use
87              * [(NSSavePanel*)m_pDialog setExtensionHidden:YES];
88              * here but unfortunately this
89              * a) only works when the dialog is already displayed because it seems to act on the corresponding checkbox (that we don't show but that doesn't matter)
90              * b) macOS saves this setting on an application-based level which means that the last state is always being restored again when the app runs for the next time
91              *
92              * So the only reliable way seems to be using the NSUserDefaults object because that is where that value is stored and
93              * to just overwrite it if it has the wrong value.
94              */
95             {
96                 NSUserDefaults *pDefaults = [NSUserDefaults standardUserDefaults];
97                 NSNumber *pExtn = [pDefaults objectForKey:kSetHideExtensionStateKey];
98                 if(pExtn == nil || [pExtn boolValue] == NO) {
99                     [pDefaults setBool:YES forKey:kSetHideExtensionStateKey];
100                 }
101             }
102             break;
104         case NAVIGATIONSERVICES_DIRECTORY:
105             m_pDialog = [NSOpenPanel openPanel];
106             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseDirectories:YES];
107             [static_cast<NSOpenPanel*>(m_pDialog) setCanChooseFiles:NO];
108             break;
110         default:
111             break;
112     }
114     if (m_pDialog != nil) {
115         [static_cast<NSOpenPanel*>(m_pDialog) setCanCreateDirectories:YES];
116         //Retain the dialog instance or it will go away immediately
117         [m_pDialog retain];
118     }
121 int SalAquaPicker::run()
123     SolarMutexGuard aGuard;
125     NSAutoreleasePool *pool = [NSAutoreleasePool new];
127     if (m_pDialog == nullptr) {
128         //this is the case e.g. for the folder picker at this stage
129         implInitialize();
130     }
132     NSView *userPane = m_pControlHelper->getUserPane();
133     if (userPane != nullptr) {
134         [m_pDialog setAccessoryView:userPane];
135     }
137     int retVal = 0;
139     NSURL *startDirectory;
140     if (m_sDisplayDirectory.getLength() > 0) {
141         NSString *temp = [NSString stringWithOUString:m_sDisplayDirectory];
142         startDirectory = [NSURL URLWithString:temp];
144         SAL_INFO("fpicker.aqua", "start dir: " << [startDirectory path]);
145     }
146     else {
147         startDirectory = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
148     }
150     switch(m_nDialogType) {
151         case NAVIGATIONSERVICES_DIRECTORY:
152         case NAVIGATIONSERVICES_OPEN:
153             [m_pDialog setDirectoryURL:startDirectory];
154             retVal = [static_cast<NSOpenPanel*>(m_pDialog) runModal];
155             break;
156         case NAVIGATIONSERVICES_SAVE:
157             [m_pDialog setDirectoryURL:startDirectory];
158             [m_pDialog setNameFieldStringValue:[NSString stringWithOUString:static_cast<SalAquaFilePicker*>(this)->getSaveFileName()]];
159             retVal = [m_pDialog runModal];
160             break;
161         default:
162             break;
163     }
165     if (retVal == NSModalResponseOK) {
166         NSURL* pDir = [m_pDialog directoryURL];
167         if (pDir) {
168             implsetDisplayDirectory([pDir OUString]);
169         }
170     }
172     [pool release];
174     return retVal;
177 int SalAquaPicker::runandwaitforresult()
179     SolarMutexGuard aGuard;
181     int status = run();
183     return status;
186 void SalAquaPicker::implsetDisplayDirectory( const OUString& aDirectory )
188     SolarMutexGuard aGuard;
190     if (aDirectory != m_sDisplayDirectory) {
191         m_sDisplayDirectory = aDirectory;
192     }
195 OUString const & SalAquaPicker::implgetDisplayDirectory()
197     return m_sDisplayDirectory;
200 void SalAquaPicker::implsetTitle( const OUString& aTitle )
202     SolarMutexGuard aGuard;
204     if (m_pDialog != nil) {
205         [m_pDialog setTitle:[NSString stringWithOUString:aTitle]];
206     }
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */