Statistics
| Branch: | Revision:

root / src / tkenv / layouterenv.cc @ e1750c09

History | View | Annotate | Download (5.03 KB)

1
//==========================================================================
2
//  LAYOUTERENV.CC - part of
3
//
4
//                     OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//  Implementation of
8
//    inspectors
9
//
10
//==========================================================================
11

    
12
/*--------------------------------------------------------------*
13
  Copyright (C) 1992-2008 Andras Varga
14
  Copyright (C) 2006-2008 OpenSim Ltd.
15

16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
18
*--------------------------------------------------------------*/
19

    
20
#include <string.h>
21
#include <stdlib.h>
22
#include <math.h>
23
#include <assert.h>
24

    
25
#include "layouterenv.h"
26
#include "tklib.h"
27
#include "modinsp.h"
28
#include "cmodule.h"
29
#include "cdisplaystring.h"
30

    
31
USING_NAMESPACE
32

    
33

    
34
TGraphLayouterEnvironment::TGraphLayouterEnvironment(Tcl_Interp *interp, cModule *parentModule, const cDisplayString& displayString)
35
    : displayString(displayString)
36
{
37
    this->interp = interp;
38
    this->parentModule = parentModule;
39
    widgetToGrab = NULL;
40
    canvas = NULL;
41
    interp = NULL;
42

    
43
    gettimeofday(&beginTime, NULL);
44
    gettimeofday(&lastCheck, NULL);
45
    grabActive = false;
46
}
47

    
48
bool TGraphLayouterEnvironment::getBoolParameter(const char *tagName, int index, bool defaultValue)
49
{
50
    return resolveBoolDispStrArg(displayString.getTagArg(tagName, index), parentModule, defaultValue);
51
}
52

    
53
long TGraphLayouterEnvironment::getLongParameter(const char *tagName, int index, long defaultValue)
54
{
55
    return resolveLongDispStrArg(displayString.getTagArg(tagName, index), parentModule, defaultValue);
56
}
57

    
58
double TGraphLayouterEnvironment::getDoubleParameter(const char *tagName, int index, double defaultValue)
59
{
60
    return resolveDoubleDispStrArg(displayString.getTagArg(tagName, index), parentModule, defaultValue);
61
}
62

    
63
void TGraphLayouterEnvironment::clearGraphics()
64
{
65
    if (inspected())
66
    {
67
        Tcl_VarEval(interp, canvas, " delete all", NULL);
68
    }
69
}
70

    
71
void TGraphLayouterEnvironment::showGraphics(const char *text)
72
{
73
    if (inspected())
74
    {
75
        Tcl_VarEval(interp, canvas, " raise node", NULL);
76
        Tcl_VarEval(interp, "layouter_debugDraw_finish ", canvas, " {", text, "}", NULL);
77
    }
78
}
79

    
80
void TGraphLayouterEnvironment::drawText(double x, double y, const char *text, const char *tags, const char *color)
81
{
82
    if (inspected())
83
    {
84
        char coords[100];
85
        sprintf(coords,"%d %d", (int)x, (int)y);
86
        Tcl_VarEval(interp, canvas, " create text ", coords, " -text ", text, " -fill ", color, " -tag ", tags, NULL);
87
    }
88
}
89

    
90
void TGraphLayouterEnvironment::drawLine(double x1, double y1, double x2, double y2, const char *tags, const char *color)
91
{
92
    if (inspected())
93
    {
94
        char coords[100];
95
        sprintf(coords,"%d %d %d %d", (int)x1, (int)y1, (int)x2, (int)y2);
96
        Tcl_VarEval(interp, canvas, " create line ", coords, " -fill ", color, " -tag ", tags, NULL);
97
    }
98
}
99

    
100
void TGraphLayouterEnvironment::drawRectangle(double x1, double y1, double x2, double y2, const char *tags, const char *color)
101
{
102
    if (inspected())
103
    {
104
        char coords[100];
105
        sprintf(coords,"%d %d %d %d", (int)x1, (int)y1, (int)x2, (int)y2);
106
        Tcl_VarEval(interp, canvas, " create rect ", coords, " -outline ", color, " -tag ", tags, NULL);
107
    }
108
}
109

    
110
bool TGraphLayouterEnvironment::okToProceed()
111
{
112
    //
113
    // Strategy: do not interact with UI for up to 3 seconds. At the end of the
114
    // 3rd second, start grab on the "STOP" button, and read its state
115
    // occasionally (5 times per second). At the end (in cleanup()) we have to
116
    // release the grab. Do not set a grab in Express mode (i.e. if widgetToGrab==NULL),
117
    // because Express mode's large STOP button already has one.
118
    //
119
    struct timeval now;
120
    gettimeofday(&now, NULL);
121
    if (timeval_msec(now - beginTime) < 3000)
122
        return true;  // no UI interaction for up to 3 sec
123

    
124
    if (!grabActive && widgetToGrab)
125
    {
126
        // start grab
127
        grabActive = true;
128
        Tcl_SetVar(interp, "stoplayouting", "0", TCL_GLOBAL_ONLY);
129
        Tcl_VarEval(interp, "layouter_startgrab ", widgetToGrab, NULL);
130
    }
131

    
132
    // only check the UI once a while
133
    if (timeval_msec(now - lastCheck) < 200)
134
        return true;
135
    lastCheck = now;
136

    
137
    // process UI events; we assume that a "grab" is in effect to the Stop button
138
    // (i.e. the user can only interact with the Stop button, but nothing else)
139
    Tcl_VarEval(interp, "update\n", NULL);
140
    const char *var = Tcl_GetVar(interp, "stoplayouting", TCL_GLOBAL_ONLY);
141
    bool stopNow = var && var[0] && var[0]!='0';
142
    return !stopNow;
143
}
144

    
145
void TGraphLayouterEnvironment::cleanup()
146
{
147
    if (inspected())
148
    {
149
        Tcl_VarEval(interp, canvas, " delete all\n",
150
                            canvas, " config -scrollregion {0 0 1 1}\n",
151
                            canvas, " xview moveto 0\n",
152
                            canvas, " yview moveto 0\n",
153
                            NULL);
154
    }
155
    if (grabActive)
156
    {
157
        Tcl_VarEval(interp, "layouter_releasegrab ", widgetToGrab, NULL);
158
    }
159
}
160

    
161