Ejemplo n.º 1
0
static void fill_combo_box_entry(GtkComboBox *box, uint32_t count, GList *items, gboolean *multi)
{
  gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(box));

  if(count == 0)
  {
    gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(box))), "");
    *multi = FALSE;
    return;
  }

  if(count > 1)
  {
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(box),
                                   _("<leave unchanged>")); // FIXME: should be italic!
    gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(box), GTK_SENSITIVITY_AUTO);
    *multi = TRUE;
  }
  else
  {
    gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(box), GTK_SENSITIVITY_OFF);
    *multi = FALSE;
  }
  for(GList *iter = items; iter; iter = g_list_next(iter))
  {
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(box), iter->data); // FIXME: dt segfaults when there
                                                                         // are illegal characters in the
                                                                         // string.
  }
  gtk_combo_box_set_active(GTK_COMBO_BOX(box), 0);
}
Ejemplo n.º 2
0
void gtk_combo_wakeup(GtkWidget *widget)
{
	// Nth workaround on GT3 bug. 
	// Auto sensitivity needs to be triggered by one fake element add and delete
	gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widget), "");
	gtk_list_store_clear(GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(widget))));
	gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(widget), GTK_SENSITIVITY_AUTO);
}
Ejemplo n.º 3
0
static void fill_combo_box_entry(GtkComboBox **box, uint32_t count, GList **items, gboolean *multi)
{
  GList *iter;

  // FIXME: use gtk_combo_box_text_remove_all() in future (gtk 3.0)
  // https://bugzilla.gnome.org/show_bug.cgi?id=324899
  gtk_list_store_clear(GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(*box))));

  // FIXME: how to make a nice empty combo box without the append/remove?
  if(count == 0)
  {
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(*box), "");
    gtk_combo_box_set_active(GTK_COMBO_BOX(*box), 0);
    gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(*box), 0);

    *multi = FALSE;
    return;
  }

  if(count > 1)
  {
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(*box),
                                   _("<leave unchanged>")); // FIXME: should be italic!
    gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(*box), GTK_SENSITIVITY_AUTO);
    *multi = TRUE;
  }
  else
  {
    gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(*box), GTK_SENSITIVITY_OFF);
    *multi = FALSE;
  }
  if((iter = g_list_first(*items)) != NULL)
  {
    do
    {
      gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(*box), iter->data); // FIXME: dt segfaults when there
                                                                            // are illegal characters in the
                                                                            // string.
    } while((iter = g_list_next(iter)) != NULL);
  }
  gtk_combo_box_set_active(GTK_COMBO_BOX(*box), 0);
}
Ejemplo n.º 4
0
gboolean
toggle_event(GtkWidget *widget, gpointer gdata) {
    Data *data = (Data *) gdata;
    GdkColor *textColor = (GdkColor *) calloc(1, sizeof (GdkColor)),
            *defaultTextColor = (GdkColor *) calloc(1, sizeof (GdkColor)),
            *baseColor = (GdkColor *) calloc(1, sizeof (GdkColor));
    gdk_color_parse(TEXT_ENTRY_NON_EDITABLE_TEXT_COLOR, textColor);
    gdk_color_parse(TEXT_ENTRY_DEFAULT_TEXT_COLOR, defaultTextColor);
    gdk_color_parse(TEXT_ENTRY_DEFAULT_BASE_COLOR, baseColor);

    if (GTK_TOGGLE_BUTTON(widget)->active) {
        gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(data->difficulty_combo),
                GTK_SENSITIVITY_ON);
        strcpy(data->player2Name, (const char *) gtk_entry_get_text(GTK_ENTRY
                (data->player2Entry)));
        gtk_widget_modify_text(data->player2Entry, GTK_STATE_NORMAL,
                textColor);
        gtk_widget_modify_base(data->player2Entry, GTK_STATE_NORMAL,
                baseColor);
        gtk_entry_set_text(GTK_ENTRY(data->player2Entry), "computer");
        gtk_entry_set_editable(GTK_ENTRY(data->player2Entry), FALSE);
    } else {
        gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(data->difficulty_combo),
                GTK_SENSITIVITY_OFF);
        gtk_widget_modify_text(data->player2Entry, GTK_STATE_NORMAL,
                defaultTextColor);
        if (strcmp(data->player2Name, "computer") &&
                strcmp(data->player2Name, "Invalid Name"))
            gtk_entry_set_text(GTK_ENTRY(data->player2Entry),
                data->player2Name);
        else
            gtk_entry_set_text(GTK_ENTRY(data->player2Entry), "");
        gtk_entry_set_editable(GTK_ENTRY(data->player2Entry), TRUE);
    }
    free(textColor);
    free(defaultTextColor);
    free(baseColor);
    return TRUE;
}
Ejemplo n.º 5
0
bool
dataInitializer(char **player1, char **player2, const int *horizontalLines,
        const int *verticalLines, OPPONENT_TYPE *opponent, Difficulty *difficulty) {
    Data *data = g_slice_new0(Data);
    GtkWidget *vbox_root, *hbox_root, *vseparator, *hseparator,
            *quit_button, *apply_button, *label;
    PangoFontDescription *font = pango_font_description_from_string
            (TEXT_ENTRY_FONT);
    char buffer[3];

    data->player1Name = (char *) calloc(21, sizeof (char));
    data->player2Name = (char *) calloc(21, sizeof (char));

    data->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    vbox_root = gtk_vbox_new(FALSE, 10);

    gtk_window_set_default_size(GTK_WINDOW(data->window), 600, 400);
    gtk_window_set_position(GTK_WINDOW(data->window), GTK_WIN_POS_CENTER);
    gtk_window_set_resizable(GTK_WINDOW(data->window), FALSE);
    gtk_window_set_title(GTK_WINDOW(data->window), "New Game - GSquares");
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(ICON_PATH, NULL);
    pixbuf = gdk_pixbuf_scale_simple(pixbuf, 48, 48, GDK_INTERP_BILINEAR);
    gtk_window_set_icon(GTK_WINDOW(data->window), pixbuf);
    gtk_container_set_border_width(GTK_CONTAINER(data->window), 10);
    gtk_container_add(GTK_CONTAINER(data->window), vbox_root);

    data->comboBoxHorizontal = gtk_combo_box_new_text();
    data->comboBoxVertical = gtk_combo_box_new_text();
    data->difficulty_combo = gtk_combo_box_new_text();

    gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(data->comboBoxHorizontal), 4);
    gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(data->comboBoxVertical), 4);

    for (int i = 1; i <= 20; i++) {
        sprintf(buffer, "%d", i);
        gtk_combo_box_append_text(GTK_COMBO_BOX(data->comboBoxHorizontal),
                buffer);
        gtk_combo_box_append_text(GTK_COMBO_BOX(data->comboBoxVertical),
                buffer);
    }
    gtk_combo_box_append_text(GTK_COMBO_BOX(data->difficulty_combo), "Easy");
    gtk_combo_box_append_text(GTK_COMBO_BOX(data->difficulty_combo), "Medium");
    gtk_combo_box_append_text(GTK_COMBO_BOX(data->difficulty_combo), "Hard");

    gtk_combo_box_set_active(GTK_COMBO_BOX(data->comboBoxHorizontal), 3);
    gtk_combo_box_set_active(GTK_COMBO_BOX(data->comboBoxVertical), 3);
    gtk_combo_box_set_active(GTK_COMBO_BOX(data->difficulty_combo), 2);
    gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(data->difficulty_combo),
            GTK_SENSITIVITY_OFF);

    GtkWidget * hbox = gtk_hbox_new(FALSE, 10);
    label = gtk_label_new("Number of Horizontal Line(s)    ");

    gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
    gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
    gtk_box_pack_start(GTK_BOX(hbox), data->comboBoxHorizontal, TRUE, FALSE, 4);

    gtk_box_pack_start(GTK_BOX(vbox_root), hbox, FALSE, FALSE, 4);

    hbox = gtk_hbox_new(FALSE, 10);
    label = gtk_label_new("Number of Vertical Line(s)       ");

    gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
    gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
    gtk_box_pack_start(GTK_BOX(hbox), data->comboBoxVertical, TRUE, FALSE, 4);

    gtk_box_pack_start(GTK_BOX(vbox_root), hbox, FALSE, FALSE, 4);

    hseparator = gtk_hseparator_new();
    gtk_box_pack_start(GTK_BOX(vbox_root), hseparator, FALSE, FALSE, 5);

    hbox_root = gtk_hbox_new(FALSE, 4);

    GtkWidget *vbox = gtk_vbox_new(TRUE, 4);
    label = gtk_label_new("Player 1 : ");
    gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
    gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5);

    label = gtk_label_new("Player 2 : ");
    gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
    gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(hbox_root), vbox, FALSE, FALSE, 0);

    vbox = gtk_vbox_new(TRUE, 4);

    data->player1Entry = gtk_entry_new();
    data->player2Entry = gtk_entry_new();
    gtk_widget_modify_font(data->player1Entry, font);
    gtk_widget_modify_font(data->player2Entry, font);
    gtk_entry_set_alignment(GTK_ENTRY(data->player1Entry), 0.5);
    gtk_entry_set_alignment(GTK_ENTRY(data->player2Entry), 0.5);
    gtk_entry_set_max_length(GTK_ENTRY(data->player1Entry),
            PLAYER_NAME_MAX_LENGTH);
    gtk_entry_set_max_length(GTK_ENTRY(data->player2Entry),
            PLAYER_NAME_MAX_LENGTH);
    gtk_entry_set_text(GTK_ENTRY(data->player1Entry), getenv("USER"));
    gtk_box_pack_start(GTK_BOX(vbox), data->player1Entry, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(vbox), data->player2Entry, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(hbox_root), vbox, FALSE, FALSE, 0);

    vseparator = gtk_vseparator_new();
    gtk_box_pack_start(GTK_BOX(hbox_root), vseparator, FALSE, FALSE, 5);

    label = gtk_label_new("Opponent ");
    gtk_box_pack_start(GTK_BOX(hbox_root), label, FALSE, FALSE, 5);

    vbox = gtk_vbox_new(TRUE, 4);
    data->radioButtonHuman = gtk_radio_button_new_with_label(NULL, "Human");
    data->radioButtonComputer = gtk_radio_button_new_with_label_from_widget
            (GTK_RADIO_BUTTON(data->radioButtonHuman), "Computer");
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->radioButtonHuman),
            TRUE);
    gtk_box_pack_start(GTK_BOX(vbox), data->radioButtonHuman, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(vbox), data->radioButtonComputer, TRUE, TRUE, 5);
    gtk_box_pack_start(GTK_BOX(hbox_root), vbox, FALSE, FALSE, 0);

    gtk_box_pack_start(GTK_BOX(vbox_root), hbox_root, FALSE, FALSE, 5);

    hseparator = gtk_hseparator_new();
    gtk_box_pack_start(GTK_BOX(vbox_root), hseparator, FALSE, FALSE, 5);

    hbox = gtk_hbox_new(TRUE, 10);

    label = gtk_label_new("AI Difficulty ");
    gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(hbox), data->difficulty_combo, TRUE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(vbox_root), hbox, FALSE, FALSE, 5);

    hseparator = gtk_hseparator_new();
    gtk_box_pack_start(GTK_BOX(vbox_root), hseparator, FALSE, FALSE, 5);

    hbox = gtk_hbox_new(FALSE, 10);

    apply_button = gtk_button_new_with_label("Apply");
    quit_button = gtk_button_new_with_label("Quit");
    gtk_widget_set_size_request(apply_button, 80, 35);
    gtk_widget_set_size_request(quit_button, 80, 35);
    gtk_box_pack_end(GTK_BOX(hbox), quit_button, FALSE, TRUE, 10);
    gtk_box_pack_end(GTK_BOX(hbox), apply_button, FALSE, TRUE, 10);

    gtk_box_pack_start(GTK_BOX(vbox_root), hbox, FALSE, FALSE, 5);


    g_signal_connect(data->window, "delete-event",
            G_CALLBACK(window_delete), NULL);
    g_signal_connect(apply_button, "clicked",
            G_CALLBACK(parse_user_data), (gpointer) data);
    g_signal_connect(quit_button, "clicked",
            G_CALLBACK(destroy), (gpointer) data);
    g_signal_connect(data->radioButtonComputer, "toggled",
            G_CALLBACK(toggle_event), (gpointer) data);
    g_signal_connect(GTK_ENTRY(data->player1Entry), "focus-in-event",
            G_CALLBACK(focus_text_entry_event), NULL);
    g_signal_connect(GTK_ENTRY(data->player2Entry), "focus-in-event",
            G_CALLBACK(focus_text_entry_event), NULL);

    gtk_widget_show_all(data->window);
    gtk_main();

    memmove((void *) horizontalLines, (void *) &(data->horizontalLines), sizeof
            (int));
    memmove((void *) verticalLines, (void *) &(data->verticalLines), sizeof
            (int));
    *player1 = (char *) calloc(strlen(data->player1Name) + 1, sizeof (char));
    *player2 = (char *) calloc(strlen(data->player2Name) + 1, sizeof (char));
    strcpy(*player1, data->player1Name);
    strcpy(*player2, data->player2Name);
    *opponent = data->opponent;
    if (data->opponent == COMPUTER)
        *difficulty = data->difficulty;

    gtk_widget_destroy(data->window);

    pango_font_description_free(font); 
    free(data->player1Name);
    free(data->player2Name);
    g_slice_free(Data, data);
    //free(pixbuf);
    
    return true;
}
Ejemplo n.º 6
0
/**
 * na_gtk_utils_set_editable:
 * @widget: the #GtkWdiget.
 * @editable: whether the @widget is editable or not.
 *
 * Try to set a visual indication of whether the @widget is editable or not.
 *
 * Having a GtkWidget should be enough, but we also deal with a GtkTreeViewColumn.
 * So the most-bottom common ancestor is just GObject (since GtkObject having been
 * deprecated in Gtk+-3.0)
 *
 * Note that using 'sensitivity' property is just a work-around because the
 * two things have distinct semantics:
 * - editable: whether we are allowed to modify the value (is not read-only)
 * - sensitive: whether the value is relevant (has a sense in this context)
 */
void
na_gtk_utils_set_editable( GObject *widget, gboolean editable )
{
	GList *renderers, *irender;

/* GtkComboBoxEntry is deprecated from Gtk+3
 * see. http://git.gnome.org/browse/gtk+/commit/?id=9612c648176378bf237ad0e1a8c6c995b0ca7c61
 * while 'has_entry' property exists since 2.24
 */
#if GTK_CHECK_VERSION( 2,24,0 )
	if( GTK_IS_COMBO_BOX( widget ) && gtk_combo_box_get_has_entry( GTK_COMBO_BOX( widget ))){
#else
	if( GTK_IS_COMBO_BOX_ENTRY( widget )){
#endif
		/* idem as GtkEntry */
		gtk_editable_set_editable( GTK_EDITABLE( gtk_bin_get_child( GTK_BIN( widget ))), editable );
		g_object_set( G_OBJECT( gtk_bin_get_child( GTK_BIN( widget ))), "can-focus", editable, NULL );
		/* disable the listbox button itself */
		gtk_combo_box_set_button_sensitivity( GTK_COMBO_BOX( widget ), editable ? GTK_SENSITIVITY_ON : GTK_SENSITIVITY_OFF );

	} else if( GTK_IS_COMBO_BOX( widget )){
		/* disable the listbox button itself */
		gtk_combo_box_set_button_sensitivity( GTK_COMBO_BOX( widget ), editable ? GTK_SENSITIVITY_ON : GTK_SENSITIVITY_OFF );

	} else if( GTK_IS_ENTRY( widget )){
		gtk_editable_set_editable( GTK_EDITABLE( widget ), editable );
		/* removing the frame leads to a disturbing modification of the
		 * height of the control */
		/*g_object_set( G_OBJECT( widget ), "has-frame", editable, NULL );*/
		/* this prevents the caret to be displayed when we click in the entry */
		g_object_set( G_OBJECT( widget ), "can-focus", editable, NULL );

	} else if( GTK_IS_TEXT_VIEW( widget )){
		g_object_set( G_OBJECT( widget ), "can-focus", editable, NULL );
		gtk_text_view_set_editable( GTK_TEXT_VIEW( widget ), editable );

	} else if( GTK_IS_TOGGLE_BUTTON( widget )){
		/* transforms to a quasi standard GtkButton */
		/*g_object_set( G_OBJECT( widget ), "draw-indicator", editable, NULL );*/
		/* this at least prevent the keyboard focus to go to the button
		 * (which is better than nothing) */
		g_object_set( G_OBJECT( widget ), "can-focus", editable, NULL );

	} else if( GTK_IS_TREE_VIEW_COLUMN( widget )){
		renderers = gtk_cell_layout_get_cells( GTK_CELL_LAYOUT( GTK_TREE_VIEW_COLUMN( widget )));
		for( irender = renderers ; irender ; irender = irender->next ){
			if( GTK_IS_CELL_RENDERER_TEXT( irender->data )){
				g_object_set( G_OBJECT( irender->data ), "editable", editable, "editable-set", TRUE, NULL );
			}
		}
		g_list_free( renderers );

	} else if( GTK_IS_BUTTON( widget )){
		gtk_widget_set_sensitive( GTK_WIDGET( widget ), editable );
	}
}

/**
 * na_gtk_utils_radio_set_initial_state:
 * @button: the #GtkRadioButton button which is initially active.
 * @handler: the corresponding "toggled" handler.
 * @user_data: the user data associated to the handler.
 * @editable: whether this radio button group is editable.
 * @sensitive: whether this radio button group is sensitive.
 *
 * This function should be called for the button which is initially active
 * inside of a radio button group when the radio group may happen to not be
 * editable.
 * This function should be called only once for the radio button group.
 *
 * It does the following operations:
 * - set the button as active
 * - set other buttons of the radio button group as icactive
 * - set all buttons of radio button group as @editable
 *
 * The initially active @button, along with its @handler, are recorded
 * as properties of the radio button group (actually as properties of each
 * radio button of the group), so that they can later be used to reset the
 * initial state.
 */
void
na_gtk_utils_radio_set_initial_state( GtkRadioButton *button,
		GCallback handler, void *user_data, gboolean editable, gboolean sensitive )
{
	GSList *group, *ig;
	GtkRadioButton *other;

	group = gtk_radio_button_get_group( button );

	for( ig = group ; ig ; ig = ig->next ){
		other = GTK_RADIO_BUTTON( ig->data );
		g_object_set_data( G_OBJECT( other ), NA_TOGGLE_DATA_BUTTON, button );
		g_object_set_data( G_OBJECT( other ), NA_TOGGLE_DATA_HANDLER, handler );
		g_object_set_data( G_OBJECT( other ), NA_TOGGLE_DATA_USER_DATA, user_data );
		g_object_set_data( G_OBJECT( other ), NA_TOGGLE_DATA_EDITABLE, GUINT_TO_POINTER( editable ));
		na_gtk_utils_set_editable( G_OBJECT( other ), editable );
		gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( other ), FALSE );
		gtk_widget_set_sensitive( GTK_WIDGET( other ), sensitive );
	}

	gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
}