2001-08-19 03:42:36 +04:00
|
|
|
//
|
2005-02-06 03:09:17 +03:00
|
|
|
// "$Id$"
|
2001-08-19 03:42:36 +04:00
|
|
|
//
|
|
|
|
// Fl_Check_Browser header file for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
2001-08-19 03:42:36 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/COPYING.php
|
2001-08-19 03:42:36 +04:00
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
2001-08-19 03:42:36 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-04-11 15:52:43 +04:00
|
|
|
#include "flstring.h"
|
2001-08-19 03:42:36 +04:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/Fl_Check_Browser.H>
|
|
|
|
|
|
|
|
/* This uses a cache for faster access when you're scanning the list
|
|
|
|
either forwards or backwards. */
|
|
|
|
|
|
|
|
Fl_Check_Browser::cb_item *Fl_Check_Browser::find_item(int n) const {
|
|
|
|
int i = n;
|
|
|
|
cb_item *p = first;
|
|
|
|
|
|
|
|
if (n <= 0 || n > nitems_ || p == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == cached_item) {
|
|
|
|
p = cache;
|
|
|
|
n = 1;
|
|
|
|
} else if (n == cached_item + 1) {
|
|
|
|
p = cache->next;
|
|
|
|
n = 1;
|
|
|
|
} else if (n == cached_item - 1) {
|
|
|
|
p = cache->prev;
|
|
|
|
n = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (--n) {
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cast to not const and cache it. */
|
|
|
|
|
|
|
|
((Fl_Check_Browser *)this)->cache = p;
|
|
|
|
((Fl_Check_Browser *)this)->cached_item = i;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fl_Check_Browser::lineno(cb_item *p0) const {
|
|
|
|
cb_item *p = first;
|
|
|
|
|
|
|
|
if (p == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
while (p) {
|
|
|
|
if (p == p0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
Fl_Check_Browser::Fl_Check_Browser(int X, int Y, int W, int H, const char *l)
|
2008-09-14 16:45:42 +04:00
|
|
|
/** The constructor makes an empty browser.*/
|
|
|
|
: Fl_Browser_(X, Y, W, H, l) {
|
2001-08-19 03:42:36 +04:00
|
|
|
type(FL_SELECT_BROWSER);
|
|
|
|
when(FL_WHEN_NEVER);
|
|
|
|
first = last = 0;
|
|
|
|
nitems_ = nchecked_ = 0;
|
|
|
|
cached_item = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Fl_Check_Browser::item_first() const {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Fl_Check_Browser::item_next(void *l) const {
|
|
|
|
return ((cb_item *)l)->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Fl_Check_Browser::item_prev(void *l) const {
|
|
|
|
return ((cb_item *)l)->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fl_Check_Browser::item_height(void *) const {
|
|
|
|
return textsize() + 2;
|
|
|
|
}
|
|
|
|
|
2002-11-15 00:25:13 +03:00
|
|
|
#define CHECK_SIZE (textsize()-2)
|
2001-08-19 03:42:36 +04:00
|
|
|
|
|
|
|
int Fl_Check_Browser::item_width(void *v) const {
|
|
|
|
fl_font(textfont(), textsize());
|
|
|
|
return int(fl_width(((cb_item *)v)->text)) + CHECK_SIZE + 8;
|
|
|
|
}
|
|
|
|
|
2002-08-09 05:09:49 +04:00
|
|
|
void Fl_Check_Browser::item_draw(void *v, int X, int Y, int, int) const {
|
2001-08-19 03:42:36 +04:00
|
|
|
cb_item *i = (cb_item *)v;
|
|
|
|
char *s = i->text;
|
2002-08-09 05:09:49 +04:00
|
|
|
int tsize = textsize();
|
2005-02-06 03:09:17 +03:00
|
|
|
Fl_Color col = active_r() ? textcolor() : fl_inactive(textcolor());
|
2002-08-09 05:09:49 +04:00
|
|
|
int cy = Y + (tsize + 1 - CHECK_SIZE) / 2;
|
|
|
|
X += 2;
|
2001-08-19 03:42:36 +04:00
|
|
|
|
2005-11-02 13:48:05 +03:00
|
|
|
fl_color(active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR));
|
2002-08-09 05:09:49 +04:00
|
|
|
fl_loop(X, cy, X, cy + CHECK_SIZE,
|
|
|
|
X + CHECK_SIZE, cy + CHECK_SIZE, X + CHECK_SIZE, cy);
|
2001-08-19 03:42:36 +04:00
|
|
|
if (i->checked) {
|
2002-11-15 00:25:13 +03:00
|
|
|
int tx = X + 3;
|
|
|
|
int tw = CHECK_SIZE - 4;
|
|
|
|
int d1 = tw/3;
|
|
|
|
int d2 = tw-d1;
|
|
|
|
int ty = cy + (CHECK_SIZE+d2)/2-d1-2;
|
|
|
|
for (int n = 0; n < 3; n++, ty++) {
|
|
|
|
fl_line(tx, ty, tx+d1, ty+d1);
|
|
|
|
fl_line(tx+d1, ty+d1, tx+tw-1, ty+d1-d2+1);
|
|
|
|
}
|
2001-08-19 03:42:36 +04:00
|
|
|
}
|
2002-08-09 05:09:49 +04:00
|
|
|
fl_font(textfont(), tsize);
|
2001-08-19 03:42:36 +04:00
|
|
|
if (i->selected) {
|
2001-10-29 06:44:33 +03:00
|
|
|
col = fl_contrast(col, selection_color());
|
2001-08-19 03:42:36 +04:00
|
|
|
}
|
|
|
|
fl_color(col);
|
2002-08-09 05:09:49 +04:00
|
|
|
fl_draw(s, X + CHECK_SIZE + 8, Y + tsize - 1);
|
2001-08-19 03:42:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Check_Browser::item_select(void *v, int state) {
|
|
|
|
cb_item *i = (cb_item *)v;
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
if (i->checked) {
|
|
|
|
i->checked = 0;
|
|
|
|
nchecked_--;
|
|
|
|
} else {
|
|
|
|
i->checked = 1;
|
|
|
|
nchecked_++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fl_Check_Browser::item_selected(void *v) const {
|
|
|
|
cb_item *i = (cb_item *)v;
|
|
|
|
return i->selected;
|
|
|
|
}
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
2011-02-01 18:41:04 +03:00
|
|
|
Add a new unchecked line to the end of the browser.
|
|
|
|
\see add(char *s, int b)
|
2008-09-14 16:45:42 +04:00
|
|
|
*/
|
2001-08-19 03:42:36 +04:00
|
|
|
int Fl_Check_Browser::add(char *s) {
|
|
|
|
return (add(s, 0));
|
|
|
|
}
|
|
|
|
|
2011-02-01 18:41:04 +03:00
|
|
|
/**
|
|
|
|
Add a new line to the end of the browser. The text is copied
|
|
|
|
using the strdup() function. It may also be NULL to make
|
|
|
|
a blank line. It can set the item checked if \p b is not 0.
|
|
|
|
*/
|
2001-08-19 03:42:36 +04:00
|
|
|
int Fl_Check_Browser::add(char *s, int b) {
|
|
|
|
cb_item *p = (cb_item *)malloc(sizeof(cb_item));
|
|
|
|
p->next = 0;
|
|
|
|
p->prev = 0;
|
|
|
|
p->checked = b;
|
|
|
|
p->selected = 0;
|
|
|
|
p->text = strdup(s);
|
|
|
|
|
|
|
|
if (b) {
|
|
|
|
nchecked_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last == 0) {
|
|
|
|
first = last = p;
|
|
|
|
} else {
|
|
|
|
last->next = p;
|
|
|
|
p->prev = last;
|
|
|
|
last = p;
|
|
|
|
}
|
|
|
|
nitems_++;
|
|
|
|
|
|
|
|
return (nitems_);
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/**
|
|
|
|
Remove line n and make the browser one line shorter. Returns the
|
|
|
|
number of lines left in the browser.
|
|
|
|
*/
|
2007-11-19 18:46:03 +03:00
|
|
|
int Fl_Check_Browser::remove(int item) {
|
|
|
|
cb_item *p = find_item(item);
|
|
|
|
|
|
|
|
// line at item exists
|
|
|
|
if(p) {
|
|
|
|
// tell the Browser_ what we will do
|
|
|
|
deleting(p);
|
|
|
|
|
|
|
|
// fix checked count
|
|
|
|
if(p->checked)
|
|
|
|
--nchecked_;
|
|
|
|
|
|
|
|
// remove the node
|
|
|
|
if (p->prev)
|
|
|
|
p->prev->next = p->next;
|
|
|
|
else
|
|
|
|
first = p->next;
|
|
|
|
if (p->next)
|
|
|
|
p->next->prev = p->prev;
|
|
|
|
else
|
|
|
|
last = p->prev;
|
|
|
|
|
|
|
|
free(p->text);
|
|
|
|
free(p);
|
|
|
|
|
|
|
|
--nitems_;
|
|
|
|
cached_item = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (nitems_);
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/** Remove every item from the browser.*/
|
2001-08-19 03:42:36 +04:00
|
|
|
void Fl_Check_Browser::clear() {
|
|
|
|
cb_item *p = first;
|
|
|
|
cb_item *next;
|
|
|
|
|
|
|
|
if (p == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_list();
|
|
|
|
do {
|
|
|
|
next = p->next;
|
|
|
|
free(p->text);
|
|
|
|
free(p);
|
|
|
|
p = next;
|
|
|
|
} while (p);
|
|
|
|
|
|
|
|
first = last = 0;
|
|
|
|
nitems_ = nchecked_ = 0;
|
|
|
|
cached_item = -1;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/** Gets the current status of item item. */
|
2001-08-19 03:42:36 +04:00
|
|
|
int Fl_Check_Browser::checked(int i) const {
|
|
|
|
cb_item *p = find_item(i);
|
|
|
|
|
|
|
|
if (p) return p->checked;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/** Sets the check status of item item to b. */
|
2001-08-19 03:42:36 +04:00
|
|
|
void Fl_Check_Browser::checked(int i, int b) {
|
|
|
|
cb_item *p = find_item(i);
|
|
|
|
|
|
|
|
if (p && (p->checked ^ b)) {
|
|
|
|
p->checked = b;
|
|
|
|
if (b) {
|
|
|
|
nchecked_++;
|
|
|
|
} else {
|
|
|
|
nchecked_--;
|
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/** Returns the index of the currently selected item.*/
|
2001-08-19 03:42:36 +04:00
|
|
|
int Fl_Check_Browser::value() const {
|
|
|
|
return lineno((cb_item *)selection());
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/** Return a pointer to an internal buffer holding item item's text.*/
|
2001-08-19 03:42:36 +04:00
|
|
|
char *Fl_Check_Browser::text(int i) const {
|
|
|
|
cb_item *p = find_item(i);
|
|
|
|
|
|
|
|
if (p) return p->text;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/** Sets all the items checked.*/
|
2001-08-19 03:42:36 +04:00
|
|
|
void Fl_Check_Browser::check_all() {
|
|
|
|
cb_item *p;
|
|
|
|
|
|
|
|
nchecked_ = nitems_;
|
|
|
|
for (p = first; p; p = p->next) {
|
|
|
|
p->checked = 1;
|
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2008-09-14 16:45:42 +04:00
|
|
|
/** Sets all the items unchecked.*/
|
2001-08-19 03:42:36 +04:00
|
|
|
void Fl_Check_Browser::check_none() {
|
|
|
|
cb_item *p;
|
|
|
|
|
|
|
|
nchecked_ = 0;
|
|
|
|
for (p = first; p; p = p->next) {
|
|
|
|
p->checked = 0;
|
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2006-04-20 07:53:41 +04:00
|
|
|
int Fl_Check_Browser::handle(int event) {
|
|
|
|
if (event==FL_PUSH)
|
|
|
|
deselect();
|
|
|
|
return Fl_Browser_::handle(event);
|
|
|
|
}
|
2001-08-19 03:42:36 +04:00
|
|
|
|
|
|
|
//
|
2005-02-06 03:09:17 +03:00
|
|
|
// End of "$Id$".
|
2001-08-19 03:42:36 +04:00
|
|
|
//
|