Related
I have tried past one month learning to use gtk for an application and migrating its code from gtk2 to gtk3. But, one button in a gtk dialog doesn't seem to work well, inspite of whatever i chose the dialog closes without calling the callback function.
int validation_pmu_server (GtkButton *button, gpointer udata)
{
/* local variables */
int tmp_p;
char *msg;
const char *text1, *text2;
/* Get the text entries filled by user */
text1 = gtk_entry_get_text(GTK_ENTRY(p_port));
text2 = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(p_protocol));
if(!isNumber((char *)text1) && (atoi(text1) < 1500 || (atoi(text1) > 65535))) /* Check for valid port number */
{
msg = "\tPlease enter valid value for PMU Server Port\t\n";
validation_result (msg); /* Show the unsuccess message to user */
return 0;
}
else
{
strncpy(PMUprotocol, text2, 3);
PMUport = atoi(text1);
if(!strcmp(text2, "UDP"))
tmp_p = 1;
else
tmp_p = 2;
p1 = *ShmPTR;
p1.cfg_bit_change_info = 0;
ShmPTR->cfg_bit_change_info = 0;
ShmPTR->serverProtocol = tmp_p;
ShmPTR->serverPort = atoi(text1);
p1.pid = ShmPTR->pid;
kill(p1.pid, SIGUSR2);
printf("PORT & Protocol information has sent to PMU Server through signal.\n");
memset(pmuFilePath, '\0', 200);
gtk_widget_set_sensitive(GTK_WIDGET(pmu_data->start_server), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(pmu_data->open_cfg), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(pmu_data->create_cfg_button), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(pmu_data->menu_setup_cfg), TRUE);
/* Close/destroy the pmu_server_window */
gtk_widget_destroy(pmu_server_window);
return 1;
}
};
void pmu_server ()
{
/* local variables */
GtkWidget *table, *label, *cancel_button;
GtkWidget *valdbutton, *help_button;
/* Create a new dialog window for PMU Server Setup */
pmu_server_window = gtk_dialog_new ();
g_signal_connect (pmu_server_window, "destroy", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
gtk_window_set_title (GTK_WINDOW (pmu_server_window), "PMU Server Setup");
gtk_container_set_border_width (GTK_CONTAINER (pmu_server_window), 10);
/* Create a table of 4 by 2 squares. */
table = gtk_grid_new (); // table = gtk_table_new (6, 2, FALSE);
/* Set the spacing to 50 on x and 5 on y */
gtk_grid_set_row_spacing (GTK_GRID(table), 16); // gtk_table_set_row_spacings (GTK_TABLE (table), 8);
gtk_grid_set_column_spacing (GTK_GRID(table), 16); // gtk_table_set_col_spacings (GTK_TABLE (table), 2);
/* Pack the table into the window */
//gtk_box_pack_start (GTK_BOX (GTK_DIALOG(pmu_server_window)->vbox), table, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG(pmu_server_window))), table, TRUE, TRUE, 0);
gtk_widget_show (table);
/* Add few buttons to the bottom of the dialog */
valdbutton = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Run", GTK_RESPONSE_OK); // gtk_button_new_with_label ("Run");
help_button = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Help", GTK_RESPONSE_APPLY); // gtk_button_new_with_label ("Help");
cancel_button = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Cancel", GTK_RESPONSE_CLOSE); //gtk_button_new_with_label ("Cancel");
/* This simply creates a grid of toggle buttons on the table */
label = gtk_label_new (" ");
markup = g_markup_printf_escaped ("<span foreground=\"#990033\" font='12'><b>Enter PMU Server Details</b></span>");
gtk_label_set_markup (GTK_LABEL (label), markup);
gtk_grid_attach (GTK_GRID (table), label, 0,0,2,1); // gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 0, 1);
gtk_widget_show (label);
g_free (markup);
label = gtk_label_new ("Server Port");
// gtk_misc_set_alignment (GTK_MISC(label),0,0);
gtk_label_set_xalign (GTK_LABEL (label),0);
gtk_label_set_yalign (GTK_LABEL (label),0);
gtk_grid_attach (GTK_GRID (table), label, 0, 2, 1, 1); // gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
gtk_widget_show (label);
label = gtk_label_new ("Protocol");
// gtk_misc_set_alignment (GTK_MISC(label),0,0);
gtk_label_set_xalign (GTK_LABEL (label),0);
gtk_label_set_yalign (GTK_LABEL (label),0);
gtk_grid_attach (GTK_GRID (table), label, 0, 3, 1, 1); // gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 3, 4);
gtk_widget_show (label);
/* Create text boxes for user to enter appropriate values */
p_port = gtk_entry_new();
gtk_entry_set_max_length ((GtkEntry *)p_port, 5);
gtk_grid_attach (GTK_GRID (table), p_port, 1, 2, 1, 1); // gtk_table_attach_defaults (GTK_TABLE (table), p_port, 1, 2, 2, 3);
gtk_widget_show (p_port);
label = gtk_label_new (" ");
markup = g_markup_printf_escaped ("<span foreground=\"#333333\" font='8'><b>Note : Use the unreserved ports for PMU Server.</b></span>");
gtk_label_set_markup (GTK_LABEL (label), markup);
// gtk_misc_set_alignment (GTK_MISC(label),0,0);
gtk_label_set_xalign (GTK_LABEL (label),0);
gtk_label_set_yalign (GTK_LABEL (label),0);
gtk_grid_attach (GTK_GRID (table), label, 0, 5, 2, 1); // gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 5, 6);
gtk_widget_show (label);
g_free (markup);
/* Create combo boxe for user with some fixed values */
p_protocol = gtk_combo_box_text_new();
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(p_protocol), "UDP");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(p_protocol), "TCP");
gtk_combo_box_set_active(GTK_COMBO_BOX(p_protocol), 0);
gtk_grid_attach (GTK_GRID (table), p_protocol, 1, 3, 1, 1); // gtk_table_attach_defaults (GTK_TABLE (table), p_protocol, 1, 2, 3, 4);
gtk_widget_show (p_protocol);
/* Signal handling for buttons on PMU Server Setup Window */
g_signal_connect_swapped (valdbutton, "clicked", G_CALLBACK (validation_pmu_server), valdbutton);
g_signal_connect_swapped (help_button, "clicked", G_CALLBACK (Pmu_Help), NULL);
g_signal_connect_swapped (cancel_button, "clicked", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
g_signal_connect_swapped (pmu_server_window, "response", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
/* This makes it so the button is the default. */
// gtk_box_pack_start (GTK_BOX (GTK_DIALOG (pmu_server_window)->action_area), valdbutton, TRUE, TRUE, 0);
// gtk_box_pack_start (GTK_BOX (GTK_DIALOG (pmu_server_window)->action_area), help_button, TRUE, TRUE, 0);
// gtk_box_pack_start (GTK_BOX (GTK_DIALOG (pmu_server_window)->action_area), cancel_button, TRUE, TRUE, 0);
/* This grabs this button to be the default button. Simply hitting the "Enter" key will cause this button to activate. */
gtk_widget_grab_default (valdbutton);
gtk_widget_show (valdbutton);
gtk_widget_grab_default (help_button);
gtk_widget_show (help_button);
gtk_widget_grab_default (cancel_button);
gtk_widget_show (cancel_button);
/* Finally show the pmu_server_window */
gtk_widget_show (pmu_server_window);
};
The function pmu_server creates a dialog as shown in this image
as per my code when run button is clicked validation_pmu_server is supposed to be called but rather the dialog box simply closes as if the gtk_widget_destroy was called. some of the commented code is the old gtk2 code which i migrated to gtk3.
PS : The above code change from gtk2 to gtk3 is done using this reference
Though I was able to make my code work somewhow, I please request anyone with knowledge to help me understand why this is happening.
Apparantly the problem was due to the below line
g_signal_connect_swapped (pmu_server_window, "response", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
Once I either remove the above line or change it to the below code, everything seems working fine, but I'm still unsure why, i'm guessing that sending "response" as the detailed signal was causing the problem.
g_signal_connect_swapped (pmu_server_window, "destroy", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
The problem you are facing is because of response signal. Whenever any action widget(in your case dialog buttons) is clicked, the response signal is emitted before any other signal(clicked signal). So the function callback associated with signal response will be executed first and then the callback associated with the clicked signal. So, the gtk_widget_destroy is getting called up first and your dialog is closing.
You can either change the callback associated with response or use any other signal.
I have used the below standalone code to reproduce the issue.
#include<stdio.h>
#include <gtk/gtk.h>
void Pmu_Help(){
printf("Pmu_Help");
fflush(stdout);
}
void validation_pmu_server(){
printf("validation_pmu_server");
fflush(stdout);
}
void response(){
printf("response");
fflush(stdout);
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget * pmu_server_window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show(window);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
pmu_server_window = gtk_dialog_new ();
g_signal_connect (pmu_server_window, "destroy", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
gtk_window_set_title (GTK_WINDOW (pmu_server_window), "PMU Server Setup");
gtk_container_set_border_width (GTK_CONTAINER (pmu_server_window), 10);
GtkWidget *table, *label, *cancel_button;
GtkWidget *valdbutton, *help_button;
valdbutton = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Run", GTK_RESPONSE_OK); // gtk_button_new_with_label ("Run");
help_button = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Help", GTK_RESPONSE_APPLY); // gtk_button_new_with_label ("Help");
cancel_button = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Cancel", GTK_RESPONSE_CLOSE); //gtk_button_new_with_label ("Cancel");
g_signal_connect_swapped (valdbutton, "clicked", G_CALLBACK (validation_pmu_server), valdbutton);
g_signal_connect_swapped (help_button, "clicked", G_CALLBACK (Pmu_Help), NULL);
g_signal_connect_swapped (cancel_button, "clicked", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
g_signal_connect_swapped (pmu_server_window, "response", G_CALLBACK (response), pmu_server_window);
//g_signal_connect_swapped (pmu_server_window, "response", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
gtk_widget_show(pmu_server_window);
gtk_main();
return 0;
}
I have a program using pthreads. I want add an interface using gtk2.0, but have a problem with drawing on window. When I call g_signal_connect(G_OBJECT(window), "configure_event",G_CALLBACK(draw_things), NULL);
lines which I draw appear and at once disappear. If I move a window calling function
gtk_window_move (GTK_WINDOW(widget), 10,10);
or by means of a mouse, lines appear. If I call g_signal_connect(G_OBJECT(window), "expose_event", G_CALLBACK(draw_things), NULL); my lines are displayed correctly, but buttons and labels are not displayed. My code looks like this:
int main(int argc, char *argv[]){
pthread_t tr1;
pthread_t tr2;
pthread_t *trs;
int rc = 0;
type j;
int16_t *id;
type Stop_Point;
GtkWidget *window;
GtkWidget *button;
GtkWidget *table;
GtkWidget *drawing_area;
g_thread_init (NULL);
gdk_threads_init ();
gdk_threads_enter ();
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window, width/1.6, height/1.33);
gtk_window_set_position(GTK_WINDOW(window), -1);
gtk_window_set_title (GTK_WINDOW (window), "Window");
g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 2);
table = gtk_table_new ( width/1.6, height/1.33, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
button = gtk_button_new_with_label ("next >>");
gtk_table_attach_defaults (GTK_TABLE (table), button, 1110, 1190, 750, 790);
gtk_widget_show (button);
button = gtk_button_new_with_label ("<< prev");
gtk_table_attach_defaults (GTK_TABLE (table), button, 1020, 1100 , 750, 790);
gtk_widget_show (button);
rc = pthread_create(&tr1, NULL, tr1_func, &id);
rc = pthread_create(&Generator, NULL, tr2_func, &Stop_Point);
trs = (pthread_t*)malloc(Num * sizeof(pthread_t));
for(j = 0; j < Num; j++) {
rc = pthread_create(trs + j, NULL, trs_func, id + j);
}
// g_signal_connect(G_OBJECT(window), "configure-event", GTK_SIGNAL_FUNC(draw_things), NULL);
g_signal_connect(G_OBJECT(window), "expose_event", G_CALLBACK(draw_things), NULL);
gtk_widget_show_all (window);
gtk_main ();
gdk_threads_leave ();
return 0;
}
Fnction for drawing looks like this
gboolean draw_things(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
GdkGC *gc;
gc=gdk_gc_new(GDK_DRAWABLE(widget->window));
gdk_gc_set_line_attributes(gc, 5, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 5, 35, 1200, 35);
gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 1170, 45, 1200, 35);
gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 1170, 25 , 1200, 35);
// gtk_window_move (GTK_WINDOW(widget), 1,1);
return TRUE;
}
Any help is welcomed, because now I use "configure_event" and call gtk_window_move (GTK_WINDOW(widget), 0,0); every time when I need to draw the picture.
I'm trying to code some simple stuff in C, with GTK+ and Cairo, but I'm having some troubles using the gtk_adjustment_get_value function.
This is my code:
#include <cairo.h>
#include <gtk/gtk.h>
#include <math.h>
#include <stdlib.h>
#include <gtk/gtkhscale.h>
#include <math.h>
typedef struct
{
GtkAdjustment *adj[2]; //Adjustments para as escalas
GtkWidget *scale[2]; //Escalas
} ProgInfo;
gboolean
on_expose_event (GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
cairo_t *cr;
double pos1x, pos1y;
static gdouble pos2x = 450., pos2y = 290.; //Coordenadas Espelho
static gdouble pos3x = 450., pos3y = 250.;
ProgInfo *pinfo;
cr = gdk_cairo_create(widget->window);
pinfo = (ProgInfo *) data;
pos1x = gtk_adjustment_get_value (pinfo->adj[0]);
//Objecto
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_set_line_width (cr, 1.0);
cairo_rectangle (cr, (double) pos1x, (double) pos1y, 20, 80);
cairo_stroke_preserve (cr);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_fill (cr);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_set_line_width (cr, 1.0);
cairo_rectangle (cr, (gint) pos3x, (gint) pos3y, 5, 80);
cairo_stroke_preserve (cr);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_fill (cr);
cairo_stroke(cr);
cairo_destroy(cr);
return FALSE;
}
int main (int argc, char *argv[])
{
GtkWidget *window, *vbox, *vbox1, *frame, *frame1, *frame2, *label, *label1, *label2, *hbox, *hbox1, *hscale;
char string[40] = "Posição Objecto", string1[30] = "Posição Espelho";
ProgInfo *pinfo;
gdouble pos1x;
pinfo = (ProgInfo *) calloc (1, sizeof (ProgInfo));
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "expose-event", G_CALLBACK(on_expose_event), NULL);
g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title (GTK_WINDOW(window), "Projecto Óptica");
gtk_window_set_default_size (GTK_WINDOW(window), 800, 600);
gtk_widget_set_app_paintable (window, TRUE);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
hbox1 = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (vbox), hbox1);
vbox1 = gtk_vbox_new (FALSE, 0);
gtk_box_pack_end (GTK_BOX (hbox1), vbox1, FALSE, FALSE, 20);
//Posição Objecto
label2 = gtk_label_new (string);
gtk_box_pack_start (GTK_BOX (vbox1), label2, FALSE, FALSE, 20);
pinfo->adj[0] = (GtkAdjustment *) gtk_adjustment_new (0.7, 0.0, 1.0, 0.001, 1.0, 0.0);
//Os parâmetros são, por ordem: valor inicial, valor mínimo e valor máximo
pinfo->scale[0] = gtk_hscale_new (GTK_ADJUSTMENT (pinfo->adj[0]));
gtk_box_pack_start (GTK_BOX (vbox1), pinfo->scale[0], FALSE, TRUE, 5);
//Posição Espelho
label1 = gtk_label_new (string1);
gtk_box_pack_start (GTK_BOX (vbox1), label1, FALSE, FALSE, 20);
pinfo->adj[1] = (GtkAdjustment *)gtk_adjustment_new (0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
pinfo->scale[1] = gtk_hscale_new (GTK_ADJUSTMENT (pinfo->adj[1]));
gtk_box_pack_start (GTK_BOX (vbox1), pinfo->scale[1], FALSE, FALSE, 0);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I created a couple of sliders with the hscale widget, and they show up fine in the window, I can move them, everything's alright. But I want to make my variable, pos1x, change when I move the slider. I tried using the gtk_adjustment_get_value function to do it, but I'm getting a segmentation fault. I've noticed that if I comment out this line:
pos1x = gtk_adjustment_get_value (pinfo->adj[0]);
The segmentation fault does not happen anymore, but the app doesn't do what I want either. I realize that this is probably very simple, and my doubts derive from scarce understanding of how GTK+ works, but I've been trying for the last few hours and I can't figure out what's wrong. I'd appreciate any help you have to offer.
Oh, and sorry for putting such a big block of code here, but I wasn't sure which parts you'd need.
You connect to the expose-event here with the userdata pointer set to NULL:
g_signal_connect (window, "expose-event", G_CALLBACK(on_expose_event), NULL);
but in the handler you expect it to be a pointer to ProgInfo. You should replace NULL with pinfo.
I've been stuck with this problem for a while. I have a tree view that shows a table. The size of the table depends on the user's input. I want to make it look nice and when the table has only 2 columns there is a lot of space that isn't use. So how do I change the heigth and width of the tree view? Thanks
static void show_results(struct listaObjetos *vectorObjetos[],int cantidad, int tamanoMochila){
int i;
char msg[180] = {0};
/
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
treeView = gtk_tree_view_new ();
gtk_widget_set_size_request (GTK_WIDGET (treeView), 0, 0);
window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window2), "Resultados");
gtk_container_set_border_width(GTK_CONTAINER(window2),10);
gtk_widget_set_size_request (window2, 480, 320);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("column1 ", renderer, "text",COLUM1, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
column = gtk_tree_view_column_new_with_attributes("colum2 ", renderer, "text",COLUM2, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
column = gtk_tree_view_column_new_with_attributes("colum3 ", renderer, "text",COLUMN3, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
column = gtk_tree_view_column_new_with_attributes("colum4", renderer, "text",COLUM4, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
store = gtk_list_store_new (4, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,G_TYPE_INT);
for(i=0; i<cantidad; i++)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, COLUM1, vectorObjetos[i]->x,COLUMN2,
vectorObjetos[i]->y, COLUM3, vectorObjetos[i]->q,
COLUM4,vectorObjetos[i]->z,-1);
}
gtk_tree_view_columns_autosize(GTK_TREE_VIEW (treeView));
gtk_tree_view_set_model (GTK_TREE_VIEW (treeView), GTK_TREE_MODEL (store));
g_object_unref (store);
scrolledWin = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledWin),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
table = gtk_table_new(10,10,FALSE);
gtk_table_set_row_spacings (GTK_TABLE (table), 5);
gtk_table_set_col_spacings (GTK_TABLE (table), 5);
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolledWin), table);
label = gtk_label_new("Objetos sujetos a analisis");
gtk_table_attach_defaults(GTK_TABLE(table), label,4,5,0,1);
g_snprintf(msg, sizeof msg, "A message here ",
cantidad, tamanoMochila );
label = gtk_label_new(msg);
gtk_table_attach_defaults(GTK_TABLE(table), label,0,10,1,2);
label = gtk_label_new(" ");
gtk_table_attach_defaults(GTK_TABLE(table), label,3,4,2,4);
gtk_table_attach_defaults (GTK_TABLE(table), treeView, 4,5,2,4);
label = gtk_label_new(" ");
gtk_table_attach_defaults(GTK_TABLE(table), label,5,6,2,4);
gtk_container_add (GTK_CONTAINER (window2), scrolledWin);
gtk_widget_show_all (window2);
}
I tried to post an image with the result but I couldn't due to reputation
I am trying a simple sample Gtk program and using GtkTable to align my widgets.
My layout should be:
Window background is black. The buttons should be white while the text on the buttons should be black. All labels should be white so they can be seen against the black window background.
With this in mind. Here is what I have done so far.
Code:
#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h> /* for atoi() and exit() */
#include <stdio.h> /* standard in and output*/
typedef struct cmd_widgets{
GtkWidget *button1;
GtkWidget *combo;
GtkWidget *label;
}my_cmd_widgets;
static gboolean close_application( GtkWidget *widget, GdkEvent *event, gpointer data )
{
gtk_main_quit ();
return FALSE;
}
static void UpdateChoice( GtkWidget *widget, gpointer data)
{
my_cmd_widgets *widgrp;
widgrp = (my_cmd_widgets *)data;
gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(widgrp->combo));
GString *val = g_string_new("You have chosen : ");
g_string_append(val, text);
gtk_label_set_text(GTK_LABEL(widgrp->label), val->str);
g_free(text);
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1;
GtkWidget *label2;
GtkStyle *style;
GtkWidget *align;
my_cmd_widgets grp_widgets;
GtkWidget *image;
gtk_init(&argc, &argv);
gtk_rc_parse("fonts.rc");
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "Tour App");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
table = gtk_table_new(7, 1, FALSE);
gtk_container_add(GTK_CONTAINER(window), table);
label1 = gtk_label_new("Our Tour Package Offers");
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), label1);
gtk_table_attach(GTK_TABLE(table), align, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 10);
label2 = gtk_label_new("Select City: ");
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), label2);
gtk_table_attach(GTK_TABLE(table), align,0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
gtk_widget_show(align);
grp_widgets.combo = gtk_combo_box_new_text();
gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "Paris");
gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "London");
gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "Tokyo");
gtk_combo_box_append_text(GTK_COMBO_BOX(grp_widgets.combo), "New York");
/* Creates a new button1. */
grp_widgets.button1 = gtk_button_new_with_label ("See Itinerary");
grp_widgets.label = gtk_label_new("");
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), grp_widgets.label);
gtk_table_attach(GTK_TABLE(table), align,0, 1, 5, 6, GTK_FILL, GTK_FILL, 0, 0);
gtk_widget_show(align);
image = gtk_image_new ();
gtk_image_set_from_file (GTK_IMAGE(image), "Images/olympics_logo.gif");
//column 1
gtk_table_attach(GTK_TABLE(table), image, 0, 1, 0, 1,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 0, 0);
gtk_table_attach(GTK_TABLE(table), grp_widgets.combo, 0, 1, 3, 4,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 0, 15);
gtk_table_attach(GTK_TABLE(table), grp_widgets.button1, 0, 1, 4, 5,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 15);
g_signal_connect(window, "destroy",G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect (window,"delete-event",G_CALLBACK (close_application), NULL);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_signal_connect (G_OBJECT(grp_widgets.button1), "clicked", G_CALLBACK (UpdateChoice), (gpointer)&grp_widgets);
gtk_widget_show(table);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Here is the rc file with properties for TABLE AND WINDOW:
style "window"
{
font_name = "fixed 12"
bg[NORMAL] = { 0.0, 0.0, 0.0 }
}
style "table"
{
bg[NORMAL] = { 1.0, 1.0, 1.0 }
fg[NORMAL] = { 1.0, 1.0, 1.0 }
}
style "container" {
font_name = "bold 12"
xthickness = 10
ythickness = 10
bg[NORMAL] = { 1.0, 1.0, 1.0 }
fg[NORMAL] = { 1.0, 1.0,1.0 }
}
style "button" {
font_name = "bold 12"
xthickness = 10
ythickness = 10
#bg[NORMAL] = { 0.0, 0.0, 0.0 }
fg[NORMAL] = { 0.0, 0.0, 0.0 }
}
#widget_class "*GtkButton*" style "button"
widget_class "GtkWindow" style "window"
widget_class "*GtkTable*" style "table"
The problem in the above code is, the Button's text(label) remains white when I change the fg to white on either table or window styles. Even after changing the fg to black(as below), the label text on my button remains white. Also how can I stretch the image and all the widgets on the window so that whenever I resize my window all widgets are resized to fit screen as well?
All labels should be white so they can be seen against the black window background.
According to what I've found online:
GtkLabel Widget is one of a few GTK+ widgets that don't create their
own window to render themselves into. This menas in order to set the
bg color for a GtkLabel Widget,you need to change the bg color of its
parent.
So in that case, for a simple example, I packed only one of the labels in a GtkEventBox.
Code Piece:
GtkWidget *eventBox;
...
eventBox = gtk_event_box_new();
label1 = gtk_label_new("Our Tour Package Offers");
gtk_container_add(GTK_CONTAINER(eventBox), label1);
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
gtk_container_add(GTK_CONTAINER(align), eventBox);
...
simplified rc file:
style "window"
{
font_name = "fixed 12"
bg[NORMAL] = { 0.0, 0.0, 0.0 }
}
style "label"{
bg[NORMAL] = { 1.0, 1.0, 1.0 }
}
widget_class "GtkWindow" style "window"
widget_class "*GtkAlignment*GtkEventBox*" style "label"
and result: