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/.
10 #include "condformatmgr.hxx"
11 #include "condformatmgr.hrc"
12 #include "scresid.hxx"
13 #include "globstr.hrc"
14 #include "condformatdlg.hxx"
15 #include "vcl/msgbox.hxx"
16 #include "document.hxx"
18 #define ITEMID_RANGE 1
19 #define ITEMID_CONDITION 2
22 ScCondFormatManagerWindow::ScCondFormatManagerWindow(Window
* pParent
, ScDocument
* pDoc
, ScConditionalFormatList
* pFormatList
):
23 SvTabListBox(pParent
, WB_SORT
| WB_HSCROLL
| WB_CLIPCHILDREN
| WB_TABSTOP
),
24 maHeaderBar( pParent
, WB_BUTTONSTYLE
| WB_BOTTOMBORDER
),
26 mpFormatList(pFormatList
)
28 Size
aBoxSize( pParent
->GetOutputSizePixel() );
30 maHeaderBar
.SetPosSizePixel( Point(0, 0), Size( aBoxSize
.Width(), 16 ) );
32 OUString
aConditionStr(ScGlobal::GetRscString(STR_HEADER_COND
));
33 OUString
aRangeStr(ScGlobal::GetRscString(STR_HEADER_RANGE
));
35 long nTabSize
= aBoxSize
.Width()/2;
36 maHeaderBar
.InsertItem( ITEMID_RANGE
, aRangeStr
, nTabSize
, HIB_LEFT
| HIB_VCENTER
);
37 maHeaderBar
.InsertItem( ITEMID_CONDITION
, aConditionStr
, nTabSize
, HIB_LEFT
| HIB_VCENTER
);
39 static long nTabs
[] = {2, 0, nTabSize
};
40 Size
aHeadSize( maHeaderBar
.GetSizePixel() );
42 //pParent->SetFocusControl( this );
43 SetPosSizePixel( Point( 0, aHeadSize
.Height() ), Size( aBoxSize
.Width(), aBoxSize
.Height() - aHeadSize
.Height() ) );
44 SetTabs( &nTabs
[0], MAP_PIXEL
);
46 maHeaderBar
.SetEndDragHdl( LINK(this, ScCondFormatManagerWindow
, HeaderEndDragHdl
) );
47 HeaderEndDragHdl(NULL
);
52 SetSelectionMode(MULTIPLE_SELECTION
);
55 OUString
ScCondFormatManagerWindow::createEntryString(const ScConditionalFormat
& rFormat
)
57 ScRangeList aRange
= rFormat
.GetRange();
59 aRange
.Format(aStr
, SCA_VALID
, mpDoc
, mpDoc
->GetAddressConvention());
61 aStr
+= ScCondFormatHelper::GetExpression(rFormat
, aRange
.GetTopLeftCorner());
65 void ScCondFormatManagerWindow::Init()
69 for(ScConditionalFormatList::iterator itr
= mpFormatList
->begin(); itr
!= mpFormatList
->end(); ++itr
)
71 SvTreeListEntry
* pEntry
= InsertEntryToColumn( createEntryString(*itr
), LIST_APPEND
, 0xffff );
72 maMapLBoxEntryToCondIndex
.insert(std::pair
<SvTreeListEntry
*,sal_Int32
>(pEntry
,itr
->GetKey()));
77 void ScCondFormatManagerWindow::DeleteSelection()
79 if(GetSelectionCount())
81 for(SvTreeListEntry
* pEntry
= FirstSelected(); pEntry
!= NULL
; pEntry
= NextSelected(pEntry
))
83 sal_Int32 nIndex
= maMapLBoxEntryToCondIndex
.find(pEntry
)->second
;
84 mpFormatList
->erase(nIndex
);
90 ScConditionalFormat
* ScCondFormatManagerWindow::GetSelection()
92 SvTreeListEntry
* pEntry
= FirstSelected();
96 sal_Int32 nIndex
= maMapLBoxEntryToCondIndex
.find(pEntry
)->second
;
97 return mpFormatList
->GetFormat(nIndex
);
100 void ScCondFormatManagerWindow::Update()
103 maMapLBoxEntryToCondIndex
.clear();
107 IMPL_LINK_NOARG(ScCondFormatManagerWindow
, HeaderEndDragHdl
)
109 long aTableSize
= maHeaderBar
.GetSizePixel().Width();
110 long aItemRangeSize
= maHeaderBar
.GetItemSize(ITEMID_RANGE
);
112 //calculate column size based on user input and minimum size
113 long aItemCondSize
= aTableSize
- aItemRangeSize
;
116 aSz
.Width() = aItemRangeSize
;
117 SetTab( ITEMID_RANGE
, PixelToLogic( aSz
, MapMode(MAP_APPFONT
) ).Width(), MAP_APPFONT
);
118 maHeaderBar
.SetItemSize(ITEMID_RANGE
, aItemRangeSize
);
119 aSz
.Width() += aItemCondSize
;
120 SetTab( ITEMID_CONDITION
, PixelToLogic( aSz
, MapMode(MAP_APPFONT
) ).Width(), MAP_APPFONT
);
121 maHeaderBar
.SetItemSize(ITEMID_CONDITION
, aItemCondSize
);
126 ScCondFormatManagerCtrl::ScCondFormatManagerCtrl(Window
* pParent
, ScDocument
* pDoc
, ScConditionalFormatList
* pFormatList
):
127 Control(pParent
, ScResId(CTRL_TABLE
)),
128 maWdManager(this, pDoc
, pFormatList
)
132 ScConditionalFormat
* ScCondFormatManagerCtrl::GetSelection()
134 return maWdManager
.GetSelection();
137 void ScCondFormatManagerCtrl::DeleteSelection()
139 maWdManager
.DeleteSelection();
142 void ScCondFormatManagerCtrl::Update()
144 maWdManager
.Update();
147 ScCondFormatManagerDlg::ScCondFormatManagerDlg(Window
* pParent
, ScDocument
* pDoc
, const ScConditionalFormatList
* pFormatList
, const ScAddress
& rPos
):
148 ModalDialog(pParent
, ScResId(RID_SCDLG_COND_FORMAT_MANAGER
)),
149 maBtnAdd(this, ScResId(BTN_ADD
)),
150 maBtnRemove(this, ScResId(BTN_REMOVE
)),
151 maBtnEdit(this, ScResId(BTN_EDIT
)),
152 maBtnOk(this, ScResId(BTN_OK
)),
153 maBtnCancel(this, ScResId(BTN_CANCEL
)),
154 maFlLine(this, ScResId(FL_LINE
)),
155 mpFormatList( pFormatList
? new ScConditionalFormatList(*pFormatList
) : NULL
),
156 maCtrlManager(this, pDoc
, mpFormatList
),
163 maBtnRemove
.SetClickHdl(LINK(this, ScCondFormatManagerDlg
, RemoveBtnHdl
));
164 maBtnEdit
.SetClickHdl(LINK(this, ScCondFormatManagerDlg
, EditBtnHdl
));
165 maBtnAdd
.SetClickHdl(LINK(this, ScCondFormatManagerDlg
, AddBtnHdl
));
166 maCtrlManager
.GetListControl().SetDoubleClickHdl(LINK(this, ScCondFormatManagerDlg
, EditBtnHdl
));
169 ScCondFormatManagerDlg::~ScCondFormatManagerDlg()
174 bool ScCondFormatManagerDlg::IsInRefMode() const
179 ScConditionalFormatList
* ScCondFormatManagerDlg::GetConditionalFormatList()
181 ScConditionalFormatList
* pList
= mpFormatList
;
186 bool ScCondFormatManagerDlg::CondFormatsChanged()
191 IMPL_LINK_NOARG(ScCondFormatManagerDlg
, RemoveBtnHdl
)
193 maCtrlManager
.DeleteSelection();
198 IMPL_LINK_NOARG(ScCondFormatManagerDlg
, EditBtnHdl
)
200 ScConditionalFormat
* pFormat
= maCtrlManager
.GetSelection();
206 ScModule
* pScMod
= SC_MOD();
207 pScMod
->SetRefDialog( nId
, true );
208 boost::scoped_ptr
<ScCondFormatDlg
> pDlg(new ScCondFormatDlg(this, mpDoc
, pFormat
, pFormat
->GetRange(),
209 pFormat
->GetRange().GetTopLeftCorner(), condformat::dialog::NONE
));
211 if(pDlg
->Execute() == RET_OK
)
213 sal_Int32 nKey
= pFormat
->GetKey();
214 mpFormatList
->erase(nKey
);
215 ScConditionalFormat
* pNewFormat
= pDlg
->GetConditionalFormat();
218 pNewFormat
->SetKey(nKey
);
219 mpFormatList
->InsertNew(pNewFormat
);
222 maCtrlManager
.Update();
227 pScMod
->SetRefDialog( nId
, false );
234 sal_uInt32
FindKey(ScConditionalFormatList
* pFormatList
)
237 for(ScConditionalFormatList::const_iterator itr
= pFormatList
->begin(), itrEnd
= pFormatList
->end();
238 itr
!= itrEnd
; ++itr
)
240 if(itr
->GetKey() > nKey
)
241 nKey
= itr
->GetKey();
249 IMPL_LINK_NOARG(ScCondFormatManagerDlg
, AddBtnHdl
)
252 ScModule
* pScMod
= SC_MOD();
253 pScMod
->SetRefDialog( nId
, true );
254 boost::scoped_ptr
<ScCondFormatDlg
> pDlg(new ScCondFormatDlg(this, mpDoc
, NULL
, ScRangeList(),
255 maPos
, condformat::dialog::CONDITION
));
257 if(pDlg
->Execute() == RET_OK
)
259 ScConditionalFormat
* pNewFormat
= pDlg
->GetConditionalFormat();
262 mpFormatList
->InsertNew(pNewFormat
);
263 pNewFormat
->SetKey(FindKey(mpFormatList
));
264 maCtrlManager
.Update();
270 pScMod
->SetRefDialog( nId
, false );
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */