Updated RELEASE_NOTES.
[mp-5.x.git] / mpv_gtk.c
blobb7736fe790b18af71c1e4772075aa3ba6f741c2d
1 /*
3 Minimum Profit - Programmer Text Editor
5 GTK driver.
7 Copyright (C) 1991-2009 Angel Ortega <angel@triptico.com>
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
27 #include "config.h"
29 #ifdef CONFOPT_GTK
31 #include <stdio.h>
32 #include <wchar.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <unistd.h>
37 #include <sys/stat.h>
38 #include <gtk/gtk.h>
39 #include <gdk/gdkkeysyms.h>
41 #include "mpdm.h"
42 #include "mpsl.h"
44 #include "mp.h"
46 #include "mp.xpm"
48 /** data **/
50 /* global data */
52 static GtkWidget * window = NULL;
53 static GtkWidget * file_tabs = NULL;
54 static GtkWidget * area = NULL;
55 static GtkWidget * scrollbar = NULL;
56 static GtkWidget * status = NULL;
57 static GtkWidget * menu_bar = NULL;
58 static GdkGC * gc = NULL;
59 static GtkIMContext * im = NULL;
60 static GdkPixmap * pixmap = NULL;
62 /* character read from the keyboard */
63 static wchar_t im_char[2];
65 /* font information */
66 static int font_width = 0;
67 static int font_height = 0;
68 static PangoFontDescription * font = NULL;
70 /* the attributes */
71 static GdkColor * inks = NULL;
72 static GdkColor * papers = NULL;
73 static int * underlines = NULL;
75 /* true if the selection is ours */
76 static int got_selection = 0;
78 /* hack for active waiting for the selection */
79 static int wait_for_selection = 0;
81 /* global modal status */
82 /* code for the 'normal' attribute */
83 static int normal_attr = 0;
85 /* mp.drv.form() controls */
87 static GtkWidget ** form_widgets = NULL;
88 static mpdm_t form_args = NULL;
89 static mpdm_t form_values = NULL;
91 /* mouse down flag */
92 static int mouse_down = 0;
94 /* timer function */
95 static mpdm_t timer_func = NULL;
97 /* maximize wanted? */
98 static int maximize = 0;
100 /** code **/
102 /** support functions **/
104 #define LL(m) (m)
106 static char * wcs_to_utf8(const wchar_t * wptr)
107 /* converts a wcs to utf-8 */
109 char * ptr;
110 gsize i, o;
112 i = wcslen(wptr);
114 /* do the conversion */
115 ptr = g_convert((const gchar *) wptr, (i + 1) * sizeof(wchar_t),
116 "UTF-8", "WCHAR_T", NULL, &o, NULL);
118 return ptr;
122 static wchar_t * utf8_to_wcs(const char * ptr)
123 /* converts utf-8 to wcs */
125 wchar_t * wptr;
126 gsize i, o;
128 i = strlen(ptr);
130 /* do the conversion */
131 wptr = (wchar_t *) g_convert((gchar *) ptr, i + 1,
132 "WCHAR_T", "UTF-8", NULL, &o, NULL);
134 return wptr;
138 static char * localize(char * msg)
140 mpdm_t v;
142 v = mpdm_gettext(MPDM_MBS(msg));
143 return wcs_to_utf8(v->data);
147 static void update_window_size(void)
148 /* updates the viewport size in characters */
150 PangoLayout * pa;
151 int tx, ty;
152 mpdm_t v;
154 /* get font metrics */
155 pa = gtk_widget_create_pango_layout(area, "m");
156 pango_layout_set_font_description(pa, font);
157 pango_layout_get_pixel_size(pa, &font_width, &font_height);
158 g_object_unref(pa);
160 /* calculate the size in chars */
161 tx = (area->allocation.width / font_width);
162 ty = (area->allocation.height / font_height) + 1;
164 /* store the 'window' size */
165 v = mpdm_hget_s(mp, L"window");
166 mpdm_hset_s(v, L"tx", MPDM_I(tx));
167 mpdm_hset_s(v, L"ty", MPDM_I(ty));
169 /* rebuild the pixmap for the double buffer */
170 if (pixmap != NULL)
171 gdk_pixmap_unref(pixmap);
173 pixmap = gdk_pixmap_new(area->window,
174 area->allocation.width, font_height, -1);
178 static void build_fonts(void)
179 /* builds the fonts */
181 char tmp[128];
182 int font_size = 12;
183 const char * font_face = "Mono";
184 mpdm_t c;
186 if(font != NULL)
187 pango_font_description_free(font);
189 /* get current configuration */
190 if ((c = mpdm_hget_s(mp, L"config")) != NULL) {
191 mpdm_t v;
193 if ((v = mpdm_hget_s(c, L"font_size")) != NULL)
194 font_size = mpdm_ival(v);
195 else
196 mpdm_hset_s(c, L"font_size", MPDM_I(font_size));
198 if ((v = mpdm_hget_s(c, L"font_face")) != NULL) {
199 v = MPDM_2MBS(v->data);
200 font_face = v->data;
202 else
203 mpdm_hset_s(c, L"font_face", MPDM_MBS(font_face));
206 snprintf(tmp, sizeof(tmp) - 1, "%s %d", font_face, font_size);
207 tmp[sizeof(tmp) - 1] = '\0';
209 font = pango_font_description_from_string(tmp);
210 update_window_size();
214 static void build_color(GdkColor * gdkcolor, int rgb)
215 /* builds a color */
217 gdkcolor->pixel = 0;
218 gdkcolor->blue = (rgb & 0x000000ff) << 8;
219 gdkcolor->green = (rgb & 0x0000ff00);
220 gdkcolor->red = (rgb & 0x00ff0000) >> 8;
221 gdk_colormap_alloc_color(gdk_colormap_get_system(), gdkcolor, FALSE, TRUE);
225 static void build_colors(void)
226 /* builds the colors */
228 mpdm_t colors;
229 mpdm_t l;
230 mpdm_t c;
231 int n, s;
233 /* gets the color definitions and attribute names */
234 colors = mpdm_hget_s(mp, L"colors");
235 l = mpdm_keys(colors);
236 s = mpdm_size(l);
238 /* redim the structures */
239 inks = realloc(inks, sizeof(GdkColor) * s);
240 papers = realloc(papers, sizeof(GdkColor) * s);
241 underlines = realloc(underlines, sizeof(int) * s);
243 /* loop the colors */
244 for (n = 0; n < s && (c = mpdm_aget(l, n)) != NULL; n++) {
245 mpdm_t d = mpdm_hget(colors, c);
246 mpdm_t v = mpdm_hget_s(d, L"gui");
248 /* store the 'normal' attribute */
249 if (wcscmp(mpdm_string(c), L"normal") == 0)
250 normal_attr = n;
252 /* store the attr */
253 mpdm_hset_s(d, L"attr", MPDM_I(n));
255 build_color(&inks[n], mpdm_ival(mpdm_aget(v, 0)));
256 build_color(&papers[n], mpdm_ival(mpdm_aget(v, 1)));
258 /* flags */
259 v = mpdm_hget_s(d, L"flags");
260 underlines[n] = mpdm_seek_s(v, L"underline", 1) != -1 ? 1 : 0;
262 if (mpdm_seek_s(v, L"reverse", 1) != -1) {
263 GdkColor t;
265 t = inks[n];
266 inks[n] = papers[n];
267 papers[n] = t;
273 /** menu functions **/
275 static void redraw(void);
277 static void menu_item_callback(mpdm_t action)
278 /* menu click callback */
280 mp_process_action(action);
281 redraw();
283 if (mp_exit_requested)
284 gtk_main_quit();
288 static void build_submenu(GtkWidget * menu, mpdm_t labels)
289 /* build a submenu */
291 int n;
292 GtkWidget * menu_item;
294 for (n = 0; n < mpdm_size(labels); n++) {
295 /* get the action */
296 mpdm_t v = mpdm_aget(labels, n);
298 /* if the action is a separator... */
299 if (*((wchar_t *)v->data) == L'-')
300 menu_item = gtk_menu_item_new();
301 else {
302 char * ptr;
303 mpdm_t d = mp_menu_label(v);
305 ptr = wcs_to_utf8(mpdm_string(d));
306 menu_item = gtk_menu_item_new_with_label(ptr);
307 g_free(ptr);
310 gtk_menu_append(GTK_MENU(menu), menu_item);
311 g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
312 G_CALLBACK(menu_item_callback), v);
313 gtk_widget_show(menu_item);
318 static void build_menu(void)
319 /* builds the menu */
321 static mpdm_t prev_menu = NULL;
322 int n;
323 mpdm_t m;
325 /* gets the current menu */
326 if ((m = mpdm_hget_s(mp, L"menu")) == NULL)
327 return;
329 /* if it's the same, do nothing */
330 if (mpdm_cmp(m, prev_menu) == 0)
331 return;
333 /* create a new menu */
334 menu_bar = gtk_menu_bar_new();
336 for (n = 0; n < mpdm_size(m); n++) {
337 char * ptr;
338 mpdm_t mi;
339 mpdm_t v;
340 GtkWidget * menu;
341 GtkWidget * menu_item;
342 int i;
344 /* get the label and the items */
345 mi = mpdm_aget(m, n);
346 v = mpdm_aget(mi, 0);
348 if ((ptr = wcs_to_utf8(mpdm_string(mpdm_gettext(v)))) == NULL)
349 continue;
351 /* change the & by _ for the mnemonic */
352 for (i = 0; ptr[i]; i++)
353 if (ptr[i] == '&')
354 ptr[i] = '_';
356 /* add the menu and the label */
357 menu = gtk_menu_new();
358 menu_item = gtk_menu_item_new_with_mnemonic(ptr);
359 g_free(ptr);
361 gtk_widget_show(menu_item);
362 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu);
363 gtk_menu_bar_append(GTK_MENU_BAR(menu_bar), menu_item);
365 /* now loop the items */
366 build_submenu(menu, mpdm_aget(mi, 1));
371 /** main area drawing functions **/
373 static void switch_page(GtkNotebook * notebook, GtkNotebookPage * page,
374 gint pg_num, gpointer data)
375 /* 'switch_page' handler (filetabs) */
377 /* sets the active one */
378 mpdm_hset_s(mp, L"active_i", MPDM_I(pg_num));
380 gtk_widget_grab_focus(area);
381 redraw();
385 static void draw_filetabs(void)
386 /* draws the filetabs */
388 static mpdm_t last = NULL;
389 mpdm_t names;
390 int n;
392 names = mp_get_doc_names();
394 /* disconnect redraw signal to avoid infinite loops */
395 g_signal_handlers_disconnect_by_func(G_OBJECT(file_tabs),
396 G_CALLBACK(switch_page), NULL);
398 /* is the list different from the previous one? */
399 if (mpdm_cmp(names, last) != 0) {
401 /* delete the current tabs */
402 for (n = 0; n < mpdm_size(last); n++)
403 gtk_notebook_remove_page(
404 GTK_NOTEBOOK(file_tabs), 0);
406 /* create the new ones */
407 for (n = 0; n < mpdm_size(names); n++) {
408 GtkWidget * p;
409 GtkWidget * f;
410 char * ptr;
411 mpdm_t v = mpdm_aget(names, n);
413 if ((ptr = wcs_to_utf8(v->data)) != NULL) {
414 p = gtk_label_new(ptr);
415 gtk_widget_show(p);
417 f = gtk_frame_new(NULL);
418 gtk_widget_show(f);
420 gtk_notebook_append_page(
421 GTK_NOTEBOOK(file_tabs), f, p);
423 g_free(ptr);
427 /* store for the next time */
428 mpdm_unref(last);
429 last = mpdm_ref(names);
432 /* set the active one */
433 gtk_notebook_set_page(GTK_NOTEBOOK(file_tabs),
434 mpdm_ival(mpdm_hget_s(mp, L"active_i")));
436 /* reconnect signal */
437 g_signal_connect(G_OBJECT(file_tabs), "switch_page",
438 G_CALLBACK(switch_page), NULL);
440 gtk_widget_grab_focus(area);
444 static void draw_status(void)
445 /* draws the status line */
447 mpdm_t t;
448 char * ptr;
450 /* call mp.status_line() */
451 t = mp_build_status_line();
453 if (t != NULL && (ptr = wcs_to_utf8(t->data)) != NULL) {
454 gtk_label_set_text(GTK_LABEL(status), ptr);
455 g_free(ptr);
460 static gint scroll_event(GtkWidget * widget, GdkEventScroll * event)
461 /* 'scroll_event' handler (mouse wheel) */
463 wchar_t * ptr = NULL;
465 switch (event->direction) {
466 case GDK_SCROLL_UP: ptr = L"mouse-wheel-up"; break;
467 case GDK_SCROLL_DOWN: ptr = L"mouse-wheel-down"; break;
468 case GDK_SCROLL_LEFT: ptr = L"mouse-wheel-left"; break;
469 case GDK_SCROLL_RIGHT: ptr = L"mouse-wheel-right"; break;
472 if (ptr != NULL)
473 mp_process_event(MPDM_S(ptr));
475 redraw();
477 return 0;
481 static void value_changed(GtkAdjustment * adj, gpointer * data)
482 /* 'value_changed' handler (scrollbar) */
484 int i = (int) adj->value;
485 mpdm_t doc;
486 mpdm_t txt;
487 int y;
489 /* get current y position */
490 doc = mp_active();
491 txt = mpdm_hget_s(doc, L"txt");
492 y = mpdm_ival(mpdm_hget_s(txt, L"y"));
494 /* if it's different, set and redraw */
495 if (y != i) {
496 mp_set_y(doc, i);
497 mpdm_hset_s(txt, L"vy", MPDM_I(i));
498 redraw();
503 static void draw_scrollbar(void)
504 /* updates the scrollbar */
506 GtkAdjustment * adjustment;
507 mpdm_t d;
508 mpdm_t v;
509 int pos, size, max;
511 /* gets the active document */
512 if ((d = mp_active()) == NULL)
513 return;
515 /* get the coordinates */
516 v = mpdm_hget_s(d, L"txt");
517 pos = mpdm_ival(mpdm_hget_s(v, L"vy"));
518 max = mpdm_size(mpdm_hget_s(v, L"lines"));
520 v = mpdm_hget_s(mp, L"window");
521 size = mpdm_ival(mpdm_hget_s(v, L"ty"));
523 adjustment = gtk_range_get_adjustment(GTK_RANGE(scrollbar));
525 /* if((int)adjustment->upper==max &&
526 (int)adjustment->page_size==size &&
527 (int)adjustment->page_increment==size &&
528 (int)adjustment->value==pos)
529 return;
531 /* disconnect to avoid infinite loops */
532 g_signal_handlers_disconnect_by_func(G_OBJECT(adjustment),
533 G_CALLBACK(value_changed), NULL);
535 adjustment->step_increment = (gfloat)1;
536 adjustment->upper = (gfloat)max;
537 adjustment->page_size = (gfloat)size;
538 adjustment->page_increment = (gfloat)size;
539 adjustment->value = (gfloat)pos;
541 gtk_range_set_adjustment(GTK_RANGE(scrollbar), adjustment);
543 gtk_adjustment_changed(adjustment);
544 gtk_adjustment_value_changed(adjustment);
546 /* reattach again */
547 g_signal_connect(
548 G_OBJECT(gtk_range_get_adjustment(GTK_RANGE(scrollbar))),
549 "value_changed", G_CALLBACK(value_changed), NULL);
553 static void gtk_drv_paint(mpdm_t doc, int optimize)
554 /* GTK document draw function */
556 GdkRectangle gr;
557 mpdm_t d = NULL;
558 int n, m;
560 if (maximize)
561 gtk_window_maximize(GTK_WINDOW(window));
563 /* no gc? create it */
564 if (gc == NULL)
565 gc = gdk_gc_new(area->window);
567 if (font == NULL)
568 build_fonts();
570 if ((d = mp_draw(doc, optimize)) == NULL)
571 return;
573 gr.x = 0;
574 gr.y = 0;
575 gr.width = area->allocation.width;
576 gr.height = font_height;
578 for (n = 0; n < mpdm_size(d); n++) {
579 PangoLayout * pl;
580 PangoAttrList * pal;
581 mpdm_t l = mpdm_aget(d, n);
582 char * str = NULL;
583 int u, p;
585 if (l == NULL)
586 continue;
588 /* create the pango stuff */
589 pl = gtk_widget_create_pango_layout(area, NULL);
590 pango_layout_set_font_description(pl, font);
591 pal = pango_attr_list_new();
593 for (m = u = p = 0; m < mpdm_size(l); m++, u = p) {
594 PangoAttribute * pa;
595 int attr;
596 mpdm_t s;
597 char * ptr;
599 /* get the attribute and the string */
600 attr = mpdm_ival(mpdm_aget(l, m++));
601 s = mpdm_aget(l, m);
603 /* convert the string to utf8 */
604 ptr = wcs_to_utf8(s->data);
606 /* add to the full line */
607 str = mpdm_poke(str, &p, ptr, strlen(ptr), 1);
609 g_free(ptr);
611 /* create the background if it's
612 different from the default */
613 if (papers[attr].red != papers[normal_attr].red ||
614 papers[attr].green != papers[normal_attr].green ||
615 papers[attr].blue != papers[normal_attr].blue) {
616 pa = pango_attr_background_new(
617 papers[attr].red, papers[attr].green,
618 papers[attr].blue);
620 pa->start_index = u;
621 pa->end_index = p;
623 pango_attr_list_insert(pal, pa);
626 /* underline? */
627 if (underlines[attr]) {
628 pa = pango_attr_underline_new(TRUE);
630 pa->start_index = u;
631 pa->end_index = p;
633 pango_attr_list_insert(pal, pa);
636 /* foreground color */
637 pa = pango_attr_foreground_new(inks[attr].red,
638 inks[attr].green, inks[attr].blue);
640 pa->start_index = u;
641 pa->end_index = p;
643 pango_attr_list_insert(pal, pa);
646 /* store the attributes */
647 pango_layout_set_attributes(pl, pal);
648 pango_attr_list_unref(pal);
650 /* store and free the text */
651 pango_layout_set_text(pl, str, p);
652 free(str);
654 /* draw the background */
655 gdk_gc_set_foreground(gc, &papers[normal_attr]);
656 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0,
657 gr.width, gr.height);
659 /* draw the text */
660 gtk_paint_layout(area->style, pixmap,
661 GTK_STATE_NORMAL, TRUE,
662 &gr, area, "", 2, 0, pl);
664 /* dump the pixmap */
665 gdk_draw_pixmap(area->window, gc, pixmap,
666 0, 0, 0, n * font_height, gr.width, gr.height);
668 g_object_unref(pl);
671 draw_filetabs();
672 draw_scrollbar();
673 draw_status();
677 static void redraw(void)
679 if (mpdm_size(mpdm_hget_s(mp, L"docs")))
680 gtk_drv_paint(mp_active(), 0);
684 static gint delete_event(GtkWidget * w, GdkEvent * e, gpointer data)
685 /* 'delete_event' handler */
687 mp_process_event(MPDM_LS(L"close-window"));
689 return mp_exit_requested ? FALSE : TRUE;
693 static void destroy(GtkWidget * w, gpointer data)
694 /* 'destroy' handler */
696 gtk_main_quit();
700 static gint key_release_event(GtkWidget * widget, GdkEventKey * event, gpointer data)
701 /* 'key_release_event' handler */
703 if (mp_keypress_throttle(0))
704 gtk_drv_paint(mp_active(), 0);
706 return 0;
710 static gint key_press_event(GtkWidget * widget, GdkEventKey * event, gpointer data)
711 /* 'key_press_event' handler */
713 wchar_t * ptr = NULL;
715 gtk_im_context_filter_keypress(im, event);
717 /* set mp.shift_pressed */
718 if (event->state & (GDK_SHIFT_MASK))
719 mpdm_hset_s(mp, L"shift_pressed", MPDM_I(1));
721 /* reserve alt for menu mnemonics */
722 if (GDK_MOD1_MASK & event->state)
723 return(0);
725 if (event->state & (GDK_CONTROL_MASK)) {
726 switch (event->keyval) {
727 case GDK_Up: ptr = L"ctrl-cursor-up"; break;
728 case GDK_Down: ptr = L"ctrl-cursor-down"; break;
729 case GDK_Left: ptr = L"ctrl-cursor-left"; break;
730 case GDK_Right: ptr = L"ctrl-cursor-right"; break;
731 case GDK_Prior: ptr = L"ctrl-page-up"; break;
732 case GDK_Next: ptr = L"ctrl-page-down"; break;
733 case GDK_Home: ptr = L"ctrl-home"; break;
734 case GDK_End: ptr = L"ctrl-end"; break;
735 case GDK_space: ptr = L"ctrl-space"; break;
736 case GDK_KP_Add: ptr = L"ctrl-kp-plus"; break;
737 case GDK_KP_Subtract: ptr = L"ctrl-kp-minus"; break;
738 case GDK_KP_Multiply: ptr = L"ctrl-kp-multiply"; break;
739 case GDK_KP_Divide: ptr = L"ctrl-kp-divide"; break;
740 case GDK_F1: ptr = L"ctrl-f1"; break;
741 case GDK_F2: ptr = L"ctrl-f2"; break;
742 case GDK_F3: ptr = L"ctrl-f3"; break;
743 case GDK_F4: ptr = L"ctrl-f4"; break;
744 case GDK_F5: ptr = L"ctrl-f5"; break;
745 case GDK_F6: ptr = L"ctrl-f6"; break;
746 case GDK_F7: ptr = L"ctrl-f7"; break;
747 case GDK_F8: ptr = L"ctrl-f8"; break;
748 case GDK_F9: ptr = L"ctrl-f9"; break;
749 case GDK_F10: ptr = L"ctrl-f10"; break;
750 case GDK_F11: ptr = L"ctrl-f11"; break;
751 case GDK_F12: ptr = L"ctrl-f12"; break;
752 case GDK_KP_Enter:
753 case GDK_Return: ptr = L"ctrl-enter"; break;
756 if (ptr == NULL) {
757 char c = event->keyval & 0xdf;
759 switch (c) {
760 case 'A': ptr = L"ctrl-a"; break;
761 case 'B': ptr = L"ctrl-b"; break;
762 case 'C': ptr = L"ctrl-c"; break;
763 case 'D': ptr = L"ctrl-d"; break;
764 case 'E': ptr = L"ctrl-e"; break;
765 case 'F': ptr = L"ctrl-f"; break;
766 case 'G': ptr = L"ctrl-g"; break;
767 case 'H': ptr = L"ctrl-h"; break;
768 case 'I': ptr = L"ctrl-i"; break;
769 case 'J': ptr = L"ctrl-j"; break;
770 case 'K': ptr = L"ctrl-k"; break;
771 case 'L': ptr = L"ctrl-l"; break;
772 case 'M': ptr = L"ctrl-m"; break;
773 case 'N': ptr = L"ctrl-n"; break;
774 case 'O': ptr = L"ctrl-o"; break;
775 case 'P': ptr = L"ctrl-p"; break;
776 case 'Q': ptr = L"ctrl-q"; break;
777 case 'R': ptr = L"ctrl-r"; break;
778 case 'S': ptr = L"ctrl-s"; break;
779 case 'T': ptr = L"ctrl-t"; break;
780 case 'U': ptr = L"ctrl-u"; break;
781 case 'V': ptr = L"ctrl-v"; break;
782 case 'W': ptr = L"ctrl-w"; break;
783 case 'X': ptr = L"ctrl-x"; break;
784 case 'Y': ptr = L"ctrl-y"; break;
785 case 'Z': ptr = L"ctrl-z"; break;
789 else {
790 switch (event->keyval) {
791 case GDK_Up: ptr = L"cursor-up"; break;
792 case GDK_Down: ptr = L"cursor-down"; break;
793 case GDK_Left: ptr = L"cursor-left"; break;
794 case GDK_Right: ptr = L"cursor-right"; break;
795 case GDK_Prior: ptr = L"page-up"; break;
796 case GDK_Next: ptr = L"page-down"; break;
797 case GDK_Home: ptr = L"home"; break;
798 case GDK_End: ptr = L"end"; break;
799 case GDK_space: ptr = L"space"; break;
800 case GDK_KP_Add: ptr = L"kp-plus"; break;
801 case GDK_KP_Subtract: ptr = L"kp-minus"; break;
802 case GDK_KP_Multiply: ptr = L"kp-multiply"; break;
803 case GDK_KP_Divide: ptr = L"kp-divide"; break;
804 case GDK_F1: ptr = L"f1"; break;
805 case GDK_F2: ptr = L"f2"; break;
806 case GDK_F3: ptr = L"f3"; break;
807 case GDK_F4: ptr = L"f4"; break;
808 case GDK_F5: ptr = L"f5"; break;
809 case GDK_F6: ptr = L"f6"; break;
810 case GDK_F7: ptr = L"f7"; break;
811 case GDK_F8: ptr = L"f8"; break;
812 case GDK_F9: ptr = L"f9"; break;
813 case GDK_F10: ptr = L"f10"; break;
814 case GDK_F11: ptr = L"f11"; break;
815 case GDK_F12: ptr = L"f12"; break;
816 case GDK_Insert: ptr = L"insert"; break;
817 case GDK_BackSpace: ptr = L"backspace"; break;
818 case GDK_Delete: ptr = L"delete"; break;
819 case GDK_KP_Enter:
820 case GDK_Return: ptr = L"enter"; break;
821 case GDK_Tab: ptr = L"tab"; break;
822 case GDK_Escape: ptr = L"escape"; break;
826 /* if there is a pending char in im_char, use it */
827 if (ptr == NULL && im_char[0] != L'\0')
828 ptr = im_char;
830 /* finally process */
831 if (ptr != NULL)
832 mp_process_event(MPDM_S(ptr));
834 /* delete the pending char */
835 im_char[0] = L'\0';
837 if (mp_exit_requested)
838 gtk_main_quit();
840 if (mp_keypress_throttle(1))
841 gtk_drv_paint(mp_active(), 1);
843 return 0;
847 static gint button_press_event(GtkWidget * widget, GdkEventButton * event, gpointer data)
848 /* 'button_press_event' handler (mouse buttons) */
850 int x, y;
851 wchar_t * ptr = NULL;
853 mouse_down = 1;
855 /* mouse instant positioning */
856 x = ((int)event->x) / font_width;
857 y = ((int)event->y) / font_height;
859 mpdm_hset_s(mp, L"mouse_x", MPDM_I(x));
860 mpdm_hset_s(mp, L"mouse_y", MPDM_I(y));
862 switch (event->button) {
863 case 1: ptr = L"mouse-left-button"; break;
864 case 2: ptr = L"mouse-middle-button"; break;
865 case 3: ptr = L"mouse-right-button"; break;
866 case 4: ptr = L"mouse-wheel-up"; break;
867 case 5: ptr = L"mouse-wheel-down"; break;
870 if (ptr != NULL)
871 mp_process_event(MPDM_S(ptr));
873 redraw();
875 return 0;
879 static gint button_release_event(GtkWidget * widget, GdkEventButton * event, gpointer data)
880 /* 'button_release_event' handle (mouse buttons) */
882 mouse_down = 0;
884 return TRUE;
888 static gint motion_notify_event(GtkWidget *widget, GdkEventMotion * event, gpointer data)
889 /* 'motion_notify_event' handler (mouse movement) */
891 static int ox = 0;
892 static int oy = 0;
894 if (mouse_down) {
895 int x, y;
897 /* mouse dragging */
898 x = ((int)event->x) / font_width;
899 y = ((int)event->y) / font_height;
901 if (ox != x && oy != y) {
902 mpdm_hset_s(mp, L"mouse_to_x", MPDM_I(x));
903 mpdm_hset_s(mp, L"mouse_to_y", MPDM_I(y));
905 mp_process_event(MPDM_LS(L"mouse-drag"));
906 gtk_drv_paint(mp_active(), 1);
910 return TRUE;
914 /** clipboard functions **/
916 static void commit(GtkIMContext * i, char * str, gpointer u)
917 /* 'commit' handler */
919 wchar_t * wstr;
921 wstr = (wchar_t *) g_convert(str, -1,
922 "WCHAR_T", "UTF-8", NULL, NULL, NULL);
924 im_char[0] = *wstr;
925 im_char[1] = L'\0';
927 g_free(wstr);
931 static void realize(GtkWidget * widget)
932 /* 'realize' handler */
934 im = gtk_im_multicontext_new();
935 g_signal_connect(im, "commit", G_CALLBACK(commit), NULL);
936 gtk_im_context_set_client_window(im, widget->window);
940 static gint expose_event(GtkWidget * widget, GdkEventExpose * event)
941 /* 'expose_event' handler */
943 redraw();
945 return FALSE;
949 static gint configure_event(GtkWidget * widget, GdkEventConfigure * event)
950 /* 'configure_event' handler */
952 static GdkEventConfigure o;
954 if (memcmp(&o, event, sizeof(o)) == 0)
955 return TRUE;
957 memcpy(&o, event, sizeof(o));
959 update_window_size();
960 redraw();
962 return TRUE;
966 static gint selection_clear_event(GtkWidget * widget,
967 GdkEventSelection * event, gpointer data)
968 /* 'selection_clear_event' handler */
970 got_selection = 0;
972 return TRUE;
976 static void selection_get(GtkWidget * widget,
977 GtkSelectionData * sel, guint info, guint tm)
978 /* 'selection_get' handler */
980 mpdm_t d;
981 unsigned char * ptr;
982 int s;
984 if (!got_selection)
985 return;
987 /* gets the clipboard and joins */
988 d = mpdm_hget_s(mp, L"clipboard");
990 if (mpdm_size(d) == 0)
991 return;
993 d = mpdm_join(MPDM_LS(L"\n"), d);
995 /* convert to current locale */
996 ptr = (unsigned char *) mpdm_wcstombs(d->data, &s);
998 /* pastes into primary selection */
999 gtk_selection_data_set(sel, GDK_SELECTION_TYPE_STRING, 8, ptr, (gsize) s);
1001 free(ptr);
1005 static void selection_received(GtkWidget * widget,
1006 GtkSelectionData * sel, gpointer data)
1007 /* 'selection_received' handler */
1009 mpdm_t d;
1011 if (sel->data != NULL) {
1012 /* get selection */
1013 wchar_t * wptr = utf8_to_wcs((char *)sel->data);
1014 d = MPDM_S(wptr);
1015 g_free(wptr);
1017 /* split and set as the clipboard */
1018 mpdm_hset_s(mp, L"clipboard", mpdm_split(MPDM_LS(L"\n"), d));
1019 mpdm_hset_s(mp, L"clipboard_vertical", MPDM_I(0));
1021 /* wait no more for the selection */
1022 wait_for_selection = 0;
1024 else
1025 wait_for_selection = -1;
1029 static mpdm_t gtk_drv_clip_to_sys(mpdm_t a)
1030 /* driver-dependent mp to system clipboard */
1032 got_selection = gtk_selection_owner_set(area,
1033 GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME);
1035 return NULL;
1039 static mpdm_t gtk_drv_sys_to_clip(mpdm_t a)
1040 /* driver-dependent system to mp clipboard */
1042 if (!got_selection) {
1043 int n;
1044 char * formats[] = { "UTF8_STRING", "STRING", NULL };
1046 for(n = 0; formats[n] != NULL; n++) {
1048 /* triggers a selection capture */
1049 if (gtk_selection_convert(area, GDK_SELECTION_PRIMARY,
1050 gdk_atom_intern(formats[n], FALSE),
1051 GDK_CURRENT_TIME)) {
1053 /* processes the pending events
1054 (i.e., the 'selection_received' handler) */
1055 wait_for_selection = 1;
1057 while(wait_for_selection == 1)
1058 gtk_main_iteration();
1060 if (!wait_for_selection)
1061 break;
1066 return NULL;
1070 /** interface functions **/
1072 static void clicked_ok(GtkWidget * widget, gpointer data)
1073 /* 'clicked_on' signal handler (for gtk_drv_form) */
1075 int n;
1077 for (n = 0; n < mpdm_size(form_args); n++) {
1078 GtkWidget * widget = form_widgets[n];
1079 mpdm_t w = mpdm_aget(form_args, n);
1080 wchar_t * wptr = mpdm_string(mpdm_hget_s(w, L"type"));
1081 mpdm_t v = NULL;
1083 /* if there is already a value there, if was
1084 previously set from a callback */
1085 if (mpdm_aget(form_values, n) != NULL)
1086 continue;
1088 if (wcscmp(wptr, L"text") == 0 ||
1089 wcscmp(wptr, L"password") == 0) {
1090 char * ptr;
1091 GtkWidget * gw = widget;
1092 mpdm_t h;
1094 if (wcscmp(wptr, L"text") == 0)
1095 gw = GTK_COMBO(widget)->entry;
1097 if ((ptr = gtk_editable_get_chars(
1098 GTK_EDITABLE(gw), 0, -1)) != NULL &&
1099 (wptr = utf8_to_wcs(ptr)) != NULL) {
1100 v = MPDM_S(wptr);
1101 g_free(wptr);
1102 g_free(ptr);
1105 /* if it has history, fill it */
1106 if ((h = mpdm_hget_s(w, L"history")) != NULL &&
1107 v != NULL && mpdm_cmp(v, MPDM_LS(L"")) != 0) {
1108 h = mp_get_history(h);
1110 if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0)
1111 mpdm_push(h, v);
1114 else
1115 if (wcscmp(wptr, L"checkbox") == 0) {
1116 v = MPDM_I(gtk_toggle_button_get_active(
1117 GTK_TOGGLE_BUTTON(widget)));
1119 else
1120 if (wcscmp(wptr, L"list") == 0) {
1121 GtkWidget * list = gtk_bin_get_child(GTK_BIN(widget));
1122 GtkTreeSelection * selection =
1123 gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
1124 GList * selected = gtk_tree_selection_get_selected_rows(selection, NULL);
1125 GtkTreePath * path = selected->data;
1127 v = MPDM_I(gtk_tree_path_get_indices(path)[0]);
1128 gtk_tree_path_free(path);
1129 g_list_free(selected);
1133 mpdm_aset(form_values, v, n);
1138 static gint timer_callback(gpointer data)
1140 mpdm_exec(timer_func, NULL);
1141 redraw();
1143 return TRUE;
1147 static void build_form_data(mpdm_t widget_list)
1148 /* builds the necessary information for a list of widgets */
1150 mpdm_unref(form_args);
1151 form_args = mpdm_ref(widget_list);
1153 mpdm_unref(form_values);
1154 form_values = widget_list == NULL ? NULL :
1155 mpdm_ref(MPDM_A(mpdm_size(form_args)));
1157 /* resize the widget array */
1158 form_widgets = (GtkWidget **) realloc(form_widgets,
1159 mpdm_size(form_args) * sizeof(GtkWidget *));
1163 /** dialog functions **/
1165 #define DIALOG_BUTTON(l,f) do { GtkWidget * btn; \
1166 ptr = localize(l); btn = gtk_button_new_with_label(ptr); \
1167 g_signal_connect_swapped(G_OBJECT(btn), "clicked", \
1168 G_CALLBACK(f), G_OBJECT(dlg)); \
1169 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dlg)->action_area), \
1170 btn, TRUE, TRUE, 0); \
1171 g_free(ptr); \
1172 } while (0);
1174 static mpdm_t gtk_drv_alert(mpdm_t a)
1175 /* alert driver function */
1177 wchar_t * wptr;
1178 gchar * ptr;
1179 GtkWidget * dlg;
1181 build_form_data(NULL);
1183 /* 1# arg: prompt */
1184 wptr = mpdm_string(mpdm_aget(a, 0));
1186 if ((ptr = wcs_to_utf8(wptr)) == NULL)
1187 return NULL;
1189 dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL,
1190 GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, ptr);
1191 gtk_window_set_title(GTK_WINDOW(dlg), "mp " VERSION);
1192 g_free(ptr);
1194 gtk_dialog_run(GTK_DIALOG(dlg));
1195 gtk_widget_destroy(dlg);
1197 return NULL;
1201 static mpdm_t gtk_drv_confirm(mpdm_t a)
1202 /* confirm driver function */
1204 wchar_t * wptr;
1205 char * ptr;
1206 GtkWidget * dlg;
1207 gint response;
1209 build_form_data(NULL);
1211 /* 1# arg: prompt */
1212 wptr = mpdm_string(mpdm_aget(a, 0));
1214 if ((ptr = wcs_to_utf8(wptr)) == NULL)
1215 return NULL;
1217 dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL,
1218 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, ptr);
1219 gtk_window_set_title(GTK_WINDOW(dlg), "mp " VERSION);
1220 g_free(ptr);
1222 gtk_dialog_add_button(GTK_DIALOG(dlg), GTK_STOCK_YES, 1);
1223 gtk_dialog_add_button(GTK_DIALOG(dlg), GTK_STOCK_NO, 2);
1224 gtk_dialog_add_button(GTK_DIALOG(dlg), GTK_STOCK_CANCEL, 0);
1226 response = gtk_dialog_run(GTK_DIALOG(dlg));
1227 gtk_widget_destroy(dlg);
1229 if (response == GTK_RESPONSE_DELETE_EVENT)
1230 response = 0;
1232 return MPDM_I(response);
1236 static mpdm_t gtk_drv_form(mpdm_t a)
1237 /* 'form' driver function */
1239 GtkWidget * dlg;
1240 GtkWidget * table;
1241 GtkWidget * content_area;
1242 int n;
1243 mpdm_t ret = NULL;
1245 /* first argument: list of widgets */
1246 build_form_data(mpdm_aget(a, 0));
1248 dlg = gtk_dialog_new_with_buttons("mp " VERSION, GTK_WINDOW(window),
1249 GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
1250 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1251 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
1252 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);
1253 gtk_container_set_border_width(GTK_CONTAINER(dlg), 5);
1255 content_area = GTK_DIALOG(dlg)->vbox;
1256 gtk_box_set_spacing(GTK_BOX(content_area), 2);
1258 table = gtk_table_new(mpdm_size(a), 2, FALSE);
1259 gtk_container_set_border_width(GTK_CONTAINER(table), 5);
1260 gtk_table_set_col_spacings(GTK_TABLE(table), 12);
1261 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
1263 for (n = 0; n < mpdm_size(form_args); n++) {
1264 mpdm_t w = mpdm_aget(form_args, n);
1265 GtkWidget * widget = NULL;
1266 wchar_t * type;
1267 char * ptr;
1268 mpdm_t t;
1269 int col = 0;
1271 type = mpdm_string(mpdm_hget_s(w, L"type"));
1273 if ((t = mpdm_hget_s(w, L"label")) != NULL) {
1274 GtkWidget * label;
1275 wchar_t * wptr;
1277 if ((wptr = mpdm_string(mpdm_gettext(t))) != NULL &&
1278 (ptr = wcs_to_utf8(wptr)) != NULL) {
1279 label = gtk_label_new(ptr);
1280 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
1282 gtk_table_attach_defaults(GTK_TABLE(table),
1283 label, 0, wcscmp(type, L"label") == 0 ? 2 : 1,
1284 n, n + 1);
1286 g_free(ptr);
1288 col++;
1292 t = mpdm_hget_s(w, L"value");
1294 if (wcscmp(type, L"text") == 0) {
1295 GList * combo_items = NULL;
1296 mpdm_t h;
1297 wchar_t * wptr;
1299 widget = gtk_combo_new();
1300 gtk_widget_set_size_request(widget, 300, -1);
1301 gtk_combo_set_use_arrows_always(GTK_COMBO(widget), TRUE);
1302 gtk_combo_set_case_sensitive(GTK_COMBO(widget), TRUE);
1303 gtk_entry_set_activates_default(GTK_ENTRY(GTK_COMBO(widget)->entry), TRUE);
1305 if ((h = mpdm_hget_s(w, L"history")) != NULL) {
1306 int i;
1308 /* has history; fill it */
1309 h = mp_get_history(h);
1311 for (i = 0; i < mpdm_size(h); i++) {
1312 wptr = mpdm_string(mpdm_aget(h, i));
1313 ptr = wcs_to_utf8(wptr);
1315 combo_items = g_list_prepend(combo_items, ptr);
1319 if (t != NULL) {
1320 wptr = mpdm_string(t);
1321 ptr = wcs_to_utf8(wptr);
1323 combo_items = g_list_prepend(combo_items, ptr);
1326 gtk_combo_set_popdown_strings(GTK_COMBO(widget), combo_items);
1327 g_list_free(combo_items);
1329 else
1330 if (wcscmp(type, L"password") == 0) {
1331 widget = gtk_entry_new();
1332 gtk_widget_set_size_request(widget, 300, -1);
1333 gtk_entry_set_visibility(GTK_ENTRY(widget), FALSE);
1335 else
1336 if (wcscmp(type, L"checkbox") == 0) {
1337 widget = gtk_check_button_new();
1339 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget),
1340 mpdm_ival(t) ? TRUE : FALSE);
1342 else
1343 if (wcscmp(type, L"list") == 0) {
1344 GtkWidget * list;
1345 GtkListStore * list_store;
1346 GtkCellRenderer * renderer;
1347 GtkTreeViewColumn * column;
1348 GtkTreePath * path;
1349 mpdm_t l;
1350 gint i;
1352 if ((i = 450 / mpdm_size(form_args)) < 100)
1353 i = 100;
1355 widget = gtk_scrolled_window_new(NULL, NULL);
1356 gtk_widget_set_size_request(widget, 500, i);
1357 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(widget),
1358 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1359 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(widget),
1360 GTK_SHADOW_IN);
1362 list_store = gtk_list_store_new(1, G_TYPE_STRING);
1363 list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store));
1364 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
1365 renderer = gtk_cell_renderer_text_new();
1366 column = gtk_tree_view_column_new_with_attributes("", renderer,
1367 "text", 0, NULL);
1368 gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
1369 gtk_container_add(GTK_CONTAINER(widget), list);
1371 l = mpdm_hget_s(w, L"list");
1373 for (i = 0; i < mpdm_size(l); i++) {
1374 wchar_t * wptr = mpdm_string(mpdm_aget(l, i));
1376 if ((ptr = wcs_to_utf8(wptr)) != NULL) {
1377 GtkTreeIter iter;
1378 gtk_list_store_append(list_store, &iter);
1379 gtk_list_store_set(list_store, &iter, 0, ptr, -1);
1380 g_free(ptr);
1384 /* initial position */
1385 i = mpdm_ival(t);
1387 path = gtk_tree_path_new_from_indices(i, -1);
1388 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list), path, NULL, FALSE);
1389 gtk_tree_path_free(path);
1391 g_signal_connect_swapped(G_OBJECT(list), "row-activated",
1392 G_CALLBACK(gtk_window_activate_default), dlg);
1395 if (widget != NULL) {
1396 form_widgets[n] = widget;
1397 gtk_table_attach_defaults(GTK_TABLE(table),
1398 widget, col, 2, n, n + 1);
1402 gtk_widget_show_all(table);
1404 gtk_box_pack_start(GTK_BOX(content_area), table, TRUE, TRUE, 0);
1406 if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
1407 clicked_ok(NULL, NULL);
1408 ret = form_values;
1410 gtk_widget_destroy(dlg);
1412 return ret;
1416 static mpdm_t run_filechooser(mpdm_t a, gboolean save)
1417 /* openfile driver function */
1419 GtkWidget * dlg;
1420 wchar_t * wptr;
1421 char * ptr;
1422 mpdm_t ret = NULL;
1423 gint response;
1425 /* 1# arg: prompt */
1426 wptr = mpdm_string(mpdm_aget(a, 0));
1428 if ((ptr = wcs_to_utf8(wptr)) == NULL)
1429 return(NULL);
1431 if (!save) {
1432 dlg = gtk_file_chooser_dialog_new(ptr, GTK_WINDOW(window),
1433 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1434 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
1436 else {
1437 dlg = gtk_file_chooser_dialog_new(ptr, GTK_WINDOW(window),
1438 GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_STOCK_CANCEL,
1439 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
1440 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dlg), TRUE);
1442 g_free(ptr);
1444 build_form_data(NULL);
1446 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dlg), TRUE);
1447 response = gtk_dialog_run(GTK_DIALOG(dlg));
1449 if (response == GTK_RESPONSE_OK) {
1450 gchar * filename;
1451 gchar * utf8name;
1452 wchar_t *wfilename;
1454 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
1455 utf8name = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
1456 g_free(filename);
1457 wfilename = utf8_to_wcs(utf8name);
1458 g_free(utf8name);
1459 ret = MPDM_S(wfilename);
1460 g_free(wfilename);
1462 gtk_widget_destroy(dlg);
1464 return ret;
1468 static mpdm_t gtk_drv_openfile(mpdm_t a)
1469 /* openfile driver function */
1471 return run_filechooser(a, FALSE);
1475 static mpdm_t gtk_drv_savefile(mpdm_t a)
1476 /* savefile driver function */
1478 return run_filechooser(a, TRUE);
1482 static mpdm_t gtk_drv_update_ui(mpdm_t a)
1484 build_fonts();
1485 build_colors();
1486 build_menu();
1488 redraw();
1490 return NULL;
1494 static mpdm_t gtk_drv_timer(mpdm_t a)
1496 static guint prev = 0;
1497 int msecs = mpdm_ival(mpdm_aget(a, 0));
1498 mpdm_t func = mpdm_aget(a, 1);
1499 mpdm_t r;
1501 /* previously defined one? remove */
1502 if (timer_func != NULL)
1503 gtk_timeout_remove(prev);
1505 /* if msecs and func are set, program timer */
1506 if (msecs > 0 && func != NULL)
1507 prev = gtk_timeout_add(msecs, timer_callback, NULL);
1509 r = mpdm_unref(timer_func);
1510 timer_func = mpdm_ref(func);
1512 return r;
1516 static mpdm_t gtk_drv_busy(mpdm_t a)
1518 int onoff = mpdm_ival(mpdm_aget(a, 0));
1520 gdk_window_set_cursor(window->window,
1521 gdk_cursor_new(onoff ? GDK_WATCH : GDK_LEFT_PTR));
1523 while (gtk_events_pending())
1524 gtk_main_iteration();
1526 return NULL;
1530 static mpdm_t gtk_drv_main_loop(mpdm_t a)
1531 /* main loop */
1533 if (!mp_exit_requested) {
1534 gtk_drv_paint(mp_active(), 0);
1536 gtk_main();
1539 return NULL;
1543 static mpdm_t gtk_drv_shutdown(mpdm_t a)
1544 /* shutdown */
1546 mpdm_t v;
1548 if ((v = mpdm_hget_s(mp, L"exit_message")) != NULL) {
1549 mpdm_write_wcs(stdout, mpdm_string(v));
1550 printf("\n");
1553 return NULL;
1557 static void register_functions(void)
1559 mpdm_t drv;
1561 drv = mpdm_hget_s(mp, L"drv");
1562 mpdm_hset_s(drv, L"main_loop", MPDM_X(gtk_drv_main_loop));
1563 mpdm_hset_s(drv, L"shutdown", MPDM_X(gtk_drv_shutdown));
1565 mpdm_hset_s(drv, L"clip_to_sys", MPDM_X(gtk_drv_clip_to_sys));
1566 mpdm_hset_s(drv, L"sys_to_clip", MPDM_X(gtk_drv_sys_to_clip));
1567 mpdm_hset_s(drv, L"update_ui", MPDM_X(gtk_drv_update_ui));
1568 mpdm_hset_s(drv, L"timer", MPDM_X(gtk_drv_timer));
1569 mpdm_hset_s(drv, L"busy", MPDM_X(gtk_drv_busy));
1571 mpdm_hset_s(drv, L"alert", MPDM_X(gtk_drv_alert));
1572 mpdm_hset_s(drv, L"confirm", MPDM_X(gtk_drv_confirm));
1573 mpdm_hset_s(drv, L"openfile", MPDM_X(gtk_drv_openfile));
1574 mpdm_hset_s(drv, L"savefile", MPDM_X(gtk_drv_savefile));
1575 mpdm_hset_s(drv, L"form", MPDM_X(gtk_drv_form));
1579 static mpdm_t gtk_drv_startup(mpdm_t a)
1580 /* driver initialization */
1582 GtkWidget * vbox;
1583 GtkWidget * hbox;
1584 GdkPixmap * pixmap;
1585 GdkPixmap * mask;
1586 GdkScreen *screen;
1587 mpdm_t v;
1588 int w, h;
1590 register_functions();
1592 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1594 /* get real screen and pick a usable size for the main area */
1595 screen = gtk_window_get_screen(GTK_WINDOW(window));
1596 w = (gdk_screen_get_width(screen) * 3) / 4;
1597 h = (gdk_screen_get_height(screen) * 2) / 3;
1599 g_signal_connect(G_OBJECT(window), "delete_event",
1600 G_CALLBACK(delete_event), NULL);
1602 g_signal_connect(G_OBJECT(window), "destroy",
1603 G_CALLBACK(destroy), NULL);
1605 /* file tabs */
1606 file_tabs = gtk_notebook_new();
1607 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(file_tabs), GTK_POS_TOP);
1608 GTK_WIDGET_UNSET_FLAGS(file_tabs,GTK_CAN_FOCUS);
1609 gtk_notebook_set_scrollable(GTK_NOTEBOOK(file_tabs), 1);
1611 vbox = gtk_vbox_new(FALSE, 2);
1612 gtk_container_add(GTK_CONTAINER(window), vbox);
1614 build_menu();
1615 gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 0);
1617 gtk_box_pack_start(GTK_BOX(vbox), file_tabs, FALSE, FALSE, 0);
1619 /* horizontal box holding the text and the scrollbar */
1620 hbox = gtk_hbox_new(FALSE, 2);
1621 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
1623 /* the Minimum Profit area */
1624 area = gtk_drawing_area_new();
1625 gtk_box_pack_start(GTK_BOX(hbox), area, TRUE, TRUE, 0);
1626 gtk_widget_set_size_request(GTK_WIDGET(area), w, h);
1627 gtk_widget_set_events(GTK_WIDGET(area), GDK_BUTTON_PRESS_MASK |
1628 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK |
1629 GDK_LEAVE_NOTIFY_MASK);
1631 gtk_widget_set_double_buffered(area, FALSE);
1633 g_signal_connect(G_OBJECT(area),"configure_event",
1634 G_CALLBACK(configure_event), NULL);
1636 g_signal_connect(G_OBJECT(area),"expose_event",
1637 G_CALLBACK(expose_event), NULL);
1639 g_signal_connect(G_OBJECT(area), "realize",
1640 G_CALLBACK(realize), NULL);
1642 g_signal_connect(G_OBJECT(window),"key_press_event",
1643 G_CALLBACK(key_press_event), NULL);
1645 g_signal_connect(G_OBJECT(window),"key_release_event",
1646 G_CALLBACK(key_release_event), NULL);
1648 g_signal_connect(G_OBJECT(area),"button_press_event",
1649 G_CALLBACK(button_press_event), NULL);
1651 g_signal_connect(G_OBJECT(area),"button_release_event",
1652 G_CALLBACK(button_release_event), NULL);
1654 g_signal_connect(G_OBJECT(area),"motion_notify_event",
1655 G_CALLBACK(motion_notify_event), NULL);
1657 g_signal_connect(G_OBJECT(area), "selection_clear_event",
1658 G_CALLBACK(selection_clear_event), NULL);
1660 g_signal_connect(G_OBJECT(area), "selection_get",
1661 G_CALLBACK(selection_get), NULL);
1663 g_signal_connect(G_OBJECT(area), "selection_received",
1664 G_CALLBACK(selection_received), NULL);
1666 g_signal_connect(G_OBJECT(area), "scroll_event",
1667 G_CALLBACK(scroll_event), NULL);
1669 gtk_selection_add_target(area, GDK_SELECTION_PRIMARY,
1670 GDK_SELECTION_TYPE_STRING, 1);
1672 g_signal_connect(G_OBJECT(file_tabs),"switch_page",
1673 G_CALLBACK(switch_page), NULL);
1675 /* the scrollbar */
1676 scrollbar = gtk_vscrollbar_new(NULL);
1677 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, FALSE, 0);
1679 g_signal_connect(
1680 G_OBJECT(gtk_range_get_adjustment(GTK_RANGE(scrollbar))),
1681 "value_changed", G_CALLBACK(value_changed), NULL);
1683 /* the status bar */
1684 status = gtk_label_new("mp " VERSION);
1685 gtk_box_pack_start(GTK_BOX(vbox), status, FALSE, FALSE, 0);
1686 gtk_misc_set_alignment(GTK_MISC(status), 0, 0.5);
1687 gtk_label_set_justify(GTK_LABEL(status), GTK_JUSTIFY_LEFT);
1689 gtk_widget_show_all(window);
1691 /* _mpv_font_size--;
1692 mpv_zoom(1);
1694 if (mpv_gtk_maximize)
1695 gtk_window_maximize(GTK_WINDOW(window));
1698 /* set size */
1699 /* if(!mpv_gtk_maximize)
1701 if(_mpv_gtk_xpos >= 0 && _mpv_gtk_ypos >= 0)
1702 gdk_window_move(GTK_WIDGET(window)->window,
1703 _mpv_gtk_xpos, _mpv_gtk_ypos);
1705 if(_mpv_gtk_width > 0 && _mpv_gtk_height > 0)
1707 if(_mpv_gtk_width < 150) _mpv_gtk_width=150;
1708 if(_mpv_gtk_height < 150) _mpv_gtk_height=150;
1710 gtk_window_set_default_size(GTK_WINDOW(window),
1711 _mpv_gtk_width, _mpv_gtk_height);
1713 gdk_window_resize(GTK_WIDGET(window)->window,
1714 _mpv_gtk_width, _mpv_gtk_height);
1718 /* colors */
1719 /* _mpv_create_colors();
1721 fclose(stderr);
1723 mp_log("X11 geometry set to %dx%d+%d+%d\n", _mpv_gtk_width, _mpv_gtk_height,
1724 _mpv_gtk_xpos, _mpv_gtk_ypos);
1727 /* set application icon */
1728 pixmap = gdk_pixmap_create_from_xpm_d(window->window,
1729 &mask, NULL, mp_xpm);
1730 gdk_window_set_icon(window->window, NULL, pixmap, mask);
1732 build_colors();
1734 if ((v = mpdm_hget_s(mp, L"config")) != NULL &&
1735 mpdm_ival(mpdm_hget_s(v, L"maximize")) > 0)
1736 maximize = 1;
1738 return NULL;
1742 int gtk_drv_detect(int * argc, char *** argv)
1744 mpdm_t drv;
1746 if (!gtk_init_check(argc, argv))
1747 return 0;
1749 drv = mpdm_hget_s(mp, L"drv");
1750 mpdm_hset_s(drv, L"id", MPDM_LS(L"gtk"));
1751 mpdm_hset_s(drv, L"startup", MPDM_X(gtk_drv_startup));
1753 return 1;
1756 #endif /* CONFOPT_GTK */