Made webkit renderer more dynamic

newfile
Markus Koch 2017-02-16 21:00:17 +01:00
parent 39fe7891de
commit b8e7169608
4 changed files with 86 additions and 58 deletions

View File

@ -361,7 +361,7 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkScrolledWindow">
<object class="GtkScrolledWindow" id="entryTextScrollArea">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>

View File

@ -13,7 +13,7 @@ int main(int argc, char *argv[])
gtk_init(&argc, &argv);
mainWindow = mainWindow_new(TRUE);
mainWindow = mainWindow_new();
settings_path = g_strdup_printf("%s/.mdiary.conf", g_get_home_dir());
settings = mdiary_settings_new(mainWindow);

View File

@ -424,27 +424,38 @@ static void mainWindow_init_to_default(struct mainWindow *mainWindow)
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mainWindow->checkSearchText), 0);
}
static void mainWindow_webkit_init(struct mainWindow *mainWindow)
/**
* @brief mainWindow_webkit_set_enabled sets the active text renderer.
* @param mainWindow the mainWindow
* @param state
*/
void mainWindow_webkit_set_enabled(struct mainWindow *mainWindow, gboolean state)
{
GtkWidget *scrollArea;
if (state) {
if (!mainWindow->webkitView) {
mainWindow->webkitView = webkit_web_view_new();
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView), "", "");
gtk_widget_show(mainWindow->webkitView);
mainWindow->webkitView = webkit_web_view_new();
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView), "", "");
gtk_widget_show(mainWindow->webkitView);
scrollArea = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scrollArea), mainWindow->webkitView);
gtk_stack_add_titled(GTK_STACK(mainWindow->stackRenderMode), scrollArea, "webkit", "webkit");
gtk_widget_show(scrollArea);
gtk_stack_set_visible_child(GTK_STACK(mainWindow->stackRenderMode), scrollArea);
mainWindow->webkitScrollArea = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(mainWindow->webkitScrollArea), mainWindow->webkitView);
gtk_stack_add_titled(GTK_STACK(mainWindow->stackRenderMode), mainWindow->webkitScrollArea,
"webkit", "webkit");
gtk_widget_show(mainWindow->webkitScrollArea);
}
gtk_stack_set_visible_child(GTK_STACK(mainWindow->stackRenderMode),
mainWindow->webkitScrollArea);
} else {
gtk_stack_set_visible_child(GTK_STACK(mainWindow->stackRenderMode),
mainWindow->entryTextScrollArea);
}
}
/**
* @brief mainWindow_new creates a new mainWindow.
* @return Returns struct mainWindow *
*/
struct mainWindow *mainWindow_new(gboolean webkit_mode)
struct mainWindow *mainWindow_new()
{
GtkBuilder *builder;
struct mainWindow *mainWindow;
@ -458,6 +469,7 @@ struct mainWindow *mainWindow_new(gboolean webkit_mode)
mainWindow->dateStart = GTK_WIDGET(gtk_builder_get_object(builder, "dateStart"));
mainWindow->dateEnd = GTK_WIDGET(gtk_builder_get_object(builder, "dateEnd"));
mainWindow->entryText = GTK_WIDGET(gtk_builder_get_object(builder, "entryText"));
mainWindow->entryTextScrollArea = GTK_WIDGET(gtk_builder_get_object(builder, "entryTextScrollArea"));
mainWindow->popoverDate = GTK_WIDGET(gtk_builder_get_object(builder, "popoverDate"));
mainWindow->calendarRange = GTK_WIDGET(gtk_builder_get_object(builder, "calendarRange"));
mainWindow->checkSearchTitle = GTK_WIDGET(gtk_builder_get_object(builder, "checkSearchTitle"));
@ -493,10 +505,8 @@ struct mainWindow *mainWindow_new(gboolean webkit_mode)
g_object_unref(G_OBJECT(builder));
mainWindow_configure_treeView(mainWindow);
if (webkit_mode)
mainWindow_webkit_init(mainWindow);
else
mainWindow->webkitView = NULL;
mainWindow->webkitView = NULL;
mainWindow->webkitScrollArea = NULL;
mainWindow_connect_signals(mainWindow);
mainWindow_configure_workspaceTreeView(mainWindow);
mainWindow_init_to_default(mainWindow);
@ -504,6 +514,8 @@ struct mainWindow *mainWindow_new(gboolean webkit_mode)
mainWindow_configure_entry_completion(mainWindow);
mainWindow_set_meta_information(mainWindow, 0, 0, TRUE);
mainWindow_webkit_set_enabled(mainWindow, TRUE);
return mainWindow;
}
@ -580,6 +592,54 @@ void mainWindow_calendarSelected(GtkWidget *widget, gpointer user_data)
g_free(text);
}
static void mainWindow_set_text(struct mainWindow *mainWindow, gchar *text)
{
gchar *tempfile_path = "/tmp/mdiary.tmp.md";
gchar *cmd = NULL;
gchar *cmd_stdout = NULL;
gchar *cmd_stderr = NULL;
gint exit_status = 0;
GFileOutputStream *stream;
GFile *file;
if (mainWindow->webkitView &&
gtk_stack_get_visible_child(GTK_STACK(mainWindow->stackRenderMode)) == mainWindow->webkitScrollArea) {
if (text[0] != '\0') {
file = g_file_new_for_path(tempfile_path);
stream = g_file_replace(
file, NULL, FALSE,
G_FILE_CREATE_NONE,
NULL, NULL);
g_output_stream_write(G_OUTPUT_STREAM(stream), text, strlen(text), NULL, NULL);
g_output_stream_flush(G_OUTPUT_STREAM(stream), NULL, NULL);
cmd = g_strdup_printf("markdown %s", tempfile_path);
if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) &&
exit_status == 0) {
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
cmd_stdout, "");
} else {
g_warning("Could not render markdown for file. Showing plain text instead.\n");
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
text, "");
}
} else {
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
text, "");
}
g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL);
g_file_delete(file, NULL, NULL);
g_object_unref(stream);
g_object_unref(file);
g_free(cmd);
g_free(cmd_stdout);
g_free(cmd_stderr);
} else {
gtk_text_buffer_set_text(mainWindow->textBuffer, text, strlen(text));
}
}
/**
* @brief mainWindow_entrySelected is the callback when the user selects a diary entry.
* @param widget N/A
@ -599,43 +659,7 @@ void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data)
selection = gtk_tree_view_get_selection(mainWindow->entryListView);
if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
gtk_tree_model_get(model, &iter, COL_TEXT, &text, -1);
if (mainWindow->webkitView) {
gchar *tempfile_path = "/tmp/mdiary.tmp.md";
gchar *cmd = NULL;
gchar *cmd_stdout = NULL;
gchar *cmd_stderr = NULL;
gint exit_status = 0;
GFileOutputStream *stream;
GFile *file;
file = g_file_new_for_path(tempfile_path);
stream = g_file_replace(
file, NULL, FALSE,
G_FILE_CREATE_NONE,
NULL, NULL);
g_output_stream_write(G_OUTPUT_STREAM(stream), text, strlen(text), NULL, NULL);
g_output_stream_flush(G_OUTPUT_STREAM(stream), NULL, NULL);
cmd = g_strdup_printf("markdown %s", tempfile_path);
if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) &&
exit_status == 0) {
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
cmd_stdout, "");
} else {
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
text, "");
}
g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL);
g_file_delete(file, NULL, NULL);
g_object_unref(stream);
g_object_unref(file);
g_free(cmd);
g_free(cmd_stdout);
g_free(cmd_stderr);
} else {
gtk_text_buffer_set_text(mainWindow->textBuffer, text, strlen(text));
}
mainWindow_set_text(mainWindow, text);
g_free(text);
gtk_tree_model_get(model, &iter, COL_SUMMARY, &summary, -1);
@ -646,7 +670,8 @@ void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data)
g_free(summary);
g_free(temp);
} else {
gtk_text_buffer_set_text(mainWindow->textBuffer, "", 0);
//gtk_text_buffer_set_text(mainWindow->textBuffer, "", 0);
mainWindow_set_text(mainWindow, "");
gtk_label_set_text(GTK_LABEL(mainWindow->labelSummary), "Please select an entry in the list above.");
}
}

View File

@ -22,6 +22,7 @@ struct mainWindow {
GtkWidget *dateStart;
GtkWidget *dateEnd;
GtkWidget *entryText;
GtkWidget *entryTextScrollArea;
GtkWidget *popoverDate;
GtkWidget *calendarRange;
GtkWidget *checkSearchTitle;
@ -34,6 +35,7 @@ struct mainWindow {
GtkWidget *labelDateToDate;
GtkWidget *buttonDecrypt;
GtkWidget *webkitView;
GtkWidget *webkitScrollArea;
GtkWidget *stackRenderMode;
GtkTreeView *entryListView;
GtkTreeSelection *entryListSelection;
@ -67,7 +69,7 @@ struct mainWindow {
GtkTreeModelFilter *workspaceListFiltered;
};
struct mainWindow *mainWindow_new(gboolean webkit_mode);
struct mainWindow *mainWindow_new();
void mainWindow_set_meta_information(struct mainWindow *mainWindow,
guint time_earliest,
guint time_latest,
@ -88,5 +90,6 @@ void mainWindow_dateIconPress(GtkWidget *widget, gint icon_pos, gint event, gpoi
void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path, gboolean append);
void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *path, gboolean gpg_enabled);
void mainWindow_decrypt_gpg_clicked(GtkWidget *widget, gpointer user_data);
void mainWindow_webkit_set_enabled(struct mainWindow *mainWindow, gboolean state);
#endif /* MAINWINDOW_H */