summaryrefslogtreecommitdiff
path: root/iup/src/mot/iupmot_open.c
blob: a59dbff53d3cce6a7f5d76fb5a4835d28a2ca913 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/** \file
 * \brief Motif Open/Close
 *
 * See Copyright Notice in "iup.h"
 */

#include <stdlib.h>    
#include <stdio.h>    
#include <locale.h>
#include <string.h>    

#include <Xm/Xm.h>

#include "iup.h"

#include "iup_object.h"
#include "iup_str.h"
#include "iup_drv.h"
#include "iup_globalattrib.h"

#include "iupmot_drv.h"
#include "iupmot_color.h"


/* global variables */
Widget   iupmot_appshell = 0;
Display* iupmot_display = 0;
int      iupmot_screen = 0;
Visual*  iupmot_visual = 0;
XtAppContext iupmot_appcontext = 0;

void* iupdrvGetDisplay(void)
{
  return iupmot_display;
}

void iupmotSetGlobalColorAttrib(Widget w, const char* xmname, const char* name)
{
  unsigned char r, g, b; 
  Pixel color;

  XtVaGetValues(w, xmname, &color, NULL);
  iupmotColorGetRGB(color, &r, &g, &b);

  iupGlobalSetDefaultColorAttrib(name, r, g, b);
}

int iupdrvOpen(int *argc, char ***argv)
{
  IupSetGlobal("DRIVER", "Motif");

  /* XtSetLanguageProc(NULL, NULL, NULL); 
     Removed to avoid invalid locale in modern Linux that set LANG=en_US.UTF-8 */

  /* We do NOT use XtVaOpenApplication because it crashes when using internal dummy argc and argv.
     iupmot_appshell = XtVaOpenApplication(&iupmot_appcontext, "Iup", NULL, 0, argc, *argv, NULL,
                                           sessionShellWidgetClass, NULL); */

  XtToolkitInitialize();

  iupmot_appcontext = XtCreateApplicationContext();

  iupmot_display = XtOpenDisplay(iupmot_appcontext, NULL, NULL, "Iup", NULL, 0, argc, *argv);   
  if (!iupmot_display)
  {
    fprintf (stderr, "IUP error: cannot open display.\n");
    return IUP_ERROR;
  }

  iupmot_appshell = XtAppCreateShell(NULL, "Iup", sessionShellWidgetClass, iupmot_display, NULL, 0);
  if (!iupmot_appshell)
  {
    fprintf(stderr, "IUP error: cannot create shell.\n");
    return IUP_ERROR;
  }
  IupSetGlobal("APPSHELL", (char*)iupmot_appshell);

  IupStoreGlobal("SYSTEMLANGUAGE", setlocale(LC_ALL, NULL));

  iupmot_screen  = XDefaultScreen(iupmot_display);

  IupSetGlobal("XDISPLAY", (char*)iupmot_display);
  IupSetGlobal("XSCREEN", (char*)iupmot_screen);

  /* screen depth can be 8bpp, but canvas can be 24bpp */
  {
    XVisualInfo vinfo;
    if (XMatchVisualInfo(iupmot_display, iupmot_screen, 24, TrueColor, &vinfo) ||
        XMatchVisualInfo(iupmot_display, iupmot_screen, 16, TrueColor, &vinfo))
    {
      iupmot_visual = vinfo.visual;
      IupSetGlobal("TRUECOLORCANVAS", "YES");
    }
    else
    {
      iupmot_visual = DefaultVisual(iupmot_display, iupmot_screen);
      IupSetGlobal("TRUECOLORCANVAS", "NO");
    }
  }

  /* driver system version */
  {
    int major = xmUseVersion/1000;
    int minor = xmUseVersion - major * 1000;
    IupSetfAttribute(NULL, "MOTIFVERSION", "%d.%d", major, minor);
    IupSetfAttribute(NULL, "MOTIFNUMBER", "%d", (XmVERSION * 1000 + XmREVISION * 100 + XmUPDATE_LEVEL));

    IupSetGlobal("XSERVERVENDOR", ServerVendor(iupmot_display));
    IupSetfAttribute(NULL, "XVENDORRELEASE", "%d", VendorRelease(iupmot_display));
  }

  iupmotColorInit();

  /* dialog background color */
  {
    iupmotSetGlobalColorAttrib(iupmot_appshell, XmNbackground, "DLGBGCOLOR");
    iupGlobalSetDefaultColorAttrib("DLGFGCOLOR", 0, 0, 0);
    IupSetGlobal("_IUP_RESET_DLGBGCOLOR", "YES");  /* will update the DLGFGCOLOR when the first dialog is mapped */

    iupGlobalSetDefaultColorAttrib("TXTBGCOLOR", 255, 255, 255);
    iupGlobalSetDefaultColorAttrib("TXTFGCOLOR", 0, 0, 0);
    IupSetGlobal("_IUP_RESET_TXTCOLORS", "YES");   /* will update the TXTCOLORS when the first text or list is mapped */
  }

  if (getenv("IUP_DEBUG"))
    XSynchronize(iupmot_display, 1);

  return IUP_NOERROR;
}

void iupdrvClose(void)
{ 
  iupmotColorFinish();
  iupmotTipsFinish();

  if (iupmot_appshell)
    XtDestroyWidget(iupmot_appshell);

  if (iupmot_appcontext)
    XtDestroyApplicationContext(iupmot_appcontext);
}