-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgtk_windef.c
354 lines (304 loc) · 12 KB
/
gtk_windef.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
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
#include "gtk_windef.h"
#include "gtk_findreplace.h"
/* TODO:
* move all indent implementation to gtk_indent.[ch]
*/
/** create application window & initialize values
* and connect callback functions. 'app' contains
* widgets for window, text_view and statusbar.
*/
GtkWidget *create_window (kwinst *app)
{
GtkAccelGroup *mainaccel;
GtkWidget *vbox; /* vbox container */
GtkWidget *menubar; /* menu container */
GtkWidget *scrolled_textview; /* scrolled win w/text_view */
GtkWidget *sbalign; /* alignment for statusbar */
guint ptop; /* padding, top, bot, l, r */
guint pbot;
guint pleft;
guint pright;
gchar *iconfile; /* filename to loading icon */
/* create toplevel window */
if (!(app->window = gtk_window_new (GTK_WINDOW_TOPLEVEL))) {
err_dialog ("create_window() gtk_window_new failure.");
return NULL;
}
// gtk_window_set_position (GTK_WINDOW (app->window), GTK_WIN_POS_CENTER);
gtk_window_set_position (GTK_WINDOW (app->window), GTK_WIN_POS_NONE);
gtk_window_set_default_size (GTK_WINDOW (app->window), app->winwidth, app->winheight);
// gtk_window_move (GTK_WINDOW (app->window), app->winrootx, app->winrooty);
/* create icon filename and set icon */
if ((iconfile = g_strdup_printf ("%s/%s", app->imgdir, ICON))) {
GdkPixbuf *pixbuf = create_pixbuf_from_file (iconfile);
if (pixbuf) {
gtk_window_set_icon(GTK_WINDOW(app->window), pixbuf);
g_object_unref (pixbuf);
}
g_free (iconfile);
}
/* create & attach accelerator group */
mainaccel = gtk_accel_group_new ();
gtk_window_add_accel_group (GTK_WINDOW (app->window), mainaccel);
/* create vbox to hold menu, toolbar, scrolled_windows, textview & statusbar
* and add contaier to main window
*/
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (app->window), vbox);
// gtk_box_set_spacing (GTK_BOX (vbox), 0);
/* create menubar and menus to add */
menubar = create_menubar (app, mainaccel);
app->menubar = menubar;
gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, 0);
gtk_widget_show (app->menubar);
/* create toolbar
* GTK_TOOLBAR_ICONS, GTK_TOOLBAR_TEXT, GTK_TOOLBAR_BOTH, GTK_TOOLBAR_BOTH_HORIZ
*/
app->toolbar = create_toolbar (mainaccel, app);
gtk_box_pack_start(GTK_BOX(vbox), app->toolbar, FALSE, FALSE, 0);
gtk_widget_show (app->toolbar);
/* create vbox to display infobar */
app->ibarvbox = gtk_vbox_new (FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), app->ibarvbox, FALSE, FALSE, 0);
gtk_widget_show (app->ibarvbox);
/* create scrolled_window and textview */
scrolled_textview = create_textview_scrolledwindow (app);
gtk_box_pack_start (GTK_BOX (vbox), scrolled_textview, TRUE, TRUE, 0);
gtk_widget_show (scrolled_textview);
/* create/pack statusbar at end within gtk_alignment */
sbalign = gtk_alignment_new (0, .5, 1, 1);
gtk_alignment_get_padding (GTK_ALIGNMENT (sbalign), &ptop, &pbot, &pleft, &pright);
gtk_alignment_set_padding (GTK_ALIGNMENT (sbalign), ptop, pbot + 2, pleft + 5, pright);
app->statusbar = gtk_statusbar_new ();
status_set_default (app);
gtk_container_add (GTK_CONTAINER (sbalign), app->statusbar);
gtk_box_pack_end (GTK_BOX (vbox), sbalign, FALSE, FALSE, 0);
gtk_widget_show (app->statusbar);
gtk_widget_show (vbox);
/* connect all signals */
g_signal_connect (G_OBJECT (app->window), "delete-event", /* window del */
G_CALLBACK (on_window_delete_event), app);
g_signal_connect (G_OBJECT (app->window), "destroy", /* window dest */
G_CALLBACK (on_window_destroy), NULL);
/* general */
g_signal_connect (G_OBJECT (app->view), "key_press_event",
G_CALLBACK (on_keypress), app);
g_signal_connect (app->buffer, "changed",
G_CALLBACK (on_buffer_changed), app);
g_signal_connect (app->buffer, "mark_set",
G_CALLBACK (on_mark_set), app);
g_signal_connect (G_OBJECT (app->view), "toggle-overwrite",
G_CALLBACK (on_insmode), app);
/* set window title */
gtkwrite_window_set_title (NULL, app);
gtk_widget_show (app->window);
/* showall widgets */
gtk_widget_show_all (app->window);
/* TODO: load saved settings */
gtk_widget_set_visible (app->toolbar, app->showtoolbar);
gtk_widget_set_sensitive (app->tbappearMi, app->showtoolbar);
return app->window;
}
/*
* window callbacks
*/
/** on_window_delete_event called first when WM_CLOSE button clicked.
* if return is FALSE, on_window_destroy is called, if TRUE, control
* is returned to gtk_main and you are responsible for gtk_main_quit.
*/
gboolean on_window_delete_event (GtkWidget *widget, GdkEvent *event,
kwinst *app)
{
if (!app->winszsaved) { /* user chose save current size in settings */
/* get window size */
gtk_window_get_size (GTK_WINDOW (app->window), &(app->winwidth),
&(app->winheight));
}
/* TODO add gboolean app->on_window_delete_event, and return it */
/* check changed, prompt yes/no */
ibar_handle_quit (app);
return TRUE;
// buffer_handle_quit (app);
// return FALSE;
/* TODO - return app->on_window_delete_event; */
if (widget) {}
if (event) {}
}
/** on_window_destroy called after on_window_delete_event processed
* if the return from on_window_delete_event is FALSE.
*/
void on_window_destroy (GtkWidget *widget, kwinst *app)
{
// g_print ("on_window_destroy\n");
gtk_main_quit();
if (widget) {}
if (app) {}
}
void on_insmode (GtkWidget *widget, kwinst *app)
{
if (app->overwrite) {
app->overwrite = FALSE;
#ifdef DEBUG
g_print (" toggle-overwrite : overwrite -> insert mode\n");
#endif
}
else {
app->overwrite = TRUE;
#ifdef DEBUG
g_print (" toggle-overwrite : insert -> overwrite mode\n");
#endif
}
status_set_default (app); /* update the status bar */
if (widget) {}
if (app) {}
}
/** on_buffer_changed fires before on_mark_set with all changes.
* (even undo) and fires before gtk_text_buffer_get_modified()
* reflects change.
*/
void on_buffer_changed (GtkTextBuffer *buffer,
kwinst *app)
{
/* Wed Aug 30 2017 19:18:14 CDT, moved to on_mark_set because
* on_buffer_changed fires before gtk_text_buffer_get_modified()
* is updated, leaving window title unchanged after first char entry.
*/
// if (!app->modified) /* app->modified set in set_title */
// gtkwrite_window_set_title (NULL, app);
/* TODO fix with central gtk_text_buffer_get_modified()
* but you want to make sure set_modified is FALSE after
* all saves, open w/o edits, etc..
*/
/* set app->eolchg if buffer changed so conversion runs on eol change */
if (app->modified && app->eol != app->oeol)
app->eolchg = TRUE;
if (buffer) {}
}
/** on cursor position change (insert mark_set), update line, lines, col.
* Note: on_mark_set fires after on_buffer_changed, so it will more
* accurately capture buffer modification state.
*/
void on_mark_set (GtkTextBuffer *buffer, GtkTextIter *iter,
GtkTextMark *mark, kwinst *app)
{
/* update current line, total lines and current column */
app->line = gtk_text_iter_get_line (iter);
app->lines = gtk_text_buffer_get_line_count (buffer);
app->col = gtk_text_iter_get_line_offset (iter);
/* update window title */
if (!app->modified)
gtkwrite_window_set_title (NULL, app);
/* update status bar */
status_set_default (app);
if (mark) {}
}
gboolean on_keypress (GtkWidget *widget, GdkEventKey *event, kwinst *app)
{
if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (app->view), event)) {
return TRUE;
}
/* handle control + shift key combinations */
if (event->type == GDK_KEY_PRESS &&
event->state & GDK_CONTROL_MASK &&
event->state & GDK_SHIFT_MASK) {
switch (event->keyval) {
case GDK_KEY_Left:
if (app->ctrl_shift_right_fix) {
gboolean rtn = buffer_select_to_prev_char (app);
/* if no prev ctrl+shift keypress or last was to left
* push another left keypress on stack.
*/
if (!app->bindex || bstack_last (app) == LEFT)
bstack_push (app, LEFT); /* record LEFT selection */
else
bstack_pop (app); /* pop left from stack */
return rtn;
}
break;
case GDK_KEY_Right:
if (app->ctrl_shift_right_fix) {
gboolean rtn = buffer_select_to_next_char (app);
/* if no prev ctrl+shift keypress or last was to right
* push another right keypress on stack.
*/
if (!app->bindex || bstack_last (app) == RIGHT)
bstack_push (app, RIGHT); /* record RIGHT selection */
else
bstack_pop (app); /* pop right from stack */
return rtn;
}
break;
}
return FALSE; /* return - only process ctrl + shift events */
}
/* handle control key combinations */
/*
if (event->type == GDK_KEY_PRESS &&
event->state & GDK_CONTROL_MASK) {
switch (event->keyval) {
case GDK_KEY_Right:
g_print ("key pressed: %s\n", "ctrl + Right->Arrow");
break;
}
return FALSE;
}
*/
/* handle shift key combinations */
if (event->type == GDK_KEY_PRESS &&
event->state & GDK_SHIFT_MASK) {
switch (event->keyval) {
case GDK_KEY_Left:
/* with ctrl_shift_right_fix, reduce selection by 1 at end
* if selection exists and started to right and user releases
* ctrl key and arrows backwards.
*/
if (app->ctrl_shift_right_fix)
return (buffer_reduce_selection (app));
break;
// case GDK_KEY_Right:
// g_print ("key pressed: %s\n", "shift + Right->Arrow");
// break;
}
return FALSE;
}
/* handle normal keypress events */
if (event->type == GDK_KEY_PRESS) {
switch (event->keyval) {
case GDK_KEY_BackSpace:
if (app->smartbs) /* smart_backspace in filebuf.c */
return smart_backspace (app);
break; /* or just return FALSE; */
case GDK_KEY_Tab:; /* catch tab, replace with softtab spaces */
return smart_tab (app);
case GDK_KEY_Return:
// #ifndef HAVESOURCEVIEW /* using my auto-indent and EOL handling for SV & non-SV */
if (app->indentauto)
return buffer_indent_auto (app);
else
return buffer_insert_eol (app);
// #endif
break;
case GDK_KEY_KP_Enter:
// #ifndef HAVESOURCEVIEW
if (app->indentauto)
return buffer_indent_auto (app);
else
return buffer_insert_eol (app);
// #endif
break;
case GDK_KEY_Home:
#ifndef HAVESOURCEVIEW
if (app->smarthe)
return ((app->kphome = smart_home (app)));
#endif
break;
}
}
app->kphome = FALSE; /* reset kphome - return above protects needed TRUE */
/* if any other key and bindex or bstack[0], clear stack */
if (app->bindex || app->bstack[0])
bstack_clear (app);
return FALSE;
if (widget) {}
if (app) {}
}