Version 5.1.2 RELEASED.
[mp-5.x.git] / mpv_gtk.c
blob7a267bc282fda29ec3a7a23622f127454678cb1f
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");
989 d = mpdm_join(MPDM_LS(L"\n"), d);
991 /* convert to current locale */
992 ptr = (unsigned char *) mpdm_wcstombs(d->data, &s);
994 /* pastes into primary selection */
995 gtk_selection_data_set(sel, GDK_SELECTION_TYPE_STRING, 8, ptr, (gsize) s);
997 free(ptr);
1001 static void selection_received(GtkWidget * widget,
1002 GtkSelectionData * sel, gpointer data)
1003 /* 'selection_received' handler */
1005 mpdm_t d;
1007 if (sel->data != NULL) {
1008 /* get selection */
1009 wchar_t * wptr = utf8_to_wcs((char *)sel->data);
1010 d = MPDM_S(wptr);
1011 g_free(wptr);
1013 /* split and set as the clipboard */
1014 mpdm_hset_s(mp, L"clipboard", mpdm_split(MPDM_LS(L"\n"), d));
1015 mpdm_hset_s(mp, L"clipboard_vertical", MPDM_I(0));
1017 /* wait no more for the selection */
1018 wait_for_selection = 0;
1020 else
1021 wait_for_selection = -1;
1025 static mpdm_t gtk_drv_clip_to_sys(mpdm_t a)
1026 /* driver-dependent mp to system clipboard */
1028 got_selection = gtk_selection_owner_set(area,
1029 GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME);
1031 return NULL;
1035 static mpdm_t gtk_drv_sys_to_clip(mpdm_t a)
1036 /* driver-dependent system to mp clipboard */
1038 if (!got_selection) {
1039 int n;
1040 char * formats[] = { "UTF8_STRING", "STRING", NULL };
1042 for(n = 0; formats[n] != NULL; n++) {
1044 /* triggers a selection capture */
1045 if (gtk_selection_convert(area, GDK_SELECTION_PRIMARY,
1046 gdk_atom_intern(formats[n], FALSE),
1047 GDK_CURRENT_TIME)) {
1049 /* processes the pending events
1050 (i.e., the 'selection_received' handler) */
1051 wait_for_selection = 1;
1053 while(wait_for_selection == 1)
1054 gtk_main_iteration();
1056 if (!wait_for_selection)
1057 break;
1062 return NULL;
1066 /** interface functions **/
1068 static void clicked_ok(GtkWidget * widget, gpointer data)
1069 /* 'clicked_on' signal handler (for gtk_drv_form) */
1071 int n;
1073 for (n = 0; n < mpdm_size(form_args); n++) {
1074 GtkWidget * widget = form_widgets[n];
1075 mpdm_t w = mpdm_aget(form_args, n);
1076 wchar_t * wptr = mpdm_string(mpdm_hget_s(w, L"type"));
1077 mpdm_t v = NULL;
1079 /* if there is already a value there, if was
1080 previously set from a callback */
1081 if (mpdm_aget(form_values, n) != NULL)
1082 continue;
1084 if (wcscmp(wptr, L"text") == 0 ||
1085 wcscmp(wptr, L"password") == 0) {
1086 char * ptr;
1087 GtkWidget * gw = widget;
1088 mpdm_t h;
1090 if (wcscmp(wptr, L"text") == 0)
1091 gw = GTK_COMBO(widget)->entry;
1093 if ((ptr = gtk_editable_get_chars(
1094 GTK_EDITABLE(gw), 0, -1)) != NULL &&
1095 (wptr = utf8_to_wcs(ptr)) != NULL) {
1096 v = MPDM_S(wptr);
1097 g_free(wptr);
1098 g_free(ptr);
1101 /* if it has history, fill it */
1102 if ((h = mpdm_hget_s(w, L"history")) != NULL &&
1103 v != NULL && mpdm_cmp(v, MPDM_LS(L"")) != 0) {
1104 h = mp_get_history(h);
1106 if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0)
1107 mpdm_push(h, v);
1110 else
1111 if (wcscmp(wptr, L"checkbox") == 0) {
1112 v = MPDM_I(gtk_toggle_button_get_active(
1113 GTK_TOGGLE_BUTTON(widget)));
1115 else
1116 if (wcscmp(wptr, L"list") == 0) {
1117 GtkWidget * list = gtk_bin_get_child(GTK_BIN(widget));
1118 GtkTreeSelection * selection =
1119 gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
1120 GList * selected = gtk_tree_selection_get_selected_rows(selection, NULL);
1121 GtkTreePath * path = selected->data;
1123 v = MPDM_I(gtk_tree_path_get_indices(path)[0]);
1124 gtk_tree_path_free(path);
1125 g_list_free(selected);
1129 mpdm_aset(form_values, v, n);
1134 static gint timer_callback(gpointer data)
1136 mpdm_exec(timer_func, NULL);
1137 redraw();
1139 return TRUE;
1143 static void build_form_data(mpdm_t widget_list)
1144 /* builds the necessary information for a list of widgets */
1146 mpdm_unref(form_args);
1147 form_args = mpdm_ref(widget_list);
1149 mpdm_unref(form_values);
1150 form_values = widget_list == NULL ? NULL :
1151 mpdm_ref(MPDM_A(mpdm_size(form_args)));
1153 /* resize the widget array */
1154 form_widgets = (GtkWidget **) realloc(form_widgets,
1155 mpdm_size(form_args) * sizeof(GtkWidget *));
1159 /** dialog functions **/
1161 #define DIALOG_BUTTON(l,f) do { GtkWidget * btn; \
1162 ptr = localize(l); btn = gtk_button_new_with_label(ptr); \
1163 g_signal_connect_swapped(G_OBJECT(btn), "clicked", \
1164 G_CALLBACK(f), G_OBJECT(dlg)); \
1165 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dlg)->action_area), \
1166 btn, TRUE, TRUE, 0); \
1167 g_free(ptr); \
1168 } while (0);
1170 static mpdm_t gtk_drv_alert(mpdm_t a)
1171 /* alert driver function */
1173 wchar_t * wptr;
1174 gchar * ptr;
1175 GtkWidget * dlg;
1177 build_form_data(NULL);
1179 /* 1# arg: prompt */
1180 wptr = mpdm_string(mpdm_aget(a, 0));
1182 if ((ptr = wcs_to_utf8(wptr)) == NULL)
1183 return NULL;
1185 dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL,
1186 GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, ptr);
1187 gtk_window_set_title(GTK_WINDOW(dlg), "mp " VERSION);
1188 g_free(ptr);
1190 gtk_dialog_run(GTK_DIALOG(dlg));
1191 gtk_widget_destroy(dlg);
1193 return NULL;
1197 static mpdm_t gtk_drv_confirm(mpdm_t a)
1198 /* confirm driver function */
1200 wchar_t * wptr;
1201 char * ptr;
1202 GtkWidget * dlg;
1203 gint response;
1205 build_form_data(NULL);
1207 /* 1# arg: prompt */
1208 wptr = mpdm_string(mpdm_aget(a, 0));
1210 if ((ptr = wcs_to_utf8(wptr)) == NULL)
1211 return NULL;
1213 dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL,
1214 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, ptr);
1215 gtk_window_set_title(GTK_WINDOW(dlg), "mp " VERSION);
1216 g_free(ptr);
1218 gtk_dialog_add_button(GTK_DIALOG(dlg), GTK_STOCK_YES, 1);
1219 gtk_dialog_add_button(GTK_DIALOG(dlg), GTK_STOCK_NO, 2);
1220 gtk_dialog_add_button(GTK_DIALOG(dlg), GTK_STOCK_CANCEL, 0);
1222 response = gtk_dialog_run(GTK_DIALOG(dlg));
1223 gtk_widget_destroy(dlg);
1225 if (response == GTK_RESPONSE_DELETE_EVENT)
1226 response = 0;
1228 return MPDM_I(response);
1232 static mpdm_t gtk_drv_form(mpdm_t a)
1233 /* 'form' driver function */
1235 GtkWidget * dlg;
1236 GtkWidget * table;
1237 GtkWidget * content_area;
1238 int n;
1239 mpdm_t ret = NULL;
1241 /* first argument: list of widgets */
1242 build_form_data(mpdm_aget(a, 0));
1244 dlg = gtk_dialog_new_with_buttons("mp " VERSION, GTK_WINDOW(window),
1245 GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
1246 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1247 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
1248 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);
1249 gtk_container_set_border_width(GTK_CONTAINER(dlg), 5);
1251 content_area = GTK_DIALOG(dlg)->vbox;
1252 gtk_box_set_spacing(GTK_BOX(content_area), 2);
1254 table = gtk_table_new(mpdm_size(a), 2, FALSE);
1255 gtk_container_set_border_width(GTK_CONTAINER(table), 5);
1256 gtk_table_set_col_spacings(GTK_TABLE(table), 12);
1257 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
1259 for (n = 0; n < mpdm_size(form_args); n++) {
1260 mpdm_t w = mpdm_aget(form_args, n);
1261 GtkWidget * widget = NULL;
1262 wchar_t * type;
1263 char * ptr;
1264 mpdm_t t;
1265 int col = 0;
1267 type = mpdm_string(mpdm_hget_s(w, L"type"));
1269 if ((t = mpdm_hget_s(w, L"label")) != NULL) {
1270 GtkWidget * label;
1271 wchar_t * wptr;
1273 if ((wptr = mpdm_string(mpdm_gettext(t))) != NULL &&
1274 (ptr = wcs_to_utf8(wptr)) != NULL) {
1275 label = gtk_label_new(ptr);
1276 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
1278 gtk_table_attach_defaults(GTK_TABLE(table),
1279 label, 0, wcscmp(type, L"label") == 0 ? 2 : 1,
1280 n, n + 1);
1282 g_free(ptr);
1284 col++;
1288 t = mpdm_hget_s(w, L"value");
1290 if (wcscmp(type, L"text") == 0) {
1291 GList * combo_items = NULL;
1292 mpdm_t h;
1293 wchar_t * wptr;
1295 widget = gtk_combo_new();
1296 gtk_widget_set_size_request(widget, 300, -1);
1297 gtk_combo_set_use_arrows_always(GTK_COMBO(widget), TRUE);
1298 gtk_combo_set_case_sensitive(GTK_COMBO(widget), TRUE);
1299 gtk_entry_set_activates_default(GTK_ENTRY(GTK_COMBO(widget)->entry), TRUE);
1301 if ((h = mpdm_hget_s(w, L"history")) != NULL) {
1302 int i;
1304 /* has history; fill it */
1305 h = mp_get_history(h);
1307 for (i = 0; i < mpdm_size(h); i++) {
1308 wptr = mpdm_string(mpdm_aget(h, i));
1309 ptr = wcs_to_utf8(wptr);
1311 combo_items = g_list_prepend(combo_items, ptr);
1315 if (t != NULL) {
1316 wptr = mpdm_string(t);
1317 ptr = wcs_to_utf8(wptr);
1319 combo_items = g_list_prepend(combo_items, ptr);
1322 gtk_combo_set_popdown_strings(GTK_COMBO(widget), combo_items);
1323 g_list_free(combo_items);
1325 else
1326 if (wcscmp(type, L"password") == 0) {
1327 widget = gtk_entry_new();
1328 gtk_widget_set_size_request(widget, 300, -1);
1329 gtk_entry_set_visibility(GTK_ENTRY(widget), FALSE);
1331 else
1332 if (wcscmp(type, L"checkbox") == 0) {
1333 widget = gtk_check_button_new();
1335 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget),
1336 mpdm_ival(t) ? TRUE : FALSE);
1338 else
1339 if (wcscmp(type, L"list") == 0) {
1340 GtkWidget * list;
1341 GtkListStore * list_store;
1342 GtkCellRenderer * renderer;
1343 GtkTreeViewColumn * column;
1344 GtkTreePath * path;
1345 mpdm_t l;
1346 gint i;
1348 if ((i = 450 / mpdm_size(form_args)) < 100)
1349 i = 100;
1351 widget = gtk_scrolled_window_new(NULL, NULL);
1352 gtk_widget_set_size_request(widget, 500, i);
1353 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(widget),
1354 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1355 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(widget),
1356 GTK_SHADOW_IN);
1358 list_store = gtk_list_store_new(1, G_TYPE_STRING);
1359 list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store));
1360 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
1361 renderer = gtk_cell_renderer_text_new();
1362 column = gtk_tree_view_column_new_with_attributes("", renderer,
1363 "text", 0, NULL);
1364 gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
1365 gtk_container_add(GTK_CONTAINER(widget), list);
1367 l = mpdm_hget_s(w, L"list");
1369 for (i = 0; i < mpdm_size(l); i++) {
1370 wchar_t * wptr = mpdm_string(mpdm_aget(l, i));
1372 if ((ptr = wcs_to_utf8(wptr)) != NULL) {
1373 GtkTreeIter iter;
1374 gtk_list_store_append(list_store, &iter);
1375 gtk_list_store_set(list_store, &iter, 0, ptr, -1);
1376 g_free(ptr);
1380 /* initial position */
1381 i = mpdm_ival(t);
1383 path = gtk_tree_path_new_from_indices(i, -1);
1384 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list), path, NULL, FALSE);
1385 gtk_tree_path_free(path);
1387 g_signal_connect_swapped(G_OBJECT(list), "row-activated",
1388 G_CALLBACK(gtk_window_activate_default), dlg);
1391 if (widget != NULL) {
1392 form_widgets[n] = widget;
1393 gtk_table_attach_defaults(GTK_TABLE(table),
1394 widget, col, 2, n, n + 1);
1398 gtk_widget_show_all(table);
1400 gtk_box_pack_start(GTK_BOX(content_area), table, TRUE, TRUE, 0);
1402 if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
1403 clicked_ok(NULL, NULL);
1404 ret = form_values;
1406 gtk_widget_destroy(dlg);
1408 return ret;
1412 static mpdm_t run_filechooser(mpdm_t a, gboolean save)
1413 /* openfile driver function */
1415 GtkWidget * dlg;
1416 wchar_t * wptr;
1417 char * ptr;
1418 mpdm_t ret = NULL;
1419 gint response;
1421 /* 1# arg: prompt */
1422 wptr = mpdm_string(mpdm_aget(a, 0));
1424 if ((ptr = wcs_to_utf8(wptr)) == NULL)
1425 return(NULL);
1427 if (!save) {
1428 dlg = gtk_file_chooser_dialog_new(ptr, GTK_WINDOW(window),
1429 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1430 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
1432 else {
1433 dlg = gtk_file_chooser_dialog_new(ptr, GTK_WINDOW(window),
1434 GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_STOCK_CANCEL,
1435 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
1436 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dlg), TRUE);
1438 g_free(ptr);
1440 build_form_data(NULL);
1442 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dlg), TRUE);
1443 response = gtk_dialog_run(GTK_DIALOG(dlg));
1445 if (response == GTK_RESPONSE_OK) {
1446 gchar * filename;
1447 gchar * utf8name;
1448 wchar_t *wfilename;
1450 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
1451 utf8name = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
1452 g_free(filename);
1453 wfilename = utf8_to_wcs(utf8name);
1454 g_free(utf8name);
1455 ret = MPDM_S(wfilename);
1456 g_free(wfilename);
1458 gtk_widget_destroy(dlg);
1460 return ret;
1464 static mpdm_t gtk_drv_openfile(mpdm_t a)
1465 /* openfile driver function */
1467 return run_filechooser(a, FALSE);
1471 static mpdm_t gtk_drv_savefile(mpdm_t a)
1472 /* savefile driver function */
1474 return run_filechooser(a, TRUE);
1478 static mpdm_t gtk_drv_update_ui(mpdm_t a)
1480 build_fonts();
1481 build_colors();
1482 build_menu();
1484 redraw();
1486 return NULL;
1490 static mpdm_t gtk_drv_timer(mpdm_t a)
1492 static guint prev = 0;
1493 int msecs = mpdm_ival(mpdm_aget(a, 0));
1494 mpdm_t func = mpdm_aget(a, 1);
1495 mpdm_t r;
1497 /* previously defined one? remove */
1498 if (timer_func != NULL)
1499 gtk_timeout_remove(prev);
1501 /* if msecs and func are set, program timer */
1502 if (msecs > 0 && func != NULL)
1503 prev = gtk_timeout_add(msecs, timer_callback, NULL);
1505 r = mpdm_unref(timer_func);
1506 timer_func = mpdm_ref(func);
1508 return r;
1512 static mpdm_t gtk_drv_busy(mpdm_t a)
1514 int onoff = mpdm_ival(mpdm_aget(a, 0));
1516 gdk_window_set_cursor(window->window,
1517 gdk_cursor_new(onoff ? GDK_WATCH : GDK_LEFT_PTR));
1519 while (gtk_events_pending())
1520 gtk_main_iteration();
1522 return NULL;
1526 static mpdm_t gtk_drv_main_loop(mpdm_t a)
1527 /* main loop */
1529 if (!mp_exit_requested) {
1530 gtk_drv_paint(mp_active(), 0);
1532 gtk_main();
1535 return NULL;
1539 static mpdm_t gtk_drv_shutdown(mpdm_t a)
1540 /* shutdown */
1542 mpdm_t v;
1544 if ((v = mpdm_hget_s(mp, L"exit_message")) != NULL) {
1545 mpdm_write_wcs(stdout, mpdm_string(v));
1546 printf("\n");
1549 return NULL;
1553 static void register_functions(void)
1555 mpdm_t drv;
1557 drv = mpdm_hget_s(mp, L"drv");
1558 mpdm_hset_s(drv, L"main_loop", MPDM_X(gtk_drv_main_loop));
1559 mpdm_hset_s(drv, L"shutdown", MPDM_X(gtk_drv_shutdown));
1561 mpdm_hset_s(drv, L"clip_to_sys", MPDM_X(gtk_drv_clip_to_sys));
1562 mpdm_hset_s(drv, L"sys_to_clip", MPDM_X(gtk_drv_sys_to_clip));
1563 mpdm_hset_s(drv, L"update_ui", MPDM_X(gtk_drv_update_ui));
1564 mpdm_hset_s(drv, L"timer", MPDM_X(gtk_drv_timer));
1565 mpdm_hset_s(drv, L"busy", MPDM_X(gtk_drv_busy));
1567 mpdm_hset_s(drv, L"alert", MPDM_X(gtk_drv_alert));
1568 mpdm_hset_s(drv, L"confirm", MPDM_X(gtk_drv_confirm));
1569 mpdm_hset_s(drv, L"openfile", MPDM_X(gtk_drv_openfile));
1570 mpdm_hset_s(drv, L"savefile", MPDM_X(gtk_drv_savefile));
1571 mpdm_hset_s(drv, L"form", MPDM_X(gtk_drv_form));
1575 static mpdm_t gtk_drv_startup(mpdm_t a)
1576 /* driver initialization */
1578 GtkWidget * vbox;
1579 GtkWidget * hbox;
1580 GdkPixmap * pixmap;
1581 GdkPixmap * mask;
1582 GdkScreen *screen;
1583 mpdm_t v;
1584 int w, h;
1586 register_functions();
1588 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1590 /* get real screen and pick a usable size for the main area */
1591 screen = gtk_window_get_screen(GTK_WINDOW(window));
1592 w = (gdk_screen_get_width(screen) * 3) / 4;
1593 h = (gdk_screen_get_height(screen) * 2) / 3;
1595 g_signal_connect(G_OBJECT(window), "delete_event",
1596 G_CALLBACK(delete_event), NULL);
1598 g_signal_connect(G_OBJECT(window), "destroy",
1599 G_CALLBACK(destroy), NULL);
1601 /* file tabs */
1602 file_tabs = gtk_notebook_new();
1603 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(file_tabs), GTK_POS_TOP);
1604 GTK_WIDGET_UNSET_FLAGS(file_tabs,GTK_CAN_FOCUS);
1605 gtk_notebook_set_scrollable(GTK_NOTEBOOK(file_tabs), 1);
1607 vbox = gtk_vbox_new(FALSE, 2);
1608 gtk_container_add(GTK_CONTAINER(window), vbox);
1610 build_menu();
1611 gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 0);
1613 gtk_box_pack_start(GTK_BOX(vbox), file_tabs, FALSE, FALSE, 0);
1615 /* horizontal box holding the text and the scrollbar */
1616 hbox = gtk_hbox_new(FALSE, 2);
1617 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
1619 /* the Minimum Profit area */
1620 area = gtk_drawing_area_new();
1621 gtk_box_pack_start(GTK_BOX(hbox), area, TRUE, TRUE, 0);
1622 gtk_widget_set_size_request(GTK_WIDGET(area), w, h);
1623 gtk_widget_set_events(GTK_WIDGET(area), GDK_BUTTON_PRESS_MASK |
1624 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK |
1625 GDK_LEAVE_NOTIFY_MASK);
1627 gtk_widget_set_double_buffered(area, FALSE);
1629 g_signal_connect(G_OBJECT(area),"configure_event",
1630 G_CALLBACK(configure_event), NULL);
1632 g_signal_connect(G_OBJECT(area),"expose_event",
1633 G_CALLBACK(expose_event), NULL);
1635 g_signal_connect(G_OBJECT(area), "realize",
1636 G_CALLBACK(realize), NULL);
1638 g_signal_connect(G_OBJECT(window),"key_press_event",
1639 G_CALLBACK(key_press_event), NULL);
1641 g_signal_connect(G_OBJECT(window),"key_release_event",
1642 G_CALLBACK(key_release_event), NULL);
1644 g_signal_connect(G_OBJECT(area),"button_press_event",
1645 G_CALLBACK(button_press_event), NULL);
1647 g_signal_connect(G_OBJECT(area),"button_release_event",
1648 G_CALLBACK(button_release_event), NULL);
1650 g_signal_connect(G_OBJECT(area),"motion_notify_event",
1651 G_CALLBACK(motion_notify_event), NULL);
1653 g_signal_connect(G_OBJECT(area), "selection_clear_event",
1654 G_CALLBACK(selection_clear_event), NULL);
1656 g_signal_connect(G_OBJECT(area), "selection_get",
1657 G_CALLBACK(selection_get), NULL);
1659 g_signal_connect(G_OBJECT(area), "selection_received",
1660 G_CALLBACK(selection_received), NULL);
1662 g_signal_connect(G_OBJECT(area), "scroll_event",
1663 G_CALLBACK(scroll_event), NULL);
1665 gtk_selection_add_target(area, GDK_SELECTION_PRIMARY,
1666 GDK_SELECTION_TYPE_STRING, 1);
1668 g_signal_connect(G_OBJECT(file_tabs),"switch_page",
1669 G_CALLBACK(switch_page), NULL);
1671 /* the scrollbar */
1672 scrollbar = gtk_vscrollbar_new(NULL);
1673 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, FALSE, 0);
1675 g_signal_connect(
1676 G_OBJECT(gtk_range_get_adjustment(GTK_RANGE(scrollbar))),
1677 "value_changed", G_CALLBACK(value_changed), NULL);
1679 /* the status bar */
1680 status = gtk_label_new("mp " VERSION);
1681 gtk_box_pack_start(GTK_BOX(vbox), status, FALSE, FALSE, 0);
1682 gtk_misc_set_alignment(GTK_MISC(status), 0, 0.5);
1683 gtk_label_set_justify(GTK_LABEL(status), GTK_JUSTIFY_LEFT);
1685 gtk_widget_show_all(window);
1687 /* _mpv_font_size--;
1688 mpv_zoom(1);
1690 if (mpv_gtk_maximize)
1691 gtk_window_maximize(GTK_WINDOW(window));
1694 /* set size */
1695 /* if(!mpv_gtk_maximize)
1697 if(_mpv_gtk_xpos >= 0 && _mpv_gtk_ypos >= 0)
1698 gdk_window_move(GTK_WIDGET(window)->window,
1699 _mpv_gtk_xpos, _mpv_gtk_ypos);
1701 if(_mpv_gtk_width > 0 && _mpv_gtk_height > 0)
1703 if(_mpv_gtk_width < 150) _mpv_gtk_width=150;
1704 if(_mpv_gtk_height < 150) _mpv_gtk_height=150;
1706 gtk_window_set_default_size(GTK_WINDOW(window),
1707 _mpv_gtk_width, _mpv_gtk_height);
1709 gdk_window_resize(GTK_WIDGET(window)->window,
1710 _mpv_gtk_width, _mpv_gtk_height);
1714 /* colors */
1715 /* _mpv_create_colors();
1717 fclose(stderr);
1719 mp_log("X11 geometry set to %dx%d+%d+%d\n", _mpv_gtk_width, _mpv_gtk_height,
1720 _mpv_gtk_xpos, _mpv_gtk_ypos);
1723 /* set application icon */
1724 pixmap = gdk_pixmap_create_from_xpm_d(window->window,
1725 &mask, NULL, mp_xpm);
1726 gdk_window_set_icon(window->window, NULL, pixmap, mask);
1728 build_colors();
1730 if ((v = mpdm_hget_s(mp, L"config")) != NULL &&
1731 mpdm_ival(mpdm_hget_s(v, L"maximize")) > 0)
1732 maximize = 1;
1734 return NULL;
1738 int gtk_drv_detect(int * argc, char *** argv)
1740 mpdm_t drv;
1742 if (!gtk_init_check(argc, argv))
1743 return 0;
1745 drv = mpdm_hget_s(mp, L"drv");
1746 mpdm_hset_s(drv, L"id", MPDM_LS(L"gtk"));
1747 mpdm_hset_s(drv, L"startup", MPDM_X(gtk_drv_startup));
1749 return 1;
1752 #endif /* CONFOPT_GTK */