package com.algobase.share.dialog;

import android.os.Build;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.content.res.ColorStateList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;

import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ContextThemeWrapper;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.Window;
import android.view.Display;

import android.widget.Toast;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.RectShape;

import com.algobase.share.compat.*;


public class MyDialogBuilder extends AlertDialog.Builder {

   public static int STYLE_TRADITIONAL   = 0;
   public static int STYLE_HOLO_DARK     = 1;
   public static int STYLE_HOLO_LIGHT    = 2;
   public static int STYLE_MATERIAL_DARK  = 3;
   public static int STYLE_MATERIAL_LIGHT = 4;

   public static int[] dialog_styles = { 
                           StyleCompat.Theme_Traditional,
                           StyleCompat.Theme_Holo,
                           StyleCompat.Theme_Holo_Light,
                           StyleCompat.Theme_Material,
                           StyleCompat.Theme_Material_Light
                         };

   public static int def_anim_style = 0;

   public static int def_style = STYLE_MATERIAL_DARK; 


   public static void setTheme(int theme_id) {
     int i = -1;
     while (++i < dialog_styles.length) { 
       if (dialog_styles[i] == theme_id) break; 
     }

     if (i < dialog_styles.length) def_style = i;
   }

   public static void setStyle(int i) { def_style = i; }
   public static int getStyle() { return def_style; }

   public static void setDefAnimationStyle(int s) { def_anim_style = s; }

   public static void setGlobalTitleButtonLeft(int r) {}


   private int style = def_style;

   private int anim_style = 0;


   private Context context;
   //private LinearLayout view = null;
   private View view = null;

   private int backgroundColor = 0;

   private String msgText = null;
   private String titleText = null;

   private View titleView = null;
   private TextView titleTextView = null;

   private ScrollView scrollView = null;

   private TextView msgView = null;
   private LinearLayout buttonLine = null;

   //private int titleTextSize = 24;
   //private int msgTextSize = 18;

   private int titleTextSize = 23;
   private int titleTextTypeface = Typeface.NORMAL; 

   private int msgTextColor = 0xff000000;
   private int msgTextSize = 19;

   private int buttonTextSize = 17;

   //private int checkBoxTextSize = 17;
   private int checkBoxTextSize = 19;

   //private int radioButtonTextSize = 18;
   private int radioButtonTextSize = 19;

   private DialogInterface.OnShowListener  onShowListener;

   private String button0_text = null;
   private Button button0 = null;
   private DialogInterface.OnClickListener  onClickListener0;

   private String button1_text = null;
   private Button button1 = null;
   private DialogInterface.OnClickListener  onClickListener1;

   private String button2_text = null;
   private Button button2 = null;
   private DialogInterface.OnClickListener  onClickListener2;

   private String title_button_text = null;
   private int title_button_img = 0;

   private DialogInterface.OnClickListener  onClickListener3;


   private CharSequence[] items = null;

   private ShapeDrawable button_up;
   private ShapeDrawable button_down;

   private AlertDialog dialog;

   private boolean autoDismiss = true;
   private boolean titleSingleLine = false;
   private boolean buttonTextBold = false;


   private int screen_width;
   private int screen_height;


   public boolean onTitleTouch(View v, MotionEvent event) { return false; }
   public void onTitleClick(View v) {}

   private static Context mkctxt(Context ctxt, int style) {
     return new ContextThemeWrapper(ctxt,dialog_styles[def_style]);
   }


   public MyDialogBuilder(Context ctxt) { 
     super(mkctxt(ctxt,def_style));
     init(ctxt,null,def_style);
   }

   public MyDialogBuilder(Context ctxt, String title) { 
     super(mkctxt(ctxt,def_style));
     init(ctxt,title,def_style);
   }

   public MyDialogBuilder(Context ctxt,int s) { 
     super(mkctxt(ctxt,s));
     init(ctxt,null,s);
   }

   public MyDialogBuilder(Context ctxt, String title, int s) { 
     super(mkctxt(ctxt,s));
     init(ctxt,title,s);
   }


   private void init(Context ctxt,String title, int s)
   { 
     //context = super.getContext();

     context = mkctxt(ctxt,s);

     titleText = title;
     style = s;


     WindowManager wm = 
              (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
     Display display = wm.getDefaultDisplay(); 
     Point pt = new Point();
     display.getSize(pt);
     screen_width = pt.x;
     screen_height = pt.y;

     msgTextSize = 19;

     if (style == STYLE_MATERIAL_LIGHT || style == STYLE_MATERIAL_DARK)
     { //titleTextSize = 22;
       titleTextSize = 20;
       titleTextTypeface = Typeface.BOLD; 
      }
     else
     { titleTextSize = 23;
       titleTextTypeface = Typeface.NORMAL; 
      }


     if (style == STYLE_HOLO_LIGHT || style == STYLE_MATERIAL_LIGHT)
       msgTextColor = 0xff000000;
     else
       msgTextColor = 0xeeeeeeee;

     button_up = new ShapeDrawable(new RectShape());

     if (style == STYLE_HOLO_LIGHT)
       button_up.getPaint().setColor(0xff999999);
     else
       button_up.getPaint().setColor(0xff555555);

     button_up.getPaint().setStyle(Paint.Style.STROKE);
     button_up.getPaint().setStrokeWidth(2);

     button_down = new ShapeDrawable(new RectShape());
     button_down.getPaint().setColor(0xff0099cc);
     button_down.getPaint().setStyle(Paint.Style.FILL);

     setCancelable(true);
   }

  public Context getContext() { return context;  }

  private int pix(float dp)
  { float dpi = context.getResources().getDisplayMetrics().densityDpi;
    return (int)(0.5 + dp * (dpi/160f));
   }

  //public TextView getTitleView() { return (TextView)titleView; }
  public TextView getTitleView() { return titleTextView; }

  public View getView() { return view; }

  public View addView(View v) //{ view = v; return view; }
  { LinearLayout ll = new LinearLayout(context);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.addView(v); 
    view = ll;
    return view; 
  }


  public View addView(View v, int l, int t, int r, int b) 
  { LinearLayout ll = new LinearLayout(context);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setPadding(l,t,r,b);
    ll.addView(v); 
    view = ll;
    return view; 
  }


  public View addView(int viewRes) {
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                             context.LAYOUT_INFLATER_SERVICE);
     return addView(inflater.inflate(viewRes,null));
  }


  public View addScrollView(View v) 
  { ScrollView sv = new ScrollView(context);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,

                                           LayoutParams.WRAP_CONTENT, 1);
    sv.addView(v); 
    view = sv;
    return view; 
  }


  public void setBackgroundColor(int clr) { backgroundColor = clr; }

  public void setTitleView(View view) { titleView = view; }

  @Override
  public AlertDialog.Builder setTitle(CharSequence title) 
  { if (title != null) titleText = title.toString(); 
    return this;
   }

  @Override
  public AlertDialog.Builder setMessage(CharSequence msg) 
  { if (msg != null) msgText = msg.toString(); 
    return this;
   }

 public void setTitleTextSize(int sz) { titleTextSize = sz; }
 public void setMessageTextSize(int sz) { msgTextSize = sz; }
 public void setButtonTextSize(int sz) { buttonTextSize = sz; }
 public void setButtonTextBold(boolean b) { buttonTextBold = b; }
 public void setCheckBoxTextSize(int sz) { checkBoxTextSize = sz; }
 public void setRadioButtonTextSize(int sz) { radioButtonTextSize = sz; }

 public void setMessageTextColor(int clr) { msgTextColor = clr; }


  @Override
  public AlertDialog.Builder setItems(CharSequence[] it, 
                            DialogInterface.OnClickListener listener)
  { items = it;
    super.setItems(it,listener);
    return this;
   }

  public void setOnTitleClickListener(View.OnClickListener listener) {
  }

  public void setAnimationStyle(int s) { anim_style = s; }

  public void setOnShowListener(DialogInterface.OnShowListener listener) {
     onShowListener = listener;
  }

  public void setNegativeButton(CharSequence txt)
  { DialogInterface.OnClickListener listener = null;
    setNegativeButton(txt,listener);
   }

  @Override
  public MyDialogBuilder setNegativeButton(CharSequence txt, 
                                    DialogInterface.OnClickListener listener)
  { 
    if (style == STYLE_HOLO_LIGHT || style == STYLE_HOLO_DARK)
      { button1_text = txt.toString();
        onClickListener1 = listener;
       }
    else
      super.setNegativeButton(txt,listener);

    return this;
   }


  public void setPositiveButton(CharSequence txt)
  { DialogInterface.OnClickListener listener = null;
    setPositiveButton(txt,listener);
   }


  @Override
  public MyDialogBuilder setPositiveButton(CharSequence txt,
                                    DialogInterface.OnClickListener listener)
  { 
    if (style == STYLE_HOLO_LIGHT || style == STYLE_HOLO_DARK)
      { button2_text = txt.toString();
        onClickListener2 = listener;
       }
    else
      super.setPositiveButton(txt,listener);

    return this;
   }


  public void setNeutralButton(CharSequence txt)
  { DialogInterface.OnClickListener listener = null;
    setNeutralButton(txt,listener);
   }


  @Override
  public MyDialogBuilder setNeutralButton(CharSequence txt,
                                    DialogInterface.OnClickListener listener)
  { 
    if (style == STYLE_HOLO_LIGHT || style == STYLE_HOLO_DARK)
      { button0_text = txt.toString();
        onClickListener0 = listener;
       }
    else
      super.setNeutralButton(txt,listener);

    return this;
   }


  public MyDialogBuilder setTitleButton(CharSequence txt,
                                    DialogInterface.OnClickListener listener)
  { 
    title_button_text = txt.toString();
    title_button_img = 0;
    onClickListener3 = listener;
    return this;
   }


  public MyDialogBuilder setTitleButton(int img_res,
                                    DialogInterface.OnClickListener listener)
  { title_button_text = null;
    title_button_img = img_res;
    onClickListener3 = listener;
    return this;
   }

  public MyDialogBuilder setTitleButtonLeft(int img_res,
                                    DialogInterface.OnClickListener listener)
  { 
    return this;
   }


  public Button newButton(String text) { return newButton(text,false); }

  public Button newButton(String text, final boolean title) 
  {
    Button button = new Button(context);
    button.setSingleLine(true);
    button.setText(text);

    if (title)
    { button.setPadding(0,0,0,0);
      button.setBackground(null);
/*
      button.setTextSize(1.6f*buttonTextSize);
      button.setTextColor(0xff606060);
*/
      button.setTextSize(1.4f*buttonTextSize);
      button.setTextColor(0xffa0a0a0);
     }
    else
    { // only used for holo styles
      button.setTextSize(buttonTextSize);
      button.setBackground(button_up);

      if (buttonTextBold)
      button.setTypeface(null,Typeface.BOLD);

     }

    button.setOnTouchListener(new View.OnTouchListener() {
         public boolean onTouch(View v, MotionEvent event) {
             Button b = (Button)v;
             if (event.getAction() == MotionEvent.ACTION_DOWN)
               { b.setBackground(button_down);
                 b.setTextColor(0xffffffff);
                }
             if (event.getAction() == MotionEvent.ACTION_UP)
               { if (title) 
                   { b.setTextColor(0xff606060);
                     b.setBackground(null);
                    }
                 else
                   { b.setTextColor(0xff000000);
                     b.setBackground(button_up);
                    }
                }
             return false;
         }
    });
  
    return button;
  }
 


  private void add_buttons(LinearLayout layout) 
  {
     LinearLayout buttonLine = new LinearLayout(context);
     buttonLine.setOrientation(LinearLayout.HORIZONTAL);

     if (button1_text != null) 
     { button1 = newButton(button1_text);
       button1.setOnClickListener( new View.OnClickListener() {
              public void onClick(View v) {
                  if (onClickListener1 != null)
                    onClickListener1.onClick(dialog,1);
                  if (dialog.isShowing() && autoDismiss) dialog.dismiss();
              }
        });
       LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                                              LayoutParams.WRAP_CONTENT, 1);
       params.leftMargin=-1;
       buttonLine.addView(button1,params);
     }

     if (button0_text != null) 
     {  button0 = newButton(button0_text);
        button0.setOnClickListener( new View.OnClickListener() {
              public void onClick(View v) {
                  if (onClickListener0 != null)
                    onClickListener0.onClick(dialog,1);
                  if (dialog.isShowing() && autoDismiss) dialog.dismiss();
              }
        });
       LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                                              LayoutParams.WRAP_CONTENT, 1);
       params.rightMargin=0;
       buttonLine.addView(button0,params);
     }

     if (button2_text != null) 
     { button2 = newButton(button2_text);
       button2.setOnClickListener( new View.OnClickListener() {
              public void onClick(View v) {
                  if (onClickListener2 != null)
                    onClickListener2.onClick(dialog,1);
                  if (dialog.isShowing() && autoDismiss) dialog.dismiss();
              }
        });
       LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                                              LayoutParams.WRAP_CONTENT, 1);
       params.leftMargin=-1;
       buttonLine.addView(button2,params);
     }


     LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                                            LayoutParams.WRAP_CONTENT);

     if (scrollView !=  null)
       params.topMargin=3;
     else
       params.topMargin=15;

     params.bottomMargin=0;

     layout.addView(buttonLine,params);
  }


  public void addMessage(LinearLayout layout, String txt) 
  { 
    msgView = new TextView(context);
    boolean msg_only = (view == null && items == null &&
                        button1_text == null && button2_text == null);

    if (style == STYLE_TRADITIONAL)
        msgView.setPadding(pix(10),pix(5),0,pix(15));
    else
      if (msg_only)
        { final ShapeDrawable d = new ShapeDrawable(new RectShape());
          d.getPaint().setColor(0xff0099cc);
          d.getPaint().setStyle(Paint.Style.FILL);
          msgView.setPadding(pix(10),pix(8),pix(10),pix(12));
          msgView.setOnTouchListener( new View.OnTouchListener() {
              public boolean onTouch(View v, MotionEvent event) {
                  ((TextView)v).setBackground(d);
                  ((TextView)v).setTextColor(0xffffffff);
                  return false;
              }
           });
          msgView.setOnClickListener( new View.OnClickListener() {
              public void onClick(View v) {
                  AlertDialog d = (AlertDialog)v.getTag();
                  d.dismiss();
              }
           });
        }
      else
        msgView.setPadding(pix(10),pix(8),pix(10),0);


    msgView.setTextSize(msgTextSize);

    msgView.setTextColor(msgTextColor);

    if (txt.length() > 500) 
    { int p = txt.length() - 1;
      while (p >= 400 && txt.charAt(p) != '.') p--;
      if (p < 400)
      { p = txt.length()-1;
        while (p >= 400 && txt.charAt(p) != ' ') p--;
        if (p < 400)  p = 499;
      }
      txt = txt.substring(0,p+1);
    }

    msgView.setText(txt);
    layout.addView(msgView);
  }


  public TextView titleTextView(String title)
  { 
    TextView tv = new TextView(context);
    tv.setSingleLine(titleSingleLine);
    tv.setPadding(pix(10),pix(8),pix(10),pix(6));
    tv.setClickable(true);

    tv.setTextSize(titleTextSize);
    tv.setTypeface(null,titleTextTypeface);

    if (style == STYLE_TRADITIONAL) 
    { tv.setBackgroundColor(0xff333333);
      tv.setTextColor(0xffffffff);
     }

    if (style == STYLE_HOLO_DARK) tv.setTextColor(0xff00ddff);

    if (style == STYLE_HOLO_LIGHT) tv.setTextColor(0xff0099cc);
/*
    if (style == STYLE_MATERIAL_DARK) tv.setTextColor(0xff80cbc4);
    if (style == STYLE_MATERIAL_LIGHT) tv.setTextColor(0xff009688);
*/
    if (style == STYLE_MATERIAL_DARK) tv.setTextColor(0xffffffff);
    if (style == STYLE_MATERIAL_LIGHT) tv.setTextColor(0xff000000);


    tv.setText(titleText);

    tv.setOnTouchListener( new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
              return onTitleTouch(v,event);
        }
    });

    tv.setOnClickListener( new View.OnClickListener() {
        public void onClick(View v) {
              //onTitleClick(v);
              onTitleClick(view);
        }
    });

    return tv;
  }



  @Override
  public AlertDialog create() {

     //setCancelable(true);

     if (titleView != null)
       setCustomTitle(titleView);
     else
     if (titleText != null) {
/*
       if (style == STYLE_MATERIAL_LIGHT || style == STYLE_MATERIAL_DARK) 
         super.setTitle(titleText);
       else
*/
       { 
         titleTextView = titleTextView(titleText);

         if (title_button_text == null && title_button_img == 0)
           titleView = titleTextView;
         else
         { // add title button

           View title_button_view = null;

           if (title_button_text != null)
           { Button b = newButton(title_button_text,true);
             title_button_view = (View)b;
            }

           if (title_button_img != 0)
           { ImageButton b = new ImageButton(context);
             b.setImageResource(title_button_img);
             //b.setBackgroundColor(0xff555555);
             b.setBackgroundColor(0x00000000);
             title_button_view = (View)b;
            }

           title_button_view.setOnClickListener( new View.OnClickListener() {
              public void onClick(View v) {
                  if (onClickListener3 != null)
                    onClickListener3.onClick(dialog,1);
              }
           });

           LinearLayout titleLayout = new LinearLayout(context);

           LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                                           LayoutParams.MATCH_PARENT, 1);
           titleLayout.addView(titleTextView,params);

           LayoutParams params1 = new LayoutParams(LayoutParams.WRAP_CONTENT,
                                           LayoutParams.WRAP_CONTENT);
           titleLayout.addView(title_button_view,params1);
           titleView = titleLayout;
          }
     
          setCustomTitle(titleView);
        }
     }

     LinearLayout layout = new LinearLayout(context);
     layout.setOrientation(LinearLayout.VERTICAL);

      if (backgroundColor != 0)
        layout.setBackgroundColor(backgroundColor);
      else
      if (style == STYLE_HOLO_LIGHT || style == STYLE_MATERIAL_LIGHT)
        layout.setBackgroundColor(0xffffffff);
      else
        layout.setBackgroundColor(0xff000000);



     if (msgText != null) addMessage(layout,msgText);

     if (view != null) 
     { view.measure(0,0);
       int h = view.getMeasuredHeight();

       int max_h =  screen_height - pix(100);

       if (msgView != null)
       { msgView.measure(0,0);
         max_h -= msgView.getMeasuredHeight();
        }

       if (button1_text != null || button2_text != null)
       { //if (buttonLine != null) buttonLine.measure(0,0);
         //max_h -= pix(60);
         max_h -= pix(70);
        }
/*
String txt = String.format("h = %d  max_h = %d",h,max_h);
Toast.makeText(context,txt,Toast.LENGTH_SHORT).show();
*/

       if (h <= max_h)
         { layout.addView(view);
           scrollView = null;
          }
       else
         { scrollView = new ScrollView(context);
           scrollView.setPadding(0,0,0,0);
           scrollView.addView(view);
           layout.addView(scrollView);
           ViewGroup.LayoutParams params = scrollView.getLayoutParams();
           params.height=max_h;
           scrollView.setLayoutParams(params);
          }
      }

     if (button1_text != null || button2_text != null) add_buttons(layout);

     if (layout.getChildCount() > 0) super.setView(layout);

     dialog = super.create();
     //if (layout.getChildCount() > 0) dialog.setView(layout,0,0,0,0);

     if (msgView != null) msgView.setTag(dialog);

     dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

     if (onShowListener != null) dialog.setOnShowListener(onShowListener);
     return dialog;
  }


  public ListView getListView() {
    if (dialog != null)
      return dialog.getListView();
    else
      return null;
 }

  public Window getWindow() { return dialog.getWindow(); }


  @Override
  public AlertDialog show() {
    dialog = create();
    dialog.show();
    return dialog;
  }


  public AlertDialog show(float f)
  {
    dialog = create();
    dialog.show();
    int w = (int)(f * screen_width);
    dialog.getWindow().setLayout(w,LayoutParams.WRAP_CONTENT);

    //dialog.getWindow().getAttributes().y = screen_height - 50;
    //dialog.getWindow().getAttributes().verticalMargin=0.05f;
    //dialog.getWindow().getAttributes().horizontalMargin=0.03f;
    //dialog.getWindow().setGravity(Gravity.TOP|Gravity.RIGHT);
    //dialog.getWindow().setGravity(Gravity.TOP);

    if (anim_style != 0)
      dialog.getWindow().getAttributes().windowAnimations = anim_style;
    else
      if (def_anim_style != 0)
        dialog.getWindow().getAttributes().windowAnimations = def_anim_style;

    return dialog;
  }

  public AlertDialog show_menu()
  { dialog = create();
    dialog.show();
    int w = (int)(0.85f * screen_width);
    dialog.getWindow().setLayout(w,LayoutParams.WRAP_CONTENT);
    //dialog.getWindow().setGravity(Gravity.BOTTOM);
    dialog.getWindow().getAttributes().horizontalMargin=0.0f;
    dialog.getWindow().getAttributes().verticalMargin=0.06f;
    dialog.getWindow().setGravity(Gravity.TOP|Gravity.RIGHT);
    return dialog;
  }



  public AlertDialog show_fullscreen() 
  {
    dialog = create();
    //if (!context.isFinishing()) 

    dialog.show();

    //WindowManager.LayoutParams params = dialog.getWindow().getAttributes();

/*
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.copyFrom(dialog.getWindow().getAttributes());
    params.gravity = Gravity.CENTER;
    params.x = 0;
    params.y = 0;
    params.horizontalMargin = 0.0f;
    params.verticalMargin = 0.0f;
    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    params.height = WindowManager.LayoutParams.MATCH_PARENT;
    dialog.getWindow().setAttributes(params);
*/
    WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    params.height = WindowManager.LayoutParams.MATCH_PARENT;


    if (view != null) view.setPadding(0,0,0,0);

    return dialog;
  }

  public void setButtonDrawable(CheckBox cb) {
    if (style == STYLE_HOLO_LIGHT || style == STYLE_HOLO_DARK) 
    { int  btn_check_id = context.getResources().getIdentifier("btn_check",
                                                               "drawable",
                                                               "android");
      cb.setButtonDrawable(btn_check_id);
     }
  }

  public void setButtonDrawable(RadioButton but) {
    if (style == STYLE_HOLO_LIGHT || style == STYLE_HOLO_DARK) 
    { int  btn_radio_id = context.getResources().getIdentifier("btn_radio",
                                                               "drawable",
                                                               "android");
      but.setButtonDrawable(btn_radio_id);
     }
  }
 
 

  public CheckBox newCheckBox() 
  { CheckBox cb = new CheckBox(context);
    cb.setTextSize(checkBoxTextSize);
    cb.setPadding(pix(0),pix(2),pix(0),pix(7));
    setButtonDrawable(cb);
/*
    if (style == STYLE_MATERIAL_LIGHT) cb.setTextColor(0xff555555);
*/
    return cb;
  }

  public RadioButton newRadioButton() 
  { RadioButton but = new RadioButton(context);
    but.setTextSize(radioButtonTextSize);
    but.setPadding(pix(0),pix(2),pix(0),pix(7));
    setButtonDrawable(but);
    return but;
  }

  public CheckBox findCheckBox(int id)
  { if (view == null) return null;
    CheckBox cb = (CheckBox)view.findViewById(id);
    if (cb != null) setButtonDrawable(cb);
    return cb;
   }

  public RadioButton findRadioButton(int id)
  { if (view == null) return null;
    RadioButton but = (RadioButton)view.findViewById(id);
    if (but != null) setButtonDrawable(but);
    return but;
   }



  public TextView newBoxTextView(int clr) {
    TextView tv = new TextView(context);
    GradientDrawable bg = new GradientDrawable();
    bg.setCornerRadius(0);
    bg.setColor(clr);
    bg.setStroke(pix(1),0xff333333);
    tv.setBackground(bg);
    tv.setTextSize(16);
    return tv;
  }


  public TextView newTextView() {
    TextView tv = new TextView(context);
/*
    if (style == STYLE_HOLO_LIGHT)
      tv.setTextColor(0xff000000);
    else
      tv.setTextColor(0xffffffff);
*/
    tv.setTextSize(16);
    return tv;
  }


  public EditText newEditText() {
    EditText edt = new EditText(context);
    edt.setTextSize(17);
    return edt;
  }

  public ListView newListView() { return new ListView(context); }

  public SeekBar newSeekBar() {
    SeekBar sb = new SeekBar(context);
    return sb;
  }

  public ProgressBar newProgressBar() {
    ProgressBar pb = new ProgressBar(context,null,
                                     android.R.attr.progressBarStyleHorizontal);
/*
    float[] rad = new float[8];
    for(int i=0;i<8;i++) rad[i] = pix(8);
    ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(rad,null,null));
    shape.getPaint().setColor(0xffaa0000);
*/
    GradientDrawable shape =  new GradientDrawable();
    shape.setColor(0xffaa0000);
    shape.setCornerRadius(pix(8));

    pb.setProgressDrawable(new ClipDrawable(shape,Gravity.LEFT,
                                                  ClipDrawable.HORIZONTAL));

    GradientDrawable bg_shape =  new GradientDrawable();
    bg_shape.setColor(0x00000000);
    bg_shape.setCornerRadius(pix(8));
    bg_shape.setStroke(1,0xff000000);

    pb.setBackground(bg_shape);

    return pb;
  }

  public void setAutoDismiss(boolean b) { autoDismiss = b; }

  public void setTitleSingleLine(boolean b) { titleSingleLine = b; }

  public boolean isShowing() { 
    return dialog != null && dialog.isShowing(); 
  }

  public void dismiss()
  { if (dialog == null) return;
    if (!dialog.isShowing()) return;
    dialog.dismiss();
  }


}
