shithub: orca

Download patch

ref: 1c62d533b7ea900881c9b027094a97945839f84d
parent: 6a383f19c0714f80605b1f8345533d59bcff56fe
author: cancel <cancel@cancel.fm>
date: Mon Jan 6 02:10:48 EST 2020

Clean up menu/block border layout logic

--- a/term_util.c
+++ b/term_util.c
@@ -82,6 +82,8 @@
   Usz items_cap;
   ITEM* initial_item;
   int id;
+  // Flag for right-padding hack. Temp until we do our own menus
+  bool has_submenu_item : 1;
 };
 
 struct Qform {
@@ -103,7 +105,8 @@
   while (qnav_stack.count != 0)
     qnav_stack_pop();
 }
-void qnav_stack_push(Qblock* qb, int height, int width) {
+static ORCA_FORCE_NO_INLINE void qnav_stack_push(Qblock* qb, int height,
+                                                 int width) {
 #ifndef NDEBUG
   for (Usz i = 0; i < qnav_stack.count; ++i) {
     assert(qnav_stack.blocks[i] != qb);
@@ -110,7 +113,7 @@
   }
 #endif
   int top = 0, left = 0;
-  int total_h = height + 2, total_w = width + 3;
+  int total_h = height + 2, total_w = width + 2;
   if (qnav_stack.count > 0) {
     WINDOW* w = qnav_stack.blocks[qnav_stack.count - 1]->outer_window;
     int prev_y, prev_x, prev_h, prev_w;
@@ -278,7 +281,8 @@
   }
   if (curlinewidth > maxlinewidth)
     maxlinewidth = curlinewidth;
-  int width = titlewidth > maxlinewidth ? titlewidth + 1 : maxlinewidth + 1;
+  int width = titlewidth > maxlinewidth ? titlewidth : maxlinewidth;
+  width += 2; // 1 padding on left and right each
   Qmsg* msg = qmsg_push(lines, width); // no wrapping yet, no real wcwidth, etc
   WINDOW* msgw = qmsg_window(msg);
   int i = 0;
@@ -320,6 +324,7 @@
   qm->items_cap = 0;
   qm->initial_item = NULL;
   qm->id = id;
+  qm->has_submenu_item = false;
   return qm;
 }
 void qmenu_destroy(Qmenu* qm) { qmenu_free(qm); }
@@ -387,6 +392,19 @@
   extras[0].owns_string = false;
   extras[0].is_spacer = false;
 }
+void qmenu_add_submenu(Qmenu* qm, int id, char const* text) {
+  assert(id != 0);
+  qm->has_submenu_item = true; // don't add +1 right padding to subwindow
+  Usz idx;
+  ITEM** items;
+  struct Qmenu_item_extra* extras;
+  qmenu_allocitems(qm, 1, &idx, &items, &extras);
+  items[0] = new_item(text, ">");
+  set_item_userptr(items[0], (void*)(uintptr_t)idx);
+  extras[0].user_id = id;
+  extras[0].owns_string = false;
+  extras[0].is_spacer = false;
+}
 void qmenu_add_printf(Qmenu* qm, int id, char const* fmt, ...) {
   va_list ap;
   va_start(ap, fmt);
@@ -465,11 +483,12 @@
   set_menu_grey(qm->ncurses_menu, A_DIM);
   int menu_min_h, menu_min_w;
   scale_menu(qm->ncurses_menu, &menu_min_h, &menu_min_w);
+  if (!qm->has_submenu_item) menu_min_w += 1; // temp hack
   if (qm->qblock.title) {
     // Stupid lack of wcswidth() means we can't know how wide this string is
     // actually displayed. Just fake it for now, until we have Unicode strings
     // in the UI. Then we get sad.
-    int title_w = (int)strlen(qm->qblock.title) + 1;
+    int title_w = (int)strlen(qm->qblock.title) + 2;
     if (title_w > menu_min_w)
       menu_min_w = title_w;
   }
--- a/term_util.h
+++ b/term_util.h
@@ -135,6 +135,7 @@
 int qmenu_id(Qmenu const* qm);
 void qmenu_set_title(Qmenu* qm, char const* title);
 void qmenu_add_choice(Qmenu* qm, int id, char const* text);
+void qmenu_add_submenu(Qmenu* qm, int id, char const* text);
 #ifdef __GNUC__
 __attribute__((format(printf, 3, 4)))
 #endif
--- a/tui_main.c
+++ b/tui_main.c
@@ -1911,10 +1911,10 @@
   qmenu_add_spacer(qm);
   qmenu_add_choice(qm, Main_menu_set_tempo, "Set BPM...");
   qmenu_add_choice(qm, Main_menu_set_grid_dims, "Set Grid Size...");
-  qmenu_add_choice(qm, Main_menu_autofit_grid, "Auto-fit Grid");
+  qmenu_add_submenu(qm, Main_menu_autofit_grid, "Auto-fit Grid");
   qmenu_add_spacer(qm);
 #ifdef FEAT_PORTMIDI
-  qmenu_add_choice(qm, Main_menu_choose_portmidi_output, "MIDI Output...");
+  qmenu_add_submenu(qm, Main_menu_choose_portmidi_output, "MIDI Output");
   qmenu_add_spacer(qm);
 #endif
   qmenu_add_choice(qm, Main_menu_controls, "Controls...");
@@ -2043,7 +2043,7 @@
     }
   }
   int mid_pad = 2;
-  int total_width = 1 + w_input + mid_pad + w_desc;
+  int total_width = 1 + w_input + mid_pad + w_desc + 1;
   Qmsg* qm = qmsg_push(ORCA_ARRAY_COUNTOF(items), total_width);
   qmsg_set_title(qm, "Controls");
   WINDOW* w = qmsg_window(qm);
@@ -2112,7 +2112,8 @@
   }
   int left_pad = 1;
   int mid_pad = 1;
-  int total_width = left_pad + 1 + mid_pad + w_desc;
+  int right_pad = 1;
+  int total_width = left_pad + 1 + mid_pad + w_desc + right_pad;
   Qmsg* qm = qmsg_push(ORCA_ARRAY_COUNTOF(items), total_width);
   qmsg_set_title(qm, "Operators");
   WINDOW* w = qmsg_window(qm);