LibreOffice Module sw (master)  1
instable.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
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>
27 
28 
29 void SwInsTableDlg::GetValues( OUString& rName, sal_uInt16& rRow, sal_uInt16& rCol,
30  SwInsertTableOptions& rInsTableOpts, OUString& rAutoName,
31  std::unique_ptr<SwTableAutoFormat>& prTAFormat )
32 {
34  rName = m_xNameEdit->get_text();
35  rRow = m_xRowSpinButton->get_value();
36  rCol = m_xColSpinButton->get_value();
37 
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())
46  if( pTAutoFormat )
47  {
48  prTAFormat.reset(new SwTableAutoFormat( *pTAutoFormat ));
49  rAutoName = prTAFormat->GetName();
50  }
51 
52  rInsTableOpts.mnInsMode = nInsMode;
53 }
54 
55 IMPL_LINK(SwInsTableDlg, TextFilterHdl, OUString&, rTest, bool)
56 {
57  rTest = m_aTextFilter.filter(rTest);
58  return true;
59 }
60 
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"))
80 {
82  m_xStyleFrame->hide();
83 
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);
88 
89  m_xNameEdit->connect_insert_text(LINK(this, SwInsTableDlg, TextFilterHdl));
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));
94 
95  m_xInsertBtn->connect_clicked(LINK(this, SwInsTableDlg, OKHdl));
96 
97  bool bHTMLMode = 0 != (::GetHtmlMode(rView.GetDocShell())&HTMLMODE_ON);
98  const SwModuleOptions* pModOpt = SW_MOD()->GetModuleConfig();
99 
100  SwInsertTableOptions aInsOpts = pModOpt->GetInsTableFlags(bHTMLMode);
101  SwInsertTableFlags nInsTableFlags = aInsOpts.mnInsMode;
102 
103  m_xHeaderCB->set_active(bool(nInsTableFlags & SwInsertTableFlags::Headline));
104  m_xRepeatHeaderCB->set_active(aInsOpts.mnRowsToRepeat > 0);
105  if (bHTMLMode)
106  m_xDontSplitCB->hide();
107  else
108  m_xDontSplitCB->set_active(!(nInsTableFlags & SwInsertTableFlags::SplitLayout));
109 
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);
115 
116  sal_Int64 nMax = m_xRowSpinButton->get_value();
117  if( nMax <= 1 )
118  nMax = 1;
119  else
120  --nMax;
121  m_xRepeatHeaderNF->set_max( nMax );
122 
124  m_xWarning->set_label_type(weld::LabelType::Warning);
125 }
126 
128 {
130 
131  m_xLbFormat->connect_changed(LINK(this, SwInsTableDlg, SelFormatHdl));
132 
134  pTableTable->Load();
135 
136  // Add "- none -" style autoformat table.
137  m_xLbFormat->append_text(SwViewShell::GetShellRes()->aStrNone); // Insert to listbox
138 
139  // Add other styles of autoformat tables.
140  for (sal_uInt8 i = 0, nCount = static_cast<sal_uInt8>(pTableTable->size());
141  i < nCount; i++)
142  {
143  SwTableAutoFormat const& rFormat = (*pTableTable)[ i ];
144  m_xLbFormat->append_text(rFormat.GetName());
145  if (pTAutoFormat && rFormat.GetName() == pTAutoFormat->GetName())
146  lbIndex = i;
147  }
148 
149  // Change this min variable if you add autotable manually.
150  minTableIndexInLb = 1;
152  lbIndex = 0;
153  m_xLbFormat->select( lbIndex );
154  tbIndex = lbIndexToTableIndex(lbIndex);
155 
156  SelFormatHdl( *m_xLbFormat );
157 }
158 
160 {
162  minTableIndexInLb <= listboxIndex &&
163  listboxIndex < maxTableIndexInLb )
164  {
165  return listboxIndex - minTableIndexInLb;
166  }
167 
168  return 255;
169 }
170 
171 static void lcl_SetProperties( SwTableAutoFormat* pTableAutoFormat, bool bVal )
172 {
173  pTableAutoFormat->SetFont( bVal );
174  pTableAutoFormat->SetJustify( bVal );
175  pTableAutoFormat->SetFrame( bVal );
176  pTableAutoFormat->SetBackground( bVal );
177  pTableAutoFormat->SetValueFormat( bVal );
178  pTableAutoFormat->SetWidthHeight( bVal );
179 }
180 
182 {
183  // Get index of selected item from the listbox
184  lbIndex = static_cast<sal_uInt8>(m_xLbFormat->get_selected_index());
185  tbIndex = lbIndexToTableIndex( lbIndex );
186 
187  // To understand this index mapping, look InitAutoTableFormat function to
188  // see how listbox item is implemented.
189  if( tbIndex < 255 )
190  m_aWndPreview.NotifyChange( (*pTableTable)[tbIndex] );
191  else
192  {
193  SwTableAutoFormat aTmp( SwViewShell::GetShellRes()->aStrNone );
194  lcl_SetProperties( &aTmp, false );
195 
196  m_aWndPreview.NotifyChange( aTmp );
197  }
198 }
199 
201 {
202  if( tbIndex < 255 )
203  pShell->SetTableStyle((*pTableTable)[tbIndex]);
204 
205  if( tbIndex < 255 )
206  {
207  if( pTAutoFormat )
208  *pTAutoFormat = (*pTableTable)[ tbIndex ];
209  else
210  pTAutoFormat = new SwTableAutoFormat( (*pTableTable)[ tbIndex ] );
211  }
212  else
213  {
214  delete pTAutoFormat;
215  pTAutoFormat = new SwTableAutoFormat( SwViewShell::GetShellRes()->aStrNone );
216  lcl_SetProperties( pTAutoFormat, false );
217  }
218 
219  m_xDialog->response(RET_OK);
220 }
221 
222 IMPL_LINK( SwInsTableDlg, ModifyName, weld::Entry&, rEdit, void )
223 {
224  OUString sTableName = rEdit.get_text();
225  m_xInsertBtn->set_sensitive(pShell->GetTableStyle(sTableName) == nullptr);
226 }
227 
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
231 // warning.
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 )
237 {
238  sal_Int64 nRow = m_xRowSpinButton->get_text().toInt64();
239  sal_Int64 nCol = m_xColSpinButton->get_text().toInt64();
240  if (nRow > 255)
241  {
242  m_xRowSpinButton->set_message_type(weld::EntryMessageType::Warning);
243  m_xWarning->set_visible(true);
244  }
245  else
246  {
247  m_xRowSpinButton->set_message_type(weld::EntryMessageType::Normal);
248  }
249  if (nCol > 63)
250  {
251  m_xColSpinButton->set_message_type(weld::EntryMessageType::Warning);
252  m_xWarning->set_visible(true);
253  }
254  else
255  {
256  m_xColSpinButton->set_message_type(weld::EntryMessageType::Normal);
257  }
258  if (nRow <= 255 && nCol <= 63)
259  {
260  m_xWarning->set_visible(false);
261  }
262  if (&rEdit != m_xColSpinButton.get())
263  {
264  if(!nRow)
265  nRow = 1;
266 
267  // adjust depending NF for repeated rows
268  sal_Int64 nMax = ( nRow == 1 )? 1 : nRow - 1 ;
269  sal_Int64 nActVal = m_xRepeatHeaderNF->get_value();
270 
271  m_xRepeatHeaderNF->set_max( nMax );
272 
273  if( nActVal > nMax )
274  m_xRepeatHeaderNF->set_value( nMax );
275  else if( nActVal < nEnteredValRepeatHeaderNF )
276  m_xRepeatHeaderNF->set_value(std::min(nEnteredValRepeatHeaderNF, nMax));
277  }
278 }
279 
281 {
282  m_xRepeatHeaderCB->set_sensitive(m_xHeaderCB->get_active());
283  RepeatHeaderCheckBoxHdl(*m_xRepeatHeaderCB);
284 }
285 
286 IMPL_LINK_NOARG(SwInsTableDlg, RepeatHeaderCheckBoxHdl, weld::ToggleButton&, void)
287 {
288  m_xRepeatGroup->set_sensitive(m_xHeaderCB->get_active() && m_xRepeatHeaderCB->get_active());
289 }
290 
291 IMPL_LINK_NOARG(SwInsTableDlg, ModifyRepeatHeaderNF_Hdl, weld::SpinButton&, void)
292 {
293  nEnteredValRepeatHeaderNF = m_xRepeatHeaderNF->get_value();
294 }
295 
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 lbIndexToTableIndex(const sal_uInt8 listboxIndex)
Definition: instable.cxx:159
void SetValueFormat(const bool bNew)
Definition: tblafmt.hxx:234
sal_uInt8 minTableIndexInLb
Definition: instable.hxx:44
static ShellResource * GetShellRes()
Definition: viewsh.cxx:2548
std::unique_ptr< weld::Entry > m_xNameEdit
Definition: instable.hxx:50
SwWrtShell * pShell
Definition: instable.hxx:38
void SetFrame(const bool bNew)
Definition: tblafmt.hxx:232
SwInsertTableFlags mnInsMode
Definition: itabenum.hxx:40
static void lcl_SetProperties(SwTableAutoFormat *pTableAutoFormat, bool bVal)
Definition: instable.cxx:171
sal_uInt16 mnRowsToRepeat
Definition: itabenum.hxx:41
SwInsertTableFlags
Definition: itabenum.hxx:25
void InitAutoTableFormat()
Definition: instable.cxx:127
void DetectRTL(SwWrtShell const *pWrtShell)
void SetFont(const bool bNew)
Definition: tblafmt.hxx:230
int nCount
std::unique_ptr< weld::CheckButton > m_xRepeatHeaderCB
Definition: instable.hxx:55
std::unique_ptr< weld::SpinButton > m_xRowSpinButton
Definition: instable.hxx:53
void SetJustify(const bool bNew)
Definition: tblafmt.hxx:231
std::unique_ptr< weld::TreeView > m_xLbFormat
Definition: instable.hxx:60
AutoFormatPreview m_aWndPreview
Definition: instable.hxx:48
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
Definition: dialoghelp.cxx:20
#define SW_MOD()
Definition: swmodule.hxx:255
std::unique_ptr< weld::SpinButton > m_xRepeatHeaderNF
Definition: instable.hxx:56
int i
IMPL_LINK_NOARG(SwInsTableDlg, SelFormatHdl, weld::TreeView &, void)
Definition: instable.cxx:181
HTMLMODE_ON
sal_uInt16 GetHtmlMode(const SwDocShell *pShell)
Definition: viewopt.cxx:345
size_t size() const
Definition: tblafmt.cxx:929
std::unique_ptr< weld::SpinButton > m_xColSpinButton
Definition: instable.hxx:52
OUString GetUniqueTableName() const
Definition: edfmt.cxx:111
SwTableAutoFormatTable * pTableTable
Definition: instable.hxx:39
void SetWidthHeight(const bool bNew)
Definition: tblafmt.hxx:235
void SetBackground(const bool bNew)
Definition: tblafmt.hxx:233
std::unique_ptr< weld::CheckButton > m_xHeaderCB
Definition: instable.hxx:54
IMPL_LINK(SwInsTableDlg, TextFilterHdl, OUString &, rTest, bool)
Definition: instable.cxx:55
SwDocShell * GetDocShell()
Definition: view.cxx:1109
virtual OUString get_text() const =0
std::unique_ptr< weld::CustomWeld > m_xWndPreview
Definition: instable.hxx:61
sal_uInt8 maxTableIndexInLb
Definition: instable.hxx:45
const OUString & GetName() const
Definition: tblafmt.hxx:208
weld::Entry & rEdit
SwInsTableDlg(SwView &rView)
Definition: instable.cxx:61
SwTableAutoFormat * pTAutoFormat
Definition: instable.hxx:40
unsigned char sal_uInt8
RET_OK
Reference< XExecutableDialog > m_xDialog
std::unique_ptr< weld::CheckButton > m_xDontSplitCB
Definition: instable.hxx:58
std::unique_ptr< weld::Label > m_xWarning
Definition: instable.hxx:51
std::unique_ptr< weld::Button > m_xInsertBtn
Definition: instable.hxx:59
std::unique_ptr< weld::Frame > m_xStyleFrame
Definition: instable.hxx:62
const SwInsertTableOptions & GetInsTableFlags(bool bHTML) const
Definition: modcfg.hxx:303
sal_uInt8 tbIndex
Definition: instable.hxx:43
sal_uInt8 lbIndex
Definition: instable.hxx:42
Definition: view.hxx:144
void GetValues(OUString &rName, sal_uInt16 &rRow, sal_uInt16 &rCol, SwInsertTableOptions &rInsTableOpts, OUString &rTableAutoFormatName, std::unique_ptr< SwTableAutoFormat > &prTAFormat)
Definition: instable.cxx:29
SAL_DLLPRIVATE bool Load(SvStream &rStream)
Definition: tblafmt.cxx:1060