Cadabra
Computer algebra system for field theory problems
Loading...
Searching...
No Matches
TeXView.hh
Go to the documentation of this file.
1
2#pragma once
3
4#include "GtkCompat.hh"
5
6#ifndef USE_GTK4
7#include <gtkmm/eventbox.h>
8#endif
9#include <gtkmm/box.h>
10#include <gtkmm/drawingarea.h>
11#if GTKMM_MAJOR_VERSION>3 || (GTKMM_MAJOR_VERSION==3 && GTKMM_MINOR_VERSION>=10)
12#include <gtkmm/revealer.h>
13#endif
14
15#include "Config.hh"
16#include "DataCell.hh"
17#include "../common/TeXEngine.hh"
18
19// MicroTeX
20#include "microtex.h"
21#include "utils/utf.h"
22
23namespace cadabra {
24
28 //
31
34#ifdef USE_GTK4
35 using TeXViewBase = Gtk::Box;
36#else
37 using TeXViewBase = Gtk::EventBox;
38#endif
39
40 class TeXView : public TeXViewBase {
41 public:
42 TeXView(TeXEngine&, DTree::iterator, int hmargin=25);
43 virtual ~TeXView();
44
45 std::shared_ptr<TeXEngine::TeXRequest> content;
46
47 sigc::signal<bool(DTree::iterator)> show_hide_requested;
48
53 sigc::signal<void()> select_requested;
54
55 void set_is_error(bool);
56
57 DTree::iterator datacell;
58 Gtk::Revealer rbox;
59 Gtk::Box vbox;
60 Gtk::Box hbox;
61
63
64 class TeXArea : public Gtk::DrawingArea {
65 public:
66 TeXArea(TeXView *owner_);
67 ~TeXArea();
68
72 bool draw_impl(const Cairo::RefPtr<Cairo::Context>& cr);
73#ifdef USE_GTK4
74 void draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height);
75#else
76 virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
77#endif
78
81
82 void update_image(std::shared_ptr<TeXEngine::TeXRequest>);
83
85
89 void set_latex(const std::string& latex);
90
93 void layout_latex() const;
94
95 int need_height(int width) const;
96 protected:
97 Gtk::SizeRequestMode get_request_mode_vfunc() const override;
98
100 void measure_height_for_width(int width, int& minimum_height,
101 int& natural_height) const;
102 void relayout_for_allocation(int width, int height);
103
104#ifdef USE_GTK4
105 void measure_vfunc(Gtk::Orientation orientation, int for_size,
106 int& minimum, int& natural,
107 int& minimum_baseline, int& natural_baseline) const override;
108 void size_allocate_vfunc(int width, int height, int baseline) override;
109#else
111 int& natural_height) const override;
113 int& natural_width) const override;
114 void on_size_allocate(Gtk::Allocation& allocation) override;
115#endif
116
117 private:
119 mutable int rendering_width;
121
122 // MicroTeX
123 mutable microtex::Render* _render;
124 std::string unfixed, fixed;
125
126 // What _render was built from, so that layout_latex() can
127 // skip a re-parse when nothing has changed. These are
128 // exactly the arguments MicroTeX::parse() varies over, so
129 // they capture the whole of its input.
130 mutable std::string _laid_out_text;
131 mutable int _laid_out_width = -1;
132 mutable float _laid_out_text_size = -1.f;
133
134 // Return a string in which backslashes, curly braces,
135 // underscores and tildes are properly backslashed, so they
136 // typeset as if we are in a verbatim environment.
137 std::string verbatimise(const std::string&) const;
138 };
139
141
143 void update_image();
144
147
148 void dim(bool);
149
150 sigc::signal<bool(std::string)> tex_error;
152
153 protected:
154#ifdef USE_GTK4
155 // See the comment on the definition: GTK4 never emits ::show for
156 // these cells, so the initial conversion hangs off ::map instead.
157 void on_map() override;
158#else
159 virtual void on_show() override;
160#endif
161
164 void on_button_pressed(int n_press, double x, double y);
165 void on_button_released(int n_press, double x, double y);
166
167 void convert();
168
169 void on_copy_as_latex() const;
170
171 private:
174
176
177 Glib::RefPtr<cdb::ClickGesture> click_gesture;
178
179 float text_size() const;
180 };
181
182 }
183
Compatibility helpers that let the gtkmm frontend build against both gtkmm-3 and gtkmm-4 from a singl...
TeXEngine is used to convert LaTeX strings into PNG images.
Definition TeXEngine.hh:23
Definition TeXView.hh:64
std::string fixed
Definition TeXView.hh:124
std::string unfixed
Definition TeXView.hh:124
int _laid_out_width
Definition TeXView.hh:131
void relayout_for_allocation(int width, int height)
The re-layout that follows a new allocation, shared between gtkmm-3's on_size_allocate and GTK4's siz...
Definition TeXView.cc:171
void get_preferred_width_for_height_vfunc(int height, int &minimum_width, int &natural_width) const override
Definition TeXView.cc:266
TeXView * owner
Definition TeXView.hh:118
void set_latex(const std::string &latex)
MicroTeX: set the LaTeX string and replace/substitute so that MicroTeX can render it (but do not actu...
Definition TeXView.cc:464
int padding_x
Definition TeXView.hh:120
void layout_latex() const
MicroTeX: just run the LaTeX layout algorithms, do not draw.
Definition TeXView.cc:360
std::string _laid_out_text
Definition TeXView.hh:130
virtual bool on_draw(const Cairo::RefPtr< Cairo::Context > &cr) override
Definition TeXView.cc:421
float _laid_out_text_size
Definition TeXView.hh:132
std::string verbatimise(const std::string &) const
Definition TeXView.cc:453
microtex::Render * _render
Definition TeXView.hh:123
void measure_height_for_width(int width, int &minimum_height, int &natural_height) const
The MicroTeX size logic, shared between the two toolkits.
Definition TeXView.cc:131
float _text_size
Definition TeXView.hh:84
int need_height(int width) const
Definition TeXView.cc:201
int padding_y
Definition TeXView.hh:120
bool draw_impl(const Cairo::RefPtr< Cairo::Context > &cr)
The actual painting, shared between the two toolkits.
Definition TeXView.cc:432
int rendering_width
Definition TeXView.hh:119
Gtk::SizeRequestMode get_request_mode_vfunc() const override
Definition TeXView.cc:123
~TeXArea()
Definition TeXView.cc:634
void on_size_allocate(Gtk::Allocation &allocation) override
Definition TeXView.cc:283
void get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const override
Definition TeXView.cc:260
Definition TeXView.hh:40
TeXEngine & engine
Definition TeXView.hh:172
void dim(bool)
Dim the output to indicate that the result is no longer guaranteed to be correlated with the input ce...
Definition TeXView.cc:313
Gtk::Revealer rbox
Definition TeXView.hh:58
virtual ~TeXView()
Definition TeXView.cc:63
float text_size() const
Definition TeXView.cc:78
TeXArea image
Definition TeXView.hh:140
DTree::iterator datacell
Definition TeXView.hh:57
sigc::signal< bool(std::string)> tex_error
Definition TeXView.hh:150
bool is_error
Definition TeXView.hh:173
void update_image()
Update the TeX image.
Definition TeXView.cc:342
Gtk::Box hbox
Definition TeXView.hh:60
void on_button_pressed(int n_press, double x, double y)
Button handling goes through a gesture controller rather than the on_button_{press,...
Definition TeXView.cc:327
cdb::ContextMenu context_menu
Definition TeXView.hh:175
virtual void on_show() override
Definition TeXView.cc:114
void on_button_released(int n_press, double x, double y)
Definition TeXView.cc:319
sigc::signal< bool(DTree::iterator)> show_hide_requested
Definition TeXView.hh:47
std::shared_ptr< TeXEngine::TeXRequest > content
Definition TeXView.hh:45
sigc::signal< void()> select_requested
Emitted on a button press which is not the context-menu button, so that the owner can select this cel...
Definition TeXView.hh:53
int window_width
Definition TeXView.hh:62
sigc::signal< bool()> signal_on_copy_as_latex
Definition TeXView.hh:151
void convert()
Definition TeXView.cc:299
Glib::RefPtr< cdb::ClickGesture > click_gesture
Definition TeXView.hh:177
Gtk::Box vbox
Definition TeXView.hh:59
void on_copy_as_latex() const
Definition TeXView.cc:68
void set_is_error(bool)
Definition TeXView.cc:73
A right-click context menu.
Definition GtkCompat.hh:439
Gtk::EventBox TeXViewBase
TeXView is a widget which knows how to turn a string into a LaTeX-rendered image and display that.
Definition TeXView.hh:37
Functions to handle the exchange properties of two or more symbols in a product.
Definition Adjform.cc:83
void set(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1180