XEmacs 21.2.28 "Hermes".
[chise/xemacs-chise.git.1] / src / frame-x.c
index 2fddbae..9a25460 100644 (file)
@@ -103,19 +103,34 @@ x_window_to_frame (struct device *d, Window wdesc)
 struct frame *
 x_any_window_to_frame (struct device *d, Window wdesc)
 {
-  Lisp_Object tail, frame;
-  struct frame *f;
-
+  Widget w;
   assert (DEVICE_X_P (d));
 
+  w = XtWindowToWidget (DEVICE_X_DISPLAY (d), wdesc);
+
+  if (!w)
+    return 0;
+
+  /* We used to map over all frames here and then map over all widgets
+     belonging to that frame. However it turns out that this was very fragile
+     as it requires our display stuctures to be in sync _and_ that the
+     loop is told about every new widget somebody adds. Therefore we
+     now let Xt find it for us (which does a bottom-up search which
+     could even be faster) */
+  return  x_any_widget_or_parent_to_frame (d, w);
+}
+
+static struct frame *
+x_find_frame_for_window (struct device *d, Window wdesc)
+{
+  Lisp_Object tail, frame;
+  struct frame *f;
   /* This function was previously written to accept only a window argument
      (and to loop over all devices looking for a matching window), but
      that is incorrect because window ID's are not unique across displays. */
 
   for (tail = DEVICE_FRAME_LIST (d); CONSP (tail); tail = XCDR (tail))
     {
-      int i;
-
       frame = XCAR (tail);
       f = XFRAME (frame);
       /* This frame matches if the window is any of its widgets. */
@@ -138,18 +153,18 @@ x_any_window_to_frame (struct device *d, Window wdesc)
         would incorrectly get sucked away by Emacs if this function matched
         on psheet widgets. */
 
-      for (i = 0; i < FRAME_X_NUM_TOP_WIDGETS (f); i++)
-       {
-         Widget wid = FRAME_X_TOP_WIDGETS (f)[i];
-         if (wid && XtIsManaged (wid) && wdesc == XtWindow (wid))
-           return f;
-       }
-
-#ifdef HAVE_SCROLLBARS
-      /* Match if the window is one of this frame's scrollbars. */
-      if (x_window_is_scrollbar (f, wdesc))
-       return f;
-#endif
+      /* Note: that this called only from
+         x_any_widget_or_parent_to_frame it is unnecessary to iterate
+         over the top level widgets. */
+
+      /* Note:  we use to special case scrollbars but this turns out to be a bad idea
+         because
+         1. We sometimes get events for _unmapped_ scrollbars and our
+         callers don't want us to fail.
+         2. Starting with the 21.2 widget stuff there are now loads of
+         widgets to check and it is easy to forget adding them in a loop here.
+         See x_any_window_to_frame
+         3. We pick up all widgets now anyway. */
     }
 
   return 0;
@@ -160,7 +175,7 @@ x_any_widget_or_parent_to_frame (struct device *d, Widget widget)
 {
   while (widget)
     {
-      struct frame *f = x_any_window_to_frame (d, XtWindow (widget));
+      struct frame *f = x_find_frame_for_window (d, XtWindow (widget));
       if (f)
        return f;
       widget = XtParent (widget);
@@ -278,8 +293,7 @@ x_wm_hack_wm_protocols (Widget widget)
   int need_delete = 1;
   int need_focus = 1;
 
-  if (!XtIsWMShell (widget))
-    abort ();
+  assert (XtIsWMShell (widget));
 
   {
     Atom type, *atoms = 0;
@@ -329,7 +343,7 @@ x_wm_store_class_hints (Widget shell, char *frame_name)
   XSetClassHint (dpy, XtWindow (shell), &classhint);
 }
 
-#ifndef HAVE_SESSION
+#ifndef HAVE_WMCOMMAND
 static void
 x_wm_maybe_store_wm_command (struct frame *f)
 {
@@ -380,7 +394,7 @@ x_wm_maybe_move_wm_command (struct frame *f)
 
     }
 }
-#endif /* !HAVE_SESSION */
+#endif /* !HAVE_WMCOMMAND */
 
 static int
 x_frame_iconified_p (struct frame *f)
@@ -422,9 +436,9 @@ static void
 init_x_prop_symbols (void)
 {
 #define def(sym, rsrc) \
-   pure_put (sym, Qx_resource_name, build_string (rsrc))
+   Fput (sym, Qx_resource_name, build_string (rsrc))
 #define defi(sym,rsrc) \
-   def (sym, rsrc); pure_put (sym, Qintegerp, Qt)
+   def (sym, rsrc); Fput (sym, Qintegerp, Qt)
 
 #if 0 /* this interferes with things. #### fix this right */
   def (Qminibuffer, XtNminibuffer);
@@ -647,13 +661,15 @@ x_set_frame_text_value (struct frame *f, Bufbyte *value,
       {
         CONST char * tmp;
         encoding = DEVICE_XATOM_COMPOUND_TEXT (XDEVICE (FRAME_DEVICE (f)));
-        GET_C_CHARPTR_EXT_CTEXT_DATA_ALLOCA ((CONST char *) value, tmp);
+       TO_EXTERNAL_FORMAT (C_STRING, value,
+                           C_STRING_ALLOCA, tmp,
+                           Qctext);
         new_XtValue = (String) tmp;
         break;
       }
 #endif /* MULE */
 
-  /* ### Caching is device-independent - belongs in update_frame_title. */
+  /* #### Caching is device-independent - belongs in update_frame_title. */
   Xt_GET_VALUE (FRAME_X_SHELL_WIDGET (f), Xt_resource_name, &old_XtValue);
   if (!old_XtValue || strcmp (new_XtValue, old_XtValue))
     {
@@ -749,13 +765,17 @@ x_set_frame_properties (struct frame *f, Lisp_Object plist)
          if (XSTRING_LENGTH (prop) == 0)
            continue;
 
-         GET_C_STRING_CTEXT_DATA_ALLOCA (prop, extprop);
+         TO_EXTERNAL_FORMAT (LISP_STRING, prop,
+                             C_STRING_ALLOCA, extprop,
+                             Qctext);
          if (STRINGP (val))
            {
              CONST Extbyte *extval;
              Extcount extvallen;
 
-             GET_STRING_CTEXT_DATA_ALLOCA (val, extval, extvallen);
+             TO_EXTERNAL_FORMAT (LISP_STRING, val,
+                                 ALLOCA, (extval, extvallen),
+                                 Qctext);
              XtVaSetValues (w, XtVaTypedArg, extprop,
                             XtRString, extval, extvallen + 1,
                             (XtArgVal) NULL);
@@ -996,7 +1016,7 @@ x_cde_destroy_callback (Widget widget, XtPointer clientData,
        {
          for (i = 0; i < dragData->numItems; i++)
            {
-             XtFree(dragData->data.buffers[i].bp);
+             XtFree((char *) dragData->data.buffers[i].bp);
              if (dragData->data.buffers[i].name)
                XtFree(dragData->data.buffers[i].name);
            }
@@ -1089,7 +1109,7 @@ WARNING: can only handle plain/text and file: transfers!
       unsigned int modifier = 0, state = 0;
       char *Ctext;
       int numItems = 0, textlen = 0, pos = 0;
-      struct Lisp_Event *lisp_event = XEVENT(event);
+      Lisp_Event *lisp_event = XEVENT (event);
       Lisp_Object item = Qnil;
       struct gcpro gcpro1;
 
@@ -1151,7 +1171,7 @@ WARNING: can only handle plain/text and file: transfers!
          numItems++;
          item = XCDR (item);
        }
-       
+
       if (numItems)
        {
          /*
@@ -1160,7 +1180,7 @@ WARNING: can only handle plain/text and file: transfers!
           */
          Ctext = (char *)xmalloc (textlen+1);
          Ctext[0] = 0;
-         
+
          item = dragdata;
          while (!NILP (item))
            {
@@ -1176,12 +1196,12 @@ WARNING: can only handle plain/text and file: transfers!
              item = XCDR (item);
            }
          Ctext[pos] = 0;
-         
+
          dnd_convert_cb_rec[0].callback = x_cde_convert_callback;
          dnd_convert_cb_rec[0].closure  = (XtPointer) Ctext;
          dnd_convert_cb_rec[1].callback = NULL;
          dnd_convert_cb_rec[1].closure  = NULL;
-         
+
          dnd_destroy_cb_rec[0].callback = x_cde_destroy_callback;
          dnd_destroy_cb_rec[0].closure  = (XtPointer) Ctext;
          dnd_destroy_cb_rec[1].callback = NULL;
@@ -1198,7 +1218,7 @@ WARNING: can only handle plain/text and file: transfers!
        }
 
       UNGCPRO;
-         
+
       return numItems?Qt:Qnil;
     }
 
@@ -1241,7 +1261,7 @@ x_cde_transfer_callback (Widget widget, XtPointer clientData,
        {
          filePath = transferInfo->dropData->data.files[ii];
          hurl = dnd_url_hexify_string ((char *)filePath, "file:");
-          /* ### Mule-izing required */
+          /* #### Mule-izing required */
          l_data = Fcons (make_string ((Bufbyte* )hurl,
                                       strlen (hurl)),
                          l_data);
@@ -1268,7 +1288,7 @@ x_cde_transfer_callback (Widget widget, XtPointer clientData,
          /* what, if the data is no text, and how can I tell it? */
          l_data = Fcons ( list3 ( list1 ( make_string ((Bufbyte *)"text/plain", 10) ),
                                   make_string ((Bufbyte *)"8bit", 4),
-                                  make_string ((Bufbyte *)transferInfo->dropData->data.buffers[ii].bp, 
+                                  make_string ((Bufbyte *)transferInfo->dropData->data.buffers[ii].bp,
                                                transferInfo->dropData->data.buffers[ii].size) ),
                           l_data );
        }
@@ -1279,7 +1299,7 @@ x_cde_transfer_callback (Widget widget, XtPointer clientData,
     enqueue=0;
 
   /* The Problem: no button and mods from CDE... */
-  if (enqueue)  
+  if (enqueue)
     enqueue_misc_user_event_pos ( frame, Qdragdrop_drop_dispatch,
                                  Fcons (l_type, l_data),
                                  0 /* this is the button */,
@@ -1316,7 +1336,7 @@ The type defaults to DndText (4).
       char *dnd_data = NULL;
       unsigned long dnd_len = 0;
       int dnd_typ = DndText, dnd_dealloc = 0;
-      struct Lisp_Event *lisp_event = XEVENT(event);
+      Lisp_Event *lisp_event = XEVENT (event);
 
       /* only drag if this is really a press */
       if (EVENT_TYPE(lisp_event) != button_press_event)
@@ -1361,15 +1381,13 @@ The type defaults to DndText (4).
          if (!STRINGP (data))
            return Qnil;
 
-         /* and whats with MULE data ??? */
+         /* and what's with MULE data ??? */
          dnd_data = (char *)XSTRING_DATA (data);
          dnd_len  = XSTRING_LENGTH (data) + 1; /* the zero */
 
        }
 
-      /*
-       * not so cross hack that converts a emacs event back to a XEvent
-       */
+      /* not so gross hack that converts an emacs event back to a XEvent */
 
       x_event.xbutton.type = ButtonPress;
       x_event.xbutton.send_event = False;
@@ -1854,7 +1872,9 @@ x_create_widgets (struct frame *f, Lisp_Object lisp_window_id,
 #endif
 
   if (STRINGP (f->name))
-    GET_C_STRING_CTEXT_DATA_ALLOCA (f->name, name);
+    TO_EXTERNAL_FORMAT (LISP_STRING, f->name,
+                       C_STRING_ALLOCA, name,
+                       Qctext);
   else
     name = "emacs";
 
@@ -1883,7 +1903,7 @@ x_create_widgets (struct frame *f, Lisp_Object lisp_window_id,
       char *string;
 
       CHECK_STRING (lisp_window_id);
-      string = (char *) (XSTRING_DATA (lisp_window_id));
+      string = (char *) XSTRING_DATA (lisp_window_id);
       if (string[0] == '0' && (string[1] == 'x' || string[1] == 'X'))
        sscanf (string+2, "%lxu", &window_id);
 #if 0
@@ -1923,9 +1943,9 @@ x_create_widgets (struct frame *f, Lisp_Object lisp_window_id,
       XtSetArg (al[ac], XtNinput, True);       ac++;
       XtSetArg (al[ac], XtNminWidthCells, 10); ac++;
       XtSetArg (al[ac], XtNminHeightCells, 1); ac++;
-      XtSetArg (al[ac], XtNvisual, visual); ac++;
-      XtSetArg (al[ac], XtNdepth, depth);   ac++;
-      XtSetArg (al[ac], XtNcolormap, cmap); ac++;
+      XtSetArg (al[ac], XtNvisual, visual);    ac++;
+      XtSetArg (al[ac], XtNdepth, depth);      ac++;
+      XtSetArg (al[ac], XtNcolormap, cmap);    ac++;
     }
 
   if (!NILP (parent))
@@ -1995,7 +2015,7 @@ x_create_widgets (struct frame *f, Lisp_Object lisp_window_id,
    though, the failure to call the popup callbacks resulted in XEmacs
    not accepting any input.  Bizarre but true.  Stupid but true.
 
-   So, in case there are any other gotches floating out there along
+   So, in case there are any other gotchas floating out there along
    the same lines I've duplicated the majority of XtPopup here.  It
    assumes no grabs and that the widget is not already popped up, both
    valid assumptions for the one place this is called from. */
@@ -2021,12 +2041,6 @@ xemacs_XtPopup (Widget widget)
   Xt_SET_VALUE (widget, XtNmappedWhenManaged, True);
 }
 
-#ifdef HAVE_CDE
-/* Does this have to be non-automatic? */
-/* hack frame to respond to dnd messages */
-static XtCallbackRec dnd_transfer_cb_rec[2];
-#endif /* HAVE_CDE */
-
 /* create the windows for the specified frame and display them.
    Note that the widgets have already been created, and any
    necessary geometry calculations have already been done. */
@@ -2068,9 +2082,9 @@ x_popup_frame (struct frame *f)
        /* tell the window manager about us. */
        x_wm_store_class_hints (shell_widget, XtName (frame_widget));
 
-#ifndef HAVE_SESSION
+#ifndef HAVE_WMCOMMAND
        x_wm_maybe_store_wm_command (f);
-#endif /* HAVE_SESSION */
+#endif /* HAVE_WMCOMMAND */
 
        x_wm_hack_wm_protocols (shell_widget);
       }
@@ -2081,7 +2095,7 @@ x_popup_frame (struct frame *f)
 
 #ifdef HACK_EDITRES
   /* Allow XEmacs to respond to EditRes requests.  See the O'Reilly Xt */
-  /* Instrinsics Programming Manual, Motif Edition, Aug 1993, Sect 14.14, */
+  /* Intrinsics Programming Manual, Motif Edition, Aug 1993, Sect 14.14, */
   /* pp. 483-493. */
   XtAddEventHandler (shell_widget,           /* the shell widget in question */
                     (EventMask) NoEventMask,/* OR with existing mask */
@@ -2092,6 +2106,8 @@ x_popup_frame (struct frame *f)
 
 #ifdef HAVE_CDE
   {
+    XtCallbackRec dnd_transfer_cb_rec[2];
+
     dnd_transfer_cb_rec[0].callback = x_cde_transfer_callback;
     dnd_transfer_cb_rec[0].closure = (XtPointer) f;
     dnd_transfer_cb_rec[1].callback = NULL;
@@ -2101,7 +2117,7 @@ x_popup_frame (struct frame *f)
                         DtDND_FILENAME_TRANSFER | DtDND_BUFFER_TRANSFER,
                         XmDROP_COPY, dnd_transfer_cb_rec,
                         DtNtextIsBuffer, True,
-                        DtNregisterChildren, True, 
+                        DtNregisterChildren, True,
                         DtNpreserveRegistration, False,
                         NULL);
   }
@@ -2183,9 +2199,9 @@ x_init_frame_2 (struct frame *f, Lisp_Object props)
    *   will update the frame title anyway, so nothing is lost.
    * JV:
    *   It turns out it gives problems with FVWMs name based mapping.
-   *   We'll just  need to be carefull in the modeline specs.
+   *   We'll just  need to be careful in the modeline specs.
    */
-  update_frame_title (f); 
+  update_frame_title (f);
 }
 
 static void
@@ -2197,10 +2213,10 @@ x_init_frame_3 (struct frame *f)
 }
 
 static void
-x_mark_frame (struct frame *f, void (*markobj) (Lisp_Object))
+x_mark_frame (struct frame *f)
 {
-  ((markobj) (FRAME_X_ICON_PIXMAP (f)));
-  ((markobj) (FRAME_X_ICON_PIXMAP_MASK (f)));
+  mark_object (FRAME_X_ICON_PIXMAP (f));
+  mark_object (FRAME_X_ICON_PIXMAP_MASK (f));
 }
 
 static void
@@ -2630,41 +2646,57 @@ x_focus_on_frame (struct frame *f)
 static void
 x_delete_frame (struct frame *f)
 {
-  Widget w = FRAME_X_SHELL_WIDGET (f);
+  Display *dpy;
 
-#ifndef HAVE_SESSION
+#ifndef HAVE_WMCOMMAND
   if (FRAME_X_TOP_LEVEL_FRAME_P (f))
     x_wm_maybe_move_wm_command (f);
-#endif /* HAVE_SESSION */
+#endif /* HAVE_WMCOMMAND */
+
+#ifdef HAVE_CDE
+  DtDndDropUnregister (FRAME_X_TEXT_WIDGET (f));
+#endif /* HAVE_CDE */
+
+  assert (FRAME_X_SHELL_WIDGET (f) != 0);
+  dpy = XtDisplay (FRAME_X_SHELL_WIDGET (f));
 
 #ifdef EXTERNAL_WIDGET
-  {
-    Display *dpy = XtDisplay (w);
-    expect_x_error (dpy);
-    /* for obscure reasons having (I think) to do with the internal
-       window-to-widget hierarchy maintained by Xt, we have to call
-       XtUnrealizeWidget() here.  Xt can really suck. */
-    if (f->being_deleted)
-      XtUnrealizeWidget (w);
-    XtDestroyWidget (w);
-    x_error_occurred_p (dpy);
-  }
+  expect_x_error (XtDisplay (FRAME_X_SHELL_WIDGET (f)));
+  /* for obscure reasons having (I think) to do with the internal
+     window-to-widget hierarchy maintained by Xt, we have to call
+     XtUnrealizeWidget() here.  Xt can really suck. */
+  if (f->being_deleted)
+    XtUnrealizeWidget (FRAME_X_SHELL_WIDGET (f));
+  XtDestroyWidget (FRAME_X_SHELL_WIDGET (f));
+  x_error_occurred_p (XtDisplay (FRAME_X_SHELL_WIDGET (f)));
 #else
-  XtDestroyWidget (w);
+  XtDestroyWidget (FRAME_X_SHELL_WIDGET (f));
+  /* make sure the windows are really gone! */
+  /* #### Is this REALLY necessary? */
+  XFlush (dpy);
 #endif /* EXTERNAL_WIDGET */
 
+  FRAME_X_SHELL_WIDGET (f) = 0;
+
   if (FRAME_X_GEOM_FREE_ME_PLEASE (f))
-    xfree (FRAME_X_GEOM_FREE_ME_PLEASE (f));
-  xfree (f->frame_data);
-  f->frame_data = 0;
+    {
+      xfree (FRAME_X_GEOM_FREE_ME_PLEASE (f));
+      FRAME_X_GEOM_FREE_ME_PLEASE (f) = 0;
+    }
+
+  if (f->frame_data)
+    {
+      xfree (f->frame_data);
+      f->frame_data = 0;
+    }
 }
 
 static void
 x_update_frame_external_traits (struct frame* frm, Lisp_Object name)
 {
-  Arg av[10];
+  Arg al[10];
   int ac = 0;
-  Lisp_Object frame = Qnil;
+  Lisp_Object frame;
 
   XSETFRAME(frame, frm);
 
@@ -2676,7 +2708,7 @@ x_update_frame_external_traits (struct frame* frm, Lisp_Object name)
      if (!EQ (color, Vthe_null_color_instance))
        {
         fgc = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (color));
-        XtSetArg (av[ac], XtNforeground, (void *) fgc.pixel); ac++;
+        XtSetArg (al[ac], XtNforeground, (void *) fgc.pixel); ac++;
        }
    }
   else if (EQ (name, Qbackground))
@@ -2687,7 +2719,7 @@ x_update_frame_external_traits (struct frame* frm, Lisp_Object name)
      if (!EQ (color, Vthe_null_color_instance))
        {
         bgc = COLOR_INSTANCE_X_COLOR (XCOLOR_INSTANCE (color));
-        XtSetArg (av[ac], XtNbackground, (void *) bgc.pixel); ac++;
+        XtSetArg (al[ac], XtNbackground, (void *) bgc.pixel); ac++;
        }
 
      /* Really crappy way to force the modeline shadows to be
@@ -2700,14 +2732,14 @@ x_update_frame_external_traits (struct frame* frm, Lisp_Object name)
      Lisp_Object font = FACE_FONT (Vdefault_face, frame, Vcharset_ascii);
 
      if (!EQ (font, Vthe_null_font_instance))
-       XtSetArg (av[ac], XtNfont,
+       XtSetArg (al[ac], XtNfont,
                 (void *) FONT_INSTANCE_X_FONT (XFONT_INSTANCE (font)));
      ac++;
    }
   else
    abort ();
 
-  XtSetValues (FRAME_X_TEXT_WIDGET (frm), av, ac);
+  XtSetValues (FRAME_X_TEXT_WIDGET (frm), al, ac);
 
 #ifdef HAVE_TOOLBARS
   /* Setting the background clears the entire frame area