Back to index

plt-scheme  4.2.1
AppMain.cc
Go to the documentation of this file.
00001 /*                                                      -*- C++ -*-
00002  *
00003  * Purpose: wxWindows application and main loop
00004  *
00005  * Authors: Markus Holzem and Julian Smart
00006  *
00007  * Copyright: (C) 2004-2009 PLT Scheme Inc.
00008  * Copyright: (C) 1995, AIAI, University of Edinburgh (Julian)
00009  * Copyright: (C) 1995, GNU (Markus)
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00027 #ifdef __GNUG__
00028 #pragma implementation "AppMain.h"
00029 #endif
00030 
00031 #define  Uses_XtIntrinsic
00032 #define  Uses_wxApp
00033 #define  Uses_wxGDI
00034 #define  Uses_wxPrintSetup
00035 #define  Uses_wxTypeTree
00036 #define  Uses_wxMenu
00037 #define  Uses_wxWindowDC
00038 #include "wx.h"
00039 #include "widgets.h" // for X11/StringDefs.h
00040 #include "wxAllocColor.h"
00041 
00042 #ifdef WX_USE_XFT
00043 # include <X11/Xft/Xft.h>
00044 #endif
00045 
00046 #ifndef NO_XKB_LIB_PRESENT
00047 # include <X11/XKBlib.h>
00048 #endif
00049 
00050 extern void wxsRememberDisplay(char *str);
00051 
00052 Visual *wxAPP_VISUAL;
00053 int wx_visual_depth;
00054 Colormap wx_default_colormap;
00055 unsigned long wx_white_pixel, wx_black_pixel;
00056 
00057 int wx_single_instance = 0;
00058 
00059 //-----------------------------------------------------------------------------
00060 // wxApp implementation
00061 //-----------------------------------------------------------------------------
00062 
00063 wxApp::wxApp()
00064 {
00065   __type = wxTYPE_APP;
00066   
00067   // no application and/or application not initialized
00068   initialized = FALSE;
00069   wxREGGLOB(wxTheApp);
00070   wxTheApp = this;
00071 }
00072 
00073 void wxApp::Dispatch(void)
00074 {
00075   wxDoNextEvent();
00076 }
00077 
00078 int wxApp::MainLoop(void)
00079 {
00080     keep_going = TRUE;
00081     while (keep_going) {
00082       wxDoEvents();
00083     }
00084     return 0;
00085 }
00086 
00087 Bool wxApp::Pending(void)
00088 {
00089     XFlush(wxAPP_DISPLAY);
00090     return (wxEventReady());
00091 }
00092 
00093 //-----------------------------------------------------------------------------
00094 // The main function 
00095 //-----------------------------------------------------------------------------
00096 
00097 #include <X11/Shell.h>
00098 
00099 static void wxCommonInit(void);
00100 
00101 void wxInitNewToplevel(void)
00102 {
00103   Widget tl;
00104 
00105   tl = XtVaAppCreateShell(wxAPP_NAME, wxAPP_CLASS,
00106                        applicationShellWidgetClass,
00107                        wxAPP_DISPLAY,
00108                        XtNvisual, wxAPP_VISUAL,
00109                        XtNdepth, wx_visual_depth,
00110                        XtNcolormap, wx_default_colormap,
00111                        NULL);
00112 
00113   wxPutAppToplevel(tl);
00114 }
00115 
00116 typedef struct {
00117   char *flag;
00118   int arg_count;
00119 } X_flag_entry;
00120 
00121 #define SINGLE_INSTANCE "-singleInstance"
00122 
00123 X_flag_entry X_flags[] = {
00124   { "-display", 1 },
00125   { "-geometry", 1 },
00126   { "-bg", 1 },
00127   { "-background", 1 },
00128   { "-fg", 1 },
00129   { "-foreground", 1 },
00130   { "-fn", 1 },
00131   { "-font", 1 },
00132   { "-iconic", 0 },
00133   { "-name", 1 },
00134   { "-rv", 0 },
00135   { "-reverse", 0 },
00136   { "+rv", 0 },
00137   { "-selectionTimeout", 1 },
00138   { "-synchronous", 0 },
00139   { "-title", 1 },
00140   { "-xnllanguage", 1 },
00141   { "-xrm", 1 },
00142   { SINGLE_INSTANCE, 0},
00143   { NULL, 0 }
00144 };
00145 
00146 static int filter_x_readable(char **argv, int argc, char **x_display_str)
00147 {
00148   int pos = 1, i;
00149 
00150   while (pos < argc) {
00151     for (i = 0; X_flags[i].flag; i++) {
00152       if (!strcmp(X_flags[i].flag, argv[pos]))
00153        break;
00154     }
00155 
00156     if (!X_flags[i].flag)
00157       return pos;
00158     else {
00159       int newpos = pos + X_flags[i].arg_count + 1;
00160       if (newpos > argc) {
00161        printf("%s: X Window System flag \"%s\" expects %d arguments, %d provided\n",
00162               argv[0], argv[pos], X_flags[i].arg_count, argc - pos - 1);
00163        exit(-1);
00164       }
00165       if (!strcmp(argv[pos], "-display")) {
00166        *x_display_str = argv[pos + 1];
00167       }
00168       pos = newpos;
00169     }
00170   }
00171 
00172   return pos;
00173 }
00174 
00175 int wxEntry(int argc, char *argv[])
00176 {
00177   int xargc, ate;
00178   char *x_display_str = NULL;
00179 
00180   if (!wxTheApp) {
00181     wxFatalError("You have to define an instance of wxApp!");
00182   }
00183 
00184   // init private and public data
00185   /* Set if not set... */
00186   if (!wxAPP_CLASS) {
00187     wxREGGLOB(wxAPP_CLASS);
00188     wxAPP_CLASS = wxFileNameFromPath(argv[0]);
00189   }
00190   if (!wxAPP_NAME) {
00191     wxREGGLOB(wxAPP_NAME);
00192     wxAPP_NAME  = wxFileNameFromPath(argv[0]);
00193   }
00194 
00195   xargc = filter_x_readable(argv, argc, &x_display_str);
00196   ate = xargc - 1;
00197 
00198   if (!x_display_str)
00199     x_display_str = getenv("DISPLAY");
00200   
00201   /* Remember -display or DISPLAY, in case someone needs it: */
00202   wxsRememberDisplay(x_display_str);
00203 
00204   XtToolkitInitialize();
00205   wxAPP_CONTEXT = XtCreateApplicationContext();
00206   wxAPP_DISPLAY = XtOpenDisplay(wxAPP_CONTEXT, NULL, NULL,NULL,
00207                             NULL, 0,
00208                             &xargc, argv); // command line arguments
00209 
00210   if (!wxAPP_DISPLAY) {
00211     if (!x_display_str) {
00212       printf("DISPLAY environment variable not set and no -display argument\n");
00213     } else {
00214       printf("Cannot open display: %s\n", x_display_str);
00215     }
00216     exit(1);
00217   }
00218 
00219 
00220   if ((xargc > 1) && !strcmp("-singleInstance", argv[1])) {
00221     wx_single_instance = 1;
00222     --xargc;
00223     if (xargc > 1) {
00224       argv[1] = argv[2];
00225     }
00226   }
00227 
00228   if (xargc != 1) {
00229     printf("%s: standard X Window System flag \"%s\" was rejected\n",
00230           argv[0], argv[1]);
00231     exit(1);
00232   }
00233   
00234   wxAPP_SCREEN = DefaultScreenOfDisplay(wxAPP_DISPLAY);
00235   wxAPP_VISUAL = DefaultVisualOfScreen(wxAPP_SCREEN);
00236   wx_default_colormap = DefaultColormapOfScreen(wxAPP_SCREEN);
00237   wx_black_pixel = BlackPixel(wxAPP_DISPLAY, DefaultScreen(wxAPP_DISPLAY));
00238   wx_white_pixel = WhitePixel(wxAPP_DISPLAY, DefaultScreen(wxAPP_DISPLAY));
00239 
00240   /* Use 24-bit TrueColor visual, if possible */
00241   {
00242     XVisualInfo *vi, vi_tmpl, vi2;
00243     int n;
00244     
00245     vi_tmpl.visualid = XVisualIDFromVisual(wxAPP_VISUAL);
00246     vi = XGetVisualInfo(wxAPP_DISPLAY, VisualIDMask, &vi_tmpl, &n);
00247     wx_visual_depth = vi->depth;
00248 
00249     if ((vi->c_class != TrueColor) || (vi->depth < 24)) {
00250       if (XMatchVisualInfo(wxAPP_DISPLAY, DefaultScreen(wxAPP_DISPLAY), 
00251                         24, TrueColor, &vi2)) {
00252        wxAPP_VISUAL = vi2.visual;
00253        wx_visual_depth = 24;
00254        wx_default_colormap = XCreateColormap(wxAPP_DISPLAY, 
00255                                          RootWindow(wxAPP_DISPLAY,
00256                                                   DefaultScreen(wxAPP_DISPLAY)),
00257                                          wxAPP_VISUAL, 
00258                                          AllocNone);
00259 
00260        {
00261          XColor xcol;
00262          xcol.red = xcol.blue = xcol.green = 0;
00263          wxAllocColor(wxAPP_DISPLAY, wx_default_colormap, &xcol);
00264          wx_black_pixel = xcol.pixel;
00265          xcol.red = xcol.blue = xcol.green = 0xFFFF;
00266          wxAllocColor(wxAPP_DISPLAY, wx_default_colormap, &xcol);
00267          wx_white_pixel = xcol.pixel;
00268        }
00269       }
00270     }
00271 
00272     XFree(vi);
00273   }
00274 
00275   wxInitNewToplevel();
00276 
00277   for (int i = ate + 1; i < argc; i++) {
00278     argv[i - ate] = argv[i];
00279   }
00280   argc -= ate;
00281   
00282   wxTheApp->argc = argc;
00283   wxTheApp->argv = argv;
00284 
00285   // initialize global data
00286   wxCommonInit();
00287   
00288   wxTheApp->OnInit();
00289   
00290   return 0;
00291 }
00292 
00293 //-----------------------------------------------------------------------------
00294 // initialize and destroy global data
00295 //-----------------------------------------------------------------------------
00296 
00297 extern int wxGetPreference(const char *name, int *len);
00298 extern int wxGetPreference(const char *name, char *res, long len);
00299 
00300 void wxCommonInit(void)
00301 {
00302     Bool supported;
00303     int fsize;
00304   
00305     wxAPP_ROOT           = RootWindow(wxAPP_DISPLAY, DefaultScreen(wxAPP_DISPLAY));
00306 
00307 #ifdef WX_USE_XFT
00308     if (wxXRenderHere())
00309       XftInit(NULL);
00310 #endif
00311   
00312     wxREGGLOB(wxAPP_COLOURMAP);
00313     wxAPP_COLOURMAP = DEBUG_NEW wxColourMap(FALSE); // default colourmap
00314 
00315     wxREGGLOB(wxBuffer);
00316     wxBuffer = new WXGC_ATOMIC char[BUFSIZ+512];
00317 
00318     wxREGGLOB(wxResourceCache);
00319     wxResourceCache = new wxList(wxKEY_STRING);
00320 
00321 #ifndef NO_XKB_LIB_PRESENT
00322     XkbSetDetectableAutoRepeat(wxAPP_DISPLAY, 1, &supported);
00323 #endif
00324 
00325     wxInitPopupMgr();
00326 
00327     wxREGGLOB(wxAllTypes);
00328     wxAllTypes = new wxTypeTree;
00329 
00330     wxInitializeFontNameDirectory();
00331 
00332     wxInitializePrintSetupData();
00333     wxREGGLOB(wxThePrintPaperDatabase);
00334     wxThePrintPaperDatabase = DEBUG_NEW wxPrintPaperDatabase;
00335     wxThePrintPaperDatabase->CreateDatabase();
00336 
00337     wxREGGLOB(wxTheColourDatabase);
00338     wxTheColourDatabase = DEBUG_NEW wxColourDatabase();
00339     wxREGGLOB(wxThePenList);
00340     wxThePenList = DEBUG_NEW wxPenList();
00341     wxREGGLOB(wxTheBrushList);
00342     wxTheBrushList = DEBUG_NEW wxBrushList();
00343     wxREGGLOB(wxTheFontList);
00344     wxTheFontList = DEBUG_NEW wxFontList();
00345 
00346 #ifdef WX_USE_XFT
00347     if (wxXRenderHere())
00348       fsize = 10;
00349     else
00350       fsize = 12;
00351 #else
00352     fsize = 12;
00353 #endif
00354     wxGetPreference("controlFontSize", &fsize);
00355 
00356     wxREGGLOB(wxNORMAL_FONT);
00357     wxNORMAL_FONT = DEBUG_NEW wxFont (fsize, wxMODERN, wxNORMAL, wxNORMAL);
00358     wxREGGLOB(wxSMALL_FONT);
00359     wxSMALL_FONT = DEBUG_NEW wxFont (10, wxSWISS, wxNORMAL, wxNORMAL);
00360     wxREGGLOB(wxITALIC_FONT);
00361     wxITALIC_FONT = DEBUG_NEW wxFont (fsize, wxROMAN, wxITALIC, wxNORMAL);
00362     wxREGGLOB(wxSWISS_FONT);
00363     wxSWISS_FONT = DEBUG_NEW wxFont (fsize, wxSWISS, wxNORMAL, wxNORMAL);
00364     wxREGGLOB(wxSYSTEM_FONT);
00365     wxSYSTEM_FONT = DEBUG_NEW wxFont (fsize, wxSYSTEM, wxNORMAL, wxNORMAL);
00366 
00367     wxREGGLOB(wxRED_PEN);
00368     wxRED_PEN = DEBUG_NEW wxPen ("RED", 0, wxSOLID);
00369     wxREGGLOB(wxCYAN_PEN);
00370     wxCYAN_PEN = DEBUG_NEW wxPen ("CYAN", 0, wxSOLID);
00371     wxREGGLOB(wxGREEN_PEN);
00372     wxGREEN_PEN = DEBUG_NEW wxPen ("GREEN", 0, wxSOLID);
00373     wxREGGLOB(wxBLACK_PEN);
00374     wxBLACK_PEN = DEBUG_NEW wxPen ("BLACK", 0, wxSOLID);
00375     wxREGGLOB(wxWHITE_PEN);
00376     wxWHITE_PEN = DEBUG_NEW wxPen ("WHITE", 0, wxSOLID);
00377     wxREGGLOB(wxTRANSPARENT_PEN);
00378     wxTRANSPARENT_PEN = DEBUG_NEW wxPen ("BLACK", 0, wxTRANSPARENT);
00379     wxREGGLOB(wxBLACK_DASHED_PEN);
00380     wxBLACK_DASHED_PEN = DEBUG_NEW wxPen ("BLACK", 0, wxSHORT_DASH);
00381     wxREGGLOB(wxGREY_PEN);
00382     wxGREY_PEN = DEBUG_NEW wxPen ("GRAY", 0, wxSOLID);
00383     wxREGGLOB(wxMEDIUM_GREY_PEN);
00384     wxMEDIUM_GREY_PEN = DEBUG_NEW wxPen ("MEDIUM GRAY", 0, wxSOLID);
00385     wxREGGLOB(wxLIGHT_GREY_PEN);
00386     wxLIGHT_GREY_PEN = DEBUG_NEW wxPen ("LIGHT GRAY", 0, wxSOLID);
00387 
00388     wxBLACK_PEN->Lock(1);
00389     wxWHITE_PEN->Lock(1);
00390 
00391     wxREGGLOB(wxBLUE_BRUSH);
00392     wxBLUE_BRUSH = DEBUG_NEW wxBrush ("BLUE", wxSOLID);
00393     wxREGGLOB(wxGREEN_BRUSH);
00394     wxGREEN_BRUSH = DEBUG_NEW wxBrush ("GREEN", wxSOLID);
00395     wxREGGLOB(wxWHITE_BRUSH);
00396     wxWHITE_BRUSH = DEBUG_NEW wxBrush ("WHITE", wxSOLID);
00397     wxREGGLOB(wxBLACK_BRUSH);
00398     wxBLACK_BRUSH = DEBUG_NEW wxBrush ("BLACK", wxSOLID);
00399     wxREGGLOB(wxTRANSPARENT_BRUSH);
00400     wxTRANSPARENT_BRUSH = DEBUG_NEW wxBrush ("BLACK", wxTRANSPARENT);
00401     wxREGGLOB(wxCYAN_BRUSH);
00402     wxCYAN_BRUSH = DEBUG_NEW wxBrush ("CYAN", wxSOLID);
00403     wxREGGLOB(wxRED_BRUSH);
00404     wxRED_BRUSH = DEBUG_NEW wxBrush ("RED", wxSOLID);
00405     wxREGGLOB(wxGREY_BRUSH);
00406     wxGREY_BRUSH = DEBUG_NEW wxBrush ("GRAY", wxSOLID);
00407     wxREGGLOB(wxMEDIUM_GREY_BRUSH);
00408     wxMEDIUM_GREY_BRUSH = DEBUG_NEW wxBrush ("MEDIUM GRAY", wxSOLID);
00409     wxREGGLOB(wxLIGHT_GREY_BRUSH);
00410     wxLIGHT_GREY_BRUSH = DEBUG_NEW wxBrush ("LIGHT GRAY", wxSOLID);
00411 
00412     wxBLACK_BRUSH->Lock(1);
00413     wxWHITE_BRUSH->Lock(1);
00414 
00415     wxREGGLOB(wxBLACK);
00416     wxBLACK = DEBUG_NEW wxColour ("BLACK");
00417     wxREGGLOB(wxWHITE);
00418     wxWHITE = DEBUG_NEW wxColour ("WHITE");
00419     wxREGGLOB(wxGREY);
00420     // wxGREY = DEBUG_NEW wxColour (214, 214, 214); /* aka "gray 84" */
00421     wxGREY = DEBUG_NEW wxColour (220, 218, 213); /* GTK2 background */
00422     wxREGGLOB(wxBUTTON_COLOR);
00423     // wxBUTTON_COLOR = DEBUG_NEW wxColour (224, 224, 224);
00424     wxBUTTON_COLOR = wxGREY;
00425     wxREGGLOB(wxRED);
00426     wxRED = DEBUG_NEW wxColour ("RED");
00427     wxREGGLOB(wxBLUE);
00428     wxBLUE = DEBUG_NEW wxColour ("BLUE");
00429     wxREGGLOB(wxGREEN);
00430     wxGREEN = DEBUG_NEW wxColour ("GREEN");
00431     wxREGGLOB(wxCYAN);
00432     wxCYAN = DEBUG_NEW wxColour ("CYAN");
00433     wxREGGLOB(wxLIGHT_GREY);
00434     wxLIGHT_GREY = DEBUG_NEW wxColour ("LIGHT GRAY");
00435 
00436     wxWHITE_PIXEL = wxWHITE->GetPixel(wxAPP_COLOURMAP);
00437     wxBLACK_PIXEL = wxBLACK->GetPixel(wxAPP_COLOURMAP);;
00438     wxGREY_PIXEL = wxGREY->GetPixel(wxAPP_COLOURMAP);;
00439     wxBUTTON_PIXEL = wxBUTTON_COLOR->GetPixel(wxAPP_COLOURMAP);;
00440     {
00441       wxColour *c;
00442       int r, g, b;
00443       r = wxGREY->Red();
00444       g = wxGREY->Green();
00445       b = wxGREY->Blue();
00446       c = new wxColour((int)(r * 0.85),
00447                      (int)(g * 0.85),
00448                      (int)(b * 0.85));
00449       wxDARK_GREY_PIXEL = c->GetPixel(wxAPP_COLOURMAP);
00450     }
00451     {
00452       int r, g, b, i, c;
00453 
00454       r = 75;
00455       g = 105;
00456       b = 131;
00457       if (wxGetPreference("hiliteColor", wxBuffer, 50)) {
00458        wxBuffer[50]= 0;
00459        if (strlen(wxBuffer) == 6) {
00460          for (i = 0; wxBuffer[i]; i++) {
00461            c = wxBuffer[i];
00462            if ((c >= '0') && (c <= '9'))
00463              wxBuffer[i] = c - '0';
00464            else if ((c >= 'a') && (c <= 'f'))
00465              wxBuffer[i] = c - 'a' + 10;
00466            else if ((c >= 'A') && (c <= 'F'))
00467              wxBuffer[i] = c - 'A' + 10;
00468            else
00469              break;
00470          }
00471          if (i == 6) {
00472            r = (wxBuffer[0] << 4) + wxBuffer[1];
00473            g = (wxBuffer[2] << 4) + wxBuffer[3];
00474            b = (wxBuffer[4] << 4) + wxBuffer[5];
00475          }
00476        }
00477       }
00478       wxREGGLOB(wxCTL_HILITE);
00479       wxCTL_HILITE = new wxColour(r, g, b);
00480       wxCTL_HIGHLIGHT_PIXEL = wxCTL_HILITE->GetPixel(wxAPP_COLOURMAP);
00481     }
00482 
00483     wxREGGLOB(wxSTANDARD_CURSOR);
00484     wxSTANDARD_CURSOR = DEBUG_NEW wxCursor (wxCURSOR_ARROW);
00485     wxREGGLOB(wxHOURGLASS_CURSOR);
00486     wxHOURGLASS_CURSOR = DEBUG_NEW wxCursor (wxCURSOR_WAIT);
00487     wxREGGLOB(wxCROSS_CURSOR);
00488     wxCROSS_CURSOR = DEBUG_NEW wxCursor (wxCURSOR_CROSS);
00489     wxREGGLOB(wxIBEAM_CURSOR);
00490     wxIBEAM_CURSOR = DEBUG_NEW wxCursor (wxCURSOR_IBEAM);
00491     wxREGGLOB(wxBLANK_CURSOR);
00492     wxBLANK_CURSOR = DEBUG_NEW wxCursor (wxCURSOR_BLANK);
00493 }
00494 
00495 static int hilite_border = -1;
00496 
00497 extern "C" int wxUseMenuHiliteBorder()
00498 {
00499   if (hilite_border < 0) {
00500     int on;
00501     hilite_border = 0;
00502     if (wxGetBoolPreference("hiliteMenuBorder", &on)) {
00503       if (on)
00504        hilite_border = 1;
00505     }
00506   }
00507   return hilite_border;
00508 }