Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <svl/eitem.hxx>
21 : #include <sfx2/app.hxx>
22 : #include <svl/intitem.hxx>
23 : #include <svtools/imgdef.hxx>
24 : #include <sfx2/dispatch.hxx>
25 : #include <sfx2/imgmgr.hxx>
26 : #include <vcl/wrkwin.hxx>
27 : #include <vcl/settings.hxx>
28 :
29 : #include "toolbox.hxx"
30 : #include "starmath.hrc"
31 : #include "toolbox.hrc"
32 : #include "view.hxx"
33 :
34 :
35 :
36 :
37 34 : static sal_uInt16 GetImageListRID( sal_uInt16 nCategoryRID )
38 : {
39 34 : sal_uInt16 nRes = 0xFFFF;
40 34 : switch (nCategoryRID)
41 : {
42 34 : case RID_UNBINOPS_CAT : nRes = RID_IL_UNBINOPS; break;
43 0 : case RID_RELATIONS_CAT : nRes = RID_IL_RELATIONS; break;
44 0 : case RID_SETOPERATIONS_CAT : nRes = RID_IL_SETOPERATIONS; break;
45 0 : case RID_FUNCTIONS_CAT : nRes = RID_IL_FUNCTIONS; break;
46 0 : case RID_OPERATORS_CAT : nRes = RID_IL_OPERATORS; break;
47 0 : case RID_ATTRIBUTES_CAT : nRes = RID_IL_ATTRIBUTES; break;
48 0 : case RID_BRACKETS_CAT : nRes = RID_IL_BRACKETS; break;
49 0 : case RID_FORMAT_CAT : nRes = RID_IL_FORMAT; break;
50 0 : case RID_MISC_CAT : nRes = RID_IL_MISC; break;
51 : default :
52 : SAL_WARN( "starmath", "unknown category" );
53 : }
54 34 : return nRes;
55 : }
56 :
57 :
58 136 : static sal_Int16 GetToolBoxCategoriesIndex( sal_uInt16 nCategoryRID )
59 : {
60 136 : sal_Int16 nIdx = -1;
61 136 : switch (nCategoryRID)
62 : {
63 136 : case RID_UNBINOPS_CAT : nIdx = 0; break;
64 0 : case RID_RELATIONS_CAT : nIdx = 1; break;
65 0 : case RID_SETOPERATIONS_CAT : nIdx = 2; break;
66 0 : case RID_FUNCTIONS_CAT : nIdx = 3; break;
67 0 : case RID_OPERATORS_CAT : nIdx = 4; break;
68 0 : case RID_ATTRIBUTES_CAT : nIdx = 5; break;
69 0 : case RID_BRACKETS_CAT : nIdx = 6; break;
70 0 : case RID_FORMAT_CAT : nIdx = 7; break;
71 0 : case RID_MISC_CAT : nIdx = 8; break;
72 : default:
73 : ;
74 : }
75 136 : return nIdx;
76 : }
77 :
78 :
79 34 : static sal_uInt16 GetCategoryRID( sal_uInt16 nResId )
80 : {
81 34 : sal_uInt16 nRes = 0xFFFF;
82 34 : switch (nResId)
83 : {
84 34 : case RID_IL_UNBINOPS : nRes = RID_UNBINOPS_CAT; break;
85 0 : case RID_IL_RELATIONS : nRes = RID_RELATIONS_CAT; break;
86 0 : case RID_IL_SETOPERATIONS : nRes = RID_SETOPERATIONS_CAT; break;
87 0 : case RID_IL_FUNCTIONS : nRes = RID_FUNCTIONS_CAT; break;
88 0 : case RID_IL_OPERATORS : nRes = RID_OPERATORS_CAT; break;
89 0 : case RID_IL_ATTRIBUTES : nRes = RID_ATTRIBUTES_CAT; break;
90 0 : case RID_IL_BRACKETS : nRes = RID_BRACKETS_CAT; break;
91 0 : case RID_IL_FORMAT : nRes = RID_FORMAT_CAT; break;
92 0 : case RID_IL_MISC : nRes = RID_MISC_CAT; break;
93 : default :
94 : SAL_WARN( "starmath", "unknown category" );
95 0 : break;
96 : }
97 34 : return nRes;
98 : }
99 :
100 34 : SmToolBoxWindow::SmToolBoxWindow(SfxBindings *pTmpBindings,
101 : SfxChildWindow *pChildWindow,
102 : vcl::Window *pParent)
103 : : SfxFloatingWindow(pTmpBindings, pChildWindow, pParent, "FloatingElements",
104 34 : "modules/smath/ui/floatingelements.ui")
105 : {
106 34 : get(m_pToolBoxCat, "catalog");
107 34 : m_nUnbinopsId = m_pToolBoxCat->GetItemId("RID_UNBINOPS_CAT");
108 34 : m_nRelationsId = m_pToolBoxCat->GetItemId("RID_RELATIONS_CAT");
109 34 : m_nSetoperationsId = m_pToolBoxCat->GetItemId("RID_SETOPERATIONS_CAT");
110 34 : m_nFunctionsId = m_pToolBoxCat->GetItemId("RID_FUNCTIONS_CAT");
111 34 : m_nOperatorsId = m_pToolBoxCat->GetItemId("RID_OPERATORS_CAT");
112 34 : m_nAttributesId = m_pToolBoxCat->GetItemId("RID_ATTRIBUTES_CAT");
113 34 : m_nBracketsId = m_pToolBoxCat->GetItemId("RID_BRACKETS_CAT");
114 34 : m_nFormatId = m_pToolBoxCat->GetItemId("RID_FORMAT_CAT");
115 34 : m_nMiscId = m_pToolBoxCat->GetItemId("RID_MISC_CAT");
116 34 : m_pToolBoxCat->InsertSpace(7);
117 34 : m_pToolBoxCat->InsertBreak(5);
118 34 : m_pToolBoxCat->SetLineCount(2);
119 :
120 : // allow for cursor travelling between toolbox and sub-categories
121 34 : SetStyle( GetStyle() | WB_DIALOGCONTROL );
122 :
123 34 : nActiveCategoryRID = USHRT_MAX;
124 :
125 34 : m_pToolBoxCat->SetClickHdl(LINK(this, SmToolBoxWindow, CategoryClickHdl));
126 :
127 : sal_uInt16 i;
128 340 : for (i = 0; i < NUM_TBX_CATEGORIES; ++i)
129 : {
130 306 : ToolBox *pBox = new ToolBox(get<vcl::Window>("box"), SmResId( TOOLBOX_CAT_A + i ));
131 306 : vToolBoxCategories[i] = pBox;
132 306 : pBox->SetSelectHdl(LINK(this, SmToolBoxWindow, CmdSelectHdl));
133 : }
134 34 : pToolBoxCmd = vToolBoxCategories[0];
135 :
136 374 : for (i = 0; i <= NUM_TBX_CATEGORIES; ++i)
137 340 : aImageLists [i] = 0;
138 34 : }
139 :
140 102 : SmToolBoxWindow::~SmToolBoxWindow()
141 : {
142 : int i;
143 340 : for (i = 0; i < NUM_TBX_CATEGORIES; ++i)
144 : {
145 306 : ToolBox *pBox = vToolBoxCategories[i];
146 306 : delete pBox;
147 : }
148 374 : for (i = 0; i < NUM_TBX_CATEGORIES + 1; ++i)
149 340 : delete aImageLists[i];
150 68 : }
151 :
152 4 : SmViewShell * SmToolBoxWindow::GetView()
153 : {
154 4 : SfxViewShell *pView = GetBindings().GetDispatcher()->GetFrame()->GetViewShell();
155 4 : return PTR_CAST(SmViewShell, pView);
156 : }
157 :
158 34 : const ImageList * SmToolBoxWindow::GetImageList( sal_uInt16 nResId )
159 : {
160 : // creates the image list via its resource id and stores that
161 : // list for later use in the respective array.
162 :
163 34 : const ImageList *pIL = 0;
164 :
165 : // get index to use
166 34 : sal_uInt16 nCategoryRID = GetCategoryRID( nResId );
167 34 : sal_Int16 nIndex = GetToolBoxCategoriesIndex( nCategoryRID );
168 :
169 34 : if (nIndex >= 0)
170 : {
171 34 : ImageList **pImgList = aImageLists;
172 34 : if (!pImgList[ nIndex ])
173 34 : pImgList[ nIndex ] = new ImageList( SmResId(nResId) );
174 34 : pIL = pImgList[ nIndex ];
175 : }
176 :
177 : OSL_ENSURE( pIL, "image list not found!" );
178 34 : return pIL;
179 : }
180 :
181 :
182 34 : void SmToolBoxWindow::ApplyImageLists( sal_uInt16 nCategoryRID )
183 : {
184 : // set image list for active (visible) category of 'catalog'
185 34 : sal_Int16 nIdx = GetToolBoxCategoriesIndex( nCategoryRID );
186 34 : sal_uInt16 nResId = GetImageListRID( nCategoryRID );
187 34 : const ImageList *pImageList = GetImageList( nResId );
188 : OSL_ENSURE( pImageList && nIdx >= 0, "image list or index missing" );
189 34 : if (pImageList && nIdx >= 0)
190 34 : vToolBoxCategories[ nIdx ]->SetImageList( *pImageList );
191 34 : }
192 :
193 0 : void SmToolBoxWindow::DataChanged( const DataChangedEvent &rEvt )
194 : {
195 0 : if ( (rEvt.GetType() == DATACHANGED_SETTINGS) && (rEvt.GetFlags() & SETTINGS_STYLE) )
196 0 : ApplyImageLists( nActiveCategoryRID );
197 :
198 0 : SfxFloatingWindow::DataChanged( rEvt );
199 0 : }
200 :
201 238 : void SmToolBoxWindow::StateChanged( StateChangedType nStateChange )
202 : {
203 : static bool bSetPosition = true;
204 238 : if (StateChangedType::INITSHOW == nStateChange)
205 : {
206 68 : SetCategory( nActiveCategoryRID == USHRT_MAX ? RID_UNBINOPS_CAT : nActiveCategoryRID );
207 :
208 : // calculate initial position to be used after creation of the window...
209 68 : if (bSetPosition)
210 : {
211 4 : AdjustPos();
212 4 : bSetPosition = false;
213 : }
214 : }
215 : //... otherwise the base class will remember the last position of the window
216 238 : SfxFloatingWindow::StateChanged( nStateChange );
217 238 : }
218 :
219 4 : void SmToolBoxWindow::AdjustPos()
220 : {
221 4 : SmViewShell *pView = GetView();
222 : OSL_ENSURE( pView, "view shell missing" );
223 4 : Point aPos( 50, 75 );
224 4 : if (pView)
225 : {
226 4 : Size aWndSize(GetOutputSizePixel());
227 4 : SmGraphicWindow &rWin = pView->GetGraphicWindow();
228 : aPos = Point( rWin.OutputToScreenPixel(
229 4 : Point( rWin.GetSizePixel().Width() - aWndSize.Width(), 0) ) );
230 : }
231 4 : if (aPos.X() < 0)
232 4 : aPos.X() = 0;
233 4 : if (aPos.Y() < 0)
234 0 : aPos.Y() = 0;
235 4 : SetPosPixel( aPos );
236 4 : }
237 :
238 0 : bool SmToolBoxWindow::Close()
239 : {
240 0 : SmViewShell *pViewSh = GetView();
241 0 : if (pViewSh)
242 : pViewSh->GetViewFrame()->GetDispatcher()->Execute(
243 : SID_TOOLBOX, SfxCallMode::RECORD,
244 0 : new SfxBoolItem(SID_TOOLBOX, false), 0L);
245 0 : return true;
246 : }
247 :
248 0 : void SmToolBoxWindow::GetFocus()
249 : {
250 : // give focus to category toolbox
251 : // (allow for cursor travelling when a category is selected with the mouse)
252 0 : m_pToolBoxCat->GrabFocus();
253 0 : }
254 :
255 68 : void SmToolBoxWindow::SetCategory(sal_uInt16 nCategoryRID)
256 : {
257 68 : if (nCategoryRID != nActiveCategoryRID)
258 34 : ApplyImageLists( nCategoryRID );
259 :
260 : sal_uInt16 nLines;
261 : // check for valid resource id
262 68 : switch (nCategoryRID)
263 : {
264 68 : case RID_UNBINOPS_CAT : nLines = 4; break;
265 0 : case RID_RELATIONS_CAT: nLines = 7; break;
266 0 : case RID_SETOPERATIONS_CAT: nLines = 5; break;
267 0 : case RID_FUNCTIONS_CAT: nLines = 5; break;
268 0 : case RID_OPERATORS_CAT: nLines = 3; break;
269 0 : case RID_ATTRIBUTES_CAT: nLines = 5; break;
270 0 : case RID_MISC_CAT: nLines = 4; break;
271 0 : case RID_BRACKETS_CAT: nLines = 5; break;
272 0 : case RID_FORMAT_CAT: nLines = 3; break;
273 : default:
274 : // nothing to be done
275 68 : return;
276 : }
277 :
278 68 : pToolBoxCmd->Hide();
279 :
280 68 : sal_Int16 nIdx = GetToolBoxCategoriesIndex( nCategoryRID );
281 : OSL_ENSURE( nIdx >= 0, "unknown category" );
282 68 : if (nIdx >= 0)
283 68 : pToolBoxCmd = vToolBoxCategories[nIdx];
284 :
285 : // calculate actual size of window to use
286 68 : Size aCatSize( m_pToolBoxCat->CalcWindowSizePixel( 2 ) );
287 68 : Size aCmdSize( pToolBoxCmd->CalcWindowSizePixel( nLines ) );
288 : OSL_ENSURE( aCatSize.Width() == aCmdSize.Width(), "width mismatch" );
289 : // main window settings
290 68 : Size aWndSize ( aCatSize.Width(), pToolBoxCmd->GetPosPixel().Y() + aCmdSize.Height() + 3);
291 68 : SetOutputSizePixel( aWndSize );
292 :
293 68 : if (nActiveCategoryRID)
294 68 : m_pToolBoxCat->CheckItem(nActiveCategoryRID, false);
295 68 : nActiveCategoryRID = nCategoryRID;
296 68 : m_pToolBoxCat->CheckItem(nActiveCategoryRID, true);
297 :
298 68 : pToolBoxCmd->Show();
299 : }
300 :
301 0 : sal_uInt16 SmToolBoxWindow::MapToolbarIdToCategory(sal_uInt16 nId) const
302 : {
303 0 : if (nId == m_nUnbinopsId)
304 0 : return RID_UNBINOPS_CAT;
305 0 : if (nId == m_nRelationsId)
306 0 : return RID_RELATIONS_CAT;
307 0 : if (nId == m_nSetoperationsId)
308 0 : return RID_SETOPERATIONS_CAT;
309 0 : if (nId == m_nFunctionsId)
310 0 : return RID_FUNCTIONS_CAT;
311 0 : if (nId == m_nOperatorsId)
312 0 : return RID_OPERATORS_CAT;
313 0 : if (nId == m_nAttributesId)
314 0 : return RID_ATTRIBUTES_CAT;
315 0 : if (nId == m_nBracketsId)
316 0 : return RID_BRACKETS_CAT;
317 0 : if (nId == m_nFormatId)
318 0 : return RID_FORMAT_CAT;
319 0 : return RID_MISC_CAT;
320 : }
321 :
322 0 : IMPL_LINK( SmToolBoxWindow, CategoryClickHdl, ToolBox*, pToolBox)
323 : {
324 0 : sal_uInt16 nItemId = pToolBox->GetCurItemId();
325 0 : if (nItemId != 0)
326 : {
327 0 : SetCategory(MapToolbarIdToCategory(nItemId));
328 : }
329 0 : return 0;
330 : }
331 :
332 0 : IMPL_LINK( SmToolBoxWindow, CmdSelectHdl, ToolBox*, pToolBox)
333 : {
334 0 : SmViewShell *pViewSh = GetView();
335 0 : if (pViewSh)
336 : pViewSh->GetViewFrame()->GetDispatcher()->Execute(
337 : SID_INSERTCOMMAND, SfxCallMode::RECORD,
338 0 : new SfxInt16Item(SID_INSERTCOMMAND, pToolBox->GetCurItemId()), 0L);
339 0 : return 0;
340 : }
341 :
342 :
343 : /**************************************************************************/
344 :
345 125 : SFX_IMPL_FLOATINGWINDOW_WITHID(SmToolBoxWrapper, SID_TOOLBOXWINDOW);
346 :
347 34 : SmToolBoxWrapper::SmToolBoxWrapper(vcl::Window *pParentWindow,
348 : sal_uInt16 nId, SfxBindings* pBindings,
349 : SfxChildWinInfo *pInfo) :
350 34 : SfxChildWindow(pParentWindow, nId)
351 : {
352 34 : eChildAlignment = SFX_ALIGN_NOALIGNMENT;
353 :
354 34 : pWindow = new SmToolBoxWindow(pBindings, this, pParentWindow);
355 34 : ((SfxFloatingWindow *)pWindow)->Initialize(pInfo);
356 106 : }
357 :
358 :
359 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|