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 <instable.hxx>
21 #include <shellres.hxx>
23 #include <swmodule.hxx>
24 #include <sfx2/htmlmode.hxx>
25 #include <viewopt.hxx>
26 #include <comphelper/lok.hxx>
29 void SwInsTableDlg::GetValues( OUString
& rName
, sal_uInt16
& rRow
, sal_uInt16
& rCol
,
30 SwInsertTableOptions
& rInsTableOpts
, OUString
& rAutoName
,
31 std::unique_ptr
<SwTableAutoFormat
>& prTAFormat
)
33 SwInsertTableFlags nInsMode
= SwInsertTableFlags::NONE
;
34 rName
= m_xNameEdit
->get_text();
35 rRow
= m_xRowSpinButton
->get_value();
36 rCol
= m_xColSpinButton
->get_value();
38 if (m_xHeaderCB
->get_active())
39 nInsMode
|= SwInsertTableFlags::Headline
;
40 if (m_xRepeatHeaderCB
->get_sensitive() && m_xRepeatHeaderCB
->get_active())
41 rInsTableOpts
.mnRowsToRepeat
= m_xRepeatHeaderNF
->get_value();
43 rInsTableOpts
.mnRowsToRepeat
= 0;
44 if (!m_xDontSplitCB
->get_active())
45 nInsMode
|= SwInsertTableFlags::SplitLayout
;
48 prTAFormat
.reset(new SwTableAutoFormat( *pTAutoFormat
));
49 rAutoName
= prTAFormat
->GetName();
52 rInsTableOpts
.mnInsMode
= nInsMode
;
55 IMPL_LINK(SwInsTableDlg
, TextFilterHdl
, OUString
&, rTest
, bool)
57 rTest
= m_aTextFilter
.filter(rTest
);
61 SwInsTableDlg::SwInsTableDlg(SwView
& rView
)
62 : SfxDialogController(rView
.GetFrameWeld(), "modules/swriter/ui/inserttable.ui", "InsertTableDialog")
63 , m_aTextFilter(" .<>")
64 , pShell(&rView
.GetWrtShell())
65 , pTAutoFormat(nullptr)
66 , nEnteredValRepeatHeaderNF(-1)
67 , m_xNameEdit(m_xBuilder
->weld_entry("nameedit"))
68 , m_xWarning(m_xBuilder
->weld_label("lbwarning"))
69 , m_xColSpinButton(m_xBuilder
->weld_spin_button("colspin"))
70 , m_xRowSpinButton(m_xBuilder
->weld_spin_button("rowspin"))
71 , m_xHeaderCB(m_xBuilder
->weld_check_button("headercb"))
72 , m_xRepeatHeaderCB(m_xBuilder
->weld_check_button("repeatcb"))
73 , m_xRepeatHeaderNF(m_xBuilder
->weld_spin_button("repeatheaderspin"))
74 , m_xRepeatGroup(m_xBuilder
->weld_widget("repeatgroup"))
75 , m_xDontSplitCB(m_xBuilder
->weld_check_button("dontsplitcb"))
76 , m_xInsertBtn(m_xBuilder
->weld_button("ok"))
77 , m_xLbFormat(m_xBuilder
->weld_tree_view("formatlbinstable"))
78 , m_xWndPreview(new weld::CustomWeld(*m_xBuilder
, "previewinstable", m_aWndPreview
))
79 , m_xStyleFrame(m_xBuilder
->weld_frame("stylesframe"))
81 if (comphelper::LibreOfficeKit::isActive())
82 m_xStyleFrame
->hide();
84 const int nWidth
= m_xLbFormat
->get_approximate_digit_width() * 32;
85 const int nHeight
= m_xLbFormat
->get_height_rows(8);
86 m_xLbFormat
->set_size_request(nWidth
, nHeight
);
87 m_xWndPreview
->set_size_request(nWidth
, nHeight
);
89 m_xNameEdit
->connect_insert_text(LINK(this, SwInsTableDlg
, TextFilterHdl
));
90 m_xNameEdit
->set_text(pShell
->GetUniqueTableName());
91 m_xNameEdit
->connect_changed(LINK(this, SwInsTableDlg
, ModifyName
));
92 m_xRowSpinButton
->connect_changed(LINK(this, SwInsTableDlg
, ModifyRowCol
));
93 m_xColSpinButton
->connect_changed(LINK(this, SwInsTableDlg
, ModifyRowCol
));
95 m_xInsertBtn
->connect_clicked(LINK(this, SwInsTableDlg
, OKHdl
));
97 bool bHTMLMode
= 0 != (::GetHtmlMode(rView
.GetDocShell())&HTMLMODE_ON
);
98 const SwModuleOptions
* pModOpt
= SW_MOD()->GetModuleConfig();
100 SwInsertTableOptions aInsOpts
= pModOpt
->GetInsTableFlags(bHTMLMode
);
101 SwInsertTableFlags nInsTableFlags
= aInsOpts
.mnInsMode
;
103 m_xHeaderCB
->set_active(bool(nInsTableFlags
& SwInsertTableFlags::Headline
));
104 m_xRepeatHeaderCB
->set_active(aInsOpts
.mnRowsToRepeat
> 0);
106 m_xDontSplitCB
->hide();
108 m_xDontSplitCB
->set_active(!(nInsTableFlags
& SwInsertTableFlags::SplitLayout
));
110 m_xRepeatHeaderNF
->connect_value_changed( LINK( this, SwInsTableDlg
, ModifyRepeatHeaderNF_Hdl
) );
111 m_xHeaderCB
->connect_toggled( LINK( this, SwInsTableDlg
, CheckBoxHdl
) );
112 m_xRepeatHeaderCB
->connect_toggled( LINK( this, SwInsTableDlg
, RepeatHeaderCheckBoxHdl
) );
113 RepeatHeaderCheckBoxHdl(*m_xRepeatHeaderCB
);
114 CheckBoxHdl(*m_xHeaderCB
);
116 sal_Int64 nMax
= m_xRowSpinButton
->get_value();
121 m_xRepeatHeaderNF
->set_max( nMax
);
123 InitAutoTableFormat();
124 m_xWarning
->set_label_type(weld::LabelType::Warning
);
127 void SwInsTableDlg::InitAutoTableFormat()
129 m_aWndPreview
.DetectRTL(pShell
);
131 m_xLbFormat
->connect_changed(LINK(this, SwInsTableDlg
, SelFormatHdl
));
133 pTableTable
= new SwTableAutoFormatTable
;
136 // Add "- none -" style autoformat table.
137 m_xLbFormat
->append_text(SwViewShell::GetShellRes()->aStrNone
); // Insert to listbox
139 // Add other styles of autoformat tables.
140 for (sal_uInt8 i
= 0, nCount
= static_cast<sal_uInt8
>(pTableTable
->size());
143 SwTableAutoFormat
const& rFormat
= (*pTableTable
)[ i
];
144 m_xLbFormat
->append_text(rFormat
.GetName());
145 if (pTAutoFormat
&& rFormat
.GetName() == pTAutoFormat
->GetName())
149 // Change this min variable if you add autotable manually.
150 minTableIndexInLb
= 1;
151 maxTableIndexInLb
= minTableIndexInLb
+ static_cast<sal_uInt8
>(pTableTable
->size());
153 m_xLbFormat
->select( lbIndex
);
154 tbIndex
= lbIndexToTableIndex(lbIndex
);
156 SelFormatHdl( *m_xLbFormat
);
159 sal_uInt8
SwInsTableDlg::lbIndexToTableIndex( const sal_uInt8 listboxIndex
)
161 if( minTableIndexInLb
!= maxTableIndexInLb
&&
162 minTableIndexInLb
<= listboxIndex
&&
163 listboxIndex
< maxTableIndexInLb
)
165 return listboxIndex
- minTableIndexInLb
;
171 static void lcl_SetProperties( SwTableAutoFormat
* pTableAutoFormat
, bool bVal
)
173 pTableAutoFormat
->SetFont( bVal
);
174 pTableAutoFormat
->SetJustify( bVal
);
175 pTableAutoFormat
->SetFrame( bVal
);
176 pTableAutoFormat
->SetBackground( bVal
);
177 pTableAutoFormat
->SetValueFormat( bVal
);
178 pTableAutoFormat
->SetWidthHeight( bVal
);
181 IMPL_LINK_NOARG(SwInsTableDlg
, SelFormatHdl
, weld::TreeView
&, void)
183 // Get index of selected item from the listbox
184 lbIndex
= static_cast<sal_uInt8
>(m_xLbFormat
->get_selected_index());
185 tbIndex
= lbIndexToTableIndex( lbIndex
);
187 // To understand this index mapping, look InitAutoTableFormat function to
188 // see how listbox item is implemented.
190 m_aWndPreview
.NotifyChange( (*pTableTable
)[tbIndex
] );
193 SwTableAutoFormat
aTmp( SwViewShell::GetShellRes()->aStrNone
);
194 lcl_SetProperties( &aTmp
, false );
196 m_aWndPreview
.NotifyChange( aTmp
);
200 IMPL_LINK_NOARG(SwInsTableDlg
, OKHdl
, weld::Button
&, void)
203 pShell
->SetTableStyle((*pTableTable
)[tbIndex
]);
208 *pTAutoFormat
= (*pTableTable
)[ tbIndex
];
210 pTAutoFormat
= new SwTableAutoFormat( (*pTableTable
)[ tbIndex
] );
215 pTAutoFormat
= new SwTableAutoFormat( SwViewShell::GetShellRes()->aStrNone
);
216 lcl_SetProperties( pTAutoFormat
, false );
219 m_xDialog
->response(RET_OK
);
222 IMPL_LINK( SwInsTableDlg
, ModifyName
, weld::Entry
&, rEdit
, void )
224 OUString sTableName
= rEdit
.get_text();
225 m_xInsertBtn
->set_sensitive(pShell
->GetTableStyle(sTableName
) == nullptr);
228 // We use weld::Entry's "changed" notification here, not weld::SpinButton's "value_changed", because
229 // the latter only fires after the control looses focus; so the notification would not fire during
230 // typing a big number, so that user typing it and immediately clicking "Insert" would not see the
232 // Since the notification is called in weld::Entry context, we can only rely on what's available for
233 // used weld::Entry's notification; specifically, we have to call spin buttons' get_text() instead
234 // of get_value(), because the latter is not guaranteed to return an up-to-date value at this point
235 // (depends on vcl plugin used).
236 IMPL_LINK( SwInsTableDlg
, ModifyRowCol
, weld::Entry
&, rEdit
, void )
238 sal_Int64 nRow
= m_xRowSpinButton
->get_text().toInt64();
239 sal_Int64 nCol
= m_xColSpinButton
->get_text().toInt64();
242 m_xRowSpinButton
->set_message_type(weld::EntryMessageType::Warning
);
243 m_xWarning
->set_visible(true);
247 m_xRowSpinButton
->set_message_type(weld::EntryMessageType::Normal
);
251 m_xColSpinButton
->set_message_type(weld::EntryMessageType::Warning
);
252 m_xWarning
->set_visible(true);
256 m_xColSpinButton
->set_message_type(weld::EntryMessageType::Normal
);
258 if (nRow
<= 255 && nCol
<= 63)
260 m_xWarning
->set_visible(false);
262 if (&rEdit
!= m_xColSpinButton
.get())
267 // adjust depending NF for repeated rows
268 sal_Int64 nMax
= ( nRow
== 1 )? 1 : nRow
- 1 ;
269 sal_Int64 nActVal
= m_xRepeatHeaderNF
->get_value();
271 m_xRepeatHeaderNF
->set_max( nMax
);
274 m_xRepeatHeaderNF
->set_value( nMax
);
275 else if( nActVal
< nEnteredValRepeatHeaderNF
)
276 m_xRepeatHeaderNF
->set_value(std::min(nEnteredValRepeatHeaderNF
, nMax
));
280 IMPL_LINK_NOARG(SwInsTableDlg
, CheckBoxHdl
, weld::ToggleButton
&, void)
282 m_xRepeatHeaderCB
->set_sensitive(m_xHeaderCB
->get_active());
283 RepeatHeaderCheckBoxHdl(*m_xRepeatHeaderCB
);
286 IMPL_LINK_NOARG(SwInsTableDlg
, RepeatHeaderCheckBoxHdl
, weld::ToggleButton
&, void)
288 m_xRepeatGroup
->set_sensitive(m_xHeaderCB
->get_active() && m_xRepeatHeaderCB
->get_active());
291 IMPL_LINK_NOARG(SwInsTableDlg
, ModifyRepeatHeaderNF_Hdl
, weld::SpinButton
&, void)
293 nEnteredValRepeatHeaderNF
= m_xRepeatHeaderNF
->get_value();
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */