/*
*
* Author: Giacomo Lozito <james@develia.org>, (C) 2005-2007
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*
*/

#include "aosd_ui.h"
#include <glib.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>


gint
aosd_display_osd ( void )
{
  GtkWidget *osd_win;
  GtkWidget *osd_darea;
  PangoLayout *osd_layout;
  PangoFontDescription *osd_fontdesc;
  GdkPixmap *osd_darea_pixmap, *osd_darea_bitmap;
  GdkGC *osd_darea_gc;
  GdkColor fg_color, bg_color, bitmap_color;
  GdkColormap *bitmap_colormap;
  gint max_width = 0, border_width = 1;
  gint width = 0, height = 0;
  gint off_x = 0, off_y = 0;
  PangoAlignment osd_alignment = PANGO_ALIGN_CENTER;
  gboolean translucent_bg = TRUE;

  osd_win = gtk_window_new( GTK_WINDOW_POPUP );
  gtk_widget_add_events( osd_win , GDK_ENTER_NOTIFY_MASK );
  gtk_widget_realize( osd_win );

  osd_darea = gtk_drawing_area_new();
  gtk_container_add( GTK_CONTAINER(osd_win) , osd_darea );
  gtk_widget_show( osd_darea );

  osd_fontdesc = pango_font_description_from_string("Courier,Mono 28");

  osd_layout = gtk_widget_create_pango_layout( osd_darea , "AUDACIOUS OSD 0.1 by Giacomo" );
  pango_layout_set_ellipsize( osd_layout , PANGO_ELLIPSIZE_NONE );
  pango_layout_set_justify( osd_layout , FALSE );
  pango_layout_set_alignment( osd_layout , osd_alignment );
  pango_layout_set_font_description( osd_layout , osd_fontdesc );

  max_width = gdk_screen_get_width( gdk_screen_get_default() ) - 8;
  pango_layout_set_width( osd_layout , PANGO_SCALE * max_width );
  pango_layout_get_pixel_size( osd_layout , &width , &height );

  off_x = border_width * 2;
  off_y = border_width * 2;

  if ( osd_alignment == PANGO_ALIGN_CENTER )
    off_x -= max_width/2 - width/2;
  else if ( osd_alignment == PANGO_ALIGN_RIGHT )
    off_x -= max_width - width;

  width += border_width * 4;
  height += border_width * 4;

  gtk_widget_set_size_request( osd_darea , width , height );
  gtk_widget_realize( osd_darea );

  osd_darea_pixmap = gdk_pixmap_new( GDK_DRAWABLE(osd_darea->window) , width , height , -1 );

  osd_darea_gc = gdk_gc_new( GDK_DRAWABLE(osd_darea_pixmap) );
  gdk_gc_copy( osd_darea_gc , osd_darea->style->fg_gc[GTK_STATE_NORMAL] );

  gdk_color_parse( "Blue" , &fg_color ); /* TODO pass color as function param */
  gdk_color_parse( "White" , &bg_color ); /* TODO pass color as function param */

  gdk_gc_set_rgb_fg_color( osd_darea_gc , &bg_color );
  gdk_draw_rectangle( GDK_DRAWABLE(osd_darea_pixmap) , osd_darea_gc , TRUE ,
                      0 , 0 , width , height );
  gdk_gc_set_rgb_fg_color( osd_darea_gc , &fg_color );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_pixmap) , osd_darea_gc , off_x , off_y , osd_layout );
  g_object_unref( osd_darea_gc );

  osd_darea_bitmap = gdk_pixmap_new( GDK_DRAWABLE(osd_darea->window) , width , height , 1 );
  osd_darea_gc = gdk_gc_new( GDK_DRAWABLE(osd_darea_bitmap) );

  /* gdk will complain if we don't pass a colormap to osd_darea_gc */
  gdk_gc_set_colormap( osd_darea_gc , gdk_colormap_get_system() );

  bitmap_color.pixel = 0;
  gdk_gc_set_foreground( osd_darea_gc , &bitmap_color );

  if ( translucent_bg == TRUE )
  {
    gint w = 2, h = 2;
    GdkPixmap *stipple_bitmap;

    stipple_bitmap = gdk_pixmap_new( NULL , w , h , 1 );
    bitmap_color.pixel = 0; gdk_gc_set_foreground( osd_darea_gc , &bitmap_color );
    gdk_draw_rectangle( GDK_DRAWABLE(stipple_bitmap) , osd_darea_gc , TRUE , 0 , 0 , w , h );
    bitmap_color.pixel = 1; gdk_gc_set_foreground( osd_darea_gc , &bitmap_color );
    gdk_draw_point( GDK_DRAWABLE(stipple_bitmap) , osd_darea_gc , 0 , 0 );
    gdk_draw_point( GDK_DRAWABLE(stipple_bitmap) , osd_darea_gc , 1 , 1 );
    bitmap_color.pixel = 0; gdk_gc_set_foreground( osd_darea_gc , &bitmap_color );
    gdk_draw_rectangle( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc , TRUE ,
                        0 , 0 , width , height );
    gdk_gc_set_stipple( osd_darea_gc , stipple_bitmap );
    gdk_gc_set_fill( osd_darea_gc , GDK_STIPPLED );
    bitmap_color.pixel = 1; gdk_gc_set_foreground( osd_darea_gc , &bitmap_color );
    gdk_draw_rectangle( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc , TRUE ,
                        0 , 0 , width , height );
    gdk_gc_set_fill( osd_darea_gc , GDK_SOLID );
  }
  else
    gdk_draw_rectangle( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc , TRUE ,
                        0 , 0 , width , height );

  bitmap_color.pixel = 1;
  gdk_gc_set_foreground( osd_darea_gc , &bitmap_color );

  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x , off_y , osd_layout );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x + border_width , off_y , osd_layout );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x + border_width , off_y + border_width , osd_layout );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x , off_y + border_width , osd_layout );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x - border_width , off_y + border_width , osd_layout );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x - border_width , off_y , osd_layout );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x - border_width , off_y - border_width , osd_layout );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x , off_y - border_width , osd_layout );
  gdk_draw_layout( GDK_DRAWABLE(osd_darea_bitmap) , osd_darea_gc ,
                   off_x + border_width , off_y - border_width , osd_layout );

  g_object_unref( osd_darea_gc );

  gdk_window_set_back_pixmap( GDK_WINDOW(osd_darea->window) , osd_darea_pixmap , FALSE );
  gdk_window_shape_combine_mask( GDK_WINDOW(osd_win->window) , osd_darea_bitmap , 0 , 0 );

  gtk_widget_show( osd_win );

  pango_font_description_free( osd_fontdesc );
  g_object_unref( osd_layout );
  return 0;
}
