xref: /trunk/main/sd/source/ui/func/fusnapln.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 
31 #include "fusnapln.hxx"
32 #include <svl/aeitem.hxx>
33 #include <vcl/msgbox.hxx>
34 #include <sfx2/request.hxx>
35 
36 
37 #include "strings.hrc"
38 
39 #include "sdattr.hxx"
40 #include "View.hxx"
41 #include "ViewShell.hxx"
42 #include "DrawViewShell.hxx"
43 #ifndef SD_WINDOW_SHELL_HXX
44 #include "Window.hxx"
45 #endif
46 #include "sdenumdef.hxx"
47 #include "sdresid.hxx"
48 #include "sdabstdlg.hxx"
49 #include "app.hrc"
50 #include <svx/svdpagv.hxx>
51 
52 namespace sd {
53 
54 TYPEINIT1( FuSnapLine, FuPoor );
55 
56 /*************************************************************************
57 |*
58 |* Konstruktor
59 |*
60 \************************************************************************/
61 
62 FuSnapLine::FuSnapLine(ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView,
63                        SdDrawDocument* pDoc, SfxRequest& rReq) :
64     FuPoor(pViewSh, pWin, pView, pDoc, rReq)
65 {
66 }
67 
68 FunctionReference FuSnapLine::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
69 {
70     FunctionReference xFunc( new FuSnapLine( pViewSh, pWin, pView, pDoc, rReq ) );
71     xFunc->DoExecute(rReq);
72     return xFunc;
73 }
74 
75 void FuSnapLine::DoExecute( SfxRequest& rReq )
76 {
77     const SfxItemSet* pArgs = rReq.GetArgs();
78     SdrPageView* pPV = 0;
79     sal_uInt16  nHelpLine = 0;
80     sal_Bool    bCreateNew = sal_True;
81 
82     // Get index of snap line or snap point from the request.
83     SFX_REQUEST_ARG (rReq, pHelpLineIndex, SfxUInt32Item, ID_VAL_INDEX, sal_False);
84     if (pHelpLineIndex != NULL)
85     {
86         nHelpLine = static_cast<sal_uInt16>(pHelpLineIndex->GetValue());
87         // Reset the argument pointer to trigger the display of the dialog.
88         pArgs = NULL;
89     }
90 
91     if ( !pArgs )
92     {
93         SfxItemSet aNewAttr(mpViewShell->GetPool(), ATTR_SNAPLINE_START, ATTR_SNAPLINE_END);
94         bool bLineExist (false);
95         pPV = mpView->GetSdrPageView();
96         Point aLinePos;
97 
98         if (pHelpLineIndex == NULL)
99         {
100             // The index of the snap line is not provided as argument to the
101             // request.  Determine it from the mouse position.
102 
103             aLinePos = static_cast<DrawViewShell*>(mpViewShell)->GetMousePos();
104             static_cast<DrawViewShell*>(mpViewShell)->SetMousePosFreezed( sal_False );
105 
106 
107             if ( aLinePos.X() >= 0 )
108             {
109                 aLinePos = mpWindow->PixelToLogic(aLinePos);
110                 sal_uInt16 nHitLog = (sal_uInt16) mpWindow->PixelToLogic(Size(HITPIX,0)).Width();
111                 bLineExist = mpView->PickHelpLine(aLinePos, nHitLog, *mpWindow, nHelpLine, pPV);
112                 if ( bLineExist )
113                     aLinePos = (pPV->GetHelpLines())[nHelpLine].GetPos();
114                 else
115                     pPV = mpView->GetSdrPageView();
116 
117                 pPV->LogicToPagePos(aLinePos);
118             }
119             else
120                 aLinePos = Point(0,0);
121         }
122         else
123         {
124             OSL_ASSERT(pPV!=NULL);
125             aLinePos = (pPV->GetHelpLines())[nHelpLine].GetPos();
126             pPV->LogicToPagePos(aLinePos);
127             bLineExist = true;
128         }
129         aNewAttr.Put(SfxUInt32Item(ATTR_SNAPLINE_X, aLinePos.X()));
130         aNewAttr.Put(SfxUInt32Item(ATTR_SNAPLINE_Y, aLinePos.Y()));
131 
132         SdAbstractDialogFactory* pFact = SdAbstractDialogFactory::Create();
133         AbstractSdSnapLineDlg* pDlg = pFact ? pFact->CreateSdSnapLineDlg( NULL, aNewAttr, mpView ) : 0;
134         if( pDlg )
135         {
136             if ( bLineExist )
137             {
138                 pDlg->HideRadioGroup();
139 
140                 const SdrHelpLine& rHelpLine = (pPV->GetHelpLines())[nHelpLine];
141 
142                 if ( rHelpLine.GetKind() == SDRHELPLINE_POINT )
143                 {
144                     pDlg->SetText(String(SdResId(STR_SNAPDLG_SETPOINT)));
145                     pDlg->SetInputFields(sal_True, sal_True);
146                 }
147                 else
148                 {
149                     pDlg->SetText(String(SdResId(STR_SNAPDLG_SETLINE)));
150 
151                     if ( rHelpLine.GetKind() == SDRHELPLINE_VERTICAL )
152                         pDlg->SetInputFields(sal_True, sal_False);
153                     else
154                         pDlg->SetInputFields(sal_False, sal_True);
155                 }
156                 bCreateNew = sal_False;
157             }
158             else
159                 pDlg->HideDeleteBtn();
160 
161             sal_uInt16 nResult = pDlg->Execute();
162 
163             pDlg->GetAttr(aNewAttr);
164             delete pDlg;
165 
166             switch( nResult )
167             {
168                 case RET_OK:
169                     rReq.Done(aNewAttr);
170                     pArgs = rReq.GetArgs();
171                     break;
172 
173                 case RET_SNAP_DELETE:
174                     // Fangobjekt loeschen
175                     if ( !bCreateNew )
176                         pPV->DeleteHelpLine(nHelpLine);
177                     // und weiter wie bei default
178                 default:
179                     return;
180             }
181         }
182     }
183     Point aHlpPos;
184 
185     aHlpPos.X() = ((const SfxUInt32Item&) pArgs->Get(ATTR_SNAPLINE_X)).GetValue();
186     aHlpPos.Y() = ((const SfxUInt32Item&) pArgs->Get(ATTR_SNAPLINE_Y)).GetValue();
187     pPV->PagePosToLogic(aHlpPos);
188 
189     if ( bCreateNew )
190     {
191         SdrHelpLineKind eKind;
192 
193         pPV = mpView->GetSdrPageView();
194 
195         switch ( (SnapKind) ((const SfxAllEnumItem&)
196                  pArgs->Get(ATTR_SNAPLINE_KIND)).GetValue() )
197         {
198             case SK_HORIZONTAL  : eKind = SDRHELPLINE_HORIZONTAL;   break;
199             case SK_VERTICAL    : eKind = SDRHELPLINE_VERTICAL;     break;
200             default             : eKind = SDRHELPLINE_POINT;        break;
201         }
202         pPV->InsertHelpLine(SdrHelpLine(eKind, aHlpPos));
203     }
204     else
205     {
206         const SdrHelpLine& rHelpLine = (pPV->GetHelpLines())[nHelpLine];
207         pPV->SetHelpLine(nHelpLine, SdrHelpLine(rHelpLine.GetKind(), aHlpPos));
208     }
209 }
210 
211 void FuSnapLine::Activate()
212 {
213 }
214 
215 void FuSnapLine::Deactivate()
216 {
217 }
218 
219 } // end of namespace sd
220