From 0dc7b6e5e0e31804b1e4d65dcfe512614529270e Mon Sep 17 00:00:00 2001 From: Wilson Brenna Date: Sat, 3 Aug 2013 00:23:19 -0400 Subject: [PATCH] Just need to finish the Control.cpp and PageView.cpp. --- src/Makefile.am | 2 + src/control/Actions.h | 1 + src/control/Control.cpp | 25 +++ src/control/Control.h | 5 + src/gui/dialog/LatexGlade.cpp | 68 +++++++++ src/gui/dialog/LatexGlade.h | 42 +++++ src/gui/toolbarMenubar/ToolMenuHandler.cpp | 2 + src/model/Element.h | 2 +- src/model/TexImage.cpp | 169 +++++++++++++++++++++ src/model/TexImage.h | 63 ++++++++ src/util/LatexAction.cpp | 95 ++++++++++++ src/util/LatexAction.h | 42 +++++ src/util/XournalTypeList.h | 4 +- ui/texdialog.glade | 4 +- 14 files changed, 519 insertions(+), 5 deletions(-) create mode 100644 src/gui/dialog/LatexGlade.cpp create mode 100644 src/gui/dialog/LatexGlade.h create mode 100644 src/model/TexImage.cpp create mode 100644 src/model/TexImage.h create mode 100644 src/util/LatexAction.cpp create mode 100644 src/util/LatexAction.h diff --git a/src/Makefile.am b/src/Makefile.am index 5ae566a1..67c4262c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -74,6 +74,7 @@ xournalpp_SOURCES = \ gui/dialog/PdfPagesDialog.cpp \ gui/dialog/SelectBackgroundColorDialog.cpp \ gui/dialog/SettingsDialog.cpp \ + gui/dialog/LatexGlade.cpp \ gui/pageposition/PagePosition.cpp \ gui/pageposition/PagePositionCache.cpp \ gui/pageposition/PagePositionHandler.cpp \ @@ -136,6 +137,7 @@ xournalpp_SOURCES = \ model/PageRef.cpp \ model/Point.cpp \ model/Stroke.cpp \ + model/TexImage.cpp \ model/Text.cpp \ model/XojPage.cpp \ pdf/cairo/CairoPdf.cpp \ diff --git a/src/control/Actions.h b/src/control/Actions.h index 217779d9..04efce61 100644 --- a/src/control/Actions.h +++ b/src/control/Actions.h @@ -123,6 +123,7 @@ enum ActionType { ACTION_SELECT_FONT, ACTION_FONT_BUTTON_CHANGED, + ACTION_TEX, // Menu View ACTION_ZOOM_IN = 600, diff --git a/src/control/Control.cpp b/src/control/Control.cpp index 2ce898b3..f62e538d 100644 --- a/src/control/Control.cpp +++ b/src/control/Control.cpp @@ -704,6 +704,9 @@ void Control::actionPerformed(ActionType type, ActionGroup group, GdkEvent *even case ACTION_SELECT_COLOR_CUSTOM: // nothing to do here, the color toolbar item handles the color break; + case ACTION_TEX: + runLatex(); + break; // Menu View case ACTION_ZOOM_100: @@ -2665,6 +2668,28 @@ void Control::fontChanged() { } } +//The core handler for inserting latex +void Control::runLatex() { + XOJ_CHECK_TYPE(Control); + + this->doc->lock(); + + int pageNr = getCurrentPageNo(); + if (pageNr == -1) { + return; + } + PageView * veiw = win->getXournal()->getViewFor(pageNr); + if (view == NULL) { + return; + } + //we get the selection + PageRef page = this->doc->getPage(pageNr); + Layer * layer = page.getSelectedLayer(); + +} + + + /** * GETTER / SETTER */ diff --git a/src/control/Control.h b/src/control/Control.h index f75b5f93..f42b438b 100644 --- a/src/control/Control.h +++ b/src/control/Control.h @@ -18,6 +18,8 @@ #include "../undo/UndoRedoHandler.h" #include "ClipboardHandler.h" #include "settings/Settings.h" +#include "../util/LatexAction.h" +#include "../gui/dialog/LatexGlade.h" #include "ToolHandler.h" #include "../model/Document.h" #include "ZoomControl.h" @@ -70,6 +72,9 @@ public: // Menu edit void showSettings(); + // Menu tools + void runLatex(); + // Menu Help void showAbout(); diff --git a/src/gui/dialog/LatexGlade.cpp b/src/gui/dialog/LatexGlade.cpp new file mode 100644 index 00000000..b32baec4 --- /dev/null +++ b/src/gui/dialog/LatexGlade.cpp @@ -0,0 +1,68 @@ +#include "LatexGlade.h" + +#include "../../control/tools/ImageHandler.h" +#include "../../model/TexImage.h" + +#include "../../util/LatexAction.h" + +LatexGlade::LatexGlade(GladeSearchpath * gladeSearchPath) : + GladeGui(gladeSearchPath, "texdialog.glade", "texDialog") { + XOJ_INIT_TYPE(LatexGlade); + + this->theLatex = NULL; + + //GtkWidget * vbox = get("texVBox"); + //g_return_if_fail(vbox != NULL); + this->texBox = get("texEntry"); + gtk_entry_set_max_length(GTK_ENTRY(this->texBox),50); + + gtk_widget_show(this->texBox); +} + +LatexGlade::~LatexGlade() { + XOJ_CHECK_TYPE(LatexGlade); + + XOJ_RELEASE_TYPE(LatexGlade); + +} + +void LatexGlade::setTex(gchar * texString) +{ + this->theLatex = texString; +} +gchar * LatexGlade::getTex() +{ + return this->theLatex; +} + +void LatexGlade::save() { + this->theLatex = g_strdup(gtk_entry_get_text(GTK_ENTRY(this->texBox))); +} + +void LatexGlade::load() { + printf("Latex::load()\n"); + + if (theLatex == NULL) + { + theLatex = "x^2"; + } + gtk_entry_set_text(GTK_ENTRY(this->texBox), this->theLatex); + + +} + +void LatexGlade::show(GtkWindow * parent){ + XOJ_CHECK_TYPE(LatexGlade); + this->load(); + gtk_window_set_transient_for(GTK_WINDOW(this->window),parent); + int res = gtk_dialog_run(GTK_DIALOG(this->window)); + if (res == 1) { + printf("Checkbox OK-d.\n"); + this->save(); + } + else + { + this->theLatex = ""; + } + gtk_widget_hide(this->window); +} diff --git a/src/gui/dialog/LatexGlade.h b/src/gui/dialog/LatexGlade.h new file mode 100644 index 00000000..211d5ccc --- /dev/null +++ b/src/gui/dialog/LatexGlade.h @@ -0,0 +1,42 @@ +/* + * Xournal++ + * + * Latex implementation + * + * @author W Brenna + * http://wbrenna.ca + * + * @license GPL + */ + +#ifndef __LATEXGLADE_H__ +#define __LATEXGLADE_H__ + +#include "../../model/LayerListener.h" +#include +#include +#include "../../model/TexImage.h" +#include +#include "../../gui/GladeGui.h" + + +class LatexGlade : public GladeGui { +public: + LatexGlade(GladeSearchpath * gladeSearchPath); + virtual ~LatexGlade(); + +public: + virtual void show(GtkWindow * parent); + void save(); + void load(); + void setTex(gchar * texString); + gchar * getTex(); + + +private: + XOJ_TYPE_ATTRIB; + GtkWidget * texBox; + gchar * theLatex; +}; + +#endif /* __LATEXGLADE_H__ */ diff --git a/src/gui/toolbarMenubar/ToolMenuHandler.cpp b/src/gui/toolbarMenubar/ToolMenuHandler.cpp index 74d9a747..a75ee197 100644 --- a/src/gui/toolbarMenubar/ToolMenuHandler.cpp +++ b/src/gui/toolbarMenubar/ToolMenuHandler.cpp @@ -503,6 +503,8 @@ void ToolMenuHandler::initToolItems() { registerMenupoint(gui->get("menuToolsTextFont"), ACTION_SELECT_FONT); + registerMenupoint(gui->get("menuEditTex"), ACTION_TEX); + registerMenupoint(gui->get("menuViewToolbarManage"), ACTION_MANAGE_TOOLBAR); registerMenupoint(gui->get("menuViewToolbarCustomize"), ACTION_CUSTOMIZE_TOOLBAR); diff --git a/src/model/Element.h b/src/model/Element.h index 11f3636b..2ecada0f 100644 --- a/src/model/Element.h +++ b/src/model/Element.h @@ -17,7 +17,7 @@ #include enum ElementType { - ELEMENT_STROKE = 1, ELEMENT_IMAGE, ELEMENT_TEXT + ELEMENT_STROKE = 1, ELEMENT_IMAGE, ELEMENT_TEXIMAGE, ELEMENT_TEXT }; class ShapeContainer { diff --git a/src/model/TexImage.cpp b/src/model/TexImage.cpp new file mode 100644 index 00000000..8e2846fe --- /dev/null +++ b/src/model/TexImage.cpp @@ -0,0 +1,169 @@ +#include "TexTexImage.h" +#include +#include +#include + +TexImage::TexImage() : + Element(ELEMENT_TEXIMAGE) { + + XOJ_INIT_TYPE(TexImage); + + this->sizeCalculated = true; + this->image = NULL; + this->data = NULL; + this->dLen = 0; + this->read = false; + + //text tags + this->text = NULL; + this->textlen = 0; +} + +TexImage::~TexImage() { + XOJ_CHECK_TYPE(TexImage); + + if (this->image) { + cairo_surface_destroy(this->image); + this->image = NULL; + } + + XOJ_RELEASE_TYPE(TexImage); +} + +void TexImage::setWidth(double width) { + XOJ_CHECK_TYPE(TexImage); + + this->width = width; +} + +void TexImage::setHeight(double height) { + XOJ_CHECK_TYPE(TexImage); + + this->height = height; +} + +cairo_status_t TexImage::cairoReadFunction(TexImage * image, unsigned char * data, unsigned int length) { + XOJ_CHECK_TYPE_OBJ(image, TexImage); + + for (int i = 0; i < length; i++, image->read++) { + if (image->read >= image->dLen) { + return CAIRO_STATUS_READ_ERROR; + } + data[i] = image->data[image->read]; + } + + return CAIRO_STATUS_SUCCESS; +} + +void TexImage::setImage(unsigned char * data, int len) { + XOJ_CHECK_TYPE(TexImage); + + if (this->image) { + cairo_surface_destroy(this->image); + this->image = NULL; + } + if (this->data) { + g_free(this->data); + } + this->data = data; + this->dLen = len; +} + +void TexImage::setImage(GdkPixbuf * img) { + setTexImage(f_pixbuf_to_cairo_surface(img)); +} + +void TexImage::setImage(cairo_surface_t * image) { + XOJ_CHECK_TYPE(TexImage); + + if (this->image) { + cairo_surface_destroy(this->image); + this->image = NULL; + } + if (this->data) { + g_free(this->data); + } + this->data = NULL; + this->dLen = 0; + + this->image = image; +} + +void TexImage::setText(unsigned char * text, int textlen) +{ + this->text = text; + this->textlen = textlen; +} + +unsigned char * TexImage::getText() +{ + return this->text; +} + +cairo_surface_t * TexImage::getImage() { + XOJ_CHECK_TYPE(TexImage); + + if (this->image == NULL && this->dLen != 0) { + this->read = 0; + this->image = cairo_image_surface_create_from_png_stream((cairo_read_func_t) &cairoReadFunction, this); + g_free(this->data); + this->data = NULL; + this->dLen = 0; + } + + return this->image; +} + +void TexImage::scale(double x0, double y0, double fx, double fy) { + XOJ_CHECK_TYPE(TexImage); + + this->x -= x0; + this->x *= fx; + this->x += x0; + this->y -= y0; + this->y *= fy; + this->y += y0; + + this->width *= fx; + this->height *= fy; +} + +void TexImage::serialize(ObjectOutputStream & out) { + XOJ_CHECK_TYPE(TexImage); + + out.writeObject("TexImage"); + + serializeElement(out); + + out.writeDouble(this->width); + out.writeDouble(this->height); + + out.writeTexImage(this->image); + + out.endObject(); +} + +void TexImage::readSerialized(ObjectInputStream & in) throw (InputStreamException) { + XOJ_CHECK_TYPE(TexImage); + + in.readObject("TexImage"); + + readSerializedElement(in); + + this->width = in.readDouble(); + this->height = in.readDouble(); + + if (this->image) { + cairo_surface_destroy(this->image); + this->image = NULL; + } + + this->image = in.readTexImage(); + + in.endObject(); +} + +void TexImage::calcSize() { + XOJ_CHECK_TYPE(TexImage); +} + diff --git a/src/model/TexImage.h b/src/model/TexImage.h new file mode 100644 index 00000000..721bb065 --- /dev/null +++ b/src/model/TexImage.h @@ -0,0 +1,63 @@ +/* + * Xournal++ + * + * An Image on the document + * + * @author Xournal Team + * http://xournal.sf.net + * + * @license GPL + */ + +#ifndef __IMAGE_H__ +#define __IMAGE_H__ + +#include "Element.h" +#include + +class TexImage: public Element { +public: + TexImage(); + virtual ~TexImage(); + +public: + void setWidth(double width); + void setHeight(double height); + + void setImage(unsigned char * data, int len); + void setImage(cairo_surface_t * image); + void setImage(GdkPixbuf * img); + cairo_surface_t * getImage(); + + virtual void scale(double x0, double y0, double fx, double fy); + + //text tag to alow latex + void setText(unsigned char * text, int textlen); + //returns length + unsigned char * getText(); + +public: + // Serialize interface + void serialize(ObjectOutputStream & out); + void readSerialized(ObjectInputStream & in) throw (InputStreamException); + +private: + virtual void calcSize(); + + static cairo_status_t cairoReadFunction(Image * image, unsigned char *data, unsigned int length); +private: + XOJ_TYPE_ATTRIB; + + + cairo_surface_t * image; + + unsigned char * data; + int dLen; + + int read; + //text + unsigned char * text; + int textlen; +}; + +#endif /* __IMAGE_H__ */ diff --git a/src/util/LatexAction.cpp b/src/util/LatexAction.cpp new file mode 100644 index 00000000..a58d7de0 --- /dev/null +++ b/src/util/LatexAction.cpp @@ -0,0 +1,95 @@ +#include "LatexAction.h" +#include "../control/Control.h" +#include "../model/Stroke.h" +#include "../model/Layer.h" +#include "../gui/PageView.h" +#include "../gui/XournalView.h" + +//some time - clean up these includes +#include +#include + + +#include "../control/tools/ImageHandler.h" +#include "../model/TexImage.h" +#include + +#include "../cfg.h" +#include + +LatexAction::LatexAction(gchar * myTex) { + //this->control = control; + + this->theLatex = myTex; + + this->texfile = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, CONFIG_DIR, G_DIR_SEPARATOR_S, "tex", NULL); + this->texfilefull = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, CONFIG_DIR, G_DIR_SEPARATOR_S, "tex.png", NULL); + printf("%s \n",this->texfile); + + //set up the default positions. + this->myx = 0; + this->myy = 0; +} + +LatexAction::~LatexAction() { + g_free(this->texfile); + g_free(this->texfilefull); +} + +void LatexAction::runCommand(){ + /* + * at some point, I may need to sanitize theLatex + */ + printf("Command is being run.\n"); + const gchar* mtex = "mathtex"; + gchar* mathtex = g_find_program_in_path(mtex); + if (!mathtex) + { + printf("Error: problem finding mathtex. Doing nothing...\n"); + return; + } + printf("Found mathtex in your path!\n"); + g_free(mathtex); + gchar* command = NULL; + //can change font colour later with more features + const gchar * fontcolour = "black"; + command = g_strdup_printf( + "%s -m 0 \"\\png\\usepackage{color}\\color{%s}\\%s %s\" -o %s", + mtex, strlen(fontcolour) ? fontcolour : "black", + "normalsize", + this->theLatex, this->texfile); + + gint rt = 0; + void(*texhandler)(int) = signal(SIGCHLD,SIG_DFL); + gboolean success = g_spawn_command_line_sync(command,NULL,NULL,&rt,NULL); + signal(SIGCHLD,texhandler); + if (!success) + { + printf("Latex Command execution failed.\n"); + return; + } + g_free(command); + printf("Tex command: \"%s\" was successful; in file %s.\n",this->theLatex,this->texfilefull); + +} + +void LatexAction::mathtexAddImage(Control * control, double x, double y) { + printf("Adding element\n"); +} + +void LatexAction::mathtexModImage(TexImage * img, Layer * layer) +{ + //get the image we're looking at and take care of all that + + //get theLatex and query for modifications + //this->texlen = img->getText(this->theLatex); + + this->myx = img->getX(); + this->myy = img->getY(); + + //remove image + // + layer->removeElement(img,false); + +} + diff --git a/src/util/LatexAction.h b/src/util/LatexAction.h new file mode 100644 index 00000000..7073300e --- /dev/null +++ b/src/util/LatexAction.h @@ -0,0 +1,42 @@ +/* + * Xournal++ + * + * Latex implementation + * + * @author W Brenna + * http://wbrenna.ca + * + * @license GPL + */ + +#ifndef __LATEXACTION_H__ +#define __LATEXACTION_H__ + +#include "../model/LayerListener.h" +#include +#include +#include "../model/TexImage.h" + + +class Control; + +class LatexAction { +public: + LatexAction(gchar * myTex); + virtual ~LatexAction(); + +public: + void mathtexModImage(TexImage * img, Layer * layer); + void mathtexAddImage(Control * control, double x, double y); + void runCommand(); + + +private: +// Control * control; + gchar * theLatex; + gchar * texfile; + gchar * texfilefull; + double myx, myy; +}; + +#endif /* __LATEXACTION_H__ */ diff --git a/src/util/XournalTypeList.h b/src/util/XournalTypeList.h index eb276654..4faa20d9 100644 --- a/src/util/XournalTypeList.h +++ b/src/util/XournalTypeList.h @@ -194,8 +194,8 @@ XOJ_DECLARE_TYPE(CustomizeableColorList, 182); XOJ_DECLARE_TYPE(GotoDialog, 183); XOJ_DECLARE_TYPE(ToolbarColorNames, 184); XOJ_DECLARE_TYPE(TextBoxUndoAction, 185); -//XOJ_DECLARE_TYPE(XXXXXXXXXXXXXXXX, 186); -//XOJ_DECLARE_TYPE(XXXXXXXXXXXXXXXX, 187); +XOJ_DECLARE_TYPE(LatexGlade, 186); +XOJ_DECLARE_TYPE(TexImage, 187); //XOJ_DECLARE_TYPE(XXXXXXXXXXXXXXXX, 188); //XOJ_DECLARE_TYPE(XXXXXXXXXXXXXXXX, 189); //XOJ_DECLARE_TYPE(XXXXXXXXXXXXXXXX, 19); diff --git a/ui/texdialog.glade b/ui/texdialog.glade index 1e47cbca..7077dbe0 100644 --- a/ui/texdialog.glade +++ b/ui/texdialog.glade @@ -22,7 +22,7 @@ gtk-cancel - -1 + 2 True True False @@ -41,7 +41,7 @@ 1 True True - False + True False True