From a61544ea9c9b895bdaba723d8898639bd1cace17 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 17 Apr 2016 10:01:44 +0000 Subject: [PATCH] Fix empty space at the end of the tooltips. Also, bring MakeAcceleratorLabel to modernity. --- src/graphicswin.cpp | 50 ++++++++++++++++++------------------------- src/solvespace.h | 2 +- src/textwin.cpp | 2 +- src/toolbar.cpp | 11 +++++----- src/win32/w32main.cpp | 8 +++---- 5 files changed, 31 insertions(+), 42 deletions(-) diff --git a/src/graphicswin.cpp b/src/graphicswin.cpp index bca2ab5..50b7c25 100644 --- a/src/graphicswin.cpp +++ b/src/graphicswin.cpp @@ -175,39 +175,31 @@ const GraphicsWindow::MenuEntry GraphicsWindow::menu[] = { #undef TC #undef TR -bool SolveSpace::MakeAcceleratorLabel(int accel, char *out) { - if(!accel) { - out[0] = '\0'; - return false; +std::string SolveSpace::MakeAcceleratorLabel(int accel) { + if(!accel) return ""; + + std::string label; + if(accel & GraphicsWindow::CTRL_MASK) { + label += "Ctrl+"; + } + if(accel & GraphicsWindow::SHIFT_MASK) { + label += "Shift+"; } - - const char *ctrl = accel & GraphicsWindow::CTRL_MASK ? "Ctrl+" : ""; - const char *shift = accel & GraphicsWindow::SHIFT_MASK ? "Shift+" : ""; - - char buf[8]; - buf[0] = (char)(accel & 0xff); - buf[1] = '\0'; - if(accel >= GraphicsWindow::FUNCTION_KEY_BASE + 1 && accel <= GraphicsWindow::FUNCTION_KEY_BASE + 12) { - sprintf(buf, "F%d", accel - GraphicsWindow::FUNCTION_KEY_BASE); + label += ssprintf("F%d", accel - GraphicsWindow::FUNCTION_KEY_BASE); + } else if(accel == '\t') { + label += "Tab"; + } else if(accel == ' ') { + label += "Space"; + } else if(accel == GraphicsWindow::ESCAPE_KEY) { + label += "Esc"; + } else if(accel == GraphicsWindow::DELETE_KEY) { + label += "Del"; + } else { + label += (char)(accel & 0xff); } - - const char *key = buf; - - switch(accel) { - case '\t': key = "Tab"; break; - case ' ': key = "Space"; break; - - case GraphicsWindow::ESCAPE_KEY: key = "Esc"; break; - case GraphicsWindow::DELETE_KEY: key = "Del"; break; - } - - if(key[0] < '!' || key[0] > '~') oops(); - if(key[0] >= 'a' && key[0] <= 'z') oops(); - - sprintf(out, "%s%s%s", ctrl, shift, key); - return true; + return label; } void GraphicsWindow::Init(void) { diff --git a/src/solvespace.h b/src/solvespace.h index 8786e36..910cdfc 100644 --- a/src/solvespace.h +++ b/src/solvespace.h @@ -388,7 +388,7 @@ void MakeMatrix(double *mat, double a11, double a12, double a13, double a14, double a21, double a22, double a23, double a24, double a31, double a32, double a33, double a34, double a41, double a42, double a43, double a44); -bool MakeAcceleratorLabel(int accel, char *out); +std::string MakeAcceleratorLabel(int accel); bool FilenameHasExtension(const std::string &str, const char *ext); void Message(const char *str, ...); void Error(const char *str, ...); diff --git a/src/textwin.cpp b/src/textwin.cpp index 2cb1798..78e4308 100644 --- a/src/textwin.cpp +++ b/src/textwin.cpp @@ -447,7 +447,7 @@ void TextWindow::DrawOrHitTestIcons(int how, double mx, double my) double ox = oldMousePos.x, oy = oldMousePos.y - LINE_HEIGHT; ox += 3; oy -= 3; - int tw = (str.length() + 1)*CHAR_WIDTH; + int tw = (str.length() + 1)*(CHAR_WIDTH - 1); ox = min(ox, (double) (width - 25) - tw); oy = max(oy, 5.0); diff --git a/src/toolbar.cpp b/src/toolbar.cpp index 0860dd5..84a36ec 100644 --- a/src/toolbar.cpp +++ b/src/toolbar.cpp @@ -206,20 +206,19 @@ bool GraphicsWindow::ToolbarDrawOrHitTest(int mx, int my, // Do this last so that nothing can draw over it. if(toolTip.show) { ssglInitializeBitmapFont(); - if(strlen(toolTip.str) >= 200) oops(); - std::string str { toolTip.str }; + std::string str = toolTip.str; for(i = 0; SS.GW.menu[i].level >= 0; i++) { if(toolbarTooltipped == SS.GW.menu[i].id) { - char accelbuf[40]; - if(MakeAcceleratorLabel(SS.GW.menu[i].accel, accelbuf)) { - str += ssprintf(" (%s)", accelbuf); + std::string accel = MakeAcceleratorLabel(SS.GW.menu[i].accel); + if(!accel.empty()) { + str += ssprintf(" (%s)", accel.c_str()); } break; } } - int tw = str.length() * SS.TW.CHAR_WIDTH + 10, + int tw = str.length() * (SS.TW.CHAR_WIDTH - 1) + 10, th = SS.TW.LINE_HEIGHT + 2; double ox = toolbarMouseX + 3, oy = toolbarMouseY + 3; diff --git a/src/win32/w32main.cpp b/src/win32/w32main.cpp index e206dad..dc50253 100644 --- a/src/win32/w32main.cpp +++ b/src/win32/w32main.cpp @@ -1208,11 +1208,9 @@ HMENU CreateGraphicsWindowMenus(void) for(i = 0; SS.GW.menu[i].level >= 0; i++) { std::string label; if(SS.GW.menu[i].label) { - char accelbuf[40]; - const char *sep = - MakeAcceleratorLabel(SS.GW.menu[i].accel, accelbuf) ? - "\t" : ""; - label = ssprintf("%s%s%s", SS.GW.menu[i].label, sep, accelbuf); + std::string accel = MakeAcceleratorLabel(SS.GW.menu[i].accel); + const char *sep = accel.empty() ? "" : "\t"; + label = ssprintf("%s%s%s", SS.GW.menu[i].label, sep, accel.c_str()); } if(SS.GW.menu[i].level == 0) {