(U-0002195D): Add `ideographic-structure'; add `sound@ja/on'; add
[chise/xemacs-chise.git.1] / src / redisplay-output.c
1 /* Synchronize redisplay structures and output changes.
2    Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
3    Copyright (C) 1995, 1996 Ben Wing.
4    Copyright (C) 1996 Chuck Thompson.
5    Copyright (C) 1999, 2002 Andy Piper.
6
7 This file is part of XEmacs.
8
9 XEmacs is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
13
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with XEmacs; see the file COPYING.  If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24 /* Synched up with: Not in FSF. */
25
26 /* This file has been Mule-ized. */
27
28 /* Author: Chuck Thompson */
29
30 /* Heavily hacked for modularity, gutter and subwindow support by Andy
31    Piper. */
32
33 #include <config.h>
34 #include "lisp.h"
35
36 #include "buffer.h"
37 #include "window.h"
38 #include "frame.h"
39 #include "device.h"
40 #include "glyphs.h"
41 #include "redisplay.h"
42 #include "faces.h"
43 #include "gutter.h"
44
45 static int compare_runes (struct window *w, struct rune *crb,
46                           struct rune *drb);
47 static void redraw_cursor_in_window (struct window *w,
48                                      int run_end_begin_glyphs);
49 static void redisplay_output_display_block (struct window *w, struct display_line *dl,
50                                             int block, int start, int end, int start_pixpos,
51                                             int cursor_start, int cursor_width,
52                                             int cursor_height);
53 static void redisplay_normalize_display_box (struct display_box* dest,
54                                              struct display_glyph_area* src);
55 static int redisplay_display_boxes_in_window_p (struct window* w,
56                                                 struct display_box* db,
57                                                 struct display_glyph_area* dga);
58 static void redisplay_clear_clipped_region (Lisp_Object locale, face_index findex,
59                                             struct display_box* dest,
60                                             struct display_glyph_area* glyphsrc,
61                                             int fullheight_p, Lisp_Object);
62
63 /*****************************************************************************
64  sync_rune_structs
65
66  Synchronize the given rune blocks.
67  ****************************************************************************/
68 static void
69 sync_rune_structs (struct window *w, rune_dynarr *cra, rune_dynarr *dra)
70 {
71   int rune_elt;
72   int max_move = ((Dynarr_length (dra) > Dynarr_largest (cra))
73                   ? Dynarr_largest (cra)
74                   : Dynarr_length (dra));
75
76   if (max_move)
77     {
78       /* #### Doing this directly breaks the encapsulation.  But, the
79          running time of this function has a measurable impact on
80          redisplay performance so avoiding all excess overhead is a
81          good thing.  Is all of this true? */
82       memcpy (cra->base, dra->base, sizeof (struct rune) * max_move);
83       Dynarr_set_size (cra, max_move);
84     }
85   else
86     Dynarr_reset (cra);
87
88   for (rune_elt = max_move; rune_elt < Dynarr_length (dra); rune_elt++)
89     {
90       struct rune rb, *crb;
91       struct rune *drb = Dynarr_atp (dra, rune_elt);
92
93       crb = &rb;
94       memcpy (crb, drb, sizeof (struct rune));
95       Dynarr_add (cra, *crb);
96     }
97 }
98
99 /*****************************************************************************
100  sync_display_line_structs
101
102  For the given LINE in window W, make the current display line equal
103  the desired display line.
104  ****************************************************************************/
105 void
106 sync_display_line_structs (struct window *w, int line, int do_blocks,
107                            display_line_dynarr *cdla,
108                            display_line_dynarr *ddla)
109 {
110   int cdla_len = Dynarr_length (cdla);
111
112   struct display_line dl, *clp, *dlp;
113   int db_elt;
114
115   dlp = Dynarr_atp (ddla, line);
116   if (line >= Dynarr_largest (cdla))
117     {
118       clp = &dl;
119       clp->display_blocks = Dynarr_new (display_block);
120     }
121   else
122     {
123       clp = Dynarr_atp (cdla, line);
124       if (clp->display_blocks)
125         Dynarr_reset (clp->display_blocks);
126       if (clp->left_glyphs)
127         {
128           Dynarr_free (clp->left_glyphs);
129           clp->left_glyphs = 0;
130         }
131       if (clp->right_glyphs)
132         {
133           Dynarr_free (clp->right_glyphs);
134           clp->right_glyphs = 0;
135         }
136     }
137   {
138     display_block_dynarr *tdb = clp->display_blocks;
139
140     memcpy (clp, dlp, sizeof (struct display_line));
141     clp->display_blocks = tdb;
142     clp->left_glyphs = 0;
143     clp->right_glyphs = 0;
144   }
145
146   if (!do_blocks && line >= cdla_len)
147     {
148       Dynarr_add (cdla, *clp);
149       return;
150     }
151
152   for (db_elt = 0; db_elt < Dynarr_length (dlp->display_blocks); db_elt++)
153     {
154       struct display_block db, *cdb;
155       struct display_block *ddb = Dynarr_atp (dlp->display_blocks, db_elt);
156
157       if (db_elt >= Dynarr_largest (clp->display_blocks))
158         {
159           cdb = &db;
160           memcpy (cdb, ddb, sizeof (struct display_block));
161           cdb->runes = Dynarr_new (rune);
162           Dynarr_add (clp->display_blocks, *cdb);
163         }
164       else
165         {
166           rune_dynarr *tr;
167
168           cdb = Dynarr_atp (clp->display_blocks, db_elt);
169           tr = cdb->runes;
170           memcpy (cdb, ddb, sizeof (struct display_block));
171           cdb->runes = tr;
172           Dynarr_increment (clp->display_blocks);
173         }
174
175       sync_rune_structs (w, cdb->runes, ddb->runes);
176     }
177
178   if (line >= cdla_len)
179     Dynarr_add (cdla, *clp);
180 }
181
182 /*****************************************************************************
183  compare_runes
184
185  Compare two runes to see if each of their fields is equal.  If so,
186  return true otherwise return false.
187  ****************************************************************************/
188 static int
189 compare_runes (struct window *w, struct rune *crb, struct rune *drb)
190 {
191   /* Do not compare the values of bufpos and endpos.  They do not
192      affect the display characteristics. */
193
194   /* Note: (hanoi 6) spends 95% of its time in redisplay, and about
195      30% here. Not using bitfields for rune.type alone gives a redisplay
196      speed up of 10%.
197
198      #### In profile arcs run of a normal Gnus session this function
199      is run 6.76 million times, only to return 1 in 6.73 million of
200      those.
201
202      In addition a quick look GCC sparc assembly shows that GCC is not
203      doing a good job here.
204      1. The function is not inlined (too complicated?)
205      2. It seems to be reloading the crb and drb variables all the
206      time.
207      3. It doesn't seem to notice that the second half of these if's
208      are really a switch statement.
209
210      So I (JV) conjecture
211
212      #### It would really be worth it to arrange for this function to
213      be (almost) a single call to memcmp. */
214
215   if (crb->xpos != drb->xpos)
216     return 0;
217   else if (crb->width != drb->width)
218     return 0;
219   else if (crb->cursor_type != drb->cursor_type)
220     return 0;
221   else if (crb->type != drb->type)
222     return 0;
223   else if (crb->type == RUNE_CHAR &&
224            !CHARC_EQ (crb->object.cglyph, drb->object.cglyph))
225     return 0;
226   else if (crb->type == RUNE_HLINE &&
227            (crb->object.hline.thickness != drb->object.hline.thickness ||
228             crb->object.hline.yoffset != drb->object.hline.yoffset))
229     return 0;
230   else if (crb->type == RUNE_DGLYPH &&
231            (!EQ (crb->object.dglyph.glyph, drb->object.dglyph.glyph) ||
232             !EQ (crb->object.dglyph.extent, drb->object.dglyph.extent) ||
233             crb->object.dglyph.xoffset != drb->object.dglyph.xoffset ||
234             crb->object.dglyph.yoffset != drb->object.dglyph.yoffset ||
235             crb->object.dglyph.ascent != drb->object.dglyph.ascent ||
236             crb->object.dglyph.descent != drb->object.dglyph.descent))
237     return 0;
238   /* Only check dirtiness if we know something has changed. */
239   else if (crb->type == RUNE_DGLYPH &&
240            (XGLYPH_DIRTYP (crb->object.dglyph.glyph) ||
241             crb->findex != drb->findex))
242     {
243       /* We need some way of telling redisplay_output_layout () that the
244          only reason we are outputting it is because something has
245          changed internally. That way we can optimize whether we need
246          to clear the layout first and also only output the components
247          that have changed. The image_instance dirty flag and
248          display_hash are no good to us because these will invariably
249          have been set anyway if the layout has changed. So it looks
250          like we need yet another change flag that we can set here and
251          then clear in redisplay_output_layout (). */
252       Lisp_Object window, image;
253       Lisp_Image_Instance* ii;
254       XSETWINDOW (window, w);
255       image = glyph_image_instance (crb->object.dglyph.glyph,
256                                     window, ERROR_ME_NOT, 1);
257
258       if (!IMAGE_INSTANCEP (image))
259         return 0;
260       ii = XIMAGE_INSTANCE (image);
261
262       if (TEXT_IMAGE_INSTANCEP (image) &&
263           (crb->findex != drb->findex ||
264            WINDOW_FACE_CACHEL_DIRTY (w, drb->findex)))
265         return 0;
266
267       /* It is quite common for the two glyphs to be EQ since in many
268          cases they will actually be the same object. This does not
269          mean, however, that nothing has changed. We therefore need to
270          check the current hash of the glyph against the last recorded
271          display hash and the pending display items. See
272          update_subwindow (). */
273       if (image_instance_changed (image) ||
274           crb->findex != drb->findex ||
275           WINDOW_FACE_CACHEL_DIRTY (w, drb->findex))
276         {
277           /* Now we are going to re-output the glyph, but since
278              this is for some internal reason not related to geometry
279              changes, send a hint to the output routines that they can
280              take some short cuts. This is most useful for
281              layouts. This flag should get reset by the output
282              routines.
283
284              #### It is possible for us to get here when the
285              face_cachel is dirty. I do not know what the implications
286              of this are.*/
287           IMAGE_INSTANCE_OPTIMIZE_OUTPUT (ii) = 1;
288           return 0;
289         }
290       else
291         return 1;
292     }
293   /* We now do this last so that glyph checks can do their own thing
294      for face changes. Face changes quite often happen when we are
295      trying to output something in the gutter, this would normally
296      lead to a lot of flashing. The indices can quite often be
297      different and yet the faces are the same, we do not want to
298      re-output in this instance. */
299   else  if (crb->findex != drb->findex ||
300             WINDOW_FACE_CACHEL_DIRTY (w, drb->findex))
301     return 0;
302   else
303     return 1;
304 }
305
306 /*****************************************************************************
307  get_next_display_block
308
309  Return the next display starting at or overlapping START_POS.  Return
310  the start of the next region in NEXT_START.
311  ****************************************************************************/
312 int
313 get_next_display_block (layout_bounds bounds, display_block_dynarr *dba,
314                         int start_pos, int *next_start)
315 {
316   int next_display_block = NO_BLOCK;
317   int priority = -1;
318   int block;
319
320   /* If we don't find a display block covering or starting at
321      start_pos, then we return the starting point of the next display
322      block or the next division boundary, whichever is closer to
323      start_pos. */
324   if (next_start)
325     {
326       if (start_pos >= bounds.left_out && start_pos < bounds.left_in)
327         *next_start = bounds.left_in;
328       else if (start_pos < bounds.left_white)
329         *next_start = bounds.left_white;
330       else if (start_pos < bounds.right_white)
331         *next_start = bounds.right_white;
332       else if (start_pos < bounds.right_in)
333         *next_start = bounds.right_in;
334       else if (start_pos <= bounds.right_out)
335         *next_start = bounds.right_out;
336       else
337         ABORT ();
338     }
339
340   for (block = 0; block < Dynarr_length (dba); block++)
341     {
342       struct display_block *db = Dynarr_atp (dba, block);
343
344       if (db->start_pos <= start_pos && db->end_pos > start_pos)
345         {
346           if ((int) db->type > priority)
347             {
348               priority = db->type;
349               next_display_block = block;
350               if (next_start)
351                 *next_start = db->end_pos;
352             }
353         }
354       else if (next_start && db->start_pos > start_pos)
355         {
356           if (db->start_pos < *next_start)
357             *next_start = db->start_pos;
358         }
359     }
360
361   return next_display_block;
362 }
363
364 /*****************************************************************************
365  get_cursor_size_and_location
366
367  Return the information defining the pixel location of the cursor.
368  ****************************************************************************/
369 static void
370 get_cursor_size_and_location (struct window *w, struct display_block *db,
371                               int cursor_location,
372                               int *cursor_start, int *cursor_width,
373                               int *cursor_height)
374 {
375   struct rune *rb;
376   Lisp_Object window;
377   int defheight, defwidth;
378
379   if (Dynarr_length (db->runes) <= cursor_location)
380     ABORT ();
381
382   XSETWINDOW (window, w);
383
384   rb = Dynarr_atp (db->runes, cursor_location);
385   *cursor_start = rb->xpos;
386
387   default_face_height_and_width (window, &defheight, &defwidth);
388   *cursor_height = defheight;
389
390   if (rb->type == RUNE_BLANK)
391     *cursor_width = defwidth;
392   else
393     *cursor_width = rb->width;
394 }
395
396 /*****************************************************************************
397  compare_display_blocks
398
399  Given two display blocks, output only those areas where they differ.
400  ****************************************************************************/
401 static int
402 compare_display_blocks (struct window *w, struct display_line *cdl,
403                         struct display_line *ddl, int c_block, int d_block,
404                         int start_pixpos, int cursor_start, int cursor_width,
405                         int cursor_height)
406 {
407   struct frame *f = XFRAME (w->frame);
408   struct display_block *cdb, *ddb;
409   int start_pos;
410   int stop_pos;
411   int force = 0;
412   int block_end;
413
414   cdb = Dynarr_atp (cdl->display_blocks, c_block);
415   ddb = Dynarr_atp (ddl->display_blocks, d_block);
416
417   assert (cdb->type == ddb->type);
418
419   start_pos = -1;
420   stop_pos = min (Dynarr_length (cdb->runes), Dynarr_length (ddb->runes));
421
422   block_end =
423     (!Dynarr_length (ddb->runes)
424      ? 0
425      : (Dynarr_atp (ddb->runes, Dynarr_length (ddb->runes) - 1)->xpos +
426         Dynarr_atp (ddb->runes, Dynarr_length (ddb->runes) - 1)->width));
427
428   /* If the new block type is not text and the cursor status is
429      changing and it overlaps the position of this block then force a
430      full redraw of the block in order to make sure that the cursor is
431      updated properly. */
432   if (ddb->type != TEXT
433 #if 0
434       /* I'm not sure exactly what this code wants to do, but it's
435        * not right--it doesn't update when cursor_elt changes from, e.g.,
436        * 0 to 8, and the new or old cursor loc overlaps this block.
437        * I've replaced it with the more conservative test below.
438        * -dkindred@cs.cmu.edu 23-Mar-1997 */
439       && ((cdl->cursor_elt == -1 && ddl->cursor_elt != -1)
440           || (cdl->cursor_elt != -1 && ddl->cursor_elt == -1))
441       && (ddl->cursor_elt == -1 ||
442           (cursor_start
443            && cursor_width
444            && (cursor_start + cursor_width) >= start_pixpos
445            && cursor_start <= block_end))
446 #else
447       && (cdl->cursor_elt != ddl->cursor_elt)
448 #endif
449       )
450     force = 1;
451
452   if (f->windows_structure_changed ||
453       /* #### Why is this so? We have face cachels so that we don't
454          have to recalculate all the display blocks when faces
455          change. I have fixed this for glyphs and am inclined to think
456          that faces should "Just Work", but I'm not feeling brave
457          today. Maybe its because the face cachels represent merged
458          faces rather than simply instantiations in a particular
459          domain. */
460       f->faces_changed ||
461       cdl->ypos != ddl->ypos ||
462       cdl->ascent != ddl->ascent ||
463       cdl->descent != ddl->descent ||
464       cdl->clip != ddl->clip ||
465       force)
466     {
467       start_pos = 0;
468       force = 1;
469     }
470   else
471     {
472       int elt = 0;
473
474       while (start_pos < 0 && elt < stop_pos)
475         {
476           if (!compare_runes (w, Dynarr_atp (cdb->runes, elt),
477                               Dynarr_atp (ddb->runes, elt)))
478             {
479               start_pos = elt;
480             }
481           else
482             {
483               elt++;
484             }
485         }
486
487       /* If nothing has changed in the area where the blocks overlap, but
488          there are new blocks in the desired block, then adjust the start
489          point accordingly. */
490       if (elt == stop_pos && stop_pos < Dynarr_length (ddb->runes))
491         start_pos = stop_pos;
492     }
493
494   if (start_pos >= 0)
495     {
496       if ((Dynarr_length (ddb->runes) != Dynarr_length (cdb->runes))
497           || force)
498         {
499           stop_pos = Dynarr_length (ddb->runes);
500         }
501       else
502         {
503           /* If the lines have the same number of runes and we are not
504              forcing a full redraw because the display line has
505              changed position then we try and optimize how much of the
506              line we actually redraw by scanning backwards from the
507              end for the first changed rune.  This optimization is
508              almost always triggered by face changes. */
509
510           int elt = Dynarr_length (ddb->runes) - 1;
511
512           while (elt > start_pos)
513             {
514               if (!compare_runes (w, Dynarr_atp (cdb->runes, elt),
515                                   Dynarr_atp (ddb->runes, elt)))
516                 break;
517               else
518                 elt--;
519             }
520           stop_pos = elt + 1;
521         }
522
523       redisplay_output_display_block (w, ddl, d_block, start_pos,
524                                       stop_pos, start_pixpos,
525                                       cursor_start, cursor_width,
526                                       cursor_height);
527       return 1;
528     }
529
530   return 0;
531 }
532
533 /*****************************************************************************
534  clear_left_border
535
536  Clear the lefthand outside border.
537  ****************************************************************************/
538 static void
539 clear_left_border (struct window *w, int y, int height)
540 {
541   struct frame *f = XFRAME (w->frame);
542   Lisp_Object window;
543
544   XSETWINDOW (window, w);
545   redisplay_clear_region (window, DEFAULT_INDEX,
546                 FRAME_LEFT_BORDER_START (f), y,
547                 FRAME_BORDER_WIDTH (f), height);
548 }
549
550 /*****************************************************************************
551  clear_right_border
552
553  Clear the righthand outside border.
554  ****************************************************************************/
555 static void
556 clear_right_border (struct window *w, int y, int height)
557 {
558   struct frame *f = XFRAME (w->frame);
559   Lisp_Object window;
560
561   XSETWINDOW (window, w);
562   redisplay_clear_region (window, DEFAULT_INDEX,
563                 FRAME_RIGHT_BORDER_START (f),
564                 y, FRAME_BORDER_WIDTH (f), height);
565 }
566
567 /*****************************************************************************
568  output_display_line
569
570  Ensure that the contents of the given display line is correct
571  on-screen.  The force_ parameters are used by redisplay_move_cursor
572  to correctly update cursor locations and only cursor locations.
573  ****************************************************************************/
574 void
575 output_display_line (struct window *w, display_line_dynarr *cdla,
576                      display_line_dynarr *ddla, int line, int force_start,
577                      int force_end)
578
579 {
580   struct frame *f = XFRAME (w->frame);
581   struct buffer *b = XBUFFER (w->buffer);
582   struct buffer *old_b = window_display_buffer (w);
583   struct display_line *cdl, *ddl;
584   display_block_dynarr *cdba, *ddba;
585   int start_pixpos, end_pixpos;
586   int cursor_start, cursor_width, cursor_height;
587
588   int force = (force_start >= 0 || force_end >= 0);
589   int clear_border = 0;
590   int must_sync = 0;
591
592   if (cdla && line < Dynarr_length (cdla))
593     {
594       cdl = Dynarr_atp (cdla, line);
595       cdba = cdl->display_blocks;
596     }
597   else
598     {
599       cdl = NULL;
600       cdba = NULL;
601     }
602
603   ddl = Dynarr_atp (ddla, line);      /* assert line < Dynarr_length (ddla) */
604   ddba = ddl->display_blocks;
605
606   if (force_start >= 0 && force_start >= ddl->bounds.left_out)
607     start_pixpos = force_start;
608   else
609     start_pixpos = ddl->bounds.left_out;
610
611   if (force_end >= 0 && force_end < ddl->bounds.right_out)
612     end_pixpos = force_end;
613   else
614     end_pixpos = ddl->bounds.right_out;
615
616   /* Get the cursor parameters. */
617   if (ddl->cursor_elt != -1)
618     {
619       struct display_block *db;
620
621       /* If the lines cursor parameter is not -1 then it indicates
622          which rune in the TEXT block contains the cursor.  This means
623          that there must be at least one display block.  The TEXT
624          block, if present, must always be the first display block. */
625       assert (Dynarr_length (ddba) != 0);
626
627       db = Dynarr_atp (ddba, 0);
628       assert (db->type == TEXT);
629
630       get_cursor_size_and_location (w, db, ddl->cursor_elt, &cursor_start,
631                                     &cursor_width, &cursor_height);
632     }
633   else
634     {
635       cursor_start = cursor_width = cursor_height = 0;
636     }
637
638   /* The modeline should only have a single block and it had better be
639      a TEXT block. */
640   if (ddl->modeline)
641     {
642       /* The shadow thickness check is necessary if only the sign of
643          the size changed. */
644       if (cdba && !w->shadow_thickness_changed)
645         {
646           must_sync |= compare_display_blocks (w, cdl, ddl, 0, 0,
647                                                start_pixpos, 0, 0, 0);
648         }
649       else
650         {
651           redisplay_output_display_block (w, ddl, 0, 0, -1, start_pixpos,
652                                           0, 0, 0);
653           must_sync = 1;
654         }
655
656       if (must_sync)
657         clear_border = 1;
658     }
659
660   while (!ddl->modeline && start_pixpos < end_pixpos)
661     {
662       int block;
663       int next_start_pixpos;
664
665       block = get_next_display_block (ddl->bounds, ddba, start_pixpos,
666                                       &next_start_pixpos);
667
668       /* If we didn't find a block then we should blank the area
669          between start_pos and next_start if necessary. */
670       if (block == NO_BLOCK)
671         {
672           /* We only erase those areas which were actually previously
673              covered by a display block unless the window structure
674              changed.  In that case we clear all areas since the current
675              structures may actually represent a different buffer. */
676           while (start_pixpos < next_start_pixpos)
677             {
678               int block_end;
679               int old_block;
680
681               if (cdba)
682                 old_block = get_next_display_block (ddl->bounds, cdba,
683                                                     start_pixpos, &block_end);
684               else
685                 {
686                   old_block = NO_BLOCK;
687                   block_end = next_start_pixpos;
688                 }
689
690               if (!cdba || old_block != NO_BLOCK || b != old_b ||
691                   f->windows_structure_changed ||
692                   f->faces_changed ||
693                   force ||
694                   (cdl && (cdl->ypos != ddl->ypos ||
695                            cdl->ascent != ddl->ascent ||
696                            cdl->descent != ddl->descent ||
697                            cdl->top_clip != ddl->top_clip ||
698                            cdl->clip != ddl->clip)))
699                 {
700                   int x, y, width, height;
701                   face_index findex;
702
703                   must_sync = 1;
704                   x = start_pixpos;
705                   y = DISPLAY_LINE_YPOS (ddl);
706                   width = min (next_start_pixpos, block_end) - x;
707                   height = DISPLAY_LINE_HEIGHT (ddl);
708
709                   if (x < ddl->bounds.left_in)
710                     {
711                       findex = ddl->left_margin_findex ?
712                         ddl->left_margin_findex
713                         : get_builtin_face_cache_index (w, Vleft_margin_face);
714                     }
715                   else if (x < ddl->bounds.right_in)
716                     {
717                       /* no check here because DEFAULT_INDEX == 0 anyway */
718                       findex = ddl->default_findex;
719                     }
720                   else if (x < ddl->bounds.right_out)
721                     {
722                       findex = ddl->right_margin_findex ?
723                         ddl->right_margin_findex
724                         : get_builtin_face_cache_index (w, Vright_margin_face);
725                     }
726                   else
727                     findex = (face_index) -1;
728
729                   if (findex != (face_index) -1)
730                     {
731                       Lisp_Object window;
732
733                       XSETWINDOW (window, w);
734
735                       /* Clear the empty area. */
736                       redisplay_clear_region (window, findex, x, y, width, height);
737
738                       /* Mark that we should clear the border.  This is
739                          necessary because italic fonts may leave
740                          droppings in the border. */
741                       clear_border = 1;
742                     }
743                 }
744
745               start_pixpos = min (next_start_pixpos, block_end);
746             }
747         }
748       else
749         {
750           struct display_block *cdb, *ddb;
751           int block_end;
752           int old_block;
753
754           if (cdba)
755             old_block = get_next_display_block (ddl->bounds, cdba,
756                                                 start_pixpos, &block_end);
757           else
758             old_block = NO_BLOCK;
759
760           ddb = Dynarr_atp (ddba, block);
761           cdb = (old_block != NO_BLOCK ? Dynarr_atp (cdba, old_block) : 0);
762
763           /* If there was formerly no block over the current
764              region or if it was a block of a different type, then
765              output the entire ddb.  Otherwise, compare cdb and
766              ddb and output only the changed region. */
767           if (!force && cdb && ddb->type == cdb->type
768               /* If there was no buffer being display before the
769                  compare anyway as we might be outputting a gutter. */
770               &&
771               (b == old_b || !old_b))
772             {
773               must_sync |= compare_display_blocks (w, cdl, ddl, old_block,
774                                                    block, start_pixpos,
775                                                    cursor_start, cursor_width,
776                                                    cursor_height);
777             }
778           else
779             {
780               int elt;
781               int first_elt = 0;
782               int last_elt = -1;
783
784               for (elt = 0; elt < Dynarr_length (ddb->runes); elt++)
785                 {
786                   struct rune *rb = Dynarr_atp (ddb->runes, elt);
787
788                   if (start_pixpos >= rb->xpos
789                       && start_pixpos < rb->xpos + rb->width)
790                     first_elt = elt;
791
792                   if (end_pixpos > rb->xpos
793                       && end_pixpos <= rb->xpos + rb->width)
794                     {
795                       last_elt = elt + 1;
796                       if (last_elt > Dynarr_length (ddb->runes))
797                         last_elt = Dynarr_length (ddb->runes);
798                       break;
799                     }
800                 }
801
802               must_sync = 1;
803               redisplay_output_display_block (w, ddl, block, first_elt,
804                                               last_elt,
805                                               start_pixpos,
806                                               cursor_start, cursor_width,
807                                               cursor_height);
808             }
809
810           start_pixpos = next_start_pixpos;
811         }
812     }
813
814   /* Clear the internal border if we are next to it and the window
815      structure or frame size has changed or if something caused
816      clear_border to be tripped.  */
817   /* #### Doing this on f->clear sucks but is necessary because of
818      window-local background values. */
819   if (f->windows_structure_changed || f->faces_changed || clear_border
820       || f->clear)
821     {
822       int y = DISPLAY_LINE_YPOS (ddl);
823       int height = DISPLAY_LINE_HEIGHT (ddl);
824
825       /* If we are in the gutter then we musn't clear the borders. */
826       if (y >= WINDOW_TEXT_TOP (w) && (y + height) <= WINDOW_TEXT_BOTTOM (w))
827         {
828           if (ddl->modeline)
829             {
830               y -= MODELINE_SHADOW_THICKNESS (w);
831               height += (2 * MODELINE_SHADOW_THICKNESS (w));
832             }
833
834           if (window_is_leftmost (w))
835             clear_left_border (w, y, height);
836           if (window_is_rightmost (w))
837             clear_right_border (w, y, height);
838         }
839     }
840
841   if (cdla)
842     sync_display_line_structs (w, line, must_sync, cdla, ddla);
843 }
844
845 /*****************************************************************************
846  redisplay_move_cursor
847
848  For the given window W, move the cursor to NEW_POINT.  Returns a
849  boolean indicating success or failure.
850  ****************************************************************************/
851
852 #define ADJ_BUFPOS (rb->bufpos + dl->offset)
853 #define ADJ_ENDPOS (rb->endpos + dl->offset)
854
855 int
856 redisplay_move_cursor (struct window *w, Bufpos new_point, int no_output_end)
857 {
858   struct frame *f = XFRAME (w->frame);
859   struct device *d = XDEVICE (f->device);
860
861   display_line_dynarr *cla = window_display_lines (w, CURRENT_DISP);
862   struct display_line *dl;
863   struct display_block *db;
864   struct rune *rb;
865   int x = w->last_point_x[CURRENT_DISP];
866   int y = w->last_point_y[CURRENT_DISP];
867
868   /*
869    * Bail if cursor_in_echo_area is non-zero and we're fiddling with
870    * the cursor in a non-active minibuffer window, since that is a
871    * special case that is handled elsewhere and this function need
872    * not handle it.  Return 1 so the caller will assume we
873    * succeeded.
874    */
875   if (cursor_in_echo_area && MINI_WINDOW_P (w) &&
876       w != XWINDOW (FRAME_SELECTED_WINDOW (f)))
877     return 1;
878
879   if (y < 0 || y >= Dynarr_length (cla))
880     return 0;
881
882   dl = Dynarr_atp (cla, y);
883   db = get_display_block_from_line (dl, TEXT);
884
885   if (x < 0 || x >= Dynarr_length (db->runes))
886     return 0;
887
888   rb = Dynarr_atp (db->runes, x);
889
890   if (rb->cursor_type == CURSOR_OFF)
891     return 0;
892   else if (ADJ_BUFPOS == new_point
893            || (ADJ_ENDPOS && (new_point >= ADJ_BUFPOS)
894                && (new_point <= ADJ_ENDPOS)))
895     {
896       w->last_point_x[CURRENT_DISP] = x;
897       w->last_point_y[CURRENT_DISP] = y;
898       Fset_marker (w->last_point[CURRENT_DISP], make_int (ADJ_BUFPOS),
899                    w->buffer);
900       dl->cursor_elt = x;
901       return 1;
902     }
903   else
904     {
905       {
906         MAYBE_DEVMETH (d, frame_output_begin, (f));
907         MAYBE_DEVMETH (d, window_output_begin, (w));
908       }
909       rb->cursor_type = CURSOR_OFF;
910       dl->cursor_elt = -1;
911       output_display_line (w, 0, cla, y, rb->xpos, rb->xpos + rb->width);
912     }
913
914   w->last_point_x[CURRENT_DISP] = -1;
915   w->last_point_y[CURRENT_DISP] = -1;
916   Fset_marker (w->last_point[CURRENT_DISP], Qnil, w->buffer);
917
918   /* If this isn't the selected frame, then erasing the old cursor is
919      all we actually had to do. */
920   if (w != XWINDOW (FRAME_SELECTED_WINDOW (device_selected_frame (d))))
921     {
922       if (!no_output_end)
923         {
924           MAYBE_DEVMETH (d, window_output_end, (w));
925           MAYBE_DEVMETH (d, frame_output_end, (f));
926         }
927
928       return 1;
929     }
930
931   /* This should only occur in the minibuffer. */
932   if (new_point == 0)
933     {
934       w->last_point_x[CURRENT_DISP] = 0;
935       w->last_point_y[CURRENT_DISP] = y;
936       Fset_marker (w->last_point[CURRENT_DISP], Qzero, w->buffer);
937
938       rb = Dynarr_atp (db->runes, 0);
939       rb->cursor_type = CURSOR_ON;
940       dl->cursor_elt = 0;
941
942       output_display_line (w, 0, cla, y, rb->xpos, rb->xpos + rb->width);
943
944       if (!no_output_end)
945         {
946           MAYBE_DEVMETH (d, window_output_end, (w));
947           MAYBE_DEVMETH (d, frame_output_end, (f));
948         }
949       return 1;
950     }
951   else
952     {
953       int cur_rb = 0;
954       int first = 0;
955       int cur_dl, up;
956
957       if (ADJ_BUFPOS < new_point)
958         {
959           up = 1;
960           cur_rb = x + 1;
961           cur_dl = y;
962         }
963       else /* (rb->bufpos + dl->offset) > new_point */
964         {
965           up = 0;
966
967           if (!x)
968             {
969               cur_dl = y - 1;
970               first = 0;
971             }
972           else
973             {
974               cur_rb = x - 1;
975               cur_dl = y;
976               first = 1;
977             }
978         }
979
980       while (up ? (cur_dl < Dynarr_length (cla)) : (cur_dl >= 0))
981         {
982           dl = Dynarr_atp (cla, cur_dl);
983           db = get_display_block_from_line (dl, TEXT);
984
985           if (!up && !first)
986             cur_rb = Dynarr_length (db->runes) - 1;
987
988           while ((!scroll_on_clipped_lines || !dl->clip) &&
989                  (up ? (cur_rb < Dynarr_length (db->runes)) : (cur_rb >= 0)))
990             {
991               rb = Dynarr_atp (db->runes, cur_rb);
992
993               if (rb->cursor_type != IGNORE_CURSOR
994                   && rb->cursor_type != NO_CURSOR &&
995                   (ADJ_BUFPOS == new_point
996                    || (ADJ_ENDPOS && (new_point >= ADJ_BUFPOS)
997                        && (new_point <= ADJ_BUFPOS))))
998                 {
999                   rb->cursor_type = CURSOR_ON;
1000                   dl->cursor_elt = cur_rb;
1001
1002
1003                   output_display_line (w, 0, cla, cur_dl, rb->xpos,
1004                                        rb->xpos + rb->width);
1005
1006                   w->last_point_x[CURRENT_DISP] = cur_rb;
1007                   w->last_point_y[CURRENT_DISP] = cur_dl;
1008                   Fset_marker (w->last_point[CURRENT_DISP],
1009                                make_int (ADJ_BUFPOS), w->buffer);
1010
1011                   if (!no_output_end)
1012                     {
1013                       MAYBE_DEVMETH (d, window_output_end, (w));
1014                       MAYBE_DEVMETH (d, frame_output_end, (f));
1015                     }
1016                   return 1;
1017                 }
1018
1019               (up ? cur_rb++ : cur_rb--);
1020             }
1021
1022           (up ? (cur_rb = 0) : (first = 0));
1023           (up ? cur_dl++ : cur_dl--);
1024         }
1025     }
1026
1027   if (!no_output_end)
1028     {
1029       MAYBE_DEVMETH (d, window_output_end, (w));
1030       MAYBE_DEVMETH (d, frame_output_end, (f));
1031     }
1032   return 0;
1033 }
1034 #undef ADJ_BUFPOS
1035 #undef ADJ_ENDPOS
1036
1037 /*****************************************************************************
1038  redraw_cursor_in_window
1039
1040  For the given window W, redraw the cursor if it is contained within
1041  the window.
1042  ****************************************************************************/
1043 static void
1044 redraw_cursor_in_window (struct window *w, int run_end_begin_meths)
1045 {
1046   struct frame *f = XFRAME (w->frame);
1047   struct device *d = XDEVICE (f->device);
1048
1049   display_line_dynarr *dla = window_display_lines (w, CURRENT_DISP);
1050   struct display_line *dl;
1051   struct display_block *db;
1052   struct rune *rb;
1053
1054   int x = w->last_point_x[CURRENT_DISP];
1055   int y = w->last_point_y[CURRENT_DISP];
1056
1057   if (cursor_in_echo_area && MINI_WINDOW_P (w) &&
1058       !echo_area_active (f) && minibuf_level == 0)
1059     {
1060       MAYBE_DEVMETH (d, set_final_cursor_coords, (f, w->pixel_top, 0));
1061     }
1062
1063   if (y < 0 || y >= Dynarr_length (dla))
1064     return;
1065
1066   if (MINI_WINDOW_P (w) && f != device_selected_frame (d) &&
1067       !is_surrogate_for_selected_frame (f))
1068     return;
1069
1070   dl = Dynarr_atp (dla, y);
1071   db = get_display_block_from_line (dl, TEXT);
1072
1073   if (x < 0 || x >= Dynarr_length (db->runes))
1074     return;
1075
1076   rb = Dynarr_atp (db->runes, x);
1077
1078   /* Don't call the output routine if the block isn't actually the
1079      cursor. */
1080   if (rb->cursor_type == CURSOR_ON)
1081     {
1082       MAYBE_DEVMETH (d, set_final_cursor_coords,
1083                      (f, dl->ypos - 1, rb->xpos));
1084
1085       if (run_end_begin_meths)
1086         {
1087           MAYBE_DEVMETH (d, frame_output_begin, (f));
1088           MAYBE_DEVMETH (d, window_output_begin, (w));
1089         }
1090
1091       output_display_line (w, 0, dla, y, rb->xpos, rb->xpos + rb->width);
1092
1093       if (run_end_begin_meths)
1094         {
1095           MAYBE_DEVMETH (d, window_output_end, (w));
1096           MAYBE_DEVMETH (d, frame_output_end, (f));
1097         }
1098     }
1099 }
1100
1101 /*****************************************************************************
1102  redisplay_redraw_cursor
1103
1104  For the given frame F, redraw the cursor on the selected window.
1105  This is used to update the cursor after focus changes.
1106  ****************************************************************************/
1107 void
1108 redisplay_redraw_cursor (struct frame *f, int run_end_begin_meths)
1109 {
1110   Lisp_Object window;
1111
1112   if (!cursor_in_echo_area)
1113     window = FRAME_SELECTED_WINDOW (f);
1114   else if (FRAME_HAS_MINIBUF_P (f))
1115     window = FRAME_MINIBUF_WINDOW (f);
1116   else
1117     return;
1118
1119   redraw_cursor_in_window (XWINDOW (window), run_end_begin_meths);
1120 }
1121
1122 /****************************************************************************
1123  redisplay_output_display_block
1124
1125  Given a display line, a block number for that start line, output all
1126  runes between start and end in the specified display block.
1127  ****************************************************************************/
1128 static void
1129 redisplay_output_display_block (struct window *w, struct display_line *dl, int block,
1130                                 int start, int end, int start_pixpos, int cursor_start,
1131                                 int cursor_width, int cursor_height)
1132 {
1133   struct frame *f = XFRAME (w->frame);
1134   struct device *d = XDEVICE (f->device);
1135   /* Temporarily disabled until generalization is done. */
1136 #if 0
1137   struct display_block *db = Dynarr_atp (dl->display_blocks, block);
1138   rune_dynarr *rba = db->runes;
1139   struct rune *rb;
1140   int xpos, width;
1141   rb = Dynarr_atp (rba, start);
1142
1143   if (!rb)
1144       /* Nothing to do so don't do anything. */
1145       return;
1146
1147   xpos = max (start_pixpos, rb->xpos);
1148
1149   if (end < 0)
1150     end = Dynarr_length (rba);
1151
1152   rb  = Dynarr_atp (rba, end - 1);
1153   width = rb->xpos + rb->width - xpos;
1154 #endif
1155   /* now actually output the block. */
1156   DEVMETH (d, output_display_block, (w, dl, block, start,
1157                                      end, start_pixpos,
1158                                      cursor_start, cursor_width,
1159                                      cursor_height));
1160 }
1161
1162 /****************************************************************************
1163  redisplay_unmap_subwindows
1164
1165  Remove subwindows from the area in the box defined by the given
1166  parameters.
1167  ****************************************************************************/
1168 static void
1169 redisplay_unmap_subwindows (struct frame* f, int x, int y, int width, int height,
1170                             Lisp_Object ignored_window)
1171 {
1172   Lisp_Object rest;
1173
1174   LIST_LOOP (rest, XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f)))
1175     {
1176       Lisp_Image_Instance *ii = XIMAGE_INSTANCE (XCAR (rest));
1177       if (IMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (ii)
1178           &&
1179           IMAGE_INSTANCE_DISPLAY_X (ii)
1180           + IMAGE_INSTANCE_DISPLAY_WIDTH (ii) > (unsigned) x
1181           &&
1182           IMAGE_INSTANCE_DISPLAY_X (ii) < (unsigned) (x + width)
1183           &&
1184           IMAGE_INSTANCE_DISPLAY_Y (ii)
1185           + IMAGE_INSTANCE_DISPLAY_HEIGHT (ii) > (unsigned) y
1186           &&
1187           IMAGE_INSTANCE_DISPLAY_Y (ii) < (unsigned) (y + height)
1188           &&
1189           !EQ (XCAR (rest), ignored_window))
1190         {
1191           unmap_subwindow (XCAR (rest));
1192         }
1193     }
1194 }
1195
1196 /****************************************************************************
1197  redisplay_unmap_subwindows_maybe
1198
1199  Potentially subwindows from the area in the box defined by the given
1200  parameters.
1201  ****************************************************************************/
1202 void redisplay_unmap_subwindows_maybe (struct frame* f, int x, int y, int width, int height)
1203 {
1204   if (!NILP (XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f))))
1205     {
1206       redisplay_unmap_subwindows (f, x, y, width, height, Qnil);
1207     }
1208 }
1209
1210 static void redisplay_unmap_subwindows_except_us (struct frame* f, int x, int y, int width,
1211                                                   int height, Lisp_Object subwindow)
1212 {
1213   if (!NILP (XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f))))
1214     {
1215       redisplay_unmap_subwindows (f, x, y, width, height, subwindow);
1216     }
1217 }
1218
1219 /****************************************************************************
1220  redisplay_output_subwindow
1221
1222  output a subwindow.  This code borrows heavily from the pixmap stuff,
1223  although is much simpler not needing to account for partial
1224  pixmaps, backgrounds etc.
1225  ****************************************************************************/
1226 void
1227 redisplay_output_subwindow (struct window *w,
1228                             Lisp_Object image_instance,
1229                             struct display_box* db, struct display_glyph_area* dga,
1230                             face_index findex, int cursor_start, int cursor_width,
1231                             int cursor_height)
1232 {
1233   Lisp_Image_Instance *p = XIMAGE_INSTANCE (image_instance);
1234   Lisp_Object window;
1235   struct display_glyph_area sdga;
1236
1237   dga->height = IMAGE_INSTANCE_HEIGHT (p);
1238   dga->width = IMAGE_INSTANCE_WIDTH (p);
1239
1240   /* The first thing we are going to do is update the display
1241      characteristics of the subwindow. This also clears the dirty
1242      flags as a side effect. */
1243   redisplay_subwindow (image_instance);
1244
1245   /* This makes the glyph area fit into the display area. */
1246   if (!redisplay_normalize_glyph_area (db, dga))
1247     return;
1248
1249   XSETWINDOW (window, w);
1250
1251   /* Clear the area the subwindow is going into. */
1252   redisplay_clear_clipped_region (window, findex,
1253                                   db, dga, 0, image_instance);
1254
1255   /* This shrinks the display box to exactly enclose the glyph
1256      area. */
1257   redisplay_normalize_display_box (db, dga);
1258
1259   /* if we can't view the whole window we can't view any of it. We
1260      have to be careful here since we may be being asked to display
1261      part of a subwindow, the rest of which is on-screen as well. We
1262      need to allow this case and map the entire subwindow. We also
1263      need to be careful since the subwindow could be outside the
1264      window in the gutter or modeline - we also need to allow these
1265      cases.*/
1266   sdga.xoffset = -dga->xoffset;
1267   sdga.yoffset = -dga->yoffset;
1268   sdga.height = IMAGE_INSTANCE_HEIGHT (p);
1269   sdga.width = IMAGE_INSTANCE_WIDTH (p);
1270
1271   if (redisplay_display_boxes_in_window_p (w, db, &sdga) == 0
1272       ||
1273       /* We only want to do full subwindow display for windows that
1274          are completely in the gutter, otherwise we must clip to be
1275          safe. */
1276       display_boxes_in_gutter_p (XFRAME (w->frame), db, &sdga) <= 0)
1277     {
1278       map_subwindow (image_instance, db->xpos, db->ypos, dga);
1279     }
1280   else
1281     {
1282       sdga.xoffset = sdga.yoffset = 0;
1283       map_subwindow (image_instance, db->xpos - dga->xoffset,
1284                      db->ypos - dga->yoffset, &sdga);
1285     }
1286 }
1287
1288 /****************************************************************************
1289  redisplay_output_layout
1290
1291  Output a widget hierarchy. This can safely call itself recursively.
1292
1293  The complexity of outputting layouts is deciding whether to do it or
1294  not. Consider a layout enclosing some text, the text changes and is
1295  marked as dirty, but the enclosing layout has not been marked as
1296  dirty so no updates occur and the text will potentially be truncated.
1297  Alternatively we hold a back pointer in the image instance to the
1298  parent and mark the parent as dirty. But the layout code assumes that
1299  if the layout is dirty then the whole layout should be redisplayed,
1300  so we then get lots of flashing even though only the text has changed
1301  size. Of course if the text shrinks in size then we do actually need
1302  to redisplay the layout to repaint the exposed area. So what happens
1303  if we make a non-structural change like changing color? Either we
1304  redisplay everything, or we redisplay nothing. These are exactly the
1305  issues lwlib has to grapple with. We really need to know what has
1306  actually changed and make a layout decision based on that. We also
1307  really need to know what has changed so that we can only make the
1308  necessary changes in update_subwindow.  This has all now been
1309  implemented, Viva la revolution!
1310  ****************************************************************************/
1311 void
1312 redisplay_output_layout (Lisp_Object domain,
1313                          Lisp_Object image_instance,
1314                          struct display_box* db, struct display_glyph_area* dga,
1315                          face_index findex, int cursor_start, int cursor_width,
1316                          int cursor_height)
1317 {
1318   Lisp_Image_Instance *p = XIMAGE_INSTANCE (image_instance);
1319   Lisp_Object rest, window = DOMAIN_WINDOW (domain);
1320   Charc_dynarr *buf;
1321   struct window *w = XWINDOW (window);
1322   struct device *d = DOMAIN_XDEVICE (domain);
1323   int layout_height, layout_width;
1324
1325   layout_height = glyph_height (image_instance, domain);
1326   layout_width = glyph_width (image_instance, domain);
1327
1328   dga->height = layout_height;
1329   dga->width = layout_width;
1330 #ifdef DEBUG_WIDGET_OUTPUT
1331   printf ("outputing layout glyph %p\n", p);
1332 #endif
1333   /* This makes the glyph area fit into the display area. */
1334   if (!redisplay_normalize_glyph_area (db, dga))
1335     return;
1336
1337   buf = Dynarr_new (Charc);
1338
1339   /* Highly dodgy optimization. We want to only output the whole
1340      layout if we really have to. */
1341   if (!IMAGE_INSTANCE_OPTIMIZE_OUTPUT (p)
1342       || IMAGE_INSTANCE_LAYOUT_CHANGED (p)
1343       || IMAGE_INSTANCE_WIDGET_FACE_CHANGED (p)
1344       || IMAGE_INSTANCE_SIZE_CHANGED (p)
1345       || IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (p))
1346     {
1347       /* First clear the area we are drawing into. This is the easiest
1348          thing to do since we have many gaps that we have to make sure are
1349          filled in. */
1350       redisplay_clear_clipped_region (window, findex, db, dga, 1, Qnil);
1351
1352       /* Output a border if required */
1353       if (!NILP (IMAGE_INSTANCE_LAYOUT_BORDER (p)))
1354         {
1355           int edges = 0;
1356           enum edge_style style;
1357           int ypos = db->ypos;
1358           int xpos = db->xpos;
1359           int height = dga->height;
1360           int width = dga->width;
1361
1362           /* The bevel_area routines always draw in from the specified
1363              area so there is no need to adjust the displayed area to
1364              make sure that the lines are visible. */
1365           if (dga->xoffset >= 0) 
1366             edges |= EDGE_LEFT;
1367           if (dga->width - dga->xoffset == layout_width) 
1368             edges |= EDGE_RIGHT;
1369           if (dga->yoffset >= 0) 
1370             edges |= EDGE_TOP;
1371           if (dga->height - dga->yoffset == layout_height)
1372             edges |= EDGE_BOTTOM;
1373           
1374           if (EQ (IMAGE_INSTANCE_LAYOUT_BORDER (p), Qetched_in))
1375             style = EDGE_ETCHED_IN;
1376           else if (EQ (IMAGE_INSTANCE_LAYOUT_BORDER (p), Qetched_out))
1377             style = EDGE_ETCHED_OUT;
1378           else if (EQ (IMAGE_INSTANCE_LAYOUT_BORDER (p), Qbevel_in))
1379             style = EDGE_BEVEL_IN;
1380           else if (INTP (IMAGE_INSTANCE_LAYOUT_BORDER (p)))
1381             {
1382               style = EDGE_ETCHED_IN;
1383               if (edges & EDGE_TOP)
1384                 {
1385                   ypos += XINT (IMAGE_INSTANCE_LAYOUT_BORDER (p));
1386                   height -= XINT (IMAGE_INSTANCE_LAYOUT_BORDER (p));
1387                 }
1388             }
1389           else
1390             style = EDGE_BEVEL_OUT;
1391
1392           MAYBE_DEVMETH (d, bevel_area,
1393                          (w, findex, xpos, ypos, width, height,
1394                           DEFAULT_WIDGET_SHADOW_WIDTH, edges, style));
1395         }
1396     }
1397
1398   /* This shrinks the display box to exactly enclose the glyph
1399      area. */
1400   redisplay_normalize_display_box (db, dga);
1401
1402   /* Flip through the widgets in the layout displaying as necessary */
1403   LIST_LOOP (rest, IMAGE_INSTANCE_LAYOUT_CHILDREN (p))
1404     {
1405       Lisp_Object child = glyph_image_instance (XCAR (rest), image_instance,
1406                                                 ERROR_ME_NOT, 1);
1407
1408       struct display_box cdb;
1409       /* For losing HP-UX */
1410       cdb.xpos = db->xpos;
1411       cdb.ypos = db->ypos;
1412       cdb.width = db->width;
1413       cdb.height = db->height;
1414
1415       /* First determine if the image is visible at all */
1416       if (IMAGE_INSTANCEP (child))
1417         {
1418           Lisp_Image_Instance* childii = XIMAGE_INSTANCE (child);
1419
1420           /* The enclosing layout offsets are +ve at this point */
1421           struct display_glyph_area cdga;
1422           cdga.xoffset  = IMAGE_INSTANCE_XOFFSET (childii) - dga->xoffset;
1423           cdga.yoffset = IMAGE_INSTANCE_YOFFSET (childii) - dga->yoffset;
1424           cdga.width = glyph_width (child, image_instance);
1425           cdga.height = glyph_height (child, image_instance);
1426
1427           IMAGE_INSTANCE_OPTIMIZE_OUTPUT (childii) =
1428             IMAGE_INSTANCE_OPTIMIZE_OUTPUT (p);
1429
1430           /* Although normalization is done by the output routines
1431              we have to do it here so that they don't try and
1432              clear all of db. This is true below also. */
1433           if (redisplay_normalize_glyph_area (&cdb, &cdga))
1434             {
1435               redisplay_normalize_display_box (&cdb, &cdga);
1436               /* Since the display boxes will now be totally in the
1437                  window if they are visible at all we can now check this easily. */
1438               if (cdb.xpos < db->xpos || cdb.ypos < db->ypos
1439                   || cdb.xpos + cdb.width > db->xpos + db->width
1440                   || cdb.ypos + cdb.height > db->ypos + db->height)
1441                 continue;
1442               /* We have to invert the offset here as normalization
1443                  will have made them positive which the output
1444                  routines will treat as a truly +ve offset. */
1445               cdga.xoffset = -cdga.xoffset;
1446               cdga.yoffset = -cdga.yoffset;
1447
1448               switch (IMAGE_INSTANCE_TYPE (childii))
1449                 {
1450                 case IMAGE_TEXT:
1451                   {
1452                     /* #### This is well hacked and could use some
1453                        generalisation.*/
1454                     if (redisplay_normalize_glyph_area (&cdb, &cdga)
1455                         &&
1456                         (!IMAGE_INSTANCE_OPTIMIZE_OUTPUT (childii) ||
1457                          IMAGE_INSTANCE_DIRTYP (childii)))
1458                       {
1459                         struct display_line dl; /* this is fake */
1460                         Lisp_Object string =
1461                           IMAGE_INSTANCE_TEXT_STRING (childii);
1462                         Charset_ID charsets[NUM_LEADING_BYTES];
1463                         struct face_cachel *cachel = WINDOW_FACE_CACHEL (w, findex);
1464
1465                         find_charsets_in_bufbyte_string (charsets,
1466                                                          XSTRING_DATA (string),
1467                                                          XSTRING_LENGTH (string));
1468                         ensure_face_cachel_complete (cachel, window, charsets);
1469
1470                         convert_bufbyte_string_into_charc_dynarr
1471                           (XSTRING_DATA (string), XSTRING_LENGTH (string),
1472                            buf);
1473
1474                         redisplay_normalize_display_box (&cdb, &cdga);
1475                         /* Offsets are now +ve again so be careful
1476                            when fixing up the display line. */
1477                         xzero (dl);
1478                         /* Munge boxes into display lines. */
1479                         dl.ypos = (cdb.ypos - cdga.yoffset)
1480                           + glyph_ascent (child, image_instance);
1481                         dl.ascent = glyph_ascent (child, image_instance);
1482                         dl.descent = glyph_descent (child, image_instance);
1483                         dl.top_clip = cdga.yoffset;
1484                         dl.clip = (dl.ypos + dl.descent) - (cdb.ypos + cdb.height);
1485                         /* output_string doesn't understand offsets in
1486                            the same way as other routines - we have to
1487                            add the offset to the width so that we
1488                            output the full string. */
1489                         MAYBE_DEVMETH (d, output_string, (w, &dl, buf, cdb.xpos,
1490                                                           cdga.xoffset, cdb.xpos,
1491                                                           cdga.width + cdga.xoffset,
1492                                                           findex, 0, 0, 0, 0));
1493                         Dynarr_reset (buf);
1494                       }
1495                   }
1496                   break;
1497
1498                 case IMAGE_MONO_PIXMAP:
1499                 case IMAGE_COLOR_PIXMAP:
1500                   if (!IMAGE_INSTANCE_OPTIMIZE_OUTPUT (childii)
1501                       || IMAGE_INSTANCE_DIRTYP (childii))
1502                     redisplay_output_pixmap (w, child, &cdb, &cdga, findex,
1503                                              0, 0, 0, 0);
1504                   break;
1505
1506                 case IMAGE_WIDGET:
1507                   if (EQ (IMAGE_INSTANCE_WIDGET_TYPE (childii), Qlayout))
1508                     {
1509                       redisplay_output_layout (image_instance, child, &cdb, &cdga, findex,
1510                                                0, 0, 0);
1511                       break;
1512                     }
1513                 case IMAGE_SUBWINDOW:
1514                   if (!IMAGE_INSTANCE_OPTIMIZE_OUTPUT (childii) ||
1515                       IMAGE_INSTANCE_DIRTYP (childii))
1516                     redisplay_output_subwindow (w, child, &cdb, &cdga, findex,
1517                                                 0, 0, 0);
1518                   break;
1519
1520                 case IMAGE_NOTHING:
1521                   /* nothing is as nothing does */
1522                   break;
1523
1524                 case IMAGE_POINTER:
1525                 default:
1526                   ABORT ();
1527                 }
1528             }
1529           IMAGE_INSTANCE_OPTIMIZE_OUTPUT (childii) = 0;
1530         }
1531     }
1532
1533   /* Update any display properties. I'm not sure whether this actually
1534      does anything for layouts except clear the changed flags. */
1535   redisplay_subwindow (image_instance);
1536
1537   Dynarr_free (buf);
1538 }
1539
1540 /****************************************************************************
1541  redisplay_output_pixmap
1542
1543
1544  output a pixmap.
1545  ****************************************************************************/
1546 void
1547 redisplay_output_pixmap (struct window *w,
1548                          Lisp_Object image_instance,
1549                          struct display_box* db, struct display_glyph_area* dga,
1550                          face_index findex, int cursor_start, int cursor_width,
1551                          int cursor_height, int offset_bitmap)
1552 {
1553   struct frame *f = XFRAME (w->frame);
1554   struct device *d = XDEVICE (f->device);
1555   Lisp_Image_Instance *p = XIMAGE_INSTANCE (image_instance);
1556   Lisp_Object window;
1557   XSETWINDOW (window, w);
1558
1559   dga->height = IMAGE_INSTANCE_PIXMAP_HEIGHT (p);
1560   dga->width = IMAGE_INSTANCE_PIXMAP_WIDTH (p);
1561
1562 #ifdef DEBUG_REDISPLAY
1563   printf ("redisplay_output_pixmap(request) \
1564 [%dx%d@%d+%d] in [%dx%d@%d+%d]\n", 
1565           db->width, db->height, db->xpos, db->ypos,
1566           dga->width, dga->height, dga->xoffset, dga->yoffset);
1567 #endif
1568
1569   /* This makes the glyph area fit into the display area. */
1570   if (!redisplay_normalize_glyph_area (db, dga))
1571     return;
1572
1573 #ifdef DEBUG_REDISPLAY
1574   printf ("redisplay_output_pixmap(normalized) \
1575 [%dx%d@%d+%d] in [%dx%d@%d+%d]\n",
1576           db->width, db->height, db->xpos, db->ypos,
1577           dga->width, dga->height, dga->xoffset, dga->yoffset);
1578 #endif
1579
1580   /* Clear the area the pixmap is going into.  The pixmap itself will
1581      always take care of the full width.  We don't want to clear where
1582      it is going to go in order to avoid flicker.  So, all we have to
1583      take care of is any area above or below the pixmap. If the pixmap
1584      has a mask in which case we have to clear the whole damn thing
1585      since we can't yet clear just the area not included in the
1586      mask. */
1587   if (!offset_bitmap)
1588     {
1589       redisplay_clear_clipped_region (window, findex,
1590                                       db, dga,
1591                                       (IMAGE_INSTANCE_PIXMAP_MASK (p) != 0),
1592                                       Qnil);
1593
1594       /* This shrinks the display box to exactly enclose the glyph
1595          area. */
1596       redisplay_normalize_display_box (db, dga);
1597     }
1598   assert (db->xpos >= 0 && db->ypos >= 0);
1599
1600   MAYBE_DEVMETH (d, output_pixmap, (w, image_instance,
1601                                     db, dga,
1602                                     findex, cursor_start,
1603                                     cursor_width, cursor_height,
1604                                     offset_bitmap));
1605 }
1606
1607 /****************************************************************************
1608  redisplay_clear_region
1609
1610  Clear the area in the box defined by the given parameters using the
1611  given face. This has been generalised so that subwindows can be
1612  coped with effectively.
1613  ****************************************************************************/
1614 void
1615 redisplay_clear_region (Lisp_Object locale, face_index findex, int x, int y,
1616                         int width, int height)
1617 {
1618   struct window *w = NULL;
1619   struct frame *f = NULL;
1620   struct device *d;
1621   Lisp_Object background_pixmap = Qunbound;
1622   Lisp_Object fcolor = Qnil, bcolor = Qnil;
1623
1624   if (!width || !height)
1625      return;
1626
1627   if (WINDOWP (locale))
1628     {
1629       w = XWINDOW (locale);
1630       f = XFRAME (w->frame);
1631     }
1632   else if (FRAMEP (locale))
1633     {
1634       w = NULL;
1635       f = XFRAME (locale);
1636     }
1637   else
1638     ABORT ();
1639
1640   d = XDEVICE (f->device);
1641
1642   /* if we have subwindows in the region we have to unmap them */
1643   redisplay_unmap_subwindows_maybe (f, x, y, width, height);
1644
1645   /* #### This isn't quite right for when this function is called
1646      from the toolbar code. */
1647
1648   /* Don't use a backing pixmap in the border area */
1649   if (x >= FRAME_LEFT_BORDER_END (f)
1650       && x < FRAME_RIGHT_BORDER_START (f)
1651       && y >= FRAME_TOP_BORDER_END (f)
1652       && y < FRAME_BOTTOM_BORDER_START (f))
1653     {
1654       Lisp_Object temp;
1655
1656       if (w)
1657         {
1658           temp = WINDOW_FACE_CACHEL_BACKGROUND_PIXMAP (w, findex);
1659
1660           if (IMAGE_INSTANCEP (temp)
1661               && IMAGE_INSTANCE_PIXMAP_TYPE_P (XIMAGE_INSTANCE (temp)))
1662             {
1663               /* #### maybe we could implement such that a string
1664                  can be a background pixmap? */
1665               background_pixmap = temp;
1666             }
1667         }
1668       else
1669         {
1670           temp = FACE_BACKGROUND_PIXMAP (Vdefault_face, locale);
1671
1672           if (IMAGE_INSTANCEP (temp)
1673               && IMAGE_INSTANCE_PIXMAP_TYPE_P (XIMAGE_INSTANCE (temp)))
1674             {
1675               background_pixmap = temp;
1676             }
1677         }
1678     }
1679
1680   if (!UNBOUNDP (background_pixmap) &&
1681       XIMAGE_INSTANCE_PIXMAP_DEPTH (background_pixmap) == 0)
1682     {
1683       if (w)
1684         {
1685           fcolor = WINDOW_FACE_CACHEL_FOREGROUND (w, findex);
1686           bcolor = WINDOW_FACE_CACHEL_BACKGROUND (w, findex);
1687         }
1688       else
1689         {
1690           fcolor = FACE_FOREGROUND (Vdefault_face, locale);
1691           bcolor = FACE_BACKGROUND (Vdefault_face, locale);
1692         }
1693     }
1694   else
1695     {
1696       fcolor = (w ?
1697                 WINDOW_FACE_CACHEL_BACKGROUND (w, findex) :
1698                 FACE_BACKGROUND (Vdefault_face, locale));
1699
1700     }
1701
1702   if (UNBOUNDP (background_pixmap))
1703     background_pixmap = Qnil;
1704
1705   DEVMETH (d, clear_region,
1706            (locale, d, f, findex, x, y, width, height, fcolor, bcolor, background_pixmap));
1707 }
1708
1709 /****************************************************************************
1710  redisplay_clear_clipped_region
1711
1712  Clear the area in the dest display_box not covered by the src
1713  display_glyph_area using the given face. This is a common occurrence
1714  for images shorter than the display line. Clipping can be played
1715  around with by altering these. glyphsrc should be normalized.
1716  ****************************************************************************/
1717 static void
1718 redisplay_clear_clipped_region (Lisp_Object window, face_index findex,
1719         struct display_box* dest, struct display_glyph_area* glyphsrc,
1720         int fullheight_p, Lisp_Object ignored_subwindow)
1721 {
1722   /* assume dest->xpos >= 0 */
1723   int clear_x;
1724   struct frame* f = XFRAME (XWINDOW (window)->frame);
1725
1726   if (glyphsrc->xoffset > 0)
1727     {
1728       clear_x = dest->xpos + glyphsrc->xoffset;
1729     }
1730   else
1731     {
1732       clear_x = dest->xpos;
1733     }
1734
1735   /* If we need the whole height cleared then just do it. */
1736   if (fullheight_p)
1737     {
1738       redisplay_clear_region (window, findex, clear_x, dest->ypos,
1739                               glyphsrc->width, dest->height);
1740     }
1741   else
1742     {
1743       int yoffset = (glyphsrc->yoffset > 0 ? glyphsrc->yoffset : 0);
1744
1745       /* We need to make sure that subwindows are unmapped from the
1746          whole area. */
1747       redisplay_unmap_subwindows_except_us (f, clear_x, dest->ypos,
1748                                             glyphsrc->width, dest->height,
1749                                             ignored_subwindow);
1750       /* first the top box */
1751       if (yoffset > 0)
1752         {
1753           redisplay_clear_region (window, findex, clear_x, dest->ypos,
1754                                   glyphsrc->width, yoffset);
1755
1756         }
1757       /* Then the bottom box */
1758       if (yoffset + glyphsrc->height < dest->height)
1759         {
1760           redisplay_clear_region (window, findex, clear_x,
1761                                   dest->ypos + yoffset + glyphsrc->height,
1762                                   glyphsrc->width,
1763                                   dest->height - (yoffset + glyphsrc->height));
1764
1765         }
1766     }
1767 }
1768
1769 /*****************************************************************************
1770  redisplay_normalize_glyph_area
1771  redisplay_normalize_display_box
1772
1773  Calculate the visible box for displaying glyphsrc in dest.
1774
1775  display_box and display_glyph_area are used to represent an area to
1776  displayed and where to display it. Using these two structures all
1777  combinations of clipping and position can be accommodated.
1778
1779  dest - display_box
1780
1781         xpos - absolute horizontal position of area.
1782
1783         ypos - absolute vertical position of area.
1784
1785   glyphsrc - display_glyph_area
1786
1787         xoffset - horizontal offset of the glyph, +ve means display
1788         the glyph with the x position offset by xoffset, -ve means
1789         display starting xoffset into the glyph.
1790
1791         yoffset - vertical offset of the glyph, +ve means display the
1792         glyph with y position offset by yoffset, -ve means display
1793         starting xoffset into the glyph.
1794
1795  ****************************************************************************/
1796 int
1797 redisplay_normalize_glyph_area (struct display_box* dest,
1798                                 struct display_glyph_area* glyphsrc)
1799 {
1800   if (dest->xpos + glyphsrc->xoffset > dest->xpos + dest->width
1801       ||
1802       dest->ypos + glyphsrc->yoffset > dest->ypos + dest->height
1803       ||
1804       -glyphsrc->xoffset >= glyphsrc->width
1805       ||
1806       -glyphsrc->yoffset >= glyphsrc->height
1807       ||
1808       /* #### Not sure why this wasn't coped with before but normalizing
1809          to zero width or height is definitely wrong. */
1810       (dest->xpos + glyphsrc->xoffset + glyphsrc->width > dest->xpos + dest->width
1811        &&
1812        dest->width - glyphsrc->xoffset <= 0)
1813       ||
1814       (dest->ypos + glyphsrc->yoffset + glyphsrc->height > dest->ypos + dest->height
1815        &&
1816        dest->height - glyphsrc->yoffset <= 0))
1817     {
1818       /* It's all clipped out */
1819       return 0;
1820     }
1821
1822   /* Horizontal offsets. This works because xoffset can be -ve as well
1823      as +ve.  When we enter this function the glyphsrc width and
1824      height are set to the actual glyph width and height irrespective
1825      of how much can be displayed. We are trying to clip both the
1826      offset into the image and the rightmost bounding box. Its
1827      possible for the glyph width to be much larger than the area we
1828      are displaying into (e.g. a large glyph in a small frame). */
1829   if (dest->xpos + glyphsrc->xoffset + glyphsrc->width > dest->xpos + dest->width)
1830     {
1831       /* glyphsrc offset is +ve we are trying to display offset from the
1832          origin (the bounding box contains some space and then the
1833          glyph). At most the width we want to display is dest->width -
1834          glyphsrc->xoffset. */
1835       if (glyphsrc->xoffset > 0)
1836         glyphsrc->width = dest->width - glyphsrc->xoffset;
1837       /* glyphsrc offset is -ve we are trying to display hard up
1838          against the dest corner inset into the glyphsrc by
1839          xoffset.*/
1840       else if (glyphsrc->xoffset < 0) 
1841         {
1842           glyphsrc->width += glyphsrc->xoffset;
1843           glyphsrc->width = min (glyphsrc->width, dest->width);
1844         }
1845       else
1846         glyphsrc->width = dest->width;
1847     }
1848
1849   else if (glyphsrc->xoffset < 0) 
1850     glyphsrc->width += glyphsrc->xoffset;
1851
1852   /* Vertical offsets. This works because yoffset can be -ve as well as +ve */
1853   if (dest->ypos + glyphsrc->yoffset + glyphsrc->height > dest->ypos + dest->height)
1854     {
1855       if ((glyphsrc->yoffset > 0) && (dest->height > glyphsrc->yoffset))
1856         glyphsrc->height = dest->height - glyphsrc->yoffset;
1857       else if (glyphsrc->yoffset < 0) 
1858         {
1859           glyphsrc->height += glyphsrc->yoffset;
1860           glyphsrc->height = min (glyphsrc->height, dest->height);
1861         }
1862       else
1863         glyphsrc->height = dest->height;
1864     }
1865
1866   else if (glyphsrc->yoffset < 0)
1867     glyphsrc->height += glyphsrc->yoffset;
1868
1869   return 1;
1870 }
1871
1872 static void
1873 redisplay_normalize_display_box (struct display_box* dest,
1874                                  struct display_glyph_area* glyphsrc)
1875 {
1876   /* Adjust the destination area. At the end of this the destination
1877    area will exactly enclose the glyph area. The only remaining
1878    adjustment will be offsets into the glyph area. */
1879
1880   /* Horizontal adjustment. */
1881   if (glyphsrc->xoffset > 0)
1882     {
1883       dest->xpos += glyphsrc->xoffset;
1884       dest->width -= glyphsrc->xoffset;
1885       glyphsrc->xoffset = 0;
1886     }
1887   else
1888     glyphsrc->xoffset = -glyphsrc->xoffset;
1889
1890   if (glyphsrc->width < dest->width)
1891     dest->width = glyphsrc->width;
1892
1893   /* Vertical adjustment. */
1894   if (glyphsrc->yoffset > 0)
1895     {
1896       dest->ypos += glyphsrc->yoffset;
1897       dest->height -= glyphsrc->yoffset;
1898       glyphsrc->yoffset = 0;
1899     }
1900   else
1901     glyphsrc->yoffset = -glyphsrc->yoffset;
1902
1903   if (glyphsrc->height < dest->height)
1904     dest->height = glyphsrc->height;
1905 }
1906
1907 /*****************************************************************************
1908  redisplay_display_boxes_in_window_p
1909
1910  Determine whether the required display_glyph_area is completely inside
1911  the window. -1 means the display_box is not in the window. 1 means the
1912  display_box and the display_glyph_area are in the window. 0 means
1913  the display_box is in the window but the display_glyph_area is not.
1914  ****************************************************************************/
1915 static int
1916 redisplay_display_boxes_in_window_p (struct window* w,
1917                                      struct display_box* db,
1918                                      struct display_glyph_area* dga)
1919 {
1920   int left = WINDOW_TEXT_LEFT (w);
1921   int right = WINDOW_TEXT_RIGHT (w);
1922   int top = WINDOW_TEXT_TOP (w);
1923   int bottom = WINDOW_TEXT_BOTTOM (w);
1924
1925   if (db->xpos < left || db->ypos < top
1926       || db->xpos + db->width > right
1927       || db->ypos + db->height > bottom)
1928       /* We are not displaying in a window at all */
1929       return -1;
1930
1931   if (db->xpos + dga->xoffset >= left
1932       &&
1933       db->ypos + dga->yoffset >= top
1934       &&
1935       db->xpos + dga->xoffset + dga->width <= right
1936       &&
1937       db->ypos + dga->yoffset + dga->height <= bottom)
1938     return 1;
1939
1940   return 0;
1941 }
1942
1943 /*****************************************************************************
1944  redisplay_calculate_display_boxes
1945
1946  Convert from rune/display_line co-ordinates to display_box
1947  co-ordinates.
1948  ****************************************************************************/
1949 int
1950 redisplay_calculate_display_boxes (struct display_line *dl, int xpos,
1951                                    int xoffset, int yoffset, int start_pixpos,
1952                                    int width, struct display_box* dest,
1953                                    struct display_glyph_area* src)
1954 {
1955   dest->xpos = xpos;
1956   dest->ypos = DISPLAY_LINE_YPOS (dl);
1957   dest->width = width;
1958   dest->height = DISPLAY_LINE_HEIGHT (dl);
1959
1960   src->xoffset = -xoffset;
1961   src->width = 0;
1962   src->height = 0;
1963
1964   src->yoffset = -dl->top_clip + yoffset;
1965
1966   if (start_pixpos >=0 && start_pixpos > xpos)
1967     {
1968       /* Oops, we're asking for a start outside of the displayable
1969          area. */
1970       if (start_pixpos > xpos + width)
1971         return 0;
1972       dest->xpos = start_pixpos;
1973       dest->width -= (start_pixpos - xpos);
1974       /* Offsets are -ve when we want to clip pixels off the displayed
1975          glyph. */
1976       src->xoffset -= (start_pixpos - xpos);
1977     }
1978
1979   return 1;
1980 }
1981
1982 /*****************************************************************************
1983  redisplay_clear_top_of_window
1984
1985  If window is topmost, clear the internal border above it.
1986  ****************************************************************************/
1987 void
1988 redisplay_clear_top_of_window (struct window *w)
1989 {
1990   Lisp_Object window;
1991   XSETWINDOW (window, w);
1992
1993   if (!NILP (Fwindow_highest_p (window)))
1994     {
1995       struct frame *f = XFRAME (w->frame);
1996       int x, y, width, height;
1997
1998       x = w->pixel_left;
1999       width = w->pixel_width;
2000
2001       if (window_is_leftmost (w))
2002         {
2003           x -= FRAME_BORDER_WIDTH (f);
2004           width += FRAME_BORDER_WIDTH (f);
2005         }
2006       if (window_is_rightmost (w))
2007         width += FRAME_BORDER_WIDTH (f);
2008
2009       y = FRAME_TOP_BORDER_START (f) - 1;
2010       height = FRAME_BORDER_HEIGHT (f) + 1;
2011
2012       redisplay_clear_region (window, DEFAULT_INDEX, x, y, width, height);
2013     }
2014 }
2015
2016 /*****************************************************************************
2017  redisplay_clear_to_window_end
2018
2019  Clear the area between ypos1 and ypos2.  Each margin area and the
2020  text area is handled separately since they may each have their own
2021  background color.
2022  ****************************************************************************/
2023 void
2024 redisplay_clear_to_window_end (struct window *w, int ypos1, int ypos2)
2025 {
2026   struct frame *f = XFRAME (w->frame);
2027   struct device *d = XDEVICE (f->device);
2028
2029   if (HAS_DEVMETH_P (d, clear_to_window_end))
2030     DEVMETH (d, clear_to_window_end, (w, ypos1, ypos2));
2031   else
2032     {
2033       int height = ypos2 - ypos1;
2034
2035       if (height)
2036         {
2037           Lisp_Object window;
2038           int bflag = 0 ; /* (window_needs_vertical_divider (w) ? 0 : 1);*/
2039           layout_bounds bounds;
2040
2041           bounds = calculate_display_line_boundaries (w, bflag);
2042           XSETWINDOW (window, w);
2043
2044           if (window_is_leftmost (w))
2045             redisplay_clear_region (window, DEFAULT_INDEX, FRAME_LEFT_BORDER_START (f),
2046                                     ypos1, FRAME_BORDER_WIDTH (f), height);
2047
2048           if (bounds.left_in - bounds.left_out > 0)
2049             redisplay_clear_region (window,
2050                                     get_builtin_face_cache_index (w, Vleft_margin_face),
2051                                     bounds.left_out, ypos1,
2052                                     bounds.left_in - bounds.left_out, height);
2053
2054           if (bounds.right_in - bounds.left_in > 0)
2055             redisplay_clear_region (window,
2056                                     DEFAULT_INDEX,
2057                                     bounds.left_in, ypos1,
2058                                     bounds.right_in - bounds.left_in, height);
2059
2060           if (bounds.right_out - bounds.right_in > 0)
2061             redisplay_clear_region (window,
2062                                     get_builtin_face_cache_index (w, Vright_margin_face),
2063                                     bounds.right_in, ypos1,
2064                                     bounds.right_out - bounds.right_in, height);
2065
2066           if (window_is_rightmost (w))
2067             redisplay_clear_region (window, DEFAULT_INDEX, FRAME_RIGHT_BORDER_START (f),
2068                                     ypos1, FRAME_BORDER_WIDTH (f), height);
2069         }
2070     }
2071 }
2072
2073 /*****************************************************************************
2074  redisplay_clear_bottom_of_window
2075
2076  Clear window from right below the last display line to right above
2077  the modeline.  The calling function can limit the area actually
2078  erased by setting min_start and/or max_end to positive values.
2079  ****************************************************************************/
2080 void
2081 redisplay_clear_bottom_of_window (struct window *w, display_line_dynarr *ddla,
2082                                   int min_start, int max_end)
2083 {
2084   struct frame *f = XFRAME (w->frame);
2085   int ypos1, ypos2;
2086   int ddla_len = Dynarr_length (ddla);
2087
2088   ypos2 = WINDOW_TEXT_BOTTOM (w);
2089 #ifdef HAVE_SCROLLBARS
2090   /* This adjustment is to catch the intersection of any scrollbars. */
2091   if (f->windows_structure_changed && NILP (w->scrollbar_on_top_p))
2092     ypos2 += window_scrollbar_height (w);
2093 #endif
2094
2095   if (ddla_len)
2096     {
2097       if (ddla_len == 1 && Dynarr_atp (ddla, 0)->modeline)
2098         {
2099           ypos1 = WINDOW_TEXT_TOP (w);
2100 #ifdef HAVE_SCROLLBARS
2101           /* This adjustment is to catch the intersection of any scrollbars. */
2102           if (f->windows_structure_changed && !NILP (w->scrollbar_on_top_p))
2103             ypos1 -= window_scrollbar_height (w);
2104 #endif
2105         }
2106       else
2107         {
2108           struct display_line *dl = Dynarr_atp (ddla, ddla_len - 1);
2109           ypos1 = dl->ypos + dl->descent - dl->clip;
2110         }
2111     }
2112   else
2113     ypos1 = WINDOW_TEXT_TOP (w);
2114
2115   /* #### See if this can be made conditional on the frame
2116      changing size. */
2117   if (MINI_WINDOW_P (w))
2118     ypos2 += FRAME_BORDER_HEIGHT (f);
2119
2120   if (min_start >= 0 && ypos1 < min_start)
2121     ypos1 = min_start;
2122   if (max_end >= 0 && ypos2 > max_end)
2123     ypos2 = max_end;
2124
2125   if (ypos2 <= ypos1)
2126     return;
2127
2128   redisplay_clear_to_window_end (w, ypos1, ypos2);
2129 }
2130
2131 /*****************************************************************************
2132  redisplay_update_line
2133
2134  This is used during incremental updates to update a single line and
2135  correct the offsets on all lines below it.  At the moment
2136  update_values is false if we are only updating the modeline.
2137  ****************************************************************************/
2138 void
2139 redisplay_update_line (struct window *w, int first_line, int last_line,
2140                        int update_values)
2141 {
2142   struct frame *f = XFRAME (w->frame);
2143   struct device *d = XDEVICE (f->device);
2144
2145   display_line_dynarr *cdla = window_display_lines (w, CURRENT_DISP);
2146   display_line_dynarr *ddla = window_display_lines (w, DESIRED_DISP);
2147
2148   MAYBE_DEVMETH (d, window_output_begin, (w));
2149
2150   while (first_line <= last_line)
2151     {
2152       Charcount old_len = (Dynarr_atp (cdla, first_line)->end_bufpos -
2153                            Dynarr_atp (cdla, first_line)->bufpos);
2154       Charcount new_len = (Dynarr_atp (ddla, first_line)->end_bufpos -
2155                            Dynarr_atp (ddla, first_line)->bufpos);
2156
2157       assert (Dynarr_length (cdla) == Dynarr_length (ddla));
2158
2159       /* Output the changes. */
2160       output_display_line (w, cdla, ddla, first_line, -1, -1);
2161
2162       /* Update the offsets. */
2163       if (update_values)
2164         {
2165           int cur_line = first_line + 1;
2166           while (cur_line < Dynarr_length (cdla))
2167             {
2168               Dynarr_atp (cdla, cur_line)->offset += (new_len - old_len);
2169               Dynarr_atp (ddla, cur_line)->offset += (new_len - old_len);
2170               cur_line++;
2171             }
2172         }
2173
2174       /* Update the window_end_pos and other settings. */
2175       if (update_values)
2176         {
2177           w->window_end_pos[CURRENT_DISP] -= (new_len - old_len);
2178
2179           if (Dynarr_atp (ddla, first_line)->cursor_elt != -1)
2180             {
2181               w->last_point_x[CURRENT_DISP] = w->last_point_x[DESIRED_DISP];
2182               w->last_point_y[CURRENT_DISP] = w->last_point_y[DESIRED_DISP];
2183             }
2184         }
2185
2186       first_line++;
2187     }
2188
2189   /* Update the window max line length.  We have to scan the entire
2190      set of display lines otherwise we might not detect if the max is
2191      supposed to shrink. */
2192   if (update_values)
2193     {
2194       int line = 0;
2195
2196       w->max_line_len = 0;
2197       while (line < Dynarr_length (ddla))
2198         {
2199           struct display_line *dl = Dynarr_atp (ddla, line);
2200
2201           if (!dl->modeline)
2202             w->max_line_len = max (dl->num_chars, w->max_line_len);
2203
2204           line++;
2205         }
2206     }
2207
2208   w->last_modified[CURRENT_DISP] = w->last_modified[DESIRED_DISP];
2209   w->last_facechange[CURRENT_DISP] = w->last_facechange[DESIRED_DISP];
2210   Fset_marker (w->last_point[CURRENT_DISP],
2211                Fmarker_position (w->last_point[DESIRED_DISP]), w->buffer);
2212   Fset_marker (w->last_start[CURRENT_DISP],
2213                Fmarker_position (w->last_start[DESIRED_DISP]), w->buffer);
2214
2215   /* We don't bother updating the vertical scrollbars here.  This
2216      gives us a performance increase while having minimal loss of
2217      quality to the scrollbar slider size and position since when this
2218      function is called we know that the changes to the buffer were
2219      very localized.  We have to update the horizontal scrollbars,
2220      though, because this routine could cause a change which has a
2221      larger impact on their sizing. */
2222   /* #### See if we can get away with only calling this if
2223      max_line_len is greater than the window_char_width. */
2224   /* #### BILL!!! Should we do this for GTK as well? */
2225 #if defined(HAVE_SCROLLBARS) && defined(HAVE_X_WINDOWS)
2226   {
2227     extern int stupid_vertical_scrollbar_drag_hack;
2228
2229     update_window_scrollbars (w, NULL, 1, stupid_vertical_scrollbar_drag_hack);
2230     stupid_vertical_scrollbar_drag_hack = 1;
2231   }
2232 #endif
2233
2234   redisplay_redraw_cursor (f, 0);
2235   MAYBE_DEVMETH (d, window_output_end, (w));
2236 }
2237
2238 /*****************************************************************************
2239  redisplay_output_window
2240
2241  For the given window W, ensure that the current display lines are
2242  equal to the desired display lines, outputing changes as necessary.
2243
2244  #### Fuck me.  This just isn't going to cut it for tty's.  The output
2245  decisions for them must be based on the contents of the entire frame
2246  because that is how the available output capabilities think.  The
2247  solution is relatively simple.  Create redisplay_output_frame.  This
2248  will basically merge all of the separate window display structs into
2249  a single one for the frame.  This combination structure will be able
2250  to be passed to the same output_display_line which works for windows
2251  on X frames and the right things will happen.  It just takes time to
2252  do.
2253  ****************************************************************************/
2254 void
2255 redisplay_output_window (struct window *w)
2256 {
2257   struct frame *f = XFRAME (w->frame);
2258   struct device *d = XDEVICE (f->device);
2259
2260   display_line_dynarr *cdla = window_display_lines (w, CURRENT_DISP);
2261   display_line_dynarr *ddla = window_display_lines (w, DESIRED_DISP);
2262
2263   int cdla_len = Dynarr_length (cdla);
2264   int ddla_len = Dynarr_length (ddla);
2265
2266   int line;
2267   int need_to_clear_bottom = 0;
2268   int need_to_clear_start = -1;
2269   int need_to_clear_end = -1;
2270
2271   /* Backgrounds may have changed or windows may have gone away
2272      leaving dividers lying around. */
2273   if (f->faces_changed
2274       || f->windows_structure_changed
2275       || w->shadow_thickness_changed)
2276     need_to_clear_bottom = 1;
2277
2278   /* The first thing we do is determine if we are going to need to
2279      clear the bottom of the window.  We only need to do this if the
2280      bottom of the current display lines is below the bottom of the
2281      desired display lines.  Note that the number of lines is
2282      irrelevant.  Only the position matters.  We also clear to the
2283      bottom of the window if the modeline has shifted position. */
2284   /* #### We can't blindly not clear the bottom if f->clear is true
2285      since there might be a window-local background.  However, for
2286      those cases where there isn't, clearing the end of the window in
2287      this case sucks. */
2288   if (!need_to_clear_bottom)
2289     {
2290       struct display_line *cdl, *ddl;
2291
2292       /* If the modeline has changed position or size, clear the bottom
2293          of the window. */
2294       if (!need_to_clear_bottom)
2295         {
2296           cdl = ddl = 0;
2297
2298           if (cdla_len)
2299             cdl = Dynarr_atp (cdla, 0);
2300           if (ddla_len)
2301             ddl = Dynarr_atp (ddla, 0);
2302
2303           if (!cdl || !ddl)
2304             need_to_clear_bottom = 1;
2305           else if ((!cdl->modeline && ddl->modeline)
2306                    || (cdl->modeline && !ddl->modeline))
2307             need_to_clear_bottom = 1;
2308           else if (cdl->ypos != ddl->ypos ||
2309                    cdl->ascent != ddl->ascent ||
2310                    cdl->descent != ddl->descent ||
2311                    cdl->clip != ddl->clip)
2312             need_to_clear_bottom = 1;
2313
2314           /* #### This kludge is to make sure the modeline shadows get
2315              redrawn if the modeline position shifts. */
2316           if (need_to_clear_bottom)
2317             w->shadow_thickness_changed = 1;
2318         }
2319
2320       if (!need_to_clear_bottom)
2321         {
2322           cdl = ddl = 0;
2323
2324           if (cdla_len)
2325             cdl = Dynarr_atp (cdla, cdla_len - 1);
2326           if (ddla_len)
2327             ddl = Dynarr_atp (ddla, ddla_len - 1);
2328
2329           if (!cdl || !ddl)
2330             need_to_clear_bottom = 1;
2331           else
2332             {
2333               int cdl_bottom, ddl_bottom;
2334
2335               cdl_bottom = cdl->ypos + cdl->descent;
2336               ddl_bottom = ddl->ypos + ddl->descent;
2337
2338               if (cdl_bottom > ddl_bottom)
2339                 {
2340                   need_to_clear_bottom = 1;
2341                   need_to_clear_start = ddl_bottom;
2342                   need_to_clear_end = cdl_bottom;
2343                 }
2344             }
2345         }
2346     }
2347
2348   /* Perform any output initialization. */
2349   MAYBE_DEVMETH (d, window_output_begin, (w));
2350
2351   /* If the window's structure has changed clear the internal border
2352      above it if it is topmost (the function will check). */
2353   if (f->windows_structure_changed || f->faces_changed)
2354     redisplay_clear_top_of_window (w);
2355
2356   /* Output each line. */
2357   for (line = 0; line < Dynarr_length (ddla); line++)
2358     {
2359       output_display_line (w, cdla, ddla, line, -1, -1);
2360     }
2361
2362   /* If the number of display lines has shrunk, adjust. */
2363   if (cdla_len > ddla_len)
2364     {
2365       Dynarr_length (cdla) = ddla_len;
2366     }
2367
2368   /* Output a vertical divider between windows, if necessary. */
2369   if (window_needs_vertical_divider (w)
2370       && (f->windows_structure_changed || f->clear))
2371     {
2372       MAYBE_DEVMETH (d, output_vertical_divider, (w, f->windows_structure_changed));
2373     }
2374
2375   /* Clear the rest of the window, if necessary. */
2376   if (need_to_clear_bottom)
2377     {
2378       redisplay_clear_bottom_of_window (w, ddla, need_to_clear_start,
2379                                         need_to_clear_end);
2380     }
2381
2382   w->window_end_pos[CURRENT_DISP] = w->window_end_pos[DESIRED_DISP];
2383   Fset_marker (w->start[CURRENT_DISP],
2384                make_int (marker_position (w->start[DESIRED_DISP])),
2385                w->buffer);
2386   Fset_marker (w->pointm[CURRENT_DISP],
2387                make_int (marker_position (w->pointm[DESIRED_DISP])),
2388                w->buffer);
2389   w->last_modified[CURRENT_DISP] = w->last_modified[DESIRED_DISP];
2390   w->last_facechange[CURRENT_DISP] = w->last_facechange[DESIRED_DISP];
2391   Fset_marker (w->last_start[CURRENT_DISP],
2392                Fmarker_position (w->last_start[DESIRED_DISP]), w->buffer);
2393   Fset_marker (w->last_point[CURRENT_DISP],
2394                Fmarker_position (w->last_point[DESIRED_DISP]), w->buffer);
2395   w->last_point_x[CURRENT_DISP] = w->last_point_x[DESIRED_DISP];
2396   w->last_point_y[CURRENT_DISP] = w->last_point_y[DESIRED_DISP];
2397   w->shadow_thickness_changed = 0;
2398
2399   set_window_display_buffer (w, XBUFFER (w->buffer));
2400   find_window_mirror (w)->truncate_win = window_truncation_on (w);
2401
2402   /* Overkill on invalidating the cache.  It is very bad for it to not
2403      get invalidated when it should be. */
2404   INVALIDATE_DEVICE_PIXEL_TO_GLYPH_CACHE (d);
2405
2406   redisplay_redraw_cursor (f, 0);
2407   MAYBE_DEVMETH (d, window_output_end, (w));
2408
2409 #ifdef HAVE_SCROLLBARS
2410   update_window_scrollbars (w, NULL, !MINI_WINDOW_P (w), 0);
2411 #endif
2412 }
2413
2414 /*****************************************************************************
2415  bevel_modeline
2416
2417  Draw a 3d border around the modeline on window W.
2418  ****************************************************************************/
2419 void
2420 bevel_modeline (struct window *w, struct display_line *dl)
2421 {
2422   struct frame *f = XFRAME (w->frame);
2423   struct device *d = XDEVICE (f->device);
2424   int x, y, width, height;
2425   int shadow_thickness = MODELINE_SHADOW_THICKNESS (w);
2426   enum edge_style style;
2427
2428   x = WINDOW_MODELINE_LEFT (w);
2429   width = WINDOW_MODELINE_RIGHT (w) - x;
2430   y = dl->ypos - dl->ascent - shadow_thickness;
2431   height = dl->ascent + dl->descent + 2 * shadow_thickness;
2432
2433   if (XINT (w->modeline_shadow_thickness) < 0)
2434     {
2435       style = EDGE_BEVEL_IN;
2436     }
2437   else
2438     {
2439       style = EDGE_BEVEL_OUT;
2440     }
2441
2442   MAYBE_DEVMETH (d, bevel_area,
2443                  (w, MODELINE_INDEX, x, y, width, height, shadow_thickness,
2444                   EDGE_ALL, style));
2445 }