How to create a Gtk combobox with the interface new_with_area? - gtk

According to the gtk documentation you can create a combobox with:
GtkWidget * gtk_combo_box_new ()
GtkWidget * gtk_combo_box_new_with_entry ()
GtkWidget * gtk_combo_box_new_with_model ()
GtkWidget * gtk_combo_box_new_with_model_and_entry ()
GtkWidget * gtk_combo_box_new_with_area ()
GtkWidget * gtk_combo_box_new_with_area_and_entry ()
I have found a lot of examples for the gtk_combo_box_new_with_model but I can't find anything related to the use of gtk_combo_box_new_with_area .
The langage used doesn't matter.

something like (pygobject):
from gi.repository import Gtk
area = Gtk.CellAreaBox()
combo = Gtk.ComboBox.new_with_area(area=area)
cell = Gtk.CellRendererText()
area.pack_start(cell, True, True, True)
you can add more CellRenderers to the box (which is a Gtk.Box) and do whatever you need to do with those.

Related

How to Check If an UI5 Control Is Rendered?

I am opening a popover on a specific control inside a FlexColumnLayout. Depending on the screensize it will not be rendered once the mid column is expanded.
I'd like to check if the cotrol is rendered before opening the popover, but i cant seem to find a property that allows that.
Already tried the solutions of this post:
Check if a control is currently rendered and visible
If you want to test around in actuall code just use the demo App of the FlexColumnLayout and try to open a popup on one of the hidden buttons when the begin column is expanded.
Thanks for your help, Eric
Edit 27.08.18 (code in question):
Controller:
/**
* Listner. Triggered when help is canceled.
* Closes popover.
* #author WN00096217 (Eric Schuster)
* #memberof xxxxxxxxxxxx
* #function onHelpCancel
*/
onHelpCancel: function () {
var iHelp = this._oHelpModel.getProperty("/counter");
this._oHelpModel.setProperty("/counter", 0);
this._oHelpModel.getProperty("/p" + iHelp).close();
},
/**
* Listner. Triggered when help is continue.
* Closes popover, opens next popover.
* #author WN00096217 (Eric Schuster)
* #memberof xxxxxxxxxxxxxxxxxxx
* #function onHelpNext
*/
onHelpNext: function () {
var iHelp = this._oHelpModel.getProperty("/counter");
this._oHelpModel.setProperty("/counter", iHelp + 1);
this._oHelpModel.getProperty("/p" + iHelp).close();
this._oHelpModel.getProperty("/p" + (iHelp + 1)).openBy(this._oHelpModel.getProperty("/c" + (iHelp + 1)));
},
controller (part of innit):
this._oHelpModel.setProperty("/c0", this._oView.byId("xxxxx"));
this._oHelpModel.setProperty("/c1", this._oView.byId("xxxx"));
this._oHelpModel.setProperty("/c2", this._oView.byId("xxxxx"));
this._oHelpModel.setProperty("/c3", this._oView.byId("xxxxxx"));
this._oHelpModel.setProperty("/c4", this._oView.byId("xxxxxx"));
this._oHelpModel.setProperty("/c5", this._oView.byId("xxxxx"));
this._oHelpModel.setProperty("/c6", this._oView.byId("xxxxx"));
this._oHelpModel.setProperty("/c7", this._oView.byId("xxxxxx"));
this._oHelpModel.setProperty("/c8", this._oView.byId("xxxxx"));
Component (part of innit):
//p eq popover c eq controll
var oHelpModel = new JSONModel({
counter: 0,
p0: null,
c0: null,
p1: null,
c1: null,
p2: null,
c2: null,
p3: null,
c3: null,
p4: null,
c4: null,
p5: null,
c5: null,
p6: null,
c6: null,
p7: null,
c7: null,
p8: null,
c8: null
});
this.setModel(oHelpModel, "helpModel");
What i d like the controler to look like:
/**
* Listner. Triggered when help is continue.
* Closes popover, opens next popover.
* #author WN00096217 (Eric Schuster)
* #memberof xxxxxxxxxxxxxxxxx
* #function onHelpNext
*/
onHelpNext: function () {
var iHelp = this._oHelpModel.getProperty("/counter");
if("control is rendered"){
this._oHelpModel.setProperty("/counter", iHelp + 1);
this._oHelpModel.getProperty("/p" + iHelp).close();
this._oHelpModel.getProperty("/p" + (iHelp + 1)).openBy(this._oHelpModel.getProperty("/c" + (iHelp + 1)));
} else {
this._oHelpModel.setProperty("/counter", iHelp + 1);
this.onHelpNext();
return;
}
},
use the control's onAfterRendering event. in your corresponding view's controller:
var oControl = this.byId("yourControl");
oControl.addEventDelegate({
onAfterRendering: function() {
// your confirmation that the control is rendered
}
}
UPDATE after op's clarification:
give the control an id if it doesn't have an id yet. get the control by its id. check if the control is active. in your corresponding view's controller:
var oControl = this.byId("yourControl");
oControl.isActive(); // true if the control is visible

GTK3 GtkCssProvider not working

Configuration is Ubunutu Server 18.04 LTS / Xorg / openbox / GTK3
I have been struggling to figure out why I can't get GtkCssProvider to work on even the most basic item. If have been working with different examples but here is one similar to another post.
Here is the app code that will display the label.
#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget * label = gtk_label_new("Label 0123456789");
GtkCssProvider * cssProvider = gtk_css_provider_new();
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
if( gtk_css_provider_load_from_path(cssProvider, "csstest.css", NULL) )
{
gtk_style_context_add_provider(gtk_widget_get_style_context(label),
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_container_add(GTK_CONTAINER(window), label);
gtk_widget_show_all(window);
gtk_main();
}
return 0;
}
The .CSS file is simple:
GtkLabel {
color: green;
}
The one post indicated that the gtk_style_context_add_provider() should be replaced with the gtk_style_context_add_provider_for_screen() call. Neither seems to have any effect. The label is appearing in the default style black on gray. So what is the trick to getting the Css style to be applied to the widget.
My requirement is to have different styles I can set to different widgets. I assume I can create some type of class construct in the Css that will allow me to apply different themes to the same widget via the gtk_style_context_add_provider() call. However I have not been able to figure that out either. I see how I can set a style in the Css for a specific named widget (via ID) but this appears to be only a predefined statc definition. Any suggestions on this would be helpful.
The CSS selector for a label is label, not GtkLabel:
label {
color: green;
}
See the GtkLabel API reference which lists the valid selectors.
You should also use the GTK Inspector to test CSS fragments.
You may also want to read the API reference for CSS handling in GTK, as well as the GtkStyleContext documentation.
Thanks ebassi for the clarification on the change to css. I shouldn't have started with an older example. Here was the completed example working in GTK 3.22 I ended up with to demonstrate the dynamic change in appearance using CSS styles:
#include <gtk/gtk.h>
GtkWidget * label1;
GtkWidget * label2;
GtkWidget * label3;
// Handler to change the appearance of the widgets from their original
void button_clicked (GtkButton * button, gpointer user_data)
{
// names are defined in the .CSS
gtk_widget_set_name( GTK_WIDGET(label1) , "" );
gtk_widget_set_name( GTK_WIDGET(label2) , "white-widget" );
gtk_widget_set_name( GTK_WIDGET(label3) , "red-widget" );
}
// Set the style provider for the widgets
static void apply_css_provider (GtkWidget * widget, GtkCssProvider * cssstyleProvider)
{
gtk_style_context_add_provider ( gtk_widget_get_style_context(widget), GTK_STYLE_PROVIDER(cssstyleProvider) , GTK_STYLE_PROVIDER_PRIORITY_USER );
// For container widgets, apply to every child widget on the container
if (GTK_IS_CONTAINER (widget))
{
gtk_container_forall( GTK_CONTAINER (widget), (GtkCallback)apply_css_provider , cssstyleProvider);
}
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkCssProvider * cssProvider = gtk_css_provider_new();
GtkWidget * fixed = gtk_fixed_new();
GtkWidget * button = gtk_button_new_with_label ("Change");
//create the labels for this test
label1 = gtk_label_new("Label 0001");
label2 = gtk_label_new("Label 23456789");
label3 = gtk_label_new("Label 3");
//setup container and add widgets
gtk_container_add (GTK_CONTAINER (window), fixed);
gtk_fixed_put( GTK_FIXED(fixed), label1 , 30 , 30 );
gtk_fixed_put( GTK_FIXED(fixed), label2 , 30 , 50 );
gtk_fixed_put( GTK_FIXED(fixed), label3 , 30 , 70 );
gtk_fixed_put( GTK_FIXED(fixed), button , 30 , 110 );
//register the handlers
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), NULL );
if( gtk_css_provider_load_from_path(cssProvider, "csstest.css", NULL) )
{
apply_css_provider( window , cssProvider );
// set a default style - name is defined in .CSS file
gtk_widget_set_name( GTK_WIDGET(label1) , "red-widget" );
gtk_widget_show_all(window);
gtk_main();
}
return 0;
}
csstest.css:
/* default coloring of widgets */
* {
background-color: #00ee00;
}
#red-widget {
color: white;
background-color: red;
}
#white-widget {
color: red;
background-color: white;
}
button {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
border-top-left-radius: 0;
border-top-right-radius: 0;
background-image: none;
}

Table viewer width increases each time section expanded

I have created a Table which is inside the form Section .Each time it expands the table size(Width) grows. I am using TableColoumnLayout for the table viewer composite.
I have checked this bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=215997 .I have no luck.
Any advice?
objectiveSection = toolkit.createSection(composite, Section.DESCRIPTION|Section.TITLE_BAR|
Section.TWISTIE|Section.EXPANDED);
objectiveSection.setActiveToggleColor(toolkit.getHyperlinkGroup().getActiveForeground());
objectiveSection.setToggleColor(toolkit.getColors().getColor(FormColors.SEPARATOR));
GridData data=new GridData(GridData.FILL, GridData.FILL, true, true);
objectiveSection.setLayoutData(data);
toolkit.createCompositeSeparator(objectiveSection);
toolkit.adapt(objectiveSection);
/**
* Creating a client inside the section
*/
Composite objective = toolkit.createComposite(objectiveSection, SWT.BORDER);
/**
* creating the object table model and object table.
*/
TableModel objectiveModel=new ObjectiveTableModel();
GridData gds = new GridData(GridData.FILL,GridData.FILL,true,true);
objectiveTable=new CustomTable(objectiveModel, objective, true, true, gds);
/**
* column for object viewer
*/
objectiveTable.createTableViewerColumn("List of Behaviors", 0);
objectiveTable.enableCellEdit();
objectiveTable.autoFocus();
toolkit.adapt(objective);
objectiveSection.setText("Behaviours");
objectiveSection.setDescription("The section contains the Behaviours of the selected operation"); //$NON-NLS-1$
objectiveSection.setClient(objective);
objectiveSection.setExpanded(true);
objectiveSection.setEnabled(true);
objectiveSection.addExpansionListener(new ExpansionAdapter() {
public void expansionStateChanged(ExpansionEvent e) {
form.reflow(false);
}
});
My table will get the values dynamically .Each time i am re sizing the column
this.table.setRedraw(false);
for (int i = 0, n = this.table.getColumnCount(); i < n; i++){
this.table.getColumn(i).pack();
int minWidth=this.table.getColumn(i).getWidth();
this.layout.setColumnData(this.table.getColumn(i), new ColumnWeightData(25,minWidth));
}
this.table.setRedraw(true );
this.composite.layout();
It seems to happen because of the following line:
GridData gds = new GridData(GridData.FILL,GridData.FILL,true,true);
You request that the table will fill its container and grab excess space.
Try instead:
GridData gds = new GridData(GridData.CENTER,GridData.CENTER,false,false);

How to create the GtkListview by Glade3?

I'm using glade3 to develop a simple app on windows. The official reference manual seems out of date, so I use the gtk-function to create the Listview and put the MySQL-Query result under the field rows. Need help about create the listview by glade3 not by the codes.
Thanks anymore!
My code:
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include "mysql.h"
#define HOST "localhost"
#define USERNAME "root"
#define PASSWORD ""
#define DATABASE "student"
enum ListCols
{
LIST_NUM,
LIST_NAME,
LIST_CHECKED,
LIST_CNT
};
int main (int argc, char *argv[])
{
GtkWidget* win;
GtkWidget* vbox ;
GtkWidget* statusbar ;
GtkTreeView* tree;
GtkTreeView* list;
GtkTreeStore* tree_store;
GtkListStore* list_store;
GtkTreeIter iter;
GtkTreeIter iter_child;
GtkCellRenderer* renderer;
GtkTreeViewColumn* column;
GtkTreeSelection* select;
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_FIELD *field;
MYSQL_ROW result_row;
int res;
int row, col;
int i, j;
char * sql = "select * from person;";
gtk_init (&argc, &argv);
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (win), "QueryData");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
gtk_widget_set_size_request(win, 480, 480);
vbox = gtk_vbox_new (FALSE, 2);
gtk_container_add (GTK_CONTAINER (win), vbox);
list = gtk_tree_view_new();
list_store = gtk_list_store_new(LIST_CNT,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_BOOLEAN);
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD,DATABASE, 0, NULL, CLIENT_FOUND_ROWS))
{
printf("Query data successfully!\n");
mysql_query(&my_connection, "set names utf8");
res = mysql_query(&my_connection, sql);
if (res)
{
printf("Error: mysql_query !\n");
mysql_close(&my_connection);
}
else
{
res_ptr = mysql_store_result(&my_connection);
if (res_ptr)
{
col = mysql_num_fields(res_ptr);
row = mysql_num_rows(res_ptr) + 1;
printf("%d lines queried\n", row);
for (i = 0; field = mysql_fetch_field(res_ptr); i++)
printf("%s ", field->name);
printf("\n");
for (i = 1; i < row; i++)
{
result_row = mysql_fetch_row(res_ptr);
gtk_list_store_append(list_store, &iter);
gtk_list_store_set(list_store, &iter,
LIST_NUM, result_row[0],
LIST_NAME, result_row[2],
LIST_CHECKED, FALSE, -1);
}
}
mysql_close(&my_connection);
}
}
else
printf("Fail to query data!\n");
for (int k = 0; k < 3; k++)
gtk_tree_view_set_model(list, list_store);
g_object_unref(list_store);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Name", renderer,
"text", LIST_NUM, NULL);
column = gtk_tree_view_append_column(list, column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Age", renderer,
"text", LIST_NAME, NULL);
column = gtk_tree_view_append_column(list, column);
renderer = gtk_cell_renderer_toggle_new();
column = gtk_tree_view_column_new_with_attributes("PersonInfo", renderer,
"active", LIST_CHECKED, NULL);
column = gtk_tree_view_append_column(list, column);
gtk_box_pack_start(vbox, list, TRUE, TRUE, 1);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
First of all you have to create a container to hold the treeview. If your using a grid, you can use a ScrolledWindow for example. In this example I just put it directly in the window container. Drag the TreeView on the window container:
Then a dialog asks you for the corresponding treestore:
Press on the pen and paper icon to open another dialog. Then press on "new".
This will get you back to the first dialog, with the newly created TreeStore already filled into the field. You can just press "Create":
Then you can start creating columns for the TreeStore. I created on for the Name (gchararray) and one for the Age (gint):
After that you can add data to the TreeStore:
Finally you still have to add columns to the TreeView. That is done by clicking on the TreeView --> click edit --> go to Hierarchy tab --> Add as many columns as you need.
Don't forget to also load the liststore from the Glade file, otherwise your data will not be present, when you run the program from the code.
Let's say you already have a container and you want to add a tree view into it.
To do that, you look at the "Control and Display" category of widgets, and locate "tree view". If you can't find it, or can't identify the icon, try hovering each icon until you find it.
Click on the icon, then click on the container that you wish to add it to. This will immediately cause a dialog box to occur, asking you to link it with a tree model. You can click the pencil icon, then click "New" to create a list store.
Alternatively, you can create a list store or tree store first. In the widget selection side panel, look under "Miscellaneous". (Between "Composite Widgets" and "Deprecated".) Locate list store or tree store, and click on it to create it. Now if you create the tree view (using the method above), you can choose this tree store / list store as your model.

add list to gtk_tree_view in C

folks,
im gonna tryin to make a little function which has something todo with the Gtk_tree_view that i've made,
the error response such as like this :
(main:8026): Gtk-CRITICAL **: IA__gtk_tree_view_get_model: assertion `GTK_IS_TREE_VIEW (tree_view)' failed
(main:8026): Gtk-CRITICAL **: IA__gtk_list_store_append: assertion `GTK_IS_LIST_STORE (list_store)' failed
(main:8026): Gtk-CRITICAL **: IA__gtk_list_store_set_valist: assertion `GTK_IS_LIST_STORE (list_store)' failed
^C
and the source, here you go :
void addlist(bahan *unit, const gchar *str)
{
GtkListStore *store;
GtkTreeIter iter; // penyambung store_append and store_set
store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(unit->treeview_aktifitas)));
gtk_list_store_append(store, &iter); // need unary &, coz iter aint define like a pointer
gtk_list_store_set(store, &iter, 1, str, -1);
}
as you've figured , the structure of 'unit' was supposed like this :
typedef struct
{
GtkWidget *window;
GtkWidget *notebook;
GtkWidget *start;
GtkWidget *verbose;
GtkWidget *settings;
GtkWidget *about;
GtkWidget *statusbar;
GtkWidget *treeview_aktifitas;
guint statusbar_id;
} bahan;
and connected by this :
unit->treeview_aktifitas = GTK_WIDGET(gtk_builder_get_object(main,"treeview1"));
in the real case, im using 'glade 3', and have two columns on glade file, how to set on gtk_list_store_set ?
very appreciate any further help :)
well, i've found the fault now
gtk_list_store_set(store, &iter, 1, str, -1);
just because my columns are two ,thus i need to
gtk_list_store_set(store, &iter, 0,"aku", 1, "tes", -1);
which 0 represent first column and 1 represent next column, and so on -_-'