You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spOSMroute/main/gui_favorites.c

211 lines
4.9 KiB

/***************************************************************************
* gui_favorites.c
*
* 2011-03-10
* Copyright (C) 2011 Steffen Pohle
* steffen@gulpe.de
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* main.c is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "osmroute.h"
#include "draw.h"
#include "gui.h"
#include "favorites.h"
#include "memoryleak.h"
#include "system.h"
enum {
FAVWIN_UNDEF = 0,
FAVWIN_LIST,
FAVWIN_ADD,
FAVWIN_REN,
FAVWIN_DEL,
FAVWIN_NAME,
FAVWIN_CLOSE
};
void gui_fav_close ();
void gui_fav_add ();
void gui_fav_del ();
void gui_fav_ren ();
void gui_fav_refresh ();
void gui_fav_callback_close ();
void gui_fav_selitem (int nr);
void gui_fav_fl_create ();
void gui_fav_fl_free ();
GUIList *fav_list = NULL;
GUIEntry *fav_name = NULL;
char **fl_array = NULL; // array to all elements
int fl_array_cnt = 0;
static GUIWindow wfav = {0};
void gui_fav_show () {
int btn = 0;
fav_new ();
fav_load (fav_getfilename());
if (wfav.screen == NULL) gui_window_new (&wfav, 220, 240);
wfav.screen_changed = 1;
wfav.callback_close = (void*)gui_fav_callback_close;
wfav.screen_changed = 1;
wfav.style = WGUI_S_VCENTER | WGUI_S_HCENTER;
strncpy (wfav.title, _("Favorites"), GUI_TEXTLEN);
/* favotires buttons */
strncpy (wfav.buttons[btn].caption, _("Close"), GUI_TEXTLEN);
wfav.buttons[btn].callback_clicked = (void*)gui_fav_close;
wfav.buttons[btn].id = FAVWIN_CLOSE;
wfav.buttons[btn].w = 60;
wfav.buttons[btn].h = 20;
wfav.buttons[btn].x = 5;
wfav.buttons[btn].y = 210;
strncpy (wfav.buttons[++btn].caption, _("Add"), GUI_TEXTLEN);
wfav.buttons[btn].callback_clicked = (void*)gui_fav_add;
wfav.buttons[btn].id = FAVWIN_ADD;
wfav.buttons[btn].w = 60;
wfav.buttons[btn].h = 20;
wfav.buttons[btn].x = 5;
wfav.buttons[btn].y = 15;
strncpy (wfav.buttons[++btn].caption, _("Del"), GUI_TEXTLEN);
wfav.buttons[btn].callback_clicked = (void*)gui_fav_del;
wfav.buttons[btn].id = FAVWIN_DEL;
wfav.buttons[btn].w = 60;
wfav.buttons[btn].h = 20;
wfav.buttons[btn].x = 70;
wfav.buttons[btn].y = 15;
strncpy (wfav.buttons[++btn].caption, _("Ren"), GUI_TEXTLEN);
wfav.buttons[btn].callback_clicked = (void*)gui_fav_ren;
wfav.buttons[btn].id = FAVWIN_REN;
wfav.buttons[btn].w = 60;
wfav.buttons[btn].h = 20;
wfav.buttons[btn].x = 135;
wfav.buttons[btn].y = 15;
/* add the list */
btn = 0;
wfav.lists[btn].callback_selectitem = (void*)gui_fav_selitem;
wfav.lists[btn].id = FAVWIN_LIST;
wfav.lists[btn].w = 210;
wfav.lists[btn].h = 130;
wfav.lists[btn].x = 5;
wfav.lists[btn].y = 45;
fav_list = &wfav.lists[btn];
/* add entry */
btn = 0;
wfav.entrys[btn].id = FAVWIN_NAME;
wfav.entrys[btn].w = 210;
wfav.entrys[btn].h = 20;
wfav.entrys[btn].x = 5;
wfav.entrys[btn].y = 180;
fav_name = &wfav.entrys[btn];
fav_name->text[0] = 0;
gui_fav_refresh ();
gui_show (&wfav);
};
void gui_fav_close () {
gui_close ();
};
void gui_fav_add () {
struct favorite f;
if (fav_name->text[0] == 0) return;
d_printf ("gui_fav_add: '%s'", fav_name->text);
strncpy (f.name, fav_name->text, FAV_NAME_LEN);
f.pos.lon = view_lon;
f.pos.lat = view_lat;
fav_add (-1, &f);
gui_fav_refresh ();
};
void gui_fav_del () {
if (fav_list->selected < 0) return;
fav_del (fav_list->selected);
gui_fav_refresh ();
};
void gui_fav_ren () {
if (fav_list->selected < 0 || fav_name->text[0] == 0) return;
strncpy (favorites[fav_list->selected].name, fav_name->text, FAV_NAME_LEN);
gui_fav_refresh ();
};
void gui_fav_selitem (int nr) {
if (nr < favorites_cnt) {
strncpy (fav_name->text, favorites[nr].name, GUI_TEXTLEN);
view_lon = favorites[nr].pos.lon;
view_lat = favorites[nr].pos.lat;
gui_close ();
draw_del_flag (DRAW_GPSFOLLOW);
}
else fav_name->text[0] = 0;
};
void gui_fav_callback_close () {
fav_save (fav_getfilename ());
gui_fav_fl_free ();
};
void gui_fav_refresh () {
gui_fav_fl_create ();
fav_name->text[0] = 0;
fav_list->data = fl_array;
};
void gui_fav_fl_create () {
int i;
if ((fl_array_cnt-1) < favorites_cnt) {
gui_fav_fl_free ();
fl_array_cnt = favorites_cnt + 8;
fl_array = (char **) ml_malloc (sizeof (char *) * fl_array_cnt);
}
for (i = 0; i < favorites_cnt; i++) {
fl_array[i] = favorites[i].name;
}
fl_array[favorites_cnt] = NULL;
};
void gui_fav_fl_free () {
if (fl_array) ml_free (fl_array);
fl_array = NULL;
fl_array_cnt = 0;
};