How to create the GtkListview by Glade3? - gtk3

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.

Related

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;
}

Mixing text and images causes incorrect vertical positioning

Using iTextSharp version 5.5.8 (same bug existed in 5.5.7), there's an unpleasant bug when you add images to Chapters and Sections - the images and the section headings start out OK but quickly become offset relative to each other.
The PDF generated from the following code starts out correctly, it says "Section 1" and below it is the image. The next section ("Section 2") has a little of the image overlapping the section text, the next section is even worse, etc. I think it's the text that's mal-positioned, not the image.
Is this a known iTextSharp bug?
static Document m_doc = null;
static BaseFont m_helvetica = null;
static Font m_font = null;
static PdfWriter m_writer = null;
static Image m_image = null;
static void Main(string[] args)
{
m_doc = new Document(PageSize.LETTER);
m_helvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
m_font = new Font(m_helvetica, 10.0f);
m_writer = PdfWriter.GetInstance(m_doc, new FileStream("Output.pdf", FileMode.Create));
m_writer.StrictImageSequence = true;
m_doc.Open();
m_doc.Add(new Chunk("Created by iTextSharp version " + new iTextSharp.text.Version().GetVersion, m_font));
Chapter chapter = new Chapter("Chapter 1", 1);
chapter.TriggerNewPage = false;
if (m_image == null)
{
m_image = Image.GetInstance(new Uri("https://pbs.twimg.com/profile_images/2002307628/Captura_de_pantalla_2012-03-17_a_la_s__22.14.48.png"));
m_image.ScaleAbsolute(100, 100);
}
for (int i = 0; i < 5; i++)
{
Section section = chapter.AddSection(18, "Section " + (i + 1));
section.Add(new Chunk(" ", m_font));
section.Add(m_image);
}
m_doc.Add(chapter);
m_doc.Close();
}
From the documentation for the Java version:
A Section is a part of a Document containing other Sections, Paragraphs, List and/or Tables.
Further looking at the Add() method in the C# source we see:
Adds a Paragraph, List, Table or another Section
Basically, instead of a Chunk use a Paragraph. So instead of this
section.Add(new Chunk(" ", m_font));
Use this:
section.Add(new Paragraph(new Chunk(" ", m_font)));
Or even just this:
section.Add(new Paragraph(" ", m_font));

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);

GWT listbox onmouseover tooltip

Id like to add tooltip to each item of a ListBox ?
Any idea ?
Thanks for all.
Since the ListBox doesn't expose it's OptionElement list, we'll need to get the underlying SelectElement.
SelectElement selectElement = SelectElement.as(listBox.getElement());
From there, we just set the title on each OptionElement.
NodeList<OptionElement> options = selectElement.getOptions();
for (int i = 0; i < options.getLength(); i++) {
options.getItem(i).setTitle("Hover text for item #" + i);
}

Dynamically Generated Telerik MVC3 Grid - Add Checkboxes

I have a grid that is dynamically generated based on search criteria. I render the grid in a partial view using Ajax. That all works fine.
I now need to add a checkbox column as the first column.
Also, how do I get filtering, sorting paging etc. to work now since it is in a partial view.
When i click on a header to sort I get a Page not found error and the filter Icon doesnt do anything.
And one more thing. When I try to add a GridCommandColumnSettings to the grid I get the error
"Invalid initializer member declarator"
Code is below for the gridcolumnsettings
public GridColumnSettings[] NewColumns(DataTable fullDT)
{
GridColumnSettings[] newColumns = new GridColumnSettings[fullDT.Columns.Count];
for (int i = 0; i < fullDT.Columns.Count; i++)
{
// set the visibility property for the DeliveryID
bool boolDeliveryID;
if (fullDT.Columns[i].ColumnName == "DeliveryID")
boolDeliveryID = false;
else
boolDeliveryID = true;
newColumns[i] = new GridColumnSettings
{
new GridCommandColumnSettings
{
Commands =
{
new GridEditActionCommand(),
new GridDeleteActionCommand()
},
Width = "200px",
Title = "Commands"
},
Member = fullDT.Columns[i].ColumnName,
Title = fullDT.Columns[i].ColumnName,
Visible = boolDeliveryID,
Filterable = true,
Sortable = true
};
}
return newColumns;
}
Any suggestions would be appreciated.
Thanks
I edited my post to add my partial for the Grid
Here is my partial for the grid
#(Html.Telerik().Grid<System.Data.DataRow>(Model.Data.Rows.Cast<System.Data.DataRow>())
.Name("Grid")
.Columns(columns =>
{
columns.LoadSettings(Model.Columns as IEnumerable<GridColumnSettings>);
})
.DataBinding(dataBinding => dataBinding.Ajax().Select("_DeliveryManagerCustomBinding", "Deliveries"))
.EnableCustomBinding(true)
.Resizable(resize => resize.Columns(true))
)
I don't add columns this way when I use the Telerik Grid control, but looking at what you're doing I would hazard a guess to say you will need to do something like the following:
increase the size of the newColumns array by 1 (because we're going to add in the checkbox column):
GridColumnSettings[] newColumns = new GridColumnSettings[fullDT.Columns.Count + 1];
if you want it at the beginning you will need to do the following before your for-loop:
GridColumnSettings s = new GridColumnSettings() {
ClientTemplate("<input type=\"checkbox\" name=\"checkeditems\" value=\"some value\" />")
Title("title goes in here")
};
Then you will add it into your array:
newColumns[0] = s;
and then increase the start index for your for-loop to 1:
for (int i = 1; i < fullDT.Columns.Count; i++)
the checkbox column will go at the beginning