Subversion Repositories svn.mios32

Rev

Rev 1502 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1003 tk 1
/* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
2
// $Id: OscTool.cpp 1724 2013-03-28 20:23:40Z tk $
3
/*
4
 * OSC Tool Window
5
 *
6
 * ==========================================================================
7
 *
8
 *  Copyright (C) 2010 Thorsten Klose (tk@midibox.org)
9
 *  Licensed for personal non-commercial use only.
10
 *  All other rights reserved.
11
 *
12
 * ==========================================================================
13
 */
14
 
15
#include "OscTool.h"
16
#include "MiosStudio.h"
17
 
18
#define OSC_BUFFER_SIZE 2000
19
 
20
//==============================================================================
21
OscToolConnect::OscToolConnect(MiosStudio *_miosStudio, OscMonitor* _oscMonitor)
22
    : miosStudio(_miosStudio)
23
    , oscMonitor(_oscMonitor)
24
    , udpSocket(NULL)
25
{
26
    addAndMakeVisible(remoteHostLabel = new Label(T("Remote Host:"), T("Remote Host:")));
27
    remoteHostLabel->setJustificationType(Justification::right);
28
 
29
    addAndMakeVisible(remoteHostLine = new TextEditor(String::empty));
30
    remoteHostLine->setMultiLine(false);
31
    remoteHostLine->setReturnKeyStartsNewLine(false);
32
    remoteHostLine->setReadOnly(false);
33
    remoteHostLine->setScrollbarsShown(false);
34
    remoteHostLine->setCaretVisible(true);
35
    remoteHostLine->setPopupMenuEnabled(true);
36
    remoteHostLine->setTextToShowWhenEmpty(T("(hostname)"), Colours::grey);
37
 
1005 tk 38
    addAndMakeVisible(portNumberWriteLabel = new Label(T("Port:"), T("Remote Port:")));
39
    portNumberWriteLabel->setJustificationType(Justification::right);
1003 tk 40
 
1005 tk 41
    addAndMakeVisible(portNumberWriteLine = new TextEditor(String::empty));
42
    portNumberWriteLine->setMultiLine(false);
43
    portNumberWriteLine->setReturnKeyStartsNewLine(false);
44
    portNumberWriteLine->setReadOnly(false);
45
    portNumberWriteLine->setScrollbarsShown(false);
46
    portNumberWriteLine->setCaretVisible(true);
47
    portNumberWriteLine->setPopupMenuEnabled(true);
48
    portNumberWriteLine->setTextToShowWhenEmpty(T("(port number)"), Colours::grey);
49
    portNumberWriteLine->setInputRestrictions(1000000, T("0123456789"));
1003 tk 50
 
1005 tk 51
    addAndMakeVisible(portNumberReadLabel = new Label(T("Port:"), T("Local Port:")));
52
    portNumberReadLabel->setJustificationType(Justification::right);
53
 
54
    addAndMakeVisible(portNumberReadLine = new TextEditor(String::empty));
55
    portNumberReadLine->setMultiLine(false);
56
    portNumberReadLine->setReturnKeyStartsNewLine(false);
57
    portNumberReadLine->setReadOnly(false);
58
    portNumberReadLine->setScrollbarsShown(false);
59
    portNumberReadLine->setCaretVisible(true);
60
    portNumberReadLine->setPopupMenuEnabled(true);
61
    portNumberReadLine->setTextToShowWhenEmpty(T("(port number)"), Colours::grey);
62
    portNumberReadLine->setInputRestrictions(1000000, T("0123456789"));
63
 
1003 tk 64
    addAndMakeVisible(connectButton = new TextButton(T("Connect Button")));
65
    connectButton->setButtonText(T("Connect"));
1502 tk 66
    connectButton->addListener(this);
1003 tk 67
 
68
    addAndMakeVisible(disconnectButton = new TextButton(T("Disconnect Button")));
69
    disconnectButton->setButtonText(T("Disconnect"));
70
    disconnectButton->setEnabled(false);
1502 tk 71
    disconnectButton->addListener(this);
1003 tk 72
 
73
    // restore settings
1724 tk 74
    PropertiesFile *propertiesFile = MiosStudioProperties::getInstance()->getCommonSettings(true);
1003 tk 75
    if( propertiesFile ) {
76
        remoteHostLine->setText(propertiesFile->getValue(T("oscRemoteHost"), String::empty));
1005 tk 77
        portNumberWriteLine->setText(propertiesFile->getValue(T("oscPortWrite"), "10000"));
78
        portNumberReadLine->setText(propertiesFile->getValue(T("oscPortRead"), "10000"));
1003 tk 79
    }
80
 
81
    setSize(800, 4+24+4);
82
}
83
 
84
OscToolConnect::~OscToolConnect()
85
{
86
    deleteAndZero(udpSocket);
87
}
88
 
89
//==============================================================================
90
void OscToolConnect::paint (Graphics& g)
91
{
92
    g.fillAll(Colours::white);
93
}
94
 
95
void OscToolConnect::resized()
96
{
97
    int buttonY = 4;
98
    int buttonWidth = 90;
99
    int buttonX = getWidth() - 2*buttonWidth - 10-4;
100
    remoteHostLabel->setBounds(10, buttonY, 75, 24);
101
    remoteHostLine->setBounds(10+75+4, buttonY, 200, 24);
1006 tk 102
    portNumberWriteLabel->setBounds(10+75+4+200+10, buttonY, 100, 24);
103
    portNumberWriteLine->setBounds(10+75+4+200+10+100+4, buttonY, 50, 24);
104
    portNumberReadLabel->setBounds(10+75+4+200+10+100+4+50+4, buttonY, 75, 24);
105
    portNumberReadLine->setBounds(10+75+4+200+10+100+4+50+4+75+4, buttonY, 50, 24);
1003 tk 106
    connectButton->setBounds(buttonX, buttonY, buttonWidth, 24);
107
    disconnectButton->setBounds(buttonX + buttonWidth + 10, buttonY, buttonWidth, 24);
108
}
109
 
110
//==============================================================================
111
void OscToolConnect::buttonClicked(Button* buttonThatWasClicked)
112
{
113
    if( buttonThatWasClicked == connectButton ) {
114
        // store settings
1724 tk 115
        PropertiesFile *propertiesFile = MiosStudioProperties::getInstance()->getCommonSettings(true);
1003 tk 116
        if( propertiesFile ) {
117
            propertiesFile->setValue(T("oscRemoteHost"), remoteHostLine->getText());
1005 tk 118
            propertiesFile->setValue(T("oscPortWrite"), portNumberWriteLine->getText());
119
            propertiesFile->setValue(T("oscPortRead"), portNumberReadLine->getText());
1003 tk 120
        }
121
 
122
        connectButton->setEnabled(false);
123
        disconnectButton->setEnabled(false); // will be set to true by timer once connection available
1005 tk 124
        remoteHostLine->setEnabled(false);
125
        portNumberWriteLine->setEnabled(false);
126
        portNumberReadLine->setEnabled(false);
1003 tk 127
        startTimer(1); // check for next message each 1 mS
128
    } else if( buttonThatWasClicked == disconnectButton ) {
129
        connectButton->setEnabled(true);
130
        disconnectButton->setEnabled(false);
1005 tk 131
        remoteHostLine->setEnabled(true);
132
        portNumberWriteLine->setEnabled(true);
133
        portNumberReadLine->setEnabled(true);
1003 tk 134
        stopTimer();
135
        if( udpSocket )
136
            udpSocket->disconnect();
137
        deleteAndZero(udpSocket);
138
    }
139
}
140
 
141
 
142
//==============================================================================
143
void OscToolConnect::timerCallback()
144
{
145
    unsigned char oscBuffer[OSC_BUFFER_SIZE];
146
 
147
    if( udpSocket == NULL ) {
148
        stopTimer(); // will be continued if connection established
149
 
150
        udpSocket = new UdpSocket();
1005 tk 151
        if( !udpSocket->connect(remoteHostLine->getText(), portNumberReadLine->getText().getIntValue(), portNumberWriteLine->getText().getIntValue()) ) {
1003 tk 152
            AlertWindow::showMessageBox(AlertWindow::WarningIcon,
153
                                        T("No connection to remote host"),
1005 tk 154
                                        T("Check remote host name (or IP) and port numbers!"),
1003 tk 155
                                        String::empty);
156
 
157
            deleteAndZero(udpSocket);
158
            connectButton->setEnabled(true);
159
            disconnectButton->setEnabled(false);
1005 tk 160
            remoteHostLine->setEnabled(true);
161
            portNumberWriteLine->setEnabled(true);
162
            portNumberReadLine->setEnabled(true);
1003 tk 163
        } else {
164
            disconnectButton->setEnabled(true);
165
            startTimer(1); // continue timer
166
        }
167
 
168
        return;
169
    }
170
 
171
    unsigned size;
172
    if( (size=udpSocket->read(oscBuffer, OSC_BUFFER_SIZE)) )
173
        oscMonitor->handleIncomingOscMessage(oscBuffer, size);
174
}
175
 
176
 
177
 
178
//==============================================================================
179
//==============================================================================
180
//==============================================================================
1005 tk 181
OscToolSend::OscToolSend(MiosStudio *_miosStudio, OscToolConnect *_oscToolConnect)
1003 tk 182
    : miosStudio(_miosStudio)
1005 tk 183
    , oscToolConnect(_oscToolConnect)
1003 tk 184
{
185
    addAndMakeVisible(sendStartButton = new TextButton(T("Send Button")));
186
    sendStartButton->setButtonText(T("Send"));
1502 tk 187
    sendStartButton->addListener(this);
1003 tk 188
 
189
    addAndMakeVisible(sendClearButton = new TextButton(T("Clear Button")));
190
    sendClearButton->setButtonText(T("Clear"));
1502 tk 191
    sendClearButton->addListener(this);
1003 tk 192
 
193
    addAndMakeVisible(statusLabel = new Label(T("Status"), String::empty));
194
    statusLabel->setJustificationType(Justification::right);
195
    addAndMakeVisible(sendBox = new OscTextEditor(statusLabel));
196
 
197
    setSize(800, 200);
198
}
199
 
200
OscToolSend::~OscToolSend()
201
{
202
}
203
 
204
//==============================================================================
205
void OscToolSend::paint (Graphics& g)
206
{
207
    g.fillAll(Colours::white);
208
}
209
 
210
void OscToolSend::resized()
211
{
212
    int sendButtonY = 4;
213
    int sendButtonWidth = 72;
214
    sendStartButton->setBounds(10 + 0*(sendButtonWidth+10), sendButtonY, sendButtonWidth, 24);
215
    sendClearButton->setBounds(10 + 1*(sendButtonWidth+10), sendButtonY, sendButtonWidth, 24);
1006 tk 216
    statusLabel->setBounds(getWidth()-10-500, sendButtonY, 500, 24);
1003 tk 217
 
218
    sendBox->setBounds(4, 32, getWidth()-8, getHeight()-32);
219
}
220
 
221
//==============================================================================
222
void OscToolSend::buttonClicked(Button* buttonThatWasClicked)
223
{
224
    if( buttonThatWasClicked == sendStartButton ) {
1005 tk 225
        if( oscToolConnect->udpSocket ) {
226
            Array<uint8> packet = sendBox->getBinary();
227
 
228
            if( packet.size() ) {
229
                oscToolConnect->udpSocket->write((unsigned char*)&packet.getReference(0), packet.size());
1006 tk 230
            } else {
231
                AlertWindow::showMessageBox(AlertWindow::WarningIcon,
232
                                            T("Invalid OSC packet"),
233
                                            T("The entered packet is invalid (or empty) - check syntax!"),
234
                                            String::empty);
1005 tk 235
            }
1006 tk 236
        } else {
237
            AlertWindow::showMessageBox(AlertWindow::WarningIcon,
238
                                        T("No connection to remote host"),
239
                                        T("Please press the connect button to establish connection!"),
240
                                        String::empty);
1005 tk 241
        }
1003 tk 242
    } else if( buttonThatWasClicked == sendClearButton ) {
243
        sendBox->clear();
244
    }
245
}
246
 
247
 
248
//==============================================================================
249
//==============================================================================
250
//==============================================================================
251
OscTool::OscTool(MiosStudio *_miosStudio)
252
    : miosStudio(_miosStudio)
253
{
254
    addAndMakeVisible(oscMonitor = new OscMonitor(miosStudio));
255
    addAndMakeVisible(oscToolConnect = new OscToolConnect(miosStudio, oscMonitor));
1005 tk 256
    addAndMakeVisible(oscToolSend = new OscToolSend(miosStudio, oscToolConnect));
1003 tk 257
 
258
    //                             num   min   max  prefered  
259
    horizontalLayout.setItemLayout(0, -0.005, -0.9, -0.25); // Send Window
260
    horizontalLayout.setItemLayout(1,    8,      8,     8); // Resizer
261
    horizontalLayout.setItemLayout(2, -0.005, -0.9, -0.25); // Receive Window
262
 
263
    horizontalDividerBar = new StretchableLayoutResizerBar(&horizontalLayout, 1, false);
264
    addAndMakeVisible(horizontalDividerBar);
265
 
266
    resizeLimits.setSizeLimits(100, 300, 2048, 2048);
267
    addAndMakeVisible(resizer = new ResizableCornerComponent(this, &resizeLimits));
268
 
1006 tk 269
    setSize(850, 500);
1003 tk 270
}
271
 
272
OscTool::~OscTool()
273
{
274
}
275
 
276
//==============================================================================
277
void OscTool::paint (Graphics& g)
278
{
279
    g.fillAll(Colour(0xffc1d0ff));
280
}
281
 
282
void OscTool::resized()
283
{
284
    oscToolConnect->setBounds(4, 4, getWidth()-8, oscToolConnect->getHeight());
285
 
286
    Component* hcomps[] = { oscToolSend,
287
                            horizontalDividerBar,
288
                            oscMonitor,
289
    };
290
 
291
    horizontalLayout.layOutComponents(hcomps, 3,
292
                                      4, oscToolConnect->getHeight()+8,
293
                                      getWidth() - 8, getHeight() - 8 - oscToolConnect->getHeight()-8,
294
                                      true,  // lay out above each other
295
                                      true); // resize the components' heights as well as widths
296
 
297
    resizer->setBounds(getWidth()-16, getHeight()-16, 16, 16);
298
}