tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / basctl / source / basicide / brkdlg.cxx
blobaafe50aafcc8408b23f0ac97a9211bb34eed94f4
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 <sal/config.h>
22 #include "breakpoint.hxx"
23 #include "brkdlg.hxx"
24 #include <basobj.hxx>
26 #include <sfx2/dispatch.hxx>
27 #include <sfx2/sfxsids.hrc>
28 #include <svl/itemset.hxx>
30 namespace basctl
32 namespace
34 bool lcl_ParseText(OUString const& rText, size_t& rLineNr)
36 // aText should look like "# n" where n > 0
37 // All spaces are ignored, so there can even be spaces within the
38 // number n. (Maybe it would be better to ignore all whitespace instead
39 // of just spaces.)
40 OUString aText(rText.replaceAll(" ", ""));
41 if (aText.isEmpty())
42 return false;
43 sal_Unicode cFirst = aText[0];
44 if (cFirst != '#' && (cFirst < '0' || cFirst > '9'))
45 return false;
46 if (cFirst == '#')
47 aText = aText.copy(1);
48 sal_Int32 n = aText.toInt32();
49 if (n <= 0)
50 return false;
51 rLineNr = static_cast<size_t>(n);
52 return true;
55 } // namespace
57 BreakPointDialog::BreakPointDialog(weld::Window* pParent, BreakPointList& rBrkPntList)
58 : GenericDialogController(pParent, u"modules/BasicIDE/ui/managebreakpoints.ui"_ustr,
59 u"ManageBreakpointsDialog"_ustr)
60 , m_rOriginalBreakPointList(rBrkPntList)
61 , m_aModifiedBreakPointList(rBrkPntList)
62 , m_xComboBox(m_xBuilder->weld_entry_tree_view(u"entriesgrid"_ustr, u"entries"_ustr,
63 u"entrieslist"_ustr))
64 , m_xOKButton(m_xBuilder->weld_button(u"ok"_ustr))
65 , m_xNewButton(m_xBuilder->weld_button(u"new"_ustr))
66 , m_xDelButton(m_xBuilder->weld_button(u"delete"_ustr))
67 , m_xCheckBox(m_xBuilder->weld_check_button(u"active"_ustr))
68 , m_xNumericField(m_xBuilder->weld_spin_button(u"pass"_ustr))
70 m_xComboBox->set_size_request(m_xComboBox->get_approximate_digit_width() * 20, -1);
71 m_xComboBox->set_height_request_by_rows(12);
73 m_xComboBox->freeze();
74 for (size_t i = 0, n = m_aModifiedBreakPointList.size(); i < n; ++i)
76 BreakPoint& rBrk = m_aModifiedBreakPointList.at(i);
77 OUString aEntryStr("# " + OUString::number(rBrk.nLine));
78 m_xComboBox->append_text(aEntryStr);
80 m_xComboBox->thaw();
82 m_xOKButton->connect_clicked(LINK(this, BreakPointDialog, ButtonHdl));
83 m_xNewButton->connect_clicked(LINK(this, BreakPointDialog, ButtonHdl));
84 m_xDelButton->connect_clicked(LINK(this, BreakPointDialog, ButtonHdl));
86 m_xCheckBox->connect_toggled(LINK(this, BreakPointDialog, CheckBoxHdl));
87 m_xComboBox->connect_changed(LINK(this, BreakPointDialog, EditModifyHdl));
88 m_xComboBox->connect_row_activated(LINK(this, BreakPointDialog, TreeModifyHdl));
89 m_xComboBox->grab_focus();
91 m_xNumericField->set_range(0, 0x7FFFFFFF);
92 m_xNumericField->set_increments(1, 10);
93 m_xNumericField->connect_value_changed(LINK(this, BreakPointDialog, FieldModifyHdl));
95 if (m_xComboBox->get_count())
96 m_xComboBox->set_active(0);
98 if (m_aModifiedBreakPointList.size())
99 UpdateFields(m_aModifiedBreakPointList.at(0));
101 CheckButtons();
104 BreakPointDialog::~BreakPointDialog() {}
106 void BreakPointDialog::SetCurrentBreakPoint(BreakPoint const& rBrk)
108 OUString aStr("# " + OUString::number(rBrk.nLine));
109 m_xComboBox->set_entry_text(aStr);
110 UpdateFields(rBrk);
113 void BreakPointDialog::CheckButtons()
115 // "New" button is enabled if the combo box edit contains a valid line
116 // number that is not already present in the combo box list; otherwise
117 // "OK" and "Delete" buttons are enabled:
118 size_t nLine;
119 if (lcl_ParseText(m_xComboBox->get_active_text(), nLine)
120 && m_aModifiedBreakPointList.FindBreakPoint(nLine) == nullptr)
122 m_xNewButton->set_sensitive(true);
123 m_xOKButton->set_sensitive(false);
124 m_xDelButton->set_sensitive(false);
125 m_xDialog->change_default_widget(m_xDelButton.get(), m_xNewButton.get());
127 else
129 m_xNewButton->set_sensitive(false);
130 m_xOKButton->set_sensitive(true);
131 m_xDelButton->set_sensitive(true);
132 m_xDialog->change_default_widget(m_xNewButton.get(), m_xDelButton.get());
136 IMPL_LINK(BreakPointDialog, CheckBoxHdl, weld::Toggleable&, rButton, void)
138 BreakPoint* pBrk = GetSelectedBreakPoint();
139 if (pBrk)
140 pBrk->bEnabled = rButton.get_active();
143 IMPL_LINK(BreakPointDialog, EditModifyHdl, weld::ComboBox&, rBox, void)
145 CheckButtons();
147 int nEntry = rBox.find_text(rBox.get_active_text());
148 if (nEntry == -1)
149 return;
150 BreakPoint& rBrk = m_aModifiedBreakPointList.at(nEntry);
151 UpdateFields(rBrk);
154 IMPL_LINK(BreakPointDialog, FieldModifyHdl, weld::SpinButton&, rEdit, void)
156 BreakPoint* pBrk = GetSelectedBreakPoint();
157 if (pBrk)
158 pBrk->nStopAfter = rEdit.get_value();
161 IMPL_LINK_NOARG(BreakPointDialog, TreeModifyHdl, weld::TreeView&, bool)
163 if (m_xDelButton->get_sensitive())
164 ButtonHdl(*m_xDelButton);
165 return true;
168 IMPL_LINK(BreakPointDialog, ButtonHdl, weld::Button&, rButton, void)
170 if (&rButton == m_xOKButton.get())
172 m_rOriginalBreakPointList.transfer(m_aModifiedBreakPointList);
173 m_xDialog->response(RET_OK);
175 else if (&rButton == m_xNewButton.get())
177 // keep checkbox in mind!
178 OUString aText(m_xComboBox->get_active_text());
179 size_t nLine;
180 bool bValid = lcl_ParseText(aText, nLine);
181 if (bValid)
183 BreakPoint aBrk(nLine);
184 aBrk.bEnabled = m_xCheckBox->get_active();
185 aBrk.nStopAfter = static_cast<size_t>(m_xNumericField->get_value());
186 m_aModifiedBreakPointList.InsertSorted(aBrk);
187 OUString aEntryStr("# " + OUString::number(aBrk.nLine));
188 m_xComboBox->append_text(aEntryStr);
189 if (SfxDispatcher* pDispatcher = GetDispatcher())
190 pDispatcher->Execute(SID_BASICIDE_BRKPNTSCHANGED);
192 else
194 m_xComboBox->set_active_text(aText);
195 m_xComboBox->grab_focus();
197 CheckButtons();
199 else if (&rButton == m_xDelButton.get())
201 int nEntry = m_xComboBox->find_text(m_xComboBox->get_active_text());
202 if (nEntry != -1)
204 m_aModifiedBreakPointList.remove(nEntry);
205 m_xComboBox->remove(nEntry);
206 if (nEntry && nEntry >= m_xComboBox->get_count())
207 nEntry--;
208 m_xComboBox->set_active_text(m_xComboBox->get_text(nEntry));
209 if (SfxDispatcher* pDispatcher = GetDispatcher())
210 pDispatcher->Execute(SID_BASICIDE_BRKPNTSCHANGED);
211 CheckButtons();
216 void BreakPointDialog::UpdateFields(BreakPoint const& rBrk)
218 m_xCheckBox->set_active(rBrk.bEnabled);
219 m_xNumericField->set_value(rBrk.nStopAfter);
222 BreakPoint* BreakPointDialog::GetSelectedBreakPoint()
224 int nEntry = m_xComboBox->find_text(m_xComboBox->get_active_text());
225 if (nEntry == -1)
226 return nullptr;
227 return &m_aModifiedBreakPointList.at(nEntry);
230 } // namespace basctl
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */