ultimatepp/uppdev/ezcomm/main.cpp.bak
cxl 4a1c627474 Adding uppdev....
git-svn-id: svn://ultimatepp.org/upp/trunk@328 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2008-08-15 08:36:24 +00:00

295 lines
7.7 KiB
C++

/***************************************************************************
ezcommon - description
-------------------
begin : Aug. 2, 2007
copyright : (C) 2007 by Allen
email : bon_ami_@hotmail.com
***************************************************************************/
/***************************************************************************
* *
* Explicit Distribution Limitation *
* This rule overrides others below. *
* This program may not be modified or used by, or, if possible, *
* redistributed to people described as below, *
* 1.Japanese who hold hostility against Chinese. *
* 2.or, those who discriminate against people based solely on race, *
* gender or sexual orientation. *
* *
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/*
* source code of EZ Comm Project common Ultimate++ functionalities
*/
#include "main.h"
#define IMAGEFILE <ezcomm/ezcomm.iml>
#include <Draw/iml_source.h>
ezcommwin::ezcommwin()
{
msg.Add("Have a nice day.");
msg.Add("Bonjour.");
msg.Add("Salut");
msg.Add("hallo");
msg.Add("ciao");
msg.Add("hola");
msg.Add("written by Allen");
msg.Add("a sub-project of EZ Project");
msg.Add("http://ezproject.sourceforge.net/");
msg.Add("Windows or Linux, Text or Graphical.");
msg.Add("Anti-Discrimination");
msg.Add("under GPL with a couple of exception rules");
msg.Add("cannot be used by Japanese who hold hostility against Chinese");
msg.Add("cannot be used by those who have race discrimination");
msg.Add("cannot be used by those who have gender discrimination");
msg.Add("cannot be used by those who have sexual orientation discrimination");
/* DrawingDraw img;
ImageCtrl img;
img.SetImage(CtrlImg::exclamation());
img.SetRect(10,10,100,100);
Add(img);
img.Show();
ViewDraw dr(this);
dr.DrawLine(10,10,100,100,10);
dr.DrawImage(10, 10, CtrlImg::exclamation());
dr.Flush();*/
// Draw::GetDrawable().DrawImage(10,10,CtrlImg::exclamation());
// img.DrawImage(10, 10, CtrlImg::exclamation());
// DrawCtrl(img, 10, 10);
Icon(IMAGECLASS::Get(0), IMAGECLASS::Get(0));
comm_type.EnableCase(0);
comm_type.Tip("select communication method");
port_out.Min(1);
port_out.Max(65535);
port_lcl.Min(1);
port_lcl.Max(65535);
CtrlLayout(*this, "EZ Comm");
ip_out <<= THISBACK(Ip_out);
port_out <<= THISBACK(Port_out);
port_lcl <<= THISBACK(Port_lcl);
hist_text_out.WhenAction = THISBACK(Hist_text_out);
butt_clr <<= THISBACK(Butt_clr);
butt_out <<= THISBACK(Butt_out);
butt_in <<= THISBACK(Butt_in);
WhenClose = THISBACK(WClose);
lowlight_style = EditField::StyleDefault();
highlighted_ctrl = NULL;
}
bool ezcommwin::HotKey(dword key)
{
switch (key)
{
case K_ENTER:
case K_CTRL_ENTER:
Butt_out();
return true;
}
return false;
}
void ezcommwin::WClose()
{
/* validate controls */
port_out.Clear();
port_lcl.Clear();
/* go on termination */
Close();
}
void ezcommwin::Ip_out()
{
lowlight_ctrl(&ip_out);
}
void ezcommwin::Port_out()
{
lowlight_ctrl(&port_out);
}
void ezcommwin::Port_lcl()
{
lowlight_ctrl(&port_lcl);
}
void ezcommwin::Hist_text_out()
{
text_out.Set(hist_text_out.GetValue().ToString());
text_out.SetFocus();
}
using namespace ezproject;
void ezcommwin::highlight_ctrl(EditField *ctrl)
{
lowlight_ctrl(NULL);
if (ctrl)
{
EditField::Style highlight_style = lowlight_style;
highlight_style.paper = Red();
ctrl->SetStyle(highlight_style);
ctrl->SetFocus();
}
highlighted_ctrl = ctrl;
}
/*
parameter: ctrl: NULL: reset highlighted one; non-NULL: reset the control if it is highlighted
*/
void ezcommwin::lowlight_ctrl(EditField *ctrl)
{
if (highlighted_ctrl)
{
if (!ctrl || (ctrl == highlighted_ctrl))
{
highlighted_ctrl->SetStyle(lowlight_style);
highlighted_ctrl = NULL;
if (ctrl)
Butt_clr();
}
}
}
void ezcommwin::disp(ezoi::eoilvl lvl, const char *cnt)
{
Font ft(info.GetFont());
switch (lvl)
{
case ezoi::OILVL_FATAL:
ft.Bold();
ft.NoItalic();
ft.Underline();
break;
case ezoi::OILVL_WARN:
ft.Bold();
ft.Italic();
ft.NoUnderline();
break;
case ezoi::OILVL_INFO:
ft.NoBold();
ft.NoItalic();
ft.NoUnderline();
break;
case ezoi::OILVL_NA:
ft.NoBold();
ft.Italic();
ft.NoUnderline();
break;
}
info.SetFont(ft);
info.SetText(cnt ? cnt : "");
}
#include <stdio.h>
#include "winsock2.h"
void ezcommwin::allen(char *ipp)
{
WSADATA wsaData;
SOCKET SendSocket;
sockaddr_in RecvAddr;
int Port = 5060;
char SendBuf[] = "hello";
int BufLen = strlen(SendBuf);
String ip;
ip= ip_out.GetData();
//---------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);
//---------------------------------------------
// Create a socket for sending data
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//---------------------------------------------
// Set up the RecvAddr structure with the IP address of
// the receiver (in this example case "123.456.789.1")
// and the specified port number.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr(ip);
//---------------------------------------------
// Send a datagram to the receiver
disp(ezoi::OILVL_FATAL, "sending");
sendto(SendSocket,
SendBuf,
BufLen,
0,
(SOCKADDR *) &RecvAddr,
sizeof(RecvAddr));
//---------------------------------------------
// When the application is finished sending, close the socket.
disp(ezoi::OILVL_FATAL, "closing");
closesocket(SendSocket);
//---------------------------------------------
// Clean up and quit.
WSACleanup();
}
void ezcommwin::Butt_out()
{
if ((0 == ip_out.GetLength()) || (0 == port_out.GetLength()))
{
disp(ezoi::OILVL_FATAL, "Input destination's IP & port!");
if (0 == ip_out.GetLength())
{
highlight_ctrl(&ip_out);
}
else
highlight_ctrl(&port_out);
}
else
{
/* send allen */
allen(NULL);
/* - send allen - */
disp(ezoi::OILVL_INFO, "sending...");
ip_out.AddHistory(HIST_DROP_NUM);
port_out.AddHistory(HIST_DROP_NUM);
hist_text_out.Add(text_out.Get());
text_out.SetCursor(0);
}
}
void ezcommwin::Butt_in()
{
if (0 == port_lcl.GetLength())
{
disp(ezoi::OILVL_FATAL, "Input local port!");
highlight_ctrl(&port_lcl);
}
else
{
disp(ezoi::OILVL_INFO, "listening...");
}
}
void ezcommwin::Butt_clr()
{
disp(ezoi::OILVL_NA, msg.At(rand() % msg.GetCount()));
}
GUI_APP_MAIN
{
ezcommwin ezcw;
/*ezoi i;
i.mem();*/
ezcw.Run();
}