///////////////////////////////////////////////////////////////////////////////
//                             Lisaac Library                                //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   This program is free software: you can redistribute it and/or modify    //
//   it under the terms of the GNU General Public License as published by    //
//   the Free Software Foundation, either version 3 of the License, or       //
//   (at your option) any later version.                                     //
//                                                                           //
//   This program is distributed in the hope that it will be useful,         //
//   but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//   GNU General Public License for more details.                            //
//                                                                           //
//   You should have received a copy of the GNU General Public License       //
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header

  + name    := INTERFACE;


  - copyright   := "2003-2005 Jérome Boutet, 2003-2007 Benoit Sonntag";
    
  - bibliography:="http://IsaacOS.com";
  - author      :="Sonntag Benoit (bsonntag@loria.fr)";
  - comment     :="User Interface and Events managment.";

  - version := 1;  
  - date    := "2003/04";
  
Section Inherit  
  
  + parent_g_raw:Expanded G_GROUP;
        
Section Public
  
  - screen:AREA; // Physical screen.
  
  - make bmp:ABSTRACT_BITMAP size (w,h:INTEGER) with elt:G_EXPR <-
  (
    set_video_support bmp;
    screen := AREA.clone;
    screen.make NULL from (0,0) size (bmp.width,bmp.height);    
    make screen from (0,0) size (w,h);
    EVENT_SYSTEM.make;
    focus := Self;
    root := elt;
    //
    connect_to MOUSE;
    connect_to KEYBOARD;
    connect_to TIMER;
  );
  
  //
  // Display.
  //
  
  - draw_slave bmp:ABSTRACT_BITMAP from (x0,y0:INTEGER) to (x1,y1:INTEGER) <-
  (     
    bmp.rectangle_fill (x0,y0) to (x1,y1) color color_back; 
  );
     
  //
  // Connect.
  //    
  
  - connect_to obj:INPUT <-
  (
    obj.make;
    obj.add_client Self;
  );
  
  - focus:INBOX;
  
  - set_focus f:INBOX <-
  (
    focus := f;
  );
  
  - resize_window (w,h:INTEGER) <-
  (    
    VIDEO.resize (w,h);
    screen.resize (w,h);
    resize (w,h);
    root.set_position Self at (0,0) size (w,h);    
  );
  
  - run <-
  ( + msg:EVENT;    
    + input:INPUT;
    
    root.set_position Self at (0,0) size (width,height);
    {
      EVENT_SYSTEM.get_event;
      
      {storage_message.is_empty}.until_do {
	msg := storage_message.first;
	storage_message.remove_first;
	msg.set_destination focus;
	focus.receive msg;	
	input ?= msg.source;
	input.acknowledge;
      };      
    }.do_while {`1`:BOOLEAN(TRUE,FALSE)}; // Infinity Loop 
  );
  
  //
  // Message Server.
  //
  
Section Private
  
  - storage_message:LINKED_LIST[EVENT] := LINKED_LIST[EVENT].create;
  
Section Public  
  
  - receive msg:EVENT <-
  ( + mouse:EVENT_MOUSE;
    + win:AREA;
    (msg.destination = NULL).if {
      // Hardware Message.
      storage_message.add_last msg;
    } else {
      // Other message.
      mouse ?= msg;
      (mouse != NULL).if {
	win := get_object ((mouse.x_current),(mouse.y_current));
	(win != focus).if {
	  focus := win;
	  msg.set_destination focus;
	  focus.receive msg;		  
	};
      };
    };
  );
  