]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - macterm.c
The edit menu is now mostly functional. I'm not sure about paste, since I
[PuTTY.git] / macterm.c
index 80ac88872483b292c3b92c6988e147cbf28ba41e..a78e2307dbaa93d09cfa4ac44d96d3c6d4130ecc 100644 (file)
--- a/macterm.c
+++ b/macterm.c
@@ -1,4 +1,4 @@
-/* $Id: macterm.c,v 1.1.2.17 1999/03/11 21:40:32 ben Exp $ */
+/* $Id: macterm.c,v 1.1.2.25 1999/03/16 20:27:31 ben Exp $ */
 /*
  * Copyright (c) 1999 Ben Harris
  * All rights reserved.
@@ -33,6 +33,7 @@
 #include <Controls.h>
 #include <Fonts.h>
 #include <Gestalt.h>
+#include <MacMemory.h>
 #include <MacWindows.h>
 #include <Palettes.h>
 #include <Quickdraw.h>
@@ -45,6 +46,7 @@
 #include <limits.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "macresid.h"
 #include "putty.h"
 #define DEFAULT_FG_BOLD        17
 #define DEFAULT_BG     18
 #define DEFAULT_BG_BOLD        19
-#define CURSOR_FG      22
-#define CURSOR_FG_BOLD 23
+#define CURSOR_FG      20
+#define CURSOR_FG_BOLD 21
+#define CURSOR_BG      22
+#define CURSOR_BG_BOLD 23
+
+#define PTOCC(x) ((x) < 0 ? -(-(x - font_width - 1) / font_width) : \
+                           (x) / font_width)
+#define PTOCR(y) ((y) < 0 ? -(-(y - font_height - 1) / font_height) : \
+                           (y) / font_height)
 
 struct mac_session {
     short              fontnum;
@@ -63,13 +72,17 @@ struct mac_session {
     WindowPtr          window;
     PaletteHandle      palette;
     ControlHandle      scrollbar;
+    WCTabHandle                wctab;
 };
 
 static void mac_initfont(struct mac_session *);
 static void mac_initpalette(struct mac_session *);
+static void mac_adjustwinbg(struct mac_session *);
 static void mac_adjustsize(struct mac_session *, int, int);
+static void mac_drawgrowicon(struct mac_session *s);
 static pascal void mac_scrolltracker(ControlHandle, short);
 static pascal void do_text_for_device(short, short, GDHandle, long);
+static pascal void mac_set_attr_mask(short, short, GDHandle, long);
 static int mac_keytrans(struct mac_session *, EventRecord *, unsigned char *);
 static void text_click(struct mac_session *, EventRecord *);
 
@@ -113,11 +126,14 @@ static void display_resource(unsigned long type, short id) {
 void mac_newsession(void) {
     struct mac_session *s;
     int i;
+    UInt32 starttime;
+    char msg[128];
 
     /* This should obviously be initialised by other means */
     mac_loadconfig(&cfg);
 /*    back = &loop_backend; */
     s = smalloc(sizeof(*s));
+    memset(s, 0, sizeof(*s));
     onlysession = s;
        
     /* XXX: Own storage management? */
@@ -131,11 +147,16 @@ void mac_newsession(void) {
     term_size(cfg.height, cfg.width, cfg.savelines);
     mac_initfont(s);
     mac_initpalette(s);
+    attr_mask = ATTR_MASK;
     /* Set to FALSE to not get palette updates in the background. */
     SetPalette(s->window, s->palette, TRUE); 
     ActivatePalette(s->window);
     ShowWindow(s->window);
+    starttime = TickCount();
     display_resource('pTST', 128);
+    sprintf(msg, "Elapsed ticks: %d\015\012", TickCount() - starttime);
+    inbuf_putstr(msg);
+    term_out();
 }
 
 static void mac_initfont(struct mac_session *s) {
@@ -174,7 +195,6 @@ static void mac_adjustsize(struct mac_session *s, int newrows, int newcols) {
 }
 
 static void mac_initpalette(struct mac_session *s) {
-    WinCTab ct;
   
     if (mac_gestalts.qdvers == gestaltOriginalQD)
        return;
@@ -182,22 +202,24 @@ static void mac_initpalette(struct mac_session *s) {
     if (s->palette == NULL)
        fatalbox("Unable to create palette");
     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
+    mac_adjustwinbg(s);
 }
 
 /*
  * I don't think this is (a) safe or (b) a good way to do this.
  */
-static void mac_updatewinbg(struct mac_session *s) {
-    WinCTab ct;
-    WCTabPtr ctp = &ct;
-    WCTabHandle cth = &ctp;
-
-    ct.wCSeed = 0;
-    ct.wCReserved = 0;
-    ct.ctSize = 1;
-    ct.ctTable[0].value = wContentColor;
-    ct.ctTable[0].rgb = (*s->palette)->pmInfo[16].ciRGB;
-    SetWinColor(s->window, cth);
+static void mac_adjustwinbg(struct mac_session *s) {
+
+    if (s->wctab == NULL)
+       s->wctab = (WCTabHandle)NewHandle(sizeof(**s->wctab));
+    if (s->wctab == NULL)
+       return; /* do without */
+    (*s->wctab)->wCSeed = 0;
+    (*s->wctab)->wCReserved = 0;
+    (*s->wctab)->ctSize = 0;
+    (*s->wctab)->ctTable[0].value = wContentColor;
+    (*s->wctab)->ctTable[0].rgb = (*s->palette)->pmInfo[DEFAULT_BG].ciRGB;
+    SetWinColor(s->window, s->wctab);
 }
 
 /*
@@ -213,7 +235,10 @@ void mac_adjusttermmenus(WindowPtr window) {
     EnableItem(menu, 0);
     DisableItem(menu, iUndo);
     DisableItem(menu, iCut);
-    DisableItem(menu, iCopy);
+    if (term_hasselection())
+       EnableItem(menu, iCopy);
+    else
+       DisableItem(menu, iCopy);
     if (GetScrap(NULL, 'TEXT', &offset) == noTypeErr)
        DisableItem(menu, iPaste);
     else
@@ -222,6 +247,23 @@ void mac_adjusttermmenus(WindowPtr window) {
     EnableItem(menu, iSelectAll);
 }
 
+void mac_menuterm(WindowPtr window, short menu, short item) {
+    struct mac_session *s;
+
+    s = (struct mac_session *)GetWRefCon(window);
+    switch (menu) {
+      case mEdit:
+       switch (item) {
+         case iCopy:
+           term_copy();
+           break;
+         case iPaste:
+           term_paste();
+           break;
+       }
+    }
+}
+           
 void mac_clickterm(WindowPtr window, EventRecord *event) {
     struct mac_session *s;
     Point mouse;
@@ -263,8 +305,8 @@ static void text_click(struct mac_session *s, EventRecord *event) {
     localwhere = event->where;
     GlobalToLocal(&localwhere);
 
-    col = localwhere.h / font_width;
-    row = localwhere.v / font_height;
+    col = PTOCC(localwhere.h);
+    row = PTOCR(localwhere.v);
     if (event->when - lastwhen < GetDblTime() &&
        row == lastrow && col == lastcol && s == lastsess)
        lastact = (lastact == MA_CLICK ? MA_2CLK :
@@ -279,8 +321,8 @@ static void text_click(struct mac_session *s, EventRecord *event) {
     lastcol = col;
     while (StillDown()) {
        GetMouse(&localwhere);
-       col = localwhere.h / font_width;
-       row = localwhere.v / font_height;
+       col = PTOCC(localwhere.h);
+       row = PTOCR(localwhere.v);
        term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT,
                   MA_DRAG, col, row);
        if (row > rows - 1)
@@ -288,9 +330,39 @@ static void text_click(struct mac_session *s, EventRecord *event) {
        else if (row < 0)
            term_scroll(0, row);
     }
+    term_mouse(event->modifiers & shiftKey ? MB_EXTEND : MB_SELECT, MA_RELEASE,
+              col, row);
     lastwhen = TickCount();
 }
 
+void write_clip(void *data, int len) {
+    
+    if (ZeroScrap() != noErr)
+       return;
+    PutScrap(len, 'TEXT', data);
+}
+
+void get_clip(void **p, int *lenp) {
+    static Handle h = NULL;
+    long offset;
+
+    if (p == NULL) {
+       /* release memory */
+       if (h != NULL)
+           DisposeHandle(h);
+       h = NULL;
+    } else
+       if (GetScrap(NULL, 'TEXT', &offset) > 0) {
+           h = NewEmptyHandle();
+           *lenp = GetScrap(h, 'TEXT', &offset);
+           HLock(h);
+           *p = *h;
+       } else {
+           *p = NULL;
+           *lenp = 0;
+       }
+}
+
 static pascal void mac_scrolltracker(ControlHandle control, short part) {
     struct mac_session *s;
 
@@ -527,16 +599,19 @@ void mac_activateterm(WindowPtr window, Boolean active) {
     term_update();
     if (active)
        ShowControl(s->scrollbar);
-    else
+    else {
+       PmBackColor(DEFAULT_BG); /* HideControl clears behind the control */
        HideControl(s->scrollbar);
+    }
+    mac_drawgrowicon(s);
 }
 
 void mac_updateterm(WindowPtr window) {
     struct mac_session *s;
-    Rect clip;
 
     s = (struct mac_session *)GetWRefCon(window);
     BeginUpdate(window);
+    get_ctx();
     term_paint(s,
               (*window->visRgn)->rgnBBox.left,
               (*window->visRgn)->rgnBBox.top,
@@ -548,14 +623,23 @@ void mac_updateterm(WindowPtr window) {
     if (FrontWindow() != window)
        EraseRect(&(*s->scrollbar)->contrlRect);
     UpdateControls(window, window->visRgn);
+    mac_drawgrowicon(s);
+    free_ctx(NULL);
+    EndUpdate(window);
+}
+
+static void mac_drawgrowicon(struct mac_session *s) {
+    Rect clip;
+
+    SetPort(s->window);
     /* Stop DrawGrowIcon giving us space for a horizontal scrollbar */
-    SetRect(&clip, window->portRect.right - 15, SHRT_MIN, SHRT_MAX, SHRT_MAX);
+    SetRect(&clip, s->window->portRect.right - 15, SHRT_MIN,
+           SHRT_MAX, SHRT_MAX);
     ClipRect(&clip);
-    DrawGrowIcon(window);
+    DrawGrowIcon(s->window);
     clip.left = SHRT_MIN;
     ClipRect(&clip);
-    EndUpdate(window);
-}
+}    
 
 struct do_text_args {
     struct mac_session *s;
@@ -600,66 +684,108 @@ void do_text(struct mac_session *s, int x, int y, char *text, int len,
        style |= underline;
     TextFace(style);
     TextSize(cfg.fontheight);
-    if (attr & ATTR_REVERSE)
-       TextMode(notSrcCopy);
-    else
-       TextMode(srcCopy);
     SetFractEnable(FALSE); /* We want characters on pixel boundaries */
     textrgn = NewRgn();
     RectRgn(textrgn, &a.textrect);
     DeviceLoop(textrgn, do_text_for_device, (long)&a, 0);
-    /* Tell the window manager about it in case this isn't an update */
     DisposeRgn(textrgn);
+    /* Tell the window manager about it in case this isn't an update */
     ValidRect(&a.textrect);
 }
 
 static pascal void do_text_for_device(short depth, short devflags,
                                      GDHandle device, long cookie) {
     struct do_text_args *a;
-    int bgcolour, fgcolour;
+    int bgcolour, fgcolour, bright;
 
     a = (struct do_text_args *)cookie;
 
+    bright = (a->attr & ATTR_BOLD) && cfg.bold_colour;
+
+    TextMode(a->attr & ATTR_REVERSE ? notSrcCopy : srcCopy);
+
     switch (depth) {
       case 1:
        /* XXX This should be done with a _little_ more configurability */
        ForeColor(whiteColor);
        BackColor(blackColor);
+       if (a->attr & ATTR_ACTCURS)
+           TextMode(a->attr & ATTR_REVERSE ? srcCopy : notSrcCopy);
        break;
       case 2:
-       if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
-           PmForeColor(DEFAULT_FG_BOLD);
-       else
-           PmForeColor(DEFAULT_FG);
-       if (a->attr & ATTR_ACTCURS)
-           PmBackColor(CURSOR_FG);
-       else
+       if (a->attr & ATTR_ACTCURS) {
+           PmForeColor(bright ? CURSOR_FG_BOLD : CURSOR_FG);
+           PmBackColor(CURSOR_BG);
+           TextMode(srcCopy);
+       } else {
+           PmForeColor(bright ? DEFAULT_FG_BOLD : DEFAULT_FG);
            PmBackColor(DEFAULT_BG);
+       }
        break;
       default:
-       fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
-       bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
-       if ((a->attr & ATTR_BOLD) && cfg.bold_colour)
-           if (a->attr & ATTR_REVERSE)
-               bgcolour++;
-           else
-               fgcolour++;
-       if (a->attr & ATTR_ACTCURS)
-           bgcolour = CURSOR_FG;
+       if (a->attr & ATTR_ACTCURS) {
+           fgcolour = bright ? CURSOR_FG_BOLD : CURSOR_FG;
+           bgcolour = CURSOR_BG;
+           TextMode(srcCopy);
+       } else {
+           fgcolour = ((a->attr & ATTR_FGMASK) >> ATTR_FGSHIFT) * 2;
+           bgcolour = ((a->attr & ATTR_BGMASK) >> ATTR_BGSHIFT) * 2;
+           if (bright)
+               if (a->attr & ATTR_REVERSE)
+                   bgcolour++;
+               else
+                   fgcolour++;
+       }
        PmForeColor(fgcolour);
        PmBackColor(bgcolour);
        break;
     }
+
     MoveTo(a->textrect.left, a->textrect.top + a->s->font_ascent);
     DrawText(a->text, 0, a->len);
+
+    if (a->attr & ATTR_PASCURS) {
+       PenNormal();
+       switch (depth) {
+         case 1:
+           PenMode(patXor);
+           break;
+         default:
+           PmForeColor(CURSOR_BG);
+           break;
+       }
+       FrameRect(&a->textrect);
+    }
 }
 
 /*
  * Call from the terminal emulator to get its graphics context.
+ * Should probably be called start_redraw or something.
  */
 struct mac_session *get_ctx(void) {
+    struct mac_session *s = onlysession;
+
+    attr_mask = ATTR_INVALID;
+    DeviceLoop(s->window->visRgn, mac_set_attr_mask, (long)s, 0);
+    return s;
+}
 
-    return onlysession;
+static pascal void mac_set_attr_mask(short depth, short devflags,
+                                    GDHandle device, long cookie) {
+
+    switch (depth) {
+      default:
+       attr_mask |= ATTR_FGMASK | ATTR_BGMASK;
+       /* FALLTHROUGH */
+      case 2:
+       attr_mask |= ATTR_BOLD;
+       /* FALLTHROUGH */
+      case 1:
+       attr_mask |= ATTR_UNDER | ATTR_REVERSE | ATTR_ACTCURS |
+           ATTR_PASCURS | ATTR_ASCII | ATTR_GBCHR | ATTR_LINEDRW |
+           (cfg.bold_colour ? 0 : ATTR_BOLD); 
+       break;
+    }
 }
 
 /*
@@ -750,6 +876,8 @@ void palette_set(int n, int r, int g, int b) {
     SetEntryColor(s->palette, first[n], &col);
     if (first[n] >= 18)
        SetEntryColor(s->palette, first[n]+1, &col);
+    if (first[n] == DEFAULT_BG)
+       mac_adjustwinbg(s);
     ActivatePalette(s->window);
 }
 
@@ -762,6 +890,7 @@ void palette_reset(void) {
     if (mac_gestalts.qdvers == gestaltOriginalQD)
        return;
     CopyPalette(cfg.colours, s->palette, 0, 0, (*cfg.colours)->pmEntries);
+    mac_adjustwinbg(s);
     ActivatePalette(s->window);
     /* Palette Manager will generate update events as required. */
 }