First checkin, AXE release 0.2
[AXE.git] / src / table.cpp
1 /**************************************************************************
2 **  (c) Copyright 1999, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : table.cpp
7 **      SYSTEM NAME    : AXE - Andromeda X-windows Encapsulation
8 **      VERSION NUMBER : $Revision: 1.1 $
9 **
10 **  DESCRIPTION      :  Implementation of table_view class
11 **
12 **  EXPORTED OBJECTS : 
13 **  LOCAL    OBJECTS : 
14 **  MODULES  USED    :
15 ***************************************************************************
16 **  ADMINISTRATIVE INFORMATION *
17 ********************************
18 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 **      CREATION DATE   : Aug 26, 1999
20 **      LAST UPDATE     : Aug 26, 1999
21 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: table.cpp,v $
26    Revision 1.1  2002-07-25 08:01:27  arjen
27    First checkin, AXE release 0.2
28
29 *****************************/
30
31 static const char *RCSID = "$Id: table.cpp,v 1.1 2002-07-25 08:01:27 arjen Exp $";
32
33 #include "table.h"
34
35 void table_view::redraw(void)
36 {
37    int      i, j, x, y;
38    unsigned w, h;
39    unsigned desired_height;
40    unsigned font_height = 20;  //  For the time being...
41
42    Size(w, h);
43    g.SetLineAttributes(2);
44    XDrawLine(stddpy, ID(), g, 0, 20, w, 20);
45
46    //  Determine if we need the scrollbar
47
48    desired_height = (nr_rows + 1) * font_height;
49    if (desired_height > h)
50    {
51       unsigned visible_rows;
52
53       visible_rows = (h - font_height) / font_height;
54       Vscroll->SetSizes(nr_rows, visible_rows);
55       Vscroll->Move(w-18, font_height+1);
56       Vscroll->Resize(16, h - font_height - 2);
57       Vscroll->Map();
58    }
59    else
60    {
61       //  All rows fit in the window
62
63       Vscroll->Unmap();
64    }
65
66    //  Draw the grid
67
68    g.SetLineAttributes(0);
69    x = 0;
70    for (i = 0; i < nr_columns; i++)
71    {
72       x += Head[i].width;
73       XDrawLine(stddpy, ID(), g, x, 0, x, h);
74    }
75
76    y = font_height;
77    g.SetLineAttributes(0, LineOnOffDash);
78    XSetDashes(stddpy, g, 0, "\001\001", 2);
79    for (j = 0; j < nr_rows && (unsigned)y < h; j++)
80    {
81       y += font_height;
82       XDrawLine(stddpy, ID(), g, 0, y, w, y);
83    }
84
85    //  Draw the header
86
87    g.SetFont(header_font);
88    x = 2;                    // small margin
89    for (i = 0; i < nr_columns; i++)
90    {
91       if ((char *)(Head[i].title))
92       {
93          XDrawString(stddpy, ID(), g, x, 15, Head[i].title, ~Head[i].title);
94       }
95       x += Head[i].width;
96    }
97
98    //  Draw the cells, depending on the scroll position.
99
100    g.SetFont(cell_font);
101    y = 35;
102    for (i = *Vscroll; i < nr_rows && (unsigned)y < h; i++)
103    {
104       x = 2;
105       for (j = 0; j < nr_columns; j++)
106       {
107          XClearArea(stddpy, ID(), x, y - 14, Head[j].width - 2,
108                                           font_height -2, False);
109          if (i == selected_row)
110          {
111             XFillRectangle(stddpy, ID(), g, x, y - 14, Head[j].width - 2,
112                                           font_height -2);
113             g.SetForeground(stddpy.White());
114          }
115          if ((char *)(Cells[i][j]))
116          {
117             int  text_x, text_width;
118
119             text_width = cell_font.TextWidth(Cells[i][j]);
120             switch (Head[j].alignment)
121             {
122             case Left:
123                text_x = x;
124                break;
125             case Center:
126                text_x = x + (Head[j].width - text_width) / 2;
127                break;
128             case Right:
129                text_x = x + Head[j].width - text_width - 4;
130                break;
131             }
132             XDrawString(stddpy, ID(), g, text_x, y, Cells[i][j], ~Cells[i][j]);
133          }
134          g.SetForeground(stddpy.Black());
135          x += Head[j].width;
136       }
137       y += font_height;
138    }
139 }
140
141 int table_view::EV_ButtonPress(XButtonEvent ev)
142 {
143    unsigned font_height = 20;  //  Should be obtained from cell_font
144    int      selected;
145
146    if ((unsigned)ev.y > font_height)
147    {
148       //   Below the header
149
150       selected = (ev.y - font_height) / font_height + *Vscroll;
151       if (selected < nr_rows)
152       {
153          selected_row = selected;
154          redraw();
155          ParentMessage(TABLE_ROW_SELECT);
156       }
157    }
158    return 1;
159 }
160
161 void table_view::ChildMessage(win_message &msg)
162 {
163    if (msg.msg_id == SCROLL_POSITIONED)
164    {
165       redraw();
166    }
167 }
168