summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/statusbar.c
blob: 3a2fd0d5747ac0e92952b7c17c0d13c479e2b0b3 (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
/** \file statusbar.c
 * Status bar
 */

/* 	XTrkCad - Model Railroad CAD
 *  Copyright (C) 2005 Dave Bullis,
 *                2017 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 <stdlib.h>
#include <string.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"

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

/**
 * Set the message text
 *
 * \param b IN widget
 * \param arg IN new text
 * \return
 */

void wStatusSetValue(
    wStatus_p b,
    const char * arg)
{
    if (b->widget == 0) {
        abort();
    }

    if (gtk_entry_get_max_length(GTK_ENTRY(b->labelWidget))<strlen(arg)) {
        gtk_entry_set_max_length(GTK_ENTRY(b->labelWidget), strlen(arg));
        gtk_entry_set_width_chars(GTK_ENTRY(b->labelWidget), strlen(arg));
    }

    gtk_entry_set_text(GTK_ENTRY(b->labelWidget), wlibConvertInput(arg));
    gtk_widget_queue_draw (GTK_WIDGET(b->labelWidget));
}
/**
 * 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
 */

wStatus_p wStatusCreate(
    wWin_p	parent,
    wPos_t	x,
    wPos_t	y,
    const char 	* labelStr,
    wPos_t	width,
    const char	*message)
{
    wStatus_p b;
    GtkRequisition requisition;
    b = (wStatus_p)wlibAlloc(parent, B_STATUS, x, y, NULL, sizeof *b, NULL);
    wlibComputePos((wControl_p)b);
    b->message = message;
    b->labelWidth = width;
    b->labelWidget = gtk_entry_new();
    gtk_editable_set_editable(GTK_EDITABLE(b->labelWidget), FALSE);
    gtk_entry_set_has_frame(GTK_ENTRY(b->labelWidget), FALSE);
    gtk_widget_set_can_focus(b->labelWidget, FALSE);
    gtk_widget_set_sensitive(b->labelWidget, FALSE);
    GdkColor black = {0, 0x0000, 0x0000, 0x0000};
    gtk_widget_modify_text(b->labelWidget,GTK_STATE_INSENSITIVE,&black);
    gtk_entry_set_text(GTK_ENTRY(b->labelWidget),
                       message?wlibConvertInput(message):"");

    b->widget = gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(b->widget), b->labelWidget);
    wlibControlGetSize((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);
    wlibAddButton((wControl_p)b);

    return b;
}

/**
 * Get the anticipated length of a message field
 *
 * \param testString IN string that should fit into the message box
 * \return expected width of message box
 */

wPos_t
wStatusGetWidth(const char *testString)
{
    GtkWidget *entry;
    GtkRequisition requisition;

    entry = gtk_entry_new();
    g_object_ref_sink(entry);

    gtk_entry_set_has_frame(GTK_ENTRY(entry), FALSE);
    gtk_entry_set_width_chars(GTK_ENTRY(entry), strlen(testString));
    gtk_entry_set_max_length(GTK_ENTRY(entry), strlen(testString));

    gtk_widget_size_request(entry, &requisition);

    gtk_widget_destroy(entry);
    g_object_unref(entry);

    return (requisition.width);
}

/**
 * Get height of message text
 *
 * \param flags IN text properties (large or small size)
 * \return text height
 */

wPos_t wStatusGetHeight(
    long flags)
{
    GtkWidget * temp;

    if (!(flags&COMBOBOX)) {
		temp = gtk_entry_new();	 //To get size of text itself
        gtk_entry_set_has_frame(GTK_ENTRY(temp), FALSE);
    } else {
        temp = gtk_combo_box_text_new();    //to get max size of an object in infoBar
    }
    g_object_ref_sink(temp);

    if (wMessageSetFont(flags))	{
        GtkStyle *style;
        PangoFontDescription *fontDesc;
        int fontSize;
        /* get the current font descriptor */
        style = gtk_widget_get_style(temp);
        fontDesc = 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(temp, fontDesc);
    }

    if (flags&1L) {
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(temp),"Test");
    }

    GtkRequisition temp_requisition;
    gtk_widget_size_request(temp,&temp_requisition);
    //g_object_ref_sink(temp);
    //g_object_unref(temp);
    gtk_widget_destroy(temp);
    return temp_requisition.height;
}

/**
 * Set the width of the widget
 *
 * \param b IN widget
 * \param width IN  new width
 * \return
 */

void wStatusSetWidth(
    wStatus_p b,
    wPos_t width)
{
    b->labelWidth = width;
    gtk_widget_set_size_request(b->widget, width, -1);
}