Add a level of indirection to make it rather easier to work out which of a
[sgt/putty] / mac / macterm.c
index 283cc5c..52c33b8 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macterm.c,v 1.65 2003/02/04 01:53:50 ben Exp $ */
+/* $Id: macterm.c,v 1.68 2003/02/04 23:39:26 ben Exp $ */
 /*
  * Copyright (c) 1999 Simon Tatham
  * Copyright (c) 1999, 2002 Ben Harris
@@ -98,6 +98,7 @@ void mac_startsession(Session *s)
 {
     char *errmsg;
     int i;
+    WinInfo *wi;
 
     init_ucs(s);
 
@@ -119,7 +120,10 @@ void mac_startsession(Session *s)
        s->window = GetNewCWindow(wTerminal, NULL, (WindowPtr)-1);
     else
        s->window = GetNewWindow(wTerminal, NULL, (WindowPtr)-1);
-    SetWRefCon(s->window, (long)s);
+    wi = smalloc(sizeof(*wi));
+    wi->s = s;
+    wi->wtype = wTerminal;
+    SetWRefCon(s->window, (long)wi);
     s->scrollbar = GetNewControl(cVScroll, s->window);
     s->term = term_init(&s->cfg, &s->ucsdata, s);
 
@@ -204,7 +208,7 @@ static void mac_initfont(Session *s) {
     TextEncoding enc;
     OptionBits fbflags;
 
-    SetPort(GetWindowPort(s->window));
+    SetPort((GrafPtr)GetWindowPort(s->window));
     GetFNum(s->cfg.font.name, &s->fontnum);
     TextFont(s->fontnum);
     TextFace(s->cfg.font.face);
@@ -384,8 +388,8 @@ void mac_adjusttermcursor(WindowPtr window, Point mouse, RgnHandle cursrgn) {
     RgnHandle visrgn;
 #endif
 
-    SetPort(GetWindowPort(window));
-    s = (Session *)GetWRefCon(window);
+    SetPort((GrafPtr)GetWindowPort(window));
+    s = mac_windowsession(window);
     GlobalToLocal(&mouse);
     part = FindControl(mouse, window, &control);
     if (control == s->scrollbar) {
@@ -435,7 +439,7 @@ void mac_adjusttermmenus(WindowPtr window) {
     long offset;
 #endif
 
-    s = (Session *)GetWRefCon(window);
+    s = mac_windowsession(window);
     menu = GetMenuHandle(mFile);
     DisableItem(menu, iSave); /* XXX enable if modified */
     EnableItem(menu, iSaveAs);
@@ -451,7 +455,7 @@ void mac_adjusttermmenus(WindowPtr window) {
 #if TARGET_API_MAC_CARBON
     if (1)
 #else
-    if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
+    if (GetScrap(NULL, kScrapFlavorTypeText, &offset) == noTypeErr)
 #endif
        DisableItem(menu, iPaste);
     else
@@ -463,7 +467,7 @@ void mac_adjusttermmenus(WindowPtr window) {
 void mac_menuterm(WindowPtr window, short menu, short item) {
     Session *s;
 
-    s = (Session *)GetWRefCon(window);
+    s = mac_windowsession(window);
     switch (menu) {
       case mEdit:
        switch (item) {
@@ -484,8 +488,8 @@ void mac_clickterm(WindowPtr window, EventRecord *event) {
     int part;
     static ControlActionUPP mac_scrolltracker_upp = NULL;
 
-    s = (Session *)GetWRefCon(window);
-    SetPort(GetWindowPort(window));
+    s = mac_windowsession(window);
+    SetPort((GrafPtr)GetWindowPort(window));
     mouse = event->where;
     GlobalToLocal(&mouse);
     part = FindControl(mouse, window, &control);
@@ -518,7 +522,7 @@ static void text_click(Session *s, EventRecord *event) {
     static int lastrow = -1, lastcol = -1;
     static Mouse_Action lastact = MA_NOTHING;
 
-    SetPort(GetWindowPort(s->window));
+    SetPort((GrafPtr)GetWindowPort(s->window));
     localwhere = event->where;
     GlobalToLocal(&localwhere);
 
@@ -579,7 +583,7 @@ void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
      */
     if (ZeroScrap() != noErr)
        return;
-    PutScrap(len * sizeof(*data), 'utxt', data);
+    PutScrap(len * sizeof(*data), kScrapFlavorTypeUnicode, data);
 
     /* Replace LINE SEPARATORs with CR for TEXT output. */
     for (i = 0; i < len; i++)
@@ -601,7 +605,7 @@ void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
                                    s->font_charset, NULL, ".", 1);
     } else
        return;
-    PutScrap(olen, 'TEXT', mactextbuf);
+    PutScrap(olen, kScrapFlavorTypeText, mactextbuf);
     sfree(mactextbuf);
 
     stsz = offsetof(StScrpRec, scrpStyleTab) + sizeof(ScrpSTElement);
@@ -616,7 +620,7 @@ void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
     stsc->scrpStyleTab[0].scrpColor.red = 0;
     stsc->scrpStyleTab[0].scrpColor.green = 0;
     stsc->scrpStyleTab[0].scrpColor.blue = 0;
-    PutScrap(stsz, 'styl', stsc);
+    PutScrap(stsz, kScrapFlavorTypeTextStyle, stsc);
     sfree(stsc);
 #endif
 }
@@ -647,15 +651,16 @@ void get_clip(void *frontend, wchar_t **p, int *lenp) {
            sfree(data);
        data = NULL;
     } else {
-       if (GetScrap(NULL, 'utxt', &offset) > 0) {
+       if (GetScrap(NULL, kScrapFlavorTypeUnicode, &offset) > 0) {
            if (h == NULL)
                h = NewHandle(0);
-           *lenp = GetScrap(h, 'utxt', &offset) / sizeof(**p);
+           *lenp =
+               GetScrap(h, kScrapFlavorTypeUnicode, &offset) / sizeof(**p);
            HLock(h);
            *p = (wchar_t *)*h;
-       } else if (GetScrap(NULL, 'TEXT', &offset) > 0) {
+       } else if (GetScrap(NULL, kScrapFlavorTypeText, &offset) > 0) {
            texth = NewHandle(0);
-           textlen = GetScrap(texth, 'TEXT', &offset);
+           textlen = GetScrap(texth, kScrapFlavorTypeText, &offset);
            HLock(texth);
            data = smalloc(textlen * 2);
            /* XXX should use 'styl' scrap if it's there. */
@@ -704,9 +709,9 @@ static pascal void mac_scrolltracker(ControlHandle control, short part) {
     Session *s;
 
 #if TARGET_API_MAC_CARBON
-    s = (Session *)GetWRefCon(GetControlOwner(control));
+    s = mac_windowsession(GetControlOwner(control));
 #else
-    s = (Session *)GetWRefCon((*control)->contrlOwner);
+    s = mac_windowsession((*control)->contrlOwner);
 #endif
     switch (part) {
       case kControlUpButtonPart:
@@ -725,7 +730,7 @@ static pascal void mac_scrolltracker(ControlHandle control, short part) {
 }
 
 void mac_keyterm(WindowPtr window, EventRecord *event) {
-    Session *s = (Session *)GetWRefCon(window);
+    Session *s = mac_windowsession(window);
     Key_Sym keysym = PK_NULL;
     unsigned int mods = 0, flags = PKF_NUMLOCK;
     UniChar utxt[1];
@@ -895,7 +900,7 @@ void mac_growterm(WindowPtr window, EventRecord *event) {
     FontInfo fi;
 #endif
 
-    s = (Session *)GetWRefCon(window);
+    s = mac_windowsession(window);
 
 #if !TARGET_API_MAC_CARBON
     draghooksave = LMGetDragHook();
@@ -969,7 +974,7 @@ static pascal void mac_growtermdraghook(void)
 
 void mac_closeterm(WindowPtr window)
 {
-    Session *s = (Session *)GetWRefCon(window);
+    Session *s = mac_windowsession(window);
 
     /* XXX warn on close */
     HideWindow(s->window);
@@ -981,6 +986,7 @@ void mac_closeterm(WindowPtr window)
     if (s->uni_to_font != NULL)
        DisposeUnicodeToTextInfo(&s->uni_to_font);
     term_free(s->term);
+    sfree((WinInfo *)GetWRefCon(s->window));
     DisposeWindow(s->window);
     DisposePalette(s->palette);
     sfree(s);
@@ -989,7 +995,7 @@ void mac_closeterm(WindowPtr window)
 void mac_activateterm(WindowPtr window, Boolean active) {
     Session *s;
 
-    s = (Session *)GetWRefCon(window);
+    s = mac_windowsession(window);
     s->term->has_focus = active;
     term_update(s->term);
     if (active)
@@ -1011,8 +1017,8 @@ void mac_updateterm(WindowPtr window) {
     RgnHandle visrgn;
 #endif
 
-    s = (Session *)GetWRefCon(window);
-    SetPort(GetWindowPort(window));
+    s = mac_windowsession(window);
+    SetPort((GrafPtr)GetWindowPort(window));
     BeginUpdate(window);
     pre_paint(s);
 #if TARGET_API_MAC_CARBON
@@ -1050,7 +1056,7 @@ static void mac_drawgrowicon(Session *s) {
     Rect clip;
     RgnHandle savergn;
 
-    SetPort(GetWindowPort(s->window));
+    SetPort((GrafPtr)GetWindowPort(s->window));
     /*
      * Stop DrawGrowIcon giving us space for a horizontal scrollbar
      * See Tech Note TB575 for details.
@@ -1103,7 +1109,7 @@ void do_text(Context ctx, int x, int y, char *text, int len,
 
     assert(len <= 1024);
 
-    SetPort(GetWindowPort(s->window));
+    SetPort((GrafPtr)GetWindowPort(s->window));
 
     fontwidth = s->font_width;
     if ((lattr & LATTR_MODE) != LATTR_NORM)
@@ -1171,7 +1177,7 @@ void do_text(Context ctx, int x, int y, char *text, int len,
        a.denom = s->font_bigdenom;
        break;
     }
-    SetPort(GetWindowPort(s->window));
+    SetPort((GrafPtr)GetWindowPort(s->window));
     TextFont(s->fontnum);
     style = s->cfg.font.face;
     if ((attr & ATTR_BOLD) && !s->cfg.bold_colour)
@@ -1213,7 +1219,7 @@ void do_text(Context ctx, int x, int y, char *text, int len,
 
 static pascal void do_text_for_device(short depth, short devflags,
                                      GDHandle device, long cookie) {
-    struct do_text_args *a;
+    struct do_text_args *a = (struct do_text_args *)cookie;
     int bgcolour, fgcolour, bright, reverse, tmp;
 #if TARGET_API_MAC_CARBON
     CQDProcsPtr gp = GetPortGrafProcs(GetWindowPort(a->s->window));
@@ -1221,8 +1227,6 @@ static pascal void do_text_for_device(short depth, short devflags,
     QDProcsPtr gp = a->s->window->grafProcs;
 #endif
 
-    a = (struct do_text_args *)cookie;
-
     bright = (a->attr & ATTR_BOLD) && a->s->cfg.bold_colour;
     reverse = a->attr & ATTR_REVERSE;
 
@@ -1321,7 +1325,7 @@ void pre_paint(Session *s) {
 
     if (HAVE_COLOR_QD()) {
        s->term->attr_mask = 0;
-       SetPort(GetWindowPort(s->window));
+       SetPort((GrafPtr)GetWindowPort(s->window));
 #if TARGET_API_MAC_CARBON
        visrgn = NewRgn();
        GetPortVisibleRegion(GetWindowPort(s->window), visrgn);
@@ -1674,7 +1678,7 @@ void do_scroll(Context ctx, int topline, int botline, int lines) {
     RgnHandle update = NewRgn();
     Point g2l = { 0, 0 };
 
-    SetPort(GetWindowPort(s->window));
+    SetPort((GrafPtr)GetWindowPort(s->window));
 
     /*
      * Work out the part of the update region that will scrolled by