summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/boxes.c
blob: cce66493f22530bcabc71e78e18733d0168ef122 (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
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
/** \file boxes.c
 * Window for drawing a rectangle
 */

/* 	XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis,
 *                2016 Martin Fischer <m_fischer@sf.net>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

#define GTK_DISABLE_SINGLE_INCLUDES
#define GDK_DISABLE_DEPRECATED
#define GTK_DISABLE_DEPRECATED
#define GSEAL_ENABLE

#include <cairo.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>

#include "gtkint.h"

struct wBox_t {
    WOBJ_COMMON
    wBoxType_e boxTyp;
};

#define B (1)
#define W (2)
#define SETCOLOR(ST, S, N ) \
		if (colors[ST][S][N] == B ) { \
			cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); \
		} else {	\
			cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); \
		}

/**
 * Set size of box window
 *
 * \param b IN window handle
 * \param w IN new width
 * \param h IN new height
 * \return
 *
 */

void wBoxSetSize(
    wBox_p b,	
    wPos_t w,	
    wPos_t h)	
{
    b->w = w;
    b->h = h;
}

/**
 * Draw the box
 * \todo too many strokes, remove and test
 *
 * \param win IN window handle
 * \param style IN frame style
 * \param x IN x position
 * \param y IN y position
 * \param w IN width
 * \param h IN height
 * \return
 */

void wlibDrawBox(
    wWin_p win,
    wBoxType_e style,
    wPos_t x,
    wPos_t y,
    wPos_t w,
    wPos_t h)
{
    wPos_t x0, y0, x1, y1;
    GdkDrawable * window;
    cairo_t *cr;
    static char colors[8][4][2] = {
        { /* ThinB  */ {B,0}, {B,0}, {B,0}, {B,0} },
        { /* ThinW  */ {W,0}, {W,0}, {W,0}, {W,0} },
        { /* AboveW */ {W,0}, {W,0}, {B,0}, {B,0} },
        { /* BelowW */ {B,0}, {B,0}, {W,0}, {W,0} },
        { /* ThickB */ {B,B}, {B,B}, {B,B}, {B,B} },
        { /* ThickW */ {W,W}, {W,W}, {W,W}, {W,W} },
        { /* RidgeW */ {W,B}, {W,B}, {B,W}, {B,W} },
        { /* TroughW*/ {B,W}, {B,W}, {W,B}, {W,B} }
    };
    window = gtk_widget_get_window(win->widget);
    cr = gdk_cairo_create(window);
    cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
    cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
    cairo_set_line_width(cr, 1.0);
    x0 = x;
    x1 = x+w;
    y0 = y;
    y1 = y+h;
    SETCOLOR(style, 0, 0);
    cairo_move_to(cr, x0, y0);
    cairo_line_to(cr, x0, y1);
    cairo_stroke_preserve(cr);
    SETCOLOR(style, 1, 0);
    cairo_move_to(cr, x0, y0);
    cairo_line_to(cr, x1, y0);
    cairo_stroke_preserve(cr);
    SETCOLOR(style, 2, 0);
    cairo_move_to(cr, x1, y1);
    cairo_line_to(cr, x0+1, y1);
    cairo_stroke_preserve(cr);
    SETCOLOR(style, 3, 0);
    cairo_move_to(cr, x1, y1-1);
    cairo_line_to(cr, x1, y0+1);
    cairo_stroke_preserve(cr);

    if (style < wBoxThickB) {
        return;
    }

    x0++;
    y0++;
    x1--;
    y1--;
    SETCOLOR(style, 0, 1);
    cairo_move_to(cr, x0, y0);
    cairo_line_to(cr, x0, y1);
    cairo_stroke_preserve(cr);
    SETCOLOR(style, 1, 1);
    cairo_move_to(cr, x0+1, y0);
    cairo_line_to(cr, x1, y0);
    cairo_stroke_preserve(cr);
    SETCOLOR(style, 2, 1);
    cairo_move_to(cr, x1, y1);
    cairo_line_to(cr, x0+1, y1);
    cairo_stroke_preserve(cr);
    SETCOLOR(style, 3, 1);
    cairo_move_to(cr, x1, y1-1);
    cairo_line_to(cr, x1, y0+1);
    cairo_stroke_preserve(cr);
    cairo_destroy(cr);
}

/**
 * Force repainting of box window
 *
 * \param b IN box window
 * \return
 */

static void boxRepaint(wControl_p b)
{
    wBox_p bb = (wBox_p)(b);
    wWin_p win = bb->parent;
    wlibDrawBox(win, bb->boxTyp, bb->realX, bb->realY, bb->w, bb->h);
}

/**
 * Create new box
 *
 * \param parent IN parent window
 * \param bx IN x position
 * \param by IN y position
 * \param labelStr IN label text (ignored)
 * \param boxTyp IN style
 * \param bw IN x width
 * \param by IN y width
 * \return window handle for newly created box
 */

wBox_p wBoxCreate(
    wWin_p	parent,
    wPos_t	bx,
    wPos_t	by,
    const char	* labelStr,
    wBoxType_e boxTyp,
    wPos_t	bw,
    wPos_t	bh)
{
    wBox_p b;
    b = (wBox_p)wlibAlloc(parent, B_BOX, bx, by, labelStr, sizeof *b, NULL);
    wlibComputePos((wControl_p)b);
    b->boxTyp = boxTyp;
    b->w = bw;
    b->h = bh;
    b->repaintProc = boxRepaint;
    wlibAddButton((wControl_p)b);
    return b;
}