
/*

      P231c final project: Periodic Table with friendly user interface.
      Zhao, Hong

      This program can display a whole periodic table and allow the 
  user to access to every element by push the corresponding button.
  The information about the element is shown in the terminal that
  running the program. In this program only two constructor is used
  --- by atomic number and main_group_element constructor.

      Please use the following command to compile the code on the sun
  stations in the 1st floor computer lab of FRH:

    g++ interface.c PeriodicTable.c -o PeriodicTable -lXm -lXt -lX11

  then type "PeriodicTable" to run it. You can get any information about
  any element in the table by pushing the corresponding button. Whenever
  you want to quit the program, just push the "quit" button.

      The background color may be different on different machine due to 
  the different color maps.

*/



#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xm/Xm.h>
#include <Xm/PushB.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include "PeriodicTable.h"


#define TOTALCOLUMNS 18
#define TOTALROWS 9
#define BUTTONWIDTH 40
#define BUTTONHEIGHT 30


void locateTitle(Widget, Widget, Widget);
void locateFtFl(Widget);
void locateFtFr(Widget);
void locateFtWl(Widget, Widget);
void locateFtWr(Widget, Widget);
void locateWtFl(Widget, Widget);
void locateWtFr(Widget, Widget);
void locateWtWl(Widget, Widget, Widget);
void locateWtWr(Widget, Widget, Widget);

bool isInMainGroups(int, int);
void button_handler(Widget, int*, XEvent *, char*);

Widget xs_create_quit_button(Widget);
void arm_callback(Widget, int *, XmAnyCallbackStruct *);
void activate_callback(Widget, int *, XmAnyCallbackStruct *);
void disarm_callback(Widget, int *, XmAnyCallbackStruct *);

const TablePointer tp;
char* crLabel[] = {"1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "11", "12", "13", "14", "15", "16", "17", "18"};
char name[2];


main(int argc, char *argv[])
{
    Widget toplevel, form;
    Widget elemButton[TOTALELEMENTS], colNum[TOTALCOLUMNS], rowNum[TOTALROWS], title, quitButton;
    int i, j, n, r, c;
    int atomNum[TOTALELEMENTS];
    int ww = BUTTONWIDTH*(TOTALCOLUMNS+1);
    int wh = BUTTONHEIGHT*(TOTALROWS+1);
    static Arg wargs[] = {{XtNwidth, ww}, {XtNheight, wh}, {XtNforeground, 30}, {XtNbackground, 233}};
    n = 4;
    
    cout << "\nLoading data. Please wait...\n\n";

    // Initialize the Intrinsics.
    toplevel = XtInitialize(argv[0], "PeriodicTable", NULL, 0, &argc, argv);

    // Create the XmForm widget "form"  that manages 
    // all the widgets in this periodic table.
    form = XtCreateManagedWidget("form", xmFormWidgetClass, toplevel, wargs, n);

    // Create labels that can display the column number.    
    for(i=0;i<TOTALCOLUMNS;i++)
      {
	colNum[i] = XtCreateManagedWidget(crLabel[i], xmLabelWidgetClass, form, NULL, 0);
      }

    // Create labels that can display the row number.    
    for(i=0;i<TOTALROWS;i++)
      {
	rowNum[i] = XtCreateManagedWidget(crLabel[i], xmLabelWidgetClass, form, NULL, 0);
      }

    // Create push button and assign an XmNarmCallback, XmNdisarmCallback, 
    // and XmNactivateCallback to this quit button so that you can quit
    // this program when you push the button.
    quitButton = xs_create_quit_button(form);

    // Create a title label to display the title. 
    title = XtCreateManagedWidget(" Periodic Table   ---   By Zhao, Hong ", xmLabelWidgetClass, form, NULL, 0);

    // Create push buttons which stand for elements in the periodic table, 
    // and assign each button a event handler so that you can get all the 
    // information about the element by pushing it.
    for(i=0;i<TOTALELEMENTS;i++)
      {
	name[0] = tp.getPName()[i][0];
	name[1] = tp.getPName()[i][1];
	elemButton[i] = XtCreateWidget(name, xmPushButtonWidgetClass, form, NULL, 0);
	atomNum[i] = i+1;
	XtAddEventHandler(elemButton[i],ButtonPressMask,FALSE,button_handler,&atomNum[i]);
      }


    
    XtManageChildren(colNum, TOTALCOLUMNS);
    XtManageChildren(rowNum, TOTALROWS);
    XtManageChildren(elemButton, TOTALELEMENTS);


    // Locate column number labels.      
    locateFtFr(colNum[TOTALCOLUMNS-1]);
    for(c=TOTALCOLUMNS-1;c>=1;c--) locateFtWr(colNum[c], colNum[c-1]);

    // Locate row number labels.      
    locateWtFl(colNum[0], rowNum[0]);
    for(r=2;r<=TOTALROWS;r++) locateWtFl(rowNum[r-2], rowNum[r-1]);
        
    // Locate element push buttons row by row.      
    locateWtWl(colNum[0], rowNum[0], elemButton[0]);
    locateWtFr(colNum[17], elemButton[1]);
    
    for(r=2;r<=3;r++)
      {
	locateWtWl(elemButton[(r-2)*2], rowNum[r-1], elemButton[(r-2)*8+2]);
	locateWtWl(elemButton[(r-2)*2], elemButton[(r-2)*8+2], elemButton[(r-2)*8+3]);
	locateWtFr(elemButton[(r-2)*2], elemButton[1+(r-1)*8]);
      
	for(c=7;c>=3;c--)
	  locateWtWr(elemButton[(r-2)*2],elemButton[(r-2)*8+c+2],elemButton[(r-2)*8+c+1]);
      }
        
    for(r=4;r<=5;r++)
      {
	locateWtWl(elemButton[10+(r-4)*8], rowNum[r-1], elemButton[(r-3)*18]);
	for(c=2;c<=18;c++)
	  locateWtWl(elemButton[10+(r-4)*8], elemButton[(r-4)*18+c+16], elemButton[(r-4)*18+c+17]);
      }
    
    locateWtWl(elemButton[36], rowNum[5], elemButton[54]);    
    for(c=2;c<=3;c++)
      locateWtWl(elemButton[36], elemButton[52+c], elemButton[53+c]);    
    locateWtWl(elemButton[36], elemButton[56], elemButton[71]);
    for(c=5;c<=18;c++)
      locateWtWl(elemButton[36], elemButton[66+c], elemButton[67+c]);

    locateWtWl(elemButton[54], rowNum[6], elemButton[86]);    
    for(c=2;c<=3;c++)
      locateWtWl(elemButton[54], elemButton[84+c], elemButton[85+c]);
    locateWtWl(elemButton[54], elemButton[88], elemButton[103]);
    for(c=5;c<=9;c++)
      locateWtWl(elemButton[54], elemButton[98+c], elemButton[99+c]);
    
    for(r=8;r<=9;r++)
      {
	locateWtWl(elemButton[103-(r-8)*46], elemButton[88], elemButton[57+(r-8)*32]);
	for(c=5;c<=17;c++)
	  locateWtWl(elemButton[103-(r-8)*46], elemButton[52+c+(r-8)*32], elemButton[53+c+(r-8)*32]);
      }

    // Locate title label.      
    locateTitle(colNum[8], elemButton[21], title);

    // Locate quit button.      
    locateWtWl(title, colNum[6], quitButton);
    

    XtRealizeWidget(toplevel);
    XtMainLoop();
}





// Locate title label.      
void locateTitle(Widget topWidget, Widget leftWidget, Widget titleLabel)
{
  int n;
  Arg wargs[10];
  
  n = 0;
  XtSetArg(wargs[n], XtNwidth, 280); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNtopWidget, topWidget); n++;
  XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNleftWidget, leftWidget); n++;
  XtSetValues(titleLabel, wargs, n);
} 

// Locate the widget which is attatched to the top and the left of the form.
void locateFtFl(Widget widget)
{
  int n;
  Arg wargs[10];

  n = 0;
  XtSetArg(wargs[n], XtNwidth, BUTTONWIDTH); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); n++;
  XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); n++;
  XtSetValues(widget, wargs, n);
}

// Locate the widget which is attatched to the top and the right of the form.
void locateFtFr(Widget widget)
{
  int n;
  Arg wargs[10];

  n = 0;
  XtSetArg(wargs[n], XtNwidth, BUTTONWIDTH); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); n++;
  XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); n++;
  XtSetValues(widget, wargs, n);
}

// Locate the widget which is attatched to the top of the form
// and the right of leftWidget.
void locateFtWl(Widget leftWidget, Widget widget)
{
  int n;
  Arg wargs[10];
  n = 0;
  XtSetArg(wargs[n], XtNwidth, BUTTONWIDTH); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); n++;
  XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNleftWidget, leftWidget); n++;
  XtSetValues(widget, wargs, n);
}

// Locate the widget which is attatched to the top of the form
// and the left of rightWidget.
void locateFtWr(Widget rightWidget, Widget widget)
{
  int n;
  Arg wargs[10];
  n = 0;
  XtSetArg(wargs[n], XtNwidth, BUTTONWIDTH); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_FORM); n++;
  XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNrightWidget, rightWidget); n++;
  XtSetValues(widget, wargs, n);
}

// Locate the widget which is attatched to the left of the form
// and the bottom of topWidget.
void locateWtFl(Widget topWidget, Widget widget)
{
  int n;
  Arg wargs[10];
  n = 0;
  XtSetArg(wargs[n], XtNwidth, BUTTONWIDTH); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNtopWidget, topWidget); n++;
  XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_FORM); n++;
  XtSetValues(widget, wargs, n);
}

// Locate the widget which is attatched to the right of the form
// and the bottom of topWidget.
void locateWtFr(Widget topWidget, Widget widget)
{
  int n;
  Arg wargs[10];
  n = 0;
  XtSetArg(wargs[n], XtNwidth, BUTTONWIDTH); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNtopWidget, topWidget); n++;
  XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); n++;
  XtSetValues(widget, wargs, n);
}

// Locate the widget which is attatched to the bottom of topWidget
// and the right of leftWidget.
void locateWtWl(Widget topWidget, Widget leftWidget, Widget widget)
{
  int n;
  Arg wargs[10];
  n = 0;
  XtSetArg(wargs[n], XtNwidth, BUTTONWIDTH); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNtopWidget, topWidget); n++;
  XtSetArg(wargs[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNleftWidget, leftWidget); n++;
  XtSetValues(widget, wargs, n);
}

// Locate the widget which is attatched to the bottom of topWidget
// and the left of rightWidget.
void locateWtWr(Widget topWidget, Widget rightWidget, Widget widget)
{
  int n;
  Arg wargs[10];
  n = 0;
  XtSetArg(wargs[n], XtNwidth, BUTTONWIDTH); n++;
  XtSetArg(wargs[n], XtNheight, BUTTONHEIGHT); n++;
  XtSetArg(wargs[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNtopWidget, topWidget); n++;
  XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
  XtSetArg(wargs[n], XmNrightWidget, rightWidget); n++;
  XtSetValues(widget, wargs, n);
}


// tell if the element is in main group.
bool isInMainGroups(int rowNumber, int columnNumber)
{
  if(columnNumber<=2 || (columnNumber>=13 && rowNumber<=6)) return true;
  else return false;
}




// event handler of the element buttons.
void button_handler(Widget w, int* an, XEvent *call_data, char* c)
{
  int rn = tp.getPRowNumber()[*an-1];
  int cn = tp.getPColumnNumber()[*an-1];

  if(isInMainGroups(rn, cn))
    {
      // construct an element by its atomic number.
      ElemInMainGroups elemMain(*an, tp); 

      //print all the information about this element.
      elemMain.printAllInfo();
    }
  else
    {
      // construct a main_group_element by its atomic number.
      Element element(*an, tp);

      //print all the information about this element.
      element.printInfo();
    }
}




// Function to add a quit button as a child of any widget.
Widget xs_create_quit_button(Widget parent)
{
  Widget w;
  static int really_quit;
  w = XtCreateManagedWidget("quit", xmPushButtonWidgetClass, parent, NULL, 0);
  XtAddCallback(w, XmNarmCallback, arm_callback, &really_quit);
  XtAddCallback(w, XmNdisarmCallback, disarm_callback, &really_quit);
  XtAddCallback(w, XmNactivateCallback, activate_callback, &really_quit);
  return(w);
}



// XmNarmCallback of the quit button.
void arm_callback(Widget w, int *flag, XmAnyCallbackStruct *call_data)
{
  *flag = FALSE;
}

// XmNdisarmCallback of the quit button.
void activate_callback(Widget w, int *flag, XmAnyCallbackStruct *call_data)
{
  *flag = TRUE;
}

// XmNactivateCallback of the quit button.
void disarm_callback(Widget w, int *flag, XmAnyCallbackStruct *call_data)
{
  if(*flag)
    {
      cout << "\n\n      *********************  Thank you.  Bye!  **************************\n\n\n";
      XtCloseDisplay(XtDisplay(w));
      exit(0);
    }
}




