summaryrefslogtreecommitdiff
path: root/src/igbox.cpp
blob: d3c6aa383cf7500369cd0e21ff7ba27b619bddec (plain)
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
/**************************************************************************\
 ibtk (Insomnia's Basic ToolKit)

  By Insomnia (Steaphan Greene)
  (insomnia@core.binghamton.edu)

  Copyright (C) 1999 Steaphan Greene

  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, USA.

\**************************************************************************/

#include <string.h>
#include <stdio.h>

#include "igbox.h"

IGBox::~IGBox()  {
  Win->RemoveClaim(this);
  }

IGBox::IGBox(IWindow *w, int xp, int yp, int xs, int ys)  {
  Init(w, xp, yp, xs, ys);
  }

void IGBox::Init(IWindow *w, int xp, int yp, int xs, int ys)  {
  xpos = xp; ypos = yp; xsize = xs; ysize = ys;
  Win = w;
  Img = None;
  parent=NULL;
  clickcallback = NULL;
  dragcallback = NULL;
  lastx = -1;
  lasty = -1;
  Create();
  }

void IGBox::Create()  {
  Window wind = Win->GetWindowBuffer();
  Display *disp = Win->GetDisplay();
  unsigned int p, b, g, gl, gd;
  if(Img != None)  XFreePixmap(disp, Img);
  Img = XCreatePixmap(disp, wind, xsize, ysize, DefaultDepth(disp, 0));
  GC gc = XCreateGC(disp, Img, 0, 0);

//  printf("Depth = %d\n", DefaultDepth(disp, 0));

  p = Win->GetPaperColor();
  b = Win->GetFGColor();
  g = Win->GetBGColor();
  gl = Win->GetLBGColor();
  gd = Win->GetDBGColor();

  XSetBackground(disp, gc, p);
  XSetForeground(disp, gc, gl);
  XFillRectangle(disp, Img, gc, 0, 0, xsize, ysize);

  XSetForeground(disp, gc, p);
  XFillRectangle(disp, Img, gc, 2, 2, xsize-4, ysize-4);

  XSetForeground(disp, gc, gd);
  XFillRectangle(disp, Img, gc, 0, 0, xsize-1, 2);
  XFillRectangle(disp, Img, gc, 0, 2, 2, ysize-3);
  XDrawPoint(disp, Img, gc, 0, ysize-1);
  XDrawPoint(disp, Img, gc, xsize-1, 0);

  Win->AddClaim(this, xpos, ypos, xsize, ysize);

  XFreeGC(disp, gc);
  Redraw();
  }

void IGBox::Rebuild()  {
  Redraw();
  }

void IGBox::Redraw()  {
  if(hidden) { Win->Rebuild(xpos, ypos, xsize, ysize); return; }
  XCopyArea(Win->GetDisplay(), Img, Win->GetWindowBuffer(), Win->GetGC(),
	0, 0, xsize, ysize, xpos, ypos);
  }

void IGBox::DrawPoint(int x, int y, int r, int g, int b) {
  unsigned int c = Win->GetRGBColor(r, g, b);
  GC gc = Win->GetGC();
  Display *Disp = Win->GetDisplay();
  XSetForeground(Disp, gc, c);
  XDrawPoint(Disp, Img, gc, x+2, y+2);
  }

void IGBox::DrawLine(int x1, int y1, int x2, int y2, int r, int g, int b) {
  unsigned int c = Win->GetRGBColor(r, g, b);
  GC gc = Win->GetGC();
  Display *Disp = Win->GetDisplay();
  XSetForeground(Disp, gc, c);
  int xd = (x2-x1), yd = (y2-y1);
  int xd2 = xd*xd, yd2 = yd*yd;
  int ctr;
  if(xd==0 && yd==0) XDrawPoint(Disp, Img, gc, x1, y1);
  if(xd2>yd2 && xd>=0) for(ctr=0; ctr<xd; ctr++)
    XDrawPoint(Disp, Img, gc, 2+x1+ctr, 2+y1+((ctr*yd)/xd));
  else if(xd2>yd2) for(ctr=0; ctr>xd; ctr--)
    XDrawPoint(Disp, Img, gc, 2+x1+ctr, 2+y1+((ctr*yd)/xd));
  else if(yd>=0) for(ctr=0; ctr<yd; ctr++)
    XDrawPoint(Disp, Img, gc, 2+x1+((ctr*xd)/yd), 2+y1+ctr);
  else for(ctr=0; ctr>yd; ctr--)
    XDrawPoint(Disp, Img, gc, 2+x1+((ctr*xd)/yd), 2+y1+ctr);
  }

void IGBox::DrawRect(int x1, int y1, int x2, int y2, int r, int g, int b) {
  }

void IGBox::FillRect(int xp, int yp, int xs, int ys, int r, int g, int b) {
  unsigned int c = Win->GetRGBColor(r, g, b);
  GC gc = Win->GetGC();
  Display *Disp = Win->GetDisplay();
  XSetForeground(Disp, gc, c);
  XFillRectangle(Disp, Img, gc, xp+2, yp+2, xs, ys);
  }

void IGBox::Resize(int xs, int ys)  {
  xsize = xs; ysize = ys;
  Create();
  }
        
void IGBox::Move(int xp, int yp)  {
  xpos = xp; ypos = yp;
  Create();
  }
 
void IGBox::MoveAndResize(int xp, int yp, int xs, int ys)  {
  xsize = xs; ysize = ys; xpos = xp; ypos = yp;
//  printf("MoveAndResize(%d, %d, %d, %d)\n", xp, yp, xs, ys);
  Create();
  }

int IGBox::Press(int b, int x, int y)  {
  lastx = x; lasty = y;
  return 1;
  }
 
int IGBox::Release(int b, int x, int y)  {
  if(lastx == x && lasty == y && clickcallback != NULL)
    (*clickcallback)(parent, this, x-2, y-2);
  else if(dragcallback != NULL)
    (*dragcallback)(parent, this, lastx, lasty, x-2, y-2);
  lastx = -1; lasty = -1;
  return 1;
  }

void IGBox::SetClickCallback(void (*cb)(IDoDad *, IDoDad *, int, int))  {
  clickcallback = cb;
  }
 
void IGBox::SetDragCallback(
	void (*cb)(IDoDad *, IDoDad *, int, int, int, int))  {
  dragcallback = cb;
  }