has a pane and list and workarea
[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 *mainworkarea;
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(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(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     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
121
122     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
123     gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
124     g_signal_connect(sel, "changed", G_CALLBACK(on_selection_changed), NULL);
125     
126     ren = gtk_cell_renderer_text_new();
127     col = gtk_tree_view_column_new_with_attributes("Name",
128                                                    ren,
129                                                    "text",
130                                                    NAME_COLUMN,
131                                                    NULL);
132     gtk_tree_view_append_column(GTK_TREE_VIEW(list), col);
133
134     *model = store;
135     return list;
136 }
137
138 int main(int argc, char **argv)
139 {
140     GtkWidget *menu;
141     GtkWidget *vbox;
142     GtkWidget *hpane;
143     GtkAccelGroup *accel;
144
145     gtk_set_locale();
146     gtk_init(&argc, &argv);
147
148     mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
149     gtk_window_set_title(GTK_WINDOW(mainwin), "Obconf");
150     gtk_window_set_wmclass(GTK_WINDOW(mainwin), "obconf", "Obconf");
151     gtk_window_set_role(GTK_WINDOW(mainwin), "main window");
152
153     g_signal_connect(mainwin, "delete-event",
154                      G_CALLBACK(on_mainwindow_delete_event), NULL);
155
156     accel = gtk_accel_group_new();
157     gtk_window_add_accel_group(GTK_WINDOW(mainwin), accel);
158
159     vbox = gtk_vbox_new(FALSE, 3);
160     gtk_container_add(GTK_CONTAINER(mainwin), vbox);
161
162     /* Menu */
163
164     menu = build_menu(accel);
165     gtk_box_pack_start(GTK_BOX(vbox), menu, FALSE, FALSE, 0);
166
167     hpane = gtk_hpaned_new();
168     gtk_box_pack_start(GTK_BOX(vbox), hpane, TRUE, TRUE, 0);
169
170     /* List */
171
172     mainlist = build_list(&mainstore);
173     gtk_container_add(GTK_CONTAINER(hpane), mainlist);
174
175     /* Main work area */
176
177     mainworkarea = gtk_vbox_new(FALSE, 1);
178     gtk_container_add(GTK_CONTAINER(hpane), mainworkarea);
179
180     gtk_widget_show_all(mainwin);
181
182     load_stock();
183     if (ob_icon) gtk_window_set_icon(GTK_WINDOW(mainwin), ob_icon);
184
185     gtk_main();
186     return 0;
187 }
188
189 gboolean on_mainwindow_delete_event(GtkWidget *w, GdkEvent *e, gpointer d)
190 {
191     gtk_main_quit();
192     return FALSE;
193 }
194
195 void on_quit_activate(GtkMenuItem *item, gpointer d)
196 {
197     gtk_main_quit();
198 }
199
200 void on_applybutton_clicked(GtkButton *but, gpointer d)
201 {
202     g_message("apply");
203 }
204
205 void on_revertbutton_clicked(GtkButton *but, gpointer d)
206 {
207     g_message("revert");
208 }
209
210 void on_helpbutton_clicked(GtkButton *but, gpointer d)
211 {
212     g_message("help");
213 }
214
215 void on_selection_changed(GtkTreeSelection *sel, gpointer data)
216 {
217     GtkTreeIter iter;
218     GtkTreeModel *model;
219
220     if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
221         g_message("activated");
222     } else {
223         g_message("none activated");
224     }
225 }