summaryrefslogtreecommitdiffstats
path: root/ui/gtk.c
blob: c8e3a59e76905edb04f5bf7924b3827eb1f7b88b (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
#include <stdio.h>
#include <gtk/gtk.h>
#include "internal.h"
#include "rccnames.h"

#define TITLE_WIDTH 160
#define TITLE_HEIGHT 10
#define BOX_SPACING 1
#define BOX_BORDER 0
#define FRAME_SPACING 1
#define FRAME_BORDER 0
#define PAGE_SPACING 0

rcc_ui_internal rccUiCreateInternal(rcc_ui_context ctx) {
    return NULL;
}

void rccUiFreeInternal(rcc_ui_context ctx) {
}

rcc_ui_widget rccUiMenuCreateWidget(rcc_ui_menu_context ctx) {
    return NULL;
}

void rccUiMenuFreeWidget(rcc_ui_menu_context ctx) {
}

rcc_ui_id rccUiMenuGet(rcc_ui_menu_context ctx) {
    GtkWidget *menu;
    
    if (!ctx) return (rcc_ui_id)-1;

    if (ctx->type == RCC_UI_MENU_OPTION)
	return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ctx->widget));
    
    menu = gtk_option_menu_get_menu(ctx->widget);
    return g_list_index(GTK_MENU_SHELL(menu)->children, gtk_menu_get_active(GTK_MENU(menu)));
}

int rccUiMenuSet(rcc_ui_menu_context ctx, rcc_ui_id id) {
    if (!ctx) return -1;
    
    switch (ctx->type) {
	case RCC_UI_MENU_OPTION:
	    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ctx->widget),id);
	break;
	default:
	    gtk_option_menu_set_history(ctx->widget, id);
    }

    return 0;
}


static int rccGtkMenuLanguageCB(GtkWidget * w, gpointer item) {
    rccUiRestoreLanguage(((rcc_ui_menu_context)item)->uictx);
}

int rccUiMenuConfigureWidget(rcc_ui_menu_context ctx) {
    unsigned int i;
    
    rcc_context rccctx;
    rcc_ui_context uictx;

    rcc_language_config config;
    rcc_language_ptr *languages;
    rcc_language_id language_id;

    rcc_charset *charsets;
    rcc_engine_ptr *engines;

    rcc_charset_id charset_id;
    rcc_engine_id engine_id;

    GtkWidget *list, *item, *menu;

    if (!ctx) return -1;
    
    uictx = ctx->uictx;
    rccctx = uictx->rccctx;
    
    switch (ctx->type) {
	case RCC_UI_MENU_LANGUAGE:
	    list = gtk_menu_new();

	    languages=rccGetLanguageList(rccctx);
	    for (i=0; languages[i]; i++) {
		item = gtk_menu_item_new_with_label(rccUiGetLanguageName(uictx, languages[i]->sn));
		gtk_signal_connect(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(rccGtkMenuLanguageCB), ctx);
		gtk_menu_append(GTK_MENU(list), item);
		gtk_widget_show(item);
	    }

	    language_id = rccGetSelectedLanguage(rccctx);
	    if (language_id < 0) language_id = 0;

	    if (ctx->widget) menu = ctx->widget;
	    else {
		menu = gtk_option_menu_new();
		ctx->widget = menu;
	    }
	    gtk_option_menu_remove_menu(GTK_OPTION_MENU(menu));
	    gtk_option_menu_set_menu(GTK_OPTION_MENU(menu), list);
	    gtk_option_menu_set_history(GTK_OPTION_MENU(menu), language_id);
	break;
	case RCC_UI_MENU_CHARSET:
	    list = gtk_menu_new();

	    language_id = (rcc_language_id)rccUiMenuGet(uictx->language);
	    charsets = rccGetCharsetList(rccctx, language_id);
	    
	    for (i=0;charsets[i];i++) {
		item = gtk_menu_item_new_with_label(charsets[i]);
		gtk_widget_show(item);
    		gtk_menu_append(GTK_MENU(list), item);
	    }

	    if (ctx->widget) menu = ctx->widget;
	    else {
		menu = gtk_option_menu_new();
		ctx->widget = menu;
	    }
    
	    gtk_option_menu_remove_menu(GTK_OPTION_MENU(menu));
	    gtk_option_menu_set_menu(GTK_OPTION_MENU(menu), list);

	    config = rccGetConfig(rccctx, language_id);
	    charset_id = rccConfigGetSelectedCharset(config, (rcc_class_id)ctx->id);
	    if (charset_id < 0) charset_id = 0;
	    gtk_option_menu_set_history(GTK_OPTION_MENU(menu), charset_id);
	break;
	case RCC_UI_MENU_ENGINE:
	    list = gtk_menu_new();

	    language_id = (rcc_language_id)rccUiMenuGet(uictx->language);
	    engines = rccGetEngineList(rccctx, language_id);
	    for (i=0;engines[i];i++) {
		item = gtk_menu_item_new_with_label(engines[i]->title);
		gtk_widget_show(item);
    		gtk_menu_append(GTK_MENU(list), item);
	    }	

	    if (ctx->widget) menu = ctx->widget;
	    else {
		menu = gtk_option_menu_new();
		ctx->widget = menu;
	    }

	    gtk_option_menu_remove_menu(GTK_OPTION_MENU(menu));
	    gtk_option_menu_set_menu(GTK_OPTION_MENU(menu), list);

	    config = rccGetConfig(rccctx, language_id);
	    engine_id = rccConfigGetCurrentEngine(config);
	    if (engine_id < 0) engine_id = 0;
	    gtk_option_menu_set_history(GTK_OPTION_MENU(menu), engine_id);
		    
	break;
	case RCC_UI_MENU_OPTION:
	    if (!ctx->widget) {
	        item = gtk_check_button_new_with_label(rccUiGetOptionName(uictx, (rcc_option)ctx->id));
    		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(item), rccGetOption(rccctx, (rcc_option)ctx->id));
		ctx->widget = item;
	    }
	break;
    }

    return 0;
}


rcc_ui_box rccUiBoxCreate(rcc_ui_menu_context ctx, const char *title) {
    GtkWidget *hbox, *label;
    hbox = gtk_hbox_new(FALSE, BOX_SPACING);
    gtk_container_border_width(GTK_CONTAINER(hbox), BOX_BORDER);
    if (ctx->type != RCC_UI_MENU_OPTION) {
	label = gtk_label_new(title);
	gtk_widget_set_usize(label, TITLE_WIDTH, TITLE_HEIGHT);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
    }
    gtk_box_pack_start(GTK_BOX(hbox), (GtkWidget*)ctx->widget, TRUE, TRUE, 0);
    return (rcc_ui_box)hbox;
}

rcc_ui_frame rccUiFrameCreate(rcc_ui_frame_context ctx, const char *title) {
    GtkWidget *frame, *box;

    if (!ctx) return NULL;
    
    frame = gtk_frame_new(title?title:"");
    gtk_container_border_width(GTK_CONTAINER(frame), FRAME_BORDER);

    box = gtk_vbox_new(FALSE, FRAME_SPACING);
    gtk_container_add(GTK_CONTAINER(frame), box);
    
    return (rcc_ui_frame)frame;
}

void rccUiFrameFree(rcc_ui_frame_context ctx) {
}


int rccUiFrameAdd(rcc_ui_frame_context ctx, rcc_ui_box box) {
    GtkWidget *vbox;
    
    if ((!ctx)||(!box)) return -1;
    
    vbox = gtk_container_children(GTK_CONTAINER(ctx->frame))->data;
    gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(box), FALSE, FALSE, 0);
    return 0;
}

rcc_ui_page rccUiPageCreate(rcc_ui_context ctx, const char *title) {
    return (rcc_ui_page)gtk_vbox_new(FALSE, PAGE_SPACING);
}

int rccUiPageAdd(rcc_ui_page page, rcc_ui_frame frame) {
    if ((!page)||(!frame)) return -1;
    gtk_box_pack_start(GTK_BOX(page), GTK_WIDGET(frame), FALSE, FALSE, 0);
    return 0;
}