check for oldschool fonts
[mikachu/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_selection_changed(GtkTreeSelection *selection, gpointer data);
27
28 static void obconf_error(GError *e)
29 {
30     GtkWidget *d;
31
32     d = gtk_message_dialog_new(mainwin ? GTK_WINDOW(mainwin) : NULL,
33                                GTK_DIALOG_DESTROY_WITH_PARENT,
34                                GTK_MESSAGE_ERROR,
35                                GTK_BUTTONS_CLOSE,
36                                "%s", e->message);
37     g_signal_connect_swapped(GTK_OBJECT(d), "response",
38                              G_CALLBACK(gtk_widget_destroy),
39                              GTK_OBJECT(d));
40     gtk_widget_show(d);
41 }
42
43 static void load_stock ()
44 {
45     GtkIconFactory *factory;
46     GError *e = NULL;
47
48     gtk_icon_factory_add_default (factory = gtk_icon_factory_new ());
49
50     ob_icon = gdk_pixbuf_new_from_file (PIXMAPDIR G_DIR_SEPARATOR_S
51                                         "openbox.png", &e);
52     if (!ob_icon) {
53         gchar *msg = g_strdup_printf 
54             (_("Failed to load the Openbox icon, Openbox is probably not "
55                "installed correctly. The error given was '%s'."),
56              e->message);
57         g_free (e->message);
58         e->message = msg;
59         obconf_error (e);
60     } else {
61         GtkIconSet *set;
62
63         set = gtk_icon_set_new_from_pixbuf (ob_icon);
64         gtk_icon_factory_add (factory, OB_ICON, set);
65         gtk_icon_set_unref (set);
66     }
67 }
68
69 GtkWidget* build_menu(GtkAccelGroup *accel)
70 {
71     GtkWidget *menu;
72     GtkWidget *submenu;
73     GtkWidget *item;
74
75     menu = gtk_menu_bar_new();
76
77     /* File menu */
78
79     submenu = gtk_menu_new();
80     gtk_menu_set_accel_group(GTK_MENU(submenu), accel);
81
82     item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel);
83     g_signal_connect(item, "activate", G_CALLBACK(on_quit_activate), NULL);
84     gtk_menu_append(GTK_MENU(submenu), item);
85
86     item = gtk_menu_item_new_with_mnemonic("_File");
87     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
88     gtk_menu_bar_append(GTK_MENU_BAR(menu), item);
89
90     /* About menu */
91
92     submenu = gtk_menu_new();
93     gtk_menu_set_accel_group(GTK_MENU(submenu), accel);
94
95     item = gtk_menu_item_new_with_mnemonic("_About");
96     gtk_menu_append(GTK_MENU(submenu), item);
97
98     item = gtk_menu_item_new_with_mnemonic("_Help");
99     gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
100     gtk_menu_bar_append(GTK_MENU_BAR(menu), item);
101
102     gtk_widget_show_all(menu);
103
104     return menu;
105 }
106
107 GtkWidget* build_list(GtkListStore **model)
108 {
109     GtkWidget *list;
110     GtkListStore *store;
111     GtkCellRenderer *ren;
112     GtkTreeViewColumn *col;
113     GtkTreeSelection *sel;
114
115     store = gtk_list_store_new(N_COLUMNS,
116                                G_TYPE_STRING);
117
118     list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
119     gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
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 *hpane;
142     GtkAccelGroup *accel;
143     GtkWidget *sep;
144     GtkWidget *bbox;
145     GtkWidget *but;
146     GtkWidget *bar;
147
148     gtk_set_locale();
149     gtk_init(&argc, &argv);
150
151     mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
152     gtk_window_set_title(GTK_WINDOW(mainwin), "Obconf");
153     gtk_window_set_wmclass(GTK_WINDOW(mainwin), "obconf", "Obconf");
154     gtk_window_set_role(GTK_WINDOW(mainwin), "main window");
155
156     g_signal_connect(mainwin, "delete-event",
157                      G_CALLBACK(on_mainwindow_delete_event), NULL);
158
159     accel = gtk_accel_group_new();
160     gtk_window_add_accel_group(GTK_WINDOW(mainwin), accel);
161
162     vbox = gtk_vbox_new(FALSE, 5);
163     gtk_container_add(GTK_CONTAINER(mainwin), vbox);
164
165     /* Menu */
166
167     menu = build_menu(accel);
168     gtk_box_pack_start(GTK_BOX(vbox), menu, FALSE, FALSE, 0);
169
170     hpane = gtk_hpaned_new();
171     gtk_box_pack_start(GTK_BOX(vbox), hpane, TRUE, TRUE, 0);
172
173     /* List */
174
175     mainlist = build_list(&mainstore);
176     gtk_container_add(GTK_CONTAINER(hpane), mainlist);
177
178     /* Main work area */
179
180     mainworkarea = gtk_layout_new(NULL, NULL);
181     gtk_container_add(GTK_CONTAINER(hpane), mainworkarea);
182
183     /* Separator */
184
185     sep = gtk_hseparator_new();
186     gtk_box_pack_start(GTK_BOX(vbox), sep, FALSE, FALSE, 0);
187
188     /* Button box */
189
190     bbox = gtk_hbutton_box_new();
191     gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
192     gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 6);
193     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
194
195     /* Revert Button */
196
197     but = gtk_button_new_with_mnemonic("_Revert");
198     gtk_box_pack_start(GTK_BOX(bbox), but, FALSE, FALSE, 0);
199
200     /* Apply Button */
201
202     but = gtk_button_new_with_mnemonic("_Apply");
203     gtk_box_pack_start(GTK_BOX(bbox), but, FALSE, FALSE, 0);
204
205     /* Status bar */
206
207     bar = gtk_statusbar_new();
208     gtk_box_pack_start(GTK_BOX(vbox), bar, FALSE, FALSE, 0);
209
210     gtk_widget_show_all(mainwin);
211
212     load_stock();
213     if (ob_icon) gtk_window_set_icon(GTK_WINDOW(mainwin), ob_icon);
214
215     gtk_main();
216     return 0;
217 }
218
219 gboolean on_mainwindow_delete_event(GtkWidget *w, GdkEvent *e, gpointer d)
220 {
221     gtk_main_quit();
222     return FALSE;
223 }
224
225 void on_quit_activate(GtkMenuItem *item, gpointer d)
226 {
227     gtk_main_quit();
228 }
229
230 void on_applybutton_clicked(GtkButton *but, gpointer d)
231 {
232     g_message("apply");
233 }
234
235 void on_revertbutton_clicked(GtkButton *but, gpointer d)
236 {
237     g_message("revert");
238 }
239
240 void on_selection_changed(GtkTreeSelection *sel, gpointer data)
241 {
242     GtkTreeIter iter;
243     GtkTreeModel *model;
244
245     if (gtk_tree_selection_get_selected(sel, &model, &iter)) {
246         g_message("activated");
247     } else {
248         g_message("none activated");
249     }
250 }