/*
 *    SCCS ID: %Z%%M% %I% %E% SST
 *
 *    Copyright (c) 1992-1993 Mark Soloway.  All rights reserved.
 *    See SST_LEGAL_NOTICE file for terms of the license.
 */

#include "CommandMenu.h"
#include "BaseWindow.h"
#include "Image.h"
#include "ComponentDisplay.h"
#include "Button.h"
#include "FileChooser.h"
#include "AlphanumericInput.h"
#include "ListChoice.h"
#include "PopUpWindow.h"
#include "Notifier.h"
#include "object_manager_ui.h"

#include "TTSession.h"

extern void create_object_handler (UIObject *);
extern void delete_object (UIObject *);
extern void edit_object_properties_handler (UIObject *);
extern void add_property_handler (UIObject *);
extern void set_property_handler (UIObject *);
extern void delete_property_handler (UIObject *);
extern void file_notify_handler (UIObject *);
extern void object_list_handler (UIObject *);
extern void values_button_notify_handler (UIObject *);
extern void property_list_handler (UIObject *);
extern void add_property (UIObject *);
extern void add_property_cancel_notify_handler (UIObject *);
extern void create_object_cancel_handler (UIObject *);
extern void create_object (UIObject *);
#if (XV_VERSION_NUMBER >= 3200)
extern void file_button_handler (UIObject *);
extern void file_chooser_handler (UIObject *);
#endif

void main (int argc, char **argv)
{
  BaseWindow bw;
  bw.initUI (argc, argv);
  bw.setWidth (400);
  bw.setHeight (279);
  bw.setLabel ("ToolTalk Object Manager");
  bw.show (TRUE);
  bw.setDisplayFooter (TRUE);
  bw.setResizable (TRUE);

  static unsigned short icon_bits [] = { 
#include "object_manager.icon" 
  }; 
  Image icon (64, 64, icon_bits);
  bw.setIcon (icon);

  static unsigned short icon_mask_bits [] = { 
#include "object_manager_mask.icon"
  }; 
  Image icon_mask (64, 64, icon_mask_bits);
  bw.setIconMask (icon_mask);

  CommandMenu edit_object_menu;
  CommandMenu edit_property_menu;

  edit_object_menu.setDisplayedColumns (1);
  edit_object_menu.setLabel ("Edit");
  (void) edit_object_menu.addItem ("Create Object...");
  edit_object_menu.setItemHandler (1, create_object_handler);
  (void) edit_object_menu.addItem ("Delete Object");
  edit_object_menu.setItemHandler (2, delete_object);
  (void) edit_object_menu.addItem ("Edit Object Properties...");
  edit_object_menu.setItemHandler (3, edit_object_properties_handler);
  edit_object_menu.setPinnable (TRUE);

  edit_property_menu.setDisplayedColumns (1);
  (void) edit_property_menu.addItem ("Add Property...");
  edit_property_menu.setItemHandler (1, add_property_handler);
  (void) edit_property_menu.addItem ("Set Property...");
  edit_property_menu.setItemHandler (2, set_property_handler);
  (void) edit_property_menu.addItem ("Delete Property");
  edit_property_menu.setItemHandler (3, delete_property_handler);
  edit_property_menu.setPinnable (TRUE);

  ComponentDisplay controls1 (TRUE);
  controls1.setX (0);
  controls1.setY (0);
  controls1.setExtendToEdge (WIDTH);
  controls1.setExtendToEdge (HEIGHT);
  controls1.setDisplayBorders (FALSE);
  bw.addDisplay (controls1);

#if (XV_VERSION_NUMBER >= 3200)
  Button file_button;
  file_button.setX (270);
  file_button.setY (12);
  file_button.setLabel ("file...");
  file_button.setNotifyHandler (file_button_handler);
  controls1.addComponent (file_button);

  FileChooser file_chooser ("Specify file to inspect");
  file_chooser.setType (FILE_CHOOSER_OPEN);
  file_chooser.setNotifyHandler (file_chooser_handler);
#endif

  Button edit_object_button;
  edit_object_button.setX (325);
  edit_object_button.setY (12);
  edit_object_button.setLabel ("Edit");
  edit_object_button.setMenu (edit_object_menu);
  controls1.addComponent (edit_object_button);

  AlphanumericInput file;
  file.setX (10);
  file.setY (14);
  file.setInputDisplayLength (25);
  file.setMaxInputLength (256);
  file.setLabel ("File:");
  file.setLabelPosition (LEFT);
  file.setReadOnly (FALSE);
  file.setNotifyHandler (file_notify_handler);
  controls1.addComponent (file);

  ListChoice object_list;
  object_list.setX (10);
  object_list.setY (50);
  object_list.setWidth (350);
  object_list.setDisplayedRows (10);
  object_list.setTitle ("Objects");
  object_list.setLabelPosition (LEFT);
  object_list.setReadOnly (TRUE);
  object_list.setMultipleChoice (FALSE);
  object_list.setSelectionRequired (TRUE);
  object_list.setNotifyHandler (object_list_handler);
  controls1.addComponent (object_list);

  PopUpWindow edit_object_properties;
  edit_object_properties.setWidth (551);
  edit_object_properties.setHeight (234);
  edit_object_properties.setLabel ("Edit Object Properties");
  edit_object_properties.show (FALSE);
  edit_object_properties.setDisplayFooter (TRUE);
  edit_object_properties.setResizable (TRUE);
  edit_object_properties.setPinned (FALSE);
  bw.addChildWindow (edit_object_properties);

  ComponentDisplay controls2 (TRUE);
  controls2.setX (0);
  controls2.setY (0);
  controls2.setExtendToEdge (WIDTH);
  controls2.setExtendToEdge (HEIGHT);
  controls2.setDisplayBorders (FALSE);
  edit_object_properties.addDisplay (controls2);

  Button edit_property_button;
  edit_property_button.setX (390);
  edit_property_button.setY (9);
  edit_property_button.setLabel ("Edit");
  edit_property_button.setMenu (edit_property_menu);
  controls2.addComponent (edit_property_button);

  Button property_values_button;
  property_values_button.setX (460);
  property_values_button.setY (9);
  property_values_button.setLabel ("Values...");
  property_values_button.setNotifyHandler (values_button_notify_handler);
  controls2.addComponent (property_values_button);

  AlphanumericInput object;
  object.setX (10);
  object.setY (11);
  object.setInputDisplayLength (35);
  object.setMaxInputLength (256);
  object.setLabel ("Object:");
  object.setLabelPosition (LEFT);
  object.setReadOnly (TRUE);
  controls2.addComponent (object);

  AlphanumericInput edit_object_object_type;
  edit_object_object_type.setX (10);
  edit_object_object_type.setY (40);
  edit_object_object_type.setInputDisplayLength (50);
  edit_object_object_type.setMaxInputLength (256);
  edit_object_object_type.setLabel ("Object Type:");
  edit_object_object_type.setLabelPosition (LEFT);
  edit_object_object_type.setReadOnly (TRUE);
  controls2.addComponent (edit_object_object_type);

  ListChoice property_list;
  property_list.setX (10);
  property_list.setY (70);
  property_list.setWidth (500);
  property_list.setDisplayedRows (6);
  property_list.setTitle ("Object Properties");
  property_list.setLabelPosition (LEFT);
  property_list.setReadOnly (TRUE);
  property_list.setMultipleChoice (FALSE);
  property_list.setSelectionRequired (TRUE);
  property_list.setNotifyHandler (property_list_handler);
  controls2.addComponent (property_list);

  PopUpWindow change_property_popup;
  change_property_popup.setWidth (338);
  change_property_popup.setHeight (117);
  change_property_popup.show (FALSE);
  change_property_popup.setDisplayFooter (TRUE);
  change_property_popup.setResizable (TRUE);
  change_property_popup.setPinned (FALSE);
  bw.addChildWindow (change_property_popup);

  ComponentDisplay controls3 (TRUE);
  controls3.setX (0);
  controls3.setY (0);
  controls3.setExtendToEdge (WIDTH);
  controls3.setExtendToEdge (HEIGHT);
  controls3.setDisplayBorders (FALSE);
  change_property_popup.addDisplay (controls3);

  AlphanumericInput add_property_name;
  add_property_name.setX (20);
  add_property_name.setY (20);
  add_property_name.setInputDisplayLength (30);
  add_property_name.setMaxInputLength (256);
  add_property_name.setLabel ("Name:");
  add_property_name.setLabelPosition (LEFT);
  add_property_name.setReadOnly (FALSE);
  controls3.addComponent (add_property_name);

  AlphanumericInput add_property_value;
  add_property_value.setX (20);
  add_property_value.setY (50);
  add_property_value.setInputDisplayLength (30);
  add_property_value.setMaxInputLength (256);
  add_property_value.setLabel ("Value:");
  add_property_value.setLabelPosition (LEFT);
  add_property_value.setReadOnly (FALSE);
  controls3.addComponent (add_property_value);

  Button add_property_apply;
  add_property_apply.setX (110);
  add_property_apply.setY (80);
  add_property_apply.setLabel ("Apply");
  add_property_apply.setNotifyHandler (add_property);
  controls3.addComponent (add_property_apply);

  Button add_property_cancel;
  add_property_cancel.setX (170);
  add_property_cancel.setY (80);
  add_property_cancel.setLabel ("Cancel");
  add_property_cancel.setNotifyHandler (add_property_cancel_notify_handler);
  controls3.addComponent (add_property_cancel);

  PopUpWindow property_values_popup;
  property_values_popup.setWidth (442);
  property_values_popup.setHeight (219);
  property_values_popup.setLabel ("Object Property Values");
  property_values_popup.show (FALSE);
  property_values_popup.setDisplayFooter (TRUE);
  property_values_popup.setResizable (TRUE);
  property_values_popup.setPinned (FALSE);
  bw.addChildWindow (property_values_popup);

  ComponentDisplay controls4 (TRUE);
  controls4.setX (0);
  controls4.setY (0);
  controls4.setExtendToEdge (WIDTH);
  controls4.setExtendToEdge (HEIGHT);
  controls4.setDisplayBorders (FALSE);
  property_values_popup.addDisplay (controls4);

  AlphanumericInput property_values_object;
  property_values_object.setX (66);
  property_values_object.setY (20);
  property_values_object.setInputDisplayLength (35);
  property_values_object.setMaxInputLength (256);
  property_values_object.setLabel ("Object:");
  property_values_object.setLabelPosition (LEFT);
  property_values_object.setReadOnly (TRUE);
  controls4.addComponent (property_values_object);

  AlphanumericInput property_values_name;
  property_values_name.setX (10);
  property_values_name.setY (45);
  property_values_name.setInputDisplayLength (35);
  property_values_name.setMaxInputLength (256);
  property_values_name.setLabel ("Property Name:");
  property_values_name.setLabelPosition (LEFT);
  property_values_name.setReadOnly (FALSE);
  controls4.addComponent (property_values_name);

  ListChoice property_values_list;
  property_values_list.setX (10);
  property_values_list.setY (70);
  property_values_list.setWidth (400);
  property_values_list.setDisplayedRows (5);
  property_values_list.setTitle ("Property Values");
  property_values_list.setLabelPosition (LEFT);
  property_values_list.setReadOnly (TRUE);
  property_values_list.setMultipleChoice (FALSE);
  property_values_list.setSelectionRequired (FALSE);
  controls4.addComponent (property_values_list);

  PopUpWindow create_object_popup;
  create_object_popup.setWidth (309);
  create_object_popup.setHeight (82);
  create_object_popup.setLabel ("Create Object");
  create_object_popup.show (FALSE);
  create_object_popup.setDisplayFooter (TRUE);
  create_object_popup.setResizable (TRUE);
  create_object_popup.setPinned (FALSE);
  bw.addChildWindow (create_object_popup);

  ComponentDisplay controls5 (TRUE);
  controls5.setX (0);
  controls5.setY (0);
  controls5.setExtendToEdge (WIDTH);
  controls5.setExtendToEdge (HEIGHT);
  controls5.setDisplayBorders (FALSE);
  create_object_popup.addDisplay (controls5);

  AlphanumericInput object_type;
  object_type.setX (10);
  object_type.setY (20);
  object_type.setInputDisplayLength (25);
  object_type.setMaxInputLength (256);
  object_type.setLabel ("Object Type:");
  object_type.setLabelPosition (LEFT);
  object_type.setReadOnly (FALSE);
  controls5.addComponent (object_type);

  Button create_object_apply;
  create_object_apply.setX (90);
  create_object_apply.setY (50);
  create_object_apply.setLabel ("Apply");
  create_object_apply.setNotifyHandler (create_object);
  controls5.addComponent (create_object_apply);

  Button create_object_cancel;
  create_object_cancel.setX (150);
  create_object_cancel.setY (50);
  create_object_cancel.setLabel ("Cancel");
  create_object_cancel.setNotifyHandler (create_object_cancel_handler);
  controls5.addComponent (create_object_cancel);

#if (XV_VERSION_NUMBER >= 3200)
  file_button.setObjectData (FILE_CHOOSER_ADDRESS, file_chooser);
  file_chooser.setObjectData (OBJECT_FILE, file);
#endif
 
  object_list.setObjectData (EDIT_OBJECT_PROPERTIES, edit_object_properties);

  edit_object_menu.setObjectData (OBJECT, object);
  edit_object_menu.setObjectData (OBJECT_LIST, object_list);
  edit_object_menu.setObjectData (OBJECT_FILE, file);
  edit_object_menu.setObjectData (CREATE_OBJECT_POPUP, create_object_popup);
  edit_object_menu.setObjectData (PROPERTY_OBJECT, property_values_object);
  edit_object_menu.setObjectData (EDIT_OBJECT_PROPERTIES, edit_object_properties);
  edit_object_menu.setObjectData (PROPERTY_VALUES_POPUP, property_values_popup);

  edit_object_properties.setObjectData (OBJECT, object);
  edit_object_properties.setObjectData (OBJECT_TYPE, edit_object_object_type);
  edit_object_properties.setObjectData (OBJECT_PROPERTY_LIST, property_list);

  edit_property_menu.setObjectData (OBJECT, object);
  edit_property_menu.setObjectData (OBJECT_PROPERTY_LIST, property_list);
  edit_property_menu.setObjectData (ADD_PROPERTY_APPLY, add_property_apply);
  edit_property_menu.setObjectData (CHANGE_PROPERTY_POPUP, change_property_popup);
  edit_property_menu.setObjectData (PROPERTY_NAME, property_values_name);
  edit_property_menu.setObjectData (PROPERTY_VALUES_POPUP, property_values_popup);

  file.setObjectData (OBJECT_LIST, object_list);

  property_values_popup.setObjectData (PROPERTY_OBJECT, property_values_object);
  property_values_popup.setObjectData (PROPERTY_NAME, property_values_name);
  property_values_popup.setObjectData (PROPERTY_VALUES, property_values_list);

  property_list.setObjectData (OBJECT, object);
  property_list.setObjectData (PROPERTY_VALUES_POPUP, property_values_popup);

  property_values_button.setObjectData (OBJECT, object);
  property_values_button.setObjectData (OBJECT_PROPERTY_LIST, property_list);
  property_values_button.setObjectData (PROPERTY_VALUES_POPUP, property_values_popup);

  create_object_apply.setObjectData (OBJECT_FILE, file);
  create_object_apply.setObjectData (OBJECT_TYPE, object_type);
  create_object_apply.setObjectData (CREATE_OBJECT_POPUP, create_object_popup);

  create_object_cancel.setObjectData (CREATE_OBJECT_POPUP, create_object_popup);

  add_property_apply.setObjectData (OBJECT, object);
  add_property_apply.setObjectData (OBJECT_PROPERTY_LIST, property_list);
  add_property_apply.setObjectData (PROPERTY_NAME, property_values_name);
  add_property_apply.setObjectData (ADD_PROPERTY_NAME, add_property_name);
  add_property_apply.setObjectData (ADD_PROPERTY_VALUE, add_property_value);
  add_property_apply.setObjectData (CHANGE_PROPERTY_POPUP, change_property_popup);
  add_property_apply.setObjectData (PROPERTY_VALUES_POPUP, property_values_popup);

  add_property_cancel.setObjectData (CHANGE_PROPERTY_POPUP, change_property_popup);

  int       mark = tt_mark ();
  Tt_status status;
  TTSession session (status);

  if (status != TT_OK) {
    printf ("Error initializing ToolTalk: %d\n", status);
  }
  else {
    Notifier notifier;
    notifier.start ();
  }

  tt_release (mark);
  exit (0);
}
