#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

# Description: Abacum, programming language
# Copyright (C) 2007 by Rafael Treviño Menéndez
# Author: Rafael Treviño Menéndez <skasi.7@gmail.com>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 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
# Library General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import draft, toolbar
import pygame

SCREEN_WIDTH  = 800
SCREEN_HEIGHT = 600

TOOLBAR_WIDTH = 100

draftRect = pygame.Rect (100, 0, 700, 600)
toolbarRect = pygame.Rect (0, 0, 100, 600)

def handleEvents ():
	draftEvents = []
	toolbarEvents = []
	for event in pygame.event.get ():
		type = event.type
		if type == pygame.QUIT:
			raise SystemExit
		
		if draftRect.collidepoint (event.pos):
			draftEvents.append (event)
		elif toolbarRect.collidepoint (event.pos):
			toolbarEvents.append (event)

	draft.mouse (draftEvents)
	toolbar.mouse (toolbarEvents)

def loop ():
	screen = pygame.display.set_mode ((SCREEN_WIDTH, SCREEN_HEIGHT))
	pygame.display.set_caption ('Abacum')
	draft.init (TOOLBAR_WIDTH, 0, SCREEN_WIDTH - TOOLBAR_WIDTH, SCREEN_HEIGHT)
	toolbar.init (0, 0, TOOLBAR_WIDTH, SCREEN_HEIGHT)
	# All event blocked
	pygame.event.set_allowed (None)
	# Only needed events allowed
	pygame.event.set_allowed ([pygame.QUIT, pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP, pygame.MOUSEMOTION])

	clock = pygame.time.Clock ()

	while 1:
		clock.tick (20)
        
		screen.fill ((0, 0, 0))

		screen.blit (toolbar.update (), (0, 0))
		screen.blit (draft.update (), (TOOLBAR_WIDTH, 0))

		handleEvents ()
        
		pygame.display.flip ()

if __name__ == '__main__':
	loop ()
