1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* patheditor.c - provides a custom widget for manipulating a SearchPath object.
*
* Copyright (c) 2006 by Alastair M. Robinson
* Distributed under the terms of the GNU General Public License -
* see the file named "COPYING" for more details.
*
*/
/* FIXME: A destructor is needed for this class, to free up the option list.
As it stands, a certain amount of memory will be lost when the widget is
destroyed.
*/
#include <iostream>
#include <string.h>
#include <gtk/gtkentry.h>
#include <gtk/gtklist.h>
#include <gtk/gtkfilesel.h>
#include <gtk/gtktreeselection.h>
#include <gtk/gtkscrolledwindow.h>
#include "generaldialogs.h"
#include "patheditor.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gettext.h"
#define _(x) gettext(x)
using namespace std;
enum {
CHANGED_SIGNAL,
LAST_SIGNAL
};
static guint patheditor_signals[LAST_SIGNAL] = { 0 };
static void patheditor_class_init (PathEditorClass *klass);
static void patheditor_init (PathEditor *sel);
static void patheditor_addpath(GtkWidget *button,gpointer user_data)
{
PathEditor *pe=PATHEDITOR(user_data);
char *dir=NULL;
if((dir=Directory_Dialog(_("Select directory..."),NULL,NULL)))
{
GtkTreeIter iter1;
gtk_list_store_append(pe->liststore,&iter1);
gtk_list_store_set(pe->liststore,&iter1,0,dir,-1);
g_signal_emit(G_OBJECT (pe),patheditor_signals[CHANGED_SIGNAL], 0);
}
}
static void patheditor_removepath(GtkWidget *button,gpointer user_data)
{
PathEditor *pe=PATHEDITOR(user_data);
GtkTreeIter iter;
GtkTreeSelection *select;
GtkTreeModel *model;
select = gtk_tree_view_get_selection (GTK_TREE_VIEW (pe->treeview));
if (gtk_tree_selection_get_selected (select, &model, &iter))
{
gtk_list_store_remove(pe->liststore,&iter);
g_signal_emit(G_OBJECT (pe),patheditor_signals[CHANGED_SIGNAL], 0);
}
}
GtkWidget*
patheditor_new (SearchPath *sp)
{
PathEditor *c=PATHEDITOR(g_object_new (patheditor_get_type (), NULL));
c->liststore=gtk_list_store_new(1,G_TYPE_STRING);
GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),GTK_SHADOW_ETCHED_IN);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),GTK_POLICY_NEVER,GTK_POLICY_AUTOMATIC);
gtk_box_pack_start (GTK_BOX (c), sw, TRUE, TRUE, 0);
gtk_widget_show(sw);
c->treeview=gtk_tree_view_new_with_model(GTK_TREE_MODEL(c->liststore));
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
renderer=gtk_cell_renderer_text_new();
column=gtk_tree_view_column_new_with_attributes(_("Path"),renderer,"text",0,NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(c->treeview),column);
gtk_container_add(GTK_CONTAINER(sw),c->treeview);
gtk_widget_show(c->treeview);
patheditor_set_paths(c,sp);
GtkWidget *hbox=gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(c),hbox,FALSE,FALSE,0);
gtk_widget_show(hbox);
GtkWidget *addbutton,*removebutton;
addbutton=gtk_button_new_with_label(_("Add..."));
gtk_box_pack_start(GTK_BOX(hbox),addbutton,TRUE,TRUE,0);
g_signal_connect(addbutton,"clicked",G_CALLBACK(patheditor_addpath),c);
gtk_widget_show(addbutton);
removebutton=gtk_button_new_with_label(_("Remove"));
gtk_box_pack_start(GTK_BOX(hbox),removebutton,TRUE,TRUE,0);
g_signal_connect(removebutton,"clicked",G_CALLBACK(patheditor_removepath),c);
gtk_widget_show(removebutton);
return(GTK_WIDGET(c));
}
GType
patheditor_get_type (void)
{
static GType stpuic_type = 0;
if (!stpuic_type)
{
static const GTypeInfo patheditor_info =
{
sizeof (PathEditorClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) patheditor_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (PathEditor),
0,
(GInstanceInitFunc) patheditor_init,
};
stpuic_type = g_type_register_static (GTK_TYPE_VBOX, "PathEditor", &patheditor_info, GTypeFlags(0));
}
return stpuic_type;
}
static void
patheditor_class_init (PathEditorClass *klass)
{
patheditor_signals[CHANGED_SIGNAL] =
g_signal_new ("changed",
G_TYPE_FROM_CLASS (klass),
GSignalFlags(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
G_STRUCT_OFFSET (PathEditorClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
static void
patheditor_init (PathEditor *c)
{
c->list=NULL;
c->entry=NULL;
c->pathlist=NULL;
c->searchpath=NULL;
}
gboolean patheditor_refresh(PathEditor *c)
{
return(true);
}
void patheditor_get_paths(PathEditor *p,SearchPath *sp)
{
GtkTreeIter iter;
gboolean valid;
valid=gtk_tree_model_get_iter_first(GTK_TREE_MODEL(p->liststore),&iter);
sp->ClearPaths();
while(valid)
{
gchar *path;
gtk_tree_model_get(GTK_TREE_MODEL(p->liststore),&iter,0,&path,-1);
cerr << "Adding: " << path << endl;
sp->AddPath(path);
valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(p->liststore),&iter);
}
}
void patheditor_set_paths(PathEditor *p,SearchPath *sp)
{
p->searchpath=sp;
GtkTreeIter iter1;
const char *path=sp->GetNextPath(NULL);
while(path)
{
gtk_list_store_append(p->liststore,&iter1);
gtk_list_store_set(p->liststore,&iter1,0,path,-1);
path=sp->GetNextPath(path);
}
}
|