Fix glitches in previous change.
[bpt/emacs.git] / src / xterm.c
index 5580fbd..474082d 100644 (file)
@@ -98,6 +98,10 @@ extern Widget Xt_app_shell;
 extern void free_frame_menubar ();
 #endif /* USE_X_TOOLKIT */
 
+#ifndef USE_X_TOOLKIT
+#define x_any_window_to_frame x_window_to_frame
+#endif
+
 #ifdef HAVE_X11
 #define XMapWindow XMapRaised          /* Raise them when mapping. */
 #else /* ! defined (HAVE_X11) */
@@ -493,7 +497,7 @@ dumpglyphs (f, left, top, gp, n, hl)
       int g = *gp;
 
       GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
-      cf = GLYPH_FACE (g);
+      cf = FAST_GLYPH_FACE (g);
 
       /* Find the run of consecutive glyphs with the same face-code.
         Extract their character codes into BUF.  */
@@ -502,10 +506,10 @@ dumpglyphs (f, left, top, gp, n, hl)
        {
          g = *gp;
          GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
-         if (GLYPH_FACE (g) != cf)
+         if (FAST_GLYPH_FACE (g) != cf)
            break;
 
-         *cp++ = GLYPH_CHAR (g);
+         *cp++ = FAST_GLYPH_CHAR (g);
          --n;
          ++gp;
        }
@@ -569,7 +573,25 @@ dumpglyphs (f, left, top, gp, n, hl)
                unsigned long mask;
 
                xgcv.background = f->display.x->cursor_pixel;
-               xgcv.foreground = f->display.x->cursor_foreground_pixel;
+               if (face == FRAME_DEFAULT_FACE (f))
+                 xgcv.foreground = f->display.x->cursor_foreground_pixel;
+               else
+                 xgcv.foreground = face->foreground;
+               /* If the glyph would be invisible,
+                  try a different foreground.  */
+               if (xgcv.foreground == xgcv.background)
+                 xgcv.foreground = face->background;
+               if (xgcv.foreground == xgcv.background)
+                 xgcv.foreground = f->display.x->cursor_foreground_pixel;
+               if (xgcv.foreground == xgcv.background)
+                 xgcv.foreground = face->foreground;
+               /* Make sure the cursor is distinct from text in this face.  */
+               if (xgcv.background == face->background
+                   && xgcv.foreground == face->foreground)
+                 {
+                   xgcv.background = face->foreground;
+                   xgcv.foreground = face->background;
+                 }
                xgcv.font = face->font->fid;
                xgcv.graphics_exposures = 0;
                mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
@@ -1542,6 +1564,10 @@ x_find_modifier_meanings ()
          KeyCode code =
            mods->modifiermap[(row * mods->max_keypermod) + col];
 
+         /* Zeroes are used for filler.  Skip them.  */
+         if (code == 0)
+           continue;
+
          /* Are any of this keycode's keysyms a meta key?  */
          {
            int code_col;
@@ -1601,31 +1627,6 @@ x_find_modifier_meanings ()
   XFreeModifiermap (mods);
 }
 
-/* Prepare a menu-event in *RESULT for placement in the input queue.  */
-
-static Lisp_Object
-construct_menu_click (result, event, f)
-     struct input_event *result;
-     XButtonEvent *event;
-     struct frame *f;
-{
-  /* Make the event type no_event; we'll change that when we decide
-     otherwise.  */
-  result->kind = mouse_click;
-  XSET (result->code, Lisp_Int, event->button - Button1);
-  result->timestamp = event->time;
-  result->modifiers = (x_x_to_emacs_modifiers (event->state)
-                      | (event->type == ButtonRelease
-                         ? up_modifier 
-                         : down_modifier));
-
-  {
-    XFASTINT (result->x) = event->x;
-    XFASTINT (result->y) = -1;    /* special meaning for menubar */
-    XSET (result->frame_or_window, Lisp_Frame, f);
-  }
-}
-
 /* Convert between the modifier bits X uses and the modifier bits
    Emacs uses.  */
 static unsigned int
@@ -1676,7 +1677,7 @@ x_is_vendor_fkey (sym)
    that the glyph at X, Y occupies, if BOUNDS != 0.
    If NOCLIP is nonzero, do not force the value into range.  */
 
-static void
+void
 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
      FRAME_PTR f;
      register int pix_x, pix_y;
@@ -1719,6 +1720,16 @@ pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
   *y = pix_y;
 }
 
+void
+glyph_to_pixel_coords (f, x, y, pix_x, pix_y)
+     FRAME_PTR f;
+     register int x, y;
+     register int *pix_x, *pix_y;
+{
+  *pix_x = CHAR_TO_PIXEL_COL (f, x);
+  *pix_y = CHAR_TO_PIXEL_ROW (f, y);
+}
+
 /* Prepare a mouse-event in *RESULT for placement in the input queue.
 
    If the event is a button press, then note that we have grabbed
@@ -1758,12 +1769,39 @@ construct_mouse_click (result, event, f)
   {
     int row, column;
 
+#if 0
     pixel_to_glyph_coords (f, event->x, event->y, &column, &row, NULL, 0);
     XFASTINT (result->x) = column;
     XFASTINT (result->y) = row;
+#endif
+    XFASTINT (result->x) = event->x;
+    XFASTINT (result->y) = event->y;
     XSET (result->frame_or_window, Lisp_Frame, f);
   }
 }
+
+/* Prepare a menu-event in *RESULT for placement in the input queue.  */
+
+static Lisp_Object
+construct_menu_click (result, event, f)
+     struct input_event *result;
+     XButtonEvent *event;
+     struct frame *f;
+{
+  /* Make the event type no_event; we'll change that when we decide
+     otherwise.  */
+  result->kind = mouse_click;
+  XSET (result->code, Lisp_Int, event->button - Button1);
+  result->timestamp = event->time;
+  result->modifiers = (x_x_to_emacs_modifiers (event->state)
+                      | (event->type == ButtonRelease
+                         ? up_modifier 
+                         : down_modifier));
+
+  XFASTINT (result->x) = event->x;
+  XSETINT (result->y, -1);
+  XSET (result->frame_or_window, Lisp_Frame, f);
+}
 \f
 /* Function to report a mouse movement to the mainstream Emacs code.
    The input handler calls this.
@@ -1933,7 +1971,7 @@ XTmouse_position (f, bar_window, part, x, y, time)
               never use them in that case.)  */
 
            /* Is win one of our frames?  */
-           f1 = x_window_to_frame (win);
+           f1 = x_any_window_to_frame (win);
          }
       
        /* If not, is it one of our scroll bars?  */
@@ -1951,10 +1989,11 @@ XTmouse_position (f, bar_window, part, x, y, time)
 
        if (f1)
          {
-           /* Ok, we found a frame.  Convert from pixels to characters
-              and store all the values.  */
+           int ignore1, ignore2;
 
-           pixel_to_glyph_coords (f1, win_x, win_y, &win_x, &win_y,
+           /* Ok, we found a frame.  Store all the values.  */
+
+           pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
                                   &last_mouse_glyph, x_mouse_grabbed);
 
            *bar_window = Qnil;
@@ -1989,9 +2028,9 @@ x_window_to_scroll_bar (window_id)
        XGCTYPE (tail) == Lisp_Cons;
        tail = XCONS (tail)->cdr)
     {
-      Lisp_Object frame = XCONS (tail)->car;
-      Lisp_Object bar, condemned;
+      Lisp_Object frame, bar, condemned;
 
+      frame = XCONS (tail)->car;
       /* All elements of Vframe_list should be frames.  */
       if (XGCTYPE (frame) != Lisp_Frame)
        abort ();
@@ -2423,12 +2462,6 @@ x_scroll_bar_expose (bar, event)
                  /* x, y, width, height */
                  0, 0, XINT (bar->width) - 1, XINT (bar->height) - 1);
 
-  /* Draw another line to make the extra-thick border on the right.  */
-  XFillRectangle (x_current_display, w, gc,
-
-                 /* x, y, width, height */
-                 XINT (bar->width) - 2, 1, 1, XINT (bar->height) - 2);
-
   UNBLOCK_INPUT;
 }
 
@@ -2819,7 +2852,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
                  }
                else if (event.xclient.data.l[0] == Xatom_wm_delete_window)
                  {
-                   struct frame *f = x_window_to_frame (event.xclient.window);
+                   struct frame *f = x_any_window_to_frame (event.xclient.window);
 
                    if (f)
                      {
@@ -3126,7 +3159,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
 #ifdef HAVE_X11
        case KeyPress:
-         f = x_window_to_frame (event.xkey.window);
+         f = x_any_window_to_frame (event.xkey.window);
 
          if (f != 0)
            {
@@ -3319,7 +3352,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
             then a mere LeaveNotify is enough to free you.  */
 
        case EnterNotify:
-         f = x_window_to_frame (event.xcrossing.window);
+         f = x_any_window_to_frame (event.xcrossing.window);
 
          if (event.xcrossing.focus)            /* Entered Window */
            {
@@ -3340,7 +3373,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
          break;
 
        case FocusIn:
-         f = x_window_to_frame (event.xfocus.window);
+         f = x_any_window_to_frame (event.xfocus.window);
          if (event.xfocus.detail != NotifyPointer) 
            x_focus_event_frame = f;
          if (f)
@@ -3352,7 +3385,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
 
 
        case LeaveNotify:
-         f = x_window_to_frame (event.xcrossing.window);
+         f = x_any_window_to_frame (event.xcrossing.window);
 
          if (event.xcrossing.focus)
            {
@@ -3374,7 +3407,7 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
          break;
 
        case FocusOut:
-         f = x_window_to_frame (event.xfocus.window);
+         f = x_any_window_to_frame (event.xfocus.window);
          if (event.xfocus.detail != NotifyPointer
              && f == x_focus_event_frame)
            x_focus_event_frame = 0;
@@ -3445,11 +3478,43 @@ XTread_socket (sd, bufp, numchars, waitp, expected)
          break;
 
        case ConfigureNotify:
+          f = x_any_window_to_frame (event.xconfigure.window);
 #ifdef USE_X_TOOLKIT
-             /* process done in widget.c */
-         goto OTHER;
+          if (f
+              && ! event.xconfigure.send_event
+              && (event.xconfigure.window == XtWindow (f->display.x->widget)))
+            {
+              Window win, child;
+              int win_x, win_y;
+
+              /* Find the position of the outside upper-left corner of
+                 the window, in the root coordinate system.  Don't
+                 refer to the parent window here; we may be processing
+                 this event after the window manager has changed our
+                 parent, but before we have reached the ReparentNotify.  */
+              XTranslateCoordinates (x_current_display,
+
+                                     /* From-window, to-window.  */
+                                     XtWindow (f->display.x->widget),
+                                     ROOT_WINDOW,
+
+                                     /* From-position, to-position.  */
+                                     -event.xconfigure.border_width,
+                                     -event.xconfigure.border_width,
+                                     &win_x, &win_y,
+
+                                     /* Child of win.  */
+                                     &child);
+              event.xconfigure.x = win_x;
+              event.xconfigure.y = win_y;
+
+              f->display.x->pixel_width = event.xconfigure.width;
+              f->display.x->pixel_height = event.xconfigure.height;
+              f->display.x->left_pos = event.xconfigure.x;
+              f->display.x->top_pos = event.xconfigure.y;
+            }
+          goto OTHER;
 #else /* not USE_X_TOOLKIT */
-         f = x_window_to_frame (event.xconfigure.window);
          if (f)
            {
              int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
@@ -4273,6 +4338,17 @@ x_check_errors (format)
     }
 }
 
+/* Nonzero if we had any X protocol errors since we did x_catch_errors.  */
+
+int
+x_had_errors_p ()
+{
+  /* Make sure to catch any errors incurred so far.  */
+  XSync (x_current_display, False);
+
+  return x_caught_error_message[0] != 0;
+}
+
 /* Stop catching X protocol errors and let them make Emacs die.  */
 
 void
@@ -4428,8 +4504,9 @@ x_new_font (f, fontname)
     }
 
   {
-    Lisp_Object lispy_name = build_string (fontname);
+    Lisp_Object lispy_name;
 
+    lispy_name = build_string (fontname);
 
     /* Free the information from XListFonts.  The data
        we actually retain comes from XLoadQueryFont.  */