#
#   Python GUI - Windows - Macintosh version
#

from Carbon.Qd import NewRgn, RectRgn, GetMainDevice
from Carbon.Windows import \
    kDocumentWindowClass, \
    kModalWindowClass, kMovableModalWindowClass, \
    kAlertWindowClass, kMovableAlertWindowClass, \
    kWindowInWindowMenuAttribute, kWindowCloseBoxAttribute, kWindowFullZoomAttribute, \
    kWindowResizableAttribute, kWindowCollapseBoxAttribute, kWindowContentRgn
from Carbon.Ctl import CreateRootControl
from Carbon.Win import CreateNewWindow, GetAvailableWindowPositioningBounds
from GGeometry import sub_pt, rect_width, rect_height
from GWindows import Window as GWindow
from Containers import Container
import Applications
from Applications import application

class Window(GWindow):

    _style_to_mac_window_class = {
        'standard':        (kDocumentWindowClass, kDocumentWindowClass),
        'nonmodal_dialog': (kDocumentWindowClass, kDocumentWindowClass),
        'modal_dialog':    (kModalWindowClass, kMovableModalWindowClass),
        'alert':           (kAlertWindowClass, kMovableAlertWindowClass),
    }
    
    _modal_styles = ('modal_dialog', 'alert')
    
    _mac_origin = (0, 0)
    
    def __init__(self, style = 'standard', title = "New Window",
            movable = 1, closable = 1, hidable = None, resizable = 1, zoomable = 1, **kw):
        try:
            win_classes = self._style_to_mac_window_class[style]
        except KeyError:
            raise ValueError("Invalid window style %r, should be one of %r" % (style,
                self._style_to_mac_window_class.keys()))
        win_class = win_classes[not not movable]
        modal = style in self._modal_styles
        if hidable is None:
            hidable = not modal
        attrs = 0
        if style == 'standard':
            attrs |= kWindowInWindowMenuAttribute
        if movable:
            if closable:
                attrs |= kWindowCloseBoxAttribute
            if hidable:
                attrs |= kWindowCollapseBoxAttribute
            if zoomable:
                attrs |= kWindowFullZoomAttribute
        if resizable:
            attrs |= kWindowResizableAttribute
        mac_window = CreateNewWindow(win_class, attrs, (20, 60, 320, 260))
        self._mac_root_control = CreateRootControl(mac_window)
        Applications._mac_window_to_window[mac_window] = self
        GWindow.__init__(self, mac_window = mac_window, title = title, closable = closable, **kw)
        self._mac_update_viewport()
        
    def destroy(self):
        GWindow.destroy(self)
        del Applications._mac_window_to_window[self._mac_window]
        self._mac_window.HideWindow()
    
    def set_menus(self, x):
        GWindow.set_menus(self, x)
        application()._window_menus_changed(self)
    
    def get_title(self):
        return self._mac_window.GetWTitle()
    
    def set_title(self, new_title):
        self._mac_window.SetWTitle(new_title)
    
    def get_bounds(self):
        return self._mac_get_bounds()
    
    def _change_bounds(self, r):
        self._mac_window.SetWindowBounds(kWindowContentRgn, r)
        RectRgn(self._mac_vis_rgn, (0, 0, rect_width(r), rect_height(r)))
        self._mac_update_subcomponent_viewports()
    
    def get_visible(self):
        return self._mac_window.IsWindowVisible()
    
    def _mac_show(self):
        self._mac_window.ShowWindow()
    
    def _mac_hide(self):
        self._mac_window.HideWindow()
    
    def show(self):
        GWindow.show(self)
        self._mac_window.SelectWindow()
    
    def _mac_update_viewport(self):
        width, height = self.size
        RectRgn(self._mac_vis_rgn, (0, 0, width, height))
    
    def _mac_get_bounds(self):
        return self._mac_window.GetWindowBounds(kWindowContentRgn)
    
    def _mac_select(self):
        self._mac_window.SelectWindow()
    
    def _mac_drag(self, mac_where):
        drag_bounds = self._screen_rect()
        self._mac_window.DragWindow(mac_where, drag_bounds)
    
    def _screen_rect(self):
        return GetAvailableWindowPositioningBounds(GetMainDevice())
    
    def _mac_grow(self, mac_where):
        result = self._mac_window.GrowWindow(mac_where, (64, 64, 32767, 32767))
        if result:
            new_size = (result & 0xffff, result >> 16)
            self.resize(size = new_size)
            #self.size = new_size
    
    def _mac_go_away(self, mac_where):
        mac_window = self._mac_window
        if mac_window.TrackGoAway(mac_where):
            self.close_cmd()
    
    def _mac_zoom(self, mac_where, mac_part):
        mac_window = self._mac_window
        if mac_window.TrackBox(mac_where, mac_part):
            old_size = self.size
            mac_window.ZoomWindow(mac_part, 1)
            new_size = self.size
            self.resized(sub_pt(new_size, old_size))

    def _mac_update(self):
        mac_win = self._mac_window
        mac_win.BeginUpdate()
        Container._mac_update(self)
        mac_win.EndUpdate()
    

