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 if (comphelper::LibreOfficeKit::isActive())
35 nInsMode
= SwInsertTableFlags::DefaultBorder
;
36 rName
= m_xNameEdit
->get_text();
37 rRow
= m_xRowSpinButton
->get_value();
38 rCol
= m_xColSpinButton
->get_value();
40 if (m_xHeaderCB
->get_active())
41 nInsMode
|= SwInsertTableFlags::Headline
;
42 if (m_xRepeatHeaderCB
->get_sensitive() && m_xRepeatHeaderCB
->get_active())
43 rInsTableOpts
.mnRowsToRepeat
= m_xRepeatHeaderNF
->get_value();
45 rInsTableOpts
.mnRowsToRepeat
= 0;
46 if (!m_xDontSplitCB
->get_active())
47 nInsMode
|= SwInsertTableFlags::SplitLayout
;
48 if (m_xTAutoFormat
&& !comphelper::LibreOfficeKit::isActive())
50 prTAFormat
.reset(new SwTableAutoFormat( *m_xTAutoFormat
));
51 rAutoName
= prTAFormat
->GetName();
54 rInsTableOpts
.mnInsMode
= nInsMode
;
57 IMPL_LINK(SwInsTableDlg
, TextFilterHdl
, OUString
&, rTest
, bool)
59 rTest
= m_aTextFilter
.filter(rTest
);
63 SwInsTableDlg::SwInsTableDlg(SwView
& rView
)
64 : SfxDialogController(rView
.GetFrameWeld(), u
"modules/swriter/ui/inserttable.ui"_ustr
, u
"InsertTableDialog"_ustr
)
65 , m_aTextFilter(u
" .<>"_ustr
)
66 , m_pShell(&rView
.GetWrtShell())
67 , m_nEnteredValRepeatHeaderNF(-1)
68 , m_xNameEdit(m_xBuilder
->weld_entry(u
"nameedit"_ustr
))
69 , m_xWarning(m_xBuilder
->weld_label(u
"lbwarning"_ustr
))
70 , m_xColSpinButton(m_xBuilder
->weld_spin_button(u
"colspin"_ustr
))
71 , m_xRowSpinButton(m_xBuilder
->weld_spin_button(u
"rowspin"_ustr
))
72 , m_xHeaderCB(m_xBuilder
->weld_check_button(u
"headercb"_ustr
))
73 , m_xRepeatHeaderCB(m_xBuilder
->weld_check_button(u
"repeatcb"_ustr
))
74 , m_xRepeatHeaderNF(m_xBuilder
->weld_spin_button(u
"repeatheaderspin"_ustr
))
75 , m_xRepeatGroup(m_xBuilder
->weld_widget(u
"repeatgroup"_ustr
))
76 , m_xDontSplitCB(m_xBuilder
->weld_check_button(u
"dontsplitcb"_ustr
))
77 , m_xInsertBtn(m_xBuilder
->weld_button(u
"ok"_ustr
))
78 , m_xLbFormat(m_xBuilder
->weld_tree_view(u
"formatlbinstable"_ustr
))
79 , m_xWndPreview(new weld::CustomWeld(*m_xBuilder
, u
"previewinstable"_ustr
, m_aWndPreview
))
80 , m_xStyleFrame(m_xBuilder
->weld_frame(u
"stylesframe"_ustr
))
82 if (comphelper::LibreOfficeKit::isActive())
83 m_xStyleFrame
->hide();
85 const int nWidth
= m_xLbFormat
->get_approximate_digit_width() * 32;
86 const int nHeight
= m_xLbFormat
->get_height_rows(8);
87 m_xLbFormat
->set_size_request(nWidth
, nHeight
);
88 m_xWndPreview
->set_size_request(nWidth
, nHeight
);
90 m_xNameEdit
->connect_insert_text(LINK(this, SwInsTableDlg
, TextFilterHdl
));
91 m_xNameEdit
->set_text(m_pShell
->GetUniqueTableName());
92 m_xNameEdit
->connect_changed(LINK(this, SwInsTableDlg
, ModifyName
));
93 m_xRowSpinButton
->connect_changed(LINK(this, SwInsTableDlg
, ModifyRowCol
));
94 m_xColSpinButton
->connect_changed(LINK(this, SwInsTableDlg
, ModifyRowCol
));
96 m_xInsertBtn
->connect_clicked(LINK(this, SwInsTableDlg
, OKHdl
));
98 bool bHTMLMode
= 0 != (::GetHtmlMode(rView
.GetDocShell())&HTMLMODE_ON
);
99 const SwModuleOptions
* pModOpt
= SwModule::get()->GetModuleConfig();
101 SwInsertTableOptions aInsOpts
= pModOpt
->GetInsTableFlags(bHTMLMode
);
102 SwInsertTableFlags nInsTableFlags
= aInsOpts
.mnInsMode
;
104 m_xHeaderCB
->set_active(bool(nInsTableFlags
& SwInsertTableFlags::Headline
));
105 m_xRepeatHeaderCB
->set_active(aInsOpts
.mnRowsToRepeat
> 0);
107 m_xDontSplitCB
->hide();
109 m_xDontSplitCB
->set_active(!(nInsTableFlags
& SwInsertTableFlags::SplitLayout
));
111 m_xRepeatHeaderNF
->connect_value_changed( LINK( this, SwInsTableDlg
, ModifyRepeatHeaderNF_Hdl
) );
112 m_xHeaderCB
->connect_toggled( LINK( this, SwInsTableDlg
, CheckBoxHdl
) );
113 m_xRepeatHeaderCB
->connect_toggled( LINK( this, SwInsTableDlg
, RepeatHeaderCheckBoxHdl
) );
114 RepeatHeaderCheckBoxHdl(*m_xRepeatHeaderCB
);
115 CheckBoxHdl(*m_xHeaderCB
);
117 sal_Int64 nMax
= m_xRowSpinButton
->get_value();
122 m_xRepeatHeaderNF
->set_max( nMax
);
124 InitAutoTableFormat();
125 m_xWarning
->set_label_type(weld::LabelType::Warning
);
128 void SwInsTableDlg::InitAutoTableFormat()
130 m_aWndPreview
.DetectRTL(m_pShell
);
132 m_xLbFormat
->connect_selection_changed(LINK(this, SwInsTableDlg
, SelFormatHdl
));
134 m_xTableTable
.reset(new SwTableAutoFormatTable
);
135 m_xTableTable
->Load();
137 // Add "- none -" style autoformat table.
138 m_xLbFormat
->append_text(SwViewShell::GetShellRes()->aStrNone
); // Insert to listbox
140 // Add other styles of autoformat tables.
141 for (sal_uInt8 i
= 0, nCount
= static_cast<sal_uInt8
>(m_xTableTable
->size());
144 SwTableAutoFormat
const& rFormat
= (*m_xTableTable
)[ i
];
145 m_xLbFormat
->append_text(rFormat
.GetName());
146 if (m_xTAutoFormat
&& rFormat
.GetName() == m_xTAutoFormat
->GetName())
150 // Change this min variable if you add autotable manually.
151 minTableIndexInLb
= 1;
152 maxTableIndexInLb
= minTableIndexInLb
+ static_cast<sal_uInt8
>(m_xTableTable
->size());
153 // 1 means default table style
154 // unfortunately when the table has a style sw/qa/uitest/writer_tests4/tdf115573.py fails
155 // because tables that have pre-applied style resets the style of the elements in their cells
156 // when a new row is inserted and the ui test above relies on that.
158 m_xLbFormat
->select(m_lbIndex
);
159 m_tbIndex
= lbIndexToTableIndex(m_lbIndex
);
161 SelFormatHdl( *m_xLbFormat
);
164 sal_uInt8
SwInsTableDlg::lbIndexToTableIndex( const sal_uInt8 listboxIndex
)
166 if( minTableIndexInLb
!= maxTableIndexInLb
&&
167 minTableIndexInLb
<= listboxIndex
&&
168 listboxIndex
< maxTableIndexInLb
)
170 return listboxIndex
- minTableIndexInLb
;
176 IMPL_LINK_NOARG(SwInsTableDlg
, SelFormatHdl
, weld::TreeView
&, void)
178 // Get index of selected item from the listbox
179 m_lbIndex
= static_cast<sal_uInt8
>(m_xLbFormat
->get_selected_index());
180 m_tbIndex
= lbIndexToTableIndex( m_lbIndex
);
182 // To understand this index mapping, look InitAutoTableFormat function to
183 // see how listbox item is implemented.
184 if( m_tbIndex
< 255 )
185 m_aWndPreview
.NotifyChange( (*m_xTableTable
)[m_tbIndex
] );
188 SwTableAutoFormat
aTmp( SwViewShell::GetShellRes()->aStrNone
);
191 m_aWndPreview
.NotifyChange( aTmp
);
195 IMPL_LINK_NOARG(SwInsTableDlg
, OKHdl
, weld::Button
&, void)
197 if( m_tbIndex
< 255 )
198 m_pShell
->SetTableStyle((*m_xTableTable
)[m_tbIndex
]);
200 if( m_tbIndex
< 255 )
203 *m_xTAutoFormat
= (*m_xTableTable
)[ m_tbIndex
];
205 m_xTAutoFormat
.reset(new SwTableAutoFormat( (*m_xTableTable
)[ m_tbIndex
] ));
209 m_xTAutoFormat
.reset(new SwTableAutoFormat( SwViewShell::GetShellRes()->aStrNone
));
210 m_xTAutoFormat
->DisableAll();
213 m_xDialog
->response(RET_OK
);
216 IMPL_LINK( SwInsTableDlg
, ModifyName
, weld::Entry
&, rEdit
, void )
218 OUString sTableName
= rEdit
.get_text();
219 m_xInsertBtn
->set_sensitive(m_pShell
->GetTableStyle(sTableName
) == nullptr);
222 // We use weld::Entry's "changed" notification here, not weld::SpinButton's "value_changed", because
223 // the latter only fires after the control looses focus; so the notification would not fire during
224 // typing a big number, so that user typing it and immediately clicking "Insert" would not see the
226 // Since the notification is called in weld::Entry context, we can only rely on what's available for
227 // used weld::Entry's notification; specifically, we have to call spin buttons' get_text() instead
228 // of get_value(), because the latter is not guaranteed to return an up-to-date value at this point
229 // (depends on vcl plugin used).
230 IMPL_LINK( SwInsTableDlg
, ModifyRowCol
, weld::Entry
&, rEdit
, void )
232 sal_Int64 nRow
= m_xRowSpinButton
->get_text().toInt64();
233 sal_Int64 nCol
= m_xColSpinButton
->get_text().toInt64();
236 m_xRowSpinButton
->set_message_type(weld::EntryMessageType::Warning
);
237 m_xWarning
->set_visible(true);
241 m_xRowSpinButton
->set_message_type(weld::EntryMessageType::Normal
);
245 m_xColSpinButton
->set_message_type(weld::EntryMessageType::Warning
);
246 m_xWarning
->set_visible(true);
250 m_xColSpinButton
->set_message_type(weld::EntryMessageType::Normal
);
252 if (nRow
<= 255 && nCol
<= 63)
254 m_xWarning
->set_visible(false);
256 if (&rEdit
== m_xColSpinButton
.get())
262 // adjust depending NF for repeated rows
263 sal_Int64 nMax
= ( nRow
== 1 )? 1 : nRow
- 1 ;
264 sal_Int64 nActVal
= m_xRepeatHeaderNF
->get_value();
266 m_xRepeatHeaderNF
->set_max( nMax
);
269 m_xRepeatHeaderNF
->set_value( nMax
);
270 else if( nActVal
< m_nEnteredValRepeatHeaderNF
)
271 m_xRepeatHeaderNF
->set_value(std::min(m_nEnteredValRepeatHeaderNF
, nMax
));
274 IMPL_LINK_NOARG(SwInsTableDlg
, CheckBoxHdl
, weld::Toggleable
&, void)
276 m_xRepeatHeaderCB
->set_sensitive(m_xHeaderCB
->get_active());
277 RepeatHeaderCheckBoxHdl(*m_xRepeatHeaderCB
);
280 IMPL_LINK_NOARG(SwInsTableDlg
, RepeatHeaderCheckBoxHdl
, weld::Toggleable
&, void)
282 m_xRepeatGroup
->set_sensitive(m_xHeaderCB
->get_active() && m_xRepeatHeaderCB
->get_active());
285 IMPL_LINK_NOARG(SwInsTableDlg
, ModifyRepeatHeaderNF_Hdl
, weld::SpinButton
&, void)
287 m_nEnteredValRepeatHeaderNF
= m_xRepeatHeaderNF
->get_value();
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */