Difference between revisions of "GUI development"

From Navit's Wiki
Jump to: navigation, search
m
m (Initializing your gui: Improved markup)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Writing a new gui.
+
=Foreword=
  
Almost everything in NavIt works as a plugin, and so do the guis.
+
Almost everything in NavIt works as a [[plugin]], and so do the guis.
  
 +
=Initializing your gui=
 
You have to provide a function plugin_init in your plugin.
 
You have to provide a function plugin_init in your plugin.
  
This function should call plugin_register_gui_type("gui-name",function)
+
This function should call
 +
<source lang="c">
 +
plugin_register_gui_type("gui-name", function);
 +
</source>
  
The function has the following parameters: struct gui_methods *meth, int w, int h
+
The function has the following parameters: <code>struct gui_methods *meth, int w, int h</code>
* meth is a structure with gui-functions you have to fill
+
* <code>meth</code> is a structure with gui-functions you have to fill
* w is the expected width
+
* <code>w</code> is the expected width
* h is the expected height of the gui.
+
* <code>h</code> is the expected height of the gui.
  
 
But you might ignore w and h, they are only recommendations.
 
But you might ignore w and h, they are only recommendations.
  
meth should be filled with the following data:
+
<code>meth</code> should be filled with the following data:
* menubar_new
+
* <code>menubar_new</code>
* toolbar_new
+
* <code>toolbar_new</code>
* statusbar_new
+
* <code>statusbar_new</code>
* popup_new
+
* <code>popup_new</code>
* set_graphics
+
* <code>set_graphics</code>
  
You can probably implement menubar_new, toolbar_new, statusbar_new and popup_new as a dummy, since it is rather GTK-specific, but set_graphics is required. It connects the graphics to the gui.
+
You can probably implement <code>menubar_new</code>, <code>toolbar_new</code>, <code>statusbar_new</code> and <code>popup_new</code> as a dummy, since it is rather GTK-specific, but <code>set_graphics</code> is required. It connects the graphics to the gui.
  
 +
<source lang="c">
 
  struct gui_methods gui_<plugin>_methods = {
 
  struct gui_methods gui_<plugin>_methods = {
 
  gui_<plugin>_menubar_new,
 
  gui_<plugin>_menubar_new,
Line 31: Line 36:
 
  gui_<plugin>_main_loop,
 
  gui_<plugin>_main_loop,
 
  };
 
  };
 
+
</source>
  
 
Here is a code sample :
 
Here is a code sample :
 +
<source lang="c">
 
  void
 
  void
 
  plugin_init(void)
 
  plugin_init(void)
Line 40: Line 46:
 
  plugin_register_gui_type("<your plugin>", <your init function>);
 
  plugin_register_gui_type("<your plugin>", <your init function>);
 
  }
 
  }
 +
</source>
  
  
 
Now, here is a sample of the init function :
 
Now, here is a sample of the init function :
 +
<source lang="c">
 
  static struct gui_priv *
 
  static struct gui_priv *
  gui_sdl_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs)  
+
  gui_<plugin>_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs)  
 
  {
 
  {
dbg(1,"Begin initialization of <your plugin>\n");
+
dbg(1,"Begin initialization of <your plugin>\n");
struct gui_priv *this_;
+
struct gui_priv *this_;
 
         *meth=gui_<plugin>_methods;
 
         *meth=gui_<plugin>_methods;
 
+
this_=g_new0(struct gui_priv, 1);
+
this_=g_new0(struct gui_priv, 1);
 
+
// Perform initializations specific to your gui
+
// Perform initializations specific to your gui
// ...
+
// ...
 
+
// If you want to register a callback function for the vehicle, you can do it so:
+
// If you want to register a callback function for the vehicle, you can do it so:
struct callback *cb=callback_new_0(callback_cast(vehicle_callback_handler));
+
struct callback *cb=callback_new_0(callback_cast(vehicle_callback_handler));
 
+
navit_add_vehicle_cb(nav,cb);
+
navit_add_vehicle_cb(nav,cb);
this_->nav=nav;
+
this_->nav=nav;
+
return this_;
+
return this_;
 
}
 
}
 +
</source>
  
 
And you plugin (gui) should initialize.
 
And you plugin (gui) should initialize.
 
Now, you need somme dummy functions (for now). Thoses are requested for GTK.
 
Now, you need somme dummy functions (for now). Thoses are requested for GTK.
  
 +
<source lang="c">
 
  static struct menu_priv *
 
  static struct menu_priv *
 
  gui_<plugin>_toolbar_new(struct gui_priv *this_, struct menu_methods *meth)
 
  gui_<plugin>_toolbar_new(struct gui_priv *this_, struct menu_methods *meth)
 
  {
 
  {
return NULL;
+
return NULL;
 
  }
 
  }
 
   
 
   
Line 76: Line 86:
 
  gui_<plugin>_statusbar_new(struct gui_priv *gui, struct statusbar_methods *meth)
 
  gui_<plugin>_statusbar_new(struct gui_priv *gui, struct statusbar_methods *meth)
 
  {
 
  {
return NULL;
+
return NULL;
 
  }
 
  }
 
   
 
   
Line 82: Line 92:
 
  gui_<plugin>_popup_new(struct gui_priv *this_, struct menu_methods *meth)
 
  gui_<plugin>_popup_new(struct gui_priv *this_, struct menu_methods *meth)
 
  {
 
  {
return NULL;
+
return NULL;
 
  }
 
  }
 +
</source>
  
 +
The following function allows to get menu entries for bookmarks
  
 
+
<source lang="c">
The following function allows to get menu entries for bookmarks
 
 
  static struct menu_priv *
 
  static struct menu_priv *
 
  gui_<plugin>_menubar_new(struct gui_priv *this_, struct menu_methods *meth)
 
  gui_<plugin>_menubar_new(struct gui_priv *this_, struct menu_methods *meth)
 
  {
 
  {
*meth=menu_methods;
+
*meth=menu_methods;
return (struct menu_priv *) 1;
+
return (struct menu_priv *) 1;
 
  }
 
  }
 +
</source>
  
 +
=The plugin main()=
  
 +
And, last but not least :
  
And, last but not least :
+
<source lang="c">
 
  static int gui_run_main_loop(struct gui_priv *this_)
 
  static int gui_run_main_loop(struct gui_priv *this_)
 
  {
 
  {
Line 103: Line 117:
 
          
 
          
 
         // Define your viewport
 
         // Define your viewport
struct map_selection sel;
+
struct map_selection sel;
 +
       
 +
memset(&sel, 0, sizeof(sel));
 +
sel.u.c_rect.rl.x=800;
 +
sel.u.c_rect.rl.y=600;
 +
 +
transform_set_screen_selection(navit_get_trans(this_->nav), &sel);
 
          
 
          
memset(&sel, 0, sizeof(sel));
 
sel.u.c_rect.rl.x=800;
 
sel.u.c_rect.rl.y=600;
 
 
transform_set_screen_selection(navit_get_trans(this_->nav), &sel);
 
 
          
 
          
       
+
navit_draw(this_->nav);
navit_draw(this_->nav);
 
 
          
 
          
 
         // Register the callback to handle navigation instructions updates
 
         // Register the callback to handle navigation instructions updates
struct navigation *navig;
+
struct navigation *navig;
navig=navit_get_navigation(navit);
+
navig=navit_get_navigation(navit);
 
          
 
          
navigation_register_callback(navig,
+
navigation_register_callback(navig,
attr_navigation_long,
+
attr_navigation_long,
callback_new_0((void (*)())update_roadbook)
+
callback_new_0((void (*)())update_roadbook)
);
+
);
 
          
 
          
timeout = g_timeout_source_new(100);
+
timeout = g_timeout_source_new(100);
g_source_set_callback(timeout, gui_timeout_cb, NULL, NULL);
+
g_source_set_callback(timeout, gui_timeout_cb, NULL, NULL);
g_source_attach(timeout, NULL);
+
g_source_attach(timeout, NULL);
while (!must_quit)
+
while (!must_quit)
{
+
{
 
         //Poll event, process them
 
         //Poll event, process them
 
          
 
          
Line 134: Line 148:
 
          
 
          
 
         // Any other task you may need
 
         // Any other task you may need
 
+
 
         }
 
         }
 
         g_source_destroy(timeout);
 
         g_source_destroy(timeout);
 
          
 
          
 
  }
 
  }
 +
</source>
 +
 +
[[Category:GUI]]
 +
[[Category:Development]]

Latest revision as of 16:39, 27 November 2017

Foreword[edit]

Almost everything in NavIt works as a plugin, and so do the guis.

Initializing your gui[edit]

You have to provide a function plugin_init in your plugin.

This function should call

plugin_register_gui_type("gui-name", function);

The function has the following parameters: struct gui_methods *meth, int w, int h

  • meth is a structure with gui-functions you have to fill
  • w is the expected width
  • h is the expected height of the gui.

But you might ignore w and h, they are only recommendations.

meth should be filled with the following data:

  • menubar_new
  • toolbar_new
  • statusbar_new
  • popup_new
  • set_graphics

You can probably implement menubar_new, toolbar_new, statusbar_new and popup_new as a dummy, since it is rather GTK-specific, but set_graphics is required. It connects the graphics to the gui.

 struct gui_methods gui_<plugin>_methods = {
 	gui_<plugin>_menubar_new,
 	gui_<plugin>_toolbar_new,
 	gui_<plugin>_statusbar_new,
 	gui_<plugin>_popup_new,
 	gui_<plugin>_set_graphics,
 	gui_<plugin>_main_loop,
 };

Here is a code sample :

 void
 plugin_init(void)
 {
 	dbg(1,"registering <your plugin>\n");
 	plugin_register_gui_type("<your plugin>", <your init function>);
 }


Now, here is a sample of the init function :

 static struct gui_priv *
 gui_<plugin>_new(struct navit *nav, struct gui_methods *meth, struct attr **attrs) 
 {
 	dbg(1,"Begin initialization of <your plugin>\n");
 	struct gui_priv *this_;
        *meth=gui_<plugin>_methods;
 
 	this_=g_new0(struct gui_priv, 1);
 
 	// Perform initializations specific to your gui
 	// ...
 
 	// If you want to register a callback function for the vehicle, you can do it so:
 	struct callback *cb=callback_new_0(callback_cast(vehicle_callback_handler));
 
 	navit_add_vehicle_cb(nav,cb);
 	this_->nav=nav;
 
 	return this_;
}

And you plugin (gui) should initialize. Now, you need somme dummy functions (for now). Thoses are requested for GTK.

 static struct menu_priv *
 gui_<plugin>_toolbar_new(struct gui_priv *this_, struct menu_methods *meth)
 {
 	return NULL;
 }
 
 static struct statusbar_priv *
 gui_<plugin>_statusbar_new(struct gui_priv *gui, struct statusbar_methods *meth)
 {
 	return NULL;
 }
 
 static struct menu_priv *
 gui_<plugin>_popup_new(struct gui_priv *this_, struct menu_methods *meth)
 {
 	return NULL;
 }

The following function allows to get menu entries for bookmarks

 static struct menu_priv *
 gui_<plugin>_menubar_new(struct gui_priv *this_, struct menu_methods *meth)
 {
 	*meth=menu_methods;
 	return (struct menu_priv *) 1;
 }

The plugin main()[edit]

And, last but not least :

 static int gui_run_main_loop(struct gui_priv *this_)
 {
        // Whatever needs to be done right before beginning the navigation
 
        // Define your viewport
 	struct map_selection sel;
 
 	memset(&sel, 0, sizeof(sel));
 	sel.u.c_rect.rl.x=800;
 	sel.u.c_rect.rl.y=600;
 
 	transform_set_screen_selection(navit_get_trans(this_->nav), &sel);
 
 
 	navit_draw(this_->nav);
 
        // Register the callback to handle navigation instructions updates
 	struct navigation *navig;
 	navig=navit_get_navigation(navit);
 
 	navigation_register_callback(navig,
 		attr_navigation_long,
 		callback_new_0((void (*)())update_roadbook)
 	);
 
 	timeout = g_timeout_source_new(100);
 	g_source_set_callback(timeout, gui_timeout_cb, NULL, NULL);
 	g_source_attach(timeout, NULL);
 	while (!must_quit)
 	{
        //Poll event, process them
 
        // Call update of gps
        g_main_context_iteration (NULL, TRUE);
 
        // Any other task you may need
 
        }
        g_source_destroy(timeout);
 
 }