Skip to content

Commit

Permalink
More correct handling of colour settings, as a result of which TeX re…
Browse files Browse the repository at this point in the history
…ndering follows theme colours and dark mode becomes somewhat usable. Fix copy-paste bugs and missing features.
  • Loading branch information
Kasper Peeters committed Jun 5, 2024
1 parent a4ee1c5 commit e247c13
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cmake/version.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(CADABRA_VERSION_MAJOR 2)
set(CADABRA_VERSION_MINOR 5)
set(CADABRA_VERSION_PATCH 1)
set(CADABRA_VERSION_TWEAK 1)
set(CADABRA_VERSION_PATCH 2)
set(CADABRA_VERSION_TWEAK 0)
set(COPYRIGHT_YEARS "2001-2024")
math(EXPR SYSTEM_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
find_program(GIT git PATHS ${GIT_DIR})
Expand Down
16 changes: 1 addition & 15 deletions frontend/gtkmm/CodeInput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ void CodeInput::init(const Prefs& prefs)
// scroll_.set_border_width(1);
// scroll_.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
edit.set_wrap_mode(Gtk::WRAP_NONE); // WRAP_WORD_CHAR); wrapping leads to weird effects

// edit.override_background_color(Gdk::RGBA("white"), Gtk::STATE_FLAG_ACTIVE);

// edit.set_name("mywidget");
// gtk_rc_parse_string("style \"mywidget\"\n"
// "{\n"
// " bg[NORMAL] = white\n"
// "}\n"
// "widget \"*.mywidget\" style \"mywidget\"");
//

edit.set_pixels_above_lines(1);
edit.set_pixels_below_lines(1);
edit.set_pixels_inside_wrap(1);
Expand Down Expand Up @@ -602,16 +591,13 @@ bool CodeInput::handle_button_press(GdkEventButton* button)
else if(hastext) sd=refClipboard->wait_for_contents("TEXT");
else if(hasstring) sd=refClipboard->wait_for_contents("STRING");
if(hascadabra || hastext || hasstring) {
// find out _where_ to insert
// Figure out where the mouse cursor is, so we know where to insert.
Gtk::TextBuffer::iterator insertpos;
int somenumber;
edit.get_iter_at_position(insertpos, somenumber, button->x, button->y);
if(insertpos!=edit.get_buffer()->end())
++insertpos;

// std::cerr << "inserting at " << insertpos << " text " << sd.get_data_as_string() << std::endl;
insertpos=edit.get_buffer()->insert(insertpos, sd.get_data_as_string());
// std::cerr << "placing cursor" << std::endl;
edit.get_buffer()->place_cursor(insertpos);
}

Expand Down
24 changes: 17 additions & 7 deletions frontend/gtkmm/NotebookWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,8 @@ void NotebookWindow::load_css()
// std::cerr << "(re)loading css" << std::endl;
std::string text_colour = prefs.highlight ? "black" : "blue";
Glib::ustring data = "";
data += "scrolledwindow { background-color: white; }\n";
data += "textview text { color: "+text_colour+"; background-color: white; -GtkWidget-cursor-aspect-ratio: 0.2; }\n";
data += "scrolledwindow { background-color: @theme_base_color; }\n";
data += "textview text { color: "+text_colour+"; background-color: @theme_base_color; -GtkWidget-cursor-aspect-ratio: 0.2; }\n";
data += "textview *:focus { background-color: #eee; }\n";
data += ".view text selection { color: #fff; background-color: #888; }\n";
data += "textview.error { background: transparent; -GtkWidget-cursor-aspect-ratio: 0.2; color: @theme_fg_color; }\n";
Expand All @@ -754,6 +754,7 @@ void NotebookWindow::load_css()
data += "#mainbox * { transition-property: opacity; transition-duration: 0.5s; }\n";
data += "#mainbox:disabled * { opacity: 0.85; }\n";
data += "label { margin-left: 4px; }\n";
data += "#TeXArea:selected { background-color: #eee; }\n";
data += "spinner { background: none; opacity: 1; -gtk-icon-source: -gtk-icontheme(\"process-working-symbolic\"); }\n";
data += "spinner:checked { opacity: 1; animation: spin 1s linear infinite; }\n";

Expand Down Expand Up @@ -2142,12 +2143,20 @@ void NotebookWindow::on_edit_copy(const Glib::VariantBase&)
on_outbox_copy(clipboard, selected_cell);
}
if(current_cell!=doc.end()) {
std::cerr << "copy called for non-outbox cell" << std::endl;
// FIXME: handle other cell types.
}
}

void NotebookWindow::on_edit_paste()
{
if(current_cell!=doc.end()) {
auto vis = canvasses[current_canvas]->visualcells.find(&(*current_cell));
if(vis!=canvasses[current_canvas]->visualcells.end()) {
CodeInput *inbox = (*vis).second.inbox;
inbox->edit.get_buffer()->insert_at_cursor(clipboard_cdb);
}
}
}

void NotebookWindow::on_edit_insert_above()
Expand Down Expand Up @@ -2967,6 +2976,7 @@ void NotebookWindow::unselect_output_cell()
if(canvasses[i]->visualcells.find(&(*selected_cell))!=canvasses[i]->visualcells.end()) {
auto& outbox = canvasses[i]->visualcells[&(*selected_cell)].outbox;
outbox->image.set_state_flags(Gtk::STATE_FLAG_NORMAL);
outbox->queue_draw();
}
}
selected_cell=doc.end();
Expand All @@ -2975,18 +2985,18 @@ void NotebookWindow::unselect_output_cell()

bool NotebookWindow::handle_outbox_select(GdkEventButton *, DTree::iterator it)
{
std::cerr << "handle_outbox_select " << it->textbuf << std::endl;
// std::cerr << "handle_outbox_select " << it->textbuf << std::endl;
unselect_output_cell();

// Colour the background of the selected cell, in all canvasses.
for(int i=0; i<(int)canvasses.size(); ++i) {
if(canvasses[i]->visualcells.find(&(*it))!=canvasses[i]->visualcells.end()) {
auto& outbox = canvasses[i]->visualcells[&(*it)].outbox;
outbox->image.set_state_flags(Gtk::STATE_FLAG_SELECTED);
// std::cerr << "selecting" << std::endl;
// if(i==current_canvas)
// outbox->grab_focus();
// FIXME: need to remove focus from any CodeInput widget; the above does not do that.
outbox->queue_draw();
if(i==current_canvas) {
outbox->grab_focus();
}
}
}
selected_cell=it;
Expand Down
28 changes: 20 additions & 8 deletions frontend/gtkmm/TeXView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ TeXView::TeXView(TeXEngine& eng, DTree::iterator it, bool use_microtex_, int hma
add(image);

add_events( Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK );
set_can_focus(true);
}

TeXView::~TeXView()
Expand Down Expand Up @@ -237,25 +238,34 @@ void TeXView::TeXArea::layout_latex() const
0xff424242);
}

guint32 color_from_rgba(Gdk::RGBA color)
{
return
(guint8)(color.get_alpha()*255)<<24|
(guint8)(color.get_red()*255)<<16|
(guint8)(color.get_green()*255)<<8|
(guint8)(color.get_blue()*255);
}

bool TeXView::TeXArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
if(use_microtex) {
// std::cerr << "*** blitting at size " << get_width() << " x " << get_height() << std::endl;

cr->set_source_rgb(1, 1, 1);
cr->rectangle(0, 0, get_width(), get_height());
auto style_context = get_style_context();
style_context->render_background(cr, 0, 0, get_width(), get_height());
Gdk::RGBA fg_colour;
style_context->lookup_color("theme_text_color", fg_colour);

auto surface = cr->get_target();
auto csurface = surface->cobj();
double device_scale_x, device_scale_y;
cairo_surface_get_device_scale(csurface, &device_scale_x, &device_scale_y);
// double device_scale_x, device_scale_y;
// cairo_surface_get_device_scale(csurface, &device_scale_x, &device_scale_y);
// std::cerr << "scale = " << device_scale_x << ", height = "<< _render->getHeight() << std::endl;
cr->scale(1.0, 1.0); // /device_scale_x, 1.0/device_scale_y);

// Don't fill, that will mess up the cell background colour.
cr->fill();
if (_render == nullptr) return true;

microtex::Graphics2D_cairo g2(cr->cobj());
_render->setForeground(color_from_rgba(fg_colour));
_render->draw(g2, padding_x, padding_y);

cr->scale(1.0, 1.0);
Expand Down Expand Up @@ -420,6 +430,8 @@ TeXView::TeXArea::TeXArea(bool use_microtex_)
, padding_x(15), padding_y(10)
{
set_hexpand(true);
set_name("TeXArea");
set_focus_on_click(true);
}

TeXView::TeXArea::~TeXArea()
Expand Down

0 comments on commit e247c13

Please sign in to comment.