All pastes #37333 Raw Edit

cavehippo

public text v1 · immutable
#37333 ·published 2006-01-19 01:57 UTC
rendered paste body
static GtkWidget *gtkwin_spell = NULL; /* spell window */
static GtkWidget *description  = NULL; /* the text box containing spell description */

static void select_spell_event(GtkWidget *gtklist, gint row, gint column, 
    GdkEventButton *event) {

    char command[MAX_BUF], *message;
    Spell *spell = gtk_clist_get_row_data (GTK_CLIST(gtklist), row);

    if (event->button==1) { /* left click, select spell, show description */
	gtk_text_freeze(GTK_TEXT(description));
	gtk_text_set_point(GTK_TEXT(description), 0);
	gtk_text_forward_delete(GTK_TEXT(description), gtk_text_get_length(GTK_TEXT(description)));
	message = spell->message;
	gtk_text_insert(GTK_TEXT(description), NULL, NULL, NULL, message, -1);
	gtk_text_thaw(GTK_TEXT(description));
    }
    else if (event->button==2) { /* middle click, invoke spell */
	sprintf(command, "invoke %d", spell->tag);
	send_command(command, -1, 1);
    }
    else if (event->button==3) { /* right click, cast spell */
	sprintf(command, "cast %d", spell->tag);
	send_command(command, -1, 1);
    }
}

void menu_cast () {
    gtkwin_spell = gtk_window_new (GTK_WINDOW_DIALOG);
    GtkWidget * scroll_window;
    GtkWidget * list;
    GtkStyle * liststyle;
    GtkWidget * vbox;
    gint row; 
    char buffer[2][MAX_BUF];
    char *columns[2];
    Spell *spell;

    scroll_window = gtk_scrolled_window_new (0,0);
    gchar *titles[] = {"Name", "Cost"};
    list = gtk_clist_new_with_titles(2, titles);
    gtk_clist_set_column_width(GTK_CLIST(list), 0, 200);
    gtk_clist_set_column_width(GTK_CLIST(list), 1, 200);
    gtk_clist_set_selection_mode(GTK_CLIST(list) , GTK_SELECTION_BROWSE);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window),
                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_window_set_title(GTK_WINDOW (gtkwin_spell), "Cast Spell");
    gtk_clist_set_row_height (GTK_CLIST(list), image_size); 


    liststyle = gtk_rc_get_style(list);
    if (liststyle) {
      liststyle->bg[GTK_STATE_SELECTED] = gdk_grey;
      liststyle->fg[GTK_STATE_SELECTED] = gdk_black;
      gtk_widget_set_style (list, liststyle);
    }
    /* let's add the spells to the list now */
    for (spell = cpl.spelldata; spell; spell=spell->next) {
	if (!spell) break;
	printf ("Spell: %s\n", spell->name);
	buffer[1][0]='\0';
	strcpy(buffer[0], spell->name);
	columns[0] = buffer[0];
	columns[1] = buffer[1];
	if (spell->sp) sprintf(buffer[1], "%d mana ", spell->sp);
	if (spell->grace) sprintf(buffer[1]+strlen(buffer[1]), "%d grace ", spell->grace);
	if (spell->dam) sprintf(buffer[1]+strlen(buffer[1]), "%d damage ", spell->dam);

	/* the columns array doesn't yet contain the data we need, but we can't set the 
	 * row colour until we create the row, so we create the row with gtk_clist_append()
	 *  set the colour, then reset the text in the second column 
	 */
	row = gtk_clist_append(GTK_CLIST(list), columns);
	gtk_clist_set_row_data(GTK_CLIST(list), row, spell); 
	if (spell->path & cpl.stats.denied) {
	    gtk_clist_set_background (GTK_CLIST(list), row, &root_color[NDI_RED]);
	    strcat(buffer[1], "(DENIED) ");
	}
	else if (spell->path & cpl.stats.attuned) {
	    gtk_clist_set_background (GTK_CLIST(list), row, &root_color[NDI_GREEN]);
	    strcat(buffer[1], "(attuned) ");
	}
	else if (spell->path & cpl.stats.repelled) {
	    gtk_clist_set_background (GTK_CLIST(list), row, &root_color[NDI_ORANGE]);
	    strcat(buffer[1], "(denied) ");
	}
	gtk_clist_set_text(GTK_CLIST(list), row, 1, columns[1]);

    }
    /* vbox splits the window - top portion is the spell list, bottom
     * portion is for the descriptions of the spells
     */
    vbox = gtk_vbox_new(FALSE, 2);
    description = gtk_text_new(NULL, NULL);
    gtk_text_set_editable(GTK_TEXT (description), FALSE);

    /* set the actions for the mouse buttons to trigger the callback function */

    gtk_clist_set_button_actions(GTK_CLIST(list), 1, GTK_BUTTON_SELECTS);
    gtk_clist_set_button_actions(GTK_CLIST(list), 2, GTK_BUTTON_SELECTS);
    gtk_signal_connect(GTK_OBJECT(list), "select_row",
        GTK_SIGNAL_FUNC(select_spell_event), NULL);

    /* ok, time to pack it all up */
    gtk_container_add(GTK_CONTAINER(gtkwin_spell),vbox);
    gtk_container_add(GTK_CONTAINER(scroll_window), list);
    gtk_box_pack_start(GTK_BOX(vbox), scroll_window, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(vbox), description, FALSE, FALSE, 0);
    gtk_widget_show(vbox);
    gtk_widget_show(scroll_window);
    gtk_widget_show(list);
    gtk_widget_show(description);
    gtk_widget_show(gtkwin_spell);

}