1 /***************************************************************************
2                        plotting.cpp  -  GDL routines for plotting
3                              -------------------
4     begin                : July 22 2002
5     copyright            : (C) 2002-2011 by Marc Schellens et al.
6     email                : m_schellens@users.sf.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "includefirst.hpp"
19 #include "plotting.hpp"
20 
21 
22 namespace lib {
23 
24   using namespace std;
25 
window(EnvT * e)26   void window(EnvT* e) {
27     GraphicsDevice* actDevice = GraphicsDevice::GetDevice();
28     int maxWin = actDevice->MaxWin();
29     if (maxWin == 0)
30       e->Throw("Routine is not defined for current graphics device.");
31 
32     SizeT nParam = e->NParam();
33 
34     DLong wIx = 0;
35     static int FREEIx = e->KeywordIx("FREE");
36     static int TITLEIx = e->KeywordIx("TITLE");
37     static int XPOSIx = e->KeywordIx("XPOS");
38     static int YPOSIx = e->KeywordIx("YPOS");
39     static int XSIZEIx = e->KeywordIx("XSIZE");
40     static int YSIZEIx = e->KeywordIx("YSIZE");
41     static int RETAINIx = e->KeywordIx("RETAIN");
42     static int PIXMAPIx = e->KeywordIx("PIXMAP");
43     if (e->KeywordSet(FREEIx)) {
44       wIx = actDevice->WAddFree();
45       if (wIx == -1)
46         e->Throw("No more window handles left.");
47     } else {
48       if (nParam == 1) {
49         e->AssureLongScalarPar(0, wIx);
50         if (wIx < 0 || wIx >= actDevice->MaxNonFreeWin()) //to comply with IDL
51           e->Throw("Window number " + i2s(wIx) +
52           " out of range or no more windows.");
53       }
54     }
55 
56     DString title;
57     if (e->KeywordPresentAndDefined(TITLEIx)) {
58       //    if (e->KeywordPresent(TITLEIx)) {
59       e->AssureStringScalarKWIfPresent(TITLEIx, title);
60     } else {
61       title = "GDL " + i2s(wIx);
62     }
63 
64     DLong xPos = -1, yPos = -1; //NOTE: xPos=-1 and yPos=-1 are when XPOS and YPOS options were not used!
65     e->AssureLongScalarKWIfPresent(XPOSIx, xPos);
66     e->AssureLongScalarKWIfPresent(YPOSIx, yPos);
67 
68     DLong xSize, ySize;
69 
70     actDevice->DefaultXYSize(&xSize, &ySize);
71 
72     e->AssureLongScalarKWIfPresent(XSIZEIx, xSize);
73     e->AssureLongScalarKWIfPresent(YSIZEIx, ySize);
74 
75     int debug = 0;
76     if (debug) {
77       cout << "input values :" << endl;
78       cout << "xPos/yPos   :" << xPos << " " << yPos << endl;
79       cout << "xSize/ySize :" << xSize << " " << ySize << endl;
80     }
81 
82     if (xSize < 0) xSize = 10000;
83     if (ySize < 0) ySize = 10000;
84 
85 
86     //NOTE: xPos=-1 and yPos=-1 are when XPOS and YPOS options were not used!
87 
88     // AC 2014/02/26: this seems to be not need as long as xSize/ySize > 0
89     // which is done above
90 
91     //    if( xSize <= 0 || ySize <= 0 || xPos < -1 || yPos < -1)
92     //      e->Throw(  "Unable to create window "
93     //		 "(BadValue (integer parameter out of range for operation)).");
94 
95     DLong retainType = 0;
96     if (e->KeywordPresent(RETAINIx)) {
97       e->AssureLongScalarKWIfPresent(RETAINIx, retainType);
98     }
99     bool success = actDevice->SetBackingStore(retainType);
100     bool hide = e->KeywordSet(PIXMAPIx);
101     success = actDevice->WOpen(wIx, title, xSize, ySize, xPos, yPos, hide);
102     if (!success)
103       e->Throw("Unable to create window.");
104     if (e->KeywordSet(PIXMAPIx)) {
105       success = actDevice->Hide();
106     } else success = actDevice->UnsetFocus();
107     actDevice->GetStream()->DefaultBackground();
108     actDevice->GetStream()->Clear();
109 
110   }
111 
wset(EnvT * e)112   void wset(EnvT* e) {
113     GraphicsDevice* actDevice = GraphicsDevice::GetDevice();
114     int maxWin = actDevice->MaxWin();
115     if (maxWin == 0)
116       e->Throw("Routine is not defined for current graphics device.");
117 
118     SizeT nParam = e->NParam();
119     DLong wIx = 0;
120     if (nParam != 0) {
121       e->AssureLongScalarPar(0, wIx);
122     }
123     //special case for "WSET,-1"
124     if (wIx == -1) {
125       wIx = actDevice->GetNonManagedWidgetActWin();
126       if (wIx == -1) {//set !D.WINDOW to -1: no windows available, only *managed* widgets are eventually present.
127         actDevice->SetActWin(wIx); //this simply sets !D.WINDOW to -1.
128         return;
129       }
130     }
131     if (wIx == 0) {
132       if (actDevice->ActWin() == -1) {
133         DLong xSize, ySize;
134         actDevice->DefaultXYSize(&xSize, &ySize);
135         bool success = actDevice->WOpen(0, "GDL 0", xSize, ySize, -1, -1, false);
136         if (!success)
137           e->Throw("Unable to create window.");
138         //        success = actDevice->UnsetFocus();  // following a deviceXXX->WOpen
139         //FIXME: ADD support for RETAIN (BackingSTORE))
140         actDevice->GetStream()->DefaultBackground();
141         actDevice->GetStream()->Clear();
142         return;
143       }
144     }
145 
146     bool success = actDevice->WSet(wIx);
147     if (!success)
148       e->Throw("Window is closed and unavailable.");
149   }
150 
wshow(EnvT * e)151   void wshow(EnvT* e) {
152     GraphicsDevice* actDevice = GraphicsDevice::GetDevice();
153     int maxWin = actDevice->MaxWin();
154     if (maxWin == 0)
155       e->Throw("Routine is not defined for current graphics device.");
156 
157     SizeT nParam = e->NParam();
158     DLong wIx = 0;
159     if (nParam != 0) e->AssureLongScalarPar(0, wIx);
160     else wIx = actDevice->ActWin();
161 
162     // note by AC on 2012-Aug-16
163     // On the system I tested (Ubuntu 10.4), I was not able to have
164     // the expected SHOW behavior, with IDL 7.0 and GDL :(
165     // Help/suggestions welcome
166     // works for me (GD) on mandriva 2010.2
167     bool show = true;
168     if (nParam == 2) {
169       DIntGDL *showval = e->GetParAs<DIntGDL>(1);
170       show = (*showval)[0] != 0;
171     }
172 
173     int iconic = -1; //signals absent parameter
174     static int ICONICIx = e->KeywordIx("ICONIC");
175     if (e->KeywordPresent(ICONICIx)) iconic = e->KeywordSet(ICONICIx);
176 
177     if (!actDevice->WShow(wIx, show, iconic))
178       e->Throw("Window number " + i2s(wIx) + " out of range or no more windows.");
179   }
180 
wdelete(EnvT * e)181   void wdelete(EnvT* e) {
182     GraphicsDevice* actDevice = GraphicsDevice::GetDevice();
183     int maxWin = actDevice->MaxWin();
184     if (maxWin == 0)
185       e->Throw("Routine is not defined for current graphics device.");
186 
187     SizeT nParam = e->NParam();
188     if (nParam == 0) {
189       DLong wIx = actDevice->ActWin();
190       bool success = actDevice->WDelete(wIx);
191       if (!success)
192         e->Throw("Window number " + i2s(wIx) +
193         " invalid or no more windows.");
194       return;
195     }
196 
197     for (SizeT i = 0; i < nParam; i++) {
198       DLong wIx;
199       e->AssureLongScalarPar(i, wIx);
200       bool success = actDevice->WDelete(wIx);
201       if (!success)
202         e->Throw("Window number " + i2s(wIx) +
203         " invalid or no more windows.");
204     }
205   }
206 
207 
208 
209 } // namespace
210