/* completionitem.cc
 *
 * Copyright (C) 2009, 2010, 2011 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 <gtksourceview/gtksourcecompletionitem.h>
#include <gtkmm/stock.h>
#include <gtk/gtk.h>

namespace Gsv
{

CompletionItem::CompletionItem(const Glib::ustring& label, const Glib::ustring& text, const Glib::RefPtr<const Gdk::Pixbuf>& icon, const Glib::ustring& info) :
  _CONSTRUCT("label", label.c_str(), "text", text.c_str(), "icon", Glib::unwrap(icon), "info", (info.empty() ? 0 : info.c_str()))
{}

CompletionItem::CompletionItem(const Markup& markup, const Glib::ustring& text, const Glib::RefPtr<const Gdk::Pixbuf>& icon, const Glib::ustring& info) :
  _CONSTRUCT("markup", markup.get_c_str(), "text", text.c_str(), "icon", Glib::unwrap(icon), "info", (info.empty() ? 0 : info.c_str()))
{}

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
CompletionItem::CompletionItem(const Glib::ustring& label, const Glib::ustring& text, const Gtk::StockID& stock, const Glib::ustring& info) :
  _CONSTRUCT("text", text.c_str(), "info", (info.empty() ? 0 : info.c_str()))
{
  GdkPixbuf* icon;
  Glib::ustring temp_label(label);

  if (stock)
  {
    GtkIconTheme* theme = gtk_icon_theme_get_default();

    int width, height;
    gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
    icon = gtk_icon_theme_load_icon (theme,
                                     stock.get_c_str(),
                                     width,
                                     GTK_ICON_LOOKUP_USE_BUILTIN,
                                     0);

    GtkStockItem stock_item;
    if (temp_label.empty() && gtk_stock_lookup (stock.get_c_str(), &stock_item))
    {
      temp_label = stock_item.label;
    }
  }
  else
  {
    icon = 0;
  }

  g_object_set(gobj(), "label", temp_label.c_str(), "icon", icon, static_cast<char*>(0));

  if (icon)
  {
    g_object_unref(icon);
  }
}
G_GNUC_END_IGNORE_DEPRECATIONS

} // namespace Gsv
