{"id":1347,"date":"2013-10-08T14:09:55","date_gmt":"2013-10-08T14:09:55","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1347"},"modified":"2020-04-27T12:02:39","modified_gmt":"2020-04-27T12:02:39","slug":"labels","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/gui-programming-in-c\/gtk\/labels\/labels","title":{"rendered":".Labels General"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Adding A Label At A Fixed Position<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); \t\t\/\/GTK_WINDOW_TOPLEVEL = Has a titlebar and border, managed by the window manager.\n\t\n\t\/\/Get screen size\n\tint ScreenWidth;\n\tint ScreenHeight;\n\tGdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(MainWindow));\n\tScreenWidth = gdk_screen_get_width(screen);\t\t\t\/\/ in pixels\n\tScreenHeight = gdk_screen_get_height(screen);\n\t\n\t\/\/Create a Fixed Container\n\tGtkWidget *fixed;\n\tfixed = gtk_fixed_new();\n\tgtk_widget_set_size_request(fixed, ScreenWidth, ScreenHeight);\n\tgtk_container_add(GTK_CONTAINER(MainWindow), fixed);\n\tgtk_widget_show(fixed);\n\t\n\t\/\/Add status label\n\tGtkWidget *StatusLabel;\n\tStatusLabel = gtk_label_new(NULL);\n\tgtk_label_set_markup(GTK_LABEL(StatusLabel), \"&lt;span foreground=\\\"white\\\" size=\\\"x-large\\\">Hello World&lt;\/span>\");\n\tgtk_label_set_justify(GTK_LABEL(StatusLabel), GTK_JUSTIFY_CENTER);\n\tgtk_fixed_put(GTK_FIXED(fixed), StatusLabel, 10, 20);\t\t\/\/x, y from top left\n\t\n\tgtk_widget_show_all(MainWindow);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Update Label<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>gtk_label_set_markup(GTK_LABEL(StatusLabel), \"&lt;span foreground=\\\"white\\\" size=\\\"x-large\\\">Helloooooooooooooo\\nWorld&lt;\/span>\");<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\t#define\tKEYBOARD_TEXT_SPAN_MARKUP\t\t\"&lt;span font_desc=\\\"Roboto\\\" foreground=\\\"black\\\" size=\\\"x-large\\\" weight=\\\"bold\\\" >\"\n\tchar temp_string&#91;512];\n\t\n\tstrcpy(temp_string, KEYBOARD_TEXT_SPAN_MARKUP);\n\tstrcat(temp_string, g_markup_escape_text(&amp;edit_text&#91;0], -1));\t\t\/\/g_markup_escape_text will escape any special (&amp;, &lt;, >) characters in your string \n\tstrcat(temp_string, \"&lt;\/span>\");\n\tgtk_label_set_markup(GTK_LABEL(lblKeyboardText), temp_string);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Styling Text<\/h4>\n\n\n\n<p>See the available Pango&nbsp;styling options&nbsp;<a href=\"http:\/\/www.gtk.org\/api\/2.6\/pango\/PangoMarkupFormat.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Setting A Font Name<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>gtk_label_set_markup(GTK_LABEL(StatusLabel), \"&lt;span font_desc=\\\"URW Palladio L 20\\\" foreground=\\\"white\\\" size=\\\"x-large\\\">Hello World&lt;\/span>\");<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Font Size<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tgtk_label_set_markup(GTK_LABEL(TimerLabel), \"&lt;span foreground=\\\"white\\\"  font_desc=\\\"40.0\\\">Hello World&lt;\/span>\");<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using Newly Installed Fonts<\/h5>\n\n\n\n<p>We originally copied new ttf fonts into ~\/.fonts (\/home\/pi\/.fonts ) and couldn&#8217;t use them in GTK. &nbsp;We moved them to \/usr\/share\/fonts\/truetype, rebooted and they worked fine, e.g.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ngtk_label_set_markup(GTK_LABEL(StatusLabel), \"&lt;span font_desc=\\\"Arcade Interlaced 20\\\" foreground=\\\"white\\\" size=\\\"x-large\\\">Hello World&lt;\/span>\");\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Multi Line<\/h4>\n\n\n\n<p>Just use newline &#8220;\\n&#8221; in the string.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Alignment<\/h4>\n\n\n\n<p>Centre align a label:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tgtk_widget_set_size_request(lblStatus, 200, 0);\t\t\t\t\/\/Set width of the label (so centre align will work)\n\tgtk_widget_set_halign(lblStatus, GTK_ALIGN_CENTER);\n\tgtk_fixed_put(GTK_FIXED(fixed), lblStatus, 396, 76);\t\t\/\/x, y from top left<\/code><\/pre>\n\n\n\n<p>Alignment of multi line labels<\/p>\n\n\n\n<p>gtk_label_set_justify() sets the alignment of the lines in the text of the label relative to each other. &nbsp; It has no effect on labels containing only a single line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tgtk_label_set_justify(GTK_LABEL(StatusLabel), GTK_JUSTIFY_CENTER);\t\t\/\/Only sets alignment of multiple lines<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete label<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tgtk_widget_destroy(lblStatus);\n\tlblStatus = NULL;<\/code><\/pre>\n\n\n\n<p>&nbsp; You can safely used destroy on a widget that hasn&#8217;t actually been displayed yet. &nbsp;You can re-create and place the widget again later after it has previously been destroyed, you&#8217;re just removing its current usage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding A Label At A Fixed Position Update Label Styling Text See the available Pango&nbsp;styling options&nbsp;here. Setting A Font Name Font Size Using Newly Installed Fonts We originally copied new ttf fonts into ~\/.fonts (\/home\/pi\/.fonts ) and couldn&#8217;t use them in GTK. &nbsp;We moved them to \/usr\/share\/fonts\/truetype, rebooted and they worked fine, e.g. Multi Line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109],"tags":[],"class_list":["post-1347","post","type-post","status-publish","format-standard","hentry","category-labels"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1347"}],"version-history":[{"count":21,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1347\/revisions"}],"predecessor-version":[{"id":3224,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1347\/revisions\/3224"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}