gcompat: provide backwards compatible functions for older gtk versions
[fio.git] / ghelpers.c
index d49af10ef20b0b2e046661472b7baa8ec0aee0e0..d9380a2a0c2a4323c144a5e4c5089c4f0347254f 100644 (file)
@@ -1,11 +1,16 @@
+#include <stdlib.h>
+#include <string.h>
 #include <gtk/gtk.h>
 
+#include "gcompat.h"
+#include "ghelpers.h"
+
 GtkWidget *new_combo_entry_in_frame(GtkWidget *box, const char *label)
 {
        GtkWidget *entry, *frame;
 
        frame = gtk_frame_new(label);
-       entry = gtk_combo_box_new_text();
+       entry = gtk_combo_box_text_new();
        gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
        gtk_container_add(GTK_CONTAINER(frame), entry);
 
@@ -18,7 +23,7 @@ GtkWidget *new_info_entry_in_frame(GtkWidget *box, const char *label)
 
        frame = gtk_frame_new(label);
        entry = gtk_entry_new();
-       gtk_entry_set_editable(GTK_ENTRY(entry), 0);
+       gtk_editable_set_editable(GTK_EDITABLE(entry), 0);
        gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
        gtk_container_add(GTK_CONTAINER(frame), entry);
 
@@ -69,3 +74,103 @@ void entry_set_int_value(GtkWidget *entry, unsigned int val)
        sprintf(tmp, "%u", val);
        gtk_entry_set_text(GTK_ENTRY(entry), tmp);
 }
+
+GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title, unsigned int flags)
+{
+       GtkCellRenderer *renderer;
+       GtkTreeViewColumn *col;
+       double xalign = 0.0; /* left as default */
+       PangoAlignment align;
+       gboolean visible;
+
+       align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
+               (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
+               PANGO_ALIGN_CENTER;
+       visible = !(flags & INVISIBLE);
+
+       renderer = gtk_cell_renderer_text_new();
+       col = gtk_tree_view_column_new();
+
+       gtk_tree_view_column_set_title(col, title);
+       if (!(flags & UNSORTABLE))
+               gtk_tree_view_column_set_sort_column_id(col, index);
+       gtk_tree_view_column_set_resizable(col, TRUE);
+       gtk_tree_view_column_pack_start(col, renderer, TRUE);
+       gtk_tree_view_column_set_expand(col, TRUE);
+       gtk_tree_view_column_add_attribute(col, renderer, "text", index);
+       g_object_set(G_OBJECT(renderer), "alignment", align, NULL);
+       switch (align) {
+       case PANGO_ALIGN_LEFT:
+               xalign = 0.0;
+               break;
+       case PANGO_ALIGN_CENTER:
+               xalign = 0.5;
+               break;
+       case PANGO_ALIGN_RIGHT:
+               xalign = 1.0;
+               break;
+       }
+       gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
+       gtk_tree_view_column_set_visible(col, visible);
+       gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
+       return col;
+}
+
+void multitext_add_entry(struct multitext_widget *mt, const char *text)
+{
+       mt->text = realloc(mt->text, (mt->max_text + 1) * sizeof(char *));
+       mt->text[mt->max_text] = strdup(text);
+       mt->max_text++;
+}
+
+void multitext_set_entry(struct multitext_widget *mt, unsigned int index)
+{
+       if (index >= mt->max_text)
+               return;
+       if (!mt->text || !mt->text[index])
+               return;
+
+       mt->cur_text = index;
+       gtk_entry_set_text(GTK_ENTRY(mt->entry), mt->text[index]);
+}
+
+void multitext_update_entry(struct multitext_widget *mt, unsigned int index,
+                           const char *text)
+{
+       if (!mt->text)
+               return;
+
+       if (mt->text[index])
+               free(mt->text[index]);
+
+       mt->text[index] = strdup(text);
+       if (mt->cur_text == index)
+               gtk_entry_set_text(GTK_ENTRY(mt->entry), mt->text[index]);
+}
+
+void multitext_free(struct multitext_widget *mt)
+{
+       int i;
+
+       gtk_entry_set_text(GTK_ENTRY(mt->entry), "");
+
+       for (i = 0; i < mt->max_text; i++) {
+               if (mt->text[i])
+                       free(mt->text[i]);
+       }
+
+       free(mt->text);
+       mt->cur_text = -1;
+       mt->max_text = 0;
+}
+
+GtkWidget *get_scrolled_window(gint border_width)
+{
+       GtkWidget *scroll;
+
+       scroll = gtk_scrolled_window_new(NULL, NULL);
+       gtk_container_set_border_width(GTK_CONTAINER(scroll), border_width);
+       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+
+       return scroll;
+}