summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/control.c
blob: 07d921005ed4bb97702f5c21146a74654db2b9ab (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
/** \file control.c
 * Control Utilities
 */
/* 
 * Copyright 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.
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>

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

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

#include "gtkint.h"

#define  GTKCONTROLHILITEWIDTH (4)

/**
 * Cause the control <b> to be displayed or hidden.
 * Used to hide control (such as a list) while it is being updated.
 *
 * \param b IN Control
 * \param show IN TRUE for visible
 */

void wControlShow(
    wControl_p b,
    wBool_t show)
{
    if (b->type == B_LINES) {
        wlibLineShow((wLine_p)b, show);
        return;
    }

    if (b->widget == NULL) {
        abort();
    }

    if (show) {
        gtk_widget_show(b->widget);

        if (b->label) {
            gtk_widget_show(b->label);
        }
    } else {
        gtk_widget_hide(b->widget);

        if (b->label) {
            gtk_widget_hide(b->label);
        }
    }
}

/**
 * Cause the control <b> to be marked active or inactive.
 * Inactive controls donot respond to actions.
 *
 * \param b IN Control
 * \param active IN TRUE for active
 */

void wControlActive(
    wControl_p b,
    int active)
{
    if (b->widget == NULL) {
        abort();
    }

    gtk_widget_set_sensitive(GTK_WIDGET(b->widget), active);
}

/**
 * Returns the width of <label>.
 * This is used for computing window layout.
 * Typically the width to the longest label is computed and used as
 * the X-position for <controls>.
 *
 * \param label IN label
 * \returns width of label including some space
*/

wPos_t wLabelWidth(
    const char * label)
{
    GtkWidget * widget;
    GtkRequisition requisition;
    widget = gtk_label_new(wlibConvertInput(label));
    gtk_widget_size_request(widget, &requisition);
    g_object_ref_sink (widget);
    gtk_widget_destroy(widget);
    g_object_unref(widget);
    return requisition.width+8;
}

/**
 * Get width of a control
 *
 * \param b IN Control
 * \returns width
 */

wPos_t wControlGetWidth(
    wControl_p b)
{
    return b->w;
}

/**
 * Get height of a control
 *
 * \param b IN Control
 * \returns height
 */

wPos_t wControlGetHeight(
    wControl_p b)
{
    return b->h;
}

/**
 * Get x position of a control
 *
 * \param b IN Control
 * \returns position
 */

wPos_t wControlGetPosX(
    wControl_p b)		/* Control */
{
    return b->realX;
}

/**
 * Get y position of a control
 *
 * \param b IN Control
 * \returns position
 */

wPos_t wControlGetPosY(
    wControl_p b)		/* Control */
{
    return b->realY - BORDERSIZE - ((b->parent->option&F_MENUBAR)?b->parent->menu_height:0);
}

/**
 * Set the fixed position of a control within its parent window
 *
 * \param b IN control
 * \param x IN x position
 * \param y IN y position
 */

void wControlSetPos(
    wControl_p b,
    wPos_t x,
    wPos_t y)
{
    b->realX = x;
    b->realY = y + BORDERSIZE + ((b->parent->option&F_MENUBAR)?b->parent->menu_height:0);

    if (b->widget) {
        gtk_fixed_move(GTK_FIXED(b->parent->widget), b->widget, b->realX, b->realY);
    }

    if (b->label) {
    	GtkRequisition requisition, reqwidget;
    	gtk_widget_size_request(b->label, &requisition);
    	if (b->widget)
    	   	gtk_widget_size_request(b->widget, &reqwidget);
    	else
    	  	reqwidget.height = requisition.height;
        gtk_fixed_move(GTK_FIXED(b->parent->widget), b->label, b->realX-b->labelW,
                       b->realY+(reqwidget.height/2 - requisition.height/2));
    }
}

/**
 * Set the label for a control
 *
 * \param b IN control
 * \param labelStr IN the new label
 */

void wControlSetLabel(
    wControl_p b,
    const char * labelStr)
{
    GtkRequisition requisition,reqwidget;

    if (b->label) {
        gtk_label_set_text(GTK_LABEL(b->label), wlibConvertInput(labelStr));
        gtk_widget_size_request(b->label, &requisition);
        if (b->widget)
        	gtk_widget_size_request(b->widget, &reqwidget);
        else
        	reqwidget.height = requisition.height;
        b->labelW = requisition.width+8;
        gtk_fixed_move(GTK_FIXED(b->parent->widget), b->label, b->realX-b->labelW,
                       b->realY+(reqwidget.height/2 - requisition.height/2));
    } else {
        b->labelW = wlibAddLabel(b, labelStr);
    }
}

/**
 * Set the context ie. additional user data for a control
 *
 * \param b IN control
 * \param context IN user date
 */

void wControlSetContext(
    wControl_p b,
    void * context)
{
    b->data = context;
}

/**
 * Not implemented
 *
 * \param b IN
 */

void wControlSetFocus(
    wControl_p b)
{
}

wBool_t wControlExpose (
		 GtkWidget * widget,
		 GdkEventExpose * event,
		 wControl_p b
		)
{
	GdkWindow * win = gtk_widget_get_window(b->widget);
	cairo_t * cr = NULL;
	if (win) {
		cr = gdk_cairo_create(win);
	} else return TRUE;

#ifdef CURSOR_SURFACE
	if (b && b->cursor_surface.surface && b->cursor_surface.show) {
		cairo_set_source_surface(cr,b->cursor_surface.surface,event->area.x, event->area.y);
		cairo_set_operator(cr,CAIRO_OPERATOR_OVER);
		cairo_rectangle(cr,event->area.x, event->area.y,
				event->area.width, event->area.height);
		cairo_fill(cr);
	}
#endif

	if (b->outline) {
		cairo_set_source_rgb(cr, 0.23, 0.37, 0.80);
		cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
		cairo_set_line_width(cr, GTKCONTROLHILITEWIDTH);
		cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
		cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
		cairo_rectangle(cr,event->area.x+2, event->area.y+2,
						event->area.width-4, event->area.height-4);
		cairo_stroke(cr);
	}


	cairo_destroy(cr);


    return FALSE;
}

/**
 * Draw a rectangle around a control
 * \param b IN the control
 * \param hilite IN unused
 * \returns
 *
 *
 */
void wControlHilite(
    wControl_p b,
    wBool_t hilite)
{
    cairo_t *cr;
    int off = GTKCONTROLHILITEWIDTH/2+1;

    if (b->widget == NULL) {
        return;
    }

    if (! gtk_widget_get_visible(b->widget)) {
        return;
    }

    if (! gtk_widget_get_visible(b->parent->widget)) {
        return;
    }

    b->outline = hilite;

    if (b->widget)
    	gtk_widget_queue_draw(b->widget);
}