#include <gtksourceview/gtksource.h>

static gboolean
on_delete_event (GtkWidget *widget G_GNUC_UNUSED,
                 GdkEvent  *event G_GNUC_UNUSED,
                 gpointer   data G_GNUC_UNUSED)
{
  return FALSE;
}

int main(int argc, char**argv)
{
  GtkWindow* window;
  GtkSourceLanguageManager* lang_man;
  GtkSourceView* view;
  GtkScrolledWindow* scrolled_window;
  GtkSourceBuffer* buffer;
  GtkSourceLanguage* lang;
  GFile* file;
  GFileInputStream* istream;
  gchar* file_buf;
  gsize read_bytes;
  GtkTextIter iter;
  GtkButton* button;
  GtkTextChildAnchor* anchor;

  gtk_init(&argc, &argv);

  window = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
  scrolled_window = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new (NULL, NULL));
  view = GTK_SOURCE_VIEW(gtk_source_view_new ());
  lang_man = gtk_source_language_manager_get_default ();

  g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (scrolled_window));
  gtk_window_set_default_size (window, 500, 350);

  gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (view));
  gtk_scrolled_window_set_policy (scrolled_window,
                                  GTK_POLICY_NEVER,
                                  GTK_POLICY_AUTOMATIC);

  gtk_source_view_set_show_line_numbers (view, TRUE);
  gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE);

  buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
  lang = gtk_source_language_manager_guess_language (lang_man, __FILE__, NULL);
  gtk_source_buffer_set_language (buffer, lang);

  file = g_file_new_for_path (__FILE__);
  istream = g_file_read (file, NULL, NULL);

#define COUNT 1024
  file_buf = (gchar*)g_malloc (COUNT);
  do
  {
    g_input_stream_read_all (G_INPUT_STREAM(istream),
                             file_buf,
                             COUNT,
                             &read_bytes,
                             NULL,
                             NULL);
    gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (buffer), &iter);
    gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer),
                            &iter,
                            file_buf,
                            read_bytes);
  }
  while (read_bytes == COUNT);
#undef COUNT
  g_free (file_buf);
  g_object_unref (file);
  g_object_unref (istream);

  gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (buffer), &iter, 3);
  gtk_text_iter_forward_chars (&iter, 10);
  button = GTK_BUTTON(gtk_button_new_with_label ("*"));
  anchor = gtk_text_buffer_create_child_anchor (GTK_TEXT_BUFFER (buffer),
                                                &iter);
  gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (view),
                                     GTK_WIDGET (button),
                                     anchor);
  gtk_widget_show (GTK_WIDGET (button));
  gtk_widget_show_all (GTK_WIDGET (window));

  gtk_main();

  return 0;
}
