594a9d6f540b07b96cabdb5a16c358dda8ab9982
[dana/openbox.git] / tools / obconf / main.c
1 /*#include "obconf.h"
2   #include "plugins.h"*/
3 #include "parser/parse.h"
4 #include "gettext.h"
5
6 #include <gtk/gtk.h>
7 #include <gdk-pixbuf/gdk-pixbuf.h>
8
9 #define OB_ICON "openbox-icon"
10
11 static GtkWidget *mainwin;
12 static GtkWidget *mainlist;
13 static GtkListStore *mainstore;
14 static GtkWidget *mainnote;
15 static GdkPixbuf *ob_icon;
16
17 enum {
18     NAME_COLUMN,
19     N_COLUMNS
20 };
21
22 gboolean on_mainwindow_delete_event(GtkWidget *w, GdkEvent *e, gpointer d);
23 void on_quit_activate(GtkMenuItem *item, gpointer d);
24 void on_applybutton_clicked(GtkButton *but, gpointer d);
25 void on_revertbutton_clicked(GtkButton *but, gpointer d);
26 void on_helpbutton_clicked(GtkButton *but, gpointer d);
27 void on_selection_changed(GtkTreeSelection *selection, gpointer data);
28
29 static void obconf_error(GError *e)
30 {
31     GtkWidget *d;
32
33     d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
34                                GTK_DIALOG_DESTROY_WITH_PARENT,
35                                GTK_MESSAGE_ERROR,
36                                GTK_BUTTONS_CLOSE,
37                                "%s", e->message);
38     g_signal_connect_swapped(GTK_OBJECT(d), "response",
39                              G_CALLBACK(gtk_widget_destroy),
40                              GTK_OBJECT(d));
41     gtk_widget_show(d);
42 }
43
44 static void load_stock ()
45 {
46     GtkIconFactory *factory;
47     GError *e = NULL;
48
49     gtk_icon_factory_add_default (factory = gtk_icon_factory_new ());
50
51     ob_icon = gdk_pixbuf_new_from_file (PIXMAPDIR G_DIR_SEPARATOR_S
52                                         "openbox.png", &e);
53     if (!ob_icon) {
54         gchar *msg = g_strdup_printf 
55             (_("Failed to load the Openbox icon, Openbox is probably not "
56                "installed correctly. The error given was '%s'."),
57              e->message);
58         g_free (e->message);
59         e->message = msg;
60         obconf_error (e);
61     } else {
62         GtkIconSet *set;
63
64         set = gtk_icon_set_new_from_pixbuf (ob_icon);
65         gtk_icon_factory_add (factory, OB_ICON, set);
66         gtk_icon_set_unref (set);
67     }
68 }
69
70 GtkWidget* build_menu(GtkWidget *win, GtkAccelGroup *accel)
71 {
72     GtkWidget *menu;
73     GtkWidget *submenu;
74     GtkWidget *item;
75
76     menu = gtk_menu_bar_new();
77
78     /* File menu */
79
80     submenu = gtk_menu_new();
81     gtk_menu_set_accel_group(GTK_MENU(submenu), accel);
82
83     item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel);
84     g_signal_connect(item, "activate", G_CALLBACK(on_quit_activate), NULL);
85     gtk_menu_append(GTK_MENU(submenu), item);
86
87     item = gtk_menu_item_new_with_mnemonic("_File");
88     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
89     gtk_menu_bar_append(GTK_MENU_BAR(menu), item);
90
91     /* About menu */
92
93     submenu = gtk_menu_new();
94     gtk_menu_set_accel_group(GTK_MENU(submenu), accel);
95
96     item = gtk_menu_item_new_with_mnemonic("_About");
97     gtk_menu_append(GTK_MENU(submenu), item);
98
99     item = gtk_menu_item_new_with_mnemonic("_Help");
100     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
101     gtk_menu_bar_append(GTK_MENU_BAR(menu), item);
102
103     gtk_widget_show_all(menu);
104
105     return menu;
106 }
107
108 GtkWidget* build_list(GtkWidget *parent, GtkListStore **model)
109 {
110     GtkWidget *list;
111     GtkListStore *store;
112     GtkCellRenderer *ren;
113     GtkTreeViewColumn *col;
114     GtkTreeSelection *sel;
115
116     store = gtk_list_store_new(N_COLUMNS,
117                                G_TYPE_STRING);
118
119     list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
120
121     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
122     gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
123     g_signal_connect(sel, "changed", G_CALLBACK(on_selection_changed), NULL);
124     
125     ren = gtk_cell_renderer_text_new();
126     col = gtk_tree_view_column_new_with_attributes("Name",
127                                                    ren,
128                                                    "text",
129                                                    NAME_COLUMN,
130                                                    NULL);
131     gtk_tree_view_append_column(GTK_TREE_VIEW(list), col);
132
133     *model = store;
134     return list;
135 }
136
137 int main(int argc, char **argv)
138 {
139     GtkWidget *menu;
140     GtkWidget *vbox;
141     GtkWidget *hbox;
142     GtkAccelGroup *accel;
143
144     gtk_set_locale();
145     gtk_init(&argc, &argv);
146
147     mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
148     gtk_window_set_title(GTK_WINDOW(mainwin), "Obconf");
149     gtk_window_set_wmclass(GTK_WINDOW(mainwin), "obconf", "Obconf");
150     gtk_window_set_role(GTK_WINDOW(mainwin), "main window");
151
152     g_signal_connect(mainwin, "delete-event",
153                      G_CALLBACK(on_mainwindow_delete_event), NULL);
154
155     accel = gtk_accel_group_new();
156     gtk_window_add_accel_group(GTK_WINDOW(mainwin), accel);
157
158     vbox = gtk_vbox_new(FALSE, 3);
159     gtk_container_add(GTK_CONTAINER(mainwin), vbox);
160
161     /* Menu */
162
163     menu = build_menu(mainwin, accel);
164     gtk_box_pack_start(GTK_BOX(vbox), menu, FALSE, FALSE, 0);
165
166     hbox = gtk_hbox_new(FALSE, 2);
167     gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
168
169     /* List */
170
171     mainlist = build_list(hbox, &mainstore);
172
173     gtk_widget_show_all(mainwin);
174
175     load_stock();
176     if (ob_icon) gtk_window_set_icon(GTK_WINDOW(mainwin), ob_icon);
177
178     gtk_main();
179     return 0;
180 }
181
182 gboolean on_mainwindow_delete_event(GtkWidget *w, GdkEvent *e, gpointer d)
183 {
184     gtk_main_quit();
185     return FALSE;
186 }
187
188 void on_quit_activate(GtkMenuItem *item, gpointer d)
189 {
190     gtk_main_quit();
191 }
192
193 void on_applybutton_clicked(GtkButton *but, gpointer d)
194 {
195     g_message("apply");
196 }
197
198 void on_revertbutton_clicked(GtkButton *but, gpointer d)
199 {
200     g_message("revert");
201 }
202
203 void on_helpbutton_clicked(GtkButton *but, gpointer d)
204 {
205     g_message("help");
206 }
207
208 void on_selection_changed(GtkTreeSelection *sel, gpointer data)
209 {
210     GtkTreeIter iter;
211     GtkTreeModel *model;
212
213     if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
214         g_message("activated");
215     } else {
216         g_message("none activated");
217     }
218 }