📄 window.c
字号:
/* window.c -- Windows in Info. $Id: window.c,v 1.1.1.3 1998/03/24 18:20:20 law Exp $ This file is part of GNU Info, a program for reading online documentation stored in Info format. Copyright (C) 1993, 97 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Written by Brian Fox (bfox@ai.mit.edu). */#include "info.h"#include "nodes.h"#include "window.h"#include "display.h"#include "info-utils.h"#include "infomap.h"/* The window which describes the screen. */WINDOW *the_screen = (WINDOW *)NULL;/* The window which describes the echo area. */WINDOW *the_echo_area = (WINDOW *)NULL;/* The list of windows in Info. */WINDOW *windows = (WINDOW *)NULL;/* Pointer to the active window in WINDOW_LIST. */WINDOW *active_window = (WINDOW *)NULL;/* The size of the echo area in Info. It never changes, irregardless of the size of the screen. */#define ECHO_AREA_HEIGHT 1/* Macro returns the amount of space that the echo area truly requires relative to the entire screen. */#define echo_area_required (1 + the_echo_area->height)/* Initalize the window system by creating THE_SCREEN and THE_ECHO_AREA. Create the first window ever. You pass the dimensions of the total screen size. */voidwindow_initialize_windows (width, height) int width, height;{ the_screen = (WINDOW *)xmalloc (sizeof (WINDOW)); the_echo_area = (WINDOW *)xmalloc (sizeof (WINDOW)); windows = (WINDOW *)xmalloc (sizeof (WINDOW)); active_window = windows; zero_mem (the_screen, sizeof (WINDOW)); zero_mem (the_echo_area, sizeof (WINDOW)); zero_mem (active_window, sizeof (WINDOW)); /* None of these windows has a goal column yet. */ the_echo_area->goal_column = -1; active_window->goal_column = -1; the_screen->goal_column = -1; /* The active and echo_area windows are visible. The echo_area is permanent. The screen is permanent. */ active_window->flags = W_WindowVisible; the_echo_area->flags = W_WindowIsPerm | W_InhibitMode | W_WindowVisible; the_screen->flags = W_WindowIsPerm; /* The height of the echo area never changes. It is statically set right here, and it must be at least 1 line for display. The size of the initial window cannot be the same size as the screen, since the screen includes the echo area. So, we make the height of the initial window equal to the screen's displayable region minus the height of the echo area. */ the_echo_area->height = ECHO_AREA_HEIGHT; active_window->height = the_screen->height - 1 - the_echo_area->height; window_new_screen_size (width, height, (VFunction *)NULL); /* The echo area uses a different keymap than normal info windows. */ the_echo_area->keymap = echo_area_keymap; active_window->keymap = info_keymap;}/* Given that the size of the screen has changed to WIDTH and HEIGHT from whatever it was before (found in the_screen->height, ->width), change the size (and possibly location) of each window in the screen. If a window would become too small, call the function DELETER on it, after deleting the window from our chain of windows. If DELETER is NULL, nothing extra is done. The last window can never be deleted, but it can become invisible. *//* If non-null, a function to call with WINDOW as argument when the function window_new_screen_size () has deleted WINDOW. */VFunction *window_deletion_notifier = (VFunction *)NULL;voidwindow_new_screen_size (width, height) int width, height;{ register WINDOW *win; int delta_height, delta_each, delta_leftover; int numwins; /* If no change, do nothing. */ if (width == the_screen->width && height == the_screen->height) return; /* If the new window height is too small, make it be zero. */ if (height < (WINDOW_MIN_SIZE + the_echo_area->height)) height = 0; if (width < 0) width = 0; /* Find out how many windows will change. */ for (numwins = 0, win = windows; win; win = win->next, numwins++); /* See if some windows will need to be deleted. This is the case if the screen is getting smaller, and the available space divided by the number of windows is less than WINDOW_MIN_SIZE. In that case, delete some windows and try again until there is either enough space to divy up among the windows, or until there is only one window left. */ while ((height - echo_area_required) / numwins <= WINDOW_MIN_SIZE) { /* If only one window, make the size of it be zero, and return immediately. */ if (!windows->next) { windows->height = 0; maybe_free (windows->line_starts); windows->line_starts = (char **)NULL; windows->line_count = 0; break; } /* If we have some temporary windows, delete one of them. */ for (win = windows; win; win = win->next) if (win->flags & W_TempWindow) break; /* Otherwise, delete the first window, and try again. */ if (!win) win = windows; if (window_deletion_notifier) (*window_deletion_notifier) (win); window_delete_window (win); numwins--; } /* The screen has changed height and width. */ delta_height = height - the_screen->height; /* This is how much. */ the_screen->height = height; /* This is the new height. */ the_screen->width = width; /* This is the new width. */ /* Set the start of the echo area. */ the_echo_area->first_row = height - the_echo_area->height; the_echo_area->width = width; /* Check to see if the screen can really be changed this way. */ if ((!windows->next) && ((windows->height == 0) && (delta_height < 0))) return; /* Divide the change in height among the available windows. */ delta_each = delta_height / numwins; delta_leftover = delta_height - (delta_each * numwins); /* Change the height of each window in the chain by delta_each. Change the height of the last window in the chain by delta_each and by the leftover amount of change. Change the width of each window to be WIDTH. */ for (win = windows; win; win = win->next) { if ((win->width != width) && ((win->flags & W_InhibitMode) == 0)) { win->width = width; maybe_free (win->modeline); win->modeline = (char *)xmalloc (1 + width); } win->height += delta_each; /* If the previous height of this window was zero, it was the only window, and it was not visible. Thus we need to compensate for the echo_area. */ if (win->height == delta_each) win->height -= (1 + the_echo_area->height); /* If this is not the first window in the chain, then change the first row of it. We cannot just add delta_each to the first row, since this window's first row is the sum of the collective increases that have gone before it. So we just add one to the location of the previous window's modeline. */ if (win->prev) win->first_row = (win->prev->first_row + win->prev->height) + 1; /* The last window in the chain gets the extra space (or shrinkage). */ if (!win->next) win->height += delta_leftover; if (win->node) recalculate_line_starts (win); win->flags |= W_UpdateWindow; } /* If the screen got smaller, check over the windows just shrunk to keep them within bounds. Some of the windows may have gotten smaller than WINDOW_MIN_HEIGHT in which case some of the other windows are larger than the available display space in the screen. Because of our intial test above, we know that there is enough space for all of the windows. */ if ((delta_each < 0) && ((windows->height != 0) && windows->next)) { int avail; avail = the_screen->height - (numwins + the_echo_area->height); win = windows; while (win) { if ((win->height < WINDOW_MIN_HEIGHT) || (win->height > avail)) { WINDOW *lastwin; /* Split the space among the available windows. */ delta_each = avail / numwins; delta_leftover = avail - (delta_each * numwins); for (win = windows; win; win = win->next) { lastwin = win; if (win->prev) win->first_row = (win->prev->first_row + win->prev->height) + 1; win->height = delta_each; } /* Give the leftover space (if any) to the last window. */ lastwin->height += delta_leftover; break; } else win= win->next; } }}/* Make a new window showing NODE, and return that window structure. If NODE is passed as NULL, then show the node showing in the active window. If the window could not be made return a NULL pointer. The active window is not changed.*/WINDOW *window_make_window (node) NODE *node;{ WINDOW *window; if (!node) node = active_window->node; /* If there isn't enough room to make another window, return now. */ if ((active_window->height / 2) < WINDOW_MIN_SIZE) return ((WINDOW *)NULL); /* Make and initialize the new window. The fudging about with -1 and +1 is because the following window in the chain cannot start at window->height, since that is where the modeline for the previous window is displayed. The inverse adjustment is made in window_delete_window (). */ window = (WINDOW *)xmalloc (sizeof (WINDOW)); window->width = the_screen->width; window->height = (active_window->height / 2) - 1;#if defined (SPLIT_BEFORE_ACTIVE) window->first_row = active_window->first_row;#else window->first_row = active_window->first_row + (active_window->height - window->height);#endif window->keymap = info_keymap; window->goal_column = -1; window->modeline = (char *)xmalloc (1 + window->width); window->line_starts = (char **)NULL; window->flags = W_UpdateWindow | W_WindowVisible; window_set_node_of_window (window, node); /* Adjust the height of the old active window. */ active_window->height -= (window->height + 1);#if defined (SPLIT_BEFORE_ACTIVE) active_window->first_row += (window->height + 1);#endif active_window->flags |= W_UpdateWindow; /* Readjust the new and old windows so that their modelines and contents will be displayed correctly. */#if defined (NOTDEF) /* We don't have to do this for WINDOW since window_set_node_of_window () already did. */ window_adjust_pagetop (window); window_make_modeline (window);#endif /* NOTDEF */ /* We do have to readjust the existing active window. */ window_adjust_pagetop (active_window); window_make_modeline (active_window);#if defined (SPLIT_BEFORE_ACTIVE) /* This window is just before the active one. The active window gets bumped down one. The active window is not changed. */ window->next = active_window; window->prev = active_window->prev; active_window->prev = window; if (window->prev) window->prev->next = window; else windows = window;#else /* This window is just after the active one. Which window is active is not changed. */ window->prev = active_window; window->next = active_window->next; active_window->next = window; if (window->next) window->next->prev = window;#endif /* !SPLIT_BEFORE_ACTIVE */ return (window);}/* These useful macros make it possible to read the code in window_change_window_height (). */#define grow_me_shrinking_next(me, next, diff) \ do { \ me->height += diff; \ next->height -= diff; \ next->first_row += diff; \ window_adjust_pagetop (next); \ } while (0)#define grow_me_shrinking_prev(me, prev, diff) \ do { \ me->height += diff; \ prev->height -= diff; \ me->first_row -=diff; \ window_adjust_pagetop (prev); \ } while (0)#define shrink_me_growing_next(me, next, diff) \ do { \ me->height -= diff; \ next->height += diff; \ next->first_row -= diff; \ window_adjust_pagetop (next); \ } while (0)#define shrink_me_growing_prev(me, prev, diff) \ do { \ me->height -= diff; \ prev->height += diff; \ me->first_row += diff; \ window_adjust_pagetop (prev); \ } while (0)/* Change the height of WINDOW by AMOUNT. This also automagically adjusts the previous and next windows in the chain. If there is only one user window, then no change takes place. */voidwindow_change_window_height (window, amount) WINDOW *window; int amount;{ register WINDOW *win, *prev, *next; /* If there is only one window, or if the amount of change is zero, return immediately. */ if (!windows->next || amount == 0) return; /* Find this window in our chain. */ for (win = windows; win; win = win->next) if (win == window) break; /* If the window is isolated (i.e., doesn't appear in our window list, then quit now. */ if (!win) return; /* Change the height of this window by AMOUNT, if that is possible. It can be impossible if there isn't enough available room on the screen, or if the resultant window would be too small. */ prev = window->prev; next = window->next; /* WINDOW decreasing in size? */ if (amount < 0) { int abs_amount = -amount; /* It is easier to deal with this way. */ /* If the resultant window would be too small, stop here. */ if ((window->height - abs_amount) < WINDOW_MIN_HEIGHT) return; /* If we have two neighboring windows, choose the smaller one to get larger. */ if (next && prev) { if (prev->height < next->height) shrink_me_growing_prev (window, prev, abs_amount); else shrink_me_growing_next (window, next, abs_amount); } else if (next) shrink_me_growing_next (window, next, abs_amount); else shrink_me_growing_prev (window, prev, abs_amount); } /* WINDOW increasing in size? */ if (amount > 0) { int total_avail, next_avail = 0, prev_avail = 0; if (next) next_avail = next->height - WINDOW_MIN_SIZE; if (prev) prev_avail = prev->height - WINDOW_MIN_SIZE; total_avail = next_avail + prev_avail; /* If there isn't enough space available to grow this window, give up. */ if (amount > total_avail) return; /* If there aren't two neighboring windows, or if one of the neighbors is larger than the other one by at least AMOUNT, grow that one. */ if ((next && !prev) || ((next_avail - amount) >= prev_avail)) grow_me_shrinking_next (window, next, amount); else if ((prev && !next) || ((prev_avail - amount) >= next_avail)) grow_me_shrinking_prev (window, prev, amount); else { int change; /* This window has two neighbors. They both must be shrunk in to make enough space for WINDOW to grow. Make them both the same size. */ if (prev_avail > next_avail) { change = prev_avail - next_avail; grow_me_shrinking_prev (window, prev, change); amount -= change; } else { change = next_avail - prev_avail; grow_me_shrinking_next (window, next, change); amount -= change; } /* Both neighbors are the same size. Split the difference in AMOUNT between them. */ while (amount) { window->height++; amount--; /* Odd numbers grow next, even grow prev. */ if (amount & 1) { prev->height--; window->first_row--; } else { next->height--; next->first_row++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -