xref: /aoo4110/main/sc/source/ui/navipi/scenwnd.cxx (revision b1cdbd2c)
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_sc.hxx"
26 
27 
28 
29 //------------------------------------------------------------------
30 
31 #include <sfx2/bindings.hxx>
32 #include <sfx2/dispatch.hxx>
33 #include <sfx2/viewfrm.hxx>
34 #include <svl/slstitm.hxx>
35 #include <svl/stritem.hxx>
36 #include <vcl/msgbox.hxx>
37 #include <vcl/svapp.hxx>
38 #include "navipi.hxx"
39 #include "popmenu.hxx"
40 #include "scresid.hxx"
41 #include "sc.hrc"
42 #include "globstr.hrc"
43 
44 //========================================================================
45 // class ScScenarioWindow ------------------------------------------------
46 //========================================================================
47 
ScScenarioListBox(ScScenarioWindow & rParent)48 ScScenarioListBox::ScScenarioListBox( ScScenarioWindow& rParent ) :
49     ListBox( &rParent, WB_BORDER | WB_TABSTOP ),
50     mrParent( rParent )
51 {
52 	Font aFont( GetFont() );
53 	aFont.SetTransparent( sal_True );
54 	aFont.SetWeight( WEIGHT_LIGHT );
55 	SetFont( aFont );
56 }
57 
~ScScenarioListBox()58 ScScenarioListBox::~ScScenarioListBox()
59 {
60 }
61 
UpdateEntries(List * pNewEntryList)62 void ScScenarioListBox::UpdateEntries( List* pNewEntryList )
63 {
64 	Clear();
65     maEntries.clear();
66 
67     if( !pNewEntryList )
68         return;
69 
70     switch( pNewEntryList->Count() )
71     {
72         case 0:
73             // no scenarios in current sheet
74             mrParent.SetComment( EMPTY_STRING );
75         break;
76 
77         case 1:
78             // sheet is a scenario container, comment only
79             mrParent.SetComment( *static_cast< String* >( pNewEntryList->First() ) );
80         break;
81 
82         default:
83         {
84             // sheet contains scenarios
85             DBG_ASSERT( pNewEntryList->Count() % 3 == 0, "ScScenarioListBox::UpdateEntries - wrong list size" );
86             SetUpdateMode( sal_False );
87             String* pEntry = static_cast< String* >( pNewEntryList->First() );
88             while( pEntry )
89             {
90                 ScenarioEntry aEntry;
91 
92                 // first entry of a triple is the scenario name
93                 aEntry.maName = *pEntry;
94                 // second entry of a triple is the scenario comment
95                 if( (pEntry = static_cast< String* >( pNewEntryList->Next() )) != 0 )
96                     aEntry.maComment = *pEntry;
97                 // third entry of a triple is the protection ("0" = not protected, "1" = protected)
98                 if( (pEntry = static_cast< String* >( pNewEntryList->Next() )) != 0 )
99                     aEntry.mbProtected = (pEntry->Len() > 0) && (pEntry->GetChar( 0 ) != '0');
100 
101                 maEntries.push_back( aEntry );
102                 InsertEntry( aEntry.maName, LISTBOX_APPEND );
103                 pEntry = static_cast< String* >( pNewEntryList->Next() );
104             }
105             SetUpdateMode( sal_True );
106             SetNoSelection();
107             mrParent.SetComment( EMPTY_STRING );
108         }
109     }
110 }
111 
Select()112 void ScScenarioListBox::Select()
113 {
114     if( const ScenarioEntry* pEntry = GetSelectedEntry() )
115         mrParent.SetComment( pEntry->maComment );
116 }
117 
DoubleClick()118 void ScScenarioListBox::DoubleClick()
119 {
120     SelectScenario();
121 }
122 
Notify(NotifyEvent & rNEvt)123 long ScScenarioListBox::Notify( NotifyEvent& rNEvt )
124 {
125     bool bHandled = false;
126 
127     if( rNEvt.GetType() == EVENT_KEYINPUT )
128     {
129         KeyCode aCode = rNEvt.GetKeyEvent()->GetKeyCode();
130         switch( aCode.GetCode() )
131         {
132             case KEY_RETURN:
133                 SelectScenario();
134                 bHandled = true;
135             break;
136             case KEY_DELETE:
137                 DeleteScenario( true );
138                 bHandled = true;
139             break;
140         }
141     }
142     else if ( rNEvt.GetType() == EVENT_COMMAND && GetSelectEntryCount() )
143     {
144         const CommandEvent* pCEvt = rNEvt.GetCommandEvent();
145         if ( pCEvt && pCEvt->GetCommand() == COMMAND_CONTEXTMENU )
146         {
147             if( const ScenarioEntry* pEntry = GetSelectedEntry() )
148             {
149                 if( !pEntry->mbProtected )
150                 {
151                     ScPopupMenu aPopup( ScResId( RID_POPUP_NAVIPI_SCENARIO ) );
152                     aPopup.Execute( this, pCEvt->GetMousePosPixel() );
153                     if (aPopup.WasHit())
154                     {
155                         switch( aPopup.GetSelected() )
156                         {
157                             case RID_NAVIPI_SCENARIO_DELETE:
158                                 DeleteScenario( true );
159                             break;
160                             case RID_NAVIPI_SCENARIO_EDIT:
161                                 EditScenario();
162                             break;
163                         }
164                     }
165                 }
166             }
167             bHandled = true;
168         }
169     }
170 
171     return bHandled ? 1 : ListBox::Notify( rNEvt );
172 }
173 
GetSelectedEntry() const174 const ScScenarioListBox::ScenarioEntry* ScScenarioListBox::GetSelectedEntry() const
175 {
176     size_t nPos = GetSelectEntryPos();
177     return (nPos < maEntries.size()) ? &maEntries[ nPos ] : 0;
178 }
179 
ExecuteScenarioSlot(sal_uInt16 nSlotId)180 void ScScenarioListBox::ExecuteScenarioSlot( sal_uInt16 nSlotId )
181 {
182     if( SfxViewFrame* pViewFrm = SfxViewFrame::Current() )
183     {
184         SfxStringItem aStringItem( nSlotId, GetSelectEntry() );
185         pViewFrm->GetDispatcher()->Execute( nSlotId, SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD, &aStringItem, 0L, 0L );
186     }
187 }
188 
SelectScenario()189 void ScScenarioListBox::SelectScenario()
190 {
191     if( GetSelectEntryCount() > 0 )
192         ExecuteScenarioSlot( SID_SELECT_SCENARIO );
193 }
194 
EditScenario()195 void ScScenarioListBox::EditScenario()
196 {
197     if( GetSelectEntryCount() > 0 )
198         ExecuteScenarioSlot( SID_EDIT_SCENARIO );
199 }
200 
DeleteScenario(bool bQueryBox)201 void ScScenarioListBox::DeleteScenario( bool bQueryBox )
202 {
203     if( GetSelectEntryCount() > 0 )
204         if( !bQueryBox || (::QueryBox( 0, WinBits( WB_YES_NO | WB_DEF_YES ), ScGlobal::GetRscString( STR_QUERY_DELSCENARIO ) ).Execute() == RET_YES) )
205             ExecuteScenarioSlot( SID_DELETE_SCENARIO );
206 }
207 
208 //========================================================================
209 // class ScScenarioWindow ------------------------------------------------
210 //========================================================================
211 
ScScenarioWindow(Window * pParent,const String & aQH_List,const String & aQH_Comment)212 ScScenarioWindow::ScScenarioWindow( Window* pParent,const String& aQH_List,
213 									const String& aQH_Comment)
214     :   Window      ( pParent, WB_TABSTOP | WB_DIALOGCONTROL ),
215         aLbScenario ( *this ),
216         aEdComment  ( this,  WB_BORDER | WB_LEFT | WB_READONLY | WB_VSCROLL | WB_TABSTOP )
217 {
218 	Font aFont( GetFont() );
219 	aFont.SetTransparent( sal_True );
220 	aFont.SetWeight( WEIGHT_LIGHT );
221 	aEdComment.SetFont( aFont );
222 	aEdComment.SetMaxTextLen( 512 );
223 	aLbScenario.SetPosPixel( Point(0,0) );
224 	aLbScenario.SetHelpId(HID_SC_SCENWIN_TOP);
225 	aEdComment.SetHelpId(HID_SC_SCENWIN_BOTTOM);
226 	aLbScenario.Show();
227 	aEdComment.Show();
228 
229 	aLbScenario.SetQuickHelpText(aQH_List);
230 	aEdComment.SetQuickHelpText(aQH_Comment);
231 	aEdComment.SetBackground( Color( COL_LIGHTGRAY ) );
232 
233 	SfxViewFrame* pViewFrm = SfxViewFrame::Current();
234 	if (pViewFrm)
235 	{
236 		SfxBindings& rBindings = pViewFrm->GetBindings();
237 		rBindings.Invalidate( SID_SELECT_SCENARIO );
238 		rBindings.Update( SID_SELECT_SCENARIO );
239 	}
240 }
241 
242 // -----------------------------------------------------------------------
243 
~ScScenarioWindow()244 ScScenarioWindow::~ScScenarioWindow()
245 {
246 }
247 
Paint(const Rectangle & rRec)248 void ScScenarioWindow::Paint( const Rectangle& rRec )
249 {
250 	const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
251 	Color aBgColor = rStyleSettings.GetFaceColor();
252 
253 	SetBackground( aBgColor );
254 
255 	Window::Paint( rRec );
256 }
257 
258 // -----------------------------------------------------------------------
259 
NotifyState(const SfxPoolItem * pState)260 void ScScenarioWindow::NotifyState( const SfxPoolItem* pState )
261 {
262 	if( pState )
263 	{
264 		aLbScenario.Enable();
265 
266 		if ( pState->ISA(SfxStringItem) )
267 		{
268 			String aNewEntry( ((const SfxStringItem*)pState)->GetValue() );
269 
270 			if ( aNewEntry.Len() > 0 )
271 				aLbScenario.SelectEntry( aNewEntry );
272 			else
273 				aLbScenario.SetNoSelection();
274 		}
275 		else if ( pState->ISA(SfxStringListItem) )
276 		{
277 			aLbScenario.UpdateEntries( ((SfxStringListItem*)pState)->GetList() );
278 		}
279 	}
280 	else
281 	{
282 		aLbScenario.Disable();
283 		aLbScenario.SetNoSelection();
284 	}
285 }
286 
287 // -----------------------------------------------------------------------
288 
SetSizePixel(const Size & rNewSize)289 void ScScenarioWindow::SetSizePixel( const Size& rNewSize )
290 {
291 	Size aSize( rNewSize );
292 	long nHeight = aSize.Height() / 2;
293 
294 	Window::SetSizePixel( aSize );
295 
296 	aSize.Height() = nHeight;
297 	aLbScenario.SetSizePixel( aSize );
298 
299 	aSize.Height() -= 4;
300 	aEdComment.SetPosSizePixel(	Point( 0, nHeight+4 ), aSize );
301 }
302 
303 
304 
305 
306