/* sourceview.cc
 *
 * Copyright (C) 2004-2005 Jae Jang
 * Copyright (C) 2005-2006 Rob Page
 * Copyright (C) 2006 Dodji Seketeli
 * Copyright (C) 2009, 2010 Krzesimir Nowak
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <gtksourceviewmm/sourceview.h>
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourceview-typebuiltins.h>

static gchar* SignalProxy_MarkTooltip_gtk_callback(GtkSourceMark* mark, gpointer user_data)
{
  gtksourceview::SourceView::SlotMarkTooltip* the_slot = static_cast<gtksourceview::SourceView::SlotMarkTooltip*>(user_data);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return g_strdup((*the_slot)(Glib::wrap(mark)).c_str());
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }

  return 0; //An arbitary default, just to avoid the compiler warning.
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}

static void SignalProxy_MarkTooltip_gtk_callback_destroy(void* data)
{
  delete static_cast<gtksourceview::SourceView::SlotMarkTooltip*>(data);
}

namespace gtksourceview
{

SourceView::SourceView() :
    _CONSTRUCT
{
    GtkSourceBuffer *buffer = gtk_source_buffer_new (0) ;
    gtk_text_view_set_buffer (GTK_TEXT_VIEW (gobject_), GTK_TEXT_BUFFER (buffer));
    g_object_unref(buffer);
}

SourceView::SourceView(const Glib::RefPtr<SourceBuffer> &a_buffer) :
    _CONSTRUCT
{
    if (a_buffer) {
        set_buffer (a_buffer) ;
    } else {
        GtkSourceBuffer *buffer = gtk_source_buffer_new (0) ;
        gtk_text_view_set_buffer (GTK_TEXT_VIEW (gobject_), GTK_TEXT_BUFFER (buffer)) ;
        g_object_unref(buffer);
    }
}

Glib::RefPtr<const SourceBuffer>
SourceView::get_source_buffer () const
{
    Glib::RefPtr<SourceBuffer> buffer ;
    buffer = Glib::wrap (GTK_SOURCE_BUFFER (get_buffer ()->gobj ()), true);
    return buffer ;
}

Glib::RefPtr<SourceBuffer>
SourceView::get_source_buffer ()
{
    Glib::RefPtr<SourceBuffer> buffer ;
    buffer = Glib::wrap (GTK_SOURCE_BUFFER (get_buffer ()->gobj ()), true) ;
    return buffer ;

}

void
SourceView::set_source_buffer (const Glib::RefPtr<SourceBuffer> &source_buffer)
{
    set_buffer (source_buffer) ;
}

#ifndef GTKSOURCEVIEWMM_DISABLE_DEPRECATED
void
SourceView::set_mark_category_pixbuf(const Glib::ustring& category, const Glib::RefPtr<Gdk::Pixbuf>& pixbuf)
{
  gtk_source_view_set_mark_category_icon_from_pixbuf(gobj(), category.c_str(), Glib::unwrap(pixbuf));
}

Glib::RefPtr<Gdk::Pixbuf>
SourceView::get_mark_category_pixbuf(const Glib::ustring&)
{
  return Glib::RefPtr<Gdk::Pixbuf>();
}

Glib::RefPtr<const Gdk::Pixbuf>
SourceView::get_mark_category_pixbuf(const Glib::ustring&) const
{
  return Glib::RefPtr<const Gdk::Pixbuf>();
}
#endif

void
SourceView::unset_mark_category_icon(const Glib::ustring& category)
{
  gtk_source_view_set_mark_category_icon_from_icon_name(gobj(), category.c_str(), 0);
  gtk_source_view_set_mark_category_icon_from_stock(gobj(), category.c_str(), 0);
  gtk_source_view_set_mark_category_icon_from_pixbuf(gobj(), category.c_str(), 0);
}

void
SourceView::unset_mark_category_background(const Glib::ustring& category)
{
  gtk_source_view_set_mark_category_background(gobj(), category.c_str(), 0);
}

void
SourceView::set_mark_category_tooltip_func(const Glib::ustring& category, const SlotMarkTooltip& slot)
{
  SlotMarkTooltip* slot_copy = new SlotMarkTooltip(slot);

  gtk_source_view_set_mark_category_tooltip_func(gobj(), category.c_str(),
    &SignalProxy_MarkTooltip_gtk_callback, slot_copy,
    &SignalProxy_MarkTooltip_gtk_callback_destroy);
}

void
SourceView::set_mark_category_tooltip_markup_func(const Glib::ustring& category, const SlotMarkTooltip& slot)
{
  SlotMarkTooltip* slot_copy = new SlotMarkTooltip(slot);

  gtk_source_view_set_mark_category_tooltip_markup_func(gobj(),
    category.c_str(), &SignalProxy_MarkTooltip_gtk_callback, slot_copy,
    &SignalProxy_MarkTooltip_gtk_callback_destroy);
}

void
SourceView::unset_mark_category_tooltip_func(const Glib::ustring& category)
{
  gtk_source_view_set_mark_category_tooltip_func(gobj(), category.c_str(), 0, 0, 0);
}

}//end namespace gtksourceview

