2011-09-20 19:40:34 +00:00
|
|
|
/* info.c */
|
|
|
|
/* creates the UI for the info frame -
|
|
|
|
* controlled through the following interfaces:
|
|
|
|
*
|
|
|
|
* void flush_dive_info_changes(struct dive *dive)
|
|
|
|
* void show_dive_info(struct dive *dive)
|
|
|
|
*
|
|
|
|
* called from gtk-ui:
|
|
|
|
* GtkWidget *extended_dive_info_widget(void)
|
|
|
|
*/
|
2011-08-31 19:09:19 +00:00
|
|
|
#include <stdio.h>
|
2011-09-03 03:00:10 +00:00
|
|
|
#include <string.h>
|
2011-08-31 19:09:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "dive.h"
|
|
|
|
#include "display.h"
|
2011-09-20 19:40:34 +00:00
|
|
|
#include "display-gtk.h"
|
2011-09-05 19:12:58 +00:00
|
|
|
#include "divelist.h"
|
2011-08-31 19:09:19 +00:00
|
|
|
|
2011-10-23 07:07:01 +00:00
|
|
|
static GtkComboBoxEntry *location, *buddy, *divemaster;
|
2011-09-04 12:38:01 +00:00
|
|
|
static GtkTextBuffer *notes;
|
2011-09-02 02:56:04 +00:00
|
|
|
static int location_changed = 1, notes_changed = 1;
|
2011-09-13 21:58:06 +00:00
|
|
|
static int divemaster_changed = 1, buddy_changed = 1;
|
2011-09-01 03:36:51 +00:00
|
|
|
|
2011-09-02 02:56:04 +00:00
|
|
|
static char *get_text(GtkTextBuffer *buffer)
|
|
|
|
{
|
|
|
|
GtkTextIter start;
|
|
|
|
GtkTextIter end;
|
|
|
|
|
|
|
|
gtk_text_buffer_get_start_iter(buffer, &start);
|
|
|
|
gtk_text_buffer_get_end_iter(buffer, &end);
|
|
|
|
return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
|
|
|
|
}
|
|
|
|
|
2011-09-21 04:29:09 +00:00
|
|
|
/* old is NULL or a valid string, new is a valid string
|
|
|
|
* NOTW: NULL and "" need to be treated as "unchanged" */
|
|
|
|
static int text_changed(char *old, char *new)
|
|
|
|
{
|
|
|
|
return ((old && strcmp(old,new)) ||
|
|
|
|
(!old && strcmp("",new)));
|
|
|
|
}
|
|
|
|
|
2011-09-10 00:10:17 +00:00
|
|
|
void flush_dive_info_changes(struct dive *dive)
|
2011-09-02 02:56:04 +00:00
|
|
|
{
|
2011-09-21 04:29:09 +00:00
|
|
|
char *old_text;
|
|
|
|
int changed = 0;
|
|
|
|
|
2011-09-02 02:56:04 +00:00
|
|
|
if (!dive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (location_changed) {
|
2011-10-23 07:07:01 +00:00
|
|
|
char *new_text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(location));
|
2011-09-21 04:29:09 +00:00
|
|
|
old_text = dive->location;
|
2011-10-23 07:07:01 +00:00
|
|
|
dive->location = new_text;
|
2011-09-21 04:29:09 +00:00
|
|
|
if (text_changed(old_text,dive->location))
|
|
|
|
changed = 1;
|
|
|
|
if (old_text)
|
|
|
|
g_free(old_text);
|
2011-09-02 02:56:04 +00:00
|
|
|
}
|
|
|
|
|
2011-09-13 21:58:06 +00:00
|
|
|
if (divemaster_changed) {
|
2011-10-23 07:07:01 +00:00
|
|
|
char *new_text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(divemaster));
|
2011-09-21 04:29:09 +00:00
|
|
|
old_text = dive->divemaster;
|
2011-10-23 07:07:01 +00:00
|
|
|
dive->divemaster = new_text;
|
2011-09-21 04:29:09 +00:00
|
|
|
if (text_changed(old_text,dive->divemaster))
|
|
|
|
changed = 1;
|
|
|
|
if (old_text)
|
|
|
|
g_free(old_text);
|
2011-09-13 21:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buddy_changed) {
|
2011-10-23 07:07:01 +00:00
|
|
|
char *new_text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(buddy));
|
2011-09-21 04:29:09 +00:00
|
|
|
old_text = dive->buddy;
|
2011-10-23 07:07:01 +00:00
|
|
|
dive->buddy = new_text;
|
2011-09-21 04:29:09 +00:00
|
|
|
if (text_changed(old_text,dive->buddy))
|
|
|
|
changed = 1;
|
|
|
|
if (old_text)
|
|
|
|
g_free(old_text);
|
2011-09-13 21:58:06 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 02:56:04 +00:00
|
|
|
if (notes_changed) {
|
2011-09-21 04:29:09 +00:00
|
|
|
old_text = dive->notes;
|
2011-09-02 02:56:04 +00:00
|
|
|
dive->notes = get_text(notes);
|
2011-09-21 04:29:09 +00:00
|
|
|
if (text_changed(old_text,dive->notes))
|
|
|
|
changed = 1;
|
|
|
|
if (old_text)
|
|
|
|
g_free(old_text);
|
2011-09-02 02:56:04 +00:00
|
|
|
}
|
2011-09-21 04:29:09 +00:00
|
|
|
if (changed)
|
|
|
|
mark_divelist_changed(TRUE);
|
2011-09-02 02:56:04 +00:00
|
|
|
}
|
|
|
|
|
2011-10-23 07:07:01 +00:00
|
|
|
static void set_combo_box_entry_text(GtkComboBoxEntry *combo_box, const char *text)
|
|
|
|
{
|
|
|
|
GtkEntry *entry = GTK_ENTRY(GTK_BIN(combo_box)->child);
|
|
|
|
gtk_entry_set_text(entry, text);
|
|
|
|
}
|
|
|
|
|
2011-09-19 19:39:35 +00:00
|
|
|
#define SET_TEXT_ENTRY(x) \
|
2011-10-23 07:07:01 +00:00
|
|
|
set_combo_box_entry_text(x, dive && dive->x ? dive->x : "")
|
2011-09-02 02:56:04 +00:00
|
|
|
|
2011-09-19 19:39:35 +00:00
|
|
|
void show_dive_info(struct dive *dive)
|
2011-09-01 03:36:51 +00:00
|
|
|
{
|
2011-09-20 05:09:47 +00:00
|
|
|
struct tm *tm;
|
|
|
|
const char *text;
|
|
|
|
char buffer[80];
|
|
|
|
|
|
|
|
/* dive number and location (or lacking that, the date) go in the window title */
|
|
|
|
tm = gmtime(&dive->when);
|
|
|
|
text = dive->location;
|
|
|
|
if (!text)
|
|
|
|
text = "";
|
|
|
|
if (*text) {
|
|
|
|
snprintf(buffer, sizeof(buffer), "Dive #%d - %s", dive->number, text);
|
|
|
|
} else {
|
|
|
|
snprintf(buffer, sizeof(buffer), "Dive #%d - %s %02d/%02d/%04d at %d:%02d",
|
|
|
|
dive->number,
|
|
|
|
weekday(tm->tm_wday),
|
|
|
|
tm->tm_mon+1, tm->tm_mday,
|
|
|
|
tm->tm_year+1900,
|
|
|
|
tm->tm_hour, tm->tm_min);
|
|
|
|
}
|
|
|
|
text = buffer;
|
|
|
|
if (!dive->number)
|
|
|
|
text += 10; /* Skip the "Dive #0 - " part */
|
|
|
|
gtk_window_set_title(GTK_WINDOW(main_window), text);
|
|
|
|
|
2011-09-19 19:39:35 +00:00
|
|
|
SET_TEXT_ENTRY(divemaster);
|
|
|
|
SET_TEXT_ENTRY(buddy);
|
|
|
|
SET_TEXT_ENTRY(location);
|
|
|
|
gtk_text_buffer_set_text(notes, dive && dive->notes ? dive->notes : "", -1);
|
2011-09-01 01:30:42 +00:00
|
|
|
}
|
|
|
|
|
2011-10-23 07:07:01 +00:00
|
|
|
static GtkComboBoxEntry *text_entry(GtkWidget *box, const char *label, GtkListStore *completions)
|
2011-09-04 12:38:01 +00:00
|
|
|
{
|
2011-10-23 07:07:01 +00:00
|
|
|
GtkEntry *entry;
|
|
|
|
GtkWidget *combo_box;
|
2011-09-04 12:38:01 +00:00
|
|
|
GtkWidget *frame = gtk_frame_new(label);
|
2011-10-23 07:07:01 +00:00
|
|
|
GtkEntryCompletion *completion;
|
2011-09-04 12:38:01 +00:00
|
|
|
|
|
|
|
gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
|
|
|
|
|
2011-10-23 07:07:01 +00:00
|
|
|
combo_box = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(completions), 0);
|
|
|
|
gtk_container_add(GTK_CONTAINER(frame), combo_box);
|
2011-09-04 12:38:01 +00:00
|
|
|
|
2011-10-23 07:07:01 +00:00
|
|
|
entry = GTK_ENTRY(GTK_BIN(combo_box)->child);
|
|
|
|
|
|
|
|
completion = gtk_entry_completion_new();
|
|
|
|
gtk_entry_completion_set_text_column(completion, 0);
|
|
|
|
gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completions));
|
|
|
|
gtk_entry_set_completion(entry, completion);
|
2011-10-23 06:47:19 +00:00
|
|
|
|
2011-10-23 07:07:01 +00:00
|
|
|
return GTK_COMBO_BOX_ENTRY(combo_box);
|
2011-09-04 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
2011-09-11 23:34:01 +00:00
|
|
|
static GtkTextBuffer *text_view(GtkWidget *box, const char *label)
|
2011-09-01 03:36:51 +00:00
|
|
|
{
|
2011-09-11 23:34:01 +00:00
|
|
|
GtkWidget *view, *vbox;
|
2011-09-02 02:56:04 +00:00
|
|
|
GtkTextBuffer *buffer;
|
2011-09-01 03:36:51 +00:00
|
|
|
GtkWidget *frame = gtk_frame_new(label);
|
2011-09-02 02:56:04 +00:00
|
|
|
|
2011-09-11 23:34:01 +00:00
|
|
|
gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 0);
|
|
|
|
box = gtk_hbox_new(FALSE, 3);
|
|
|
|
gtk_container_add(GTK_CONTAINER(frame), box);
|
|
|
|
vbox = gtk_vbox_new(FALSE, 3);
|
|
|
|
gtk_container_add(GTK_CONTAINER(box), vbox);
|
2011-09-02 02:56:04 +00:00
|
|
|
|
2011-09-11 23:34:01 +00:00
|
|
|
GtkWidget* scrolled_window = gtk_scrolled_window_new(0, 0);
|
2011-09-04 13:08:01 +00:00
|
|
|
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
2011-09-04 00:37:22 +00:00
|
|
|
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_IN);
|
|
|
|
|
2011-09-11 23:34:01 +00:00
|
|
|
view = gtk_text_view_new();
|
2011-09-04 13:08:01 +00:00
|
|
|
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD);
|
2011-09-04 00:37:22 +00:00
|
|
|
gtk_container_add(GTK_CONTAINER(scrolled_window), view);
|
|
|
|
|
2011-09-02 02:56:04 +00:00
|
|
|
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
|
|
|
|
|
2011-09-11 23:34:01 +00:00
|
|
|
gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
|
2011-09-02 02:56:04 +00:00
|
|
|
return buffer;
|
2011-09-01 03:36:51 +00:00
|
|
|
}
|
|
|
|
|
2011-10-23 06:47:19 +00:00
|
|
|
static int found_string_entry;
|
|
|
|
|
|
|
|
static gboolean match_string_entry(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
|
|
|
|
{
|
|
|
|
const char *string = data;
|
|
|
|
char *entry;
|
|
|
|
|
|
|
|
gtk_tree_model_get(model, iter, 0, &entry, -1);
|
|
|
|
if (strcmp(entry, string))
|
|
|
|
return FALSE;
|
|
|
|
found_string_entry = 1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int match_list(GtkListStore *list, const char *string)
|
|
|
|
{
|
|
|
|
found_string_entry = 0;
|
|
|
|
gtk_tree_model_foreach(GTK_TREE_MODEL(list), match_string_entry, (void *)string);
|
|
|
|
return found_string_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkListStore *location_list, *people_list;
|
|
|
|
|
|
|
|
static void add_string_list_entry(const char *string, GtkListStore *list)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
if (!string || !*string)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (match_list(list, string))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Fixme! Check for duplicates! */
|
|
|
|
gtk_list_store_append(list, &iter);
|
|
|
|
gtk_list_store_set(list, &iter, 0, string, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_people(const char *string)
|
|
|
|
{
|
|
|
|
add_string_list_entry(string, people_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_location(const char *string)
|
|
|
|
{
|
|
|
|
add_string_list_entry(string, location_list);
|
|
|
|
}
|
|
|
|
|
2011-09-04 17:01:30 +00:00
|
|
|
GtkWidget *extended_dive_info_widget(void)
|
2011-09-01 01:30:42 +00:00
|
|
|
{
|
2011-09-13 21:58:06 +00:00
|
|
|
GtkWidget *vbox, *hbox;
|
2011-09-04 15:08:40 +00:00
|
|
|
vbox = gtk_vbox_new(FALSE, 6);
|
2011-09-01 01:30:42 +00:00
|
|
|
|
2011-10-23 06:47:19 +00:00
|
|
|
people_list = gtk_list_store_new(1, G_TYPE_STRING);
|
|
|
|
location_list = gtk_list_store_new(1, G_TYPE_STRING);
|
|
|
|
|
2011-09-04 15:08:40 +00:00
|
|
|
gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
|
2011-10-23 06:47:19 +00:00
|
|
|
location = text_entry(vbox, "Location", location_list);
|
2011-09-13 21:58:06 +00:00
|
|
|
|
|
|
|
hbox = gtk_hbox_new(FALSE, 3);
|
|
|
|
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
|
|
|
|
|
2011-10-23 06:47:19 +00:00
|
|
|
divemaster = text_entry(hbox, "Divemaster", people_list);
|
|
|
|
buddy = text_entry(hbox, "Buddy", people_list);
|
2011-09-13 21:58:06 +00:00
|
|
|
|
2011-09-11 23:34:01 +00:00
|
|
|
notes = text_view(vbox, "Notes");
|
2011-09-04 15:08:40 +00:00
|
|
|
return vbox;
|
2011-08-31 19:09:19 +00:00
|
|
|
}
|