C语言 GTK4:菜单中的Pango标记

guykilcj  于 2023-08-03  发布在  Go
关注(0)|答案(1)|浏览(111)

要构建一个干净的GTK 4版本的我的程序(https://atomes.ipcms.fr),我需要能够在GTK 4菜单中添加pango标记,并且,菜单必须是C编码的,而不是从XML文件中读取。
这个问题遵循这篇文章中的一个问题(包含一个视觉插图):
https://discourse.gnome.org/t/gtk4-menus-and-pango-markup/8828
一年多以前,我问过同样的问题,得到了答案,等待了很长时间才在GTK 4库中更新,希望答案在某个时候能起作用......没有...
基本上,它在从XML文件阅读菜单时工作,而在直接编码菜单元素时则不工作,我真的,真的,必须这样做。
下面是我写的一段示例代码:

#include <gtk/gtk.h>

GtkApplication * TestApp = NULL;

void menu_bar_action (GSimpleAction * action, GVariant * parameter, gpointer data)
{
  gchar * name = g_strdup_printf ("%s", g_action_get_name(G_ACTION(action)));
  if (g_strcmp0 (name, "quit") == 0)
  {
    g_application_quit (G_APPLICATION(TestApp));
  }
}

GMenuItem * create_gmenu_item (const gchar * label, const gchar * action)
{
  GMenuItem * item;
  item = g_menu_item_new (label, action);
  // Requesting to use markup here:
  g_menu_item_set_attribute (item, "use-markup", "b", TRUE, NULL);
  return item;
}

void append_menu_item (GMenu * menu, const gchar * label, const gchar * action)
{
  GMenuItem * item = create_gmenu_item (label, action);
  g_menu_append_item (menu, item);
  g_object_unref (item);
}

void run_program (GApplication * app, gpointer data)
{
  GtkWidget * window = gtk_application_window_new (GTK_APPLICATION(app));
  gtk_window_set_title (GTK_WINDOW(window), "Test");
  gtk_window_set_resizable (GTK_WINDOW(window), TRUE);
  gtk_widget_set_size_request (window, 900, 450);

#ifdef UIMODE
  GtkBuilder * builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "test.ui", NULL);
  gtk_application_set_menubar (GTK_APPLICATION (app),
                               G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
  g_object_unref (builder);
#else
  GMenu * menubar = g_menu_new();
  GMenu * menu    = g_menu_new ();
  // Trying different things just in case:
  append_menu_item (menu, "C(H)<sub>2</sub>", "None");
  append_menu_item (menu, "C(H)&lt;sub&gt;3&lt;/sub&gt;", "None");
  append_menu_item (menu, "C(H)<sub>4</sub>", "None");
  append_menu_item (menu, "Quit", "app.quit");
    
  g_menu_append_submenu (menubar, "File", G_MENU_MODEL (menu));
  g_object_unref (menu);
    
  gtk_application_set_menubar (GTK_APPLICATION(app), G_MENU_MODEL(menubar));
#endif  
  GSimpleAction * act = g_simple_action_new ("quit", NULL);
  g_signal_connect (act, "activate", G_CALLBACK(menu_bar_action), NULL);
  g_action_map_add_action (G_ACTION_MAP(GTK_APPLICATION(app)), G_ACTION(act)); 
  gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW(window), TRUE);
  gtk_window_present (GTK_WINDOW(window)); 
}

int main (int argc, char *argv[])
{
  // setlocale(LC_ALL,"en_US");
  gtk_disable_setlocale ();
  TestApp = gtk_application_new (g_strdup_printf ("test._%d.gtk", (int)clock()), G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (G_OBJECT(TestApp), "activate", G_CALLBACK(run_program), NULL);
  int status = g_application_run (G_APPLICATION (TestApp), 0, NULL);
  g_object_unref (TestApp);
  return status;
}

字符串
以及菜单的'test.ui'可选XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <menu id="menubar">
    <submenu>
      <attribute name="label" translatable="yes">Test</attribute>
      <section>
        <item>
      <attribute name="label" translatable="yes">C(H)&lt;sub&gt;2&lt;/sub&gt;</attribute>
          <attribute name="use-markup">TRUE</attribute>
          <attribute name="action">None</attribute>
        </item>
        <item>
      <attribute name="label" translatable="yes">C(H)&lt;sub&gt;3&lt;/sub&gt;</attribute>
          <attribute name="use-markup">TRUE</attribute>
          <attribute name="action">None</attribute>
        </item>
        <item>
      <attribute name="label" translatable="yes">C(H)&lt;sub&gt;3&lt;/sub&gt;</attribute>
          <attribute name="use-markup">TRUE</attribute>
          <attribute name="action">None</attribute>
        </item>
        <item>
      <attribute name="label" translatable="yes">Quit</attribute>
          <attribute name="action">app.quit</attribute>
        </item>
      </section>
    </submenu>
  </menu>
</interface>


如果我使用以下方法构建代码:

#!/bin/bash

app=$1

LIBS="-Wl,--export-dynamic `pkg-config --libs gtk4`"
INCLUDES="`pkg-config --cflags gtk4`"
CFLAGS="-O2"
gcc -o $app'.x' $CFLAGS $INCLUDES $app'.c' $LIBS -DUIMODE


然后,从XML文件加载的菜单将正确使用pango标记。
但是如果我使用以下方法构建代码:

#!/bin/bash

app=$1

LIBS="-Wl,--export-dynamic `pkg-config --libs gtk4`"
INCLUDES="`pkg-config --cflags gtk4`"
CFLAGS="-O2"
gcc -o $app'.x' $CFLAGS $INCLUDES $app'.c' $LIBS


那就不要加价。
我在一个全新的Debian 12上用GTK 4.8.3进行了测试。
根据我的例子,你会明白我想显示化学式。
提前感谢如果你有任何想法来帮助!
最好的问候;- )

5ssjco0h

5ssjco0h1#

不提交一个布尔值,而是提交一个字符串作为属性。
而不是:

gmenu_item_set_attribute (item, "use-markup", "b", TRUE, NULL);

字符串
用途:

gmenu_item_set_attribute (item, "use-markup", "s", "TRUE", NULL);

相关问题