1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
/* Notes Area display of project notes for GTimeTracker
* Copyright (C) 2003 Linas Vepstas <linas@linas.org>
*
* 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 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <glib-object.h>
#include <glade/glade.h>
#include <gnome.h>
#include "proj.h"
#include "props-task.h"
#include "notes-area.h"
#include "util.h"
#include "menus.h"
struct NotesArea_s
{
GladeXML *gtxml;
GtkPaned *vpane; /* top level pane */
GtkContainer *projects_tree_holder; /* scrolled widget that holds the projects tree */
GtkPaned *hpane; /* left-right divider */
GtkEntry *proj_title;
GtkEntry *proj_desc;
GtkTextView *proj_notes;
GtkComboBox *task_combo;
GtkTextView *task_notes;
GtkButton *close_proj;
GtkButton *close_task;
GtkButton *new_task;
GtkButton *edit_task;
GttProject *proj;
/* The goal of 'ignore events' is to prevent an inifinite
* loop of cascading events as we modify the project and the GUI.
*/
gboolean ignore_events;
/* The goal of the freezes is to prevent more than one update
* of windows per second. The problem is that without this,
* there would be one event per keystroke, which could cause
* a redraw of e.g. the journal window. In such a case, even
* moderate typists on a slow CPU could saturate the CPU entirely.
*/
gboolean proj_freeze;
GttTask * task_freeze;
};
/* ============================================================== */
#define TSK_SETUP \
GttTask *tsk; \
const char * str; \
if (NULL == na->proj) return; \
if (na->ignore_events) return; \
\
na->ignore_events = TRUE; \
tsk = gtt_project_get_current_task (na->proj); \
if (NULL == tsk) \
{ \
tsk = gtt_task_new(); \
gtt_project_prepend_task (na->proj, tsk); \
} \
if (tsk != na->task_freeze) \
{ \
/* Try to avoid race condition if another task */ \
/* is created while this task is frozen. */ \
if (NULL != na->task_freeze) gtt_task_thaw (na->task_freeze); \
na->task_freeze = tsk; \
} \
#ifdef UNUSED_CODE_RIGHT_NOW
static void
task_memo_changed (GtkEntry *entry, NotesArea *na)
{
TSK_SETUP;
str = gtk_entry_get_text (entry);
gtt_task_set_memo (tsk, str);
na->ignore_events = FALSE;
}
#endif /* UNUSED_CODE_RIGHT_NOW */
/* ============================================================== */
static void
task_notes_changed (GtkTextBuffer *entry, NotesArea *na)
{
TSK_SETUP;
str = xxxgtk_textview_get_text (na->task_notes);
gtt_task_set_notes (tsk, str);
na->ignore_events = FALSE;
}
/* ============================================================== */
#define PRJ_SETUP \
const char * str; \
if (NULL == na->proj) return; \
if (na->ignore_events) return; \
\
if (FALSE == na->proj_freeze) \
{ \
na->proj_freeze = TRUE; \
} \
na->ignore_events = TRUE; \
static void
proj_title_changed (GtkEntry *entry, NotesArea *na)
{
PRJ_SETUP
str = gtk_entry_get_text (entry);
gtt_project_set_title (na->proj, str);
na->ignore_events = FALSE;
}
/* ============================================================== */
static void
proj_desc_changed (GtkEntry *entry, NotesArea *na)
{
PRJ_SETUP
str = gtk_entry_get_text (entry);
gtt_project_set_desc (na->proj, str);
na->ignore_events = FALSE;
}
/* ============================================================== */
static GttTask *
tasks_model_get_task (GtkTreeModel *model, GtkTreeIter *iter)
{
GValue v = {G_TYPE_INVALID};
gtk_tree_model_get_value (model, iter, 1, &v);
GttTask *task = (GttTask *) g_value_get_pointer (&v);
g_value_unset (&v);
return task;
}
/* ============================================================== */
static void
proj_notes_changed (GtkTextBuffer *entry, NotesArea *na)
{
PRJ_SETUP
str = xxxgtk_textview_get_text (na->proj_notes);
gtt_project_set_notes (na->proj, str);
na->ignore_events = FALSE;
}
/* ============================================================== */
/* This routine will cause pending events to get delivered. */
void
gtt_notes_timer_callback (NotesArea *na)
{
if (!na) return;
na->ignore_events = TRUE;
if (na->task_freeze)
{
gtt_task_thaw (na->task_freeze);
na->task_freeze = NULL;
}
if (na->proj_freeze)
{
na->proj_freeze = FALSE;
gtt_project_thaw (na->proj);
}
na->ignore_events = FALSE;
}
/* ============================================================== */
/* These are some strange routines used to close off paned areas.
* They are weird, they need to guess at sizes to close off the
* panes. This should really be a gkpaned built-in function.
*/
#define CLOSED_MARGIN 10
static void
close_proj_area (GtkButton *but, NotesArea *na)
{
int hpane_width;
int hpane_div;
hpane_width = GTK_WIDGET(na->hpane)->allocation.width;
hpane_div = gtk_paned_get_position (na->hpane);
if (hpane_div > hpane_width -CLOSED_MARGIN)
{
int vpane_height;
vpane_height = GTK_WIDGET(na->vpane)->allocation.height;
gtk_paned_set_position (na->vpane, vpane_height);
}
else
{
gtk_paned_set_position (na->hpane, 0);
}
}
static void
close_task_area (GtkButton *but, NotesArea *na)
{
int hpane_width;
int hpane_div;
hpane_width = GTK_WIDGET(na->hpane)->allocation.width;
hpane_div = gtk_paned_get_position (na->hpane);
/* XXX we really need only the first test, but the second
* one deals iwth a freaky gtk vpaned bug that makes this
* hidden button active. Whatever.
*/
if ((hpane_div < CLOSED_MARGIN) ||
(hpane_div > hpane_width -CLOSED_MARGIN))
{
int vpane_height;
vpane_height = GTK_WIDGET(na->vpane)->allocation.height;
gtk_paned_set_position (na->vpane, vpane_height);
}
else
{
gtk_paned_set_position (na->hpane, hpane_width);
}
}
/* ============================================================== */
static void
new_task_cb (GtkButton *but, NotesArea *na)
{
GttTask *tsk;
if (NULL == na->proj) return;
// if (na->ignore_events) return;
// na->ignore_events = TRUE;
tsk = gtt_task_new();
gtt_project_prepend_task (na->proj, tsk);
prop_task_dialog_show (tsk);
if (NULL != na->task_freeze) gtt_task_thaw (na->task_freeze);
gtt_task_freeze (tsk);
na->task_freeze = tsk;
// na->ignore_events = FALSE;
}
/* ============================================================== */
static void
edit_task_cb (GtkButton *but, NotesArea *na)
{
if (NULL == na->proj) return;
GttTask *tsk = gtt_project_get_current_task (na->proj);
prop_task_dialog_show (tsk);
if (NULL != na->task_freeze) gtt_task_thaw (na->task_freeze);
gtt_task_freeze (tsk);
na->task_freeze = tsk;
}
/* ============================================================== */
static void
task_selected_cb (GtkComboBox *combo, gpointer dialog)
{
GtkTreeIter iter;
GtkTreeModel *model = gtk_combo_box_get_model (combo);
NotesArea *na = (NotesArea *) dialog;
na->ignore_events = TRUE;
gtk_combo_box_get_active_iter (combo, &iter);
GttTask *task = tasks_model_get_task (model, &iter);
GttProject *project = gtt_task_get_parent (task);
gtt_project_set_current_task (project, task);
const char *str = gtt_task_get_notes (task);
if (!str) str = "";
xxxgtk_textview_set_text (na->task_notes, str);
na->ignore_events = FALSE;
}
/* ============================================================== */
enum {
COL_ID,
COL_TASK_MEMO,
COL_TASK_POINTER,
NUM_COLS
};
/* ============================================================== */
#define CONNECT_ENTRY(GLADE_NAME,CB) ({ \
GtkEntry * entry; \
entry = GTK_ENTRY(glade_xml_get_widget (gtxml, GLADE_NAME)); \
g_signal_connect (G_OBJECT (entry), "changed", \
G_CALLBACK (CB), dlg); \
entry; })
#define CONNECT_TEXT(GLADE_NAME,CB) ({ \
GtkTextView *tv; \
GtkTextBuffer *buff; \
tv = GTK_TEXT_VIEW(glade_xml_get_widget (gtxml, GLADE_NAME)); \
buff = gtk_text_view_get_buffer (tv); \
g_signal_connect (G_OBJECT (buff), "changed", \
G_CALLBACK (CB), dlg); \
tv; })
NotesArea *
notes_area_new (void)
{
NotesArea *dlg;
GladeXML *gtxml;
dlg = g_new0 (NotesArea, 1);
gtxml = gtt_glade_xml_new ("glade/notes.glade", "top window");
dlg->gtxml = gtxml;
dlg->vpane = GTK_PANED(glade_xml_get_widget (gtxml, "notes vpane"));
dlg->projects_tree_holder = GTK_CONTAINER(glade_xml_get_widget (gtxml, "ctree holder"));
dlg->hpane = GTK_PANED(glade_xml_get_widget (gtxml, "leftright hpane"));
dlg->close_proj = GTK_BUTTON(glade_xml_get_widget (gtxml, "close proj button"));
dlg->close_task = GTK_BUTTON(glade_xml_get_widget (gtxml, "close diary button"));
dlg->new_task = GTK_BUTTON(glade_xml_get_widget (gtxml, "new_diary_entry_button"));
dlg->edit_task = GTK_BUTTON(glade_xml_get_widget (gtxml, "edit_diary_entry_button"));
dlg->proj_title = CONNECT_ENTRY ("proj title entry", proj_title_changed);
dlg->proj_desc = CONNECT_ENTRY ("proj desc entry", proj_desc_changed);
dlg->task_combo = GTK_COMBO_BOX (glade_xml_get_widget (gtxml, "diary_entry_combo"));
gtk_combo_box_set_model (dlg->task_combo, NULL);
GtkCellRenderer *cell;
cell = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(dlg->task_combo), cell, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT (dlg->task_combo), cell, "text", 0, NULL);
g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
dlg->proj_notes = CONNECT_TEXT ("proj notes textview", proj_notes_changed);
dlg->task_notes = CONNECT_TEXT ("diary notes textview", task_notes_changed);
g_signal_connect (G_OBJECT (dlg->close_proj), "clicked",
G_CALLBACK (close_proj_area), dlg);
g_signal_connect (G_OBJECT (dlg->close_task), "clicked",
G_CALLBACK (close_task_area), dlg);
g_signal_connect (G_OBJECT (dlg->new_task), "clicked",
G_CALLBACK (new_task_cb), dlg);
g_signal_connect (G_OBJECT (dlg->edit_task), "clicked",
G_CALLBACK (edit_task_cb), dlg);
g_signal_connect (G_OBJECT (dlg->task_combo), "changed",
G_CALLBACK(task_selected_cb), dlg);
gtk_widget_show (GTK_WIDGET(dlg->vpane));
dlg->proj = NULL;
dlg->ignore_events = FALSE;
dlg->proj_freeze = FALSE;
dlg->task_freeze = NULL;
return dlg;
}
static void
notes_area_choose_task (NotesArea *na, GttTask *task)
{
GtkTreeIter iter;
GtkTreeModel *model = gtk_combo_box_get_model (na->task_combo);
if (gtk_tree_model_get_iter_first (model, &iter))
{
do
{
GttTask *t = tasks_model_get_task (model, &iter);
if (task == t)
{
gtk_combo_box_set_active_iter (na->task_combo, &iter);
break;
}
} while (gtk_tree_model_iter_next (model, &iter));
}
else
{
g_warning ("Trying to select task with empty tree model\n");
}
}
/* ============================================================== */
void
combo_model_add_task (gpointer task_p, gpointer model_p)
{
GttTask *task = (GttTask *) task_p;
GtkListStore *model = GTK_LIST_STORE (model_p);
GtkTreeIter iter;
gtk_list_store_append (model, &iter);
gtk_list_store_set (model, &iter,
0, gtt_task_get_memo (task),
1, task,
-1);
}
/* ============================================================== */
static GtkTreeModel *
build_task_combo_model (GttProject *proj)
{
GtkTreeModel *model = GTK_TREE_MODEL (gtk_list_store_new (2,
G_TYPE_STRING,
G_TYPE_POINTER));
g_list_foreach(gtt_project_get_tasks (proj),
combo_model_add_task,
model);
return model;
}
/* ============================================================== */
/* This routine copies data from the data engine, and pushes it
* into the GUI.
*/
static void
notes_area_do_set_project (NotesArea *na, GttProject *proj)
{
const char * str;
GttTask *tsk;
if (!na) return;
if (na->ignore_events) return;
/* Calling gtk_entry_set_text makes 'changed' events happen,
* which causes us to get the entry text, which exposes a gtk
* bug. So we work around the bug and save cpu time by ignoring
* change events during a mass update. */
na->ignore_events = TRUE;
/* Note Bene its OK to have the proj be null: this has the
* effect of clearing all the fields out.
*/
na->proj = proj;
/* Fetch data from the data engine, stuff it into the GUI. */
str = gtt_project_get_title (proj);
if (!str) str = "";
gtk_entry_set_text (na->proj_title, str);
str = gtt_project_get_desc (proj);
if (!str) str = "";
gtk_entry_set_text (na->proj_desc, str);
str = gtt_project_get_notes (proj);
if (!str) str = "";
xxxgtk_textview_set_text (na->proj_notes, str);
GtkTreeModel *model = build_task_combo_model (proj);
gtk_combo_box_set_model (na->task_combo, model);
g_object_unref (model);
tsk = gtt_project_get_current_task (proj);
if (tsk == NULL)
tsk = gtt_project_get_first_task (proj);
notes_area_choose_task (na, tsk);
na->ignore_events = FALSE;
}
/* ============================================================== */
static void
redraw (GttProject *prj, gpointer data)
{
NotesArea *na = data;
if (na->ignore_events) return;
notes_area_do_set_project (na, prj);
}
/* ============================================================== */
void
notes_area_set_project (NotesArea *na, GttProject *proj)
{
if (na->proj != NULL) {
gtt_project_remove_notifier (na->proj, redraw, na);
na->proj = NULL;
}
if (proj != NULL) {
gtt_project_add_notifier (proj, redraw, na);
}
notes_area_do_set_project (na, proj);
}
/* ============================================================== */
GtkWidget *
notes_area_get_widget (NotesArea *nadlg)
{
if (!nadlg) return NULL;
return GTK_WIDGET(nadlg->vpane);
}
static void
projects_tree_selection_changed (GtkTreeSelection *selection, gpointer user_data)
{
NotesArea *nadlg = (NotesArea *) user_data;
GttProjectsTree *gpt = GTT_PROJECTS_TREE (gtk_tree_selection_get_tree_view (selection));
GttProject *prj = gtt_projects_tree_get_selected_project (gpt);
notes_area_set_project (nadlg, prj);
}
static int
projects_tree_clicked (GtkWidget *ptree, GdkEvent *event, gpointer data)
{
GdkEventButton *bevent = (GdkEventButton *) event;
// GttProjectsTree *projects_tree = GTT_PROJECTS_TREE (ptree);
GtkMenuShell *menu;
if (!(event->type == GDK_BUTTON_PRESS && bevent->button == 3))
{
return FALSE;
}
menu = menus_get_popup ();
gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 3, bevent->time);
return FALSE;
}
void
notes_area_add_projects_tree (NotesArea *nadlg, GttProjectsTree *ptree)
{
if (!nadlg) return;
GtkTreeSelection *tree_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (ptree));
g_signal_connect (tree_selection, "changed", G_CALLBACK (projects_tree_selection_changed), nadlg);
gtk_container_add (nadlg->projects_tree_holder, GTK_WIDGET(ptree));
gtk_widget_show_all (GTK_WIDGET(nadlg->projects_tree_holder));
g_signal_connect (GTK_WIDGET (ptree), "button_press_event",
G_CALLBACK (projects_tree_clicked), NULL);
}
void
notes_area_get_pane_sizes (NotesArea *na, int *vp, int *hp)
{
if (!na) return;
if(vp) *vp = gtk_paned_get_position (na->vpane);
if(hp) *hp = gtk_paned_get_position (na->hpane);
}
void
notes_area_set_pane_sizes (NotesArea *na, int vp, int hp)
{
if (!na) return;
gtk_paned_set_position (na->vpane, vp);
gtk_paned_set_position (na->hpane, hp);
}
/* ========================= END OF FILE ======================== */
|