Truncate long TUI window titles
[deliverable/binutils-gdb.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-command.h"
32 #include "tui/tui-data.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 "tui/tui-source.h"
41 #include "gdb_curses.h"
42
43 /*******************************
44 ** Static Local Decls
45 ********************************/
46 static void show_layout (enum tui_layout_type);
47 static void show_source_or_disasm_and_command (enum tui_layout_type);
48 static void show_source_command (void);
49 static void show_disasm_command (void);
50 static void show_source_disasm_command (void);
51 static void show_data (enum tui_layout_type);
52 static enum tui_layout_type next_layout (void);
53 static enum tui_layout_type prev_layout (void);
54 static void tui_layout_command (const char *, int);
55 static void extract_display_start_addr (struct gdbarch **, CORE_ADDR *);
56
57
58 static enum tui_layout_type current_layout = UNDEFINED_LAYOUT;
59
60 /* Accessor for the current layout. */
61 enum tui_layout_type
62 tui_current_layout (void)
63 {
64 return current_layout;
65 }
66
67 /***************************************
68 ** DEFINITIONS
69 ***************************************/
70
71 /* Show the screen layout defined. */
72 static void
73 show_layout (enum tui_layout_type layout)
74 {
75 enum tui_layout_type cur_layout = tui_current_layout ();
76
77 if (layout != cur_layout)
78 {
79 tui_make_all_invisible ();
80 switch (layout)
81 {
82 case SRC_DATA_COMMAND:
83 case DISASSEM_DATA_COMMAND:
84 show_data (layout);
85 break;
86 /* Now show the new layout. */
87 case SRC_COMMAND:
88 show_source_command ();
89 break;
90 case DISASSEM_COMMAND:
91 show_disasm_command ();
92 break;
93 case SRC_DISASSEM_COMMAND:
94 show_source_disasm_command ();
95 break;
96 default:
97 break;
98 }
99
100 tui_delete_invisible_windows ();
101 }
102 }
103
104
105 /* Function to set the layout to SRC_COMMAND, DISASSEM_COMMAND,
106 SRC_DISASSEM_COMMAND, SRC_DATA_COMMAND, or DISASSEM_DATA_COMMAND. */
107 void
108 tui_set_layout (enum tui_layout_type layout_type)
109 {
110 gdb_assert (layout_type != UNDEFINED_LAYOUT);
111
112 enum tui_layout_type cur_layout = tui_current_layout ();
113 struct gdbarch *gdbarch;
114 CORE_ADDR addr;
115 struct tui_win_info *win_with_focus = tui_win_with_focus ();
116
117 extract_display_start_addr (&gdbarch, &addr);
118
119 enum tui_layout_type new_layout = layout_type;
120
121 if (new_layout != cur_layout)
122 {
123 show_layout (new_layout);
124
125 /* Now determine where focus should be. */
126 if (win_with_focus != TUI_CMD_WIN)
127 {
128 switch (new_layout)
129 {
130 case SRC_COMMAND:
131 tui_set_win_focus_to (TUI_SRC_WIN);
132 break;
133 case DISASSEM_COMMAND:
134 /* The previous layout was not showing code.
135 This can happen if there is no source
136 available:
137
138 1. if the source file is in another dir OR
139 2. if target was compiled without -g
140 We still want to show the assembly though! */
141
142 tui_get_begin_asm_address (&gdbarch, &addr);
143 tui_set_win_focus_to (TUI_DISASM_WIN);
144 break;
145 case SRC_DISASSEM_COMMAND:
146 /* The previous layout was not showing code.
147 This can happen if there is no source
148 available:
149
150 1. if the source file is in another dir OR
151 2. if target was compiled without -g
152 We still want to show the assembly though! */
153
154 tui_get_begin_asm_address (&gdbarch, &addr);
155 if (win_with_focus == TUI_SRC_WIN)
156 tui_set_win_focus_to (TUI_SRC_WIN);
157 else
158 tui_set_win_focus_to (TUI_DISASM_WIN);
159 break;
160 case SRC_DATA_COMMAND:
161 if (win_with_focus != TUI_DATA_WIN)
162 tui_set_win_focus_to (TUI_SRC_WIN);
163 else
164 tui_set_win_focus_to (TUI_DATA_WIN);
165 break;
166 case DISASSEM_DATA_COMMAND:
167 /* The previous layout was not showing code.
168 This can happen if there is no source
169 available:
170
171 1. if the source file is in another dir OR
172 2. if target was compiled without -g
173 We still want to show the assembly though! */
174
175 tui_get_begin_asm_address (&gdbarch, &addr);
176 if (win_with_focus != TUI_DATA_WIN)
177 tui_set_win_focus_to (TUI_DISASM_WIN);
178 else
179 tui_set_win_focus_to (TUI_DATA_WIN);
180 break;
181 default:
182 break;
183 }
184 }
185 /*
186 * Now update the window content.
187 */
188 tui_update_source_windows_with_addr (gdbarch, addr);
189 if (new_layout == SRC_DATA_COMMAND
190 || new_layout == DISASSEM_DATA_COMMAND)
191 TUI_DATA_WIN->show_registers (TUI_DATA_WIN->get_current_group ());
192 }
193 }
194
195 /* Add the specified window to the layout in a logical way. This
196 means setting up the most logical layout given the window to be
197 added. */
198 void
199 tui_add_win_to_layout (enum tui_win_type type)
200 {
201 enum tui_layout_type cur_layout = tui_current_layout ();
202
203 switch (type)
204 {
205 case SRC_WIN:
206 if (cur_layout != SRC_COMMAND
207 && cur_layout != SRC_DISASSEM_COMMAND
208 && cur_layout != SRC_DATA_COMMAND)
209 {
210 tui_clear_source_windows_detail ();
211 if (cur_layout == DISASSEM_DATA_COMMAND)
212 show_layout (SRC_DATA_COMMAND);
213 else
214 show_layout (SRC_COMMAND);
215 }
216 break;
217 case DISASSEM_WIN:
218 if (cur_layout != DISASSEM_COMMAND
219 && cur_layout != SRC_DISASSEM_COMMAND
220 && cur_layout != DISASSEM_DATA_COMMAND)
221 {
222 tui_clear_source_windows_detail ();
223 if (cur_layout == SRC_DATA_COMMAND)
224 show_layout (DISASSEM_DATA_COMMAND);
225 else
226 show_layout (DISASSEM_COMMAND);
227 }
228 break;
229 case DATA_WIN:
230 if (cur_layout != SRC_DATA_COMMAND
231 && cur_layout != DISASSEM_DATA_COMMAND)
232 {
233 if (cur_layout == DISASSEM_COMMAND)
234 show_layout (DISASSEM_DATA_COMMAND);
235 else
236 show_layout (SRC_DATA_COMMAND);
237 }
238 break;
239 default:
240 break;
241 }
242 }
243
244
245 /* Answer the height of a window. If it hasn't been created yet,
246 answer what the height of a window would be based upon its type and
247 the layout. */
248 static int
249 tui_default_win_height (enum tui_win_type type,
250 enum tui_layout_type layout)
251 {
252 int h;
253
254 if (tui_win_list[type] != NULL)
255 h = tui_win_list[type]->height;
256 else
257 {
258 switch (layout)
259 {
260 case SRC_COMMAND:
261 case DISASSEM_COMMAND:
262 if (TUI_CMD_WIN == NULL)
263 h = tui_term_height () / 2;
264 else
265 h = tui_term_height () - TUI_CMD_WIN->height;
266 break;
267 case SRC_DISASSEM_COMMAND:
268 case SRC_DATA_COMMAND:
269 case DISASSEM_DATA_COMMAND:
270 if (TUI_CMD_WIN == NULL)
271 h = tui_term_height () / 3;
272 else
273 h = (tui_term_height () - TUI_CMD_WIN->height) / 2;
274 break;
275 default:
276 h = 0;
277 break;
278 }
279 }
280
281 return h;
282 }
283
284
285 /* Answer the height of a window. If it hasn't been created yet,
286 answer what the height of a window would be based upon its type and
287 the layout. */
288 int
289 tui_default_win_viewport_height (enum tui_win_type type,
290 enum tui_layout_type layout)
291 {
292 int h;
293
294 h = tui_default_win_height (type, layout);
295
296 if (type == CMD_WIN)
297 h -= 1;
298 else
299 h -= 2;
300
301 return h;
302 }
303
304 /* Complete possible layout names. TEXT is the complete text entered so
305 far, WORD is the word currently being completed. */
306
307 static void
308 layout_completer (struct cmd_list_element *ignore,
309 completion_tracker &tracker,
310 const char *text, const char *word)
311 {
312 static const char *layout_names [] =
313 { "src", "asm", "split", "regs", "next", "prev", NULL };
314
315 complete_on_enum (tracker, layout_names, text, word);
316 }
317
318 /* Function to initialize gdb commands, for tui window layout
319 manipulation. */
320
321 void
322 _initialize_tui_layout (void)
323 {
324 struct cmd_list_element *cmd;
325
326 cmd = add_com ("layout", class_tui, tui_layout_command, _("\
327 Change the layout of windows.\n\
328 Usage: layout prev | next | LAYOUT-NAME\n\
329 Layout names are:\n\
330 src : Displays source and command windows.\n\
331 asm : Displays disassembly and command windows.\n\
332 split : Displays source, disassembly and command windows.\n\
333 regs : Displays register window. If existing layout\n\
334 is source/command or assembly/command, the \n\
335 register window is displayed. If the\n\
336 source/assembly/command (split) is displayed, \n\
337 the register window is displayed with \n\
338 the window that has current logical focus."));
339 set_cmd_completer (cmd, layout_completer);
340 }
341
342
343 /*************************
344 ** STATIC LOCAL FUNCTIONS
345 **************************/
346
347
348 /* Function to set the layout to SRC, ASM, SPLIT, NEXT, PREV, DATA, or
349 REGS. */
350 static void
351 tui_layout_command (const char *layout_name, int from_tty)
352 {
353 int i;
354 enum tui_layout_type new_layout = UNDEFINED_LAYOUT;
355 enum tui_layout_type cur_layout = tui_current_layout ();
356
357 if (layout_name == NULL)
358 error (_("Usage: layout prev | next | LAYOUT-NAME"));
359
360 std::string copy = layout_name;
361 for (i = 0; i < copy.size (); i++)
362 copy[i] = toupper (copy[i]);
363 const char *buf_ptr = copy.c_str ();
364
365 /* First check for ambiguous input. */
366 if (strlen (buf_ptr) <= 1 && *buf_ptr == 'S')
367 error (_("Ambiguous command input."));
368
369 if (subset_compare (buf_ptr, "SRC"))
370 new_layout = SRC_COMMAND;
371 else if (subset_compare (buf_ptr, "ASM"))
372 new_layout = DISASSEM_COMMAND;
373 else if (subset_compare (buf_ptr, "SPLIT"))
374 new_layout = SRC_DISASSEM_COMMAND;
375 else if (subset_compare (buf_ptr, "REGS"))
376 {
377 if (cur_layout == SRC_COMMAND
378 || cur_layout == SRC_DATA_COMMAND)
379 new_layout = SRC_DATA_COMMAND;
380 else
381 new_layout = DISASSEM_DATA_COMMAND;
382 }
383 else if (subset_compare (buf_ptr, "NEXT"))
384 new_layout = next_layout ();
385 else if (subset_compare (buf_ptr, "PREV"))
386 new_layout = prev_layout ();
387 else
388 error (_("Unrecognized layout: %s"), layout_name);
389
390 /* Make sure the curses mode is enabled. */
391 tui_enable ();
392 tui_set_layout (new_layout);
393 }
394
395
396 static void
397 extract_display_start_addr (struct gdbarch **gdbarch_p, CORE_ADDR *addr_p)
398 {
399 enum tui_layout_type cur_layout = tui_current_layout ();
400 struct gdbarch *gdbarch = get_current_arch ();
401 CORE_ADDR addr;
402 CORE_ADDR pc;
403 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
404
405 switch (cur_layout)
406 {
407 case SRC_COMMAND:
408 case SRC_DATA_COMMAND:
409 gdbarch = TUI_SRC_WIN->gdbarch;
410 find_line_pc (cursal.symtab,
411 TUI_SRC_WIN->start_line_or_addr.u.line_no,
412 &pc);
413 addr = pc;
414 break;
415 case DISASSEM_COMMAND:
416 case SRC_DISASSEM_COMMAND:
417 case DISASSEM_DATA_COMMAND:
418 gdbarch = TUI_DISASM_WIN->gdbarch;
419 addr = TUI_DISASM_WIN->start_line_or_addr.u.addr;
420 break;
421 default:
422 addr = 0;
423 break;
424 }
425
426 *gdbarch_p = gdbarch;
427 *addr_p = addr;
428 }
429
430
431 /* Answer the previous layout to cycle to. */
432 static enum tui_layout_type
433 next_layout (void)
434 {
435 int new_layout;
436
437 new_layout = tui_current_layout ();
438 if (new_layout == UNDEFINED_LAYOUT)
439 new_layout = SRC_COMMAND;
440 else
441 {
442 new_layout++;
443 if (new_layout == UNDEFINED_LAYOUT)
444 new_layout = SRC_COMMAND;
445 }
446
447 return (enum tui_layout_type) new_layout;
448 }
449
450
451 /* Answer the next layout to cycle to. */
452 static enum tui_layout_type
453 prev_layout (void)
454 {
455 int new_layout;
456
457 new_layout = tui_current_layout ();
458 if (new_layout == SRC_COMMAND)
459 new_layout = DISASSEM_DATA_COMMAND;
460 else
461 {
462 new_layout--;
463 if (new_layout == UNDEFINED_LAYOUT)
464 new_layout = DISASSEM_DATA_COMMAND;
465 }
466
467 return (enum tui_layout_type) new_layout;
468 }
469
470 /* Show the Source/Command layout. */
471 static void
472 show_source_command (void)
473 {
474 show_source_or_disasm_and_command (SRC_COMMAND);
475 }
476
477
478 /* Show the Dissassem/Command layout. */
479 static void
480 show_disasm_command (void)
481 {
482 show_source_or_disasm_and_command (DISASSEM_COMMAND);
483 }
484
485
486 /* Show the Source/Disassem/Command layout. */
487 static void
488 show_source_disasm_command (void)
489 {
490 int cmd_height, src_height, asm_height;
491
492 if (TUI_CMD_WIN != NULL)
493 cmd_height = TUI_CMD_WIN->height;
494 else
495 cmd_height = tui_term_height () / 3;
496
497 src_height = (tui_term_height () - cmd_height) / 2;
498 asm_height = tui_term_height () - (src_height + cmd_height);
499
500 if (TUI_SRC_WIN == NULL)
501 tui_win_list[SRC_WIN] = new tui_source_window ();
502 TUI_SRC_WIN->resize (src_height,
503 tui_term_width (),
504 0,
505 0);
506
507 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
508 gdb_assert (locator != nullptr);
509
510 TUI_SRC_WIN->show_source_content ();
511 if (TUI_DISASM_WIN == NULL)
512 tui_win_list[DISASSEM_WIN] = new tui_disasm_window ();
513 TUI_DISASM_WIN->resize (asm_height,
514 tui_term_width (),
515 0,
516 src_height - 1);
517 locator->resize (2 /* 1 */ ,
518 tui_term_width (),
519 0,
520 (src_height + asm_height) - 1);
521 TUI_DISASM_WIN->show_source_content ();
522
523 if (TUI_CMD_WIN == NULL)
524 tui_win_list[CMD_WIN] = new tui_cmd_window ();
525 TUI_CMD_WIN->resize (cmd_height,
526 tui_term_width (),
527 0,
528 tui_term_height () - cmd_height);
529 current_layout = SRC_DISASSEM_COMMAND;
530 }
531
532
533 /* Show the Source/Data/Command or the Dissassembly/Data/Command
534 layout. */
535 static void
536 show_data (enum tui_layout_type new_layout)
537 {
538 int total_height = (tui_term_height () - TUI_CMD_WIN->height);
539 int src_height, data_height;
540 enum tui_win_type win_type;
541
542 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
543 gdb_assert (locator != nullptr);
544
545 data_height = total_height / 2;
546 src_height = total_height - data_height;
547 if (tui_win_list[DATA_WIN] == nullptr)
548 tui_win_list[DATA_WIN] = new tui_data_window ();
549 tui_win_list[DATA_WIN]->resize (data_height, tui_term_width (), 0, 0);
550
551 if (new_layout == SRC_DATA_COMMAND)
552 win_type = SRC_WIN;
553 else
554 win_type = DISASSEM_WIN;
555
556 if (tui_win_list[win_type] == NULL)
557 {
558 if (win_type == SRC_WIN)
559 tui_win_list[win_type] = new tui_source_window ();
560 else
561 tui_win_list[win_type] = new tui_disasm_window ();
562 }
563
564 tui_win_list[win_type]->resize (src_height,
565 tui_term_width (),
566 0,
567 data_height - 1);
568 locator->resize (2 /* 1 */ ,
569 tui_term_width (),
570 0,
571 total_height - 1);
572 TUI_CMD_WIN->resize (TUI_CMD_WIN->height, tui_term_width (),
573 0, total_height);
574
575 current_layout = new_layout;
576 }
577
578 void
579 tui_gen_win_info::resize (int height_, int width_,
580 int origin_x_, int origin_y_)
581 {
582 if (width == width_ && height == height_
583 && origin.x == origin_x_ && origin.y == origin_y_
584 && handle != nullptr)
585 return;
586
587 width = width_;
588 height = height_;
589 if (height > 1)
590 viewport_height = height - 2;
591 else
592 viewport_height = 1;
593 origin.x = origin_x_;
594 origin.y = origin_y_;
595
596 if (handle != nullptr)
597 {
598 #ifdef HAVE_WRESIZE
599 wresize (handle, height, width);
600 mvwin (handle, origin.y, origin.x);
601 wmove (handle, 0, 0);
602 #else
603 tui_delete_win (handle);
604 handle = NULL;
605 #endif
606 }
607
608 if (handle == nullptr)
609 make_window ();
610
611 rerender ();
612 }
613
614 /* Show the Source/Command or the Disassem layout. */
615 static void
616 show_source_or_disasm_and_command (enum tui_layout_type layout_type)
617 {
618 struct tui_source_window_base *win_info;
619 int src_height, cmd_height;
620 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
621 gdb_assert (locator != nullptr);
622
623 if (TUI_CMD_WIN != NULL)
624 cmd_height = TUI_CMD_WIN->height;
625 else
626 cmd_height = tui_term_height () / 3;
627 src_height = tui_term_height () - cmd_height;
628
629 if (layout_type == SRC_COMMAND)
630 {
631 if (tui_win_list[SRC_WIN] == nullptr)
632 tui_win_list[SRC_WIN] = new tui_source_window ();
633 win_info = TUI_SRC_WIN;
634 }
635 else
636 {
637 if (tui_win_list[DISASSEM_WIN] == nullptr)
638 tui_win_list[DISASSEM_WIN] = new tui_disasm_window ();
639 win_info = TUI_DISASM_WIN;
640 }
641
642 locator->resize (2 /* 1 */ ,
643 tui_term_width (),
644 0,
645 src_height - 1);
646 win_info->resize (src_height - 1,
647 tui_term_width (),
648 0,
649 0);
650
651 win_info->show_source_content ();
652
653 if (TUI_CMD_WIN == NULL)
654 tui_win_list[CMD_WIN] = new tui_cmd_window ();
655 TUI_CMD_WIN->resize (cmd_height,
656 tui_term_width (),
657 0,
658 src_height);
659 current_layout = layout_type;
660 }
This page took 0.12495 seconds and 4 git commands to generate.