1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <unotools/moduleoptions.hxx>
22 #include <unotools/dynamicmenuoptions.hxx>
23 #include <unotools/historyoptions.hxx>
24 #include <rtl/ustring.hxx>
25 #include <tools/urlobj.hxx>
27 #include <comphelper/sequenceashashmap.hxx>
28 #include <sfx2/app.hxx>
29 #include <sal/macros.h>
30 #include <sfx2/sfxresid.hxx>
31 #include <sfx2/strings.hrc>
32 #include <vcl/svapp.hxx>
33 #include "shutdownicon.hxx"
35 #include <com/sun/star/util/XStringWidth.hpp>
37 #include <cppuhelper/implbase.hxx>
43 #include <objc/objc-runtime.h>
44 #include <Cocoa/Cocoa.h>
55 #define MI_STARTMODULE 9
57 @interface QSMenuExecute : NSObject
60 -(void)executeMenuItem: (NSMenuItem*)pItem;
61 -(void)dockIconClicked: (NSObject*)pSender;
64 @implementation QSMenuExecute
65 -(void)executeMenuItem: (NSMenuItem*)pItem
70 ShutdownIcon::FileOpen();
73 ShutdownIcon::OpenURL( WRITER_URL, "_default" );
76 ShutdownIcon::OpenURL( CALC_URL, "_default" );
79 ShutdownIcon::OpenURL( IMPRESS_URL, "_default" );
82 ShutdownIcon::OpenURL( DRAW_URL, "_default" );
85 ShutdownIcon::OpenURL( BASE_URL, "_default" );
88 ShutdownIcon::OpenURL( MATH_URL, "_default" );
91 ShutdownIcon::FromTemplate();
94 ShutdownIcon::OpenURL( STARTMODULE_URL, "_default" );
101 -(void)dockIconClicked: (NSObject*)pSender
105 ShutdownIcon::OpenURL( STARTMODULE_URL, "_default" );
110 bool ShutdownIcon::IsQuickstarterInstalled()
115 static NSMenuItem* pDefMenu = nil, *pDockSubMenu = nil;
116 static QSMenuExecute* pExecute = nil;
118 static std::set< OUString > aShortcuts;
120 static NSString* getAutoreleasedString( const OUString& rStr )
122 return [[[NSString alloc] initWithCharacters: reinterpret_cast<unichar const *>(rStr.getStr()) length: rStr.getLength()] autorelease];
127 struct RecentMenuEntry
135 class RecentFilesStringLength : public ::cppu::WeakImplHelper< css::util::XStringWidth >
138 RecentFilesStringLength() {}
141 sal_Int32 SAL_CALL queryStringWidth( const OUString& aString ) override
143 return aString.getLength();
149 @interface RecentMenuDelegate : NSObject <NSMenuDelegate>
151 std::vector< RecentMenuEntry >* m_pRecentFilesItems;
155 -(void)menuNeedsUpdate:(NSMenu *)menu;
156 -(void)executeRecentEntry: (NSMenuItem*)item;
159 @implementation RecentMenuDelegate
162 if( (self = [super init]) )
164 m_pRecentFilesItems = new std::vector< RecentMenuEntry >();
171 delete m_pRecentFilesItems;
175 -(void)menuNeedsUpdate:(NSMenu *)menu
178 int nItems = [menu numberOfItems];
180 [menu removeItemAtIndex: 0];
182 // update recent item list
183 std::vector< SvtHistoryOptions::HistoryItem > aHistoryList( SvtHistoryOptions::GetList( EHistoryType::PickList ) );
185 int nPickListMenuItems = ( aHistoryList.size() > 99 ) ? 99 : aHistoryList.size();
187 m_pRecentFilesItems->clear();
188 if( nPickListMenuItems > 0 )
190 for ( int i = 0; i < nPickListMenuItems; i++ )
192 const SvtHistoryOptions::HistoryItem & rPickListEntry = aHistoryList[i];
193 RecentMenuEntry aRecentFile;
194 aRecentFile.aURL = rPickListEntry.sURL;
195 aRecentFile.aFilter = rPickListEntry.sFilter;
196 aRecentFile.aTitle = rPickListEntry.sTitle;
197 aRecentFile.aPassword = rPickListEntry.sPassword;
198 m_pRecentFilesItems->push_back( aRecentFile );
202 // insert new recent items
203 for ( std::vector<RecentMenuEntry>::size_type i = 0; i < m_pRecentFilesItems->size(); i++ )
206 INetURLObject aURL( (*m_pRecentFilesItems)[i].aURL );
208 if ( aURL.GetProtocol() == INetProtocol::File )
210 // Do handle file URL differently => convert it to a system
211 // path and abbreviate it with a special function:
212 OUString aSystemPath( aURL.getFSysPath( FSysStyle::Detect ) );
213 OUString aCompactedSystemPath;
215 oslFileError nError = osl_abbreviateSystemPath( aSystemPath.pData, &aCompactedSystemPath.pData, 46, nullptr );
217 aMenuTitle = aCompactedSystemPath;
219 aMenuTitle = aSystemPath;
223 // Use INetURLObject to abbreviate all other URLs
224 css::uno::Reference< css::util::XStringWidth > xStringLength( new RecentFilesStringLength() );
225 aMenuTitle = aURL.getAbbreviated( xStringLength, 46, INetURLObject::DecodeMechanism::Unambiguous );
228 NSMenuItem* pNewItem = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( aMenuTitle )
229 action: @selector(executeRecentEntry:)
231 [pNewItem setTag: i];
232 [pNewItem setTarget: self];
233 [pNewItem setEnabled: YES];
234 [menu addItem: pNewItem];
235 [pNewItem autorelease];
239 -(void)executeRecentEntry: (NSMenuItem*)item
241 sal_Int32 nIndex = [item tag];
242 if( ( nIndex >= 0 ) && ( nIndex < static_cast<sal_Int32>( m_pRecentFilesItems->size() ) ) )
244 const RecentMenuEntry& rRecentFile = (*m_pRecentFilesItems)[ nIndex ];
245 int NUM_OF_PICKLIST_ARGS = 3;
246 css::uno::Sequence< css::beans::PropertyValue > aArgsList( NUM_OF_PICKLIST_ARGS );
247 css::beans::PropertyValue* pArgsList = aArgsList.getArray();
249 pArgsList[0].Name = "Referer";
250 pArgsList[0].Value <<= OUString( "private:user" );
252 // documents in the picklist will never be opened as templates
253 pArgsList[1].Name = "AsTemplate";
254 pArgsList[1].Value <<= false;
256 OUString aFilter( rRecentFile.aFilter );
257 sal_Int32 nPos = aFilter.indexOf( '|' );
260 OUString aFilterOptions;
262 if ( nPos < ( aFilter.getLength() - 1 ) )
263 aFilterOptions = aFilter.copy( nPos+1 );
265 pArgsList[2].Name = "FilterOptions";
266 pArgsList[2].Value <<= aFilterOptions;
268 aFilter = aFilter.copy( 0, nPos-1 );
269 aArgsList.realloc( ++NUM_OF_PICKLIST_ARGS );
270 pArgsList = aArgsList.getArray();
273 pArgsList[NUM_OF_PICKLIST_ARGS-1].Name = "FilterName";
274 pArgsList[NUM_OF_PICKLIST_ARGS-1].Value <<= aFilter;
276 ShutdownIcon::OpenURL( rRecentFile.aURL, "_default", aArgsList );
281 static RecentMenuDelegate* pRecentDelegate = nil;
283 static OUString getShortCut( const OUString& i_rTitle )
287 for( sal_Int32 nIndex = 0; nIndex < i_rTitle.getLength(); nIndex++ )
289 OUString aShortcut( i_rTitle.copy( nIndex, 1 ).toAsciiLowerCase() );
290 if( aShortcuts.find( aShortcut ) == aShortcuts.end() )
292 aShortcuts.insert( aShortcut );
293 aKeyEquiv = aShortcut;
301 static void appendMenuItem( NSMenu* i_pMenu, NSMenu* i_pDockMenu, const OUString& i_rTitle, int i_nTag, const OUString& i_rKeyEquiv )
303 if( ! i_rTitle.getLength() )
306 NSMenuItem* pItem = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( i_rTitle )
307 action: @selector(executeMenuItem:)
308 keyEquivalent: (i_rKeyEquiv.getLength() ? getAutoreleasedString( i_rKeyEquiv ) : @"")
310 [pItem setTag: i_nTag];
311 [pItem setTarget: pExecute];
312 [pItem setEnabled: YES];
313 [i_pMenu addItem: pItem];
317 // create a similar entry in the dock menu
318 pItem = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( i_rTitle )
319 action: @selector(executeMenuItem:)
322 [pItem setTag: i_nTag];
323 [pItem setTarget: pExecute];
324 [pItem setEnabled: YES];
325 [i_pDockMenu addItem: pItem];
329 static void appendRecentMenu( NSMenu* i_pMenu, NSMenu* i_pDockMenu, const OUString& i_rTitle )
331 if( ! pRecentDelegate )
332 pRecentDelegate = [[RecentMenuDelegate alloc] init];
334 NSMenuItem* pItem = [i_pMenu addItemWithTitle: getAutoreleasedString( i_rTitle )
335 action: @selector(executeMenuItem:)
338 [pItem setEnabled: YES];
339 NSMenu* pRecentMenu = [[NSMenu alloc] initWithTitle: getAutoreleasedString( i_rTitle ) ];
341 [pRecentMenu setDelegate: pRecentDelegate];
343 [pRecentMenu setAutoenablesItems: NO];
344 [pItem setSubmenu: pRecentMenu];
348 // create a similar entry in the dock menu
349 pItem = [i_pDockMenu addItemWithTitle: getAutoreleasedString( i_rTitle )
350 action: @selector(executeMenuItem:)
353 [pItem setEnabled: YES];
354 pRecentMenu = [[NSMenu alloc] initWithTitle: getAutoreleasedString( i_rTitle ) ];
356 [pRecentMenu setDelegate: pRecentDelegate];
358 [pRecentMenu setAutoenablesItems: NO];
359 [pItem setSubmenu: pRecentMenu];
367 void aqua_init_systray()
369 SolarMutexGuard aGuard;
371 ShutdownIcon *pShutdownIcon = ShutdownIcon::getInstance();
372 if( ! pShutdownIcon )
376 pShutdownIcon->SetVeto( true );
377 ShutdownIcon::addTerminateListener();
381 if( [NSApp respondsToSelector: @selector(addFallbackMenuItem:)] )
385 pExecute = [[QSMenuExecute alloc] init];
386 pDefMenu = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( SfxResId(STR_QUICKSTART_FILE) ) action: nullptr keyEquivalent: @""];
387 pDockSubMenu = [[NSMenuItem alloc] initWithTitle: getAutoreleasedString( SfxResId(STR_QUICKSTART_FILE) ) action: nullptr keyEquivalent: @""];
388 NSMenu* pMenu = [[NSMenu alloc] initWithTitle: getAutoreleasedString( SfxResId(STR_QUICKSTART_FILE) )];
389 [pMenu setAutoenablesItems: NO];
390 NSMenu* pDockMenu = [[NSMenu alloc] initWithTitle: getAutoreleasedString( SfxResId(STR_QUICKSTART_FILE) )];
391 [pDockMenu setAutoenablesItems: NO];
393 // collect the URLs of the entries in the File/New menu
394 SvtModuleOptions aModuleOptions;
395 std::set< OUString > aFileNewAppsAvailable;
396 std::vector < SvtDynMenuEntry > const aNewMenu = SvtDynamicMenuOptions::GetMenu( EDynamicMenuType::NewMenu );
398 for ( SvtDynMenuEntry const & newMenuProp : aNewMenu )
400 if ( !newMenuProp.sURL.isEmpty() )
401 aFileNewAppsAvailable.insert( newMenuProp.sURL );
404 // describe the menu entries for launching the applications
405 struct MenuEntryDescriptor
407 SvtModuleOptions::EModule eModuleIdentifier;
409 const char* pAsciiURLDescription;
412 { SvtModuleOptions::EModule::WRITER, MI_WRITER, WRITER_URL },
413 { SvtModuleOptions::EModule::CALC, MI_CALC, CALC_URL },
414 { SvtModuleOptions::EModule::IMPRESS, MI_IMPRESS, IMPRESS_WIZARD_URL },
415 { SvtModuleOptions::EModule::DRAW, MI_DRAW, DRAW_URL },
416 { SvtModuleOptions::EModule::DATABASE, MI_BASE, BASE_URL },
417 { SvtModuleOptions::EModule::MATH, MI_MATH, MATH_URL }
420 // insert entry for startcenter
421 if( aModuleOptions.IsModuleInstalled( SvtModuleOptions::EModule::STARTMODULE ) )
423 appendMenuItem( pMenu, nil, SfxResId(STR_QUICKSTART_STARTCENTER), MI_STARTMODULE, "n" );
424 if( [NSApp respondsToSelector: @selector(setDockIconClickHandler:)] )
425 [NSApp performSelector:@selector(setDockIconClickHandler:) withObject: pExecute];
427 OSL_FAIL( "setDockIconClickHandler selector failed on NSApp" );
431 // insert the menu entries for launching the applications
432 for ( size_t i = 0; i < SAL_N_ELEMENTS( aMenuItems ); ++i )
434 if ( !aModuleOptions.IsModuleInstalled( aMenuItems[i].eModuleIdentifier ) )
435 // the complete application is not even installed
438 OUString sURL( OUString::createFromAscii( aMenuItems[i].pAsciiURLDescription ) );
440 if ( aFileNewAppsAvailable.find( sURL ) == aFileNewAppsAvailable.end() )
441 // the application is installed, but the entry has been configured to *not* appear in the File/New
442 // menu => also let not appear it in the quickstarter
445 OUString aKeyEquiv( getShortCut( ShutdownIcon::GetUrlDescription( sURL ) ) );
447 appendMenuItem( pMenu, pDockMenu, ShutdownIcon::GetUrlDescription( sURL ), aMenuItems[i].nMenuTag, aKeyEquiv );
450 // insert the remaining menu entries
453 appendRecentMenu( pMenu, pDockMenu, SfxResId(STR_QUICKSTART_RECENTDOC) );
455 OUString aTitle( SfxResId(STR_QUICKSTART_FROMTEMPLATE) );
456 OUString aKeyEquiv( getShortCut( aTitle ) );
457 appendMenuItem( pMenu, pDockMenu, aTitle, MI_TEMPLATE, aKeyEquiv );
458 aTitle = SfxResId(STR_QUICKSTART_FILEOPEN);
459 aKeyEquiv = getShortCut( aTitle );
460 appendMenuItem( pMenu, pDockMenu, aTitle, MI_OPEN, aKeyEquiv );
462 [pDefMenu setSubmenu: pMenu];
463 [NSApp performSelector:@selector(addFallbackMenuItem:) withObject: pDefMenu];
465 if( [NSApp respondsToSelector: @selector(addDockMenuItem:)] )
467 [pDockSubMenu setSubmenu: pDockMenu];
469 [NSApp performSelector:@selector(addDockMenuItem:) withObject: pDockSubMenu];
472 OSL_FAIL( "addDockMenuItem selector failed on NSApp" );
475 OSL_FAIL( "addFallbackMenuItem selector failed on NSApp" );
479 void SAL_DLLPUBLIC_EXPORT aqua_shutdown_systray()
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */