// Common procedures for all GL operations performed
// on an X orm window
//
// Eric Amram, Oct 1997
//
// --- Templated functions ----

// WARNING : As SGI compiler CC messes everything up with templates
// I use an option that look for the template in the .C corresponding
// to the header where the template class is defined
// This MUST bear the same name as the .h


#include "glcanvas.H"
#include "glforms.H"



// ======================================================================
// ==================== XForms class methods ============================
// ======================================================================

// Set up the Form major functionalities
template <class FD_NAME> void
XForms<FD_NAME>::
initFormsInteraction(char *title)
{
  int i;

  
  // Create the form now : open the window, etc... Thank you, XForms !
  Xform = create_form_NAME();

  // Little hack since NAME is not fixed and is the name of the form variable
  TheForm = *((FL_FORM**)Xform);  // Always first in struct
  // As fdesign source is in C, names are "hard-coded". Thank you, XForms !
  
  // Let the show begin...
  fl_show_form( TheForm, 
		FL_PLACE_CENTER | FL_FREE_SIZE, // for sizing, add | FL_FREE_SIZE
		FL_FULLBORDER, 
		title );

  // Init all GL canvases 
  for(i=0; i<numGLcan; i++)
    GLcan[i].initGL( displayClas[i], numGLcan, i, Xform->GLCanvas[i] );

  // Add now handlers for expose/mouse events in GL canvases
  for(i=0; i<numGLcan; i++) {  // Another loop because render() is for all GLcan
    fl_add_canvas_handler(Xform->GLCanvas[i], Expose, TempHandleEvent, 
			  (void *)(&(GLcan[i])));
    if (displayClas[i]->interactionMode != GLCAN_INTERACT_MODE_NONE) {
      fl_add_canvas_handler(Xform->GLCanvas[i], 
							ButtonPress,   TempHandleEvent, 
							(void *)(&(GLcan[i])));
      fl_add_canvas_handler(Xform->GLCanvas[i], 
							ButtonRelease, TempHandleEvent,
							(void *)(&(GLcan[i])));
      fl_add_canvas_handler(Xform->GLCanvas[i], 
							MotionNotify,  TempHandleEvent,
							(void *)(&(GLcan[i])));
      fl_add_canvas_handler(Xform->GLCanvas[i], 
							ResizeRequest, TempHandleEvent,
							(void *)(&(GLcan[i])));
    }
	// suppress motion notifications when no mouse button is down
	// XXX may wish to handle these directly, for "anticipatory" UI
    fl_remove_selected_xevent(fl_get_canvas_id(Xform->GLCanvas[i]), 
			      PointerMotionMask|PointerMotionHintMask);
  }
  // Expose events trigger rendering
  
  return;
}

  
// Main loop interaction. Split from initFormsInteraction for multi-forms use
template <class FD_NAME> void
XForms<FD_NAME>::
mainLoopInteraction()
{ 
  FL_OBJECT *obj;
  do {
    obj=fl_do_forms(); // Main loop - exit button has no callback function
    fl_hide_form(obj->form);    
  } while (obj->form != TheForm);
  
  return;
}
