Skip to content

Commit

Permalink
Prevent notebook from scrolling to top of a cell when selecting it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasper Peeters committed Nov 23, 2024
1 parent 781a985 commit 597cac9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
50 changes: 36 additions & 14 deletions frontend/gtkmm/CodeInput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ std::string trim(const std::string& s)
return std::string(s, b, e - b + 1);
}

CodeInput::exp_input_tv::exp_input_tv(DTree::iterator it, Glib::RefPtr<Gtk::TextBuffer> tb, double scale)
: Gtk::TextView(tb), scale_(scale), datacell(it)
CodeInput::exp_input_tv::exp_input_tv(DTree::iterator it, Glib::RefPtr<Gtk::TextBuffer> tb, double scale,
Glib::RefPtr<Gtk::Adjustment> vadjustment_)
: Gtk::TextView(tb), scale_(scale), datacell(it), vadjustment(vadjustment_)
{
set_events(Gdk::STRUCTURE_MASK);
// get_buffer()->signal_insert().connect(sigc::mem_fun(this, &exp_input_tv::on_my_insert), false);
Expand All @@ -31,20 +32,16 @@ CodeInput::exp_input_tv::exp_input_tv(DTree::iterator it, Glib::RefPtr<Gtk::Text
set_name("CodeInput"); // to be able to style it with CSS
}

//CodeInput::CodeInput()
// : buffer(Gtk::TextBuffer::create()), edit(buffer)
// {
// init();
// }

CodeInput::CodeInput(DTree::iterator it, Glib::RefPtr<Gtk::TextBuffer> tb, double s, const Prefs& prefs)
: buffer(tb), edit(it, tb, s)
CodeInput::CodeInput(DTree::iterator it, Glib::RefPtr<Gtk::TextBuffer> tb, double s, const Prefs& prefs,
Glib::RefPtr<Gtk::Adjustment> vadjustment)
: buffer(tb), edit(it, tb, s, vadjustment)
{
init(prefs);
}

CodeInput::CodeInput(DTree::iterator it, const std::string& txt, double s, const Prefs& prefs)
: buffer(Gtk::TextBuffer::create()), edit(it, buffer, s)
CodeInput::CodeInput(DTree::iterator it, const std::string& txt, double s, const Prefs& prefs,
Glib::RefPtr<Gtk::Adjustment> vadjustment)
: buffer(Gtk::TextBuffer::create()), edit(it, buffer, s, vadjustment)
{
buffer->set_text(txt);
init(prefs);
Expand Down Expand Up @@ -102,6 +99,9 @@ void CodeInput::init(const Prefs& prefs)
}
edit.set_can_focus(true);

auto dummy_adj = Gtk::Adjustment::create(0,0,0);
edit.set_focus_hadjustment(dummy_adj);

add(edit);
// set_border_width(3);
show_all();
Expand Down Expand Up @@ -536,7 +536,6 @@ bool CodeInput::exp_input_tv::on_key_press_event(GdkEventKey* event)
}
else if(is_tab) {
// If one or more lines are selected, indent the whole block.
// FIXME: implement
Gtk::TextBuffer::iterator beg, end;
if(get_buffer()->get_selection_bounds(beg, end)) {
if(beg.starts_line()) {
Expand Down Expand Up @@ -729,9 +728,32 @@ bool CodeInput::exp_input_tv::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
bool CodeInput::exp_input_tv::on_focus_in_event(GdkEventFocus *event)
{
cell_got_focus(datacell);
return Gtk::TextView::on_focus_in_event(event);
vadjustment->set_value(previous_value);
return false;
}

bool CodeInput::exp_input_tv::on_motion_notify_event(GdkEventMotion* event)
{
previous_value = vadjustment->get_value();
return false;
}

// bool CodeInput::exp_input_tv::on_move_cursor_event(Glib::RefPtr<Gtk::TextBuffer::Mark> iter, Gtk::MovementStep step, bool extend_selection)
// {
// std::cerr << "on move cursor" << std::endl;
// return false;
// // return Gtk::TextView::on_move_cursor(iter, step, extend_selection);
// // auto mark_iter = get_buffer()->get_insert();
// //
// // // Explicitly control scrolling with the custom parameters (minimal scrolling)
// // // 0.0 means no scrolling, so it won't scroll automatically
// // get_buffer()->scroll_to_mark(mark_iter, 0.0, false, 0.0, 0.0);
// //
// // // Return false to allow the cursor movement to proceed without interfering with other behaviors
// // return false;
// }


void CodeInput::exp_input_tv::on_show()
{
if(!datacell->hidden)
Expand Down
19 changes: 13 additions & 6 deletions frontend/gtkmm/CodeInput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@ namespace cadabra {
/// it (e.g. in order to know when to display a 'busy' indicator).
/// The scale parameter refers to hdpi scaling.

CodeInput(DTree::iterator, Glib::RefPtr<Gtk::TextBuffer>, double scale, const Prefs& prefs);
CodeInput(DTree::iterator, Glib::RefPtr<Gtk::TextBuffer>, double scale, const Prefs& prefs,
Glib::RefPtr<Gtk::Adjustment>);

/// Initialise with a new TextBuffer (to be created by
/// CodeInput), filling it with the content of the given
/// string.

CodeInput(DTree::iterator, const std::string&, double scale, const Prefs& prefs);
CodeInput(DTree::iterator, const std::string&, double scale, const Prefs& prefs,
Glib::RefPtr<Gtk::Adjustment>);

/// The actual text widget used by CodeInput.

class exp_input_tv : public Gtk::TextView {
public:
exp_input_tv(DTree::iterator, Glib::RefPtr<Gtk::TextBuffer>, double scale);
exp_input_tv(DTree::iterator, Glib::RefPtr<Gtk::TextBuffer>, double scale,
Glib::RefPtr<Gtk::Adjustment>);
virtual bool on_key_press_event(GdkEventKey*) override;
virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>&) override;
virtual bool on_focus_in_event(GdkEventFocus *) override;
virtual void on_show() override;

// virtual bool on_move_cursor_event(Glib::RefPtr<Gtk::TextBuffer::Mark>, Gtk::MovementStep, bool) override;
virtual bool on_motion_notify_event(GdkEventMotion *event) override;

void shift_enter_pressed();
void on_textbuf_change();

Expand All @@ -54,8 +59,10 @@ namespace cadabra {
friend CodeInput;

private:
double scale_;
DTree::iterator datacell;
double scale_;
DTree::iterator datacell;
Glib::RefPtr<Gtk::Adjustment> vadjustment;
double previous_value = 0.0;
};

/// Set highlighting modes.
Expand Down
8 changes: 5 additions & 3 deletions frontend/gtkmm/NotebookWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1269,10 +1269,12 @@ void NotebookWindow::add_cell(const DTree& tr, DTree::iterator it, bool visible)
CodeInput *ci;
// Ensure that all CodeInput cells share the same text buffer.
if(i==0) {
ci = new CodeInput(it, it->textbuf,scale/display_scale,prefs);
ci = new CodeInput(it, it->textbuf, scale/display_scale, prefs,
canvasses[i]->scroll.get_vadjustment() );
global_buffer=ci->buffer;
}
else ci = new CodeInput(it, global_buffer,scale/display_scale,prefs);
else ci = new CodeInput(it, global_buffer, scale/display_scale, prefs,
canvasses[i]->scroll.get_vadjustment());
using namespace std::placeholders;
ci->relay_cursor_pos(std::bind(&NotebookWindow::set_statusbar_message, this, "", _1, _2));
if(read_only)
Expand Down Expand Up @@ -1613,7 +1615,7 @@ void NotebookWindow::scroll_current_cell_into_view()
void NotebookWindow::scroll_cell_into_view(DTree::iterator cell)
{
// std::cerr << "-----" << std::endl;
// std::cerr << "cell content to show: " << cell->textbuf << std::endl;
std::cerr << "cell content to show: " << cell->textbuf << std::endl;

if(current_canvas>=(int)canvasses.size()) return;

Expand Down

0 comments on commit 597cac9

Please sign in to comment.