android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / table / instable.cxx
bloba5aa4cd8385332a4de06d5cf5f1014d733a7915f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <modcfg.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();
42 else
43 rInsTableOpts.mnRowsToRepeat = 0;
44 if (!m_xDontSplitCB->get_active())
45 nInsMode |= SwInsertTableFlags::SplitLayout;
46 if( m_xTAutoFormat )
48 prTAFormat.reset(new SwTableAutoFormat( *m_xTAutoFormat ));
49 rAutoName = prTAFormat->GetName();
52 rInsTableOpts.mnInsMode = nInsMode;
55 IMPL_LINK(SwInsTableDlg, TextFilterHdl, OUString&, rTest, bool)
57 rTest = m_aTextFilter.filter(rTest);
58 return true;
61 SwInsTableDlg::SwInsTableDlg(SwView& rView)
62 : SfxDialogController(rView.GetFrameWeld(), "modules/swriter/ui/inserttable.ui", "InsertTableDialog")
63 , m_aTextFilter(" .<>")
64 , m_pShell(&rView.GetWrtShell())
65 , m_nEnteredValRepeatHeaderNF(-1)
66 , m_xNameEdit(m_xBuilder->weld_entry("nameedit"))
67 , m_xWarning(m_xBuilder->weld_label("lbwarning"))
68 , m_xColSpinButton(m_xBuilder->weld_spin_button("colspin"))
69 , m_xRowSpinButton(m_xBuilder->weld_spin_button("rowspin"))
70 , m_xHeaderCB(m_xBuilder->weld_check_button("headercb"))
71 , m_xRepeatHeaderCB(m_xBuilder->weld_check_button("repeatcb"))
72 , m_xRepeatHeaderNF(m_xBuilder->weld_spin_button("repeatheaderspin"))
73 , m_xRepeatGroup(m_xBuilder->weld_widget("repeatgroup"))
74 , m_xDontSplitCB(m_xBuilder->weld_check_button("dontsplitcb"))
75 , m_xInsertBtn(m_xBuilder->weld_button("ok"))
76 , m_xLbFormat(m_xBuilder->weld_tree_view("formatlbinstable"))
77 , m_xWndPreview(new weld::CustomWeld(*m_xBuilder, "previewinstable", m_aWndPreview))
78 , m_xStyleFrame(m_xBuilder->weld_frame("stylesframe"))
80 if (comphelper::LibreOfficeKit::isActive())
81 m_xStyleFrame->hide();
83 const int nWidth = m_xLbFormat->get_approximate_digit_width() * 32;
84 const int nHeight = m_xLbFormat->get_height_rows(8);
85 m_xLbFormat->set_size_request(nWidth, nHeight);
86 m_xWndPreview->set_size_request(nWidth, nHeight);
88 m_xNameEdit->connect_insert_text(LINK(this, SwInsTableDlg, TextFilterHdl));
89 m_xNameEdit->set_text(m_pShell->GetUniqueTableName());
90 m_xNameEdit->connect_changed(LINK(this, SwInsTableDlg, ModifyName));
91 m_xRowSpinButton->connect_changed(LINK(this, SwInsTableDlg, ModifyRowCol));
92 m_xColSpinButton->connect_changed(LINK(this, SwInsTableDlg, ModifyRowCol));
94 m_xInsertBtn->connect_clicked(LINK(this, SwInsTableDlg, OKHdl));
96 bool bHTMLMode = 0 != (::GetHtmlMode(rView.GetDocShell())&HTMLMODE_ON);
97 const SwModuleOptions* pModOpt = SW_MOD()->GetModuleConfig();
99 SwInsertTableOptions aInsOpts = pModOpt->GetInsTableFlags(bHTMLMode);
100 SwInsertTableFlags nInsTableFlags = aInsOpts.mnInsMode;
102 m_xHeaderCB->set_active(bool(nInsTableFlags & SwInsertTableFlags::Headline));
103 m_xRepeatHeaderCB->set_active(aInsOpts.mnRowsToRepeat > 0);
104 if (bHTMLMode)
105 m_xDontSplitCB->hide();
106 else
107 m_xDontSplitCB->set_active(!(nInsTableFlags & SwInsertTableFlags::SplitLayout));
109 m_xRepeatHeaderNF->connect_value_changed( LINK( this, SwInsTableDlg, ModifyRepeatHeaderNF_Hdl ) );
110 m_xHeaderCB->connect_toggled( LINK( this, SwInsTableDlg, CheckBoxHdl ) );
111 m_xRepeatHeaderCB->connect_toggled( LINK( this, SwInsTableDlg, RepeatHeaderCheckBoxHdl ) );
112 RepeatHeaderCheckBoxHdl(*m_xRepeatHeaderCB);
113 CheckBoxHdl(*m_xHeaderCB);
115 sal_Int64 nMax = m_xRowSpinButton->get_value();
116 if( nMax <= 1 )
117 nMax = 1;
118 else
119 --nMax;
120 m_xRepeatHeaderNF->set_max( nMax );
122 InitAutoTableFormat();
123 m_xWarning->set_label_type(weld::LabelType::Warning);
126 void SwInsTableDlg::InitAutoTableFormat()
128 m_aWndPreview.DetectRTL(m_pShell);
130 m_xLbFormat->connect_changed(LINK(this, SwInsTableDlg, SelFormatHdl));
132 m_xTableTable.reset(new SwTableAutoFormatTable);
133 m_xTableTable->Load();
135 // Add "- none -" style autoformat table.
136 m_xLbFormat->append_text(SwViewShell::GetShellRes()->aStrNone); // Insert to listbox
138 // Add other styles of autoformat tables.
139 for (sal_uInt8 i = 0, nCount = static_cast<sal_uInt8>(m_xTableTable->size());
140 i < nCount; i++)
142 SwTableAutoFormat const& rFormat = (*m_xTableTable)[ i ];
143 m_xLbFormat->append_text(rFormat.GetName());
144 if (m_xTAutoFormat && rFormat.GetName() == m_xTAutoFormat->GetName())
145 m_lbIndex = i;
148 // Change this min variable if you add autotable manually.
149 minTableIndexInLb = 1;
150 maxTableIndexInLb = minTableIndexInLb + static_cast<sal_uInt8>(m_xTableTable->size());
151 // 1 means default table style
152 // unfortunately when the table has a style sw/qa/uitest/writer_tests4/tdf115573.py fails
153 // because tables that have pre-applied style resets the style of the elements in their cells
154 // when a new row is inserted and the ui test above relies on that. For now this is LOK only
155 m_lbIndex = comphelper::LibreOfficeKit::isActive() ? 1 : 0;
156 m_xLbFormat->select(m_lbIndex);
157 m_tbIndex = lbIndexToTableIndex(m_lbIndex);
159 SelFormatHdl( *m_xLbFormat );
162 sal_uInt8 SwInsTableDlg::lbIndexToTableIndex( const sal_uInt8 listboxIndex )
164 if( minTableIndexInLb != maxTableIndexInLb &&
165 minTableIndexInLb <= listboxIndex &&
166 listboxIndex < maxTableIndexInLb )
168 return listboxIndex - minTableIndexInLb;
171 return 255;
174 static void lcl_SetProperties( SwTableAutoFormat* pTableAutoFormat, bool bVal )
176 pTableAutoFormat->SetFont( bVal );
177 pTableAutoFormat->SetJustify( bVal );
178 pTableAutoFormat->SetFrame( bVal );
179 pTableAutoFormat->SetBackground( bVal );
180 pTableAutoFormat->SetValueFormat( bVal );
181 pTableAutoFormat->SetWidthHeight( bVal );
184 IMPL_LINK_NOARG(SwInsTableDlg, SelFormatHdl, weld::TreeView&, void)
186 // Get index of selected item from the listbox
187 m_lbIndex = static_cast<sal_uInt8>(m_xLbFormat->get_selected_index());
188 m_tbIndex = lbIndexToTableIndex( m_lbIndex );
190 // To understand this index mapping, look InitAutoTableFormat function to
191 // see how listbox item is implemented.
192 if( m_tbIndex < 255 )
193 m_aWndPreview.NotifyChange( (*m_xTableTable)[m_tbIndex] );
194 else
196 SwTableAutoFormat aTmp( SwViewShell::GetShellRes()->aStrNone );
197 lcl_SetProperties( &aTmp, false );
199 m_aWndPreview.NotifyChange( aTmp );
203 IMPL_LINK_NOARG(SwInsTableDlg, OKHdl, weld::Button&, void)
205 if( m_tbIndex < 255 )
206 m_pShell->SetTableStyle((*m_xTableTable)[m_tbIndex]);
208 if( m_tbIndex < 255 )
210 if( m_xTAutoFormat )
211 *m_xTAutoFormat = (*m_xTableTable)[ m_tbIndex ];
212 else
213 m_xTAutoFormat.reset(new SwTableAutoFormat( (*m_xTableTable)[ m_tbIndex ] ));
215 else
217 m_xTAutoFormat.reset(new SwTableAutoFormat( SwViewShell::GetShellRes()->aStrNone ));
218 lcl_SetProperties( m_xTAutoFormat.get(), false );
221 m_xDialog->response(RET_OK);
224 IMPL_LINK( SwInsTableDlg, ModifyName, weld::Entry&, rEdit, void )
226 OUString sTableName = rEdit.get_text();
227 m_xInsertBtn->set_sensitive(m_pShell->GetTableStyle(sTableName) == nullptr);
230 // We use weld::Entry's "changed" notification here, not weld::SpinButton's "value_changed", because
231 // the latter only fires after the control looses focus; so the notification would not fire during
232 // typing a big number, so that user typing it and immediately clicking "Insert" would not see the
233 // warning.
234 // Since the notification is called in weld::Entry context, we can only rely on what's available for
235 // used weld::Entry's notification; specifically, we have to call spin buttons' get_text() instead
236 // of get_value(), because the latter is not guaranteed to return an up-to-date value at this point
237 // (depends on vcl plugin used).
238 IMPL_LINK( SwInsTableDlg, ModifyRowCol, weld::Entry&, rEdit, void )
240 sal_Int64 nRow = m_xRowSpinButton->get_text().toInt64();
241 sal_Int64 nCol = m_xColSpinButton->get_text().toInt64();
242 if (nRow > 255)
244 m_xRowSpinButton->set_message_type(weld::EntryMessageType::Warning);
245 m_xWarning->set_visible(true);
247 else
249 m_xRowSpinButton->set_message_type(weld::EntryMessageType::Normal);
251 if (nCol > 63)
253 m_xColSpinButton->set_message_type(weld::EntryMessageType::Warning);
254 m_xWarning->set_visible(true);
256 else
258 m_xColSpinButton->set_message_type(weld::EntryMessageType::Normal);
260 if (nRow <= 255 && nCol <= 63)
262 m_xWarning->set_visible(false);
264 if (&rEdit == m_xColSpinButton.get())
265 return;
267 if(!nRow)
268 nRow = 1;
270 // adjust depending NF for repeated rows
271 sal_Int64 nMax = ( nRow == 1 )? 1 : nRow - 1 ;
272 sal_Int64 nActVal = m_xRepeatHeaderNF->get_value();
274 m_xRepeatHeaderNF->set_max( nMax );
276 if( nActVal > nMax )
277 m_xRepeatHeaderNF->set_value( nMax );
278 else if( nActVal < m_nEnteredValRepeatHeaderNF )
279 m_xRepeatHeaderNF->set_value(std::min(m_nEnteredValRepeatHeaderNF, nMax));
282 IMPL_LINK_NOARG(SwInsTableDlg, CheckBoxHdl, weld::Toggleable&, void)
284 m_xRepeatHeaderCB->set_sensitive(m_xHeaderCB->get_active());
285 RepeatHeaderCheckBoxHdl(*m_xRepeatHeaderCB);
288 IMPL_LINK_NOARG(SwInsTableDlg, RepeatHeaderCheckBoxHdl, weld::Toggleable&, void)
290 m_xRepeatGroup->set_sensitive(m_xHeaderCB->get_active() && m_xRepeatHeaderCB->get_active());
293 IMPL_LINK_NOARG(SwInsTableDlg, ModifyRepeatHeaderNF_Hdl, weld::SpinButton&, void)
295 m_nEnteredValRepeatHeaderNF = m_xRepeatHeaderNF->get_value();
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */