/*
Copyright (c) 2009 Christian Dywan <christian@lanedo.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* gcc -o statusbar statusbar.c $(pkg-config --cflags --libs gtk+-2.0) -Wall -O1 -g */

#include <gtk/gtk.h>

#define SPACING 4
#define NORMAL_CONTEXT "text"
#define _(x) x

static void
totem_statusbar_set_text (GtkWidget  *statusbar,
                          const char *label)
{
  GtkStatusbar *gstatusbar = GTK_STATUSBAR (statusbar);
  guint id;

  id = gtk_statusbar_get_context_id (gstatusbar, NORMAL_CONTEXT);
  gtk_statusbar_pop (gstatusbar, id);
  gtk_statusbar_push (gstatusbar, id, label);
}

static void
totem_statusbar_init (GtkWidget *statusbar)
{
  GtkStatusbar *gstatusbar = GTK_STATUSBAR (statusbar);
  GtkWidget *packer, *hbox, *vbox;
  GtkWidget *progress, *time_label;

  /* Widget surgery */
  #if 0
  hbox = gtk_hbox_new (FALSE, SPACING);
  g_object_ref (gstatusbar->label);
  gtk_container_remove (GTK_CONTAINER (gstatusbar->frame), gstatusbar->label);
  gtk_box_pack_start (GTK_BOX (hbox), gstatusbar->label, FALSE, FALSE, 0);
  g_object_unref (gstatusbar->label);
  gtk_container_add (GTK_CONTAINER (gstatusbar->frame), hbox);
  gtk_label_set_ellipsize (GTK_LABEL (gstatusbar->label), FALSE);
  #elif 1
  hbox = gtk_statusbar_get_message_area (gstatusbar);
  #endif
  gtk_widget_show (hbox);

  /* progressbar for network streams */
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, TRUE, 0);
  gtk_widget_show (vbox);

  progress = gtk_progress_bar_new ();
  gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR (progress),
				    gtk_widget_get_direction (progress) == GTK_TEXT_DIR_LTR ?
				    GTK_PROGRESS_LEFT_TO_RIGHT : GTK_PROGRESS_RIGHT_TO_LEFT);
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress), 0.);
  gtk_box_pack_start (GTK_BOX (vbox), progress, TRUE, TRUE, 1);
  gtk_widget_set_size_request (progress, 150, 10);

  packer = gtk_vseparator_new ();
  gtk_box_pack_start (GTK_BOX (hbox), packer, FALSE, FALSE, 0);
  gtk_widget_show (packer);

  time_label = gtk_label_new (_("0:00 / 0:00"));
  gtk_misc_set_alignment (GTK_MISC (time_label), 0.0, 0.5);
  gtk_box_pack_start (GTK_BOX (hbox), time_label, FALSE, FALSE, 0);
  gtk_widget_show (time_label);

  totem_statusbar_set_text (statusbar, _("Stopped"));
}

int
main (int   argc,
      char *argv[])
{
  GtkWidget *window;
  GtkWidget *box;
  GtkWidget *statusbar;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  box = gtk_vbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (box), gtk_text_view_new (), TRUE, TRUE, 0);
  statusbar = gtk_statusbar_new ();
  totem_statusbar_init (statusbar);
  gtk_box_pack_start (GTK_BOX (box), statusbar, FALSE, FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), box);

  g_signal_connect (window, "destroy",
      G_CALLBACK (gtk_main_quit), NULL);

  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
