(version): New variable, set by configure.
[bpt/emacs.git] / src / macterm.c
index 5b1bda4..e36fafa 100644 (file)
@@ -108,6 +108,10 @@ static Lisp_Object last_window;
    (Not yet supported.)  */
 int x_use_underline_position_properties;
 
+/* Non-zero means to draw the underline at the same place as the descent line.  */
+
+int x_underline_at_descent_line;
+
 /* This is a chain of structures for all the X displays currently in
    use.  */
 
@@ -240,7 +244,8 @@ static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
 static void mac_focus_changed P_ ((int, struct mac_display_info *,
                                   struct frame *, struct input_event *));
 static void x_detect_focus_change P_ ((struct mac_display_info *,
-                                      EventRecord *, struct input_event *));
+                                      const EventRecord *,
+                                      struct input_event *));
 static void XTframe_rehighlight P_ ((struct frame *));
 static void x_frame_rehighlight P_ ((struct x_display_info *));
 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
@@ -261,9 +266,6 @@ static int is_emacs_window P_ ((WindowPtr));
 static XCharStruct *mac_per_char_metric P_ ((XFontStruct *, XChar2b *, int));
 static void XSetFont P_ ((Display *, GC, XFontStruct *));
 
-/* Defined in macmenu.h.  */
-extern void menubar_selection_callback (FRAME_PTR, int);
-
 #define GC_FORE_COLOR(gc)      (&(gc)->fore_color)
 #define GC_BACK_COLOR(gc)      (&(gc)->back_color)
 #define GC_FONT(gc)            ((gc)->xgcv.font)
@@ -639,7 +641,7 @@ mac_create_bitmap_from_bitmap_data (bitmap, bits, w, h)
      char *bits;
      int w, h;
 {
-  static unsigned char swap_nibble[16]
+  static const unsigned char swap_nibble[16]
     = { 0x0, 0x8, 0x4, 0xc,    /* 0000 1000 0100 1100 */
        0x2, 0xa, 0x6, 0xe,    /* 0010 1010 0110 1110 */
        0x1, 0x9, 0x5, 0xd,    /* 0001 1001 0101 1101 */
@@ -825,9 +827,9 @@ atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout)
 
   if (saved_text_layout == NULL)
     {
-      UniCharCount lengths[] = {kATSUToTextEnd};
-      ATSUAttributeTag tags[] = {kATSULineLayoutOptionsTag};
-      ByteCount sizes[] = {sizeof (ATSLineLayoutOptions)};
+      static const UniCharCount lengths[] = {kATSUToTextEnd};
+      static const ATSUAttributeTag tags[] = {kATSULineLayoutOptionsTag};
+      static const ByteCount sizes[] = {sizeof (ATSLineLayoutOptions)};
       static ATSLineLayoutOptions line_layout =
 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
        kATSLineDisableAllLayoutOperations | kATSLineUseDeviceMetrics
@@ -836,7 +838,7 @@ atsu_get_text_layout_with_text_ptr (text, text_length, style, text_layout)
        kATSLineIsDisplayOnly | kATSLineFractDisable
 #endif
        ;
-      ATSUAttributeValuePtr values[] = {&line_layout};
+      static const ATSUAttributeValuePtr values[] = {&line_layout};
 
       err = ATSUCreateTextLayoutWithTextPtr (text,
                                             kATSUFromTextBeginning,
@@ -890,12 +892,13 @@ mac_invert_rectangle (f, x, y, width, height)
 
 
 static void
-mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, bytes_per_char)
+mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width,
+                       overstrike_p, bytes_per_char)
      struct frame *f;
      GC gc;
      int x, y;
      char *buf;
-     int nchars, bg_width, bytes_per_char;
+     int nchars, bg_width, overstrike_p, bytes_per_char;
 {
   SetPortWindowPort (FRAME_MAC_WINDOW (f));
 
@@ -945,17 +948,24 @@ mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, bytes_per_char)
          ATSUDrawText (text_layout,
                        kATSUFromTextBeginning, kATSUToTextEnd,
                        kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
+         if (overstrike_p)
+           {
+             MoveTo (x + 1, y);
+             ATSUDrawText (text_layout,
+                           kATSUFromTextBeginning, kATSUToTextEnd,
+                           kATSUUseGrafPortPenLoc, kATSUUseGrafPortPenLoc);
+           }
          mac_end_clip (gc);
 #ifdef MAC_OSX
        }
       else
        {
          CGrafPtr port;
-         CGContextRef context;
+         static CGContextRef context;
          float port_height = FRAME_PIXEL_HEIGHT (f);
-         ATSUAttributeTag tags[] = {kATSUCGContextTag};
-         ByteCount sizes[] = {sizeof (CGContextRef)};
-         ATSUAttributeValuePtr values[] = {&context};
+         static const ATSUAttributeTag tags[] = {kATSUCGContextTag};
+         static const ByteCount sizes[] = {sizeof (CGContextRef)};
+         static const ATSUAttributeValuePtr values[] = {&context};
 
 #if USE_CG_DRAWING
          context = mac_begin_cg_clip (f, gc);
@@ -988,9 +998,15 @@ mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, bytes_per_char)
                                       sizeof (tags) / sizeof (tags[0]),
                                       tags, sizes, values);
          if (err == noErr)
-           ATSUDrawText (text_layout,
-                         kATSUFromTextBeginning, kATSUToTextEnd,
-                         Long2Fix (x), Long2Fix (port_height - y));
+           {
+             ATSUDrawText (text_layout,
+                           kATSUFromTextBeginning, kATSUToTextEnd,
+                           Long2Fix (x), Long2Fix (port_height - y));
+             if (overstrike_p)
+               ATSUDrawText (text_layout,
+                             kATSUFromTextBeginning, kATSUToTextEnd,
+                             Long2Fix (x + 1), Long2Fix (port_height - y));
+           }
 #if USE_CG_DRAWING
          mac_end_cg_clip (f);
          context = NULL;
@@ -1056,6 +1072,12 @@ mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, bytes_per_char)
       TextFace (GC_FONT (gc)->mac_fontface);
       MoveTo (x, y);
       DrawText (buf, 0, nchars * bytes_per_char);
+      if (overstrike_p)
+       {
+         TextMode (srcOr);
+         MoveTo (x + 1, y);
+         DrawText (buf, 0, nchars * bytes_per_char);
+       }
       if (bg_width)
        RGBBackColor (GC_BACK_COLOR (FRAME_NORMAL_GC (f)));
       mac_end_clip (gc);
@@ -1068,59 +1090,33 @@ mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, bytes_per_char)
 }
 
 
-/* Mac replacement for XDrawString.  */
-
-static void
-mac_draw_string (f, gc, x, y, buf, nchars)
-     struct frame *f;
-     GC gc;
-     int x, y;
-     char *buf;
-     int nchars;
-{
-  mac_draw_string_common (f, gc, x, y, buf, nchars, 0, 1);
-}
-
-
-/* Mac replacement for XDrawString16. */
-
-static void
-mac_draw_string_16 (f, gc, x, y, buf, nchars)
-     struct frame *f;
-     GC gc;
-     int x, y;
-     XChar2b *buf;
-     int nchars;
-{
-  mac_draw_string_common (f, gc, x, y, (char *) buf, nchars, 0, 2);
-}
-
-
 /* Mac replacement for XDrawImageString.  */
 
 static void
-mac_draw_image_string (f, gc, x, y, buf, nchars, bg_width)
+mac_draw_image_string (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
      struct frame *f;
      GC gc;
      int x, y;
      char *buf;
-     int nchars, bg_width;
+     int nchars, bg_width, overstrike_p;
 {
-  mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width, 1);
+  mac_draw_string_common (f, gc, x, y, buf, nchars, bg_width,
+                         overstrike_p, 1);
 }
 
 
-/* Mac replacement for XDrawString16.  */
+/* Mac replacement for XDrawImageString16.  */
 
 static void
-mac_draw_image_string_16 (f, gc, x, y, buf, nchars, bg_width)
+mac_draw_image_string_16 (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
      struct frame *f;
      GC gc;
      int x, y;
      XChar2b *buf;
-     int nchars, bg_width;
+     int nchars, bg_width, overstrike_p;
 {
-  mac_draw_string_common (f, gc, x, y, (char *) buf, nchars, bg_width, 2);
+  mac_draw_string_common (f, gc, x, y, (char *) buf, nchars, bg_width,
+                         overstrike_p, 2);
 }
 
 
@@ -1203,7 +1199,12 @@ mac_query_char_extents (style, c,
                err1 = ATSUGetGlyphInfo (text_layout, kATSUFromTextBeginning,
                                         kATSUToTextEnd, &count,
                                         &glyph_info_array);
-             if (err1 == noErr)
+             if (err1 == noErr
+                 /* Make sure that we don't have to make layout
+                    adjustments.  */
+                 && glyph_info_array.glyphs[0].deltaY == 0.0f
+                 && glyph_info_array.glyphs[0].idealX == 0.0f
+                 && glyph_info_array.glyphs[0].screenX == 0)
                {
                  xassert (glyph_info_array.glyphs[0].glyphID);
                  *cg_glyph = glyph_info_array.glyphs[0].glyphID;
@@ -1294,12 +1295,12 @@ init_cg_text_anti_aliasing_threshold ()
 }
 
 static int
-mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width)
+mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
      struct frame *f;
      GC gc;
      int x, y;
      XChar2b *buf;
-     int nchars, bg_width;
+     int nchars, bg_width, overstrike_p;
 {
   CGrafPtr port;
   float port_height, gx, gy;
@@ -1359,13 +1360,31 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width)
   if (GC_FONT (gc)->mac_fontsize <= cg_text_anti_aliasing_threshold)
     CGContextSetShouldAntialias (context, false);
 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
-  CGContextSetTextPosition (context, gx, gy);
-  CGContextShowGlyphsWithAdvances (context, glyphs, advances, nchars);
-#else
-  for (i = 0; i < nchars; i++)
+#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
+  if (CGContextShowGlyphsWithAdvances != NULL)
+#endif
+    {
+      CGContextSetTextPosition (context, gx, gy);
+      CGContextShowGlyphsWithAdvances (context, glyphs, advances, nchars);
+      if (overstrike_p)
+       {
+         CGContextSetTextPosition (context, gx + 1.0f, gy);
+         CGContextShowGlyphsWithAdvances (context, glyphs, advances, nchars);
+       }
+    }
+#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
+  else
+#endif
+#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030  */
+#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020
     {
-      CGContextShowGlyphsAtPoint (context, gx, gy, glyphs + i, 1);
-      gx += advances[i].width;
+      for (i = 0; i < nchars; i++)
+       {
+         CGContextShowGlyphsAtPoint (context, gx, gy, glyphs + i, 1);
+         if (overstrike_p)
+           CGContextShowGlyphsAtPoint (context, gx + 1.0f, gy, glyphs + i, 1);
+         gx += advances[i].width;
+       }
     }
 #endif
 #if USE_CG_DRAWING
@@ -2189,7 +2208,7 @@ pcm_init (pcm, count)
 
 static enum pcm_status
 pcm_get_status (pcm)
-     XCharStruct *pcm;
+     const XCharStruct *pcm;
 {
   int height = pcm->ascent + pcm->descent;
 
@@ -2633,6 +2652,11 @@ mac_compute_glyph_string_overhangs (s)
       Rect r;
       MacFontStruct *font = s->font;
 
+#if USE_CG_DRAWING
+      mac_prepare_for_quickdraw (s->f);
+#endif
+      SetPortWindowPort (FRAME_MAC_WINDOW (s->f));
+
       TextFont (font->mac_fontnum);
       TextSize (font->mac_fontsize);
       TextFace (font->mac_fontface);
@@ -2765,15 +2789,18 @@ x_draw_glyph_string_foreground (s)
 #if USE_CG_TEXT_DRAWING
        if (!s->two_byte_p
            && mac_draw_image_string_cg (s->f, s->gc, x, s->ybase - boff,
-                                        s->char2b, s->nchars, bg_width))
+                                        s->char2b, s->nchars, bg_width,
+                                        s->face->overstrike))
          ;
        else
 #endif
          mac_draw_image_string_16 (s->f, s->gc, x, s->ybase - boff,
-                                   s->char2b, s->nchars, bg_width);
+                                   s->char2b, s->nchars, bg_width,
+                                   s->face->overstrike);
       else
        mac_draw_image_string (s->f, s->gc, x, s->ybase - boff,
-                              char1b, s->nchars, bg_width);
+                              char1b, s->nchars, bg_width,
+                              s->face->overstrike);
     }
 }
 
@@ -2809,10 +2836,10 @@ x_draw_composite_glyph_string_foreground (s)
   else
     {
       for (i = 0; i < s->nchars; i++, ++s->gidx)
-       mac_draw_string_16 (s->f, s->gc,
-                           x + s->cmp->offsets[s->gidx * 2],
-                           s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
-                           s->char2b + i, 1);
+       mac_draw_image_string_16 (s->f, s->gc,
+                                 x + s->cmp->offsets[s->gidx * 2],
+                                 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
+                                 s->char2b + i, 1, 0, s->face->overstrike);
     }
 }
 
@@ -3674,18 +3701,45 @@ x_draw_glyph_string (s)
       /* Draw underline.  */
       if (s->face->underline_p)
        {
-          unsigned long h = 1;
-          unsigned long dy = s->height - h;
+         unsigned long tem, h;
+         int y;
+
+#if 0
+         /* Get the underline thickness.  Default is 1 pixel.  */
+         if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
+#endif
+           h = 1;
+
+         y = s->y + s->height - h;
+         if (!x_underline_at_descent_line)
+            {
+             /* Get the underline position.  This is the recommended
+                 vertical offset in pixels from the baseline to the top of
+                 the underline.  This is a signed value according to the
+                 specs, and its default is
+
+                ROUND ((maximum descent) / 2), with
+                ROUND(x) = floor (x + 0.5)  */
+
+#if 0
+              if (x_use_underline_position_properties
+                  && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
+                y = s->ybase + (long) tem;
+              else
+#endif
+             if (s->face->font)
+                y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
+            }
 
          if (s->face->underline_defaulted_p)
-           mac_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
+           mac_fill_rectangle (s->f, s->gc, s->x, y,
                                s->background_width, h);
          else
            {
              XGCValues xgcv;
              XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
              XSetForeground (s->display, s->gc, s->face->underline_color);
-             mac_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
+             mac_fill_rectangle (s->f, s->gc, s->x, y,
                                  s->background_width, h);
              XSetForeground (s->display, s->gc, xgcv.foreground);
            }
@@ -4177,7 +4231,7 @@ mac_focus_changed (type, dpyinfo, frame, bufp)
 static void
 x_detect_focus_change (dpyinfo, event, bufp)
      struct mac_display_info *dpyinfo;
-     EventRecord *event;
+     const EventRecord *event;
      struct input_event *bufp;
 {
   struct frame *frame;
@@ -5190,7 +5244,7 @@ static void
 x_scroll_bar_handle_click (bar, part_code, er, bufp)
      struct scroll_bar *bar;
      ControlPartCode part_code;
-     EventRecord *er;
+     const EventRecord *er;
      struct input_event *bufp;
 {
   int win_y, top_range;
@@ -6653,11 +6707,12 @@ xlfdpat_destroy (pat)
 
 static struct xlfdpat *
 xlfdpat_create (pattern)
-     char *pattern;
+     const char *pattern;
 {
   struct xlfdpat *pat;
   int nblocks, i, skip;
   unsigned char last_char, *p, *q, *anychar_head;
+  const unsigned char *ptr;
   struct xlfdpat_block *blk;
 
   pat = xmalloc (sizeof (struct xlfdpat));
@@ -6668,9 +6723,9 @@ xlfdpat_create (pattern)
   anychar_head = NULL;
   q = pat->buf;
   last_char = '\0';
-  for (p = pattern; *p; p++)
+  for (ptr = pattern; *ptr; ptr++)
     {
-      unsigned char c = *p;
+      unsigned char c = *ptr;
 
       if (c == '*')
        if (last_char == '*')
@@ -6774,14 +6829,15 @@ xlfdpat_exact_p (pat)
    that the pattern in *BLK matches with its prefix.  Return NULL
    there is no such strings.  STRING must be lowered in advance.  */
 
-static char *
+static const char *
 xlfdpat_block_match_1 (blk, string, start_max)
      struct xlfdpat_block *blk;
-     unsigned char *string;
+     const unsigned char *string;
      int start_max;
 {
   int start, infinity;
-  unsigned char *p, *s;
+  unsigned char *p;
+  const unsigned char *s;
 
   xassert (blk->len > 0);
   xassert (start_max + blk->len <= strlen (string));
@@ -6838,17 +6894,17 @@ xlfdpat_block_match_1 (blk, string, start_max)
   ((b)->len == 1 ? memchr ((s), (b)->last_char, (m) + 1) \
    : xlfdpat_block_match_1 (b, s, m))
 
-/* Check if XLFD pattern PAT, which is generated by `xfldpat_create',
+/* Check if XLFD pattern PAT, which is generated by `xlfdpat_create',
    matches with STRING.  STRING must be lowered in advance.  */
 
 static int
 xlfdpat_match (pat, string)
      struct xlfdpat *pat;
-     unsigned char *string;
+     const unsigned char *string;
 {
   int str_len, nblocks, i, start_max;
   struct xlfdpat_block *blk;
-  unsigned char *s;
+  const unsigned char *s;
 
   xassert (pat->nblocks > 0);
 
@@ -7019,7 +7075,7 @@ decode_mac_font_name (name, size, coding_system)
 
 static char *
 mac_to_x_fontname (name, size, style, charset)
-     char *name;
+     const char *name;
      int size;
      Style style;
      char *charset;
@@ -7066,7 +7122,8 @@ const int kDefaultFontSize = 12;
 
 static int
 parse_x_font_name (xf, family, size, style, charset)
-     char *xf, *family;
+     const char *xf;
+     char *family;
      int *size;
      Style *style;
      char *charset;
@@ -7149,10 +7206,10 @@ add_font_name_table_entry (char *font_name)
 
 static void
 add_mac_font_name (name, size, style, charset)
-     char *name;
+     const char *name;
      int size;
      Style style;
-     char *charset;
+     const char *charset;
 {
   if (size > 0)
     add_font_name_table_entry (mac_to_x_fontname (name, size, style, charset));
@@ -7454,7 +7511,7 @@ enum xlfd_scalable_field_index
     XLFD_SCL_LAST
   };
 
-static int xlfd_scalable_fields[] =
+static const int xlfd_scalable_fields[] =
   {
     6,                         /* PIXEL_SIZE */
     7,                         /* POINT_SIZE */
@@ -7464,14 +7521,16 @@ static int xlfd_scalable_fields[] =
 
 static Lisp_Object
 mac_do_list_fonts (pattern, maxnames)
-     char *pattern;
+     const char *pattern;
      int maxnames;
 {
   int i, n_fonts = 0;
   Lisp_Object font_list = Qnil;
   struct xlfdpat *pat;
-  char *scaled, *ptr;
-  int scl_val[XLFD_SCL_LAST], *field, *val;
+  char *scaled;
+  const char *ptr;
+  int scl_val[XLFD_SCL_LAST], *val;
+  const int *field;
   int exact;
 
   if (font_name_table == NULL)  /* Initialize when first used.  */
@@ -7721,7 +7780,8 @@ x_compute_min_glyph_bounds (f)
    fields are present, none is '*'.  */
 
 static int
-is_fully_specified_xlfd (char *p)
+is_fully_specified_xlfd (p)
+     const char *p;
 {
   int i;
   char *q;
@@ -7749,14 +7809,16 @@ is_fully_specified_xlfd (char *p)
 }
 
 
-/* XLoadQueryFont creates and returns an internal representation for a
-   font in a MacFontStruct struct.  There is really no concept
+/* mac_load_query_font creates and returns an internal representation
+   for a font in a MacFontStruct struct.  There is really no concept
    corresponding to "loading" a font on the Mac.  But we check its
    existence and find the font number and all other information for it
    and store them in the returned MacFontStruct.  */
 
 static MacFontStruct *
-XLoadQueryFont (Display *dpy, char *fontname)
+mac_load_query_font (f, fontname)
+     struct frame *f;
+     char *fontname;
 {
   int size;
   char *name;
@@ -7796,18 +7858,21 @@ XLoadQueryFont (Display *dpy, char *fontname)
   if (strcmp (charset, "iso10646-1") == 0) /* XXX */
     {
       OSStatus err;
-      ATSUAttributeTag tags[] = {kATSUFontTag, kATSUSizeTag,
-                                kATSUQDBoldfaceTag, kATSUQDItalicTag};
-      ByteCount sizes[] = {sizeof (ATSUFontID), sizeof (Fixed),
-                          sizeof (Boolean), sizeof (Boolean)};
+      static const ATSUAttributeTag tags[] =
+       {kATSUFontTag, kATSUSizeTag,
+        kATSUQDBoldfaceTag, kATSUQDItalicTag};
+      static const ByteCount sizes[] =
+       {sizeof (ATSUFontID), sizeof (Fixed),
+        sizeof (Boolean), sizeof (Boolean)};
       static Fixed size_fixed;
       static Boolean bold_p, italic_p;
-      ATSUAttributeValuePtr values[] = {&font_id, &size_fixed,
-                                       &bold_p, &italic_p};
-      ATSUFontFeatureType types[] = {kAllTypographicFeaturesType,
-                                    kDiacriticsType};
-      ATSUFontFeatureSelector selectors[] = {kAllTypeFeaturesOffSelector,
-                                            kDecomposeDiacriticsSelector};
+      static const ATSUAttributeValuePtr values[] =
+       {&font_id, &size_fixed,
+        &bold_p, &italic_p};
+      static const ATSUFontFeatureType types[] =
+       {kAllTypographicFeaturesType, kDiacriticsType};
+      static const ATSUFontFeatureSelector selectors[] =
+       {kAllTypeFeaturesOffSelector, kDecomposeDiacriticsSelector};
       Lisp_Object font_id_cons;
       FMFontStyle style;
 
@@ -7924,7 +7989,8 @@ XLoadQueryFont (Display *dpy, char *fontname)
                                    NULL
 #endif
                                    );
-      if (err != noErr)
+      if (err != noErr
+         || space_bounds->width <= 0 || FONT_HEIGHT (font) <= 0)
        {
          mac_unload_font (&one_mac_display_info, font);
          return NULL;
@@ -7967,23 +8033,13 @@ XLoadQueryFont (Display *dpy, char *fontname)
   else
 #endif
     {
-      GrafPtr port;
-      SInt16 old_fontnum, old_fontsize;
-      Style old_fontface;
       FontInfo the_fontinfo;
       int is_two_byte_font;
 
-      /* Save the current font number used.  */
-      GetPort (&port);
-#if TARGET_API_MAC_CARBON
-      old_fontnum = GetPortTextFont (port);
-      old_fontsize = GetPortTextSize (port);
-      old_fontface = GetPortTextFace (port);
-#else
-      old_fontnum = port->txFont;
-      old_fontsize = port->txSize;
-      old_fontface = port->txFace;
+#if USE_CG_DRAWING
+      mac_prepare_for_quickdraw (f);
 #endif
+      SetPortWindowPort (FRAME_MAC_WINDOW (f));
 
       TextFont (fontnum);
       TextSize (size);
@@ -8065,11 +8121,6 @@ XLoadQueryFont (Display *dpy, char *fontname)
          for (c = 0x21, pcm = space_bounds + 1; c <= 0xff; c++, pcm++)
            mac_query_char_extents (NULL, c, NULL, NULL, pcm, NULL);
        }
-
-      /* Restore previous font number, size and face.  */
-      TextFont (old_fontnum);
-      TextSize (old_fontsize);
-      TextFace (old_fontface);
     }
 
   if (space_bounds)
@@ -8088,6 +8139,8 @@ XLoadQueryFont (Display *dpy, char *fontname)
                                             pcm->width);
            font->min_bounds.ascent   = min (font->min_bounds.ascent,
                                             pcm->ascent);
+           font->min_bounds.descent  = min (font->min_bounds.descent,
+                                            pcm->descent);
 
            font->max_bounds.lbearing = max (font->max_bounds.lbearing,
                                             pcm->lbearing);
@@ -8097,6 +8150,8 @@ XLoadQueryFont (Display *dpy, char *fontname)
                                             pcm->width);
            font->max_bounds.ascent   = max (font->max_bounds.ascent,
                                             pcm->ascent);
+           font->max_bounds.descent  = max (font->max_bounds.descent,
+                                            pcm->descent);
          }
       if (
 #if USE_ATSUI
@@ -8205,7 +8260,7 @@ x_load_font (f, fontname, size)
     fontname = (char *) SDATA (XCAR (font_names));
 
     BLOCK_INPUT;
-    font = (MacFontStruct *) XLoadQueryFont (FRAME_MAC_DISPLAY (f), fontname);
+    font = mac_load_query_font (f, fontname);
     UNBLOCK_INPUT;
     if (!font)
       return NULL;
@@ -8492,7 +8547,7 @@ mac_set_font_info_for_selection (f, face_id, c)
 #endif
 #endif /* ! TARGET_API_MAC_CARBON */
 
-#define M_APPLE 128
+#define M_APPLE 234
 #define I_ABOUT 1
 
 #define WINDOW_RESOURCE 128
@@ -8608,7 +8663,7 @@ extern void terminate_applescript();
    except `clear' (-> <clear>) on the KeyPad, `enter' (-> <kp-enter>)
    on the right of the Cmd key on laptops, and fn + `enter' (->
    <linefeed>). */
-static unsigned char keycode_to_xkeysym_table[] = {
+static const unsigned char keycode_to_xkeysym_table[] = {
   /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   /*0x20*/ 0, 0, 0, 0, 0x0d /*return*/, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -8645,7 +8700,7 @@ static unsigned char keycode_to_xkeysym_table[] = {
    keyboard, and they may not be the same on other types of keyboards.
    If the destination is identical to the source (f1 ... f12), it
    doesn't map `fn' key to a modifier.  */
-static unsigned char fn_keycode_to_keycode_table[] = {
+static const unsigned char fn_keycode_to_keycode_table[] = {
   /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   /*0x20*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -9082,10 +9137,10 @@ mac_tsm_suspend ()
 }
 #endif
 
-static void
+#if !TARGET_API_MAC_CARBON
+void
 do_apple_menu (SInt16 menu_item)
 {
-#if !TARGET_API_MAC_CARBON
   Str255 item_name;
   SInt16 da_driver_refnum;
 
@@ -9096,49 +9151,16 @@ do_apple_menu (SInt16 menu_item)
       GetMenuItemText (GetMenuHandle (M_APPLE), menu_item, item_name);
       da_driver_refnum = OpenDeskAcc (item_name);
     }
-#endif /* !TARGET_API_MAC_CARBON */
-}
-
-void
-do_menu_choice (SInt32 menu_choice)
-{
-  SInt16 menu_id, menu_item;
-
-  menu_id = HiWord (menu_choice);
-  menu_item = LoWord (menu_choice);
-
-  switch (menu_id)
-    {
-    case 0:
-      break;
-
-    case M_APPLE:
-      do_apple_menu (menu_item);
-      break;
-
-    default:
-      {
-        struct frame *f = mac_focus_frame (&one_mac_display_info);
-        MenuHandle menu = GetMenuHandle (menu_id);
-        if (menu)
-          {
-            UInt32 refcon;
-
-            GetMenuItemRefCon (menu, menu_item, &refcon);
-            menubar_selection_callback (f, refcon);
-          }
-      }
-    }
-
-  HiliteMenu (0);
 }
-
+#endif /* !TARGET_API_MAC_CARBON */
 
 /* Handle drags in size box.  Based on code contributed by Ben
    Mesander and IM - Window Manager A.  */
 
 static void
-do_grow_window (WindowPtr w, EventRecord *e)
+do_grow_window (w, e)
+     WindowPtr w;
+     const EventRecord *e;
 {
   Rect limit_rect;
   int rows, columns, width, height;
@@ -9322,8 +9344,8 @@ mac_store_event_ref_as_apple_event (class, id, class_key, id_key,
      Lisp_Object class_key, id_key;
      EventRef event;
      UInt32 num_params;
-     EventParamName *names;
-     EventParamType *types;
+     const EventParamName *names;
+     const EventParamType *types;
 {
   OSStatus err = eventNotHandledErr;
   Lisp_Object binding;
@@ -9384,10 +9406,10 @@ mac_handle_command_event (next_handler, event, data)
 {
   OSStatus result, err;
   HICommand command;
-  static EventParamName names[] = {kEventParamDirectObject,
-                                  kEventParamKeyModifiers};
-  static EventParamType types[] = {typeHICommand,
-                                  typeUInt32};
+  static const EventParamName names[] =
+    {kEventParamDirectObject, kEventParamKeyModifiers};
+  static const EventParamType types[] =
+    {typeHICommand, typeUInt32};
   int num_params = sizeof (names) / sizeof (names[0]);
 
   result = CallNextEventHandler (next_handler, event);
@@ -9411,7 +9433,8 @@ mac_handle_command_event (next_handler, event, data)
 static OSStatus
 init_command_handler ()
 {
-  EventTypeSpec specs[] = {{kEventClassCommand, kEventCommandProcess}};
+  static const EventTypeSpec specs[] =
+    {{kEventClassCommand, kEventCommandProcess}};
   static EventHandlerUPP handle_command_eventUPP = NULL;
 
   if (handle_command_eventUPP == NULL)
@@ -9571,18 +9594,18 @@ mac_handle_window_event (next_handler, event, data)
     case kEventWindowToolbarSwitchMode:
       result = CallNextEventHandler (next_handler, event);
       {
-       static EventParamName names[] = {kEventParamDirectObject,
-                                        kEventParamWindowMouseLocation,
-                                        kEventParamKeyModifiers,
-                                        kEventParamMouseButton,
-                                        kEventParamClickCount,
-                                        kEventParamMouseChord};
-       static EventParamType types[] = {typeWindowRef,
-                                        typeQDPoint,
-                                        typeUInt32,
-                                        typeMouseButton,
-                                        typeUInt32,
-                                        typeUInt32};
+       static const EventParamName names[] = {kEventParamDirectObject,
+                                              kEventParamWindowMouseLocation,
+                                              kEventParamKeyModifiers,
+                                              kEventParamMouseButton,
+                                              kEventParamClickCount,
+                                              kEventParamMouseChord};
+       static const EventParamType types[] = {typeWindowRef,
+                                              typeQDPoint,
+                                              typeUInt32,
+                                              typeMouseButton,
+                                              typeUInt32,
+                                              typeUInt32};
        int num_params = sizeof (names) / sizeof (names[0]);
 
        err = mac_store_event_ref_as_apple_event (0, 0,
@@ -9689,18 +9712,18 @@ mac_handle_font_event (next_handler, event, data)
   OSStatus result, err;
   Lisp_Object id_key;
   int num_params;
-  EventParamName *names;
-  EventParamType *types;
-  static EventParamName names_sel[] = {kEventParamATSUFontID,
-                                      kEventParamATSUFontSize,
-                                      kEventParamFMFontFamily,
-                                      kEventParamFMFontSize,
-                                      kEventParamFontColor};
-  static EventParamType types_sel[] = {typeATSUFontID,
-                                      typeATSUSize,
-                                      typeFMFontFamily,
-                                      typeFMFontSize,
-                                      typeFontColor};
+  const EventParamName *names;
+  const EventParamType *types;
+  static const EventParamName names_sel[] = {kEventParamATSUFontID,
+                                            kEventParamATSUFontSize,
+                                            kEventParamFMFontFamily,
+                                            kEventParamFMFontSize,
+                                            kEventParamFontColor};
+  static const EventParamType types_sel[] = {typeATSUFontID,
+                                            typeATSUSize,
+                                            typeFMFontFamily,
+                                            typeFMFontSize,
+                                            typeFontColor};
 
   result = CallNextEventHandler (next_handler, event);
   if (result != eventNotHandledErr)
@@ -9741,10 +9764,10 @@ mac_handle_text_input_event (next_handler, event, data)
   OSStatus result, err = noErr;
   Lisp_Object id_key = Qnil;
   int num_params;
-  EventParamName *names;
-  EventParamType *types;
+  const EventParamName *names;
+  const EventParamType *types;
   static UInt32 seqno_uaia = 0;
-  static EventParamName names_uaia[] =
+  static const EventParamName names_uaia[] =
     {kEventParamTextInputSendComponentInstance,
      kEventParamTextInputSendRefCon,
      kEventParamTextInputSendSLRec,
@@ -9757,7 +9780,7 @@ mac_handle_text_input_event (next_handler, event, data)
      kEventParamTextInputSendTextServiceEncoding,
      kEventParamTextInputSendTextServiceMacEncoding,
      EVENT_PARAM_TEXT_INPUT_SEQUENCE_NUMBER};
-  static EventParamType types_uaia[] =
+  static const EventParamType types_uaia[] =
     {typeComponentInstance,
      typeLongInteger,
      typeIntlWritingCode,
@@ -9774,12 +9797,12 @@ mac_handle_text_input_event (next_handler, event, data)
      typeUInt32,
      typeUInt32,
      typeUInt32};
-  static EventParamName names_ufke[] =
+  static const EventParamName names_ufke[] =
     {kEventParamTextInputSendComponentInstance,
      kEventParamTextInputSendRefCon,
      kEventParamTextInputSendSLRec,
      kEventParamTextInputSendText};
-  static EventParamType types_ufke[] =
+  static const EventParamType types_ufke[] =
     {typeComponentInstance,
      typeLongInteger,
      typeIntlWritingCode,
@@ -9932,12 +9955,12 @@ mac_store_service_event (event)
   OSStatus err;
   Lisp_Object id_key;
   int num_params;
-  EventParamName *names;
-  EventParamType *types;
-  static EventParamName names_pfm[] = {kEventParamServiceMessageName,
-                                      kEventParamServiceUserData};
-  static EventParamType types_pfm[] = {typeCFStringRef,
-                                      typeCFStringRef};
+  const EventParamName *names;
+  const EventParamType *types;
+  static const EventParamName names_pfm[] =
+    {kEventParamServiceMessageName, kEventParamServiceUserData};
+  static const EventParamType types_pfm[] =
+    {typeCFStringRef, typeCFStringRef};
 
   switch (GetEventKind (event))
     {
@@ -9975,7 +9998,7 @@ install_window_handler (window)
 {
   OSStatus err = noErr;
 #if USE_CARBON_EVENTS
-  EventTypeSpec specs_window[] =
+  static const EventTypeSpec specs_window[] =
     {{kEventClassWindow, kEventWindowUpdate},
      {kEventClassWindow, kEventWindowGetIdealSize},
      {kEventClassWindow, kEventWindowBoundsChanging},
@@ -9993,16 +10016,18 @@ install_window_handler (window)
      {kEventClassWindow, kEventWindowFocusRelinquish},
 #endif
   };
-  EventTypeSpec specs_mouse[] = {{kEventClassMouse, kEventMouseWheelMoved}};
+  static const EventTypeSpec specs_mouse[] =
+    {{kEventClassMouse, kEventMouseWheelMoved}};
   static EventHandlerUPP handle_window_eventUPP = NULL;
   static EventHandlerUPP handle_mouse_eventUPP = NULL;
 #if USE_MAC_FONT_PANEL
-  EventTypeSpec specs_font[] = {{kEventClassFont, kEventFontPanelClosed},
-                               {kEventClassFont, kEventFontSelection}};
+  static const EventTypeSpec specs_font[] =
+    {{kEventClassFont, kEventFontPanelClosed},
+     {kEventClassFont, kEventFontSelection}};
   static EventHandlerUPP handle_font_eventUPP = NULL;
 #endif
 #if USE_MAC_TSM
-  EventTypeSpec specs_text_input[] =
+  static const EventTypeSpec specs_text_input[] =
     {{kEventClassTextInput, kEventTextInputUpdateActiveInputArea},
      {kEventClassTextInput, kEventTextInputUnicodeForKeyEvent},
      {kEventClassTextInput, kEventTextInputOffsetToPos}};
@@ -11219,7 +11244,7 @@ mac_initialize_display_info ()
 
 static XrmDatabase
 mac_make_rdb (xrm_option)
-     char *xrm_option;
+     const char *xrm_option;
 {
   XrmDatabase database;
 
@@ -11658,6 +11683,14 @@ to 4.1, set this to nil.
 NOTE: Not supported on Mac yet.  */);
   x_use_underline_position_properties = 0;
 
+  DEFVAR_BOOL ("x-underline-at-descent-line",
+              &x_underline_at_descent_line,
+     doc: /* *Non-nil means to draw the underline at the same place as the descent line.
+nil means to draw the underline according to the value of the variable
+`x-use-underline-position-properties', which is usually at the baseline
+level.  The default value is nil.  */);
+  x_underline_at_descent_line = 0;
+
   DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
     doc: /* If not nil, Emacs uses toolkit scroll bars.  */);
 #ifdef USE_TOOLKIT_SCROLL_BARS