-
Notifications
You must be signed in to change notification settings - Fork 2
/
gtk_goto.c
225 lines (187 loc) · 8.01 KB
/
gtk_goto.c
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
#include "gtk_goto.h"
/* globals to avoid storage in mainwin_t */
static GtkWidget *gotowin;
static GtkWidget *spinbtn;
static GtkTextMark *new_pos;
/* goto keypress handler */
static gboolean on_goto_keypress (GtkWidget *widget, GdkEventKey *event,
gpointer data)
{
switch (event->keyval)
{
case GDK_KEY_Escape:
goto_btnclose (widget, data);
// return TRUE; /* return TRUE - no further processing */
break;
case GDK_KP_Enter: /* toggle focus to set value in spinbtn */
gtk_widget_child_focus (widget, GTK_DIR_RIGHT);
gtk_widget_child_focus (widget, GTK_DIR_LEFT);
goto_btnfind (widget, data);
return TRUE;
case GDK_Return:
gtk_widget_child_focus (widget, GTK_DIR_RIGHT);
gtk_widget_child_focus (widget, GTK_DIR_LEFT);
goto_btnfind (widget, data);
goto_btnclose (widget, data);
return TRUE;
}
return FALSE;
}
GtkWidget *create_goto_dlg (gpointer data)
{
mainwin_t *app = data;
kinst_t *inst = app->einst[app->focused]->inst;
GtkWidget *vbox; /* vbox container */
GtkWidget *adjhbox;
GtkWidget *adjvbox;
GtkWidget *hbox;
GtkWidget *image; /* nix image for now - use cairo */
GtkWidget *btnfind;
GtkWidget *btnclose; /* TODO: confirm reuse is no problem */
GtkWidget *label;
GtkWidget *vscale;
GtkTextIter iter;
GtkTextMark *imark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER(inst->buf));
gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER(inst->buf), &iter, imark);
gint line = gtk_text_iter_get_line (&iter);
gint last = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER(inst->buf));
/* initialize persistent variables */
new_pos = NULL;
/* value, lower, upper, step_increment, page_increment, page_size
* (as with statusbar, the value is line + 1)
*/
GtkObject *adj = gtk_adjustment_new (line + 1, 0.0, last, 1.0, 1.0, 0.0);
gchar *str = "\nKeyboard Shortcuts:\n<b>Enter</b> - goto/close dlg.\n<b>KP_Enter</b> - multi-goto.";
if (last == 1) {
// show_info_bar_markup_ok ("insufficient number of lines in buffer "
// "(nowhere to go to).",
// GTK_MESSAGE_INFO, app);
err_dialog ("Error:\n\ninsufficient lines in buffer.");
return NULL;
}
/* create toplevel window */
if (!(gotowin = gtk_window_new (GTK_WINDOW_TOPLEVEL))) {
// show_info_bar_markup_ok ("create_find_dlg() gtk_window_new failure.",
// GTK_MESSAGE_ERROR, app);
err_dialog ("create_find_dlg() gtk_window_new failure.");
return NULL;
}
gtk_window_set_position (GTK_WINDOW (gotowin), GTK_WIN_POS_CENTER);
gtk_window_set_default_size (GTK_WINDOW (gotowin), 185, 185);
gtk_window_set_title (GTK_WINDOW (gotowin), "Goto Line");
gtk_window_set_modal (GTK_WINDOW(gotowin), TRUE);
gtk_window_set_transient_for (GTK_WINDOW(gotowin),
GTK_WINDOW(app->window));
gtk_container_set_border_width (GTK_CONTAINER (gotowin), 5);
g_signal_connect (gotowin, "destroy",
G_CALLBACK (goto_btnclose), app);
/* main vbox container
* spacing provided on frames as containers */
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (gotowin), vbox);
gtk_widget_show (vbox);
adjhbox = gtk_hbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (vbox), adjhbox);
adjvbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (adjhbox), adjvbox);
/* TODO: add textbox or label before (or after) image */
image = gtk_image_new_from_stock (GTK_STOCK_INDEX, GTK_ICON_SIZE_DND);
gtk_box_pack_start (GTK_BOX (adjvbox), image, FALSE, FALSE, 0);
gtk_widget_show (image);
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL(label), str);
gtk_label_set_justify (GTK_LABEL(label), GTK_JUSTIFY_LEFT);
gtk_box_pack_start (GTK_BOX (adjvbox), label, FALSE, FALSE, 0);
gtk_widget_show (label);
spinbtn = gtk_spin_button_new (GTK_ADJUSTMENT(adj), 1.0, 0);
// gtk_box_pack_start (GTK_BOX (adjhbox), app->spinbtn, TRUE, TRUE, 0);
gtk_box_pack_end (GTK_BOX (adjvbox), spinbtn, TRUE, TRUE, 0);
gtk_widget_show (spinbtn);
gtk_widget_show (adjvbox);
gtk_widget_show (adjhbox);
vscale = gtk_vscale_new (GTK_ADJUSTMENT (adj));
gtk_range_set_update_policy (GTK_RANGE (vscale),
GTK_UPDATE_CONTINUOUS);
gtk_scale_set_digits (GTK_SCALE (vscale), 0);
gtk_scale_set_value_pos (GTK_SCALE (vscale), GTK_POS_TOP);
gtk_scale_set_draw_value (GTK_SCALE (vscale), TRUE);
gtk_box_pack_start (GTK_BOX (adjhbox), vscale, TRUE, TRUE, 0);
gtk_widget_show (vscale);
/* hbox button spacing */
hbox = gtk_hbox_new (FALSE, 0);
gtk_box_set_spacing (GTK_BOX (hbox), 5);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
/* button sizes 80 x 24 */
btnfind = gtk_button_new_with_mnemonic ("_Goto");
gtk_widget_set_size_request (btnfind, 80, 24);
// gtk_widget_set_sensitive (btnfind, (line != app->line));
gtk_widget_set_sensitive (btnfind, (line !=
gtk_text_iter_get_line (&iter)));
gtk_widget_show (btnfind);
btnclose = gtk_button_new_with_mnemonic ("_Close");
gtk_widget_set_size_request (btnclose, 80, 24);
gtk_box_pack_end (GTK_BOX (hbox), btnclose, FALSE, FALSE, 0);
gtk_box_pack_end (GTK_BOX (hbox), btnfind, FALSE, FALSE, 0); // pack on rt-side.
gtk_widget_show (btnclose);
gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);
gtk_widget_show (gotowin);
// g_signal_connect (app->spinbtn, "preedit-changed",
// G_CALLBACK (goto_spinbtn_preedit), app);
g_signal_connect (spinbtn, "value-changed",
G_CALLBACK (goto_spinbtn_changed), btnfind);
g_signal_connect (btnfind, "clicked",
G_CALLBACK (goto_btnfind), data);
g_signal_connect (btnclose, "clicked",
G_CALLBACK (goto_btnclose), data);
g_signal_connect (gotowin, "key_press_event",
G_CALLBACK (on_goto_keypress), data);
return (gotowin);
}
// void goto_spinbtn_preedit (GtkWidget *widget, gchar *txt, kwinst *app)
// {
// g_print ("preedit text: %s\n", txt);
// gtk_widget_set_sensitive (app->btnfind, TRUE);
// if (widget) {}
// }
void goto_spinbtn_changed (GtkWidget *widget, gpointer data)
{
gtk_widget_set_sensitive (data, TRUE);
// gtk_widget_set_sensitive (app->btnfind, TRUE);
if (widget) {}
}
void goto_btnfind (GtkWidget *widget, gpointer data)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkWidget *view = einst->view;
GtkTextIter liter;
gint line = 0;
line = gtk_spin_button_get_value_as_int (
GTK_SPIN_BUTTON(spinbtn)) - 1; /* subtract 1 */
#ifdef DEBUG
g_print ("goto line: %d\n", line);
#endif
gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER(inst->buf), &liter, line);
new_pos = gtk_text_buffer_create_mark (GTK_TEXT_BUFFER(inst->buf),
"new_line", &liter, FALSE);
// gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (app->view),
// app->new_pos);
gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (view),
new_pos, 0.0, TRUE, 0.95, 0.8);
/* must place cursor after scroll or statusbar will reflect scroll */
gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER(inst->buf), &liter);
if (widget) {}
if (app) {}
}
void goto_btnclose (GtkWidget *widget, gpointer data)
{
mainwin_t *app = data;
kinst_t *inst = app->einst[app->focused]->inst;
if (new_pos)
gtk_text_buffer_delete_mark (GTK_TEXT_BUFFER(inst->buf), new_pos);
new_pos = NULL;
gtk_widget_destroy (gotowin);
if (widget) {}
}