#include <fspanel.h>


#define PAGER_BOX_WIDTH 24

applet_info_t info_g;
int pager_box_width_g = 24;

applet_info_t get_applet_info( void )
{
    int max_desks = wm_get_workspace_count();
    
    info_g.width = pager_box_width_g * max_desks;
    
    return info_g;
}

set_applet_info( applet_info_t inf )
{
    int max_desks = wm_get_workspace_count();
    
    info_g = inf;
    
    if( info_g.width != pager_box_width_g * max_desks )
    {
        // we have work to do .. someone has changed our width.
        // the pager box width, should be width divided by the number of
        // desktops ...
        pager_box_width_g = info_g.width / max_desks;
    }
}

// NOTE to me ... consider passing in the display, root window, etc ...
int gui_draw( int x, void *token ) 
{
  int i, loc, text_x;
  char label[2];
  unsigned long max_desks;
  int pager_width;
  int winheight;  
  
  max_desks = wm_get_workspace_count();

  winheight = panel_get_winheight();
  
  if( max_desks )
  {    
      pager_width = pager_box_width_g * max_desks;

      gui_draw_vline( x );
      
      for ( i = 0; i < max_desks; i++)
      {
          loc = x + (i * pager_box_width_g);
          label[0] = i + '1';
          text_x = loc + (pager_box_width_g / 2) -
              ( XTextWidth( x_get_fontstruct(), label,1 ) / 2 );
          
          if( i == wm_get_current_desktop() )
          {
              set_foreground(1);
              fill_rect ( loc + 3, 2, pager_box_width_g - 5, winheight - 3 );
              set_foreground (4);
              draw_line ( loc + 2, 1, loc + 2, winheight - 2 );
              draw_line ( loc + 3, 1, loc + pager_box_width_g - 2, 1 );
              set_foreground (3);
              draw_line ( loc + 3, winheight - 2 , loc + pager_box_width_g - 2,
                         winheight - 2 );
              draw_line ( loc + pager_box_width_g - 2, winheight - 3,
                         loc + pager_box_width_g - 2, 2 );
              set_foreground(5);
              XDrawString( x_get_display(),
                           x_get_window(),
                           x_get_foregroundGC(),
                           text_x,
                           panel_get_y_text_origin(),
                           label,
                           1 );
          }
          else 
          {
              set_foreground(0);
              fill_rect ( loc + 2, 1, pager_box_width_g - 3, winheight - 2 );
              set_foreground(5);
              XDrawString( x_get_display(),
                           x_get_window(),
                           x_get_foregroundGC(),
                           text_x,
                           panel_get_y_text_origin(),
                           label,
                           1 );
          }
          gui_draw_vline( loc + pager_box_width_g);
      }
  }
  else
  {
      pager_width = 0;
  }

  return x + pager_width;
}


int gui_button_press ( int x, int y, int button, void *token )
{
	XClientMessageEvent xev;
    unsigned long max_desks;
    int target;
    
    max_desks = wm_get_workspace_count();
    
    target = (x / pager_box_width_g);
    
    
    wm_switch_desktop( target ); 
}

int event_notification( void *token )
{

}

int gui_button_release ( int x, int y, int button, void *token )
{

}
