#include <stdlib.h>
#include <stdio.h>

#include <xclass/OXClient.h>
#include <xclass/OXWindow.h>
#include <xclass/OXFrame.h>
#include <xclass/OXWidgets.h>
#include <xclass/OString.h>
#include <xclass/utils.h>

#include "ecran.xpm"
#include "es.xpm"

class OXTestMainFrame : public OXMainFrame {
public:
  OXTestMainFrame(const OXWindow *p, int w, int h);
  virtual ~OXTestMainFrame();

  virtual int ProcessMessage(long msg, long parm1, long parm2);

protected:
  OLayoutHints *CommonCLayout, *CommonXLayout;
  OLayoutHints *CommonTLayout, *CommonBLayout;
  OLayoutHints *CommonLLayout, *CommonRLayout;

  OXTab *Main;
  OXCompositeFrame *F1, *F1A, *F2, *F3, *F4;
  OXGroupFrame *F1G1, *F1G2, *F2G1, *F2G2;
  OXCompositeFrame *F2G2A, *F2G2B;
  OLayoutHints *TabLayout;

  OXCompositeFrame *Buttons;
  OXButton *OKButton, *CancelButton, *ApplyButton;
  OLayoutHints *ButtonLayout, *ButtonFrameLayout;
};

OXClient *clientX;
OXTestMainFrame *mainWindow;

OPicturePool *Pictures;

main() {

  clientX = new OXClient;

  Pictures = new OPicturePool(clientX, ".");

  Debug(DBG_INFO, "Creating main window...\n");

  mainWindow = new OXTestMainFrame(clientX->GetRoot(), 500, 450);
  mainWindow->MapWindow();

  Debug(DBG_INFO, "Running...\n");

  clientX->Run();
}

OXTestMainFrame::OXTestMainFrame(const OXWindow *p, int w, int h) :
  OXMainFrame(p, w, h) {

  const OPicture *Energy_Star = Pictures->GetPicture("es.xpm", es);
  if (!Energy_Star) FatalError("Pixmap not found: es.xpm");

  const OPicture *Monitor = Pictures->GetPicture("ecran.xpm", ecran);
  if (!Monitor) FatalError("Pixmap not found: ecran.xpm");

  OXCompositeFrame *tmp_frame;

  CommonCLayout = new OLayoutHints(LHINTS_CENTER_X | LHINTS_CENTER_Y, 2, 2, 2, 2);
  CommonXLayout = new OLayoutHints(LHINTS_EXPAND_X | LHINTS_EXPAND_Y, 2, 2, 2, 2);
  CommonTLayout = new OLayoutHints(LHINTS_TOP | LHINTS_CENTER_X, 2, 2, 2, 2);
  CommonBLayout = new OLayoutHints(LHINTS_BOTTOM | LHINTS_CENTER_X, 2, 2, 2, 2);
  CommonLLayout = new OLayoutHints(LHINTS_LEFT | LHINTS_CENTER_Y, 2, 2, 2, 2);
  CommonRLayout = new OLayoutHints(LHINTS_LEFT | LHINTS_CENTER_Y, 2, 2, 2, 2);

  Buttons = new OXCompositeFrame(this, 250, 20, HORIZONTAL_FRAME | FIXED_WIDTH);

    OKButton = new OXTextButton(Buttons, new OHotString("&Ok"), 1);
    OKButton->Associate(this);
    CancelButton = new OXTextButton(Buttons, new OHotString("&Annuler"), 2);
    CancelButton->Associate(this);
    ApplyButton = new OXTextButton(Buttons, new OHotString("A&ppliquer"), 3);
    ApplyButton->Associate(this);

  int width = OKButton->GetDefaultWidth();
  width = max(width, CancelButton->GetDefaultWidth());
  width = max(width, ApplyButton->GetDefaultWidth());

  Buttons->Resize((width+15)*3, Buttons->GetDefaultHeight());

    ButtonLayout = new OLayoutHints(LHINTS_EXPAND_X,
                          5, 0, 0, 0);
    Buttons->AddFrame(OKButton, ButtonLayout);
    Buttons->AddFrame(CancelButton, ButtonLayout);
    Buttons->AddFrame(ApplyButton, ButtonLayout);

    ButtonFrameLayout = new OLayoutHints(LHINTS_BOTTOM | LHINTS_RIGHT,
                                         0, 2, 3, 3);
    AddFrame(Buttons, ButtonFrameLayout);

  Main = new OXTab(this, 10, 10);

    TabLayout = new OLayoutHints( LHINTS_EXPAND_X | LHINTS_EXPAND_Y,
					5, 5, 5, 5);

    tmp_frame = Main->AddTab(new OString("Arrière-plan"));
    F1 = new OXCompositeFrame(tmp_frame, 460, 300, VERTICAL_FRAME);
      F1->AddFrame(new OXIcon(F1, Monitor, 185, 170), CommonTLayout);
      F1->AddFrame(F1A = new OXCompositeFrame(F1, 1, 1, HORIZONTAL_FRAME),
						CommonXLayout);
        F1A->AddFrame(F1G1 = new OXGroupFrame(F1A, new OString("Motif")),
			CommonXLayout);
        F1A->AddFrame(F1G2 = new OXGroupFrame(F1A, new OString("Papier Peint")),
			CommonXLayout);
    tmp_frame->AddFrame(F1,TabLayout);

    tmp_frame = Main->AddTab(new OString("Ecran de veille"));
    F2 = new OXCompositeFrame(tmp_frame, 460, 300, VERTICAL_FRAME);
      F2->AddFrame(new OXIcon(F2, Monitor, 185, 170), CommonTLayout);
      F2->AddFrame(F2G1 = new OXGroupFrame(F2, new OString("Ecran de veille")),
			CommonXLayout);
      F2->AddFrame(F2G2 = new OXGroupFrame(F2, new OString("Fonctions pour l'économie du moniteur")),
			CommonXLayout);
       F2G2->AddFrame(F2G2A = new OXCompositeFrame(F2G2, 1, 1, HORIZONTAL_FRAME),
							CommonXLayout);
        F2G2A->AddFrame(new OXIcon(F2G2A, Energy_Star, 71, 42),
			CommonCLayout);
        F2G2A->AddFrame(F2G2B = new OXCompositeFrame(F2G2A, 1, 1, VERTICAL_FRAME),
			CommonXLayout);
         F2G2B->AddFrame(new OXCheckButton(F2G2B, new OHotString("Mise en &veille. Alimentation minimale"), 0),
			 CommonLLayout);
         F2G2B->AddFrame(new OXCheckButton(F2G2B, new OHotString("Désactivation du moniteur"), 0),
			 CommonLLayout);
    tmp_frame->AddFrame(F2,TabLayout);

    tmp_frame = Main->AddTab(new OString("Apparence"));
    F3 = new OXCompositeFrame(tmp_frame, 460, 300, VERTICAL_FRAME);
      F3->AddFrame(new OXCheckButton(F3, new OHotString("&Test"), 0),
	 		CommonCLayout);
    tmp_frame->AddFrame(F3,TabLayout);

    tmp_frame = Main->AddTab(new OString("Configuration"));
    F4 = new OXCompositeFrame(tmp_frame, 460, 300, VERTICAL_FRAME);
      F4->AddFrame(new OXIcon(F4, Monitor, 185, 170), CommonTLayout);
      F4->AddFrame(new OXTextButton(F4, new OHotString("&Test"), 0),
	 		CommonXLayout);
    tmp_frame->AddFrame(F4,TabLayout);

    AddFrame(Main, CommonXLayout);

  SetWindowName("WinTest");

  MapSubwindows();

//  Resize(GetDefaultSize());

  MapWindow();

  Layout();

  clientX->Run();
}

OXTestMainFrame::~OXTestMainFrame() {
  delete OKButton;
  delete CancelButton;
  delete ApplyButton;
  delete Buttons;
}

int OXTestMainFrame::ProcessMessage(long msg, long parm1, long parm2) {

  switch (GET_MSG(msg)) {

    case XC_COMMAND:    // buttons and menu commands
      switch(GET_SUBMSG(msg)) {

        case CM_BUTTON:
          Debug(DBG_INFO, "\nButton was pressed, ID=%d\n", parm1);
          break;

        case CM_MENUSELECT:
          Debug(DBG_INFO, "Pointer over menu entry, ID=%d\n", parm1);
          break;

        case CM_MENU:
          switch(parm1) {
            default:
              Debug(DBG_INFO, "\nID: %d\n", parm1);
              break;
          }
          break;

        default:
          break;

      }
      break;

    default:
      break;

  }

  return True;
}
