Add task-bar icon
[xboard.git] / xboard.c
index 2857317..0ac3a44 100644 (file)
--- a/xboard.c
+++ b/xboard.c
@@ -149,52 +149,9 @@ extern char *getenv();
 #include <locale.h>
 #endif
 
-#include <X11/Intrinsic.h>
-#include <X11/StringDefs.h>
-#include <X11/Shell.h>
-#include <X11/cursorfont.h>
-#include <X11/Xatom.h>
-#include <X11/Xmu/Atoms.h>
-#if USE_XAW3D
-#include <X11/Xaw3d/Dialog.h>
-#include <X11/Xaw3d/Form.h>
-#include <X11/Xaw3d/List.h>
-#include <X11/Xaw3d/Label.h>
-#include <X11/Xaw3d/SimpleMenu.h>
-#include <X11/Xaw3d/SmeBSB.h>
-#include <X11/Xaw3d/SmeLine.h>
-#include <X11/Xaw3d/Box.h>
-#include <X11/Xaw3d/MenuButton.h>
-#include <X11/Xaw3d/Text.h>
-#include <X11/Xaw3d/AsciiText.h>
-#else
-#include <X11/Xaw/Dialog.h>
-#include <X11/Xaw/Form.h>
-#include <X11/Xaw/List.h>
-#include <X11/Xaw/Label.h>
-#include <X11/Xaw/SimpleMenu.h>
-#include <X11/Xaw/SmeBSB.h>
-#include <X11/Xaw/SmeLine.h>
-#include <X11/Xaw/Box.h>
-#include <X11/Xaw/MenuButton.h>
-#include <X11/Xaw/Text.h>
-#include <X11/Xaw/AsciiText.h>
-#endif
-
 // [HGM] bitmaps: put before incuding the bitmaps / pixmaps, to know how many piece types there are.
 #include "common.h"
 
-#if HAVE_LIBXPM
-#include <X11/xpm.h>
-#define IMAGE_EXT "xpm"
-#else
-#define IMAGE_EXT "xim"
-#endif
-
-#include "bitmaps/icon_white.bm"
-#include "bitmaps/icon_black.bm"
-#include "bitmaps/checkmark.bm"
-
 #include "frontend.h"
 #include "backend.h"
 #include "backendz.h"
@@ -232,17 +189,16 @@ int main P((int argc, char **argv));
 RETSIGTYPE CmailSigHandler P((int sig));
 RETSIGTYPE IntSigHandler P((int sig));
 RETSIGTYPE TermSizeSigHandler P((int sig));
-Widget CreateMenuBar P((Menu *mb, int boardWidth));
 #if ENABLE_NLS
 char *InsertPxlSize P((char *pattern, int targetPxlSize));
 XFontSet CreateFontSet P((char *base_fnt_lst));
 #else
 char *FindFont P((char *pattern, int targetPxlSize));
 #endif
-void ReadBitmap P((Pixmap *pm, String name, unsigned char bits[],
-                  u_int wreq, u_int hreq));
-void EventProc P((Widget widget, caddr_t unused, XEvent *event));
 void DelayedDrag P((void));
+void ICSInputBoxPopUp P((void));
+gboolean KeyPressProc P((GtkWindow *window, GdkEventKey *eventkey, gpointer data));
+#ifdef TODO_GTK
 static void MoveTypeInProc P((Widget widget, caddr_t unused, XEvent *event));
 void HandlePV P((Widget w, XEvent * event,
                     String * params, Cardinal * nParams));
@@ -250,7 +206,6 @@ void DrawPositionProc P((Widget w, XEvent *event,
                     String *prms, Cardinal *nprms));
 void CommentClick P((Widget w, XEvent * event,
                   String * params, Cardinal * nParams));
-void ICSInputBoxPopUp P((void));
 void SelectCommand P((Widget w, XtPointer client_data, XtPointer call_data));
 void KeyBindingProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
 void QuitWrapper P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
@@ -259,20 +214,21 @@ static void UpKeyProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms)
 static void DownKeyProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
 void TempBackwardProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
 void TempForwardProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
-Boolean TempBackwardActive = False;
 void ManInner P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
+void SelectMove P((Widget w, XEvent * event, String * params, Cardinal * nParams));
+#endif
+Boolean TempBackwardActive = False;
 void DisplayMove P((int moveNumber));
 void ICSInitScript P((void));
-void SelectMove P((Widget w, XEvent * event, String * params, Cardinal * nParams));
 void update_ics_width P(());
 int CopyMemoProc P(());
 
+#ifdef TODO_GTK
 /*
 * XBoard depends on Xt R4 or higher
 */
 int xtVersion = XtSpecificationRelease;
 
-#ifdef TODO_GTK
 int xScreen;
 Display *xDisplay;
 Window xBoardWindow;
@@ -298,8 +254,11 @@ char *layoutName;
 
 char installDir[] = "."; // [HGM] UCI: needed for UCI; probably needs run-time initializtion
 
-Position commentX = -1, commentY = -1;
-Dimension commentW, commentH;
+/* pixbufs */
+static GdkPixbuf       *mainwindowIcon=NULL;
+static GdkPixbuf       *WhiteIcon=NULL;
+static GdkPixbuf       *BlackIcon=NULL;
+
 typedef unsigned int BoardSize;
 BoardSize boardSize;
 Boolean chessProgram;
@@ -309,9 +268,11 @@ int smallLayout = 0, tinyLayout = 0,
   marginW, marginH, // [HGM] for run-time resizing
   fromX = -1, fromY = -1, toX, toY, commentUp = False,
   errorExitStatus = -1, defaultLineGap;
+#ifdef TODO_GTK
 Dimension textHeight;
 Pixel timerForegroundPixel, timerBackgroundPixel;
 Pixel buttonForegroundPixel, buttonBackgroundPixel;
+#endif
 char *chessDir, *programName, *programVersion;
 Boolean alwaysOnTop = False;
 char *icsTextMenuString;
@@ -328,20 +289,6 @@ WindowPlacement wpEngineOutput;
 WindowPlacement wpGameList;
 WindowPlacement wpTags;
 
-#define INPUT_SOURCE_BUF_SIZE 8192
-
-typedef struct {
-    CPKind kind;
-    int fd;
-    int lineByLine;
-    char *unused;
-    InputCallback func;
-    XtInputId xid;
-    char buf[INPUT_SOURCE_BUF_SIZE];
-    VOIDSTAR closure;
-} InputSource;
-
-
 /* This magic number is the number of intermediate frames used
    in each half of the animation. For short moves it's reduced
    by 1. The total number of frames will be factor * 2 + 1.  */
@@ -1072,9 +1019,10 @@ main (int argc, char **argv)
 #ifdef TODO_GTK
     XSetWindowAttributes window_attributes;
     Arg args[16];
+    Dimension boardWidth, boardHeight, w, h;
 #else
 #endif
-    Dimension boardWidth, boardHeight, w, h;
+    int boardWidth, boardHeight, w, h;
     char *p;
     int forceMono = False;
     GError *gtkerror=NULL;
@@ -1300,14 +1248,6 @@ main (int argc, char **argv)
        appData.monoMode = True;
     }
 
-    if (appData.monoMode && appData.debugMode) {
-#ifdef TODO_GTK
-       fprintf(stderr, _("white pixel = 0x%lx, black pixel = 0x%lx\n"),
-               (unsigned long) XWhitePixel(xDisplay, xScreen),
-               (unsigned long) XBlackPixel(xDisplay, xScreen));
-#endif
-    }
-
     ParseIcsTextColors();
 
 #ifdef TODO_GTK
@@ -1341,8 +1281,8 @@ main (int argc, char **argv)
     menuBarWidget    = optList[W_MENU].handle;
     dropMenu         = optList[W_DROP].handle;
     titleWidget = optList[optList[W_TITLE].type != -1 ? W_TITLE : W_SMALL].handle;
-    formWidget  = XtParent(boardWidget);
 #ifdef TODO_GTK
+    formWidget  = XtParent(boardWidget);
     XtSetArg(args[0], XtNbackground, &timerBackgroundPixel);
     XtSetArg(args[1], XtNforeground, &timerForegroundPixel);
     XtGetValues(optList[W_WHITE].handle, args, 2);
@@ -1352,7 +1292,6 @@ main (int argc, char **argv)
       XtGetValues(optList[W_PAUSE].handle, args, 2);
     }
 #endif
-    AppendEnginesToMenu(appData.recentEngineList);
 
 #ifdef TODO_GTK
     xBoardWindow = XtWindow(boardWidget);
@@ -1361,28 +1300,16 @@ main (int argc, char **argv)
     // [HGM] it seems the layout code ends here, but perhaps the color stuff is size independent and would
     //       not need to go into InitDrawingSizes().
 
-    /*
-     * Create X checkmark bitmap and initialize option menu checks.
-     */
-#ifdef TODO_GTK
-    ReadBitmap(&xMarkPixmap, "checkmark.bm",
-              checkmark_bits, checkmark_width, checkmark_height);
-#endif
     InitMenuMarkers();
 
     /*
-     * Create an icon.
+     * Create an icon. (Use two icons, to indicate whther it is white's or black's turn.)
      */
-#ifdef TODO_GTK
-    ReadBitmap(&wIconPixmap, "icon_white.bm",
-              icon_white_bits, icon_white_width, icon_white_height);
-    ReadBitmap(&bIconPixmap, "icon_black.bm",
-              icon_black_bits, icon_black_width, icon_black_height);
-    iconPixmap = wIconPixmap;
-    i = 0;
-    XtSetArg(args[i], XtNiconPixmap, iconPixmap);  i++;
-    XtSetValues(shellWidget, args, i);
-#endif
+    WhiteIcon  = gdk_pixbuf_new_from_file(SVGDIR "/icon_white.svg", NULL);
+    BlackIcon  = gdk_pixbuf_new_from_file(SVGDIR "/icon_black.svg", NULL);
+    mainwindowIcon = WhiteIcon;
+    gtk_window_set_icon(GTK_WINDOW(shellWidget), mainwindowIcon);
+
 
     /*
      * Create a cursor for the board widget.
@@ -1433,6 +1360,7 @@ main (int argc, char **argv)
     XtAddEventHandler(shellWidget, StructureNotifyMask, False,
                      (XtEventHandler) EventProc, NULL);
 #endif
+    g_signal_connect(shells[BoardWindow], "key-press-event", G_CALLBACK(KeyPressProc), NULL);
 
     /* [AS] Restore layout */
     if( wpMoveHistory.visible ) {
@@ -1477,7 +1405,10 @@ main (int argc, char **argv)
 #endif
 
     /* check for GTK events and process them */
-    gtk_main();
+//    gtk_main();
+while(1) {
+gtk_main_iteration();
+}
 
     if (appData.debugMode) fclose(debugFP); // [DM] debug
     return 0;
@@ -1679,24 +1610,11 @@ FindFont (char *pattern, int targetPxlSize)
 #endif
 
 void
-ReadBitmap (Pixmap *pm, String name, unsigned char bits[], u_int wreq, u_int hreq)
-{
-    if (bits != NULL) {
-#ifdef TODO_GTK
-       *pm = XCreateBitmapFromData(xDisplay, xBoardWindow, (char *) bits,
-                                   wreq, hreq);
-#endif
-    }
-}
-
-void
 EnableNamedMenuItem (char *menuRef, int state)
 {
     MenuItem *item = MenuNameToItem(menuRef);
 
-#ifdef TODO_GTK
-    if(item) XtSetSensitive(item->handle, state);
-#endif
+    if(item) gtk_widget_set_sensitive(item->handle, state);
 }
 
 void
@@ -1717,6 +1635,26 @@ SetMenuEnables (Enables *enab)
   }
 }
 
+gboolean KeyPressProc(window, eventkey, data)
+     GtkWindow *window;
+     GdkEventKey  *eventkey;
+     gpointer data;
+{
+
+    MoveTypeInProc(eventkey); // pop up for typed in moves
+
+#ifdef TODO_GTK
+    /* check for other key values */
+    switch(eventkey->keyval) {
+        case GDK_question:
+         AboutGameEvent();
+         break;
+        default:
+         break;
+    }
+#endif
+    return False;
+}
 #ifdef TODO_GTK
 void
 KeyBindingProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
@@ -1728,22 +1666,6 @@ KeyBindingProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
 }
 #endif
 
-#ifdef TODO_GTK
-static void
-MenuEngineSelect (Widget w, caddr_t addr, caddr_t index)
-{
-    RecentEngineEvent((int) (intptr_t) addr);
-}
-#endif
-
-void
-AppendMenuItem (char *msg, int n)
-{
-#ifdef TODO_GTK
-    CreateMenuItem((Widget) optList[W_ENGIN].textValue, msg, (XtCallbackProc) MenuEngineSelect, n);
-#endif
-}
-
 void
 SetupDropMenu ()
 {
@@ -1780,10 +1702,7 @@ do_flash_delay (unsigned long msec)
 void
 FlashDelay (int flash_delay)
 {
-#ifdef TODO_GTK
-       XSync(xDisplay, False);
        if(flash_delay) do_flash_delay(flash_delay);
-#endif
 }
 
 double
@@ -1893,72 +1812,26 @@ DelayedDrag ()
 #endif
 }
 
+#ifdef TODO_GTK
 void
 EventProc (Widget widget, caddr_t unused, XEvent *event)
 {
-#ifdef TODO_GTK
     if(XtIsRealized(widget) && event->type == ConfigureNotify || appData.useStickyWindows)
        DelayedDrag(); // as long as events keep coming in faster than 50 msec, they destroy each other
-#endif
 }
+#endif
 
 /*
  * event handler for redrawing the board
  */
+#ifdef TODO_GTK
 void
 DrawPositionProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
 {
     DrawPosition(True, NULL);
 }
-
-
-#ifdef TODO_GTK
-void
-HandlePV (Widget w, XEvent * event, String * params, Cardinal * nParams)
-{   // [HGM] pv: walk PV
-    MovePV(event->xmotion.x, event->xmotion.y, lineGap + BOARD_HEIGHT * (squareSize + lineGap));
-}
 #endif
 
-static int savedIndex;  /* gross that this is global */
-
-void
-CommentClick (Widget w, XEvent * event, String * params, Cardinal * nParams)
-{
-#ifdef TODO_GTK
-       String val;
-       XawTextPosition index, dummy;
-       Arg arg;
-
-       XawTextGetSelectionPos(w, &index, &dummy);
-       XtSetArg(arg, XtNstring, &val);
-       XtGetValues(w, &arg, 1);
-       ReplaceComment(savedIndex, val);
-       if(savedIndex != currentMove) ToNrEvent(savedIndex);
-       LoadVariation( index, val ); // [HGM] also does the actual moving to it, now
-#endif
-}
-
-void
-EditCommentPopUp (int index, char *title, char *text)
-{
-    savedIndex = index;
-    if (text == NULL) text = "";
-    NewCommentPopup(title, text, index);
-}
-
-void
-CommentPopUp (char *title, char *text)
-{
-    savedIndex = currentMove; // [HGM] vari
-    NewCommentPopup(title, text, currentMove);
-}
-
-void
-CommentPopDown ()
-{
-    PopDown(CommentDlg);
-}
 
 
 /* Disable all user input other than deleting the window */
@@ -1969,7 +1842,7 @@ FreezeUI ()
 {
   if (frozen) return;
   /* Grab by a widget that doesn't accept input */
-  XtAddGrab(optList[W_MESSG].handle, TRUE, FALSE);
+  gtk_grab_add(optList[W_MESSG].handle);
   frozen = 1;
 }
 
@@ -1978,9 +1851,7 @@ void
 ThawUI ()
 {
   if (!frozen) return;
-#ifdef TODO_GTK
-  XtRemoveGrab(optList[W_MESSG].handle);
-#endif
+  gtk_grab_remove(optList[W_MESSG].handle);
   frozen = 0;
 }
 
@@ -1990,10 +1861,7 @@ ModeHighlight ()
     static int oldPausing = FALSE;
     static GameMode oldmode = (GameMode) -1;
     char *wname;
-#ifdef TODO_GTK
-    Arg args[16];
-
-    if (!boardWidget || !XtIsRealized(boardWidget)) return;
+    if (!boardWidget) return;
 
     if (pausing != oldPausing) {
        oldPausing = pausing;
@@ -2003,19 +1871,11 @@ ModeHighlight ()
          /* Always toggle, don't set.  Previous code messes up when
             invoked while the button is pressed, as releasing it
             toggles the state again. */
-         {
-           Pixel oldbg, oldfg;
-           XtSetArg(args[0], XtNbackground, &oldbg);
-           XtSetArg(args[1], XtNforeground, &oldfg);
-           XtGetValues(optList[W_PAUSE].handle,
-                       args, 2);
-           XtSetArg(args[0], XtNbackground, oldfg);
-           XtSetArg(args[1], XtNforeground, oldbg);
-         }
-         XtSetValues(optList[W_PAUSE].handle, args, 2);
+           GdkColor color;     
+            gdk_color_parse( pausing ? "#808080" : "#F0F0F0", &color );
+            gtk_widget_modify_bg ( GTK_WIDGET(optList[W_PAUSE].handle), GTK_STATE_NORMAL, &color );
        }
     }
-#endif
 
     wname = ModeToWidgetName(oldmode);
     if (wname != NULL) {
@@ -2214,62 +2074,31 @@ PasteGameProc ()
 }
 
 
+#ifdef TODO_GTK
 void
 QuitWrapper (Widget w, XEvent *event, String *prms, Cardinal *nprms)
 {
     QuitProc();
 }
-
-int
-ShiftKeys ()
-{   // bassic primitive for determining if modifier keys are pressed
-    int i,j,  k=0;
-#ifdef TODO_GTK
-    long int codes[] = { XK_Meta_L, XK_Meta_R, XK_Control_L, XK_Control_R, XK_Shift_L, XK_Shift_R };
-    char keys[32];
-    XQueryKeymap(xDisplay,keys);
-    for(i=0; i<6; i++) {
-       k <<= 1;
-       j = XKeysymToKeycode(xDisplay, codes[i]);
-       k += ( (keys[j>>3]&1<<(j&7)) != 0 );
-    }
 #endif
-    return k;
-}
 
-static void
-MoveTypeInProc (Widget widget, caddr_t unused, XEvent *event)
+void MoveTypeInProc(eventkey)
+    GdkEventKey  *eventkey;
 {
-#ifdef TODO_GTK
     char buf[10];
-    KeySym sym;
-    int n = XLookupString(&(event->xkey), buf, 10, &sym, NULL);
-    if ( n == 1 && *buf >= 32 // printable
-        && !(ShiftKeys() & 0x3C) // no Alt, Ctrl
-       ) BoxAutoPopUp (buf);
-#endif
-}
 
-#ifdef TODO_GTK
-static void
-UpKeyProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
-{   // [HGM] input: let up-arrow recall previous line from history
-    IcsKey(1);
-}
-
-static void
-DownKeyProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
-{   // [HGM] input: let down-arrow recall next line from history
-    IcsKey(-1);
-}
+    // ingnore if ctrl or alt is pressed
+    if (eventkey->state & (GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
+        return;
+    }
 
-static void
-EnterKeyProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
-{
-    IcsKey(0);
+    buf[0]=eventkey->keyval;
+    buf[1]='\0';
+    if (*buf >= 32)        
+       BoxAutoPopUp (buf);
 }
 
-
+#ifdef TODO_GTK
 void
 TempBackwardProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
 {
@@ -2334,6 +2163,10 @@ SetWindowTitle (char *text, char *title, char *icon)
     XtSetValues(shellWidget, args, i);
     XSync(xDisplay, False);
 #endif
+    if (appData.titleInWindow) {
+       SetWidgetLabel(titleWidget, text);
+    }
+    gtk_window_set_title (GTK_WINDOW(shells[BoardWindow]), title);
 }
 
 
@@ -2378,76 +2211,91 @@ DisplayIcsInteractionTitle (String message)
 void
 DisplayTimerLabel (Option *opt, char *color, long timer, int highlight)
 {
-#ifdef TODO_GTK
-    char buf[MSG_SIZ];
-    Arg args[16];
-    Widget w = (Widget) opt->handle;
-
-    /* check for low time warning */
-    Pixel foregroundOrWarningColor = timerForegroundPixel;
+    GtkWidget *w = (GtkWidget *) opt->handle;
+    char *markup;
+    char bgcolor[10];
+    char fgcolor[10];
 
+    if (highlight) {
+       strcpy(bgcolor, "black");
+        strcpy(fgcolor, "white");
+    } else {
+        strcpy(bgcolor, "white");
+        strcpy(fgcolor, "black");
+    }
     if (timer > 0 &&
         appData.lowTimeWarning &&
-        (timer / 1000) < appData.icsAlarmTime)
-      foregroundOrWarningColor = lowTimeWarningColor;
-
-    if (appData.clockMode) {
-      snprintf(buf, MSG_SIZ, "%s:%s%s", color, appData.logoSize && !partnerUp ? "\n" : " ", TimeString(timer));
-      XtSetArg(args[0], XtNlabel, buf);
-    } else {
-      snprintf(buf, MSG_SIZ, "%s  ", color);
-      XtSetArg(args[0], XtNlabel, buf);
+        (timer / 1000) < appData.icsAlarmTime) {
+        strcpy(fgcolor, appData.lowTimeWarningColor);
     }
 
-    if (highlight) {
-
-       XtSetArg(args[1], XtNbackground, foregroundOrWarningColor);
-       XtSetArg(args[2], XtNforeground, timerBackgroundPixel);
+    if (appData.clockMode) {
+        markup = g_markup_printf_escaped("<span size=\"xx-large\" weight=\"heavy\" background=\"%s\" foreground=\"%s\">%s:%s%s</span>",
+                                        bgcolor, fgcolor, color, appData.logoSize && !partnerUp ? "\n" : " ", TimeString(timer));
     } else {
-       XtSetArg(args[1], XtNbackground, timerBackgroundPixel);
-       XtSetArg(args[2], XtNforeground, foregroundOrWarningColor);
+        markup = g_markup_printf_escaped("<span size=\"xx-large\" weight=\"heavy\" background=\"%s\" foreground=\"%s\">%s  </span>",
+                                        bgcolor, fgcolor, color);
     }
-
-    XtSetValues(w, args, 3);
-#endif
+    gtk_label_set_markup(GTK_LABEL(w), markup);
+    g_free(markup);
 }
 
-#ifdef TODO_GTK
-static Pixmap *clockIcons[] = { &wIconPixmap, &bIconPixmap };
-#endif
+static GdkPixbuf **clockIcons[] = { &WhiteIcon, &BlackIcon };
 
 void
 SetClockIcon (int color)
 {
-#ifdef TODO_GTK
-    Arg args[16];
-    Pixmap pm = *clockIcons[color];
-    if (iconPixmap != pm) {
-       iconPixmap = pm;
-       XtSetArg(args[0], XtNiconPixmap, iconPixmap);
-       XtSetValues(shellWidget, args, 1);
+    GdkPixbuf *pm = *clockIcons[color];
+    if (mainwindowIcon != pm) {
+        mainwindowIcon = pm;
+       gtk_window_set_icon(GTK_WINDOW(shellWidget), mainwindowIcon);
     }
-#endif
 }
 
-#ifdef TODO_GTK
-void
-DoInputCallback (caddr_t closure, int *source, XtInputId *xid)
+#define INPUT_SOURCE_BUF_SIZE 8192
+
+typedef struct {
+    CPKind kind;
+    int fd;
+    int lineByLine;
+    char *unused;
+    InputCallback func;
+    guint sid;
+    char buf[INPUT_SOURCE_BUF_SIZE];
+    VOIDSTAR closure;
+} InputSource;
+
+gboolean
+DoInputCallback(io, cond, data)
+     GIOChannel  *io;
+     GIOCondition cond;
+     gpointer    *data;
 {
-    InputSource *is = (InputSource *) closure;
+  /* read input from one of the input source (for example a chess program, ICS, etc).
+   * and call a function that will handle the input
+   */
+
     int count;
     int error;
     char *p, *q;
 
+    /* All information (callback function, file descriptor, etc) is
+     * saved in an InputSource structure
+     */
+    InputSource *is = (InputSource *) data;
+
     if (is->lineByLine) {
        count = read(is->fd, is->unused,
                     INPUT_SOURCE_BUF_SIZE - (is->unused - is->buf));
        if (count <= 0) {
            (is->func)(is, is->closure, is->buf, count, count ? errno : 0);
-           return;
+           return True;
        }
        is->unused += count;
        p = is->buf;
+       /* break input into lines and call the callback function on each
+        * line
+        */
        while (p < is->unused) {
            q = memchr(p, '\n', is->unused - p);
            if (q == NULL) break;
@@ -2455,12 +2303,16 @@ DoInputCallback (caddr_t closure, int *source, XtInputId *xid)
            (is->func)(is, is->closure, p, q - p, 0);
            p = q;
        }
+       /* remember not yet used part of the buffer */
        q = is->buf;
        while (p < is->unused) {
            *q++ = *p++;
        }
        is->unused = q;
     } else {
+      /* read maximum length of input buffer and send the whole buffer
+       * to the callback function
+       */
        count = read(is->fd, is->buf, INPUT_SOURCE_BUF_SIZE);
        if (count == -1)
          error = errno;
@@ -2468,14 +2320,17 @@ DoInputCallback (caddr_t closure, int *source, XtInputId *xid)
          error = 0;
        (is->func)(is, is->closure, is->buf, count, error);
     }
+    return True; // Must return true or the watch will be removed
 }
-#endif
 
-InputSourceRef
-AddInputSource (ProcRef pr, int lineByLine, InputCallback func, VOIDSTAR closure)
+InputSourceRef AddInputSource(pr, lineByLine, func, closure)
+     ProcRef pr;
+     int lineByLine;
+     InputCallback func;
+     VOIDSTAR closure;
 {
-#ifdef TODO_GTK
     InputSource *is;
+    GIOChannel *channel;
     ChildProc *cp = (ChildProc *) pr;
 
     is = (InputSource *) calloc(1, sizeof(InputSource));
@@ -2488,31 +2343,32 @@ AddInputSource (ProcRef pr, int lineByLine, InputCallback func, VOIDSTAR closure
        is->kind = cp->kind;
        is->fd = cp->fdFrom;
     }
-    if (lineByLine) {
-       is->unused = is->buf;
-    }
+    if (lineByLine)
+      is->unused = is->buf;
+    else
+      is->unused = NULL;
+
+   /* GTK-TODO: will this work on windows?*/
+
+    channel = g_io_channel_unix_new(is->fd);
+    g_io_channel_set_close_on_unref (channel, TRUE);
+    is->sid = g_io_add_watch(channel, G_IO_IN,(GIOFunc) DoInputCallback, is);
 
-    is->xid = XtAppAddInput(appContext, is->fd,
-                           (XtPointer) (XtInputReadMask),
-                           (XtInputCallbackProc) DoInputCallback,
-                           (XtPointer) is);
     is->closure = closure;
     return (InputSourceRef) is;
-#else
-    return (InputSourceRef) 0;
-#endif
 }
 
+
 void
-RemoveInputSource (InputSourceRef isr)
+RemoveInputSource(isr)
+     InputSourceRef isr;
 {
-#ifdef TODO_GTK
     InputSource *is = (InputSource *) isr;
 
-    if (is->xid == 0) return;
-    XtRemoveInput(is->xid);
-    is->xid = 0;
-#endif
+    if (is->sid == 0) return;
+    g_source_remove(is->sid);
+    is->sid = 0;
+    return;
 }
 
 #ifndef HAVE_USLEEP
@@ -2530,11 +2386,8 @@ FrameAlarm (int sig)
 void
 FrameDelay (int time)
 {
-#ifdef TODO_GTK
   struct itimerval delay;
 
-  XSync(xDisplay, False);
-
   if (time > 0) {
     frameWaiting = True;
     signal(SIGALRM, FrameAlarm);
@@ -2548,7 +2401,6 @@ FrameDelay (int time)
     delay.it_interval.tv_usec = delay.it_value.tv_usec = 0;
     setitimer(ITIMER_REAL, &delay, NULL);
   }
-#endif
 }
 
 #else
@@ -2559,6 +2411,8 @@ FrameDelay (int time)
 #ifdef TODO_GTK
   XSync(xDisplay, False);
 #endif
+//  gtk_main_iteration_do(False);
+
   if (time > 0)
     usleep(time * 1000);
 }
@@ -2592,3 +2446,104 @@ UpdateLogos (int displ)
     return;
 }
 
+void FileNamePopUpGTK(label, def, filter, proc, pathFlag, openMode, name, fp)
+     char *label;
+     char *def;
+     char *filter;
+     FileProc proc;
+     char *openMode;
+     Boolean pathFlag;
+     char **name;
+     FILE **fp;
+{
+  GtkWidget     *dialog;
+  GtkFileFilter *gtkfilter;
+  GtkFileFilter *gtkfilter_all;
+  char space[]     = " ";
+  char fileext[10] = "";
+  char *result     = NULL;
+  char *cp;
+
+  /* make a copy of the filter string, so that strtok can work with it*/
+  cp = strndup(filter,strlen(filter));
+
+  /* add filters for file extensions */
+  gtkfilter     = gtk_file_filter_new();
+  gtkfilter_all = gtk_file_filter_new();
+
+  /* one filter to show everything */
+  gtk_file_filter_add_pattern(gtkfilter_all, "*.*");
+  gtk_file_filter_set_name   (gtkfilter_all, "All Files");
+
+  /* add filter if present */
+  result = strtok(cp, space);
+  while( result != NULL  ) {
+    snprintf(fileext,10,"*%s",result);
+    result = strtok( NULL, space );
+    gtk_file_filter_add_pattern(gtkfilter, fileext);
+  };
+
+  /* second filter to only show what's useful */
+  gtk_file_filter_set_name (gtkfilter,filter);
+
+  if (openMode[0] == 'r')
+    {
+      dialog = gtk_file_chooser_dialog_new (label,
+                                           NULL,
+                                           GTK_FILE_CHOOSER_ACTION_OPEN,
+                                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                           GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                                           NULL);
+    }
+  else
+    {
+      dialog = gtk_file_chooser_dialog_new (label,
+                                           NULL,
+                                           GTK_FILE_CHOOSER_ACTION_SAVE,
+                                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                           GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+                                           NULL);
+      /* add filename suggestions */
+      if (strlen(def) > 0 )
+       gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), def);
+
+      //gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER (dialog),TRUE);
+    }
+
+  /* add filters */
+  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog),gtkfilter_all);
+  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog),gtkfilter);
+  /* activate filter */
+  gtk_file_chooser_set_filter (GTK_FILE_CHOOSER(dialog),gtkfilter);
+
+  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+    {
+      char *filename;
+      FILE *f;
+
+      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+
+      //see loadgamepopup
+      f = fopen(filename, openMode);
+      if (f == NULL)
+        {
+          DisplayError(_("Failed to open file"), errno);
+        }
+      else
+        {
+          /* TODO add indec */
+           *fp = f;
+           ASSIGN(*name, filename);
+           ScheduleDelayedEvent(DelayedLoad, 50);
+        }
+      g_free (filename);
+    };
+
+  gtk_widget_destroy (dialog);
+  ModeHighlight();
+
+  free(cp);
+  return;
+
+}
+