How to send GtkWidgets to the back or front? - c

I wonder if there is a way to "Send to back" or "Bring to front" a GtkWidget dynamically. ( I know, it can be done by changing the creating order of GtkWidget )
example : (GTK+ 2.0, Cent OS 7.0, C)
#include <gtk/gtk.h>
int main( int argc, char *argv[] )
{
GtkWidget *window, *button1, *button2, *fixed;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
button1 = gtk_button_new_with_label("A button");
button2 = gtk_button_new_with_label("B button");
fixed = gtk_fixed_new();
gtk_fixed_put( (GtkFixed*)fixed,button2, 30, 30 );
gtk_fixed_put( (GtkFixed*)fixed,button1, 50, 50 );
gtk_container_add(GTK_CONTAINER(window), fixed);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
this program will display : (the first picture)
Are there some commands that can make the "B Button" be in front of "A Button". ( look likes this following picture --> the second picture )

No, there aren't "functions" like those in a GtkFixed.
From the Gtk 2 documentation:
Description
The GtkFixed widget is a container which can place child widgets at
fixed positions and with fixed sizes, given in pixels. GtkFixed
performs no automatic layout management.
For most applications, you should not use this container! It keeps you
from having to learn about the other GTK+ containers, but it results
in broken applications. With GtkFixed, the following things will
result in truncated text, overlapping widgets, and other display bugs:
Themes, which may change widget sizes.
Fonts other than the one you used to write the app will of course change the size of widgets containing text; keep in mind that users
may use a larger font because of difficulty reading the default, or
they may be using Windows or the framebuffer port of GTK+, where
different fonts are available.
Translation of text into other languages changes its size. Also, display of non-English text will use a different font in many cases.
In addition, the fixed widget can't properly be mirrored in
right-to-left languages such as Hebrew and Arabic. i.e. normally GTK+
will flip the interface to put labels to the right of the thing they
label, but it can't do that with GtkFixed. So your application will
not be usable in right-to-left languages.
Finally, fixed positioning makes it kind of annoying to add/remove GUI
elements, since you have to reposition all the other elements. This is
a long-term maintenance problem for your application.
If you know none of these things are an issue for your application,
and prefer the simplicity of GtkFixed, by all means use the widget.
But you should be aware of the tradeoffs.
The order by which they are added is the order they will keep in the virtual Z axis. You can control this by removing and readding them or other similar approaches.

Related

How to make multiple views in GTK application

I want to make a GTK+ application with multiple views and I don't really know how to achieve it in the best possible way. In every view i need to have some labels and buttons. Firstly I tried to make the appliaction with GtkStack, but StackSwitcher has poor customization options (its buttons are in row and stackswitcher icon's are too small, even with maximum possible size with “icon-size” property).
Connecting stack's page switching with normal buttons would solve the case, but i have no idea how to do such a thing.
My second approach was with multiple windows. I was able to make a few windows and hiding/showing them with buttons. Unfortunately the app will be working on quite bad pc (what's more pc is connected to touchscreen what make it's performance even worse) and after some tests I can say that app has some lags. The whole thing makes all windows in the beggining and then just hide them or show them (depends on what button on which window was pressed).
To sum up my question:
what is the most optimal way to make such an application? With multiple windows or with GtkStack?
If with windows how to optimize the whole thing?
if with stack how to implement switchng stack's tab on normal buttons?
I prefer GtkStack. It has awesome gtk_stack_set_visible_child_name, which let's you set visible child by it's ID. In the following snippet I use GtkListBox for switching (and I had to store a GPtrArray with child names)
static void
row_activated (GtkListBox *box,
GtkListBoxRow *row,
gpointer udata)
{
MyWid *self = udata;
MyWidPrivate *priv = self->priv;
gint row_index = gtk_list_box_row_get_index (row);
gchar *path = g_ptr_array_index (priv->paths, row_index);
gtk_stack_set_visible_child_name (priv->stack, path);
}
If you want to use GtkButton thing are even more simple:
gchar *id; // just a string, that allows you to connect buttons and tabs
GtkWidget *child, *button;
child = create_tab_for_id (id); // not a real function! You should define it yourself
gtk_stack_add_named (stack, child, id);
button = create_button_for_id (id); // also not a real function
/* Time for magic */
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_stack_set_visible_child_name),
stack);

GTK3 Set GtkButton size

I have a very simple code, wich create a GtkWindow and place in it a GtkButton.
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(_window, 800, 450);
gtk_window_set_decorated(GTK_WINDOW (_window), FALSE);
gtk_window_set_position(GTK_WINDOW (_window),GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_resizable(GTK_WINDOW (_window), FALSE);
_startbutton = gtk_button_new_with_label("myLabel");
gtk_container_add(GTK_CONTAINER(_window), _startbutton);
gtk_widget_show_all(_window);
Yet, this doesn't work as expected because the button fills the whole window.
I tried to find a way to change the button size, but all the methods that i found use some methods that are deprecated...
Can someone explain to me the way to do this ?
Because the GtkButton is the only control in the GtkWindow, it will be given the entire area of the GtkWindow to fill. If you want to do anything more complicated, you will need to use layout containers like GtkBox and GtkGrid to explicitly lay out the button, usually in relation to other controls that you will also have in the window.
Once you do lay out your controls, you can use expansion and alignment to control how the button makes use of its allotted space.

Change GTK Label position in window - C

I am currently coding in C for linux and I need a GUI, so I took the GTK library to do so . I now have a window with a label (to begin with) but I don't know how to move it (in (x,y) coordinates, not in zPosition) to put it at the top of the window.
Here is my GTK code in main()
//Label
pLabel=gtk_label_new(NULL);
sUtf8 = g_locale_to_utf8("<span font_desc=\"Times New Roman italic 12\" foreground=\"#0000FF\">Neural Network - XOR Example</span>\n"
,-1, NULL, NULL, NULL);
gtk_label_set_markup(GTK_LABEL(pLabel), sUtf8);
g_free(sUtf8);
gtk_label_set_justify(GTK_LABEL(pLabel), GTK_JUSTIFY_CENTER);
//Window
gtk_window_set_position(GTK_WINDOW(pWindow), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(pWindow), "Neural Network");
gtk_window_set_default_size(GTK_WINDOW(pWindow), 900, 600);
gtk_container_add(GTK_CONTAINER(pWindow), pLabel);
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(OnDestroy), NULL);
gtk_widget_show_all(pWindow);
gtk_main();
Has anybody an idea on how to move it ?
Thank you!
Here is what I got:
(source: hostingpics.net)
carl gave you part of the answer.
As to why you see what you have, there are two parts:
First, GtkLabel has an archaic alignment mechanism it inherits from the deprecated GtkMisc class. See GtkMisc's documentation for details: the xalign and yalign properties control the position of the text within the label's allocation, and it's set to centered by default. But remember that they are deprecated, so there's usually better alternatives.
Second, you can only have one control in a GtkWindow. This control gets allocated the entire space of the window.
Put these two facts together and you should see why you have what you have.
So what is correct instead?
You have to use a container, such as GtkBox or GtkGrid, to get more than one widget into a GtkWindow. Each widget carries with it four special properties: hexpand, halign, vexpand, and valign which control the position and size of each member of the container. These are properties of the widgets themselves.
hexpand and vexpand determine whether or not the container allocates whatever space is left over to this control in the X and Y directions, respectively. If multiple widgets have expand set, the space is divided evenly.
halign and valign determine the position of a widget in its allocation. GTK_ALIGN_START, GTK_ALIGN_CENTER, and GTK_ALIGN_END put the widget at the start, middle, and end of the given alignment side, respectively. GTK_ALIGN_FILL fills the widget to fit the allocation.
This page has more information.
If you're trying to do exact positioning of controls, you should first investigate if GtkGrid and GtkBox (applied recursively) provide what you want. An important thing about the default internal centering of GtkLabels is that a GtkLabel in a GtkGrid will already be vertically aligned to its neighbor's text; just set halign to start or end to get the label left-aligned or right-aligned (and set valign to start if its neighbor is something big like a table). There are also functions that provide margins and padding to make the UI look nice.
If you absolutely must position things yourself, however, keep everything in mind.

GTK3 - textview transparency to show applications behind

I am currently working on an existing Linux application called Xpad, which is a sticky notes application written in C. I try to implement transparency but I have a hard time achieving what I want. A sticky note looks like this.
I have a (toplevel) gtk_window with a gtk_textview.
If the background color of the textview is set with the function gtk_widget_override_background_color() to a transparant color (GdkRGBA where alpha value is smaller than 1), the color of the gtk_window behind it, becomes more visible.
However, I don't want to see the gtk_window, but the applications behind the gtk_window, such as the browser, libreoffice, or the desktop.
If the gtk_window is set to a certain transparancy, either with gtk_widget_override_background_color() or with gtk_widget_set_opacity(), the whole widget, including the window decorations become (partially) transparent.
To make the relations between the visible objects more clear, I have created a diagram of the different parts, and marked the place where I believe the transparency issue is taking place.
Anybody have any ideas how to make the textview background transparent, without making the window decorations transparent, so I can see whatever is behind this application?
Set a proper RGBA visual for the widget
w = //some GtkWidget or GtkWidget derived klass (i.e. GtkWindow)
gtk_widget_set_app_paintable (w, TRUE); // important or you will get solid color
// the next 3 lines should be wrapped in a func which is also hooked to "screen-changed"
GdkScreen *screen = gtk_widget_get_screen (w);
GdkVisual *visual = gdk_screen_get_rgba_visual (screen);
gtk_widget_set_visual(w, visual);
gtk_widget_show_all(w);
g_signal_connect(G_OBJECT(w), "screen-changed", G_CALLBACK(screen_changed_contaniing_above_code), NULL);

How to make a Gtk+ widget inside a GtkScrolledWindow to expand when packed into a GtkPane?

I have a program which shows two GtkTreeViews packed inside a GtkPaned (sscce: here):
gtk_paned_add1(GTK_PANED(paned), tree_view1);
gtk_paned_add2(GTK_PANED(paned), tree_view2);
The result is the following:
However, the tables can become bigger, so I added then to GtkScrolledWindows (sscce: here):
GtkWidget *scrolled_window1 = gtk_scrolled_window_new(NULL, NULL),
*scrolled_window2 = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scrolled_window1), tree_view1);
gtk_container_add(GTK_CONTAINER(scrolled_window2), tree_view2);
gtk_paned_add1(GTK_PANED(paned), scrolled_window1);
gtk_paned_add2(GTK_PANED(paned), scrolled_window2);
However, now the window collapses itself to the point it is almost a thin trance, as in the screenshot below:
If I maximize the window, the first column does not appear (although I can manually expand it):
So, what is the best method of getting the appearance of the first screenshot wen using GtkScrolledWindows in this scenario? Also, could I define the size of the pane columns in relation to one another (for example, 30% for the first one, 70% for the second one?
I've been using this pattern (I'd be happy if someone posted a better answer):
GtkWidget* treeView_, pane1_;
// [Removed code to create and show the widget heirarchy.]
GtkRequisition sizeReq;
gtk_widget_size_request(treeView_, &sizeReq); // get tree's preferred size
gtk_paned_set_position(GTK_PANED(pane1_), sizeReq.width);
This gets pretty close, but you'll probably have a horizontal scrollbar with a few hidden pixels. Since our application remembers the user's adjustments to the pane positions, it only needs to look "good enough" on initial layout.
Also, there are requirements for gtk_widget_size_request() returning something meaningful. In my case, I've invoked a gtk_widget_show_all() on the hierarchy before retrieving the size request.
The solution I adopted was to actually set the size of the scrolled windows (sscce):
gtk_widget_set_size_request(scrolled_window1, 200, 600);
gtk_widget_set_size_request(scrolled_window2, 600, 600);
The result was, as one would expect, a window with 800x600:
I am not satisfied: this approach relies on arbitrary sizes and it seems too "manual". Nonetheless, I want to use my software so I will adopt it for now.

Resources