Make tui_gen_win_info constructor protected
[external/binutils.git] / gdb / tui / tui-layout.c
1 /* TUI layout window management.
2
3    Copyright (C) 1998-2019 Free Software Foundation, Inc.
4
5    Contributed by Hewlett-Packard Company.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "command.h"
25 #include "symtab.h"
26 #include "frame.h"
27 #include "source.h"
28 #include <ctype.h>
29
30 #include "tui/tui.h"
31 #include "tui/tui-data.h"
32 #include "tui/tui-windata.h"
33 #include "tui/tui-wingeneral.h"
34 #include "tui/tui-stack.h"
35 #include "tui/tui-regs.h"
36 #include "tui/tui-win.h"
37 #include "tui/tui-winsource.h"
38 #include "tui/tui-disasm.h"
39 #include "tui/tui-layout.h"
40 #include "gdb_curses.h"
41
42 /*******************************
43 ** Static Local Decls
44 ********************************/
45 static void show_layout (enum tui_layout_type);
46 static tui_gen_win_info *init_and_make_win (tui_gen_win_info *,
47                                             enum tui_win_type,
48                                             int, int, int, int,
49                                             enum tui_box);
50 static void show_source_or_disasm_and_command (enum tui_layout_type);
51 static struct tui_win_info *make_source_or_disasm_window (enum tui_win_type, 
52                                                           int, int);
53 static struct tui_win_info *make_command_window (int, int);
54 static struct tui_win_info *make_source_window (int, int);
55 static struct tui_win_info *make_disasm_window (int, int);
56 static void make_data_window (struct tui_win_info **, int, int);
57 static void show_source_command (void);
58 static void show_disasm_command (void);
59 static void show_source_disasm_command (void);
60 static void show_data (enum tui_layout_type);
61 static enum tui_layout_type next_layout (void);
62 static enum tui_layout_type prev_layout (void);
63 static void tui_layout_command (const char *, int);
64 static void extract_display_start_addr (struct gdbarch **, CORE_ADDR *);
65
66
67 /***************************************
68 ** DEFINITIONS
69 ***************************************/
70
71 #define LAYOUT_USAGE     "Usage: layout prev | next | <layout_name> \n"
72
73 /* Show the screen layout defined.  */
74 static void
75 show_layout (enum tui_layout_type layout)
76 {
77   enum tui_layout_type cur_layout = tui_current_layout ();
78
79   if (layout != cur_layout)
80     {
81       /* Since the new layout may cause changes in window size, we
82          should free the content and reallocate on next display of
83          source/asm.  */
84       tui_clear_source_windows ();
85       if (layout == SRC_DATA_COMMAND 
86           || layout == DISASSEM_DATA_COMMAND)
87         {
88           show_data (layout);
89           tui_refresh_all (tui_win_list);
90         }
91       else
92         {
93           /* First make the current layout be invisible.  */
94           tui_make_all_invisible ();
95           tui_make_invisible (tui_locator_win_info_ptr ());
96
97           switch (layout)
98             {
99               /* Now show the new layout.  */
100             case SRC_COMMAND:
101               show_source_command ();
102               tui_add_to_source_windows (TUI_SRC_WIN);
103               break;
104             case DISASSEM_COMMAND:
105               show_disasm_command ();
106               tui_add_to_source_windows (TUI_DISASM_WIN);
107               break;
108             case SRC_DISASSEM_COMMAND:
109               show_source_disasm_command ();
110               tui_add_to_source_windows (TUI_SRC_WIN);
111               tui_add_to_source_windows (TUI_DISASM_WIN);
112               break;
113             default:
114               break;
115             }
116         }
117     }
118 }
119
120
121 /* Function to set the layout to SRC_COMMAND, DISASSEM_COMMAND,
122    SRC_DISASSEM_COMMAND, SRC_DATA_COMMAND, or DISASSEM_DATA_COMMAND.  */
123 enum tui_status
124 tui_set_layout (enum tui_layout_type layout_type)
125 {
126   enum tui_status status = TUI_SUCCESS;
127
128   if (layout_type != UNDEFINED_LAYOUT)
129     {
130       enum tui_layout_type cur_layout = tui_current_layout (),
131         new_layout = UNDEFINED_LAYOUT;
132       int regs_populate = FALSE;
133       struct gdbarch *gdbarch;
134       CORE_ADDR addr;
135       struct tui_win_info *win_with_focus = tui_win_with_focus ();
136       struct tui_layout_def *layout_def = tui_layout_def ();
137
138       extract_display_start_addr (&gdbarch, &addr);
139
140       new_layout = layout_type;
141
142       regs_populate = (new_layout == SRC_DATA_COMMAND
143                        || new_layout == DISASSEM_DATA_COMMAND);
144       if (new_layout != cur_layout)
145         {
146           show_layout (new_layout);
147
148           /* Now determine where focus should be.  */
149           if (win_with_focus != TUI_CMD_WIN)
150             {
151               switch (new_layout)
152                 {
153                 case SRC_COMMAND:
154                   tui_set_win_focus_to (TUI_SRC_WIN);
155                   layout_def->display_mode = SRC_WIN;
156                   break;
157                 case DISASSEM_COMMAND:
158                   /* The previous layout was not showing code.
159                      This can happen if there is no source
160                      available:
161
162                      1. if the source file is in another dir OR
163                      2. if target was compiled without -g
164                      We still want to show the assembly though!  */
165
166                   tui_get_begin_asm_address (&gdbarch, &addr);
167                   tui_set_win_focus_to (TUI_DISASM_WIN);
168                   layout_def->display_mode = DISASSEM_WIN;
169                   break;
170                 case SRC_DISASSEM_COMMAND:
171                   /* The previous layout was not showing code.
172                      This can happen if there is no source
173                      available:
174
175                      1. if the source file is in another dir OR
176                      2. if target was compiled without -g
177                      We still want to show the assembly though!  */
178
179                   tui_get_begin_asm_address (&gdbarch, &addr);
180                   if (win_with_focus == TUI_SRC_WIN)
181                     tui_set_win_focus_to (TUI_SRC_WIN);
182                   else
183                     tui_set_win_focus_to (TUI_DISASM_WIN);
184                   break;
185                 case SRC_DATA_COMMAND:
186                   if (win_with_focus != TUI_DATA_WIN)
187                     tui_set_win_focus_to (TUI_SRC_WIN);
188                   else
189                     tui_set_win_focus_to (TUI_DATA_WIN);
190                   layout_def->display_mode = SRC_WIN;
191                   break;
192                 case DISASSEM_DATA_COMMAND:
193                   /* The previous layout was not showing code.
194                      This can happen if there is no source
195                      available:
196
197                      1. if the source file is in another dir OR
198                      2. if target was compiled without -g
199                      We still want to show the assembly though!  */
200
201                   tui_get_begin_asm_address (&gdbarch, &addr);
202                   if (win_with_focus != TUI_DATA_WIN)
203                     tui_set_win_focus_to (TUI_DISASM_WIN);
204                   else
205                     tui_set_win_focus_to (TUI_DATA_WIN);
206                   layout_def->display_mode = DISASSEM_WIN;
207                   break;
208                 default:
209                   break;
210                 }
211             }
212           /*
213            * Now update the window content.
214            */
215           if (!regs_populate
216               && (new_layout == SRC_DATA_COMMAND
217                   || new_layout == DISASSEM_DATA_COMMAND))
218             tui_display_all_data ();
219
220           tui_update_source_windows_with_addr (gdbarch, addr);
221
222           if (regs_populate)
223             tui_show_registers (TUI_DATA_WIN->current_group);
224         }
225     }
226   else
227     status = TUI_FAILURE;
228
229   return status;
230 }
231
232 /* Add the specified window to the layout in a logical way.  This
233    means setting up the most logical layout given the window to be
234    added.  */
235 void
236 tui_add_win_to_layout (enum tui_win_type type)
237 {
238   enum tui_layout_type cur_layout = tui_current_layout ();
239
240   switch (type)
241     {
242     case SRC_WIN:
243       if (cur_layout != SRC_COMMAND
244           && cur_layout != SRC_DISASSEM_COMMAND
245           && cur_layout != SRC_DATA_COMMAND)
246         {
247           tui_clear_source_windows_detail ();
248           if (cur_layout == DISASSEM_DATA_COMMAND)
249             show_layout (SRC_DATA_COMMAND);
250           else
251             show_layout (SRC_COMMAND);
252         }
253       break;
254     case DISASSEM_WIN:
255       if (cur_layout != DISASSEM_COMMAND
256           && cur_layout != SRC_DISASSEM_COMMAND
257           && cur_layout != DISASSEM_DATA_COMMAND)
258         {
259           tui_clear_source_windows_detail ();
260           if (cur_layout == SRC_DATA_COMMAND)
261             show_layout (DISASSEM_DATA_COMMAND);
262           else
263             show_layout (DISASSEM_COMMAND);
264         }
265       break;
266     case DATA_WIN:
267       if (cur_layout != SRC_DATA_COMMAND
268           && cur_layout != DISASSEM_DATA_COMMAND)
269         {
270           if (cur_layout == DISASSEM_COMMAND)
271             show_layout (DISASSEM_DATA_COMMAND);
272           else
273             show_layout (SRC_DATA_COMMAND);
274         }
275       break;
276     default:
277       break;
278     }
279 }
280
281
282 /* Answer the height of a window.  If it hasn't been created yet,
283    answer what the height of a window would be based upon its type and
284    the layout.  */
285 int
286 tui_default_win_height (enum tui_win_type type, 
287                         enum tui_layout_type layout)
288 {
289   int h;
290
291   if (tui_win_list[type] != NULL)
292     h = tui_win_list[type]->height;
293   else
294     {
295       switch (layout)
296         {
297         case SRC_COMMAND:
298         case DISASSEM_COMMAND:
299           if (TUI_CMD_WIN == NULL)
300             h = tui_term_height () / 2;
301           else
302             h = tui_term_height () - TUI_CMD_WIN->height;
303           break;
304         case SRC_DISASSEM_COMMAND:
305         case SRC_DATA_COMMAND:
306         case DISASSEM_DATA_COMMAND:
307           if (TUI_CMD_WIN == NULL)
308             h = tui_term_height () / 3;
309           else
310             h = (tui_term_height () - TUI_CMD_WIN->height) / 2;
311           break;
312         default:
313           h = 0;
314           break;
315         }
316     }
317
318   return h;
319 }
320
321
322 /* Answer the height of a window.  If it hasn't been created yet,
323    answer what the height of a window would be based upon its type and
324    the layout.  */
325 int
326 tui_default_win_viewport_height (enum tui_win_type type,
327                                  enum tui_layout_type layout)
328 {
329   int h;
330
331   h = tui_default_win_height (type, layout);
332
333   if (tui_win_list[type] == TUI_CMD_WIN)
334     h -= 1;
335   else
336     h -= 2;
337
338   return h;
339 }
340
341 /* Complete possible layout names.  TEXT is the complete text entered so
342    far, WORD is the word currently being completed.  */
343
344 static void
345 layout_completer (struct cmd_list_element *ignore,
346                   completion_tracker &tracker,
347                   const char *text, const char *word)
348 {
349   static const char *layout_names [] =
350     { "src", "asm", "split", "regs", "next", "prev", NULL };
351
352   complete_on_enum (tracker, layout_names, text, word);
353 }
354
355 /* Function to initialize gdb commands, for tui window layout
356    manipulation.  */
357
358 void
359 _initialize_tui_layout (void)
360 {
361   struct cmd_list_element *cmd;
362
363   cmd = add_com ("layout", class_tui, tui_layout_command, _("\
364 Change the layout of windows.\n\
365 Usage: layout prev | next | LAYOUT-NAME\n\
366 Layout names are:\n\
367    src   : Displays source and command windows.\n\
368    asm   : Displays disassembly and command windows.\n\
369    split : Displays source, disassembly and command windows.\n\
370    regs  : Displays register window. If existing layout\n\
371            is source/command or assembly/command, the \n\
372            register window is displayed. If the\n\
373            source/assembly/command (split) is displayed, \n\
374            the register window is displayed with \n\
375            the window that has current logical focus."));
376   set_cmd_completer (cmd, layout_completer);
377 }
378
379
380 /*************************
381 ** STATIC LOCAL FUNCTIONS
382 **************************/
383
384
385 /* Function to set the layout to SRC, ASM, SPLIT, NEXT, PREV, DATA, or
386    REGS. */
387 enum tui_status
388 tui_set_layout_by_name (const char *layout_name)
389 {
390   enum tui_status status = TUI_SUCCESS;
391
392   if (layout_name != NULL)
393     {
394       int i;
395       enum tui_layout_type new_layout = UNDEFINED_LAYOUT;
396       enum tui_layout_type cur_layout = tui_current_layout ();
397
398       std::string copy = layout_name;
399       for (i = 0; i < copy.size (); i++)
400         copy[i] = toupper (copy[i]);
401       const char *buf_ptr = copy.c_str ();
402
403       /* First check for ambiguous input.  */
404       if (strlen (buf_ptr) <= 1 && *buf_ptr == 'S')
405         {
406           warning (_("Ambiguous command input."));
407           status = TUI_FAILURE;
408         }
409       else
410         {
411           if (subset_compare (buf_ptr, "SRC"))
412             new_layout = SRC_COMMAND;
413           else if (subset_compare (buf_ptr, "ASM"))
414             new_layout = DISASSEM_COMMAND;
415           else if (subset_compare (buf_ptr, "SPLIT"))
416             new_layout = SRC_DISASSEM_COMMAND;
417           else if (subset_compare (buf_ptr, "REGS"))
418             {
419               if (cur_layout == SRC_COMMAND
420                   || cur_layout == SRC_DATA_COMMAND)
421                 new_layout = SRC_DATA_COMMAND;
422               else
423                 new_layout = DISASSEM_DATA_COMMAND;
424             }
425           else if (subset_compare (buf_ptr, "NEXT"))
426             new_layout = next_layout ();
427           else if (subset_compare (buf_ptr, "PREV"))
428             new_layout = prev_layout ();
429           else
430             status = TUI_FAILURE;
431
432           if (status == TUI_SUCCESS)
433             {
434               /* Make sure the curses mode is enabled.  */
435               tui_enable ();
436               tui_set_layout (new_layout);
437             }
438         }
439     }
440   else
441     status = TUI_FAILURE;
442
443   return status;
444 }
445
446
447 static void
448 extract_display_start_addr (struct gdbarch **gdbarch_p, CORE_ADDR *addr_p)
449 {
450   enum tui_layout_type cur_layout = tui_current_layout ();
451   struct gdbarch *gdbarch = get_current_arch ();
452   CORE_ADDR addr;
453   CORE_ADDR pc;
454   struct symtab_and_line cursal = get_current_source_symtab_and_line ();
455
456   switch (cur_layout)
457     {
458     case SRC_COMMAND:
459     case SRC_DATA_COMMAND:
460       gdbarch = TUI_SRC_WIN->gdbarch;
461       find_line_pc (cursal.symtab,
462                     TUI_SRC_WIN->start_line_or_addr.u.line_no,
463                     &pc);
464       addr = pc;
465       break;
466     case DISASSEM_COMMAND:
467     case SRC_DISASSEM_COMMAND:
468     case DISASSEM_DATA_COMMAND:
469       gdbarch = TUI_DISASM_WIN->gdbarch;
470       addr = TUI_DISASM_WIN->start_line_or_addr.u.addr;
471       break;
472     default:
473       addr = 0;
474       break;
475     }
476
477   *gdbarch_p = gdbarch;
478   *addr_p = addr;
479 }
480
481
482 static void
483 tui_layout_command (const char *arg, int from_tty)
484 {
485   /* Switch to the selected layout.  */
486   if (tui_set_layout_by_name (arg) != TUI_SUCCESS)
487     warning (_("Invalid layout specified.\n%s"), LAYOUT_USAGE);
488 }
489
490 /* Answer the previous layout to cycle to.  */
491 static enum tui_layout_type
492 next_layout (void)
493 {
494   int new_layout;
495
496   new_layout = tui_current_layout ();
497   if (new_layout == UNDEFINED_LAYOUT)
498     new_layout = SRC_COMMAND;
499   else
500     {
501       new_layout++;
502       if (new_layout == UNDEFINED_LAYOUT)
503         new_layout = SRC_COMMAND;
504     }
505
506   return (enum tui_layout_type) new_layout;
507 }
508
509
510 /* Answer the next layout to cycle to.  */
511 static enum tui_layout_type
512 prev_layout (void)
513 {
514   int new_layout;
515
516   new_layout = tui_current_layout ();
517   if (new_layout == SRC_COMMAND)
518     new_layout = DISASSEM_DATA_COMMAND;
519   else
520     {
521       new_layout--;
522       if (new_layout == UNDEFINED_LAYOUT)
523         new_layout = DISASSEM_DATA_COMMAND;
524     }
525
526   return (enum tui_layout_type) new_layout;
527 }
528
529
530
531 static struct tui_win_info *
532 make_command_window (int height, int origin_y)
533 {
534   struct tui_win_info *result
535     = (struct tui_win_info *) init_and_make_win (NULL,
536                                                  CMD_WIN,
537                                                  height,
538                                                  tui_term_width (),
539                                                  0,
540                                                  origin_y,
541                                                  DONT_BOX_WINDOW);
542   return result;
543 }
544
545
546 /* make_source_window().
547  */
548 static struct tui_win_info *
549 make_source_window (int height, int origin_y)
550 {
551   return make_source_or_disasm_window (SRC_WIN, height, origin_y);
552 }                               /* make_source_window */
553
554
555 /* make_disasm_window().
556  */
557 static struct tui_win_info *
558 make_disasm_window (int height, int origin_y)
559 {
560   return make_source_or_disasm_window (DISASSEM_WIN, height, origin_y);
561 }                               /* make_disasm_window */
562
563
564 static void
565 make_data_window (struct tui_win_info **win_info_ptr, 
566                   int height, int origin_y)
567 {
568   *win_info_ptr
569     = (struct tui_win_info *) init_and_make_win (*win_info_ptr,
570                                                  DATA_WIN,
571                                                  height,
572                                                  tui_term_width (),
573                                                  0,
574                                                  origin_y,
575                                                  BOX_WINDOW);
576 }
577
578
579
580 /* Show the Source/Command layout.  */
581 static void
582 show_source_command (void)
583 {
584   show_source_or_disasm_and_command (SRC_COMMAND);
585 }
586
587
588 /* Show the Dissassem/Command layout.  */
589 static void
590 show_disasm_command (void)
591 {
592   show_source_or_disasm_and_command (DISASSEM_COMMAND);
593 }
594
595
596 /* Show the Source/Disassem/Command layout.  */
597 static void
598 show_source_disasm_command (void)
599 {
600   if (tui_current_layout () != SRC_DISASSEM_COMMAND)
601     {
602       int cmd_height, src_height, asm_height;
603
604       if (TUI_CMD_WIN != NULL)
605         cmd_height = TUI_CMD_WIN->height;
606       else
607         cmd_height = tui_term_height () / 3;
608
609       src_height = (tui_term_height () - cmd_height) / 2;
610       asm_height = tui_term_height () - (src_height + cmd_height);
611
612       if (TUI_SRC_WIN == NULL)
613         tui_win_list[SRC_WIN] = make_source_window (src_height, 0);
614       else
615         {
616           TUI_SRC_WIN->reset (TUI_SRC_WIN->type,
617                               src_height,
618                               TUI_SRC_WIN->width,
619                               TUI_SRC_WIN->execution_info->width,
620                               0);
621           TUI_SRC_WIN->execution_info->reset (EXEC_INFO_WIN,
622                                               src_height,
623                                               3,
624                                               0,
625                                               0);
626           tui_make_visible (TUI_SRC_WIN);
627           tui_make_visible (TUI_SRC_WIN->execution_info);
628           TUI_SRC_WIN->m_has_locator = false;
629         }
630
631       struct tui_locator_window *locator = tui_locator_win_info_ptr ();
632       gdb_assert (locator != nullptr);
633
634       tui_show_source_content (TUI_SRC_WIN);
635       if (TUI_DISASM_WIN == NULL)
636         {
637           tui_win_list[DISASSEM_WIN]
638             = make_disasm_window (asm_height, src_height - 1);
639           init_and_make_win (locator,
640                              LOCATOR_WIN,
641                              2 /* 1 */ ,
642                              tui_term_width (),
643                              0,
644                              (src_height + asm_height) - 1,
645                              DONT_BOX_WINDOW);
646         }
647       else
648         {
649           locator->reset (LOCATOR_WIN,
650                           2 /* 1 */ ,
651                           tui_term_width (),
652                           0,
653                           (src_height + asm_height) - 1);
654           TUI_DISASM_WIN->m_has_locator = true;
655           TUI_DISASM_WIN->reset (TUI_DISASM_WIN->type,
656                                  asm_height,
657                                  TUI_DISASM_WIN->width,
658                                  TUI_DISASM_WIN->execution_info->width,
659                                  src_height - 1);
660           TUI_DISASM_WIN->execution_info->reset (EXEC_INFO_WIN,
661                                                  asm_height,
662                                                  3,
663                                                  0,
664                                                  src_height - 1);
665           tui_make_visible (TUI_DISASM_WIN);
666           tui_make_visible (TUI_DISASM_WIN->execution_info);
667         }
668       TUI_SRC_WIN->m_has_locator = false;
669       TUI_DISASM_WIN->m_has_locator = true;
670       tui_make_visible (locator);
671       tui_show_locator_content ();
672       tui_show_source_content (TUI_DISASM_WIN);
673
674       if (TUI_CMD_WIN == NULL)
675         tui_win_list[CMD_WIN]
676           = make_command_window (cmd_height, tui_term_height () - cmd_height);
677       else
678         {
679           TUI_CMD_WIN->reset (TUI_CMD_WIN->type,
680                               TUI_CMD_WIN->height,
681                               TUI_CMD_WIN->width,
682                               0,
683                               TUI_CMD_WIN->origin.y);
684           tui_make_visible (TUI_CMD_WIN);
685         }
686       TUI_CMD_WIN->refresh_window ();
687       tui_set_current_layout_to (SRC_DISASSEM_COMMAND);
688     }
689 }
690
691
692 /* Show the Source/Data/Command or the Dissassembly/Data/Command
693    layout.  */
694 static void
695 show_data (enum tui_layout_type new_layout)
696 {
697   int total_height = (tui_term_height () - TUI_CMD_WIN->height);
698   int src_height, data_height;
699   enum tui_win_type win_type;
700
701   struct tui_locator_window *locator = tui_locator_win_info_ptr ();
702   gdb_assert (locator != nullptr);
703
704   data_height = total_height / 2;
705   src_height = total_height - data_height;
706   tui_make_all_invisible ();
707   tui_make_invisible (locator);
708   make_data_window (&tui_win_list[DATA_WIN], data_height, 0);
709   if (new_layout == SRC_DATA_COMMAND)
710     win_type = SRC_WIN;
711   else
712     win_type = DISASSEM_WIN;
713
714   tui_source_window_base *base;
715   if (tui_win_list[win_type] == NULL)
716     {
717       if (win_type == SRC_WIN)
718         tui_win_list[win_type]
719           = make_source_window (src_height, data_height - 1);
720       else
721         tui_win_list[win_type]
722           = make_disasm_window (src_height, data_height - 1);
723       init_and_make_win (locator,
724                              LOCATOR_WIN,
725                              2 /* 1 */ ,
726                              tui_term_width (),
727                              0,
728                              total_height - 1,
729                              DONT_BOX_WINDOW);
730       base = (tui_source_window_base *) tui_win_list[win_type];
731     }
732   else
733     {
734       base = (tui_source_window_base *) tui_win_list[win_type];
735       tui_win_list[win_type]->reset (tui_win_list[win_type]->type,
736                                      src_height,
737                                      tui_win_list[win_type]->width,
738                                      base->execution_info->width,
739                                      data_height - 1);
740       base->execution_info->reset (EXEC_INFO_WIN,
741                                    src_height,
742                                    3,
743                                    0,
744                                    data_height - 1);
745       tui_make_visible (tui_win_list[win_type]);
746       tui_make_visible (base->execution_info);
747       locator->reset (LOCATOR_WIN,
748                       2 /* 1 */ ,
749                       tui_term_width (),
750                       0,
751                       total_height - 1);
752     }
753   base->m_has_locator = true;
754   tui_make_visible (locator);
755   tui_show_locator_content ();
756   tui_add_to_source_windows
757     ((tui_source_window_base *) tui_win_list[win_type]);
758   tui_set_current_layout_to (new_layout);
759 }
760
761 void
762 tui_gen_win_info::reset (enum tui_win_type win_type,
763                          int height_, int width_,
764                          int origin_x_, int origin_y_)
765 {
766   int h = height_;
767
768   gdb_assert (type == win_type);
769
770   width = width_;
771   height = h;
772   if (h > 1)
773     {
774       viewport_height = h - 1;
775       if (type != CMD_WIN)
776         viewport_height--;
777     }
778   else
779     viewport_height = 1;
780   origin.x = origin_x_;
781   origin.y = origin_y_;
782 }
783
784 /* init_and_make_win().
785  */
786 static tui_gen_win_info *
787 init_and_make_win (tui_gen_win_info *win_info, 
788                    enum tui_win_type win_type,
789                    int height, int width, 
790                    int origin_x, int origin_y,
791                    enum tui_box box_it)
792 {
793   if (win_info == NULL)
794     {
795       switch (win_type)
796         {
797         case SRC_WIN:
798           win_info = new tui_source_window ();
799           break;
800
801         case DISASSEM_WIN:
802           win_info = new tui_disasm_window ();
803           break;
804
805         case DATA_WIN:
806           win_info = new tui_data_window ();
807           break;
808
809         case CMD_WIN:
810           win_info = new tui_cmd_window ();
811           break;
812
813         case EXEC_INFO_WIN:
814           win_info = new tui_exec_info_window ();
815           break;
816
817         default:
818           gdb_assert_not_reached (_("unhandled window type"));
819         }
820     }
821
822   win_info->reset (win_type, height, width, origin_x, origin_y);
823   tui_make_window (win_info, box_it);
824
825   return win_info;
826 }
827
828
829 static struct tui_win_info *
830 make_source_or_disasm_window (enum tui_win_type type,
831                               int height, int origin_y)
832 {
833   struct tui_exec_info_window *execution_info
834     = (tui_exec_info_window *) init_and_make_win (nullptr,
835                                                   EXEC_INFO_WIN,
836                                                   height,
837                                                   3,
838                                                   0,
839                                                   origin_y,
840                                                   DONT_BOX_WINDOW);
841
842   /* Now create the source window.  */
843   struct tui_source_window_base *result
844     = ((struct tui_source_window_base *)
845        init_and_make_win (NULL,
846                           type,
847                           height,
848                           tui_term_width () - execution_info->width,
849                           execution_info->width,
850                           origin_y,
851                           BOX_WINDOW));
852   result->execution_info = execution_info;
853   return result;
854 }
855
856
857 /* Show the Source/Command or the Disassem layout.  */
858 static void
859 show_source_or_disasm_and_command (enum tui_layout_type layout_type)
860 {
861   if (tui_current_layout () != layout_type)
862     {
863       struct tui_win_info **win_info_ptr;
864       int src_height, cmd_height;
865       struct tui_locator_window *locator = tui_locator_win_info_ptr ();
866       gdb_assert (locator != nullptr);
867
868       if (TUI_CMD_WIN != NULL)
869         cmd_height = TUI_CMD_WIN->height;
870       else
871         cmd_height = tui_term_height () / 3;
872       src_height = tui_term_height () - cmd_height;
873
874       if (layout_type == SRC_COMMAND)
875         win_info_ptr = &tui_win_list[SRC_WIN];
876       else
877         win_info_ptr = &tui_win_list[DISASSEM_WIN];
878
879       tui_source_window_base *base;
880       if ((*win_info_ptr) == NULL)
881         {
882           if (layout_type == SRC_COMMAND)
883             *win_info_ptr = make_source_window (src_height - 1, 0);
884           else
885             *win_info_ptr = make_disasm_window (src_height - 1, 0);
886           init_and_make_win (locator,
887                              LOCATOR_WIN,
888                              2 /* 1 */ ,
889                              tui_term_width (),
890                              0,
891                              src_height - 1,
892                              DONT_BOX_WINDOW);
893           base = (tui_source_window_base *) *win_info_ptr;
894         }
895       else
896         {
897           base = (tui_source_window_base *) *win_info_ptr;
898           locator->reset (LOCATOR_WIN,
899                           2 /* 1 */ ,
900                           tui_term_width (),
901                           0,
902                           src_height - 1);
903           base->m_has_locator = true;
904           (*win_info_ptr)->reset ((*win_info_ptr)->type,
905                                   src_height - 1,
906                                   (*win_info_ptr)->width,
907                                   base->execution_info->width,
908                                   0);
909           base->execution_info->reset (EXEC_INFO_WIN,
910                                        src_height - 1,
911                                        3,
912                                        0,
913                                        0);
914           tui_make_visible (*win_info_ptr);
915           tui_make_visible (base->execution_info);
916         }
917
918       base->m_has_locator = true;
919       tui_make_visible (locator);
920       tui_show_locator_content ();
921       tui_show_source_content (base);
922
923       if (TUI_CMD_WIN == NULL)
924         {
925           tui_win_list[CMD_WIN] = make_command_window (cmd_height,
926                                                        src_height);
927           TUI_CMD_WIN->refresh_window ();
928         }
929       else
930         {
931           TUI_CMD_WIN->reset (TUI_CMD_WIN->type,
932                               TUI_CMD_WIN->height,
933                               TUI_CMD_WIN->width,
934                               TUI_CMD_WIN->origin.x,
935                               TUI_CMD_WIN->origin.y);
936           tui_make_visible (TUI_CMD_WIN);
937         }
938       tui_set_current_layout_to (layout_type);
939     }
940 }