cmd-resize-pane.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include "tmux.h"
  20. /*
  21. * Increase or decrease pane size.
  22. */
  23. enum cmd_retval cmd_resize_pane_exec(struct cmd *, struct cmd_q *);
  24. void cmd_resize_pane_mouse_update(struct client *, struct mouse_event *);
  25. const struct cmd_entry cmd_resize_pane_entry = {
  26. .name = "resize-pane",
  27. .alias = "resizep",
  28. .args = { "DLMRt:Ux:y:Z", 0, 1 },
  29. .usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
  30. "[adjustment]",
  31. .tflag = CMD_PANE,
  32. .flags = 0,
  33. .exec = cmd_resize_pane_exec
  34. };
  35. enum cmd_retval
  36. cmd_resize_pane_exec(struct cmd *self, struct cmd_q *cmdq)
  37. {
  38. struct args *args = self->args;
  39. struct window_pane *wp = cmdq->state.tflag.wp;
  40. struct winlink *wl = cmdq->state.tflag.wl;
  41. struct window *w = wl->window;
  42. struct client *c = cmdq->client;
  43. struct session *s = cmdq->state.tflag.s;
  44. const char *errstr;
  45. char *cause;
  46. u_int adjust;
  47. int x, y;
  48. if (args_has(args, 'M')) {
  49. if (cmd_mouse_window(&cmdq->item->mouse, &s) == NULL)
  50. return (CMD_RETURN_NORMAL);
  51. if (c == NULL || c->session != s)
  52. return (CMD_RETURN_NORMAL);
  53. c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
  54. cmd_resize_pane_mouse_update(c, &cmdq->item->mouse);
  55. return (CMD_RETURN_NORMAL);
  56. }
  57. if (args_has(args, 'Z')) {
  58. if (w->flags & WINDOW_ZOOMED)
  59. window_unzoom(w);
  60. else
  61. window_zoom(wp);
  62. server_redraw_window(w);
  63. server_status_window(w);
  64. return (CMD_RETURN_NORMAL);
  65. }
  66. server_unzoom_window(w);
  67. if (args->argc == 0)
  68. adjust = 1;
  69. else {
  70. adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
  71. if (errstr != NULL) {
  72. cmdq_error(cmdq, "adjustment %s", errstr);
  73. return (CMD_RETURN_ERROR);
  74. }
  75. }
  76. if (args_has(self->args, 'x')) {
  77. x = args_strtonum(self->args, 'x', PANE_MINIMUM, INT_MAX,
  78. &cause);
  79. if (cause != NULL) {
  80. cmdq_error(cmdq, "width %s", cause);
  81. free(cause);
  82. return (CMD_RETURN_ERROR);
  83. }
  84. layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
  85. }
  86. if (args_has(self->args, 'y')) {
  87. y = args_strtonum(self->args, 'y', PANE_MINIMUM, INT_MAX,
  88. &cause);
  89. if (cause != NULL) {
  90. cmdq_error(cmdq, "height %s", cause);
  91. free(cause);
  92. return (CMD_RETURN_ERROR);
  93. }
  94. layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
  95. }
  96. if (args_has(self->args, 'L'))
  97. layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust);
  98. else if (args_has(self->args, 'R'))
  99. layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust);
  100. else if (args_has(self->args, 'U'))
  101. layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust);
  102. else if (args_has(self->args, 'D'))
  103. layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust);
  104. server_redraw_window(wl->window);
  105. return (CMD_RETURN_NORMAL);
  106. }
  107. void
  108. cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
  109. {
  110. struct winlink *wl;
  111. struct window_pane *wp;
  112. int found;
  113. u_int y, ly;
  114. wl = cmd_mouse_window(m, NULL);
  115. if (wl == NULL) {
  116. c->tty.mouse_drag_update = NULL;
  117. return;
  118. }
  119. y = m->y;
  120. if (m->statusat == 0 && y > 0)
  121. y--;
  122. else if (m->statusat > 0 && y >= (u_int)m->statusat)
  123. y = m->statusat - 1;
  124. ly = m->ly;
  125. if (m->statusat == 0 && ly > 0)
  126. ly--;
  127. else if (m->statusat > 0 && ly >= (u_int)m->statusat)
  128. ly = m->statusat - 1;
  129. found = 0;
  130. TAILQ_FOREACH(wp, &wl->window->panes, entry) {
  131. if (!window_pane_visible(wp))
  132. continue;
  133. if (wp->xoff + wp->sx == m->lx &&
  134. wp->yoff <= 1 + ly && wp->yoff + wp->sy >= ly) {
  135. layout_resize_pane(wp, LAYOUT_LEFTRIGHT, m->x - m->lx);
  136. found = 1;
  137. }
  138. if (wp->yoff + wp->sy == ly &&
  139. wp->xoff <= 1 + m->lx && wp->xoff + wp->sx >= m->lx) {
  140. layout_resize_pane(wp, LAYOUT_TOPBOTTOM, y - ly);
  141. found = 1;
  142. }
  143. }
  144. if (found)
  145. server_redraw_window(wl->window);
  146. else
  147. c->tty.mouse_drag_update = NULL;
  148. }