xref: /trunk/main/vcl/aqua/source/app/salsys.cxx (revision 9f62ea84)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include "tools/rc.hxx"
28 
29 #include "rtl/ustrbuf.hxx"
30 
31 #include "vcl/button.hxx"
32 
33 #include "aqua/salsys.h"
34 #include "aqua/saldata.hxx"
35 #include "aqua/salinst.h"
36 
37 #include "svids.hrc"
38 
39 using namespace rtl;
40 
41 // =======================================================================
42 
~AquaSalSystem()43 AquaSalSystem::~AquaSalSystem()
44 {
45 }
46 
GetDisplayScreenCount()47 unsigned int AquaSalSystem::GetDisplayScreenCount()
48 {
49     NSArray* pScreens = [NSScreen screens];
50 	return pScreens ? [pScreens count] : 1;
51 }
52 
IsMultiDisplay()53 bool AquaSalSystem::IsMultiDisplay()
54 {
55     return false;
56 }
57 
GetDefaultDisplayNumber()58 unsigned int AquaSalSystem::GetDefaultDisplayNumber()
59 {
60     return 0;
61 }
62 
GetDisplayScreenPosSizePixel(unsigned int nScreen)63 Rectangle AquaSalSystem::GetDisplayScreenPosSizePixel( unsigned int nScreen )
64 {
65     NSArray* pScreens = [NSScreen screens];
66     Rectangle aRet;
67     NSScreen* pScreen = nil;
68     if( pScreens && nScreen < [pScreens count] )
69         pScreen = [pScreens objectAtIndex: nScreen];
70     else
71         pScreen = [NSScreen mainScreen];
72 
73     if( pScreen )
74     {
75         NSRect aFrame = [pScreen frame];
76         aRet = Rectangle( Point( static_cast<long int>(aFrame.origin.x), static_cast<long int>(aFrame.origin.y) ),
77                           Size( static_cast<long int>(aFrame.size.width), static_cast<long int>(aFrame.size.height) ) );
78     }
79 	return aRet;
80 }
81 
GetDisplayWorkAreaPosSizePixel(unsigned int nScreen)82 Rectangle AquaSalSystem::GetDisplayWorkAreaPosSizePixel( unsigned int nScreen )
83 {
84     NSArray* pScreens = [NSScreen screens];
85     Rectangle aRet;
86     NSScreen* pScreen = nil;
87     if( pScreens && nScreen < [pScreens count] )
88         pScreen = [pScreens objectAtIndex: nScreen];
89     else
90         pScreen = [NSScreen mainScreen];
91 
92     if( pScreen )
93     {
94         NSRect aFrame = [pScreen visibleFrame];
95         aRet = Rectangle( Point( static_cast<long int>(aFrame.origin.x), static_cast<long int>(aFrame.origin.y) ),
96                           Size( static_cast<long int>(aFrame.size.width), static_cast<long int>(aFrame.size.height) ) );
97     }
98 	return aRet;
99 }
100 
GetScreenName(unsigned int nScreen)101 rtl::OUString AquaSalSystem::GetScreenName( unsigned int nScreen )
102 {
103    NSArray* pScreens = [NSScreen screens];
104    OUString aRet;
105    if( nScreen < [pScreens count] )
106    {
107         ResMgr* pMgr = ImplGetResMgr();
108         if( pMgr )
109         {
110             String aScreenName( ResId( SV_MAC_SCREENNNAME, *pMgr ) );
111             aScreenName.SearchAndReplaceAllAscii( "%d", String::CreateFromInt32( nScreen ) );
112             aRet = aScreenName;
113         }
114    }
115    return aRet;
116 }
117 
getStandardString(int nButtonId)118 static NSString* getStandardString( int nButtonId )
119 {
120     rtl::OUString aText( Button::GetStandardText( nButtonId ) );
121     if( ! aText.getLength() ) // this is for bad cases, we might be missing the vcl resource
122     {
123         switch( nButtonId )
124         {
125         case BUTTON_OK:         aText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OK" ) );break;
126         case BUTTON_ABORT:      aText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Abort" ) );break;
127         case BUTTON_CANCEL:     aText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Cancel" ) );break;
128         case BUTTON_RETRY:      aText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Retry" ) );break;
129         case BUTTON_YES:        aText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Yes" ) );break;
130         case BUTTON_NO :        aText = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No" ) );break;
131         }
132     }
133     return aText.getLength() ? CreateNSString( aText) : nil;
134 }
135 
ShowNativeMessageBox(const String & rTitle,const String & rMessage,int nButtonCombination,int nDefaultButton)136 int AquaSalSystem::ShowNativeMessageBox( const String& rTitle,
137                                         const String& rMessage,
138                                         int nButtonCombination,
139                                         int nDefaultButton)
140 {
141     NSString* pTitle = CreateNSString( rTitle );
142     NSString* pMessage = CreateNSString( rMessage );
143 
144     struct id_entry
145     {
146         int nCombination;
147         int nDefaultButton;
148         int nTextIds[3];
149     } aButtonIds[] =
150     {
151         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_OK, { BUTTON_OK, -1, -1 } },
152         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK_CANCEL, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_OK, { BUTTON_OK, BUTTON_CANCEL, -1 } },
153         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_OK_CANCEL, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_CANCEL, { BUTTON_CANCEL, BUTTON_OK, -1 } },
154         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_ABORT_RETRY_IGNORE, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_ABORT, { BUTTON_ABORT, BUTTON_IGNORE, BUTTON_RETRY } },
155         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_ABORT_RETRY_IGNORE, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY, { BUTTON_RETRY, BUTTON_IGNORE, BUTTON_ABORT } },
156         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_ABORT_RETRY_IGNORE, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_IGNORE, { BUTTON_IGNORE, BUTTON_IGNORE, BUTTON_ABORT } },
157         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO_CANCEL, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_YES, { BUTTON_YES, BUTTON_NO, BUTTON_CANCEL } },
158         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO_CANCEL, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_NO, { BUTTON_NO, BUTTON_YES, BUTTON_CANCEL } },
159         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO_CANCEL, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_CANCEL, { BUTTON_CANCEL, BUTTON_YES, BUTTON_NO } },
160         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_YES, { BUTTON_YES, BUTTON_NO, -1 } },
161         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_YES_NO, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_NO, { BUTTON_NO, BUTTON_YES, -1 } },
162         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_RETRY_CANCEL, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY, { BUTTON_RETRY, BUTTON_CANCEL, -1 } },
163         { SALSYSTEM_SHOWNATIVEMSGBOX_BTNCOMBI_RETRY_CANCEL, SALSYSTEM_SHOWNATIVEMSGBOX_BTN_CANCEL, { BUTTON_CANCEL, BUTTON_RETRY, -1 } }
164     };
165 
166     NSString* pDefText = nil;
167     NSString* pAltText = nil;
168     NSString* pOthText = nil;
169 
170     unsigned int nC;
171     for( nC = 0; nC < sizeof(aButtonIds)/sizeof(aButtonIds[0]); nC++ )
172     {
173         if( aButtonIds[nC].nCombination == nButtonCombination )
174         {
175             if( aButtonIds[nC].nDefaultButton == nDefaultButton )
176             {
177                 if( aButtonIds[nC].nTextIds[0] != -1 )
178                     pDefText = getStandardString( aButtonIds[nC].nTextIds[0] );
179                 if( aButtonIds[nC].nTextIds[1] != -1 )
180                     pAltText = getStandardString( aButtonIds[nC].nTextIds[1] );
181                 if( aButtonIds[nC].nTextIds[2] != -1 )
182                     pOthText = getStandardString( aButtonIds[nC].nTextIds[2] );
183                 break;
184             }
185         }
186     }
187 
188 
189     int nResult = NSRunAlertPanel( pTitle, pMessage, pDefText, pAltText, pOthText );
190 
191     if( pTitle )
192         [pTitle release];
193     if( pMessage )
194         [pMessage release];
195     if( pDefText )
196         [pDefText release];
197     if( pAltText )
198         [pAltText release];
199     if( pOthText )
200         [pOthText release];
201 
202     int nRet = 0;
203     if( nC < sizeof(aButtonIds)/sizeof(aButtonIds[0]) && nResult >= 1 && nResult <= 3 )
204     {
205         int nPressed = aButtonIds[nC].nTextIds[nResult-1];
206         switch( nPressed )
207         {
208         case BUTTON_NO:     nRet = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_NO; break;
209         case BUTTON_YES:    nRet = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_YES; break;
210         case BUTTON_OK:     nRet = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_OK; break;
211         case BUTTON_CANCEL: nRet = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_CANCEL; break;
212         case BUTTON_ABORT:  nRet = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_ABORT; break;
213         case BUTTON_RETRY:  nRet = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_RETRY; break;
214         case BUTTON_IGNORE: nRet = SALSYSTEM_SHOWNATIVEMSGBOX_BTN_IGNORE; break;
215         }
216     }
217 
218     return nRet;
219 }
220