xref: /trunk/main/sd/source/ui/view/tabcontr.cxx (revision a0d53b35b9c5a6bd9856ab272d521493ba628169)
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_sd.hxx"
26 
27 #include "TabControl.hxx"
28 
29 #include <sfx2/viewfrm.hxx>
30 #include <svx/svdlayer.hxx>
31 #include <svx/svdpagv.hxx>
32 #include <sfx2/dispatch.hxx>
33 
34 
35 #include "sdattr.hxx"
36 #include "app.hxx"
37 #include "app.hrc"
38 #include "glob.hrc"
39 #include "res_bmp.hrc"
40 #include "DrawViewShell.hxx"
41 #include "GraphicViewShell.hxx"
42 #include "helpids.h"
43 #include "View.hxx"
44 #include "sdpage.hxx"
45 #include "drawdoc.hxx"
46 #include "Window.hxx"
47 #include "unmodpg.hxx"
48 #include "DrawDocShell.hxx"
49 #include "sdresid.hxx"
50 
51 
52 namespace sd {
53 
54 #define SWITCH_TIMEOUT  20
55 
56 // -----------------------------------------
57 // - SdTabControl::SdPageObjsTransferable -
58 // -----------------------------------------
59 
60 TabControl::TabControlTransferable::~TabControlTransferable()
61 {
62 }
63 
64 // -----------------------------------------------------------------------------
65 
66 void TabControl::TabControlTransferable::AddSupportedFormats()
67 {
68     AddFormat( SOT_FORMATSTR_ID_STARDRAW_TABBAR );
69 }
70 
71 // -----------------------------------------------------------------------------
72 
73 sal_Bool TabControl::TabControlTransferable::GetData( const ::com::sun::star::datatransfer::DataFlavor& )
74 {
75     return sal_False;
76 }
77 
78 // -----------------------------------------------------------------------------
79 
80 void TabControl::TabControlTransferable::DragFinished( sal_Int8 nDropAction )
81 {
82     mrParent.DragFinished( nDropAction );
83 }
84 
85 /*************************************************************************
86 |*
87 |* Standard-Konstruktor
88 |*
89 \************************************************************************/
90 
91 TabControl::TabControl(DrawViewShell* pViewSh, Window* pParent) :
92     TabBar( pParent, WinBits( WB_BORDER | WB_3DLOOK | WB_SCROLL | WB_SIZEABLE | WB_DRAG) ),
93     DragSourceHelper( this ),
94     DropTargetHelper( this ),
95     RrePageID(1),
96     pDrViewSh(pViewSh),
97     bInternalMove(sal_False)
98 {
99     EnableEditMode();
100     SetSizePixel(Size(0, 0));
101     SetMaxPageWidth( 150 );
102     SetHelpId( HID_SD_TABBAR_PAGES );
103 }
104 
105 /*************************************************************************
106 |*
107 |* Destruktor
108 |*
109 \************************************************************************/
110 
111 TabControl::~TabControl()
112 {
113 }
114 
115 /*************************************************************************
116 |*
117 \************************************************************************/
118 
119 void TabControl::Select()
120 {
121     SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
122     pDispatcher->Execute(SID_SWITCHPAGE, SFX_CALLMODE_ASYNCHRON |
123                             SFX_CALLMODE_RECORD);
124 }
125 
126 /*************************************************************************
127 |*
128 \************************************************************************/
129 
130 void  TabControl::MouseButtonDown(const MouseEvent& rMEvt)
131 {
132     if (rMEvt.IsLeft()
133         && !rMEvt.IsMod1()
134         && !rMEvt.IsMod2()
135         && !rMEvt.IsShift())
136     {
137         Point aPos = PixelToLogic( rMEvt.GetPosPixel() );
138         sal_uInt16 aPageId = GetPageId(aPos);
139 
140 //IAccessibility2 Implementation 2009-----
141         //Solution: initialize
142         if(RrePageID!=aPageId)
143             pDrViewSh->FreshNavigatrEntry();
144         RrePageID=aPageId;
145 //-----IAccessibility2 Implementation 2009
146         if (aPageId == 0)
147         {
148             SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
149 
150             pDispatcher->Execute(SID_INSERTPAGE_QUICK,
151                                 SFX_CALLMODE_SYNCHRON | SFX_CALLMODE_RECORD);
152         }
153     }
154 
155     // A single left click with pressed control key on a tab page first
156     // switches to that page before the usual handling (copying with drag
157     // and drop) takes place.
158     else if (rMEvt.IsLeft() && rMEvt.IsMod1() && !rMEvt.IsMod2() && !rMEvt.IsShift())
159     {
160         pDrViewSh->SwitchPage (GetPageId (rMEvt.GetPosPixel()) - 1);
161     }
162 
163     // When only the right button is pressed then first process a
164     // synthesized left button click to make the page the current one
165     // whose tab has been clicked.  When then the actual right button
166     // click is processed the resulting context menu relates to the
167     // now current page.
168     if (rMEvt.IsRight() && ! rMEvt.IsLeft())
169     {
170         MouseEvent aSyntheticEvent (
171             rMEvt.GetPosPixel(),
172             rMEvt.GetClicks(),
173             rMEvt.GetMode(),
174             MOUSE_LEFT,
175             rMEvt.GetModifier());
176         TabBar::MouseButtonDown(aSyntheticEvent);
177     }
178 
179     TabBar::MouseButtonDown(rMEvt);
180 }
181 
182 /*************************************************************************
183 |*
184 \************************************************************************/
185 
186 void TabControl::DoubleClick()
187 {
188     if (GetCurPageId() != 0)
189     {
190         SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
191         pDispatcher->Execute( SID_MODIFYPAGE,
192                         SFX_CALLMODE_SYNCHRON | SFX_CALLMODE_RECORD );
193     }
194 }
195 
196 /*************************************************************************
197 |*
198 |* StartDrag-Request
199 |*
200 \************************************************************************/
201 
202 void TabControl::StartDrag( sal_Int8, const Point& )
203 {
204     bInternalMove = sal_True;
205 
206     // object is delete by reference mechanismn
207     ( new TabControl::TabControlTransferable( *this ) )->StartDrag( this, DND_ACTION_COPYMOVE );
208 }
209 
210 /*************************************************************************
211 |*
212 |* DragFinished
213 |*
214 \************************************************************************/
215 
216 void TabControl::DragFinished( sal_Int8 )
217 {
218     bInternalMove = sal_False;
219 }
220 
221 /*************************************************************************
222 |*
223 |* AcceptDrop-Event
224 |*
225 \************************************************************************/
226 
227 sal_Int8 TabControl::AcceptDrop( const AcceptDropEvent& rEvt )
228 {
229     sal_Int8 nRet = DND_ACTION_NONE;
230 
231     if( rEvt.mbLeaving )
232         EndSwitchPage();
233 
234     if( !pDrViewSh->GetDocSh()->IsReadOnly() )
235     {
236         SdDrawDocument* pDoc = pDrViewSh->GetDoc();
237         Point           aPos( rEvt.maPosPixel );
238 
239         if( bInternalMove )
240         {
241             if( rEvt.mbLeaving || ( pDrViewSh->GetEditMode() == EM_MASTERPAGE ) )
242                 HideDropPos();
243             else
244             {
245                 ShowDropPos( aPos );
246                 nRet = rEvt.mnAction;
247             }
248         }
249         else
250         {
251             HideDropPos();
252 
253             sal_Int32 nPageId = GetPageId( aPos ) - 1;
254 
255             if( ( nPageId >= 0 ) && pDoc->GetPage( (sal_uInt16)nPageId ) )
256             {
257                 nRet = pDrViewSh->AcceptDrop( rEvt, *this, NULL, (sal_uInt16)nPageId, SDRLAYER_NOTFOUND );
258                 SwitchPage( aPos );
259             }
260         }
261     }
262 
263     return nRet;
264 }
265 
266 /*************************************************************************
267 |*
268 |* ExecuteDrop-Event
269 |*
270 \************************************************************************/
271 
272 sal_Int8 TabControl::ExecuteDrop( const ExecuteDropEvent& rEvt )
273 {
274     SdDrawDocument* pDoc = pDrViewSh->GetDoc();
275     Point           aPos( rEvt.maPosPixel );
276     sal_Int8        nRet = DND_ACTION_NONE;
277 
278     if( bInternalMove )
279     {
280         sal_uInt16 nPageId = ShowDropPos( aPos ) - 1;
281 
282         switch (rEvt.mnAction)
283         {
284             case DND_ACTION_MOVE:
285                 if( pDrViewSh->IsSwitchPageAllowed() && pDoc->MovePages( nPageId ) )
286                 {
287                     SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
288                     pDispatcher->Execute(SID_SWITCHPAGE, SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD);
289                 }
290                 break;
291 
292             case DND_ACTION_COPY:
293             {
294                 // Copying the selected page to the place that rEvt points
295                 // takes place in three steps:
296                 // 1. Create a copy of the selected page.  This copy will
297                 // lie directly behind the selected page.
298                 // 2. Move the copy to the desired place.
299                 // 3. Select the copy.
300                 if (pDrViewSh->IsSwitchPageAllowed())
301                 {
302                     // 1. Create a copy.
303                     sal_uInt16 nPageNumOfCopy = pDoc->DuplicatePage (GetCurPageId() - 1);
304                     // 2. Move page.  For this first switch to the copy:
305                     // MovePages operates on the currently selected page(s).
306                     pDrViewSh->SwitchPage (nPageNumOfCopy);
307                     // Adapt target page id when necessary, i.e. page copy
308                     // has been inserted in front of the target page.
309                     sal_uInt16 nPageNum = nPageId;
310                     if ((nPageNumOfCopy <= nPageNum) && (nPageNum != (sal_uInt16)-1))
311                         nPageNum += 1;
312                     if (pDoc->MovePages(nPageNum))
313                     {
314                         // 3. Switch to the copy that has been moved to its
315                         // final destination.  Use an asynchron slot call to
316                         // be executed after the still pending ones.
317                         if (nPageNumOfCopy >= nPageNum || (nPageNum == (sal_uInt16)-1))
318                             nPageNum += 1;
319                         SetCurPageId (GetPageId(nPageNum));
320                         SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
321                         pDispatcher->Execute(SID_SWITCHPAGE,
322                             SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD);
323                     }
324                 }
325 
326                 break;
327             }
328         }
329 
330         nRet = rEvt.mnAction;
331     }
332     else
333     {
334         sal_Int32 nPageId = GetPageId( aPos ) - 1;
335 
336         if( ( nPageId >= 0 ) && pDoc->GetPage( (sal_uInt16)nPageId ) )
337         {
338             nRet = pDrViewSh->ExecuteDrop( rEvt, *this, NULL, (sal_uInt16)nPageId, SDRLAYER_NOTFOUND );
339         }
340     }
341 
342     HideDropPos();
343     EndSwitchPage();
344 
345     return nRet;
346 }
347 
348 /*************************************************************************
349 |*
350 \************************************************************************/
351 
352 void TabControl::Command(const CommandEvent& rCEvt)
353 {
354     sal_uInt16 nCmd = rCEvt.GetCommand();
355 
356     if ( nCmd == COMMAND_CONTEXTMENU )
357     {
358         sal_Bool bGraphicShell = pDrViewSh->ISA(GraphicViewShell);
359         sal_uInt16 nResId = bGraphicShell ? RID_GRAPHIC_PAGETAB_POPUP :
360                                         RID_DRAW_PAGETAB_POPUP;
361         SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
362         pDispatcher->ExecutePopup( SdResId( nResId ) );
363     }
364 }
365 
366 /*************************************************************************
367 |*
368 \************************************************************************/
369 
370 long TabControl::StartRenaming()
371 {
372     sal_Bool bOK = sal_False;
373 
374     if (pDrViewSh->GetPageKind() == PK_STANDARD)
375     {
376         bOK = sal_True;
377 
378         ::sd::View* pView = pDrViewSh->GetView();
379 
380         if ( pView->IsTextEdit() )
381             pView->SdrEndTextEdit();
382     }
383 
384     return( bOK );
385 }
386 
387 /*************************************************************************
388 |*
389 \************************************************************************/
390 
391 long TabControl::AllowRenaming()
392 {
393     sal_Bool bOK = sal_True;
394 
395     String aNewName( GetEditText() );
396     String aCompareName( GetPageText( GetEditPageId() ) );
397 
398     if( aCompareName != aNewName )
399     {
400         // Seite umbenennen
401         if( pDrViewSh->GetDocSh()->CheckPageName( this, aNewName ) )
402         {
403             SetEditText( aNewName );
404             EndRenaming();
405         }
406         else
407         {
408             bOK = sal_False;
409         }
410     }
411     return( bOK );
412 }
413 
414 /*************************************************************************
415 |*
416 \************************************************************************/
417 
418 void TabControl::EndRenaming()
419 {
420     if( !IsEditModeCanceled() )
421         pDrViewSh->RenameSlide( GetEditPageId(), GetEditText() );
422 }
423 
424 
425 /*************************************************************************
426 |*
427 \************************************************************************/
428 
429 void TabControl::ActivatePage()
430 {
431     if ( /*IsInSwitching && */ pDrViewSh->IsSwitchPageAllowed() )
432     {
433         SfxDispatcher* pDispatcher = pDrViewSh->GetViewFrame()->GetDispatcher();
434         pDispatcher->Execute(SID_SWITCHPAGE,
435                              SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD);
436     }
437 }
438 
439 
440 /*************************************************************************
441 |*
442 \************************************************************************/
443 
444 long TabControl::DeactivatePage()
445 {
446     return pDrViewSh->IsSwitchPageAllowed();
447 }
448 
449 
450 
451 
452 void TabControl::SendActivatePageEvent (void)
453 {
454     CallEventListeners (VCLEVENT_TABBAR_PAGEACTIVATED,
455         reinterpret_cast<void*>(GetCurPageId()));
456 }
457 
458 
459 
460 
461 void TabControl::SendDeactivatePageEvent (void)
462 {
463     CallEventListeners (VCLEVENT_TABBAR_PAGEDEACTIVATED,
464         reinterpret_cast<void*>(GetCurPageId()));
465 }
466 
467 } // end of namespace sd
468