1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: toolbox.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_starmath.hxx"
35 #include <rtl/logfile.hxx>
36 #include <svtools/eitem.hxx>
37 #include <sfx2/app.hxx>
38 #include <svtools/intitem.hxx>
39 #include <svtools/imgdef.hxx>
40 #include <sfx2/dispatch.hxx>
41 #include <sfx2/imgmgr.hxx>
42 #include <vcl/wrkwin.hxx>
43 #include "toolbox.hxx"
45 #include "starmath.hrc"
48 #include "toolbox.hrc"
53 ////////////////////////////////////////////////////////////
55 static USHORT
GetImageListRID( USHORT nCategoryRID
, BOOL bHighContrast
)
60 case RID_UNBINOPS_CAT
: nRes
= RID_IL_UNBINOPS
; break;
61 case RID_RELATIONS_CAT
: nRes
= RID_IL_RELATIONS
; break;
62 case RID_SETOPERATIONS_CAT
: nRes
= RID_IL_SETOPERATIONS
; break;
63 case RID_FUNCTIONS_CAT
: nRes
= RID_IL_FUNCTIONS
; break;
64 case RID_OPERATORS_CAT
: nRes
= RID_IL_OPERATORS
; break;
65 case RID_ATTRIBUTES_CAT
: nRes
= RID_IL_ATTRIBUTES
; break;
66 case RID_BRACKETS_CAT
: nRes
= RID_IL_BRACKETS
; break;
67 case RID_FORMAT_CAT
: nRes
= RID_IL_FORMAT
; break;
68 case RID_MISC_CAT
: nRes
= RID_IL_MISC
; break;
70 DBG_ERROR( "unkown category" );
72 if (nRes
!= 0xFFFF && bHighContrast
)
73 ++nRes
; //! the resource ID for the high contrast image list is just +1 compared to the regular ones
78 static sal_Int16
GetToolBoxCategoriesIndex( USHORT nCategoryRID
)
83 case RID_UNBINOPS_CAT
: nIdx
= 0; break;
84 case RID_RELATIONS_CAT
: nIdx
= 1; break;
85 case RID_SETOPERATIONS_CAT
: nIdx
= 2; break;
86 case RID_FUNCTIONS_CAT
: nIdx
= 3; break;
87 case RID_OPERATORS_CAT
: nIdx
= 4; break;
88 case RID_ATTRIBUTES_CAT
: nIdx
= 5; break;
89 case RID_BRACKETS_CAT
: nIdx
= 6; break;
90 case RID_FORMAT_CAT
: nIdx
= 7; break;
91 case RID_MISC_CAT
: nIdx
= 8; break;
99 static USHORT
GetCategoryRID( USHORT nResId
)
101 USHORT nRes
= 0xFFFF;
104 case RID_IL_UNBINOPS
:
105 case RID_ILH_UNBINOPS
: nRes
= RID_UNBINOPS_CAT
; break;
106 case RID_IL_RELATIONS
:
107 case RID_ILH_RELATIONS
: nRes
= RID_RELATIONS_CAT
; break;
108 case RID_IL_SETOPERATIONS
:
109 case RID_ILH_SETOPERATIONS
: nRes
= RID_SETOPERATIONS_CAT
; break;
110 case RID_IL_FUNCTIONS
:
111 case RID_ILH_FUNCTIONS
: nRes
= RID_FUNCTIONS_CAT
; break;
112 case RID_IL_OPERATORS
:
113 case RID_ILH_OPERATORS
: nRes
= RID_OPERATORS_CAT
; break;
114 case RID_IL_ATTRIBUTES
:
115 case RID_ILH_ATTRIBUTES
: nRes
= RID_ATTRIBUTES_CAT
; break;
116 case RID_IL_BRACKETS
:
117 case RID_ILH_BRACKETS
: nRes
= RID_BRACKETS_CAT
; break;
119 case RID_ILH_FORMAT
: nRes
= RID_FORMAT_CAT
; break;
121 case RID_ILH_MISC
: nRes
= RID_MISC_CAT
; break;
123 if (nResId
!= RID_IL_CATALOG
&& nResId
!= RID_ILH_CATALOG
)
125 #if OSL_DEBUG_LEVEL > 1
126 DBG_ERROR( "unkown category" );
134 ////////////////////////////////////////////////////////////
137 SmToolBoxWindow::SmToolBoxWindow(SfxBindings
*pTmpBindings
,
138 SfxChildWindow
*pChildWindow
,
140 SfxFloatingWindow(pTmpBindings
, pChildWindow
, pParent
, SmResId(RID_TOOLBOXWINDOW
)),
141 aToolBoxCat(this, SmResId(NUM_TBX_CATEGORIES
+ 1)),
142 aToolBoxCat_Delim(this, SmResId( FL_TOOLBOX_CAT_DELIM
))
144 RTL_LOGFILE_CONTEXT( aLog
, "starmath: SmToolBoxWindow::SmToolBoxWindow" );
146 // allow for cursor travelling between toolbox and sub-categories
147 SetStyle( GetStyle() | WB_DIALOGCONTROL
);
149 nActiveCategoryRID
= sal::static_int_cast
< USHORT
>(-1);
151 aToolBoxCat
.SetClickHdl(LINK(this, SmToolBoxWindow
, CategoryClickHdl
));
154 for (i
= 0; i
< NUM_TBX_CATEGORIES
; i
++)
156 ToolBox
*pBox
= new ToolBox(this, SmResId (i
+1));
157 vToolBoxCategories
[i
] = pBox
;
158 pBox
->SetSelectHdl(LINK(this, SmToolBoxWindow
, CmdSelectHdl
));
160 pToolBoxCmd
= vToolBoxCategories
[0];
162 for (i
= 0; i
<= NUM_TBX_CATEGORIES
; ++i
)
170 ApplyImageLists( RID_UNBINOPS_CAT
);
171 SetCategory( RID_UNBINOPS_CAT
);
174 SmToolBoxWindow::~SmToolBoxWindow()
177 for (i
= 0; i
< NUM_TBX_CATEGORIES
; i
++)
179 ToolBox
*pBox
= vToolBoxCategories
[i
];
182 for (i
= 0; i
< NUM_TBX_CATEGORIES
+ 1; ++i
)
184 delete aImageLists
[i
];
185 delete aImageListsH
[i
];
190 SmViewShell
* SmToolBoxWindow::GetView()
192 SfxViewShell
*pView
= GetBindings().GetDispatcher()->GetFrame()->GetViewShell();
193 return PTR_CAST(SmViewShell
, pView
);
197 const ImageList
* SmToolBoxWindow::GetImageList( USHORT nResId
, BOOL bHighContrast
)
199 // creates the image list via its resource id and stores that
200 // list for later use in the respective array.
202 const ImageList
*pIL
= 0;
205 USHORT nCategoryRID
= GetCategoryRID( nResId
);
206 sal_Int16 nIndex
= GetToolBoxCategoriesIndex( nCategoryRID
);
207 if (nIndex
== -1 && (nResId
== RID_IL_CATALOG
|| nResId
== RID_ILH_CATALOG
))
208 nIndex
= NUM_TBX_CATEGORIES
;
212 ImageList
**pImgList
= bHighContrast
? aImageListsH
: aImageLists
;
213 if (!pImgList
[ nIndex
])
214 pImgList
[ nIndex
] = new ImageList( SmResId(nResId
) );
215 pIL
= pImgList
[ nIndex
];
218 DBG_ASSERT( pIL
, "image list not found!" );
223 void SmToolBoxWindow::ApplyImageLists( USHORT nCategoryRID
)
225 BOOL bHighContrast
= GetDisplayBackground().GetColor().IsDark() != 0;
227 // set image list for toolbox 'catalog'
228 const ImageList
*pImageList
= GetImageList( bHighContrast
? RID_ILH_CATALOG
: RID_IL_CATALOG
, bHighContrast
);
229 DBG_ASSERT( pImageList
, "image list missing" );
231 aToolBoxCat
.SetImageList( *pImageList
);
233 // set image list for active (visible) category of 'catalog'
234 sal_Int16 nIdx
= GetToolBoxCategoriesIndex( nCategoryRID
);
235 USHORT nResId
= GetImageListRID( nCategoryRID
, bHighContrast
);
236 pImageList
= GetImageList( nResId
, bHighContrast
);
237 DBG_ASSERT( pImageList
&& nIdx
>= 0, "image list or index missing" );
238 if (pImageList
&& nIdx
>= 0)
239 vToolBoxCategories
[ nIdx
]->SetImageList( *pImageList
);
242 void SmToolBoxWindow::DataChanged( const DataChangedEvent
&rEvt
)
244 if ( (rEvt
.GetType() == DATACHANGED_SETTINGS
) && (rEvt
.GetFlags() & SETTINGS_STYLE
) )
245 ApplyImageLists( nActiveCategoryRID
);
247 SfxFloatingWindow::DataChanged( rEvt
);
250 void SmToolBoxWindow::StateChanged( StateChangedType nStateChange
)
252 static BOOL bSetPosition
= TRUE
;
253 if (STATE_CHANGE_INITSHOW
== nStateChange
)
255 // calculate initial position to be used after creation of the window...
256 AdjustPosSize( bSetPosition
);
257 bSetPosition
= FALSE
;
259 SetCategory(RID_UNBINOPS_CAT
);
261 //... otherwise the base class will remember the last position of the window
262 SfxFloatingWindow::StateChanged( nStateChange
);
266 void SmToolBoxWindow::AdjustPosSize( BOOL bSetPos
)
268 Size
aCatSize( aToolBoxCat
.CalcWindowSizePixel( 2 ) );
269 Size
aCmdSize( pToolBoxCmd
->CalcWindowSizePixel( 5 ) );
270 DBG_ASSERT( aCatSize
.Width() == aCmdSize
.Width(), "width mismatch" );
273 aToolBoxCat
.SetPosPixel( Point(0, 3) );
274 aToolBoxCat
.SetSizePixel( aCatSize
);
275 // settings for catalog / category delimiter
276 Point
aP( aToolBoxCat_Delim
.GetPosPixel() );
278 aToolBoxCat_Delim
.SetPosPixel( aP
);
279 Size
aS( aCatSize
.Width() - 10, 10 );
280 aToolBoxCat_Delim
.SetSizePixel( aS
);
283 aP
.Y() += aToolBoxCat_Delim
.GetSizePixel().Height();
284 for (int i
= 0; i
< NUM_TBX_CATEGORIES
; i
++)
286 vToolBoxCategories
[i
]->SetPosPixel( aP
);
287 vToolBoxCategories
[i
]->SetSizePixel( aCmdSize
);
289 // main window settings
290 Size
aWndSize ( aCatSize
.Width(), pToolBoxCmd
->GetPosPixel().Y() + pToolBoxCmd
->GetSizePixel().Height() + 3);
291 SetOutputSizePixel( aWndSize
);
295 SmViewShell
*pView
= GetView();
296 DBG_ASSERT( pView
, "view shell missing" );
297 Point
aPos( 50, 75 );
300 SmGraphicWindow
&rWin
= pView
->GetGraphicWindow();
301 aPos
= Point( rWin
.OutputToScreenPixel(
302 Point( rWin
.GetSizePixel().Width() - aWndSize
.Width(), 0) ) );
313 BOOL
SmToolBoxWindow::Close()
315 SmViewShell
*pViewSh
= GetView();
317 pViewSh
->GetViewFrame()->GetDispatcher()->Execute(
318 SID_TOOLBOX
, SFX_CALLMODE_STANDARD
,
319 new SfxBoolItem(SID_TOOLBOX
, FALSE
), 0L);
323 void SmToolBoxWindow::GetFocus()
325 // give focus to category toolbox
326 // (allow for cursor travelling when a category is selected with the mouse)
327 aToolBoxCat
.GrabFocus();
330 void SmToolBoxWindow::SetCategory(USHORT nCategoryRID
)
332 if (nCategoryRID
!= nActiveCategoryRID
)
334 ApplyImageLists( nCategoryRID
);
337 // check for valid resource id
338 switch (nCategoryRID
)
340 case RID_UNBINOPS_CAT
: nLines
= 4; break;
341 case RID_RELATIONS_CAT
: nLines
= 5; break;
342 case RID_SETOPERATIONS_CAT
: nLines
= 5; break;
343 case RID_FUNCTIONS_CAT
: nLines
= 5; break;
344 case RID_OPERATORS_CAT
: nLines
= 3; break;
345 case RID_ATTRIBUTES_CAT
: nLines
= 5; break;
346 case RID_MISC_CAT
: nLines
= 4; break;
347 case RID_BRACKETS_CAT
: nLines
= 5; break;
348 case RID_FORMAT_CAT
: nLines
= 3; break;
350 // nothing to be done
356 sal_Int16 nIdx
= GetToolBoxCategoriesIndex( nCategoryRID
);
357 DBG_ASSERT( nIdx
>= 0, "unkown category" );
359 pToolBoxCmd
= vToolBoxCategories
[nIdx
];
361 // calculate actual size of window to use
362 Size
aCatSize( aToolBoxCat
.CalcWindowSizePixel( 2 ) );
363 Size
aCmdSize( pToolBoxCmd
->CalcWindowSizePixel( nLines
) );
364 DBG_ASSERT( aCatSize
.Width() == aCmdSize
.Width(), "width mismatch" );
365 // main window settings
366 Size
aWndSize ( aCatSize
.Width(), pToolBoxCmd
->GetPosPixel().Y() + aCmdSize
.Height() + 3);
367 SetOutputSizePixel( aWndSize
);
369 if (nActiveCategoryRID
)
370 aToolBoxCat
.CheckItem(nActiveCategoryRID
, FALSE
);
371 nActiveCategoryRID
= nCategoryRID
;
372 aToolBoxCat
.CheckItem(nActiveCategoryRID
, TRUE
);
379 IMPL_LINK_INLINE_START( SmToolBoxWindow
, CategoryClickHdl
, ToolBox
*, pToolBox
)
381 int nItemId
= pToolBox
->GetCurItemId();
383 SetCategory( sal::static_int_cast
< USHORT
>(nItemId
) );
386 IMPL_LINK_INLINE_END( SmToolBoxWindow
, CategoryClickHdl
, ToolBox
*, pToolBox
)
389 IMPL_LINK_INLINE_START( SmToolBoxWindow
, CmdSelectHdl
, ToolBox
*, pToolBox
)
391 SmViewShell
*pViewSh
= GetView();
393 pViewSh
->GetViewFrame()->GetDispatcher()->Execute(
394 SID_INSERTCOMMAND
, SFX_CALLMODE_STANDARD
,
395 new SfxInt16Item(SID_INSERTCOMMAND
, pToolBox
->GetCurItemId()), 0L);
398 IMPL_LINK_INLINE_END( SmToolBoxWindow
, CmdSelectHdl
, ToolBox
*, pToolBox
)
401 /**************************************************************************/
403 SFX_IMPL_FLOATINGWINDOW(SmToolBoxWrapper
, SID_TOOLBOXWINDOW
);
405 SmToolBoxWrapper::SmToolBoxWrapper(Window
*pParentWindow
,
406 USHORT nId
, SfxBindings
* pBindings
,
407 SfxChildWinInfo
*pInfo
) :
408 SfxChildWindow(pParentWindow
, nId
)
410 eChildAlignment
= SFX_ALIGN_NOALIGNMENT
;
412 pWindow
= new SmToolBoxWindow(pBindings
, this, pParentWindow
);
413 ((SfxFloatingWindow
*)pWindow
)->Initialize(pInfo
);