/**
* erwin - really simple html editor
* Copyright (C) 2003 Adrian Reber
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: erwinhighlighting.c,v 1.6 2004/07/03 20:14:25 erwin Exp $
*
* $Author: erwin $
*
* Description: syntax higlighting related stuff
*
* $Log: erwinhighlighting.c,v $
* Revision 1.6 2004/07/03 20:14:25 erwin
* commit
*
* Revision 1.5 2004/06/21 21:45:19 erwin
* big commit
* it has been a long time without a checkout
* we are close to version 0.7
*
* Revision 1.4 2004/03/24 14:59:38 erwin
* copy external files to prefix/share/erwin
*
* Revision 1.3 2004/03/11 21:05:01 erwin
* too many changes without commits
*
* Revision 1.2 2003/10/30 20:54:06 erwin
* syntax highlighting implementation
*
* Revision 1.1 2003/10/24 13:32:07 erwin
* syntax higlighting
*
*/
#ifndef lint
static const char vcid[] = "$Id: erwinhighlighting.c,v 1.6 2004/07/03 20:14:25 erwin Exp $";
#endif /* lint */
#include
#include
#include
#include
#include
#include
#include
#include
#include
void toggle_syntax_highlighting(gpointer callback_data, guint callback_action, GtkWidget * widget)
{
struct document *document;
GtkTextBuffer *buffer;
document = (g_list_nth(documents, gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook_main))))->data;
if (document->is_rendered) {
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), TRUE);
return;
}
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(document->text_view));
if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
g_object_set(G_OBJECT(buffer), "highlight", FALSE, NULL);
else
g_object_set(G_OBJECT(buffer), "highlight", TRUE, NULL);
g_object_get(G_OBJECT(buffer), "highlight", &(document->syntax_highlighting), NULL);
}
void change_highlighting_mode(GtkMenuItem * menuitem, gpointer user_data)
{
struct document *document;
GtkTextBuffer *buffer;
int i = 0;
gpointer blub;
document = (g_list_nth(documents, gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook_main))))->data;
if (document->is_rendered) {
while ((blub = g_slist_nth_data(highlighting_group, i++)))
GTK_CHECK_MENU_ITEM(blub)->active = FALSE;
return;
}
document->syntax_highlighting_menuitem = GTK_WIDGET(menuitem);
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(document->text_view));
if (user_data) {
document->language = GTK_SOURCE_LANGUAGE(user_data);
gtk_source_buffer_set_language(GTK_SOURCE_BUFFER(buffer), document->language);
add_to_statusbar(gtk_source_language_get_name(document->language), TRUE);
}
}
void populate_highlighting_menu(GtkWidget * menubar)
{
GtkItemFactory *factory;
GtkWidget *highlighting_menu;
GtkWidget *menuitem;
GSList *languages = NULL;
//GSList *group = NULL;
struct document *document;
gpointer blub;
int i = 0;
if (!menubar)
return;
document = (g_list_nth(documents, gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook_main))))->data;
factory = gtk_item_factory_from_widget(menubar);
highlighting_menu = gtk_item_factory_get_widget(factory, "/Options/Syntax Highlighting");
menuitem = gtk_radio_menu_item_new_with_label(highlighting_group, "by Extension");
gtk_menu_shell_append(GTK_MENU_SHELL(highlighting_menu), menuitem);
highlighting_group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(menuitem));
g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(change_highlighting_mode), NULL);
gtk_widget_show(menuitem);
languages = (GSList *)
gtk_source_languages_manager_get_available_languages(document->manager);
while ((blub = g_slist_nth_data(languages, i++))) {
menuitem =
gtk_radio_menu_item_new_with_label(highlighting_group,
gtk_source_language_get_name(blub));
highlighting_group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(menuitem));
gtk_menu_shell_append(GTK_MENU_SHELL(highlighting_menu), menuitem);
g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(change_highlighting_mode), blub);
gtk_widget_show(menuitem);
}
}
char *get_mime_type(const char *filename)
{
char *extension = NULL;
xmlNodePtr cur;
xmlDocPtr mime_type_table = NULL;
char *mime_type = NULL;
_DEBUG_ fprintf(stderr, "%s:extension: %s\n", PACKAGE, extension);
_DEBUG_ fprintf(stderr, "%s:filename: %s\n", PACKAGE, filename);
extension = strrchr(filename, 46);
_DEBUG_ fprintf(stderr, "%s:extension: %s\n", PACKAGE, extension);
if (!extension++)
return mime_type;
_DEBUG_ fprintf(stderr, "%s:extension: %s\n", PACKAGE, extension);
mime_type_table = xmlParseFile(MIME_FILE);
cur = xmlDocGetRootElement(mime_type_table);
if (!cur)
return mime_type;
_DEBUG_ fprintf(stderr, "%s:2top level node: %s\n", PACKAGE, cur->name);
if (!xmlStrcmp(cur->name, (const xmlChar *) "erwin")) {
cur = cur->xmlChildrenNode;
while (cur != NULL) {
_DEBUG_ fprintf(stderr, "%s:name:%s\n", PACKAGE, cur->name);
_DEBUG_ fprintf(stderr, "%s:get_string:%s\n", PACKAGE,
xmlNodeListGetString(mime_type_table, cur->xmlChildrenNode, 1));
if (!xmlStrcmp(cur->name, (const xmlChar *) "mime-type")) {
if (!strcmp(extension, (const char *) xmlGetProp(cur,
(const xmlChar *) "ext"))) {
mime_type =
strdup((const char *) xmlGetProp(cur, (const xmlChar *) "type"));
break;
}
}
cur = cur->next;
}
}
xmlFreeDoc(mime_type_table);
return mime_type;
}
void activate_highlighting_from_filename(struct document *document)
{
GtkTextBuffer *buffer;
char *mime_type = NULL;
if (document->is_rendered)
return;
_DEBUG_ fprintf(stderr, "%s:filename: %s\n", PACKAGE, document->filename->str);
mime_type = get_mime_type(document->filename->str);
if (!mime_type)
return;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(document->text_view));
document->language =
gtk_source_languages_manager_get_language_from_mime_type(document->manager, mime_type);
gtk_source_buffer_set_language(GTK_SOURCE_BUFFER(buffer), document->language);
add_to_statusbar(gtk_source_language_get_name(document->language), TRUE);
free(mime_type);
}