fixed style (trail space)
[ptask.git] / src / ui_taskpanel.c
1 /*
2  * Copyright (C) 2012-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * 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 Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <string.h>
20
21 #include <log.h>
22 #include <ui_taskpanel.h>
23
24 static GtkTextView *w_note;
25 static GtkEntry *w_description;
26 static GtkEntry *w_project;
27 static GtkComboBox *w_priority;
28 static GtkButton *w_tasksave_btn;
29 static GtkButton *w_taskdone_btn;
30 static GtkButton *w_taskcancel_btn;
31
32 static void enable(int enable)
33 {
34         GtkTextBuffer *buf;
35
36         gtk_widget_set_sensitive(GTK_WIDGET(w_tasksave_btn), enable);
37         gtk_widget_set_sensitive(GTK_WIDGET(w_taskdone_btn), enable);
38         gtk_widget_set_sensitive(GTK_WIDGET(w_taskcancel_btn), enable);
39
40         buf = gtk_text_view_get_buffer(w_note);
41         if (!enable)
42                 gtk_text_buffer_set_text(buf, "", 0);
43         gtk_widget_set_sensitive(GTK_WIDGET(w_note), enable);
44
45         if (!enable)
46                 gtk_entry_set_text(w_description, "");
47         gtk_widget_set_sensitive(GTK_WIDGET(w_description), enable);
48
49         if (!enable)
50                 gtk_entry_set_text(w_project, "");
51         gtk_widget_set_sensitive(GTK_WIDGET(w_project), enable);
52
53         if (!enable)
54                 gtk_combo_box_set_active(w_priority, 0);
55         gtk_widget_set_sensitive(GTK_WIDGET(w_priority), enable);
56 }
57
58 void ui_taskpanel_init(GtkBuilder *builder)
59 {
60         log_fct(__func__, "ENTER");
61
62         w_note = GTK_TEXT_VIEW(gtk_builder_get_object(builder, "tasknote"));
63
64         w_description = GTK_ENTRY(gtk_builder_get_object(builder,
65                                                          "taskdescription"));
66         w_project = GTK_ENTRY(gtk_builder_get_object(builder, "taskproject"));
67         w_priority = GTK_COMBO_BOX(gtk_builder_get_object(builder,
68                                                           "taskpriority"));
69
70         w_tasksave_btn = GTK_BUTTON(gtk_builder_get_object(builder,
71                                                            "tasksave"));
72         w_taskdone_btn = GTK_BUTTON(gtk_builder_get_object(builder,
73                                                            "taskdone"));
74         w_taskcancel_btn = GTK_BUTTON(gtk_builder_get_object(builder,
75                                                              "taskcancel"));
76
77         enable(0);
78
79         log_fct(__func__, "EXIT");
80 }
81
82 static int priority_to_int(const char *str)
83 {
84         switch (*str) {
85         case 'H':
86                 return 3;
87         case 'M':
88                 return 2;
89         case 'L':
90                 return 1;
91         default:
92                 return 0;
93         }
94 }
95
96 void ui_taskpanel_update(struct task *task)
97 {
98         GtkTextBuffer *buf;
99         int priority;
100
101         if (task) {
102                 buf = gtk_text_view_get_buffer(w_note);
103                 if (task->note)
104                         gtk_text_buffer_set_text(buf,
105                                                  task->note,
106                                                  strlen(task->note));
107                 else
108                         gtk_text_buffer_set_text(buf, "", 0);
109
110                 gtk_entry_set_text(w_description, task->description);
111
112                 if (task->project)
113                         gtk_entry_set_text(w_project, task->project);
114                 else
115                         gtk_entry_set_text(w_project, "");
116
117                 priority = priority_to_int(task->priority);
118                 gtk_combo_box_set_active(w_priority, priority);
119
120                 enable(1);
121         } else {
122                 enable(0);
123         }
124 }
125