XEmacs 21.2.33 "Melpomene".
[chise/xemacs-chise.git.1] / src / frame.c
1 /* Generic frame functions.
2    Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3    Copyright (C) 1995, 1996 Ben Wing.
4    Copyright (C) 1995 Sun Microsystems, Inc.
5
6 This file is part of XEmacs.
7
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Synched up with: FSF 19.30. */
24
25 /* This file has been Mule-ized. */
26
27 #include <config.h>
28 #include "lisp.h"
29
30 #include "buffer.h"             /* for Vbuffer_alist */
31 #include "console.h"
32 #include "events.h"
33 #include "extents.h"
34 #include "faces.h"
35 #include "frame.h"
36 #include "glyphs.h"
37 #include "gutter.h"
38 #include "menubar.h"
39 #include "redisplay.h"
40 #include "scrollbar.h"
41 #include "window.h"
42
43 Lisp_Object Vselect_frame_hook, Qselect_frame_hook;
44 Lisp_Object Vdeselect_frame_hook, Qdeselect_frame_hook;
45 Lisp_Object Vcreate_frame_hook, Qcreate_frame_hook;
46 Lisp_Object Vdelete_frame_hook, Qdelete_frame_hook;
47 Lisp_Object Vmouse_enter_frame_hook, Qmouse_enter_frame_hook;
48 Lisp_Object Vmouse_leave_frame_hook, Qmouse_leave_frame_hook;
49 Lisp_Object Vmap_frame_hook, Qmap_frame_hook;
50 Lisp_Object Vunmap_frame_hook, Qunmap_frame_hook;
51 int  allow_deletion_of_last_visible_frame;
52 Lisp_Object Vadjust_frame_function;
53 Lisp_Object Vmouse_motion_handler;
54 Lisp_Object Vsynchronize_minibuffers;
55 Lisp_Object Qsynchronize_minibuffers;
56 Lisp_Object Qbuffer_predicate;
57 Lisp_Object Qmake_initial_minibuffer_frame;
58 Lisp_Object Qcustom_initialize_frame;
59
60 /* We declare all these frame properties here even though many of them
61    are currently only used in frame-x.c, because we should generalize
62    them. */
63
64 Lisp_Object Qminibuffer;
65 Lisp_Object Qunsplittable;
66 Lisp_Object Qinternal_border_width;
67 Lisp_Object Qtop_toolbar_shadow_color;
68 Lisp_Object Qbottom_toolbar_shadow_color;
69 Lisp_Object Qbackground_toolbar_color;
70 Lisp_Object Qtop_toolbar_shadow_pixmap;
71 Lisp_Object Qbottom_toolbar_shadow_pixmap;
72 Lisp_Object Qtoolbar_shadow_thickness;
73 Lisp_Object Qscrollbar_placement;
74 Lisp_Object Qinter_line_space;
75 Lisp_Object Qvisual_bell;
76 Lisp_Object Qbell_volume;
77 Lisp_Object Qpointer_background;
78 Lisp_Object Qpointer_color;
79 Lisp_Object Qtext_pointer;
80 Lisp_Object Qspace_pointer;
81 Lisp_Object Qmodeline_pointer;
82 Lisp_Object Qgc_pointer;
83 Lisp_Object Qinitially_unmapped;
84 Lisp_Object Quse_backing_store;
85 Lisp_Object Qborder_color;
86 Lisp_Object Qborder_width;
87
88 Lisp_Object Qframep, Qframe_live_p;
89 Lisp_Object Qdelete_frame;
90
91 Lisp_Object Qframe_title_format, Vframe_title_format;
92 Lisp_Object Qframe_icon_title_format, Vframe_icon_title_format;
93
94 Lisp_Object Vdefault_frame_name;
95 Lisp_Object Vdefault_frame_plist;
96
97 Lisp_Object Vframe_icon_glyph;
98
99 Lisp_Object Qhidden;
100
101 Lisp_Object Qvisible, Qiconic, Qinvisible, Qvisible_iconic, Qinvisible_iconic;
102 Lisp_Object Qnomini, Qvisible_nomini, Qiconic_nomini, Qinvisible_nomini;
103 Lisp_Object Qvisible_iconic_nomini, Qinvisible_iconic_nomini;
104
105 Lisp_Object Qset_specifier, Qset_glyph_image, Qset_face_property;
106 Lisp_Object Qface_property_instance;
107
108 Lisp_Object Qframe_property_alias;
109
110 /* If this is non-nil, it is the frame that make-frame is currently
111    creating.  We can't set the current frame to this in case the
112    debugger goes off because it would try and display to it.  However,
113    there are some places which need to reference it which have no
114    other way of getting it if it isn't the selected frame. */
115 Lisp_Object Vframe_being_created;
116 Lisp_Object Qframe_being_created;
117
118 static void store_minibuf_frame_prop (struct frame *f, Lisp_Object val);
119 static struct display_line title_string_display_line;
120 /* Used by generate_title_string. Global because they get used so much that
121    the dynamic allocation time adds up. */
122 static Emchar_dynarr *title_string_emchar_dynarr;
123
124 \f
125 static Lisp_Object
126 mark_frame (Lisp_Object obj)
127 {
128   struct frame *f = XFRAME (obj);
129
130 #define MARKED_SLOT(x) mark_object (f->x)
131 #include "frameslots.h"
132
133   mark_subwindow_cachels (f->subwindow_cachels);
134
135   if (FRAME_LIVE_P (f)) /* device is nil for a dead frame */
136     MAYBE_FRAMEMETH (f, mark_frame, (f));
137
138   return Qnil;
139 }
140
141 static void
142 print_frame (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
143 {
144   struct frame *frm = XFRAME (obj);
145   char buf[200];
146
147   if (print_readably)
148     error ("printing unreadable object #<frame %s 0x%x>",
149            XSTRING_DATA (frm->name), frm->header.uid);
150
151   sprintf (buf, "#<%s-frame ", !FRAME_LIVE_P (frm) ? "dead" :
152            FRAME_TYPE_NAME (frm));
153   write_c_string (buf, printcharfun);
154   print_internal (frm->name, printcharfun, 1);
155   sprintf (buf, " 0x%x>", frm->header.uid);
156   write_c_string (buf, printcharfun);
157 }
158
159 DEFINE_LRECORD_IMPLEMENTATION ("frame", frame,
160                                mark_frame, print_frame, 0, 0, 0, 0,
161                                struct frame);
162 \f
163 static void
164 nuke_all_frame_slots (struct frame *f)
165 {
166 #define MARKED_SLOT(x)  f->x = Qnil
167 #include "frameslots.h"
168 }
169
170 /* Allocate a new frame object and set all its fields to reasonable
171    values.  The root window is created but the minibuffer will be done
172    later. */
173
174 static struct frame *
175 allocate_frame_core (Lisp_Object device)
176 {
177   /* This function can GC */
178   Lisp_Object frame;
179   Lisp_Object root_window;
180   struct frame *f = alloc_lcrecord_type (struct frame, &lrecord_frame);
181
182   zero_lcrecord (f);
183   nuke_all_frame_slots (f);
184   XSETFRAME (frame, f);
185
186   f->device = device;
187   f->framemeths = XDEVICE (device)->devmeths;
188   f->buffer_alist = Fcopy_sequence (Vbuffer_alist);
189
190   root_window = allocate_window ();
191   XWINDOW (root_window)->frame = frame;
192
193   /* 10 is arbitrary,
194      Just so that there is "something there."
195      Correct size will be set up later with change_frame_size.  */
196
197   f->width  = 10;
198   f->height = 10;
199
200   XWINDOW (root_window)->pixel_width = 10;
201   XWINDOW (root_window)->pixel_height = 9;
202
203   /* The size of the minibuffer window is now set in x_create_frame
204      in xfns.c. */
205
206   f->root_window = root_window;
207   f->selected_window = root_window;
208   f->last_nonminibuf_window = root_window;
209
210   /* cache of subwindows visible on frame */
211   f->subwindow_cachels    = Dynarr_new (subwindow_cachel);
212
213   /* associated exposure ignore list */
214   f->subwindow_exposures = 0;
215   f->subwindow_exposures_tail = 0;
216
217   FRAME_SET_PAGENUMBER (f, 1);
218
219   /* Choose a buffer for the frame's root window.  */
220   XWINDOW (root_window)->buffer = Qt;
221   {
222     Lisp_Object buf;
223
224     buf = Fcurrent_buffer ();
225     /* If buf is a 'hidden' buffer (i.e. one whose name starts with
226        a space), try to find another one.  */
227     if (string_char (XSTRING (Fbuffer_name (buf)), 0) == ' ')
228       buf = Fother_buffer (buf, Qnil, Qnil);
229     Fset_window_buffer (root_window, buf, Qnil);
230   }
231
232   return f;
233 }
234
235 static void
236 setup_normal_frame (struct frame *f)
237 {
238   Lisp_Object mini_window;
239   Lisp_Object frame;
240
241   XSETFRAME (frame, f);
242
243   mini_window = allocate_window ();
244   XWINDOW (f->root_window)->next = mini_window;
245   XWINDOW (mini_window)->prev = f->root_window;
246   XWINDOW (mini_window)->mini_p = Qt;
247   XWINDOW (mini_window)->frame = frame;
248   f->minibuffer_window = mini_window;
249   f->has_minibuffer = 1;
250
251   XWINDOW (mini_window)->buffer = Qt;
252   Fset_window_buffer (mini_window, Vminibuffer_zero, Qt);
253 }
254
255 /* Make a frame using a separate minibuffer window on another frame.
256    MINI_WINDOW is the minibuffer window to use.  nil means use the
257    default-minibuffer-frame.  */
258
259 static void
260 setup_frame_without_minibuffer (struct frame *f, Lisp_Object mini_window)
261 {
262   /* This function can GC */
263   Lisp_Object device = f->device;
264
265   if (!NILP (mini_window))
266     CHECK_LIVE_WINDOW (mini_window);
267
268   if (!NILP (mini_window)
269       && !EQ (DEVICE_CONSOLE (XDEVICE (device)),
270               FRAME_CONSOLE (XFRAME (XWINDOW (mini_window)->frame))))
271     error ("frame and minibuffer must be on the same console");
272
273   if (NILP (mini_window))
274     {
275       struct console *con = XCONSOLE (FRAME_CONSOLE (f));
276       /* Use default-minibuffer-frame if possible.  */
277       if (!FRAMEP (con->default_minibuffer_frame)
278           || ! FRAME_LIVE_P (XFRAME (con->default_minibuffer_frame)))
279         {
280           /* If there's no minibuffer frame to use, create one.  */
281           con->default_minibuffer_frame
282             = call1 (Qmake_initial_minibuffer_frame, device);
283         }
284       mini_window = XFRAME (con->default_minibuffer_frame)->minibuffer_window;
285     }
286
287   /* Install the chosen minibuffer window, with proper buffer.  */
288   store_minibuf_frame_prop (f, mini_window);
289   Fset_window_buffer (mini_window, Vminibuffer_zero, Qt);
290 }
291
292 /* Make a frame containing only a minibuffer window.  */
293
294 static void
295 setup_minibuffer_frame (struct frame *f)
296 {
297   /* This function can GC */
298   /* First make a frame containing just a root window, no minibuffer.  */
299   Lisp_Object mini_window;
300   Lisp_Object frame;
301
302   XSETFRAME (frame, f);
303
304   f->no_split = 1;
305   f->has_minibuffer = 1;
306
307   /* Now label the root window as also being the minibuffer.
308      Avoid infinite looping on the window chain by marking next pointer
309      as nil. */
310
311   mini_window = f->minibuffer_window = f->root_window;
312   XWINDOW (mini_window)->mini_p = Qt;
313   XWINDOW (mini_window)->next   = Qnil;
314   XWINDOW (mini_window)->prev   = Qnil;
315   XWINDOW (mini_window)->frame  = frame;
316
317   /* Put the proper buffer in that window.  */
318
319   Fset_window_buffer (mini_window, Vminibuffer_zero, Qt);
320 }
321
322 static Lisp_Object
323 make_sure_its_a_fresh_plist (Lisp_Object foolist)
324 {
325   if (CONSP (Fcar (foolist)))
326     {
327       /* looks like an alist to me. */
328       foolist = Fcopy_alist (foolist);
329       foolist = Fdestructive_alist_to_plist (foolist);
330     }
331   else
332     foolist = Fcopy_sequence (foolist);
333
334   return foolist;
335 }
336
337 DEFUN ("make-frame", Fmake_frame, 0, 2, "", /*
338 Create and return a new frame, displaying the current buffer.
339 Runs the functions listed in `create-frame-hook' after frame creation.
340
341 Optional argument PROPS is a property list (a list of alternating
342 keyword-value specifications) of properties for the new frame.
343 \(An alist is accepted for backward compatibility but should not
344 be passed in.)
345
346 See `set-frame-properties', `default-x-frame-plist', and
347 `default-tty-frame-plist' for the specially-recognized properties.
348 */
349        (props, device))
350 {
351   struct frame *f;
352   struct device *d;
353   Lisp_Object frame = Qnil, name = Qnil, minibuf;
354   struct gcpro gcpro1, gcpro2, gcpro3;
355   int speccount = specpdl_depth ();
356   int first_frame_on_device = 0;
357   int first_frame_on_console = 0;
358
359   d = decode_device (device);
360   XSETDEVICE (device, d);
361
362   /* PROPS and NAME may be freshly-created, so make sure to GCPRO. */
363   GCPRO3 (frame, props, name);
364
365   props = make_sure_its_a_fresh_plist (props);
366   if (DEVICE_SPECIFIC_FRAME_PROPS (d))
367     /* Put the device-specific props before the more general ones so
368        that they override them. */
369     props = nconc2 (props,
370                     make_sure_its_a_fresh_plist
371                     (*DEVICE_SPECIFIC_FRAME_PROPS (d)));
372   props = nconc2 (props, make_sure_its_a_fresh_plist (Vdefault_frame_plist));
373   Fcanonicalize_lax_plist (props, Qnil);
374
375   name = Flax_plist_get (props, Qname, Qnil);
376   if (!NILP (name))
377     CHECK_STRING (name);
378   else if (STRINGP (Vdefault_frame_name))
379     name = Vdefault_frame_name;
380   else
381     name = build_string ("emacs");
382
383   if (!NILP (Fstring_match (make_string ((const Bufbyte *) "\\.", 2), name,
384                             Qnil, Qnil)))
385     signal_simple_error (". not allowed in frame names", name);
386
387   f = allocate_frame_core (device);
388   XSETFRAME (frame, f);
389
390   specbind (Qframe_being_created, name);
391   f->name = name;
392
393   FRAMEMETH (f, init_frame_1, (f, props));
394
395   minibuf = Flax_plist_get (props, Qminibuffer, Qunbound);
396   if (UNBOUNDP (minibuf))
397     {
398       /* If minibuf is unspecified, then look for a minibuffer X resource. */
399       /* #### Not implemented any more.  We need to fix things up so
400          that we search out all X resources and append them to the end of
401          props, above.  This is the only way in general to assure
402          coherent behavior for all frame properties/resources/etc. */
403     }
404   else
405     props = Flax_plist_remprop (props, Qminibuffer);
406
407   if (EQ (minibuf, Qnone) || NILP (minibuf))
408     setup_frame_without_minibuffer (f, Qnil);
409   else if (EQ (minibuf, Qonly))
410     setup_minibuffer_frame (f);
411   else if (WINDOWP (minibuf))
412     setup_frame_without_minibuffer (f, minibuf);
413   else if (EQ (minibuf, Qt) || UNBOUNDP (minibuf))
414     setup_normal_frame (f);
415   else
416     signal_simple_error ("Invalid value for `minibuffer'", minibuf);
417
418   update_frame_window_mirror (f);
419
420   if (initialized && !DEVICE_STREAM_P (d))
421     {
422       if (!NILP (f->minibuffer_window))
423         reset_face_cachels (XWINDOW (f->minibuffer_window));
424       reset_face_cachels (XWINDOW (f->root_window));
425     }
426
427   /* If no frames on this device formerly existed, say this is the
428      first frame.  It kind of assumes that frameless devices don't
429      exist, but it shouldn't be too harmful.  */
430   if (NILP (DEVICE_FRAME_LIST (d)))
431     first_frame_on_device = 1;
432
433   /* This *must* go before the init_*() methods.  Those functions
434      call Lisp code, and if any of them causes a warning to be displayed
435      and the *Warnings* buffer to be created, it won't get added to
436      the frame-specific version of the buffer-alist unless the frame
437      is accessible from the device. */
438
439 #if 0
440   DEVICE_FRAME_LIST (d) = nconc2 (DEVICE_FRAME_LIST (d), Fcons (frame, Qnil));
441 #endif
442   DEVICE_FRAME_LIST (d) = Fcons (frame, DEVICE_FRAME_LIST (d));
443   RESET_CHANGED_SET_FLAGS;
444
445   /* Now make sure that the initial cached values are set correctly.
446      Do this after the init_frame method is called because that may
447      do things (e.g. create widgets) that are necessary for the
448      specifier value-changed methods to work OK. */
449   recompute_all_cached_specifiers_in_frame (f);
450
451   if (!DEVICE_STREAM_P (d))
452     {
453       init_frame_faces (f);
454
455 #ifdef HAVE_SCROLLBARS
456       /* Finish up resourcing the scrollbars. */
457       init_frame_scrollbars (f);
458 #endif
459
460 #ifdef HAVE_TOOLBARS
461       /* Create the initial toolbars.  We have to do this after the frame
462          methods are called because it may potentially call some things itself
463          which depend on the normal frame methods having initialized
464          things. */
465       init_frame_toolbars (f);
466 #endif
467       reset_face_cachels (XWINDOW (FRAME_SELECTED_WINDOW (f)));
468       reset_glyph_cachels (XWINDOW (FRAME_SELECTED_WINDOW (f)));
469       reset_subwindow_cachels (f);
470
471       change_frame_size (f, f->height, f->width, 0);
472     }
473
474   MAYBE_FRAMEMETH (f, init_frame_2, (f, props));
475   Fset_frame_properties (frame, props);
476   MAYBE_FRAMEMETH (f, init_frame_3, (f));
477
478   /* Hallelujah, praise the lord. */
479   f->init_finished = 1;
480
481   /* If this is the first frame on the device, make it the selected one. */
482   if (first_frame_on_device && NILP (DEVICE_SELECTED_FRAME (d)))
483     set_device_selected_frame (d, frame);
484
485   /* If at startup or if the current console is a stream console
486      (usually also at startup), make this console the selected one
487      so that messages show up on it. */
488   if (NILP (Fselected_console ()) ||
489       CONSOLE_STREAM_P (XCONSOLE (Fselected_console ())))
490     Fselect_console (DEVICE_CONSOLE (d));
491
492   first_frame_on_console =
493     (first_frame_on_device &&
494      XINT (Flength (CONSOLE_DEVICE_LIST (XCONSOLE (DEVICE_CONSOLE (d)))))
495      == 1);
496
497   /* #### all this calling of frame methods at various odd times
498      is somewhat of a mess.  It's necessary to do it this way due
499      to strange console-type-specific things that need to be done. */
500   MAYBE_FRAMEMETH (f, after_init_frame, (f, first_frame_on_device,
501                                          first_frame_on_console));
502
503   if (!DEVICE_STREAM_P (d))
504     {
505       /* Now initialise the gutters. This won't change the frame size,
506          but is needed as input to the layout that change_frame_size
507          will eventually do. Unfortunately gutter sizing code relies
508          on the frame in question being visible so we can't do this
509          earlier. */
510       init_frame_gutters (f);
511
512       change_frame_size (f, f->height, f->width, 0);
513     }
514
515   if (first_frame_on_device)
516     {
517       if (first_frame_on_console)
518         va_run_hook_with_args (Qcreate_console_hook, 1, DEVICE_CONSOLE (d));
519       va_run_hook_with_args (Qcreate_device_hook, 1, device);
520     }
521   va_run_hook_with_args (Qcreate_frame_hook, 1, frame);
522
523   /* Initialize custom-specific stuff. */
524   if (!UNBOUNDP (symbol_function (XSYMBOL (Qcustom_initialize_frame))))
525     call1 (Qcustom_initialize_frame, frame);
526
527   unbind_to (speccount, Qnil);
528
529   UNGCPRO;
530   return frame;
531 }
532
533 \f
534 /* this function should be used in most cases when a Lisp function is passed
535    a FRAME argument.  Use this unless you don't accept nil == current frame
536    (in which case, do a CHECK_LIVE_FRAME() and then an XFRAME()) or you
537    allow dead frames.  Note that very few functions should accept dead
538    frames.  It could be argued that functions should just do nothing when
539    given a dead frame, but the presence of a dead frame usually indicates
540    an oversight in the Lisp code that could potentially lead to strange
541    results and so it is better to catch the error early.
542
543    If you only accept X frames, use decode_x_frame(), which does what this
544    function does but also makes sure the frame is an X frame. */
545
546 struct frame *
547 decode_frame (Lisp_Object frame)
548 {
549   if (NILP (frame))
550     return selected_frame ();
551
552   CHECK_LIVE_FRAME (frame);
553   return XFRAME (frame);
554 }
555
556 struct frame *
557 decode_frame_or_selected (Lisp_Object cdf)
558 {
559   if (CONSOLEP (cdf))
560     cdf = CONSOLE_SELECTED_DEVICE (decode_console (cdf));
561   if (DEVICEP (cdf))
562     cdf = DEVICE_SELECTED_FRAME (decode_device (cdf));
563   return decode_frame (cdf);
564 }
565
566 Lisp_Object
567 make_frame (struct frame *f)
568 {
569   Lisp_Object frame;
570   XSETFRAME (frame, f);
571   return frame;
572 }
573
574 \f
575 /*
576  * window size changes are held up during critical regions.  Afterwards,
577  * we want to deal with any delayed changes.
578  */
579 void
580 hold_frame_size_changes (void)
581 {
582   in_display = 1;
583 }
584
585 void
586 unhold_one_frame_size_changes (struct frame *f)
587 {
588   in_display = 0;
589
590   if (f->size_change_pending)
591     change_frame_size (f, f->new_height, f->new_width, 0);
592 }
593
594 void
595 unhold_frame_size_changes (void)
596 {
597   Lisp_Object frmcons, devcons, concons;
598
599   FRAME_LOOP_NO_BREAK (frmcons, devcons, concons)
600     unhold_one_frame_size_changes (XFRAME (XCAR (frmcons)));
601 }
602
603 void
604 invalidate_vertical_divider_cache_in_frame (struct frame *f)
605 {
606   /* Invalidate cached value of needs_vertical_divider_p in
607      every and all windows */
608   map_windows (f, invalidate_vertical_divider_cache_in_window, 0);
609 }
610 \f
611 /*
612  * Frame size may change due to changes in scrollbars, toolbars,
613  * default font etc. These changes are applied early in redisplay
614  * frame.
615  */
616 void
617 adjust_frame_size (struct frame *f)
618 {
619   int keep_char_size = 0;
620   Lisp_Object frame;
621   XSETFRAME (frame, f);
622
623   if (!f->size_slipped)
624     return;
625
626   /* Don't adjust tty frames. #### May break when TTY have menubars.
627      Then, write an Vadjust_frame_function which will return t for TTY
628      frames. Another solution is frame_size_fixed_p method for TTYs,
629      which always returned yes it's fixed.
630   */
631   if (!FRAME_WIN_P (f))
632     {
633       CLEAR_FRAME_SIZE_SLIPPED (f);
634       return;
635     }
636
637   /* frame_size_fixed_p tells that frame size cannot currently
638      be changed change due to external conditions */
639   if (!FRAMEMETH_OR_GIVEN (f, frame_size_fixed_p, (f), 0))
640     {
641       if (NILP (Vadjust_frame_function))
642         keep_char_size = 1;
643       else if (EQ (Vadjust_frame_function, Qt))
644         keep_char_size = 0;
645       else
646         keep_char_size =
647           NILP (call1_trapping_errors ("Error in adjust-frame-function",
648                                        Vadjust_frame_function, frame));
649
650       if (keep_char_size)
651         Fset_frame_size (frame, make_int (FRAME_CHARWIDTH(f)),
652                          make_int (FRAME_CHARHEIGHT(f)), Qnil);
653     }
654
655   if (!keep_char_size)
656     {
657       int height, width;
658       pixel_to_char_size (f, FRAME_PIXWIDTH(f), FRAME_PIXHEIGHT(f),
659                           &width, &height);
660       change_frame_size (f, height, width, 0);
661       CLEAR_FRAME_SIZE_SLIPPED (f);
662     }
663 }
664
665 /*
666  * This is a "specifier changed in frame" handler for various specifiers
667  * changing which causes frame size adjustment
668  */
669 void
670 frame_size_slipped (Lisp_Object specifier, struct frame *f,
671                     Lisp_Object oldval)
672 {
673   MARK_FRAME_SIZE_SLIPPED(f);
674 }
675 \f
676 DEFUN ("framep", Fframep, 1, 1, 0, /*
677 Return non-nil if OBJECT is a frame.
678 Also see `frame-live-p'.
679 Note that FSF Emacs kludgily returns a value indicating what type of
680 frame this is.  Use the cleaner function `frame-type' for that.
681 */
682        (object))
683 {
684   return FRAMEP (object) ? Qt : Qnil;
685 }
686
687 DEFUN ("frame-live-p", Fframe_live_p, 1, 1, 0, /*
688 Return non-nil if OBJECT is a frame which has not been deleted.
689 */
690        (object))
691 {
692   return FRAMEP (object) && FRAME_LIVE_P (XFRAME (object)) ? Qt : Qnil;
693 }
694
695 \f
696 DEFUN ("focus-frame", Ffocus_frame, 1, 1, 0, /*
697 Select FRAME and give it the window system focus.
698 This function is not affected by the value of `focus-follows-mouse'.
699 */
700        (frame))
701 {
702   CHECK_LIVE_FRAME (frame);
703
704   MAYBE_DEVMETH (XDEVICE (FRAME_DEVICE (XFRAME (frame))), focus_on_frame,
705                  (XFRAME (frame)));
706   /* FRAME will be selected by the time we receive the next event.
707      However, it is better to select it explicitly now, in case the
708      Lisp code depends on frame being selected.  */
709   Fselect_frame (frame);
710   return Qnil;
711 }
712
713 /* Called from Fselect_window() */
714 void
715 select_frame_1 (Lisp_Object frame)
716 {
717   struct frame *f = XFRAME (frame);
718   Lisp_Object old_selected_frame = Fselected_frame (Qnil);
719
720   if (EQ (frame, old_selected_frame))
721     return;
722
723   /* now select the frame's device */
724   set_device_selected_frame (XDEVICE (FRAME_DEVICE (f)), frame);
725   select_device_1 (FRAME_DEVICE (f));
726
727   update_frame_window_mirror (f);
728 }
729
730 DEFUN ("select-frame", Fselect_frame, 1, 1, 0, /*
731 Select the frame FRAME.
732 Subsequent editing commands apply to its selected window.
733 The selection of FRAME lasts until the next time the user does
734 something to select a different frame, or until the next time this
735 function is called.
736
737 Note that this does not actually cause the window-system focus to be
738 set to this frame, or the `select-frame-hook' or `deselect-frame-hook'
739 to be run, until the next time that XEmacs is waiting for an event.
740
741 Also note that when focus-follows-mouse is non-nil, the frame
742 selection is temporary and is reverted when the current command
743 terminates, much like the buffer selected by `set-buffer'.  In order
744 to effect a permanent focus change, use `focus-frame'.
745 */
746        (frame))
747 {
748   CHECK_LIVE_FRAME (frame);
749
750   /* select the frame's selected window.  This will call
751      selected_frame_1(). */
752   Fselect_window (FRAME_SELECTED_WINDOW (XFRAME (frame)), Qnil);
753
754   /* Nothing should be depending on the return value of this function.
755      But, of course, there is stuff out there which is. */
756   return frame;
757 }
758
759 /* use this to retrieve the currently selected frame.  You should use
760    this in preference to Fselected_frame (Qnil) unless you are prepared
761    to handle the possibility of there being no selected frame (this
762    happens at some points during startup). */
763
764 struct frame *
765 selected_frame (void)
766 {
767   Lisp_Object device = Fselected_device (Qnil);
768   Lisp_Object frame = DEVICE_SELECTED_FRAME (XDEVICE (device));
769   if (NILP (frame))
770     signal_simple_error ("No frames exist on device", device);
771   return XFRAME (frame);
772 }
773
774 /* use this instead of XFRAME (DEVICE_SELECTED_FRAME (d)) to catch
775    the possibility of there being no frames on the device (just created).
776    There is no point doing this inside of redisplay because errors
777    cause an abort(), indicating a flaw in the logic, and error_check_frame()
778    will catch this just as well. */
779
780 struct frame *
781 device_selected_frame (struct device *d)
782 {
783   Lisp_Object frame = DEVICE_SELECTED_FRAME (d);
784   if (NILP (frame))
785     {
786       Lisp_Object device;
787       XSETDEVICE (device, d);
788       signal_simple_error ("No frames exist on device", device);
789     }
790   return XFRAME (frame);
791 }
792
793 #if 0 /* FSFmacs */
794
795 xxDEFUN ("handle-switch-frame", Fhandle_switch_frame, 1, 2, "e", /*
796 Handle a switch-frame event EVENT.
797 Switch-frame events are usually bound to this function.
798 A switch-frame event tells Emacs that the window manager has requested
799 that the user's events be directed to the frame mentioned in the event.
800 This function selects the selected window of the frame of EVENT.
801
802 If EVENT is frame object, handle it as if it were a switch-frame event
803 to that frame.
804 */
805          (frame, no_enter))
806 {
807   /* Preserve prefix arg that the command loop just cleared.  */
808   XCONSOLE (Vselected_console)->prefix_arg = Vcurrent_prefix_arg;
809 #if 0 /* unclean! */
810   run_hook (Qmouse_leave_buffer_hook);
811 #endif
812   return do_switch_frame (frame, no_enter, 0);
813 }
814
815 /* A load of garbage. */
816 xxDEFUN ("ignore-event", Fignore_event, 0, 0, "", /*
817 Do nothing, but preserve any prefix argument already specified.
818 This is a suitable binding for iconify-frame and make-frame-visible.
819 */
820          ())
821 {
822   struct console *c = XCONSOLE (Vselected_console);
823
824   c->prefix_arg = Vcurrent_prefix_arg;
825   return Qnil;
826 }
827
828 #endif /* 0 */
829
830 DEFUN ("selected-frame", Fselected_frame, 0, 1, 0, /*
831 Return the frame that is now selected on device DEVICE.
832 If DEVICE is not specified, the selected device will be used.
833 If no frames exist on the device, nil is returned.
834 */
835        (device))
836 {
837   if (NILP (device) && NILP (Fselected_device (Qnil)))
838     return Qnil; /* happens early in temacs */
839   return DEVICE_SELECTED_FRAME (decode_device (device));
840 }
841
842 Lisp_Object
843 frame_first_window (struct frame *f)
844 {
845   Lisp_Object w = f->root_window;
846
847   while (1)
848     {
849       if (! NILP (XWINDOW (w)->hchild))
850         w = XWINDOW (w)->hchild;
851       else if (! NILP (XWINDOW (w)->vchild))
852         w = XWINDOW (w)->vchild;
853       else
854         break;
855     }
856
857   return w;
858 }
859
860 DEFUN ("active-minibuffer-window", Factive_minibuffer_window, 0, 0, 0, /*
861 Return the currently active minibuffer window, or nil if none.
862 */
863        ())
864 {
865   return minibuf_level ? minibuf_window : Qnil;
866 }
867
868 DEFUN ("last-nonminibuf-frame", Flast_nonminibuf_frame, 0, 1, 0, /*
869 Return the most-recently-selected non-minibuffer-only frame on CONSOLE.
870 This will always be the same as (selected-frame device) unless the
871 selected frame is a minibuffer-only frame.
872 CONSOLE defaults to the selected console if omitted.
873 */
874        (console))
875 {
876   Lisp_Object result;
877
878   XSETCONSOLE (console, decode_console (console));
879   /* Just in case the machinations in delete_frame_internal() resulted
880      in the last-nonminibuf-frame getting out of sync, make sure and
881      return the selected frame if it's acceptable. */
882   result = Fselected_frame (CONSOLE_SELECTED_DEVICE (XCONSOLE (console)));
883   if (!NILP (result) && !FRAME_MINIBUF_ONLY_P (XFRAME (result)))
884     return result;
885   return CONSOLE_LAST_NONMINIBUF_FRAME (XCONSOLE (console));
886 }
887
888 DEFUN ("frame-root-window", Fframe_root_window, 0, 1, 0, /*
889 Return the root-window of FRAME.
890 If omitted, FRAME defaults to the currently selected frame.
891 */
892        (frame))
893 {
894   struct frame *f = decode_frame (frame);
895   return FRAME_ROOT_WINDOW (f);
896 }
897
898 DEFUN ("frame-selected-window", Fframe_selected_window, 0, 1, 0, /*
899 Return the selected window of frame object FRAME.
900 If omitted, FRAME defaults to the currently selected frame.
901 */
902        (frame))
903 {
904   struct frame *f = decode_frame (frame);
905   return FRAME_SELECTED_WINDOW (f);
906 }
907
908 void
909 set_frame_selected_window (struct frame *f, Lisp_Object window)
910 {
911   assert (XFRAME (WINDOW_FRAME (XWINDOW (window))) == f);
912   f->selected_window = window;
913   if (!MINI_WINDOW_P (XWINDOW (window)) || FRAME_MINIBUF_ONLY_P (f))
914     {
915       if (!EQ (f->last_nonminibuf_window, window))
916         {
917 #ifdef HAVE_TOOLBARS
918           MARK_TOOLBAR_CHANGED;
919 #endif
920           MARK_GUTTER_CHANGED;
921         }
922       f->last_nonminibuf_window = window;
923     }
924 }
925
926 DEFUN ("set-frame-selected-window", Fset_frame_selected_window, 2, 2, 0, /*
927 Set the selected window of frame object FRAME to WINDOW.
928 If FRAME is nil, the selected frame is used.
929 If FRAME is the selected frame, this makes WINDOW the selected window.
930 */
931        (frame, window))
932 {
933   XSETFRAME (frame, decode_frame (frame));
934   CHECK_LIVE_WINDOW (window);
935
936   if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
937     error ("In `set-frame-selected-window', WINDOW is not on FRAME");
938
939   if (XFRAME (frame) == selected_frame ())
940     return Fselect_window (window, Qnil);
941
942   set_frame_selected_window (XFRAME (frame), window);
943   return window;
944 }
945
946 \f
947 DEFUN ("frame-device", Fframe_device, 0, 1, 0, /*
948 Return the device that FRAME is on.
949 If omitted, FRAME defaults to the currently selected frame.
950 */
951        (frame))
952 {
953   return FRAME_DEVICE (decode_frame (frame));
954 }
955
956 int
957 is_surrogate_for_selected_frame (struct frame *f)
958 {
959   struct device *d = XDEVICE (f->device);
960   struct frame *dsf = device_selected_frame (d);
961
962   /* Can't be a surrogate for ourselves. */
963   if (f == dsf)
964     return 0;
965
966   if (!FRAME_HAS_MINIBUF_P (dsf) &&
967       f == XFRAME (WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (dsf)))))
968     return 1;
969   else
970     return 0;
971 }
972
973 static int
974 frame_matches_frametype (Lisp_Object frame, Lisp_Object type)
975 {
976   struct frame *f = XFRAME (frame);
977
978   if (WINDOWP (type))
979     {
980       CHECK_LIVE_WINDOW (type);
981
982       if (EQ (FRAME_MINIBUF_WINDOW (f), type)
983           /* Check that F either is, or has forwarded
984              its focus to, TYPE's frame.  */
985           && (EQ (WINDOW_FRAME (XWINDOW (type)), frame)
986               || EQ (WINDOW_FRAME (XWINDOW (type)),
987                      FRAME_FOCUS_FRAME (f))))
988         return 1;
989       else
990         return 0;
991     }
992
993 #if 0 /* FSFmacs */
994   if (EQ (type, Qvisible) || EQ (type, Qiconic) || EQ (type, Qvisible_iconic)
995       || EQ (type, Qvisible_nomini) || EQ (type, Qiconic_nomini)
996       || EQ (type, Qvisible_iconic_nomini))
997     FRAME_SAMPLE_VISIBILITY (f);
998 #endif
999
1000   if (NILP (type))
1001     type = Qnomini;
1002   if (ZEROP (type))
1003     type = Qvisible_iconic;
1004
1005   if (EQ (type, Qvisible))
1006     return FRAME_VISIBLE_P (f);
1007   if (EQ (type, Qiconic))
1008     return FRAME_ICONIFIED_P (f);
1009   if (EQ (type, Qinvisible))
1010     return !FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f);
1011   if (EQ (type, Qvisible_iconic))
1012     return FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1013   if (EQ (type, Qinvisible_iconic))
1014     return !FRAME_VISIBLE_P (f);
1015
1016   if (EQ (type, Qnomini))
1017     return !FRAME_MINIBUF_ONLY_P (f);
1018   if (EQ (type, Qvisible_nomini))
1019     return FRAME_VISIBLE_P (f) && !FRAME_MINIBUF_ONLY_P (f);
1020   if (EQ (type, Qiconic_nomini))
1021     return FRAME_ICONIFIED_P (f) && !FRAME_MINIBUF_ONLY_P (f);
1022   if (EQ (type, Qinvisible_nomini))
1023     return !FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f) &&
1024       !FRAME_MINIBUF_ONLY_P (f);
1025   if (EQ (type, Qvisible_iconic_nomini))
1026     return ((FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
1027             && !FRAME_MINIBUF_ONLY_P (f));
1028   if (EQ (type, Qinvisible_iconic_nomini))
1029     return !FRAME_VISIBLE_P (f) && !FRAME_MINIBUF_ONLY_P (f);
1030
1031   return 1;
1032 }
1033
1034 int
1035 device_matches_console_spec (Lisp_Object frame, Lisp_Object device,
1036                              Lisp_Object console)
1037 {
1038   if (EQ (console, Qwindow_system))
1039     return DEVICE_WIN_P (XDEVICE (device));
1040   if (NILP (console))
1041     console = (DEVICE_CONSOLE (XDEVICE (FRAME_DEVICE (XFRAME (frame)))));
1042   if (DEVICEP (console))
1043     return EQ (device, console);
1044   if (CONSOLEP (console))
1045     return EQ (DEVICE_CONSOLE (XDEVICE (device)), console);
1046   if (valid_console_type_p (console))
1047     return EQ (DEVICE_TYPE (XDEVICE (device)), console);
1048   return 1;
1049 }
1050
1051 /* Return the next frame in the frame list after FRAME.
1052    FRAMETYPE and CONSOLE control which frames and devices
1053    are considered; see `next-frame'. */
1054
1055 static Lisp_Object
1056 next_frame_internal (Lisp_Object frame, Lisp_Object frametype,
1057                      Lisp_Object console, int called_from_delete_device)
1058 {
1059   int passed = 0;
1060   int started_over = 0;
1061
1062   /* If this frame is dead, it won't be in frame_list, and we'll loop
1063      forever.  Forestall that.  */
1064   CHECK_LIVE_FRAME (frame);
1065
1066   while (1)
1067     {
1068       Lisp_Object devcons, concons;
1069
1070       DEVICE_LOOP_NO_BREAK (devcons, concons)
1071         {
1072           Lisp_Object device = XCAR (devcons);
1073           Lisp_Object frmcons;
1074
1075           if (!device_matches_console_spec (frame, device, console))
1076             continue;
1077
1078           DEVICE_FRAME_LOOP (frmcons, XDEVICE (device))
1079             {
1080               Lisp_Object f = XCAR (frmcons);
1081               if (passed)
1082                 {
1083                   /* #### Doing this here is bad and is now
1084                      unnecessary.  The real bug was that f->iconified
1085                      was never, ever updated unless a user explicitly
1086                      called frame-iconified-p.  That has now been
1087                      fixed.  With this change removed all of the other
1088                      changes made to support this routine having the
1089                      called_from_delete_device arg could be removed.
1090                      But it is too close to release to do that now. */
1091 #if 0
1092                   /* Make sure the visibility and iconified flags are
1093                      up-to-date unless we're being deleted. */
1094                   if (!called_from_delete_device)
1095                     {
1096                       Fframe_iconified_p (f);
1097                       Fframe_visible_p (f);
1098                     }
1099 #endif
1100
1101                   /* Decide whether this frame is eligible to be returned.  */
1102
1103                   /* If we've looped all the way around without finding any
1104                      eligible frames, return the original frame.  */
1105                   if (EQ (f, frame))
1106                     return f;
1107
1108                   if (frame_matches_frametype (f, frametype))
1109                     return f;
1110                 }
1111
1112               if (EQ (frame, f))
1113                 passed++;
1114             }
1115         }
1116       /* We hit the end of the list, and need to start over again. */
1117       if (started_over)
1118         return Qnil;
1119       started_over++;
1120     }
1121 }
1122
1123 Lisp_Object
1124 next_frame (Lisp_Object frame, Lisp_Object frametype, Lisp_Object console)
1125 {
1126   return next_frame_internal (frame, frametype, console, 0);
1127 }
1128
1129 /* Return the previous frame in the frame list before FRAME.
1130    FRAMETYPE and CONSOLE control which frames and devices
1131    are considered; see `next-frame'. */
1132
1133 Lisp_Object
1134 prev_frame (Lisp_Object frame, Lisp_Object frametype, Lisp_Object console)
1135 {
1136   Lisp_Object devcons, concons;
1137   Lisp_Object prev;
1138
1139   /* If this frame is dead, it won't be in frame_list, and we'll loop
1140      forever.  Forestall that.  */
1141   CHECK_LIVE_FRAME (frame);
1142
1143   prev = Qnil;
1144   DEVICE_LOOP_NO_BREAK (devcons, concons)
1145     {
1146       Lisp_Object device = XCAR (devcons);
1147       Lisp_Object frmcons;
1148
1149       if (!device_matches_console_spec (frame, device, console))
1150         continue;
1151
1152       DEVICE_FRAME_LOOP (frmcons, XDEVICE (device))
1153         {
1154           Lisp_Object f = XCAR (frmcons);
1155
1156           if (EQ (frame, f) && !NILP (prev))
1157             return prev;
1158
1159           /* Decide whether this frame is eligible to be returned,
1160              according to frametype.  */
1161
1162           if (frame_matches_frametype (f, frametype))
1163             prev = f;
1164
1165         }
1166     }
1167
1168   /* We've scanned the entire list.  */
1169   if (NILP (prev))
1170     /* We went through the whole frame list without finding a single
1171        acceptable frame.  Return the original frame.  */
1172     return frame;
1173   else
1174     /* There were no acceptable frames in the list before FRAME; otherwise,
1175        we would have returned directly from the loop.  Since PREV is the last
1176        acceptable frame in the list, return it.  */
1177     return prev;
1178 }
1179
1180 DEFUN ("next-frame", Fnext_frame, 0, 3, 0, /*
1181 Return the next frame of the right type in the frame list after FRAME.
1182 FRAMETYPE controls which frames are eligible to be returned; all
1183 others will be skipped.  Note that if there is only one eligible
1184 frame, then `next-frame' called repeatedly will always return
1185 the same frame, and if there is no eligible frame, then FRAME is
1186 returned.
1187
1188 Possible values for FRAMETYPE are
1189
1190 'visible                 Consider only frames that are visible.
1191 'iconic                  Consider only frames that are iconic.
1192 'invisible               Consider only frames that are invisible
1193                          (this is different from iconic).
1194 'visible-iconic          Consider frames that are visible or iconic.
1195 'invisible-iconic        Consider frames that are invisible or iconic.
1196 'nomini                  Consider all frames except minibuffer-only ones.
1197 'visible-nomini          Like `visible' but omits minibuffer-only frames.
1198 'iconic-nomini           Like `iconic' but omits minibuffer-only frames.
1199 'invisible-nomini        Like `invisible' but omits minibuffer-only frames.
1200 'visible-iconic-nomini   Like `visible-iconic' but omits minibuffer-only
1201                          frames.
1202 'invisible-iconic-nomini Like `invisible-iconic' but omits minibuffer-only
1203                          frames.
1204 any other value          Consider all frames.
1205
1206 If FRAMETYPE is omitted, 'nomini is used.  A FRAMETYPE of 0 (a number)
1207 is treated like 'iconic, for backwards compatibility.
1208
1209 If FRAMETYPE is a window, include only its own frame and any frame now
1210 using that window as the minibuffer.
1211
1212 Optional third argument CONSOLE controls which consoles or devices the
1213 returned frame may be on.  If CONSOLE is a console, return frames only
1214 on that console.  If CONSOLE is a device, return frames only on that
1215 device.  If CONSOLE is a console type, return frames only on consoles
1216 of that type.  If CONSOLE is 'window-system, return any frames on any
1217 window-system consoles.  If CONSOLE is nil or omitted, return frames only
1218 on the FRAME's console.  Otherwise, all frames are considered.
1219 */
1220        (frame, frametype, console))
1221 {
1222   XSETFRAME (frame, decode_frame (frame));
1223
1224   return next_frame (frame, frametype, console);
1225 }
1226
1227 DEFUN ("previous-frame", Fprevious_frame, 0, 3, 0, /*
1228 Return the next frame of the right type in the frame list after FRAME.
1229 FRAMETYPE controls which frames are eligible to be returned; all
1230 others will be skipped.  Note that if there is only one eligible
1231 frame, then `previous-frame' called repeatedly will always return
1232 the same frame, and if there is no eligible frame, then FRAME is
1233 returned.
1234
1235 See `next-frame' for an explanation of the FRAMETYPE and CONSOLE
1236 arguments.
1237 */
1238        (frame, frametype, console))
1239 {
1240   XSETFRAME (frame, decode_frame (frame));
1241
1242   return prev_frame (frame, frametype, console);
1243 }
1244
1245 /* Return any frame for which PREDICATE is non-zero, or return Qnil
1246    if there aren't any. */
1247
1248 Lisp_Object
1249 find_some_frame (int (*predicate) (Lisp_Object, void *),
1250                  void *closure)
1251 {
1252   Lisp_Object framecons, devcons, concons;
1253
1254   FRAME_LOOP_NO_BREAK (framecons, devcons, concons)
1255     {
1256       Lisp_Object frame = XCAR (framecons);
1257
1258       if ((predicate) (frame, closure))
1259         return frame;
1260     }
1261
1262   return Qnil;
1263 }
1264
1265 \f
1266
1267 /* extern void free_line_insertion_deletion_costs (struct frame *f); */
1268
1269 /* Return 1 if it is ok to delete frame F;
1270    0 if all frames aside from F are invisible.
1271    (Exception: if F is a stream frame, it's OK to delete if
1272    any other frames exist.) */
1273
1274 static int
1275 other_visible_frames_internal (struct frame *f, int called_from_delete_device)
1276 {
1277   Lisp_Object frame;
1278
1279   XSETFRAME (frame, f);
1280   if (FRAME_STREAM_P (f))
1281     return !EQ (frame, next_frame_internal (frame, Qt, Qt,
1282                                             called_from_delete_device));
1283   return !EQ (frame, next_frame_internal (frame, Qvisible_iconic_nomini, Qt,
1284                                           called_from_delete_device));
1285 }
1286
1287 int
1288 other_visible_frames (struct frame *f)
1289 {
1290   return other_visible_frames_internal (f, 0);
1291 }
1292
1293 /* Delete frame F.
1294
1295    If FORCE is non-zero, allow deletion of the only frame.
1296
1297    If CALLED_FROM_DELETE_DEVICE is non-zero, then, if
1298    deleting the last frame on a device, just delete it,
1299    instead of calling `delete-device'.
1300
1301    If FROM_IO_ERROR is non-zero, then the frame is gone due
1302    to an I/O error.  This affects what happens if we exit
1303    (we do an emergency exit instead of `save-buffers-kill-emacs'.)
1304 */
1305
1306 void
1307 delete_frame_internal (struct frame *f, int force,
1308                        int called_from_delete_device,
1309                        int from_io_error)
1310 {
1311   /* This function can GC */
1312   int minibuffer_selected;
1313   struct device *d;
1314   struct console *con;
1315   Lisp_Object frame;
1316   Lisp_Object device;
1317   Lisp_Object console;
1318   struct gcpro gcpro1;
1319
1320   /* OK to delete an already deleted frame. */
1321   if (! FRAME_LIVE_P (f))
1322     return;
1323
1324   XSETFRAME (frame, f);
1325   GCPRO1 (frame);
1326
1327   device = FRAME_DEVICE (f);
1328   d = XDEVICE (device);
1329   console = DEVICE_CONSOLE (d);
1330   con = XCONSOLE (console);
1331
1332   if (!called_from_delete_device &&
1333       !(MAYBE_INT_DEVMETH (d, device_implementation_flags, ())
1334         & XDEVIMPF_FRAMELESS_OK))
1335     {
1336       /* If we're deleting the only non-minibuffer frame on the
1337          device, delete the device. */
1338       if (EQ (frame, next_frame (frame, Qnomini, FRAME_DEVICE (f))))
1339         {
1340           delete_device_internal (d, force, 0, from_io_error);
1341           UNGCPRO;
1342           return;
1343         }
1344     }
1345
1346   /* In FSF, delete-frame will not normally allow you to delete the
1347      last visible frame.  This was too annoying, so we changed it to the
1348      only frame.  However, this would let people shoot themselves by
1349      deleting all frames which were either visible or iconified and thus
1350      losing any way of communicating with the still running XEmacs process.
1351      So we put it back.  */
1352   if (!force && !allow_deletion_of_last_visible_frame &&
1353       !other_visible_frames_internal (f, called_from_delete_device))
1354     error ("Attempt to delete the sole visible or iconified frame");
1355
1356   /* Does this frame have a minibuffer, and is it the surrogate
1357      minibuffer for any other frame?  */
1358   if (FRAME_HAS_MINIBUF_P (f))
1359     {
1360       Lisp_Object frmcons, devcons, concons;
1361
1362       FRAME_LOOP_NO_BREAK (frmcons, devcons, concons)
1363         {
1364           Lisp_Object this = XCAR (frmcons);
1365
1366           if (! EQ (this, frame)
1367               && EQ (frame, (WINDOW_FRAME
1368                              (XWINDOW
1369                               (FRAME_MINIBUF_WINDOW (XFRAME (this)))))))
1370             {
1371               /* We've found another frame whose minibuffer is on
1372                  this frame. */
1373               signal_simple_error
1374                 ("Attempt to delete a surrogate minibuffer frame", frame);
1375             }
1376         }
1377     }
1378
1379   /* Test for popup frames hanging around. */
1380   /* Deletion of a parent frame with popups is deadly. */
1381   {
1382     Lisp_Object frmcons, devcons, concons;
1383
1384     FRAME_LOOP_NO_BREAK (frmcons, devcons, concons)
1385       {
1386         Lisp_Object this = XCAR (frmcons);
1387
1388
1389         if (! EQ (this, frame))
1390           {
1391             struct device *devcons_d = XDEVICE (XCAR (devcons));
1392             if (EQ (frame, DEVMETH_OR_GIVEN (devcons_d, get_frame_parent,
1393                                              (XFRAME (this)),
1394                                              Qnil)))
1395               /* We've found a popup frame whose parent is this frame. */
1396               signal_simple_error
1397                 ("Attempt to delete a frame with live popups", frame);
1398           }
1399       }
1400   }
1401
1402   /* Before here, we haven't made any dangerous changes (just checked for
1403      error conditions).  Now run the delete-frame-hook.  Remember that
1404      user code there could do any number of dangerous things, including
1405      signalling an error. */
1406
1407   va_run_hook_with_args (Qdelete_frame_hook, 1, frame);
1408
1409   if (!FRAME_LIVE_P (f)) /* Make sure the delete-frame-hook didn't */
1410     {                    /* go ahead and delete anything. */
1411       UNGCPRO;
1412       return;
1413     }
1414
1415   /* Call the delete-device-hook and delete-console-hook now if
1416      appropriate, before we do any dangerous things -- they too could
1417      signal an error. */
1418   if (XINT (Flength (DEVICE_FRAME_LIST (d))) == 1)
1419     {
1420       va_run_hook_with_args (Qdelete_device_hook, 1, device);
1421       if (!FRAME_LIVE_P (f)) /* Make sure the delete-device-hook didn't */
1422         {                    /* go ahead and delete anything. */
1423           UNGCPRO;
1424           return;
1425         }
1426
1427       if (XINT (Flength (CONSOLE_DEVICE_LIST (con))) == 1)
1428         {
1429           va_run_hook_with_args (Qdelete_console_hook, 1, console);
1430           if (!FRAME_LIVE_P (f)) /* Make sure the delete-console-hook didn't */
1431             {                    /* go ahead and delete anything. */
1432               UNGCPRO;
1433               return;
1434             }
1435         }
1436     }
1437
1438   minibuffer_selected = EQ (minibuf_window, Fselected_window (Qnil));
1439
1440   /* If we were focused on this frame, then we're not any more.
1441      Assume that we lost the focus; that way, the call to
1442      Fselect_frame() below won't end up making us explicitly
1443      focus on another frame, which is generally undesirable in
1444      a point-to-type world.  If our mouse ends up sitting over
1445      another frame, we will receive a FocusIn event and end up
1446      making that frame the selected frame.
1447
1448      #### This may not be an ideal solution in a click-to-type
1449      world (in that case, we might want to explicitly choose
1450      another frame to have the focus, rather than relying on
1451      the WM, which might focus on a frame in a different app
1452      or focus on nothing at all).  But there's no easy way
1453      to detect which focus model we're running on, and the
1454      alternative is more heinous. */
1455
1456   if (EQ (frame, DEVICE_FRAME_WITH_FOCUS_REAL (d)))
1457     DEVICE_FRAME_WITH_FOCUS_REAL (d) = Qnil;
1458   if (EQ (frame, DEVICE_FRAME_WITH_FOCUS_FOR_HOOKS (d)))
1459     DEVICE_FRAME_WITH_FOCUS_FOR_HOOKS (d) = Qnil;
1460   if (EQ (frame, DEVICE_FRAME_THAT_OUGHT_TO_HAVE_FOCUS (d)))
1461     DEVICE_FRAME_THAT_OUGHT_TO_HAVE_FOCUS (d) = Qnil;
1462
1463   /* Don't allow the deleted frame to remain selected.
1464      Note that in the former scheme of things, this would
1465      have caused us to regain the focus.  This no longer
1466      applies (see above); I think the new behavior is more
1467      logical.  If someone disagrees, it can always be
1468      changed (or a new user variable can be introduced, ugh.) */
1469   if (EQ (frame, DEVICE_SELECTED_FRAME (d)))
1470     {
1471       Lisp_Object next;
1472
1473       /* If this is a popup frame, select its parent if possible.
1474          Otherwise, find another visible frame; if none, just take any frame.
1475          First try the same device, then the same console. */
1476
1477       next = DEVMETH_OR_GIVEN (d, get_frame_parent, (f), Qnil);
1478       if (NILP (next) || EQ (next, frame) || ! FRAME_LIVE_P (XFRAME (next)))
1479         next = next_frame_internal (frame, Qvisible, device,
1480                                     called_from_delete_device);
1481       if (NILP (next) || EQ (next, frame))
1482         next = next_frame_internal (frame, Qvisible, console,
1483                                     called_from_delete_device);
1484       if (NILP (next) || EQ (next, frame))
1485         next = next_frame_internal (frame, Qvisible, Qt,
1486                                     called_from_delete_device);
1487       if (NILP (next) || EQ (next, frame))
1488         next = next_frame_internal (frame, Qt, device,
1489                                     called_from_delete_device);
1490       if (NILP (next) || EQ (next, frame))
1491         next = next_frame_internal (frame, Qt, console,
1492                                     called_from_delete_device);
1493       if (NILP (next) || EQ (next, frame))
1494         next = next_frame_internal (frame, Qt, Qt, called_from_delete_device);
1495
1496       /* if we haven't found another frame at this point
1497          then there aren't any. */
1498       if (NILP (next) || EQ (next, frame))
1499         ;
1500       else
1501         {
1502           int did_select = 0;
1503           /* if this is the global selected frame, select another one. */
1504           if (EQ (frame, Fselected_frame (Qnil)))
1505             {
1506                 Fselect_frame (next);
1507                 did_select = 1;
1508             }
1509           /*
1510            * If the new frame we just selected is on a different
1511            * device then we still need to change DEVICE_SELECTED_FRAME(d)
1512            * to a live frame, if there are any left on this device.
1513            */
1514           if (!EQ (device, FRAME_DEVICE(XFRAME(next))))
1515             {
1516                 Lisp_Object next_f =
1517                     next_frame_internal (frame, Qt, device,
1518                                          called_from_delete_device);
1519                 if (NILP (next_f) || EQ (next_f, frame))
1520                   set_device_selected_frame (d, Qnil);
1521                 else
1522                   set_device_selected_frame (d, next_f);
1523             }
1524           else if (! did_select)
1525             set_device_selected_frame (d, next);
1526
1527         }
1528     }
1529
1530   /* Don't allow minibuf_window to remain on a deleted frame.  */
1531   if (EQ (f->minibuffer_window, minibuf_window))
1532     {
1533       struct frame *sel_frame = selected_frame ();
1534       Fset_window_buffer (sel_frame->minibuffer_window,
1535                           XWINDOW (minibuf_window)->buffer, Qt);
1536       minibuf_window = sel_frame->minibuffer_window;
1537
1538       /* If the dying minibuffer window was selected,
1539          select the new one.  */
1540       if (minibuffer_selected)
1541         Fselect_window (minibuf_window, Qnil);
1542     }
1543
1544   /* After this point, no errors must be allowed to occur. */
1545
1546 #ifdef HAVE_MENUBARS
1547   free_frame_menubars (f);
1548 #endif
1549 #ifdef HAVE_SCROLLBARS
1550   free_frame_scrollbars (f);
1551 #endif
1552 #ifdef HAVE_TOOLBARS
1553   free_frame_toolbars (f);
1554 #endif
1555   free_frame_gutters (f);
1556
1557   /* This must be done before the window and window_mirror structures
1558      are freed.  The scrollbar information is attached to them. */
1559   MAYBE_FRAMEMETH (f, delete_frame, (f));
1560
1561   /* Mark all the windows that used to be on FRAME as deleted, and then
1562      remove the reference to them.  */
1563   delete_all_subwindows (XWINDOW (f->root_window));
1564   f->root_window = Qnil;
1565
1566   /* clear out the cached glyph information */
1567   if (f->subwindow_cachels)
1568     {
1569       Dynarr_free (f->subwindow_cachels);
1570       f->subwindow_cachels = 0;
1571     }
1572
1573   /* Remove the frame now from the list.  This way, any events generated
1574      on this frame by the maneuvers below will disperse themselves. */
1575
1576   /* This used to be Fdelq(), but that will cause a seg fault if the
1577      QUIT checker happens to get invoked, because the frame list is in
1578      an inconsistent state. */
1579   d->frame_list = delq_no_quit (frame, d->frame_list);
1580   RESET_CHANGED_SET_FLAGS;
1581
1582   f->dead = 1;
1583   f->visible = 0;
1584
1585   free_window_mirror (f->root_mirror);
1586 /*  free_line_insertion_deletion_costs (f); */
1587
1588   /* If we've deleted the last non-minibuf frame, then try to find
1589      another one.  */
1590   if (EQ (frame, CONSOLE_LAST_NONMINIBUF_FRAME (con)))
1591     {
1592       Lisp_Object frmcons, devcons;
1593
1594       set_console_last_nonminibuf_frame (con, Qnil);
1595
1596       CONSOLE_FRAME_LOOP_NO_BREAK (frmcons, devcons, con)
1597         {
1598           Lisp_Object ecran = XCAR (frmcons);
1599           if (!FRAME_MINIBUF_ONLY_P (XFRAME (ecran)))
1600             {
1601               set_console_last_nonminibuf_frame (con, ecran);
1602               goto double_break_1;
1603             }
1604         }
1605     }
1606  double_break_1:
1607
1608 #if 0
1609   /* The following test is degenerate FALSE */
1610   if (called_from_delete_device < 0)
1611     /* then we're being called from delete-console, and we shouldn't
1612        try to find another default-minibuffer frame for the console.
1613        */
1614     con->default_minibuffer_frame = Qnil;
1615 #endif
1616
1617   /* If we've deleted this console's default_minibuffer_frame, try to
1618      find another one.  Prefer minibuffer-only frames, but also notice
1619      frames with other windows.  */
1620   if (EQ (frame, con->default_minibuffer_frame))
1621     {
1622       Lisp_Object frmcons, devcons;
1623       /* The last frame we saw with a minibuffer, minibuffer-only or not.  */
1624       Lisp_Object frame_with_minibuf;
1625       /* Some frame we found on the same console, or nil if there are none. */
1626       Lisp_Object frame_on_same_console;
1627
1628       frame_on_same_console = Qnil;
1629       frame_with_minibuf = Qnil;
1630
1631       set_console_last_nonminibuf_frame (con, Qnil);
1632
1633       CONSOLE_FRAME_LOOP_NO_BREAK (frmcons, devcons, con)
1634         {
1635           Lisp_Object this;
1636           struct frame *f1;
1637
1638           this = XCAR (frmcons);
1639           f1 = XFRAME (this);
1640
1641           /* Consider only frames on the same console
1642              and only those with minibuffers.  */
1643           if (FRAME_HAS_MINIBUF_P (f1))
1644             {
1645               frame_with_minibuf = this;
1646               if (FRAME_MINIBUF_ONLY_P (f1))
1647                 goto double_break_2;
1648             }
1649
1650           frame_on_same_console = this;
1651         }
1652     double_break_2:
1653
1654       if (!NILP (frame_on_same_console))
1655         {
1656           /* We know that there must be some frame with a minibuffer out
1657              there.  If this were not true, all of the frames present
1658              would have to be minibuffer-less, which implies that at some
1659              point their minibuffer frames must have been deleted, but
1660              that is prohibited at the top; you can't delete surrogate
1661              minibuffer frames.  */
1662           if (NILP (frame_with_minibuf))
1663             abort ();
1664
1665           con->default_minibuffer_frame = frame_with_minibuf;
1666         }
1667       else
1668         /* No frames left on this console--say no minibuffer either.  */
1669         con->default_minibuffer_frame = Qnil;
1670     }
1671
1672   nuke_all_frame_slots (f); /* nobody should be accessing the device
1673                                or anything else any more, and making
1674                                them Qnil allows for better GC'ing
1675                                in case a pointer to the dead frame
1676                                continues to hang around. */
1677   f->framemeths = dead_console_methods;
1678   UNGCPRO;
1679 }
1680
1681 void
1682 io_error_delete_frame (Lisp_Object frame)
1683 {
1684   delete_frame_internal (XFRAME (frame), 1, 0, 1);
1685 }
1686
1687 DEFUN ("delete-frame", Fdelete_frame, 0, 2, "", /*
1688 Delete FRAME, permanently eliminating it from use.
1689 If omitted, FRAME defaults to the selected frame.
1690 A frame may not be deleted if its minibuffer is used by other frames.
1691 Normally, you cannot delete the last non-minibuffer-only frame (you must
1692 use `save-buffers-kill-emacs' or `kill-emacs').  However, if optional
1693 second argument FORCE is non-nil, you can delete the last frame. (This
1694 will automatically call `save-buffers-kill-emacs'.)
1695 */
1696        (frame, force))
1697 {
1698   /* This function can GC */
1699   struct frame *f;
1700
1701   if (NILP (frame))
1702     {
1703       f = selected_frame ();
1704       XSETFRAME (frame, f);
1705     }
1706   else
1707     {
1708       CHECK_FRAME (frame);
1709       f = XFRAME (frame);
1710     }
1711
1712   delete_frame_internal (f, !NILP (force), 0, 0);
1713   return Qnil;
1714 }
1715
1716 \f
1717 /* Return mouse position in character cell units.  */
1718
1719 static int
1720 mouse_pixel_position_1 (struct device *d, Lisp_Object *frame,
1721                         int *x, int *y)
1722 {
1723   switch (DEVMETH_OR_GIVEN (d, get_mouse_position, (d, frame, x, y), -1))
1724     {
1725     case 1:
1726       return 1;
1727
1728     case 0:
1729       *frame = Qnil;
1730       break;
1731
1732     case -1:
1733       *frame = DEVICE_SELECTED_FRAME (d);
1734       break;
1735
1736     default:
1737       abort (); /* method is incorrectly written */
1738     }
1739
1740   return 0;
1741 }
1742
1743 DEFUN ("mouse-pixel-position", Fmouse_pixel_position, 0, 1, 0, /*
1744 Return a list (WINDOW X . Y) giving the current mouse window and position.
1745 The position is given in pixel units, where (0, 0) is the upper-left corner.
1746
1747 When the cursor is not over a window, the return value is a list (nil nil).
1748
1749 DEVICE specifies the device on which to read the mouse position, and
1750 defaults to the selected device.  If the device is a mouseless terminal
1751 or Emacs hasn't been programmed to read its mouse position, it returns
1752 the device's selected window for WINDOW and nil for X and Y.
1753 */
1754        (device))
1755 {
1756   struct device *d = decode_device (device);
1757   Lisp_Object frame;
1758   Lisp_Object window = Qnil;
1759   Lisp_Object x = Qnil;
1760   Lisp_Object y = Qnil;
1761   int intx, inty;
1762
1763   if (mouse_pixel_position_1 (d, &frame, &intx, &inty) > 0)
1764     {
1765       struct window *w =
1766         find_window_by_pixel_pos (intx, inty, XFRAME (frame)->root_window);
1767       if (w)
1768         {
1769           XSETWINDOW (window, w);
1770
1771           /* Adjust the position to be relative to the window. */
1772           intx -= w->pixel_left;
1773           inty -= w->pixel_top;
1774           XSETINT (x, intx);
1775           XSETINT (y, inty);
1776         }
1777     }
1778   else if (FRAMEP (frame))
1779     window = FRAME_SELECTED_WINDOW (XFRAME (frame));
1780
1781   return Fcons (window, Fcons (x, y));
1782 }
1783
1784 DEFUN ("mouse-position", Fmouse_position, 0, 1, 0, /*
1785 Return a list (WINDOW X . Y) giving the current mouse window and position.
1786 The position is of a character under cursor, where (0, 0) is the upper-left
1787 corner of the window.
1788
1789 When the cursor is not over a character, or not over a window, the return
1790 value is a list (nil nil).
1791
1792 DEVICE specifies the device on which to read the mouse position, and
1793 defaults to the selected device.  If the device is a mouseless terminal
1794 or Emacs hasn't been programmed to read its mouse position, it returns
1795 the device's selected window for WINDOW and nil for X and Y.
1796 */
1797        (device))
1798 {
1799   struct device *d = decode_device (device);
1800   struct window *w;
1801   Lisp_Object frame, window = Qnil, lisp_x = Qnil, lisp_y = Qnil;
1802   int x, y, obj_x, obj_y;
1803   Bufpos bufpos, closest;
1804   Charcount modeline_closest;
1805   Lisp_Object obj1, obj2;
1806
1807   if (mouse_pixel_position_1 (d, &frame, &x, &y) > 0)
1808     {
1809       int res = pixel_to_glyph_translation (XFRAME (frame), x, y, &x, &y,
1810                                             &obj_x, &obj_y, &w, &bufpos,
1811                                             &closest, &modeline_closest,
1812                                             &obj1, &obj2);
1813       if (res == OVER_TEXT)
1814         {
1815           lisp_x = make_int (x);
1816           lisp_y = make_int (y);
1817           XSETWINDOW (window, w);
1818         }
1819     }
1820   else if (FRAMEP (frame))
1821     window = FRAME_SELECTED_WINDOW (XFRAME (frame));
1822
1823   return Fcons (window, Fcons (lisp_x, lisp_y));
1824 }
1825
1826 DEFUN ("mouse-position-as-motion-event", Fmouse_position_as_motion_event, 0, 1, 0, /*
1827 Return the current mouse position as a motion event.
1828 This allows you to call the standard event functions such as
1829 `event-over-toolbar-p' to determine where the mouse is.
1830
1831 DEVICE specifies the device on which to read the mouse position, and
1832 defaults to the selected device.  If the mouse position can't be determined
1833 \(e.g. DEVICE is a TTY device), nil is returned instead of an event.
1834 */
1835        (device))
1836 {
1837   struct device *d = decode_device (device);
1838   Lisp_Object frame;
1839   int intx, inty;
1840
1841   if (mouse_pixel_position_1 (d, &frame, &intx, &inty))
1842     {
1843       Lisp_Object event = Fmake_event (Qnil, Qnil);
1844       XEVENT (event)->event_type = pointer_motion_event;
1845       XEVENT (event)->channel = frame;
1846       XEVENT (event)->event.motion.x = intx;
1847       XEVENT (event)->event.motion.y = inty;
1848       return event;
1849     }
1850   else
1851     return Qnil;
1852 }
1853
1854 DEFUN ("set-mouse-position", Fset_mouse_position, 3, 3, 0, /*
1855 Move the mouse pointer to the center of character cell (X,Y) in WINDOW.
1856 Note, this is a no-op for an X frame that is not visible.
1857 If you have just created a frame, you must wait for it to become visible
1858 before calling this function on it, like this.
1859   (while (not (frame-visible-p frame)) (sleep-for .5))
1860 Note also: Warping the mouse is contrary to the ICCCM, so be very sure
1861  that the behavior won't end up being obnoxious!
1862 */
1863        (window, x, y))
1864 {
1865   struct window *w;
1866   int pix_x, pix_y;
1867
1868   CHECK_LIVE_WINDOW (window);
1869   CHECK_INT (x);
1870   CHECK_INT (y);
1871
1872   /* Warping the mouse will cause EnterNotify and Focus events under X. */
1873   w = XWINDOW (window);
1874   glyph_to_pixel_translation (w, XINT (x), XINT (y), &pix_x, &pix_y);
1875
1876   MAYBE_FRAMEMETH (XFRAME (w->frame), set_mouse_position, (w, pix_x, pix_y));
1877
1878   return Qnil;
1879 }
1880
1881 DEFUN ("set-mouse-pixel-position", Fset_mouse_pixel_position, 3, 3, 0, /*
1882 Move the mouse pointer to pixel position (X,Y) in WINDOW.
1883 Note, this is a no-op for an X frame that is not visible.
1884 If you have just created a frame, you must wait for it to become visible
1885 before calling this function on it, like this.
1886   (while (not (frame-visible-p frame)) (sleep-for .5))
1887 */
1888        (window, x, y))
1889 {
1890   struct window *w;
1891
1892   CHECK_LIVE_WINDOW (window);
1893   CHECK_INT (x);
1894   CHECK_INT (y);
1895
1896   /* Warping the mouse will cause EnterNotify and Focus events under X. */
1897   w = XWINDOW (window);
1898   FRAMEMETH (XFRAME (w->frame), set_mouse_position, (w, XINT (x), XINT (y)));
1899
1900   return Qnil;
1901 }
1902 \f
1903 DEFUN ("make-frame-visible", Fmake_frame_visible, 0, 1, 0, /*
1904 Make the frame FRAME visible (assuming it is an X-window).
1905 If omitted, FRAME defaults to the currently selected frame.
1906 Also raises the frame so that nothing obscures it.
1907 */
1908        (frame))
1909 {
1910   struct frame *f = decode_frame (frame);
1911
1912   MAYBE_FRAMEMETH (f, make_frame_visible, (f));
1913   return frame;
1914 }
1915
1916 DEFUN ("make-frame-invisible", Fmake_frame_invisible, 0, 2, 0, /*
1917 Unconditionally removes frame from the display (assuming it is an X-window).
1918 If omitted, FRAME defaults to the currently selected frame.
1919 If what you want to do is iconify the frame (if the window manager uses
1920 icons) then you should call `iconify-frame' instead.
1921 Normally you may not make FRAME invisible if all other frames are invisible
1922 and uniconified, but if the second optional argument FORCE is non-nil,
1923 you may do so.
1924 */
1925        (frame, force))
1926 {
1927   struct frame *f, *sel_frame;
1928   struct device *d;
1929
1930   f = decode_frame (frame);
1931   d = XDEVICE (FRAME_DEVICE (f));
1932   sel_frame = XFRAME (DEVICE_SELECTED_FRAME (d));
1933
1934   if (NILP (force) && !other_visible_frames (f))
1935     error ("Attempt to make invisible the sole visible or iconified frame");
1936
1937   /* Don't allow minibuf_window to remain on a deleted frame.  */
1938   if (EQ (f->minibuffer_window, minibuf_window))
1939     {
1940       Fset_window_buffer (sel_frame->minibuffer_window,
1941                           XWINDOW (minibuf_window)->buffer, Qt);
1942       minibuf_window = sel_frame->minibuffer_window;
1943     }
1944
1945   MAYBE_FRAMEMETH (f, make_frame_invisible, (f));
1946
1947   return Qnil;
1948 }
1949
1950 DEFUN ("iconify-frame", Ficonify_frame, 0, 1, "", /*
1951 Make the frame FRAME into an icon, if the window manager supports icons.
1952 If omitted, FRAME defaults to the currently selected frame.
1953 */
1954        (frame))
1955 {
1956   struct frame *f, *sel_frame;
1957   struct device *d;
1958
1959   f = decode_frame (frame);
1960   d = XDEVICE (FRAME_DEVICE (f));
1961   sel_frame = XFRAME (DEVICE_SELECTED_FRAME (d));
1962
1963   /* Don't allow minibuf_window to remain on a deleted frame.  */
1964   if (EQ (f->minibuffer_window, minibuf_window))
1965     {
1966       Fset_window_buffer (sel_frame->minibuffer_window,
1967                           XWINDOW (minibuf_window)->buffer, Qt);
1968       minibuf_window = sel_frame->minibuffer_window;
1969     }
1970
1971   MAYBE_FRAMEMETH (f, iconify_frame, (f));
1972
1973   return Qnil;
1974 }
1975
1976 DEFUN ("deiconify-frame", Fdeiconify_frame, 0, 1, 0, /*
1977 Open (de-iconify) the iconified frame FRAME.
1978 Under X, this is currently the same as `make-frame-visible'.
1979 If omitted, FRAME defaults to the currently selected frame.
1980 Also raises the frame so that nothing obscures it.
1981 */
1982        (frame))
1983 {
1984   return Fmake_frame_visible (frame);
1985 }
1986
1987 /* FSF returns 'icon for iconized frames.  What a crock! */
1988
1989 DEFUN ("frame-visible-p", Fframe_visible_p, 0, 1, 0, /*
1990 Return non NIL if FRAME is now "visible" (actually in use for display).
1991 A frame that is not visible is not updated, and, if it works through a
1992 window system, may not show at all.
1993 N.B. Under X "visible" means Mapped. It the window is mapped but not
1994 actually visible on screen then frame_visible returns 'hidden.
1995 */
1996        (frame))
1997 {
1998   struct frame *f = decode_frame (frame);
1999   int visible = FRAMEMETH_OR_GIVEN (f, frame_visible_p, (f), f->visible);
2000   return visible ? ( visible > 0 ? Qt : Qhidden ) : Qnil;
2001 }
2002
2003 DEFUN ("frame-totally-visible-p", Fframe_totally_visible_p, 0, 1, 0, /*
2004 Return t if frame is not obscured by any other window system windows.
2005 Always returns t for tty frames.
2006 */
2007        (frame))
2008 {
2009   struct frame *f = decode_frame (frame);
2010   return (FRAMEMETH_OR_GIVEN (f, frame_totally_visible_p, (f), f->visible)
2011           ? Qt : Qnil);
2012 }
2013
2014 DEFUN ("frame-iconified-p", Fframe_iconified_p, 0, 1, 0, /*
2015 Return t if FRAME is iconified.
2016 Not all window managers use icons; some merely unmap the window, so this
2017 function is not the inverse of `frame-visible-p'.  It is possible for a
2018 frame to not be visible and not be iconified either.  However, if the
2019 frame is iconified, it will not be visible.
2020 */
2021        (frame))
2022 {
2023   struct frame *f = decode_frame (frame);
2024   if (f->visible)
2025     return Qnil;
2026   f->iconified = FRAMEMETH_OR_GIVEN (f, frame_iconified_p, (f), 0);
2027   return f->iconified ? Qt : Qnil;
2028 }
2029
2030 DEFUN ("visible-frame-list", Fvisible_frame_list, 0, 1, 0, /*
2031 Return a list of all frames now "visible" (being updated).
2032 If DEVICE is specified only frames on that device will be returned.
2033 Note that under virtual window managers not all these frame are necessarily
2034 really updated.
2035 */
2036        (device))
2037 {
2038   Lisp_Object devcons, concons;
2039   struct frame *f;
2040   Lisp_Object value;
2041
2042   value = Qnil;
2043
2044   DEVICE_LOOP_NO_BREAK (devcons, concons)
2045     {
2046       assert (DEVICEP (XCAR (devcons)));
2047
2048       if (NILP (device) || EQ (device, XCAR (devcons)))
2049         {
2050           Lisp_Object frmcons;
2051
2052           DEVICE_FRAME_LOOP (frmcons, XDEVICE (XCAR (devcons)))
2053             {
2054               Lisp_Object frame = XCAR (frmcons);
2055               f = XFRAME (frame);
2056               if (FRAME_VISIBLE_P(f))
2057                 value = Fcons (frame, value);
2058             }
2059         }
2060     }
2061
2062   return value;
2063 }
2064
2065 \f
2066 DEFUN ("raise-frame", Fraise_frame, 0, 1, "", /*
2067 Bring FRAME to the front, so it occludes any frames it overlaps.
2068 If omitted, FRAME defaults to the currently selected frame.
2069 If FRAME is invisible, make it visible.
2070 If Emacs is displaying on an ordinary terminal or some other device which
2071 doesn't support multiple overlapping frames, this function does nothing.
2072 */
2073        (frame))
2074 {
2075   struct frame *f = decode_frame (frame);
2076
2077   /* Do like the documentation says. */
2078   Fmake_frame_visible (frame);
2079   MAYBE_FRAMEMETH (f, raise_frame, (f));
2080   return Qnil;
2081 }
2082
2083 DEFUN ("lower-frame", Flower_frame, 0, 1, "", /*
2084 Send FRAME to the back, so it is occluded by any frames that overlap it.
2085 If omitted, FRAME defaults to the currently selected frame.
2086 If Emacs is displaying on an ordinary terminal or some other device which
2087 doesn't support multiple overlapping frames, this function does nothing.
2088 */
2089        (frame))
2090 {
2091   struct frame *f = decode_frame (frame);
2092
2093   MAYBE_FRAMEMETH (f, lower_frame, (f));
2094   return Qnil;
2095 }
2096
2097 /* Ben thinks there is no need for `redirect-frame-focus' or `frame-focus',
2098    crockish FSFmacs functions.  See summary on focus in event-stream.c. */
2099 \f
2100 DEFUN ("print-job-page-number", Fprint_job_page_number, 1, 1, 0, /*
2101 Return current page number for the print job FRAME.
2102 */
2103        (frame))
2104 {
2105   CHECK_PRINTER_FRAME (frame);
2106   return make_int (FRAME_PAGENUMBER (XFRAME (frame)));
2107 }
2108
2109 DEFUN ("print-job-eject-page", Fprint_job_eject_page, 1, 1, 0, /*
2110 Eject page in the print job FRAME.
2111 */
2112        (frame))
2113 {
2114   struct frame *f;
2115
2116   CHECK_PRINTER_FRAME (frame);
2117   f = XFRAME (frame);
2118   FRAMEMETH (f, eject_page, (f));
2119   FRAME_SET_PAGENUMBER (f, 1 + FRAME_PAGENUMBER (f));
2120   f->clear = 1;
2121
2122   return Qnil;
2123 }
2124
2125 \f
2126 /***************************************************************************/
2127 /*                           frame properties                              */
2128 /***************************************************************************/
2129
2130 static void internal_set_frame_size (struct frame *f, int cols, int rows,
2131                                      int pretend);
2132
2133 static void
2134 store_minibuf_frame_prop (struct frame *f, Lisp_Object val)
2135 {
2136   Lisp_Object frame;
2137   XSETFRAME (frame, f);
2138
2139   if (WINDOWP (val))
2140     {
2141       if (! MINI_WINDOW_P (XWINDOW (val)))
2142         signal_simple_error
2143           ("Surrogate minibuffer windows must be minibuffer windows",
2144            val);
2145
2146       if (FRAME_HAS_MINIBUF_P (f) || FRAME_MINIBUF_ONLY_P (f))
2147         signal_simple_error
2148           ("Can't change the surrogate minibuffer of a frame with its own minibuffer", frame);
2149
2150       /* Install the chosen minibuffer window, with proper buffer.  */
2151       f->minibuffer_window = val;
2152     }
2153   else if (EQ (val, Qt))
2154     {
2155       if (FRAME_HAS_MINIBUF_P (f) || FRAME_MINIBUF_ONLY_P (f))
2156         signal_simple_error
2157           ("Frame already has its own minibuffer", frame);
2158       else
2159         {
2160           setup_normal_frame (f);
2161           f->mirror_dirty = 1;
2162
2163           update_frame_window_mirror (f);
2164           internal_set_frame_size (f, f->width, f->height, 1);
2165         }
2166     }
2167 }
2168
2169 #if 0
2170
2171 /* possible code if you want to have symbols such as `default-background'
2172    map to setting the background of `default', etc. */
2173
2174 static int
2175 dissect_as_face_setting (Lisp_Object sym, Lisp_Object *face_out,
2176                          Lisp_Object *face_prop_out)
2177 {
2178   Lisp_Object list = Vbuilt_in_face_specifiers;
2179   Lisp_String *s;
2180
2181   if (!SYMBOLP (sym))
2182     return 0;
2183
2184   s = symbol_name (XSYMBOL (sym));
2185
2186   while (!NILP (list))
2187     {
2188       Lisp_Object prop = Fcar (list);
2189       Lisp_String *prop_name;
2190
2191       if (!SYMBOLP (prop))
2192         continue;
2193       prop_name = symbol_name (XSYMBOL (prop));
2194       if (string_length (s) > string_length (prop_name) + 1
2195           && !memcmp (string_data (prop_name),
2196                       string_data (s) + string_length (s)
2197                       - string_length (prop_name),
2198                       string_length (prop_name))
2199           && string_data (s)[string_length (s) - string_length (prop_name)
2200                              - 1] == '-')
2201         {
2202           Lisp_Object face =
2203             Ffind_face (make_string (string_data (s),
2204                                      string_length (s)
2205                                      - string_length (prop_name)
2206                                      - 1));
2207           if (!NILP (face))
2208             {
2209               *face_out = face;
2210               *face_prop_out = prop;
2211               return 1;
2212             }
2213         }
2214
2215       list = Fcdr (list);
2216     }
2217
2218   return 0;
2219 }
2220
2221 #endif /* 0 */
2222
2223 static Lisp_Object
2224 get_property_alias (Lisp_Object prop)
2225 {
2226   while (1)
2227     {
2228       Lisp_Object alias = Qnil;
2229
2230       if (SYMBOLP (prop))
2231         alias = Fget (prop, Qframe_property_alias, Qnil);
2232       if (NILP (alias))
2233         break;
2234       prop = alias;
2235       QUIT;
2236     }
2237
2238   return prop;
2239 }
2240
2241 /* #### Using this to modify the internal border width has no effect
2242    because the change isn't propagated to the windows.  Are there
2243    other properties which this claims to handle, but doesn't?
2244
2245    But of course.  This stuff needs more work, but it's a lot closer
2246    to sanity now than before with the horrible frame-params stuff. */
2247
2248 DEFUN ("set-frame-properties", Fset_frame_properties, 2, 2, 0, /*
2249 Change some properties of a frame.
2250 PLIST is a property list.
2251 You can also change frame properties individually using `set-frame-property',
2252 but it may be more efficient to change many properties at once.
2253
2254 Frame properties can be retrieved using `frame-property' or `frame-properties'.
2255
2256 The following symbols etc. have predefined meanings:
2257
2258  name           Name of the frame.  Used with X resources.
2259                 Unchangeable after creation.
2260
2261  height         Height of the frame, in lines.
2262
2263  width          Width of the frame, in characters.
2264
2265  minibuffer     Gives the minibuffer behavior for this frame.  Either
2266                 t (frame has its own minibuffer), `only' (frame is
2267                 a minibuffer-only frame), `none' (frame has no minibuffer)
2268                 or a window (frame uses that window, which is on another
2269                 frame, as the minibuffer).
2270
2271  unsplittable   If non-nil, frame cannot be split by `display-buffer'.
2272
2273  current-display-table, menubar-visible-p, left-margin-width,
2274  right-margin-width, minimum-line-ascent, minimum-line-descent,
2275  use-left-overflow, use-right-overflow, scrollbar-width, scrollbar-height,
2276  default-toolbar, top-toolbar, bottom-toolbar, left-toolbar, right-toolbar,
2277  default-toolbar-height, default-toolbar-width, top-toolbar-height,
2278  bottom-toolbar-height, left-toolbar-width, right-toolbar-width,
2279  default-toolbar-visible-p, top-toolbar-visible-p, bottom-toolbar-visible-p,
2280  left-toolbar-visible-p, right-toolbar-visible-p, toolbar-buttons-captioned-p,
2281  top-toolbar-border-width, bottom-toolbar-border-width,
2282  left-toolbar-border-width, right-toolbar-border-width,
2283  modeline-shadow-thickness, has-modeline-p,
2284  default-gutter, top-gutter, bottom-gutter, left-gutter, right-gutter,
2285  default-gutter-height, default-gutter-width, top-gutter-height,
2286  bottom-gutter-height, left-gutter-width, right-gutter-width,
2287  default-gutter-visible-p, top-gutter-visible-p, bottom-gutter-visible-p,
2288  left-gutter-visible-p, right-gutter-visible-p, top-gutter-border-width,
2289  bottom-gutter-border-width, left-gutter-border-width, right-gutter-border-width,
2290                 [Giving the name of any built-in specifier variable is
2291                 equivalent to calling `set-specifier' on the specifier,
2292                 with a locale of FRAME.  Giving the name to `frame-property'
2293                 calls `specifier-instance' on the specifier.]
2294
2295  text-pointer-glyph, nontext-pointer-glyph, modeline-pointer-glyph,
2296  selection-pointer-glyph, busy-pointer-glyph, toolbar-pointer-glyph,
2297  menubar-pointer-glyph, scrollbar-pointer-glyph, gc-pointer-glyph,
2298  octal-escape-glyph, control-arrow-glyph, invisible-text-glyph,
2299  hscroll-glyph, truncation-glyph, continuation-glyph
2300                 [Giving the name of any glyph variable is equivalent to
2301                 calling `set-glyph-image' on the glyph, with a locale
2302                 of FRAME.  Giving the name to `frame-property' calls
2303                 `glyph-image-instance' on the glyph.]
2304
2305  [default foreground], [default background], [default font],
2306  [modeline foreground], [modeline background], [modeline font],
2307  etc.
2308                 [Giving a vector of a face and a property is equivalent
2309                 to calling `set-face-property' on the face and property,
2310                 with a locale of FRAME.  Giving the vector to
2311                 `frame-property' calls `face-property-instance' on the
2312                 face and property.]
2313
2314 Finally, if a frame property symbol has the property `frame-property-alias'
2315 on it, then the value will be used in place of that symbol when looking
2316 up and setting frame property values.  This allows you to alias one
2317 frame property name to another.
2318
2319 See the variables `default-x-frame-plist', `default-tty-frame-plist'
2320 and `default-mswindows-frame-plist' for a description of the properties
2321 recognized for particular types of frames.
2322 */
2323        (frame, plist))
2324 {
2325   struct frame *f = decode_frame (frame);
2326   Lisp_Object tail;
2327   Lisp_Object *tailp;
2328   struct gcpro gcpro1, gcpro2;
2329
2330   XSETFRAME (frame, f);
2331   GCPRO2 (frame, plist);
2332   Fcheck_valid_plist (plist);
2333   plist = Fcopy_sequence (plist);
2334   Fcanonicalize_lax_plist (plist, Qnil);
2335   for (tail = plist; !NILP (tail); tail = Fcdr (Fcdr (tail)))
2336     {
2337       Lisp_Object prop = Fcar (tail);
2338       Lisp_Object val = Fcar (Fcdr (tail));
2339
2340       prop = get_property_alias (prop);
2341
2342 #if 0
2343       /* mly wants this, but it's not reasonable to change the name of a
2344          frame after it has been created, because the old name was used
2345          for resource lookup. */
2346       if (EQ (prop, Qname))
2347         {
2348           CHECK_STRING (val);
2349           f->name = val;
2350         }
2351 #endif /* 0 */
2352       if (EQ (prop, Qminibuffer))
2353         store_minibuf_frame_prop (f, val);
2354       if (EQ (prop, Qunsplittable))
2355         f->no_split = !NILP (val);
2356       if (EQ (prop, Qbuffer_predicate))
2357         f->buffer_predicate = val;
2358       if (SYMBOLP (prop) && EQ (Fbuilt_in_variable_type (prop),
2359                                 Qconst_specifier))
2360         call3 (Qset_specifier, Fsymbol_value (prop), val, frame);
2361       if (SYMBOLP (prop) && !NILP (Fget (prop, Qconst_glyph_variable, Qnil)))
2362         call3 (Qset_glyph_image, Fsymbol_value (prop), val, frame);
2363       if (VECTORP (prop) && XVECTOR_LENGTH (prop) == 2)
2364         {
2365           Lisp_Object face_prop = XVECTOR_DATA (prop)[1];
2366           CHECK_SYMBOL (face_prop);
2367           call4 (Qset_face_property,
2368                  Fget_face (XVECTOR_DATA (prop)[0]),
2369                  face_prop, val, frame);
2370         }
2371     }
2372
2373   MAYBE_FRAMEMETH (f, set_frame_properties, (f, plist));
2374   for (tailp = &plist; !NILP (*tailp);)
2375     {
2376       Lisp_Object *next_tailp;
2377       Lisp_Object next;
2378       Lisp_Object prop;
2379
2380       next = Fcdr (*tailp);
2381       CHECK_CONS (next);
2382       next_tailp = &XCDR (next);
2383       prop = Fcar (*tailp);
2384
2385       prop = get_property_alias (prop);
2386
2387       if (EQ (prop, Qminibuffer)
2388           || EQ (prop, Qunsplittable)
2389           || EQ (prop, Qbuffer_predicate)
2390           || EQ (prop, Qheight)
2391           || EQ (prop, Qwidth)
2392           || (SYMBOLP (prop) && EQ (Fbuilt_in_variable_type (prop),
2393                                     Qconst_specifier))
2394           || (SYMBOLP (prop) && !NILP (Fget (prop, Qconst_glyph_variable,
2395                                              Qnil)))
2396           || (VECTORP (prop) && XVECTOR_LENGTH (prop) == 2)
2397           || FRAMEMETH_OR_GIVEN (f, internal_frame_property_p, (f, prop), 0))
2398         *tailp = *next_tailp;
2399       tailp = next_tailp;
2400     }
2401
2402   f->plist = nconc2 (plist, f->plist);
2403   Fcanonicalize_lax_plist (f->plist, Qnil);
2404   UNGCPRO;
2405   return Qnil;
2406 }
2407
2408 DEFUN ("frame-property", Fframe_property, 2, 3, 0, /*
2409 Return FRAME's value for property PROPERTY.
2410 See `set-frame-properties' for the built-in property names.
2411 */
2412        (frame, property, default_))
2413 {
2414   struct frame *f = decode_frame (frame);
2415   Lisp_Object value;
2416
2417   XSETFRAME (frame, f);
2418
2419   property = get_property_alias (property);
2420
2421   if (EQ (Qname, property)) return f->name;
2422
2423   if (EQ (Qheight, property) || EQ (Qwidth, property))
2424     {
2425       if (window_system_pixelated_geometry (frame))
2426         {
2427           int width, height;
2428           pixel_to_real_char_size (f, FRAME_PIXWIDTH (f), FRAME_PIXHEIGHT (f),
2429                                    &width, &height);
2430           return make_int (EQ (Qheight, property) ? height: width);
2431         }
2432       else
2433         return make_int (EQ (Qheight, property) ?
2434                          FRAME_HEIGHT (f) :
2435                          FRAME_WIDTH  (f));
2436     }
2437
2438   /* NOTE: FSF returns Qnil instead of Qt for FRAME_HAS_MINIBUF_P.
2439      This is over-the-top bogosity, because it's inconsistent with
2440      the semantics of `minibuffer' when passed to `make-frame'.
2441      Returning Qt makes things consistent. */
2442   if (EQ (Qminibuffer, property))
2443     return (FRAME_MINIBUF_ONLY_P (f) ? Qonly :
2444             FRAME_HAS_MINIBUF_P  (f) ? Qt    :
2445             FRAME_MINIBUF_WINDOW (f));
2446   if (EQ (Qunsplittable, property))
2447     return FRAME_NO_SPLIT_P (f) ? Qt : Qnil;
2448   if (EQ (Qbuffer_predicate, property))
2449     return f->buffer_predicate;
2450
2451   if (SYMBOLP (property))
2452     {
2453       if (EQ (Fbuilt_in_variable_type (property), Qconst_specifier))
2454         return Fspecifier_instance (Fsymbol_value (property),
2455                                     frame, default_, Qnil);
2456       if (!NILP (Fget (property, Qconst_glyph_variable, Qnil)))
2457         {
2458           Lisp_Object glyph = Fsymbol_value (property);
2459           CHECK_GLYPH (glyph);
2460           return Fspecifier_instance (XGLYPH_IMAGE (glyph),
2461                                       frame, default_, Qnil);
2462         }
2463     }
2464
2465   if (VECTORP (property) && XVECTOR_LENGTH (property) == 2)
2466     {
2467       Lisp_Object face_prop = XVECTOR_DATA (property)[1];
2468       CHECK_SYMBOL (face_prop);
2469       return call3 (Qface_property_instance,
2470                     Fget_face (XVECTOR_DATA (property)[0]),
2471                     face_prop, frame);
2472     }
2473
2474   if (HAS_FRAMEMETH_P (f, frame_property))
2475     if (!UNBOUNDP (value = FRAMEMETH (f, frame_property, (f, property))))
2476       return value;
2477
2478   if (!UNBOUNDP (value = external_plist_get (&f->plist, property, 1, ERROR_ME)))
2479     return value;
2480
2481   return default_;
2482 }
2483
2484 DEFUN ("frame-properties", Fframe_properties, 0, 1, 0, /*
2485 Return a property list of the properties of FRAME.
2486 Do not modify this list; use `set-frame-property' instead.
2487 */
2488        (frame))
2489 {
2490   struct frame *f = decode_frame (frame);
2491   Lisp_Object result = Qnil;
2492   struct gcpro gcpro1;
2493
2494   GCPRO1 (result);
2495
2496   XSETFRAME (frame, f);
2497
2498   /* #### for the moment (since old code uses `frame-parameters'),
2499      we call `copy-sequence' on f->plist.  That allows frame-parameters
2500      to destructively convert the plist into an alist, which is more
2501      efficient than doing it non-destructively.  At some point we
2502      should remove the call to copy-sequence. */
2503   result = Fcopy_sequence (f->plist);
2504
2505   /* #### should we be adding all the specifiers and glyphs?
2506      That would entail having a list of them all. */
2507   if (HAS_FRAMEMETH_P (f, frame_properties))
2508     result = nconc2 (FRAMEMETH (f, frame_properties, (f)), result);
2509
2510   if (!NILP (f->buffer_predicate))
2511     result = cons3 (Qbuffer_predicate, f->buffer_predicate, result);
2512
2513   if (FRAME_NO_SPLIT_P (f))
2514     result = cons3 (Qunsplittable, Qt, result);
2515
2516   /* NOTE: FSF returns Qnil instead of Qt for FRAME_HAS_MINIBUF_P.
2517      This is over-the-top bogosity, because it's inconsistent with
2518      the semantics of `minibuffer' when passed to `make-frame'.
2519      Returning Qt makes things consistent. */
2520   result = cons3 (Qminibuffer,
2521                   (FRAME_MINIBUF_ONLY_P (f) ? Qonly :
2522                    FRAME_HAS_MINIBUF_P  (f) ? Qt    :
2523                    FRAME_MINIBUF_WINDOW (f)),
2524                   result);
2525   {
2526     int width, height;
2527
2528     if (window_system_pixelated_geometry (frame))
2529       {
2530         pixel_to_real_char_size (f, FRAME_PIXWIDTH (f), FRAME_PIXHEIGHT (f),
2531                                  &width, &height);
2532       }
2533     else
2534       {
2535         height = FRAME_HEIGHT (f);
2536         width = FRAME_WIDTH (f);
2537       }
2538     result = cons3 (Qwidth , make_int (width),  result);
2539     result = cons3 (Qheight, make_int (height), result);
2540   }
2541
2542   result = cons3 (Qname, f->name, result);
2543
2544   UNGCPRO;
2545   return result;
2546 }
2547
2548 \f
2549 DEFUN ("frame-pixel-height", Fframe_pixel_height, 0, 1, 0, /*
2550 Return the height in pixels of FRAME.
2551 */
2552        (frame))
2553 {
2554   return make_int (decode_frame (frame)->pixheight);
2555 }
2556
2557 DEFUN ("frame-pixel-width", Fframe_pixel_width, 0, 1, 0, /*
2558 Return the width in pixels of FRAME.
2559 */
2560        (frame))
2561 {
2562   return make_int (decode_frame (frame)->pixwidth);
2563 }
2564
2565 DEFUN ("frame-name", Fframe_name, 0, 1, 0, /*
2566 Return the name of FRAME (defaulting to the selected frame).
2567 This is not the same as the `title' of the frame.
2568 */
2569        (frame))
2570 {
2571   return decode_frame (frame)->name;
2572 }
2573
2574 DEFUN ("frame-modified-tick", Fframe_modified_tick, 0, 1, 0, /*
2575 Return FRAME's tick counter, incremented for each change to the frame.
2576 Each frame has a tick counter which is incremented each time the frame
2577 is resized, a window is resized, added, or deleted, a face is changed,
2578 `set-window-buffer' or `select-window' is called on a window in the
2579 frame, the window-start of a window in the frame has changed, or
2580 anything else interesting has happened.  It wraps around occasionally.
2581 No argument or nil as argument means use selected frame as FRAME.
2582 */
2583        (frame))
2584 {
2585   return make_int (decode_frame (frame)->modiff);
2586 }
2587
2588 static void
2589 internal_set_frame_size (struct frame *f, int cols, int rows, int pretend)
2590 {
2591   /* An explicit size change cancels any pending frame size adjustment */
2592   CLEAR_FRAME_SIZE_SLIPPED(f);
2593
2594   if (pretend || !HAS_FRAMEMETH_P (f, set_frame_size))
2595     change_frame_size (f, rows, cols, 0);
2596   else
2597     FRAMEMETH (f, set_frame_size, (f, cols, rows));
2598 }
2599
2600 DEFUN ("set-frame-height", Fset_frame_height, 2, 3, 0, /*
2601 Specify that the frame FRAME has LINES lines.
2602 Optional third arg non-nil means that redisplay should use LINES lines
2603 but that the idea of the actual height of the frame should not be changed.
2604 */
2605        (frame, rows, pretend))
2606 {
2607   struct frame *f = decode_frame (frame);
2608   int height, width;
2609   XSETFRAME (frame, f);
2610   CHECK_INT (rows);
2611
2612   if (window_system_pixelated_geometry (frame))
2613     {
2614       char_to_real_pixel_size (f, 0, XINT (rows), 0, &height);
2615       width = FRAME_PIXWIDTH (f);
2616     }
2617   else
2618     {
2619       height = XINT (rows);
2620       width = FRAME_WIDTH (f);
2621     }
2622
2623   internal_set_frame_size (f, width, height, !NILP (pretend));
2624   return frame;
2625 }
2626
2627 DEFUN ("set-frame-width", Fset_frame_width, 2, 3, 0, /*
2628 Specify that the frame FRAME has COLS columns.
2629 Optional third arg non-nil means that redisplay should use COLS columns
2630 but that the idea of the actual width of the frame should not be changed.
2631 */
2632        (frame, cols, pretend))
2633 {
2634   struct frame *f = decode_frame (frame);
2635   int width, height;
2636   XSETFRAME (frame, f);
2637   CHECK_INT (cols);
2638
2639   if (window_system_pixelated_geometry (frame))
2640     {
2641       char_to_real_pixel_size (f, XINT (cols), 0, &width, 0);
2642       height = FRAME_PIXHEIGHT (f);
2643     }
2644   else
2645     {
2646       width = XINT (cols);
2647       height = FRAME_HEIGHT (f);
2648     }
2649
2650   internal_set_frame_size (f, width, height, !NILP (pretend));
2651   return frame;
2652 }
2653
2654 DEFUN ("set-frame-size", Fset_frame_size, 3, 4, 0, /*
2655 Set the size of FRAME to COLS by ROWS.
2656 Optional fourth arg non-nil means that redisplay should use COLS by ROWS
2657 but that the idea of the actual size of the frame should not be changed.
2658 */
2659        (frame, cols, rows, pretend))
2660 {
2661   struct frame *f = decode_frame (frame);
2662   int height, width;
2663   XSETFRAME (frame, f);
2664   CHECK_INT (cols);
2665   CHECK_INT (rows);
2666
2667   if (window_system_pixelated_geometry (frame))
2668     char_to_real_pixel_size (f, XINT (cols), XINT (rows), &width, &height);
2669   else
2670     {
2671       height = XINT (rows);
2672       width = XINT (cols);
2673     }
2674
2675   internal_set_frame_size (f, width, height, !NILP (pretend));
2676   return frame;
2677 }
2678
2679 DEFUN ("set-frame-position", Fset_frame_position, 3, 3, 0, /*
2680 Set position of FRAME in pixels to XOFFSET by YOFFSET.
2681 This is actually the position of the upper left corner of the frame.
2682 Negative values for XOFFSET or YOFFSET are interpreted relative to
2683 the rightmost or bottommost possible position (that stays within the screen).
2684 */
2685        (frame, xoffset, yoffset))
2686 {
2687   struct frame *f = decode_frame (frame);
2688   CHECK_INT (xoffset);
2689   CHECK_INT (yoffset);
2690
2691   MAYBE_FRAMEMETH (f, set_frame_position, (f, XINT (xoffset), XINT (yoffset)));
2692
2693   return Qt;
2694 }
2695
2696 \f
2697
2698 /* Frame size conversion functions moved here from EmacsFrame.c
2699    because they're generic and really don't belong in that file.
2700    Function get_default_char_pixel_size() removed because it's
2701    exactly the same as default_face_height_and_width(). */
2702 static void
2703 frame_conversion_internal (struct frame *f, int pixel_to_char,
2704                            int *pixel_width, int *pixel_height,
2705                            int *char_width, int *char_height,
2706                            int real_face)
2707 {
2708   int cpw;
2709   int cph;
2710   int egw;
2711   int obw, obh, bdr;
2712   Lisp_Object frame, window;
2713
2714   XSETFRAME (frame, f);
2715   if (real_face)
2716     default_face_height_and_width (frame, &cph, &cpw);
2717   else
2718     default_face_height_and_width_1 (frame, &cph, &cpw);
2719
2720   window = FRAME_SELECTED_WINDOW (f);
2721
2722   egw = max (glyph_width (Vcontinuation_glyph, window),
2723              glyph_width (Vtruncation_glyph, window));
2724   egw = max (egw, cpw);
2725   bdr = 2 * f->internal_border_width;
2726   obw = FRAME_SCROLLBAR_WIDTH (f) + FRAME_THEORETICAL_LEFT_TOOLBAR_WIDTH (f) +
2727     FRAME_THEORETICAL_RIGHT_TOOLBAR_WIDTH (f) +
2728     2 * FRAME_THEORETICAL_LEFT_TOOLBAR_BORDER_WIDTH (f) +
2729     2 * FRAME_THEORETICAL_RIGHT_TOOLBAR_BORDER_WIDTH (f);
2730   obh = FRAME_SCROLLBAR_HEIGHT (f) + FRAME_THEORETICAL_TOP_TOOLBAR_HEIGHT (f) +
2731     FRAME_THEORETICAL_BOTTOM_TOOLBAR_HEIGHT (f) +
2732     2 * FRAME_THEORETICAL_TOP_TOOLBAR_BORDER_WIDTH (f) +
2733     2 * FRAME_THEORETICAL_BOTTOM_TOOLBAR_BORDER_WIDTH (f);
2734
2735   if (pixel_to_char)
2736     {
2737       if (char_width)
2738         *char_width = 1 + ((*pixel_width - egw) - bdr - obw) / cpw;
2739       if (char_height)
2740         *char_height = (*pixel_height - bdr - obh) / cph;
2741     }
2742   else
2743     {
2744       if (pixel_width)
2745         *pixel_width = (*char_width - 1) * cpw + egw + bdr + obw;
2746       if (pixel_height)
2747         *pixel_height = *char_height * cph + bdr + obh;
2748     }
2749 }
2750
2751 /* This takes the size in pixels of the text area, and returns the number
2752    of characters that will fit there, taking into account the internal
2753    border width, and the pixel width of the line terminator glyphs (which
2754    always count as one "character" wide, even if they are not the same size
2755    as the default character size of the default font).  The frame scrollbar
2756    width and left and right toolbar widths are also subtracted out of the
2757    available width.  The frame scrollbar height and top and bottom toolbar
2758    heights are subtracted out of the available height.
2759
2760    Therefore the result is not necessarily a multiple of anything in
2761    particular.  */
2762 void
2763 pixel_to_char_size (struct frame *f, int pixel_width, int pixel_height,
2764                     int *char_width, int *char_height)
2765 {
2766   frame_conversion_internal (f, 1, &pixel_width, &pixel_height, char_width,
2767                              char_height, 0);
2768 }
2769
2770 /* Given a character size, this returns the minimum number of pixels
2771    necessary to display that many characters, taking into account the
2772    internal border width, scrollbar height and width, toolbar heights and
2773    widths and the size of the line terminator glyphs (assuming the line
2774    terminators take up exactly one character position).
2775
2776    Therefore the result is not necessarily a multiple of anything in
2777    particular.  */
2778 void
2779 char_to_pixel_size (struct frame *f, int char_width, int char_height,
2780                     int *pixel_width, int *pixel_height)
2781 {
2782   frame_conversion_internal (f, 0, pixel_width, pixel_height, &char_width,
2783                              &char_height, 0);
2784 }
2785
2786 /* Given a pixel size, rounds DOWN to the smallest size in pixels necessary
2787    to display the same number of characters as are displayable now.
2788  */
2789 void
2790 round_size_to_char (struct frame *f, int in_width, int in_height,
2791                     int *out_width, int *out_height)
2792 {
2793   int char_width;
2794   int char_height;
2795   pixel_to_char_size (f, in_width, in_height, &char_width, &char_height);
2796   char_to_pixel_size (f, char_width, char_height, out_width, out_height);
2797 }
2798
2799 /* Versions of the above which always account for real font metrics.
2800  */
2801 void
2802 pixel_to_real_char_size (struct frame *f, int pixel_width, int pixel_height,
2803                          int *char_width, int *char_height)
2804 {
2805   frame_conversion_internal (f, 1, &pixel_width, &pixel_height, char_width,
2806                              char_height, 1);
2807 }
2808
2809 void
2810 char_to_real_pixel_size (struct frame *f, int char_width, int char_height,
2811                          int *pixel_width, int *pixel_height)
2812 {
2813   frame_conversion_internal (f, 0, pixel_width, pixel_height, &char_width,
2814                              &char_height, 1);
2815 }
2816
2817 void
2818 round_size_to_real_char (struct frame *f, int in_width, int in_height,
2819                          int *out_width, int *out_height)
2820 {
2821   int char_width;
2822   int char_height;
2823   pixel_to_real_char_size (f, in_width, in_height, &char_width, &char_height);
2824   char_to_real_pixel_size (f, char_width, char_height, out_width, out_height);
2825 }
2826
2827 /* Change the frame height and/or width.  Values may be given as zero to
2828    indicate no change is to take place. */
2829 static void
2830 change_frame_size_1 (struct frame *f, int newheight, int newwidth)
2831 {
2832   Lisp_Object frame;
2833   int new_pixheight, new_pixwidth;
2834   int font_height, real_font_height, font_width;
2835
2836   /* #### Chuck -- shouldn't we be checking to see if the frame
2837      is being "changed" to its existing size, and do nothing if so? */
2838   /* No, because it would hose toolbar updates.  The toolbar
2839      update code relies on this function to cause window `top' and
2840      `left' coordinates to be recomputed even though no frame size
2841      change occurs. --kyle */
2842   if (in_display)
2843     abort ();
2844
2845   XSETFRAME (frame, f);
2846
2847   default_face_height_and_width (frame, &real_font_height, 0);
2848   default_face_height_and_width_1 (frame, &font_height, &font_width);
2849
2850   /* This size-change overrides any pending one for this frame.  */
2851   FRAME_NEW_HEIGHT (f) = 0;
2852   FRAME_NEW_WIDTH (f) = 0;
2853
2854   new_pixheight = newheight * font_height;
2855   new_pixwidth = (newwidth - 1) * font_width;
2856
2857   /* #### dependency on FRAME_WIN_P should be removed. */
2858   if (FRAME_WIN_P (f))
2859     {
2860       new_pixheight += FRAME_SCROLLBAR_HEIGHT (f);
2861       new_pixwidth += FRAME_SCROLLBAR_WIDTH (f);
2862     }
2863
2864   /* when frame_conversion_internal() calculated the number of rows/cols
2865      in the frame, the theoretical toolbar sizes were subtracted out.
2866      The calculations below adjust for real toolbar height/width in
2867      frame, which may be different from frame spec, taking the above
2868      fact into account */
2869   new_pixheight +=
2870     + FRAME_THEORETICAL_TOP_TOOLBAR_HEIGHT (f)
2871     + 2 * FRAME_THEORETICAL_TOP_TOOLBAR_BORDER_WIDTH (f)
2872     - FRAME_REAL_TOP_TOOLBAR_HEIGHT (f)
2873     - 2 * FRAME_REAL_TOP_TOOLBAR_BORDER_WIDTH (f);
2874
2875   new_pixheight +=
2876     + FRAME_THEORETICAL_BOTTOM_TOOLBAR_HEIGHT (f)
2877     + 2 * FRAME_THEORETICAL_BOTTOM_TOOLBAR_BORDER_WIDTH (f)
2878     - FRAME_REAL_BOTTOM_TOOLBAR_HEIGHT (f)
2879     - 2 * FRAME_REAL_BOTTOM_TOOLBAR_BORDER_WIDTH (f);
2880
2881   new_pixwidth +=
2882     + FRAME_THEORETICAL_LEFT_TOOLBAR_WIDTH (f)
2883     + 2 * FRAME_THEORETICAL_LEFT_TOOLBAR_BORDER_WIDTH (f)
2884     - FRAME_REAL_LEFT_TOOLBAR_WIDTH (f)
2885     - 2 * FRAME_REAL_LEFT_TOOLBAR_BORDER_WIDTH (f);
2886
2887   new_pixwidth +=
2888     + FRAME_THEORETICAL_RIGHT_TOOLBAR_WIDTH (f)
2889     + 2 * FRAME_THEORETICAL_RIGHT_TOOLBAR_BORDER_WIDTH (f)
2890     - FRAME_REAL_RIGHT_TOOLBAR_WIDTH (f)
2891     - 2 * FRAME_REAL_RIGHT_TOOLBAR_BORDER_WIDTH (f);
2892
2893   /* Adjust the width for the end glyph which may be a different width
2894      than the default character width. */
2895   {
2896     int adjustment, trunc_width, cont_width;
2897
2898     trunc_width = glyph_width (Vtruncation_glyph,
2899                                FRAME_SELECTED_WINDOW (f));
2900     cont_width = glyph_width (Vcontinuation_glyph,
2901                               FRAME_SELECTED_WINDOW (f));
2902     adjustment = max (trunc_width, cont_width);
2903     adjustment = max (adjustment, font_width);
2904
2905     new_pixwidth += adjustment;
2906   }
2907
2908   /* If we don't have valid values, exit. */
2909   if (!new_pixheight && !new_pixwidth)
2910     return;
2911
2912   if (new_pixheight)
2913     {
2914       /* Adjust for gutters here so that we always get set
2915          properly. */
2916       new_pixheight -= 
2917         (FRAME_TOP_GUTTER_BOUNDS (f)
2918          + FRAME_BOTTOM_GUTTER_BOUNDS (f));
2919
2920       XWINDOW (FRAME_ROOT_WINDOW (f))->pixel_top 
2921         = FRAME_TOP_BORDER_END (f) + FRAME_TOP_GUTTER_BOUNDS (f);
2922
2923       if (FRAME_HAS_MINIBUF_P (f)
2924           && ! FRAME_MINIBUF_ONLY_P (f))
2925         /* Frame has both root and minibuffer.  */
2926         {
2927           /*
2928            * Leave the minibuffer height the same if the frame has
2929            * been initialized, and the minibuffer height is tall
2930            * enough to display at least one line of text in the default
2931            * font, and the old minibuffer height is a multiple of the
2932            * default font height.  This should cause the minibuffer
2933            * height to be recomputed on font changes but not for
2934            * other frame size changes, which seems reasonable.
2935            */
2936           int old_minibuf_height =
2937             XWINDOW(FRAME_MINIBUF_WINDOW(f))->pixel_height;
2938           int minibuf_height =
2939             f->init_finished && (old_minibuf_height % real_font_height) == 0 ?
2940             max(old_minibuf_height, real_font_height) :
2941             real_font_height;
2942           set_window_pixheight (FRAME_ROOT_WINDOW (f),
2943                                 /* - font_height for minibuffer */
2944                                 new_pixheight - minibuf_height, 0);
2945
2946           XWINDOW (FRAME_MINIBUF_WINDOW (f))->pixel_top =
2947             FRAME_TOP_BORDER_END (f) +
2948             FRAME_TOP_GUTTER_BOUNDS (f) + 
2949             FRAME_BOTTOM_GUTTER_BOUNDS (f) +
2950             new_pixheight - minibuf_height;
2951
2952           set_window_pixheight (FRAME_MINIBUF_WINDOW (f), minibuf_height, 0);
2953         }
2954       else
2955         /* Frame has just one top-level window.  */
2956         set_window_pixheight (FRAME_ROOT_WINDOW (f), new_pixheight, 0);
2957
2958       FRAME_HEIGHT (f) = newheight;
2959       if (FRAME_TTY_P (f))
2960         f->pixheight = newheight;
2961     }
2962
2963   if (new_pixwidth)
2964     {
2965       /* Adjust for gutters here so that we always get set
2966          properly. */
2967       new_pixwidth -= 
2968         (FRAME_LEFT_GUTTER_BOUNDS (f)
2969          + FRAME_RIGHT_GUTTER_BOUNDS (f));
2970       
2971       XWINDOW (FRAME_ROOT_WINDOW (f))->pixel_left = 
2972         FRAME_LEFT_BORDER_END (f) + FRAME_LEFT_GUTTER_BOUNDS (f);
2973       set_window_pixwidth (FRAME_ROOT_WINDOW (f), new_pixwidth, 0);
2974
2975       if (FRAME_HAS_MINIBUF_P (f))
2976         {
2977           XWINDOW (FRAME_MINIBUF_WINDOW (f))->pixel_left =
2978             FRAME_LEFT_BORDER_END (f) + FRAME_LEFT_GUTTER_BOUNDS (f);
2979           set_window_pixwidth (FRAME_MINIBUF_WINDOW (f), new_pixwidth, 0);
2980         }
2981
2982       FRAME_WIDTH (f) = newwidth;
2983       if (FRAME_TTY_P (f))
2984         f->pixwidth = newwidth;
2985     }
2986
2987   if (window_system_pixelated_geometry (frame))
2988     pixel_to_real_char_size (f, FRAME_PIXWIDTH (f), FRAME_PIXHEIGHT (f),
2989                              &FRAME_CHARWIDTH (f), &FRAME_CHARHEIGHT (f));
2990   else
2991     {
2992       FRAME_CHARWIDTH (f) = FRAME_WIDTH (f);
2993       FRAME_CHARHEIGHT (f) = FRAME_HEIGHT (f);
2994     }
2995
2996   MARK_FRAME_TOOLBARS_CHANGED (f);
2997   MARK_FRAME_GUTTERS_CHANGED (f);
2998   MARK_FRAME_CHANGED (f);
2999   f->echo_area_garbaged = 1;
3000 }
3001
3002 void
3003 change_frame_size (struct frame *f, int newheight, int newwidth, int delay)
3004 {
3005   /* sometimes we get passed a size that's too small (esp. when a
3006      client widget gets resized, since we have no control over this).
3007      So deal. */
3008   check_frame_size (f, &newheight, &newwidth);
3009
3010   if (delay || in_display || gc_in_progress)
3011     {
3012       MARK_FRAME_SIZE_CHANGED (f);
3013       f->new_width = newwidth;
3014       f->new_height = newheight;
3015       return;
3016     }
3017
3018   f->size_change_pending = 0;
3019   /* For TTY frames, it's like one, like all ...
3020      Can't have two TTY frames of different sizes on the same device. */
3021   if (FRAME_TTY_P (f))
3022     {
3023       Lisp_Object frmcons;
3024
3025       DEVICE_FRAME_LOOP (frmcons, XDEVICE (FRAME_DEVICE (f)))
3026         change_frame_size_1 (XFRAME (XCAR (frmcons)), newheight, newwidth);
3027     }
3028   else
3029     change_frame_size_1 (f, newheight, newwidth);
3030 }
3031
3032 \f
3033 /* The caller is responsible for freeing the returned string. */
3034 static Bufbyte *
3035 generate_title_string (struct window *w, Lisp_Object format_str,
3036                        face_index findex, int type)
3037 {
3038   struct display_line *dl;
3039   struct display_block *db;
3040   int elt = 0;
3041
3042   dl = &title_string_display_line;
3043   db = get_display_block_from_line (dl, TEXT);
3044   Dynarr_reset (db->runes);
3045
3046   generate_formatted_string_db (format_str, Qnil, w, dl, db, findex, 0,
3047                                 -1, type);
3048
3049   Dynarr_reset (title_string_emchar_dynarr);
3050   while (elt < Dynarr_length (db->runes))
3051     {
3052       if (Dynarr_atp (db->runes, elt)->type == RUNE_CHAR)
3053         Dynarr_add (title_string_emchar_dynarr,
3054                     Dynarr_atp (db->runes, elt)->object.chr.ch);
3055       elt++;
3056     }
3057
3058   return
3059     convert_emchar_string_into_malloced_string
3060     (Dynarr_atp (title_string_emchar_dynarr, 0),
3061      Dynarr_length (title_string_emchar_dynarr), 0);
3062 }
3063
3064 void
3065 update_frame_title (struct frame *f)
3066 {
3067   struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
3068   Lisp_Object title_format;
3069   Lisp_Object icon_format;
3070   Bufbyte *title;
3071
3072   /* We don't change the title for the minibuffer unless the frame
3073      only has a minibuffer. */
3074   if (MINI_WINDOW_P (w) && !FRAME_MINIBUF_ONLY_P (f))
3075     return;
3076
3077   /* And we don't want dead buffers to blow up on us. */
3078   if (!BUFFER_LIVE_P (XBUFFER (w->buffer)))
3079     return;
3080
3081   title = NULL;
3082   title_format = symbol_value_in_buffer (Qframe_title_format,      w->buffer);
3083   icon_format  = symbol_value_in_buffer (Qframe_icon_title_format, w->buffer);
3084
3085   if (HAS_FRAMEMETH_P (f, set_title_from_bufbyte))
3086     {
3087       title = generate_title_string (w, title_format,
3088                                      DEFAULT_INDEX, CURRENT_DISP);
3089       FRAMEMETH (f, set_title_from_bufbyte, (f, title));
3090     }
3091
3092   if (HAS_FRAMEMETH_P (f, set_icon_name_from_bufbyte))
3093     {
3094       if (!EQ (icon_format, title_format) || !title)
3095         {
3096           if (title)
3097             xfree (title);
3098
3099           title = generate_title_string (w, icon_format,
3100                                          DEFAULT_INDEX, CURRENT_DISP);
3101         }
3102       FRAMEMETH (f, set_icon_name_from_bufbyte, (f, title));
3103     }
3104
3105   if (title)
3106     xfree (title);
3107 }
3108
3109 \f
3110 DEFUN ("set-frame-pointer", Fset_frame_pointer, 2, 2, 0, /*
3111 Set the mouse pointer of FRAME to the given pointer image instance.
3112 You should not call this function directly.  Instead, set one of
3113 the variables `text-pointer-glyph', `nontext-pointer-glyph',
3114 `modeline-pointer-glyph', `selection-pointer-glyph',
3115 `busy-pointer-glyph', or `toolbar-pointer-glyph'.
3116 */
3117        (frame, image_instance))
3118 {
3119   struct frame *f = decode_frame (frame);
3120   CHECK_POINTER_IMAGE_INSTANCE (image_instance);
3121   if (!EQ (f->pointer, image_instance))
3122     {
3123       f->pointer = image_instance;
3124       MAYBE_FRAMEMETH (f, set_frame_pointer, (f));
3125     }
3126   return Qnil;
3127 }
3128
3129 \f
3130 void
3131 update_frame_icon (struct frame *f)
3132 {
3133   if (f->icon_changed || f->windows_changed)
3134     {
3135       Lisp_Object frame;
3136       Lisp_Object new_icon;
3137
3138       XSETFRAME (frame, f);
3139       new_icon = glyph_image_instance (Vframe_icon_glyph, frame,
3140                                        ERROR_ME_WARN, 0);
3141       if (!EQ (new_icon, f->icon))
3142         {
3143           f->icon = new_icon;
3144           MAYBE_FRAMEMETH (f, set_frame_icon, (f));
3145         }
3146     }
3147
3148   f->icon_changed = 0;
3149 }
3150
3151 static void
3152 icon_glyph_changed (Lisp_Object glyph, Lisp_Object property,
3153                     Lisp_Object locale)
3154 {
3155   MARK_ICON_CHANGED;
3156 }
3157
3158 \f
3159 /***************************************************************************/
3160 /*                                                                         */
3161 /*                              initialization                             */
3162 /*                                                                         */
3163 /***************************************************************************/
3164
3165 void
3166 init_frame (void)
3167 {
3168 #ifndef PDUMP
3169   if (!initialized)
3170 #endif
3171     {
3172       title_string_emchar_dynarr = Dynarr_new (Emchar);
3173       xzero (title_string_display_line);
3174     }
3175 }
3176
3177 void
3178 syms_of_frame (void)
3179 {
3180   INIT_LRECORD_IMPLEMENTATION (frame);
3181
3182   defsymbol (&Qdelete_frame_hook, "delete-frame-hook");
3183   defsymbol (&Qselect_frame_hook, "select-frame-hook");
3184   defsymbol (&Qdeselect_frame_hook, "deselect-frame-hook");
3185   defsymbol (&Qcreate_frame_hook, "create-frame-hook");
3186   defsymbol (&Qcustom_initialize_frame, "custom-initialize-frame");
3187   defsymbol (&Qmouse_enter_frame_hook, "mouse-enter-frame-hook");
3188   defsymbol (&Qmouse_leave_frame_hook, "mouse-leave-frame-hook");
3189   defsymbol (&Qmap_frame_hook, "map-frame-hook");
3190   defsymbol (&Qunmap_frame_hook, "unmap-frame-hook");
3191
3192   defsymbol (&Qframep, "framep");
3193   defsymbol (&Qframe_live_p, "frame-live-p");
3194   defsymbol (&Qdelete_frame, "delete-frame");
3195   defsymbol (&Qsynchronize_minibuffers, "synchronize-minibuffers");
3196   defsymbol (&Qbuffer_predicate, "buffer-predicate");
3197   defsymbol (&Qframe_being_created, "frame-being-created");
3198   defsymbol (&Qmake_initial_minibuffer_frame, "make-initial-minibuffer-frame");
3199
3200   defsymbol (&Qframe_title_format, "frame-title-format");
3201   defsymbol (&Qframe_icon_title_format, "frame-icon-title-format");
3202
3203   defsymbol (&Qhidden, "hidden");
3204   defsymbol (&Qvisible, "visible");
3205   defsymbol (&Qiconic, "iconic");
3206   defsymbol (&Qinvisible, "invisible");
3207   defsymbol (&Qvisible_iconic, "visible-iconic");
3208   defsymbol (&Qinvisible_iconic, "invisible-iconic");
3209   defsymbol (&Qnomini, "nomini");
3210   defsymbol (&Qvisible_nomini, "visible-nomini");
3211   defsymbol (&Qiconic_nomini, "iconic-nomini");
3212   defsymbol (&Qinvisible_nomini, "invisible-nomini");
3213   defsymbol (&Qvisible_iconic_nomini, "visible-iconic-nomini");
3214   defsymbol (&Qinvisible_iconic_nomini, "invisible-iconic-nomini");
3215
3216   defsymbol (&Qminibuffer, "minibuffer");
3217   defsymbol (&Qunsplittable, "unsplittable");
3218   defsymbol (&Qinternal_border_width, "internal-border-width");
3219   defsymbol (&Qtop_toolbar_shadow_color, "top-toolbar-shadow-color");
3220   defsymbol (&Qbottom_toolbar_shadow_color, "bottom-toolbar-shadow-color");
3221   defsymbol (&Qbackground_toolbar_color, "background-toolbar-color");
3222   defsymbol (&Qtop_toolbar_shadow_pixmap, "top-toolbar-shadow-pixmap");
3223   defsymbol (&Qbottom_toolbar_shadow_pixmap, "bottom-toolbar-shadow-pixmap");
3224   defsymbol (&Qtoolbar_shadow_thickness, "toolbar-shadow-thickness");
3225   defsymbol (&Qscrollbar_placement, "scrollbar-placement");
3226   defsymbol (&Qinter_line_space, "inter-line-space");
3227   /* Qiconic already in this function. */
3228   defsymbol (&Qvisual_bell, "visual-bell");
3229   defsymbol (&Qbell_volume, "bell-volume");
3230   defsymbol (&Qpointer_background, "pointer-background");
3231   defsymbol (&Qpointer_color, "pointer-color");
3232   defsymbol (&Qtext_pointer, "text-pointer");
3233   defsymbol (&Qspace_pointer, "space-pointer");
3234   defsymbol (&Qmodeline_pointer, "modeline-pointer");
3235   defsymbol (&Qgc_pointer, "gc-pointer");
3236   defsymbol (&Qinitially_unmapped, "initially-unmapped");
3237   defsymbol (&Quse_backing_store, "use-backing-store");
3238   defsymbol (&Qborder_color, "border-color");
3239   defsymbol (&Qborder_width, "border-width");
3240   /* Qwidth, Qheight, Qleft, Qtop in general.c */
3241   defsymbol (&Qset_specifier, "set-specifier");
3242   defsymbol (&Qset_glyph_image, "set-glyph-image");
3243   defsymbol (&Qset_face_property, "set-face-property");
3244   defsymbol (&Qface_property_instance, "face-property-instance");
3245   defsymbol (&Qframe_property_alias, "frame-property-alias");
3246
3247   DEFSUBR (Fmake_frame);
3248   DEFSUBR (Fframep);
3249   DEFSUBR (Fframe_live_p);
3250 #if 0 /* FSFmacs */
3251   DEFSUBR (Fignore_event);
3252 #endif
3253   DEFSUBR (Ffocus_frame);
3254   DEFSUBR (Fselect_frame);
3255   DEFSUBR (Fselected_frame);
3256   DEFSUBR (Factive_minibuffer_window);
3257   DEFSUBR (Flast_nonminibuf_frame);
3258   DEFSUBR (Fframe_root_window);
3259   DEFSUBR (Fframe_selected_window);
3260   DEFSUBR (Fset_frame_selected_window);
3261   DEFSUBR (Fframe_device);
3262   DEFSUBR (Fnext_frame);
3263   DEFSUBR (Fprevious_frame);
3264   DEFSUBR (Fdelete_frame);
3265   DEFSUBR (Fmouse_position);
3266   DEFSUBR (Fmouse_pixel_position);
3267   DEFSUBR (Fmouse_position_as_motion_event);
3268   DEFSUBR (Fset_mouse_position);
3269   DEFSUBR (Fset_mouse_pixel_position);
3270   DEFSUBR (Fmake_frame_visible);
3271   DEFSUBR (Fmake_frame_invisible);
3272   DEFSUBR (Ficonify_frame);
3273   DEFSUBR (Fdeiconify_frame);
3274   DEFSUBR (Fframe_visible_p);
3275   DEFSUBR (Fframe_totally_visible_p);
3276   DEFSUBR (Fframe_iconified_p);
3277   DEFSUBR (Fvisible_frame_list);
3278   DEFSUBR (Fraise_frame);
3279   DEFSUBR (Flower_frame);
3280   DEFSUBR (Fframe_property);
3281   DEFSUBR (Fframe_properties);
3282   DEFSUBR (Fset_frame_properties);
3283   DEFSUBR (Fframe_pixel_height);
3284   DEFSUBR (Fframe_pixel_width);
3285   DEFSUBR (Fframe_name);
3286   DEFSUBR (Fframe_modified_tick);
3287   DEFSUBR (Fset_frame_height);
3288   DEFSUBR (Fset_frame_width);
3289   DEFSUBR (Fset_frame_size);
3290   DEFSUBR (Fset_frame_position);
3291   DEFSUBR (Fset_frame_pointer);
3292   DEFSUBR (Fprint_job_page_number);
3293   DEFSUBR (Fprint_job_eject_page);
3294 }
3295
3296 void
3297 vars_of_frame (void)
3298 {
3299   /* */
3300   Vframe_being_created = Qnil;
3301   staticpro (&Vframe_being_created);
3302
3303 #ifdef HAVE_CDE
3304   Fprovide (intern ("cde"));
3305 #endif
3306
3307 #ifdef HAVE_OFFIX_DND
3308   Fprovide (intern ("offix"));
3309 #endif
3310
3311 #if 0 /* FSFmacs stupidity */
3312   xxDEFVAR_LISP ("emacs-iconified", &Vemacs_iconified /*
3313 Non-nil if all of emacs is iconified and frame updates are not needed.
3314 */ );
3315   Vemacs_iconified = Qnil;
3316 #endif
3317
3318   DEFVAR_LISP ("select-frame-hook", &Vselect_frame_hook /*
3319 Function or functions to run just after a new frame is given the focus.
3320 Note that calling `select-frame' does not necessarily set the focus:
3321 The actual window-system focus will not be changed until the next time
3322 that XEmacs is waiting for an event, and even then, the window manager
3323 may refuse the focus-change request.
3324 */ );
3325   Vselect_frame_hook = Qnil;
3326
3327   DEFVAR_LISP ("deselect-frame-hook", &Vdeselect_frame_hook /*
3328 Function or functions to run just before a frame loses the focus.
3329 See `select-frame-hook'.
3330 */ );
3331   Vdeselect_frame_hook = Qnil;
3332
3333   DEFVAR_LISP ("delete-frame-hook", &Vdelete_frame_hook /*
3334 Function or functions to call when a frame is deleted.
3335 One argument, the about-to-be-deleted frame.
3336 */ );
3337   Vdelete_frame_hook = Qnil;
3338
3339   DEFVAR_LISP ("create-frame-hook", &Vcreate_frame_hook /*
3340 Function or functions to call when a frame is created.
3341 One argument, the newly-created frame.
3342 */ );
3343   Vcreate_frame_hook = Qnil;
3344
3345   DEFVAR_LISP ("mouse-enter-frame-hook", &Vmouse_enter_frame_hook /*
3346 Function or functions to call when the mouse enters a frame.
3347 One argument, the frame.
3348 Be careful not to make assumptions about the window manager's focus model.
3349 In most cases, the `deselect-frame-hook' is more appropriate.
3350 */ );
3351   Vmouse_enter_frame_hook = Qnil;
3352
3353   DEFVAR_LISP ("mouse-leave-frame-hook", &Vmouse_leave_frame_hook /*
3354 Function or functions to call when the mouse leaves a frame.
3355 One argument, the frame.
3356 Be careful not to make assumptions about the window manager's focus model.
3357 In most cases, the `select-frame-hook' is more appropriate.
3358 */ );
3359   Vmouse_leave_frame_hook = Qnil;
3360
3361   DEFVAR_LISP ("map-frame-hook", &Vmap_frame_hook /*
3362 Function or functions to call when a frame is mapped.
3363 One argument, the frame.
3364 */ );
3365   Vmap_frame_hook = Qnil;
3366
3367   DEFVAR_LISP ("unmap-frame-hook", &Vunmap_frame_hook /*
3368 Function or functions to call when a frame is unmapped.
3369 One argument, the frame.
3370 */ );
3371   Vunmap_frame_hook = Qnil;
3372
3373   DEFVAR_BOOL ("allow-deletion-of-last-visible-frame",
3374                &allow_deletion_of_last_visible_frame /*
3375 *Non-nil means to assume the force option to delete-frame.
3376 */ );
3377   allow_deletion_of_last_visible_frame = 0;
3378
3379   DEFVAR_LISP ("adjust-frame-function", &Vadjust_frame_function /*
3380 Function or constant controlling adjustment of frame.
3381 When scrollbars, toolbars, default font etc. change in frame, the frame
3382 needs to be adjusted. The adjustment is controlled by this variable.
3383 Legal values are:
3384   nil to keep character frame size unchanged when possible (resize)
3385   t   to keep pixel size unchanged (never resize)
3386   function symbol or lambda form. This function must return boolean
3387       value which is treated as above. Function is passed one parameter,
3388       the frame being adjusted. It function should not modify or delete
3389       the frame.
3390 */ );
3391   Vadjust_frame_function = Qnil;
3392
3393   DEFVAR_LISP ("mouse-motion-handler", &Vmouse_motion_handler /*
3394 Handler for motion events.  One arg, the event.
3395 For most applications, you should use `mode-motion-hook' instead of this.
3396 */ );
3397   Vmouse_motion_handler = Qnil;
3398
3399   DEFVAR_LISP ("synchronize-minibuffers",&Vsynchronize_minibuffers /*
3400 Set to t if all minibuffer windows are to be synchronized.
3401 This will cause echo area messages to appear in the minibuffers of all
3402 visible frames.
3403 */ );
3404   Vsynchronize_minibuffers = Qnil;
3405
3406   DEFVAR_LISP ("frame-title-format", &Vframe_title_format /*
3407 Controls the title of the window-system window of the selected frame.
3408 This is the same format as `modeline-format' with the exception that
3409 %- is ignored.
3410 */ );
3411 /* #### I would change this unilaterally but for the wrath of the Kyles
3412 of the world. */
3413 #ifdef WINDOWSNT
3414   Vframe_title_format = build_string ("%b - XEmacs");
3415 #else
3416   Vframe_title_format = build_string ("%S: %b");
3417 #endif
3418
3419   DEFVAR_LISP ("frame-icon-title-format", &Vframe_icon_title_format /*
3420 Controls the title of the icon corresponding to the selected frame.
3421 See also the variable `frame-title-format'.
3422 */ );
3423   Vframe_icon_title_format = build_string ("%b");
3424
3425   DEFVAR_LISP ("default-frame-name", &Vdefault_frame_name /*
3426 The default name to assign to newly-created frames.
3427 This can be overridden by arguments to `make-frame'.  This must be a string.
3428 This is used primarily for picking up X resources, and is *not* the title
3429 of the frame. (See `frame-title-format'.)
3430 */ );
3431 #ifndef INFODOCK
3432   Vdefault_frame_name = build_string ("emacs");
3433 #else
3434   Vdefault_frame_name = build_string ("InfoDock");
3435 #endif
3436
3437   DEFVAR_LISP ("default-frame-plist", &Vdefault_frame_plist /*
3438 Plist of default values for frame creation, other than the first one.
3439 These may be set in your init file, like this:
3440
3441   \(setq default-frame-plist '(width 80 height 55))
3442
3443 The properties may be in alist format for backward compatibility
3444 but you should not rely on this behavior.
3445
3446 These override values given in window system configuration data,
3447  including X Windows' defaults database.
3448
3449 Since the first X frame is created before loading your .emacs file,
3450 you must use the X resource database for that.
3451
3452 For values specific to the first Emacs frame, see `initial-frame-plist'.
3453 For values specific to the separate minibuffer frame, see
3454  `minibuffer-frame-plist'.
3455
3456 See also the variables `default-x-frame-plist' and
3457 `default-tty-frame-plist', which are like `default-frame-plist'
3458 except that they apply only to X or tty frames, respectively
3459 \(whereas `default-frame-plist' applies to all types of frames).
3460 */ );
3461   Vdefault_frame_plist = Qnil;
3462
3463   DEFVAR_LISP ("frame-icon-glyph", &Vframe_icon_glyph /*
3464 Icon glyph used to iconify a frame.
3465 */ );
3466 }
3467
3468 void
3469 complex_vars_of_frame (void)
3470 {
3471   Vframe_icon_glyph = allocate_glyph (GLYPH_ICON, icon_glyph_changed);
3472 }