1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/logfile.hxx>
21 #include <svl/eitem.hxx>
22 #include <sfx2/app.hxx>
23 #include <svl/intitem.hxx>
24 #include <svtools/imgdef.hxx>
25 #include <sfx2/dispatch.hxx>
26 #include <sfx2/imgmgr.hxx>
27 #include <vcl/wrkwin.hxx>
28 #include "toolbox.hxx"
29 #include "starmath.hrc"
30 #include "toolbox.hrc"
34 ////////////////////////////////////////////////////////////
36 static sal_uInt16
GetImageListRID( sal_uInt16 nCategoryRID
)
38 sal_uInt16 nRes
= 0xFFFF;
41 case RID_UNBINOPS_CAT
: nRes
= RID_IL_UNBINOPS
; break;
42 case RID_RELATIONS_CAT
: nRes
= RID_IL_RELATIONS
; break;
43 case RID_SETOPERATIONS_CAT
: nRes
= RID_IL_SETOPERATIONS
; break;
44 case RID_FUNCTIONS_CAT
: nRes
= RID_IL_FUNCTIONS
; break;
45 case RID_OPERATORS_CAT
: nRes
= RID_IL_OPERATORS
; break;
46 case RID_ATTRIBUTES_CAT
: nRes
= RID_IL_ATTRIBUTES
; break;
47 case RID_BRACKETS_CAT
: nRes
= RID_IL_BRACKETS
; break;
48 case RID_FORMAT_CAT
: nRes
= RID_IL_FORMAT
; break;
49 case RID_MISC_CAT
: nRes
= RID_IL_MISC
; break;
51 OSL_FAIL( "unknown category" );
57 static sal_Int16
GetToolBoxCategoriesIndex( sal_uInt16 nCategoryRID
)
62 case RID_UNBINOPS_CAT
: nIdx
= 0; break;
63 case RID_RELATIONS_CAT
: nIdx
= 1; break;
64 case RID_SETOPERATIONS_CAT
: nIdx
= 2; break;
65 case RID_FUNCTIONS_CAT
: nIdx
= 3; break;
66 case RID_OPERATORS_CAT
: nIdx
= 4; break;
67 case RID_ATTRIBUTES_CAT
: nIdx
= 5; break;
68 case RID_BRACKETS_CAT
: nIdx
= 6; break;
69 case RID_FORMAT_CAT
: nIdx
= 7; break;
70 case RID_MISC_CAT
: nIdx
= 8; break;
78 static sal_uInt16
GetCategoryRID( sal_uInt16 nResId
)
80 sal_uInt16 nRes
= 0xFFFF;
83 case RID_IL_UNBINOPS
: nRes
= RID_UNBINOPS_CAT
; break;
84 case RID_IL_RELATIONS
: nRes
= RID_RELATIONS_CAT
; break;
85 case RID_IL_SETOPERATIONS
: nRes
= RID_SETOPERATIONS_CAT
; break;
86 case RID_IL_FUNCTIONS
: nRes
= RID_FUNCTIONS_CAT
; break;
87 case RID_IL_OPERATORS
: nRes
= RID_OPERATORS_CAT
; break;
88 case RID_IL_ATTRIBUTES
: nRes
= RID_ATTRIBUTES_CAT
; break;
89 case RID_IL_BRACKETS
: nRes
= RID_BRACKETS_CAT
; break;
90 case RID_IL_FORMAT
: nRes
= RID_FORMAT_CAT
; break;
91 case RID_IL_MISC
: nRes
= RID_MISC_CAT
; break;
93 if (nResId
!= RID_IL_CATALOG
)
95 #if OSL_DEBUG_LEVEL > 1
96 OSL_FAIL( "unknown category" );
104 ////////////////////////////////////////////////////////////
107 SmToolBoxWindow::SmToolBoxWindow(SfxBindings
*pTmpBindings
,
108 SfxChildWindow
*pChildWindow
,
110 SfxFloatingWindow(pTmpBindings
, pChildWindow
, pParent
, SmResId(RID_TOOLBOXWINDOW
)),
111 aToolBoxCat(this, SmResId(TOOLBOX_CATALOG
)),
112 aToolBoxCat_Delim(this, SmResId( FL_TOOLBOX_CAT_DELIM
))
114 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmToolBoxWindow::SmToolBoxWindow" );
116 // allow for cursor travelling between toolbox and sub-categories
117 SetStyle( GetStyle() | WB_DIALOGCONTROL
);
119 nActiveCategoryRID
= USHRT_MAX
;
121 aToolBoxCat
.SetClickHdl(LINK(this, SmToolBoxWindow
, CategoryClickHdl
));
124 for (i
= 0; i
< NUM_TBX_CATEGORIES
; ++i
)
126 ToolBox
*pBox
= new ToolBox(this, SmResId( TOOLBOX_CAT_A
+ i
));
127 vToolBoxCategories
[i
] = pBox
;
128 pBox
->SetSelectHdl(LINK(this, SmToolBoxWindow
, CmdSelectHdl
));
130 pToolBoxCmd
= vToolBoxCategories
[0];
132 for (i
= 0; i
<= NUM_TBX_CATEGORIES
; ++i
)
138 SmToolBoxWindow::~SmToolBoxWindow()
141 for (i
= 0; i
< NUM_TBX_CATEGORIES
; ++i
)
143 ToolBox
*pBox
= vToolBoxCategories
[i
];
146 for (i
= 0; i
< NUM_TBX_CATEGORIES
+ 1; ++i
)
147 delete aImageLists
[i
];
151 SmViewShell
* SmToolBoxWindow::GetView()
153 SfxViewShell
*pView
= GetBindings().GetDispatcher()->GetFrame()->GetViewShell();
154 return PTR_CAST(SmViewShell
, pView
);
158 const ImageList
* SmToolBoxWindow::GetImageList( sal_uInt16 nResId
)
160 // creates the image list via its resource id and stores that
161 // list for later use in the respective array.
163 const ImageList
*pIL
= 0;
166 sal_uInt16 nCategoryRID
= GetCategoryRID( nResId
);
167 sal_Int16 nIndex
= GetToolBoxCategoriesIndex( nCategoryRID
);
168 if (nIndex
== -1 && (nResId
== RID_IL_CATALOG
))
169 nIndex
= NUM_TBX_CATEGORIES
;
173 ImageList
**pImgList
= aImageLists
;
174 if (!pImgList
[ nIndex
])
175 pImgList
[ nIndex
] = new ImageList( SmResId(nResId
) );
176 pIL
= pImgList
[ nIndex
];
179 OSL_ENSURE( pIL
, "image list not found!" );
184 void SmToolBoxWindow::ApplyImageLists( sal_uInt16 nCategoryRID
)
186 // set image list for toolbox 'catalog'
187 const ImageList
*pImageList
= GetImageList( RID_IL_CATALOG
);
188 OSL_ENSURE( pImageList
, "image list missing" );
190 aToolBoxCat
.SetImageList( *pImageList
);
192 // set image list for active (visible) category of 'catalog'
193 sal_Int16 nIdx
= GetToolBoxCategoriesIndex( nCategoryRID
);
194 sal_uInt16 nResId
= GetImageListRID( nCategoryRID
);
195 pImageList
= GetImageList( nResId
);
196 OSL_ENSURE( pImageList
&& nIdx
>= 0, "image list or index missing" );
197 if (pImageList
&& nIdx
>= 0)
198 vToolBoxCategories
[ nIdx
]->SetImageList( *pImageList
);
201 void SmToolBoxWindow::DataChanged( const DataChangedEvent
&rEvt
)
203 if ( (rEvt
.GetType() == DATACHANGED_SETTINGS
) && (rEvt
.GetFlags() & SETTINGS_STYLE
) )
204 ApplyImageLists( nActiveCategoryRID
);
206 SfxFloatingWindow::DataChanged( rEvt
);
209 void SmToolBoxWindow::StateChanged( StateChangedType nStateChange
)
211 static bool bSetPosition
= true;
212 if (STATE_CHANGE_INITSHOW
== nStateChange
)
214 SetCategory( nActiveCategoryRID
== USHRT_MAX
? RID_UNBINOPS_CAT
: nActiveCategoryRID
);
216 // calculate initial position to be used after creation of the window...
217 AdjustPosSize( bSetPosition
);
218 bSetPosition
= false;
220 //... otherwise the base class will remember the last position of the window
221 SfxFloatingWindow::StateChanged( nStateChange
);
225 void SmToolBoxWindow::AdjustPosSize( bool bSetPos
)
227 Size
aCatSize( aToolBoxCat
.CalcWindowSizePixel( 2 ) );
228 Size
aCmdSize( pToolBoxCmd
->CalcWindowSizePixel( 7 /* see nLines in SetCategory*/ ) );
229 OSL_ENSURE( aCatSize
.Width() == aCmdSize
.Width(), "width mismatch" );
232 aToolBoxCat
.SetPosPixel( Point(0, 3) );
233 aToolBoxCat
.SetSizePixel( aCatSize
);
234 // settings for catalog / category delimiter
235 Point
aP( aToolBoxCat_Delim
.GetPosPixel() );
237 aToolBoxCat_Delim
.SetPosPixel( aP
);
238 aToolBoxCat_Delim
.SetSizePixel( Size( aCatSize
.Width(), aToolBoxCat_Delim
.GetSizePixel().Height() ) );
240 aP
.Y() += aToolBoxCat_Delim
.GetSizePixel().Height();
241 for (int i
= 0; i
< NUM_TBX_CATEGORIES
; ++i
)
243 vToolBoxCategories
[i
]->SetPosPixel( aP
);
244 vToolBoxCategories
[i
]->SetSizePixel( aCmdSize
);
246 // main window settings
247 Size
aWndSize ( aCatSize
.Width(), pToolBoxCmd
->GetPosPixel().Y() + pToolBoxCmd
->GetSizePixel().Height() + 3);
248 SetOutputSizePixel( aWndSize
);
252 SmViewShell
*pView
= GetView();
253 OSL_ENSURE( pView
, "view shell missing" );
254 Point
aPos( 50, 75 );
257 SmGraphicWindow
&rWin
= pView
->GetGraphicWindow();
258 aPos
= Point( rWin
.OutputToScreenPixel(
259 Point( rWin
.GetSizePixel().Width() - aWndSize
.Width(), 0) ) );
270 sal_Bool
SmToolBoxWindow::Close()
272 SmViewShell
*pViewSh
= GetView();
274 pViewSh
->GetViewFrame()->GetDispatcher()->Execute(
275 SID_TOOLBOX
, SFX_CALLMODE_STANDARD
,
276 new SfxBoolItem(SID_TOOLBOX
, false), 0L);
280 void SmToolBoxWindow::GetFocus()
282 // give focus to category toolbox
283 // (allow for cursor travelling when a category is selected with the mouse)
284 aToolBoxCat
.GrabFocus();
287 void SmToolBoxWindow::SetCategory(sal_uInt16 nCategoryRID
)
289 if (nCategoryRID
!= nActiveCategoryRID
)
290 ApplyImageLists( nCategoryRID
);
293 // check for valid resource id
294 switch (nCategoryRID
)
296 case RID_UNBINOPS_CAT
: nLines
= 4; break;
297 case RID_RELATIONS_CAT
: nLines
= 7; break;
298 case RID_SETOPERATIONS_CAT
: nLines
= 5; break;
299 case RID_FUNCTIONS_CAT
: nLines
= 5; break;
300 case RID_OPERATORS_CAT
: nLines
= 3; break;
301 case RID_ATTRIBUTES_CAT
: nLines
= 5; break;
302 case RID_MISC_CAT
: nLines
= 4; break;
303 case RID_BRACKETS_CAT
: nLines
= 5; break;
304 case RID_FORMAT_CAT
: nLines
= 3; break;
306 // nothing to be done
312 sal_Int16 nIdx
= GetToolBoxCategoriesIndex( nCategoryRID
);
313 OSL_ENSURE( nIdx
>= 0, "unknown category" );
315 pToolBoxCmd
= vToolBoxCategories
[nIdx
];
317 // calculate actual size of window to use
318 Size
aCatSize( aToolBoxCat
.CalcWindowSizePixel( 2 ) );
319 Size
aCmdSize( pToolBoxCmd
->CalcWindowSizePixel( nLines
) );
320 OSL_ENSURE( aCatSize
.Width() == aCmdSize
.Width(), "width mismatch" );
321 // main window settings
322 Size
aWndSize ( aCatSize
.Width(), pToolBoxCmd
->GetPosPixel().Y() + aCmdSize
.Height() + 3);
323 SetOutputSizePixel( aWndSize
);
325 if (nActiveCategoryRID
)
326 aToolBoxCat
.CheckItem(nActiveCategoryRID
, false);
327 nActiveCategoryRID
= nCategoryRID
;
328 aToolBoxCat
.CheckItem(nActiveCategoryRID
, true);
334 IMPL_LINK( SmToolBoxWindow
, CategoryClickHdl
, ToolBox
*, pToolBox
)
336 int nItemId
= pToolBox
->GetCurItemId();
338 SetCategory( sal::static_int_cast
< sal_uInt16
>(nItemId
) );
343 IMPL_LINK( SmToolBoxWindow
, CmdSelectHdl
, ToolBox
*, pToolBox
)
345 SmViewShell
*pViewSh
= GetView();
347 pViewSh
->GetViewFrame()->GetDispatcher()->Execute(
348 SID_INSERTCOMMAND
, SFX_CALLMODE_STANDARD
,
349 new SfxInt16Item(SID_INSERTCOMMAND
, pToolBox
->GetCurItemId()), 0L);
354 /**************************************************************************/
356 SFX_IMPL_FLOATINGWINDOW_WITHID(SmToolBoxWrapper
, SID_TOOLBOXWINDOW
);
358 SmToolBoxWrapper::SmToolBoxWrapper(Window
*pParentWindow
,
359 sal_uInt16 nId
, SfxBindings
* pBindings
,
360 SfxChildWinInfo
*pInfo
) :
361 SfxChildWindow(pParentWindow
, nId
)
363 eChildAlignment
= SFX_ALIGN_NOALIGNMENT
;
365 pWindow
= new SmToolBoxWindow(pBindings
, this, pParentWindow
);
366 ((SfxFloatingWindow
*)pWindow
)->Initialize(pInfo
);
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */