Add:gui_internal:Add via navit.xml configurable menus, expect some screwups the next...
[navit-package] / navit / gui.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <glib.h>
21 #include <string.h>
22 #include "debug.h"
23 #include "callback.h"
24 #include "gui.h"
25 #include "menu.h"
26 #include "data_window.h"
27 #include "item.h"
28 #include "plugin.h"
29
30 struct gui {
31         struct gui_methods meth;
32         struct gui_priv *priv;
33         struct attr **attrs;
34         struct attr parent;
35 };
36
37 struct gui *
38 gui_new(struct attr *parent, struct attr **attrs)
39 {
40         struct gui *this_;
41         struct attr *type_attr;
42         struct gui_priv *(*guitype_new)(struct navit *nav, struct gui_methods *meth, struct attr **attrs, struct gui *gui);
43         struct attr cbl;
44         if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
45                 return NULL;
46         }
47
48         guitype_new=plugin_get_gui_type(type_attr->u.str);
49         if (! guitype_new)
50                 return NULL;
51
52         this_=g_new0(struct gui, 1);
53         this_->attrs=attr_list_dup(attrs);
54         cbl.type=attr_callback_list;
55         cbl.u.callback_list=callback_list_new();
56         this_->attrs=attr_generic_add_attr(this_->attrs, &cbl);
57         this_->priv=guitype_new(parent->u.navit, &this_->meth, this_->attrs, this_);
58         this_->parent=*parent;
59         return this_;
60 }
61
62 int
63 gui_get_attr(struct gui *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
64 {
65         int ret;
66         if (this_->meth.get_attr) {
67                 ret=this_->meth.get_attr(this_->priv, type, attr);
68                 if (ret)
69                         return ret;
70         }
71         if (type == this_->parent.type) {
72                 *attr=this_->parent;
73                 return 1;
74         }
75         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
76 }
77
78 int
79 gui_add_attr(struct gui *this_, struct attr *attr)
80 {
81         int ret=0;
82         if (this_->meth.add_attr) 
83                 ret=this_->meth.add_attr(this_->priv, attr);
84         return ret;
85 }
86
87 struct menu *
88 gui_menubar_new(struct gui *gui)
89 {
90         struct menu *this_;
91         if (! gui->meth.menubar_new)
92                 return NULL;
93         this_=g_new0(struct menu, 1);
94         this_->priv=gui->meth.menubar_new(gui->priv, &this_->meth);
95         if (! this_->priv) {
96                 g_free(this_);
97                 return NULL;
98         }
99         return this_;
100 }
101
102 struct menu *
103 gui_popup_new(struct gui *gui)
104 {
105         struct menu *this_;
106         if (! gui->meth.popup_new)
107                 return NULL;
108         this_=g_new0(struct menu, 1);
109         this_->priv=gui->meth.popup_new(gui->priv, &this_->meth);
110         if (! this_->priv) {
111                 g_free(this_);
112                 return NULL;
113         }
114         return this_;
115 }
116
117 struct datawindow *
118 gui_datawindow_new(struct gui *gui, char *name, struct callback *click, struct callback *close)
119 {
120         struct datawindow *this_;
121         if (! gui->meth.datawindow_new)
122                 return NULL;
123         this_=g_new0(struct datawindow, 1);
124         this_->priv=gui->meth.datawindow_new(gui->priv, name, click, close, &this_->meth);
125         if (! this_->priv) {
126                 g_free(this_);
127                 return NULL;
128         }
129         return this_;
130 }
131
132 int
133 gui_add_bookmark(struct gui *gui, struct pcoord *c, char *description)
134 {
135         int ret;
136         dbg(2,"enter\n");
137         if (! gui->meth.add_bookmark)
138                 return 0;
139         ret=gui->meth.add_bookmark(gui->priv, c, description);
140         
141         dbg(2,"ret=%d\n", ret);
142         return ret;
143 }
144
145 int
146 gui_set_graphics(struct gui *this_, struct graphics *gra)
147 {
148         if (! this_->meth.set_graphics)
149                 return 1;
150         return this_->meth.set_graphics(this_->priv, gra);
151 }
152
153 void
154 gui_disable_suspend(struct gui *this_)
155 {
156         if (this_->meth.disable_suspend)
157                 this_->meth.disable_suspend(this_->priv);
158 }
159
160 int
161 gui_has_main_loop(struct gui *this_)
162 {
163         if (! this_->meth.run_main_loop)
164                 return 0;
165         return 1;
166 }
167
168 int
169 gui_run_main_loop(struct gui *this_)
170 {
171         if (! gui_has_main_loop(this_))
172                 return 1;
173         return this_->meth.run_main_loop(this_->priv);
174 }
175