diff options
author | Robert Tari <robert@tari.in> | 2021-06-14 23:08:17 +0200 |
---|---|---|
committer | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2021-06-16 08:26:44 +0200 |
commit | 9be11d94c663ea66cd41a9364a78531537ae405e (patch) | |
tree | 51f125b8c5b0560b1ed44668591f6cf3f7b7e5f0 /src | |
parent | db18ded6c8a4ab9d4a0921a3f52e8f3507607238 (diff) | |
download | libayatana-common-9be11d94c663ea66cd41a9364a78531537ae405e.tar.gz libayatana-common-9be11d94c663ea66cd41a9364a78531537ae405e.tar.bz2 libayatana-common-9be11d94c663ea66cd41a9364a78531537ae405e.zip |
Add ability to ellipsize dynamic menu item lengths.
Plus making the maximum length of non-ellipsized strings configurable
via GSettings.
- data/org.ayatana.common.gschema.xml.in: Add file.
- data/CMakeLists.txt: Add file.
- CMakeLists.txt: Add gio-2.0 dependency + 'data' build folder.
- src/utils.*: Add ayatana_common_utils_elipsize function + include
glib-object.h and gio.h
- tests/tst_utils.cpp: Add StringFunctionsTest.
- tests/CMakeLists.txt: Add GLIB_LIBRARIES to target.
Diffstat (limited to 'src')
-rw-r--r-- | src/utils.c | 26 | ||||
-rw-r--r-- | src/utils.h | 4 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index d735172..1d21b02 100644 --- a/src/utils.c +++ b/src/utils.c @@ -273,3 +273,29 @@ ayatana_common_utils_zenity_warning (const char * icon_name, g_free (zenity); return confirmed; } + +void ayatana_common_utils_elipsize(char *sText) +{ + guint nMaxLetters = 50; + glong nLetters = g_utf8_strlen(sText, -1); + GSettingsSchemaSource *pSource = g_settings_schema_source_get_default(); + + if (pSource != NULL) + { + GSettingsSchema *pSchema = g_settings_schema_source_lookup(pSource, "org.ayatana.common", FALSE); + + if (pSchema != NULL) + { + g_settings_schema_unref(pSchema); + GSettings *pSettings = g_settings_new("org.ayatana.common"); + nMaxLetters = g_settings_get_uint(pSettings, "max-menu-text-length"); + g_object_unref(pSettings); + } + } + + if (nLetters > nMaxLetters + 4) + { + gchar *pLastChar = g_utf8_offset_to_pointer(sText, nMaxLetters); + memcpy(pLastChar, "...\0", 4); + } +} diff --git a/src/utils.h b/src/utils.h index 0fa643b..13e2d2f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,6 +19,8 @@ #pragma once #include <glib.h> +#include <glib-object.h> +#include <gio/gio.h> #define DESKTOP_LOMIRI "Lomiri" #define DESKTOP_UNITY "Unity" @@ -47,3 +49,5 @@ gboolean ayatana_common_utils_execute_command(const gchar * cmd); gboolean ayatana_common_utils_open_url(const gchar * url); gboolean ayatana_common_utils_have_program(const gchar * program); gboolean ayatana_common_utils_zenity_warning(const char *icon_name, const char *title, const char *text); + +void ayatana_common_utils_elipsize(char *sText); |