Bump version to 24.04.3.4
[LibreOffice.git] / basctl / source / basicide / brkdlg.cxx
blobf39255371b19bb459f42983df9fdd30ca715247c
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, "modules/BasicIDE/ui/managebreakpoints.ui",
59 "ManageBreakpointsDialog")
60 , m_rOriginalBreakPointList(rBrkPntList)
61 , m_aModifiedBreakPointList(rBrkPntList)
62 , m_xComboBox(m_xBuilder->weld_entry_tree_view("entriesgrid", "entries", "entrieslist"))
63 , m_xOKButton(m_xBuilder->weld_button("ok"))
64 , m_xNewButton(m_xBuilder->weld_button("new"))
65 , m_xDelButton(m_xBuilder->weld_button("delete"))
66 , m_xCheckBox(m_xBuilder->weld_check_button("active"))
67 , m_xNumericField(m_xBuilder->weld_spin_button("pass"))
69 m_xComboBox->set_size_request(m_xComboBox->get_approximate_digit_width() * 20, -1);
70 m_xComboBox->set_height_request_by_rows(12);
72 m_xComboBox->freeze();
73 for (size_t i = 0, n = m_aModifiedBreakPointList.size(); i < n; ++i)
75 BreakPoint& rBrk = m_aModifiedBreakPointList.at(i);
76 OUString aEntryStr("# " + OUString::number(rBrk.nLine));
77 m_xComboBox->append_text(aEntryStr);
79 m_xComboBox->thaw();
81 m_xOKButton->connect_clicked(LINK(this, BreakPointDialog, ButtonHdl));
82 m_xNewButton->connect_clicked(LINK(this, BreakPointDialog, ButtonHdl));
83 m_xDelButton->connect_clicked(LINK(this, BreakPointDialog, ButtonHdl));
85 m_xCheckBox->connect_toggled(LINK(this, BreakPointDialog, CheckBoxHdl));
86 m_xComboBox->connect_changed(LINK(this, BreakPointDialog, EditModifyHdl));
87 m_xComboBox->connect_row_activated(LINK(this, BreakPointDialog, TreeModifyHdl));
88 m_xComboBox->grab_focus();
90 m_xNumericField->set_range(0, 0x7FFFFFFF);
91 m_xNumericField->set_increments(1, 10);
92 m_xNumericField->connect_value_changed(LINK(this, BreakPointDialog, FieldModifyHdl));
94 if (m_xComboBox->get_count())
95 m_xComboBox->set_active(0);
97 if (m_aModifiedBreakPointList.size())
98 UpdateFields(m_aModifiedBreakPointList.at(0));
100 CheckButtons();
103 BreakPointDialog::~BreakPointDialog() {}
105 void BreakPointDialog::SetCurrentBreakPoint(BreakPoint const& rBrk)
107 OUString aStr("# " + OUString::number(rBrk.nLine));
108 m_xComboBox->set_entry_text(aStr);
109 UpdateFields(rBrk);
112 void BreakPointDialog::CheckButtons()
114 // "New" button is enabled if the combo box edit contains a valid line
115 // number that is not already present in the combo box list; otherwise
116 // "OK" and "Delete" buttons are enabled:
117 size_t nLine;
118 if (lcl_ParseText(m_xComboBox->get_active_text(), nLine)
119 && m_aModifiedBreakPointList.FindBreakPoint(nLine) == nullptr)
121 m_xNewButton->set_sensitive(true);
122 m_xOKButton->set_sensitive(false);
123 m_xDelButton->set_sensitive(false);
124 m_xDialog->change_default_widget(m_xDelButton.get(), m_xNewButton.get());
126 else
128 m_xNewButton->set_sensitive(false);
129 m_xOKButton->set_sensitive(true);
130 m_xDelButton->set_sensitive(true);
131 m_xDialog->change_default_widget(m_xNewButton.get(), m_xDelButton.get());
135 IMPL_LINK(BreakPointDialog, CheckBoxHdl, weld::Toggleable&, rButton, void)
137 BreakPoint* pBrk = GetSelectedBreakPoint();
138 if (pBrk)
139 pBrk->bEnabled = rButton.get_active();
142 IMPL_LINK(BreakPointDialog, EditModifyHdl, weld::ComboBox&, rBox, void)
144 CheckButtons();
146 int nEntry = rBox.find_text(rBox.get_active_text());
147 if (nEntry == -1)
148 return;
149 BreakPoint& rBrk = m_aModifiedBreakPointList.at(nEntry);
150 UpdateFields(rBrk);
153 IMPL_LINK(BreakPointDialog, FieldModifyHdl, weld::SpinButton&, rEdit, void)
155 BreakPoint* pBrk = GetSelectedBreakPoint();
156 if (pBrk)
157 pBrk->nStopAfter = rEdit.get_value();
160 IMPL_LINK_NOARG(BreakPointDialog, TreeModifyHdl, weld::TreeView&, bool)
162 if (m_xDelButton->get_sensitive())
163 ButtonHdl(*m_xDelButton);
164 return true;
167 IMPL_LINK(BreakPointDialog, ButtonHdl, weld::Button&, rButton, void)
169 if (&rButton == m_xOKButton.get())
171 m_rOriginalBreakPointList.transfer(m_aModifiedBreakPointList);
172 m_xDialog->response(RET_OK);
174 else if (&rButton == m_xNewButton.get())
176 // keep checkbox in mind!
177 OUString aText(m_xComboBox->get_active_text());
178 size_t nLine;
179 bool bValid = lcl_ParseText(aText, nLine);
180 if (bValid)
182 BreakPoint aBrk(nLine);
183 aBrk.bEnabled = m_xCheckBox->get_active();
184 aBrk.nStopAfter = static_cast<size_t>(m_xNumericField->get_value());
185 m_aModifiedBreakPointList.InsertSorted(aBrk);
186 OUString aEntryStr("# " + OUString::number(aBrk.nLine));
187 m_xComboBox->append_text(aEntryStr);
188 if (SfxDispatcher* pDispatcher = GetDispatcher())
189 pDispatcher->Execute(SID_BASICIDE_BRKPNTSCHANGED);
191 else
193 m_xComboBox->set_active_text(aText);
194 m_xComboBox->grab_focus();
196 CheckButtons();
198 else if (&rButton == m_xDelButton.get())
200 int nEntry = m_xComboBox->find_text(m_xComboBox->get_active_text());
201 if (nEntry != -1)
203 m_aModifiedBreakPointList.remove(nEntry);
204 m_xComboBox->remove(nEntry);
205 if (nEntry && nEntry >= m_xComboBox->get_count())
206 nEntry--;
207 m_xComboBox->set_active_text(m_xComboBox->get_text(nEntry));
208 if (SfxDispatcher* pDispatcher = GetDispatcher())
209 pDispatcher->Execute(SID_BASICIDE_BRKPNTSCHANGED);
210 CheckButtons();
215 void BreakPointDialog::UpdateFields(BreakPoint const& rBrk)
217 m_xCheckBox->set_active(rBrk.bEnabled);
218 m_xNumericField->set_value(rBrk.nStopAfter);
221 BreakPoint* BreakPointDialog::GetSelectedBreakPoint()
223 int nEntry = m_xComboBox->find_text(m_xComboBox->get_active_text());
224 if (nEntry == -1)
225 return nullptr;
226 return &m_aModifiedBreakPointList.at(nEntry);
229 } // namespace basctl
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */