summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/gtksimple.c
blob: 244c0a381c294954acc6e8045a6fbb8ee4f113b1 (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
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
/*
 * $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/wlib/gtklib/gtksimple.c,v 1.6 2009-09-25 05:38:15 dspagnol Exp $
 */

/*  XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis
 *
 *  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 <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>

#include "gtkint.h"

static int windowxxx = 0;
/*
 *****************************************************************************
 *
 * Message Boxes
 *
 *****************************************************************************
 */

struct wMessage_t {
		WOBJ_COMMON
		GtkWidget * labelWidget;
		const char * message;
		wPos_t labelWidth;
		};

EXPORT void wMessageSetValue(
		wMessage_p b,
		const char * arg )
{
	if (b->widget == 0) abort();
	gtk_label_set( GTK_LABEL( b->labelWidget ), gtkConvertInput(arg) );
}


EXPORT void wMessageSetWidth(
		wMessage_p b,
		wPos_t width )
{
	b->labelWidth = width;
#ifndef GTK1
	gtk_widget_set_size_request( b->widget, width, -1 );
#else
	gtk_widget_set_usize( b->widget, width, -1 );
#endif
}


EXPORT wPos_t wMessageGetHeight(
		long flags )
{
	return 14;
}

/**
 * Create a window for a simple text. 
 *
 * \param IN parent Handle of parent window
 * \param IN x position in x direction
 * \param IN y position in y direction
 * \param IN labelStr ???
 * \param IN width horizontal size of window
 * \param IN message message to display ( null terminated )
 * \param IN flags display options
 * \return handle for created window
 */

EXPORT wMessage_p wMessageCreateEx(
		wWin_p	parent,
		wPos_t	x,
		wPos_t	y,
		const char 	* labelStr,
		wPos_t	width,
		const char	*message, 
		long flags )
{
	wMessage_p b;
	GtkRequisition requisition;
	PangoFontDescription *fontDesc;
	int fontSize;

	b = (wMessage_p)gtkAlloc( parent, B_MESSAGE, x, y, NULL, sizeof *b, NULL );
	gtkComputePos( (wControl_p)b );
	b->message = message;
	b->labelWidth = width;

	b->labelWidget = gtk_label_new( message?gtkConvertInput(message):"" );

	/* do we need to set a special font? */
	if( wMessageSetFont( flags ))	{
		/* get the current font descriptor */
		fontDesc = (b->labelWidget)->style->font_desc;
		
		/* get the current font size */
		fontSize = PANGO_PIXELS(pango_font_description_get_size( fontDesc ));
		
		/* calculate the new font size */
		if( flags & BM_LARGE ) {
			pango_font_description_set_size( fontDesc, fontSize * 1.4 * PANGO_SCALE );
		} else {
			pango_font_description_set_size( fontDesc, fontSize * 0.7 * PANGO_SCALE );
		}			
		
		/* set the new font size */
		gtk_widget_modify_font( (GtkWidget *)b->labelWidget, fontDesc );
	}

	b->widget = gtk_fixed_new();
	gtk_widget_size_request( GTK_WIDGET(b->labelWidget), &requisition );
	gtk_container_add( GTK_CONTAINER(b->widget), b->labelWidget );
	
	gtk_widget_set_size_request( b->widget, width?width:requisition.width, requisition.height );
	gtkControlGetSize( (wControl_p)b );
	gtk_fixed_put( GTK_FIXED(parent->widget), b->widget, b->realX, b->realY );

	gtk_widget_show( b->widget );
	gtk_widget_show( b->labelWidget );
	gtkAddButton( (wControl_p)b );

	/* Reset font size to normal */
	if( wMessageSetFont( flags ))	{
		if( flags & BM_LARGE ) {
			pango_font_description_set_size(fontDesc, fontSize * PANGO_SCALE);
		} else {
			pango_font_description_set_size(fontDesc, fontSize * PANGO_SCALE);
		}
	}
	return b;
}

/*
 *****************************************************************************
 *
 * Lines
 *
 *****************************************************************************
 */

struct wLine_t {
		WOBJ_COMMON
		wBool_t visible;
		int count;
		wLines_t * lines;
		};

static void linesRepaint( wControl_p b )
{
	wLine_p bl = (wLine_p)(b);
	int i;
	wWin_p win = (wWin_p)(bl->parent);
	GdkDrawable * window;
	GdkColor *black;

	if (!bl->visible)
		return;
if (windowxxx)
	window = win->gtkwin->window;
else
	window = win->widget->window;

	/* get the GC attached to the panel in main() */
	black = gtkGetColor( wDrawColorBlack, TRUE );
	gdk_gc_set_foreground( win->gc, black );
	gdk_gc_set_function( win->gc, GDK_COPY );
	for (i=0; i<bl->count; i++) {
		if (win->gc_linewidth != bl->lines[i].width) {
			win->gc_linewidth = bl->lines[i].width;
			gdk_gc_set_line_attributes( win->gc, win->gc_linewidth, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER );
		}
		gdk_draw_line( window, win->gc,
				bl->lines[i].x0, bl->lines[i].y0,
				bl->lines[i].x1, bl->lines[i].y1 );
	}
}


void gtkLineShow(
		wLine_p bl,
		wBool_t visible )
{
	bl->visible = visible;
}


wLine_p wLineCreate(
		wWin_p	parent,
		const char	* labelStr,
		int	count,
		wLines_t * lines )
{
	wLine_p b;
	int i;
	b = (wLine_p)gtkAlloc( parent, B_LINES, 0, 0, labelStr, sizeof *b, NULL );
	if (parent->gc == NULL) {
		parent->gc = gdk_gc_new( parent->gtkwin->window );
		gdk_gc_copy( parent->gc, parent->gtkwin->style->base_gc[GTK_STATE_NORMAL] );
		parent->gc_linewidth = 0;
		gdk_gc_set_line_attributes( parent->gc, parent->gc_linewidth, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER );
	}
	b->visible = TRUE;
	b->count = count;
	b->lines = lines;

	b->w = b->h = 0;
	for ( i=0; i<count; i++ ) {
		if (lines[i].x0 > b->w)
			b->w = lines[i].x0;
		if (lines[i].y0 > b->h)
			b->h = lines[i].y0;
		if (lines[i].x1 > b->w)
			b->w = lines[i].x1;
		if (lines[i].y1 > b->h)
			b->h = lines[i].y1;
	}
	b->repaintProc = linesRepaint;
	gtkAddButton( (wControl_p)b );
	b->widget = 0;
	return b;
}


/*
 *****************************************************************************
 *
 * Boxes
 *
 *****************************************************************************
 */

struct wBox_t {
		WOBJ_COMMON
		wBoxType_e boxTyp;
		};

#define B (1)
#define W (2)
#define SETCOLOR( S, N ) \
		if ( lastColor != colors[style][S][N] ) { \
			lastColor = colors[style][S][N]; \
			gdk_gc_set_foreground( win->gc, (lastColor==B)?black:white ); \
		}

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


EXPORT void gtkDrawBox(
		wWin_p win,
		wBoxType_e style,
		wPos_t x,
		wPos_t y,
		wPos_t w,
		wPos_t h )
{
	wPos_t x0, y0, x1, y1;
	char lastColor;
	GdkColor *white;
	GdkColor *black;
	GdkDrawable * window;
	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} } };

if (windowxxx)
	window = win->gtkwin->window;
else
	window = win->widget->window;
	white = gtkGetColor( wDrawColorWhite, TRUE );
	black = gtkGetColor( wDrawColorBlack, TRUE );
	win->gc_linewidth = 0;
	gdk_gc_set_line_attributes( win->gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER );
	gdk_gc_set_function( win->gc, GDK_COPY );
	x0 = x;
	x1 = x+w;
	y0 = y;
	y1 = y+h;
	lastColor = colors[style][0][0];
	gdk_gc_set_foreground( win->gc, (lastColor==B)?black:white );
	gdk_draw_line( window, win->gc, x0, y0, x0, y1 );
	SETCOLOR( 1, 0 );
	gdk_draw_line( window, win->gc, x0+1, y0, x1, y0 );
	SETCOLOR( 2, 0 );
	gdk_draw_line( window, win->gc, x1, y1, x0+1, y1 );
	SETCOLOR( 3, 0 );
	gdk_draw_line( window, win->gc, x1, y1-1, x1, y0+1 );
	if (style < wBoxThickB)
		return;
	x0++; y0++; x1--; y1--;
	SETCOLOR( 0, 1 );
	gdk_draw_line( window, win->gc, x0, y0, x0, y1 );
	SETCOLOR( 1, 1 );
	gdk_draw_line( window, win->gc, x0+1, y0, x1, y0 );
	SETCOLOR( 2, 1 );
	gdk_draw_line( window, win->gc, x1, y1, x0+1, y1 );
	SETCOLOR( 3, 1 );
	gdk_draw_line( window, win->gc, x1, y1-1, x1, y0+1 );
	gdk_gc_set_foreground( win->gc, black );
}


static void boxRepaint( wControl_p b )
{
	wBox_p bb = (wBox_p)(b);
	wWin_p win = bb->parent;

	gtkDrawBox( win, bb->boxTyp, bb->realX, bb->realY, bb->w, bb->h );
}


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)gtkAlloc( parent, B_BOX, bx, by, labelStr, sizeof *b, NULL );
	gtkComputePos( (wControl_p)b );
	b->boxTyp = boxTyp;
	b->w = bw;
	b->h = bh;
	if (parent->gc == NULL) {
		parent->gc = gdk_gc_new( parent->gtkwin->window );
		gdk_gc_copy( parent->gc, parent->gtkwin->style->base_gc[GTK_STATE_NORMAL] );
		parent->gc_linewidth = 0;
		gdk_gc_set_line_attributes( parent->gc, parent->gc_linewidth, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER );
	}
	b->repaintProc = boxRepaint;
	gtkAddButton( (wControl_p)b );
	return b;
}