April 29, 2024, 10:50:32 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Header Maker

Started by Bruce Peaslee, January 13, 2006, 08:35:44 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bruce Peaslee

January 13, 2006, 08:35:44 AM Last Edit: February 25, 2006, 11:19:43 AM by peaslee
I have a few utilities I use that I am porting over from IBasic. I plan to share them with the group as they may be helpful to the new kids. There is not a lot of sophistication here (compared to what some folks are doingÂÃ,  ;)ÂÃ,  ), but it might be useful to some. I have a "wordy" coding style that allows me to come back to a project months later and still have some idea of how it works. Part of this style is the use of headings in the code, but it bothered me when the text was not centered and it was hard to do manually. It was a distraction that many probably do not experience, but I wrote this to automate the process. It does show how to use the clipboard for text.


/* +--------------------------------------------------------------------------+
| HeaderMaker (c) 2006 Bruce Peaslee - All Rights ReservedÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, |
+--------------------------------------------------------------------------+
| Compile as a windows exeÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, |
+--------------------------------------------------------------------------+
| This program accepts an input string from the user, formats aÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  |
| source code header, and places the header on the clipboard.ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  |
+--------------------------------------------------------------------------+
*/

#AutoDefine "off"

// *****************************************************************************
// *ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  GLOBAL CONSTANTSÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, *
// *****************************************************************************

// General dialog constants
const OKÂÃ,  ÂÃ,  ÂÃ, = 1;
const CANCEL = 2;

// Clipboard constants
const CFTEXT = 0x01;
const GHNDÂÃ,  ÂÃ, = 0x42;

// Dialog D1 constants (Main dialog)
const cmdD1OKÂÃ,  ÂÃ,  ÂÃ,  = 1;
const cmdD1CancelÂÃ,  = 2;
const txtD1EditÂÃ,  ÂÃ,  = 3;
const lblD1Heading = 4;

// Menu constants (for program control)
const FILE_OPENÂÃ,  ÂÃ, =ÂÃ,  1;
const FILE_CLOSEÂÃ,  =ÂÃ,  2;
const FILE_NEWÂÃ,  ÂÃ,  =ÂÃ,  3;
const FILE_QUITÂÃ,  ÂÃ, =ÂÃ,  4;
const EDIT_CUTÂÃ,  ÂÃ,  =ÂÃ,  5;
const EDIT_COPYÂÃ,  ÂÃ, =ÂÃ,  6;
const EDIT_PASTEÂÃ,  =ÂÃ,  7;
const HELP_ABOUTÂÃ,  =ÂÃ,  8;
const HELP_HELPÂÃ,  ÂÃ, =ÂÃ,  9;

// *****************************************************************************
// *ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  API DECLARATIONSÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, *
// *****************************************************************************

declare import, CloseClipboardÂÃ,  (),int;
declare import, EmptyClipboardÂÃ,  (),int;
declare import, GetSysColorÂÃ,  ÂÃ,  ÂÃ, (int index),int;
declare import, GlobalAllocÂÃ,  ÂÃ,  ÂÃ, (int flags, int bytes),unsigned int;
declare import, GlobalLockÂÃ,  ÂÃ,  ÂÃ,  (unsigned int handle),pointer;
declare import, GlobalUnlockÂÃ,  ÂÃ,  (unsigned int handle),int;
declare import, OpenClipboardÂÃ,  ÂÃ, (unsigned int hwnd),int;
declare import, SetClipboardData(int format, int handle),int;

// *****************************************************************************
// *ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, CLASS DEFINITIONSÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, *
// *****************************************************************************

// class ------------------------------------------------------------ dlg:cdialog
class dlg:cdialog
{
// Overridden methods
declare OnClose(),int;
declare OnControl(int nID, int nNotifyCode, unsigned int hControl),int;
declare OnInitDialog(),int;
// Member variables
CControl *m_pEdit;
string m_sHeading;
}

dlg::OnClose(),int
{
CloseDialog(CANCEL);
return true;
}

dlg::OnInitDialog(),int
{
CenterWindow();
return true;
}

dlg::OnControl(int nID, int nNotifyCode, unsigned int hControl),int
{
string sStars;
int iNum;
unsigned int hdlMemory;
pointer ptrLock;

select nID
{
case cmdD1Cancel:
if(nNotifyCode = 0)
{
CloseDialog(false);
}
case cmdD1OK:
if(nNotifyCode = 0)
{
// get and format string
m_pEdit = GetControl(txtD1Edit);
m_sHeading = m_pEdit->GetText();
sStars = "// " + String$(77,"*") + "\n";
m_sHeading = LTrim$(RTrim$(m_sHeading));
iNum = (80-len(m_sHeading))/2 - 3; /* 1 for the space and 2 for "//" */
m_sHeading = "// *" + String$(iNum," ") + " " + m_sHeading + " ";
m_sHeading = m_sHeading + String$(79-len(m_sHeading)," ") + "*" + "\n";
m_sHeading = sStars + m_sHeading + sStars;

// put on clipboard
// code converted from clipboard.iba (IBasic) by Paul
hdlMemory = GlobalAlloc(GHND,255);
ptrLock = GlobalLock(hdlMemory);
*(string)ptrLock = m_sHeading;
GlobalUnlock(hdlMemory);
OpenClipboard(null);
EmptyClipboard();
SetClipboardData(CFTEXT,hdlMemory);
CloseClipboard();
CloseDialog(true);
}
}
return true;
}

// class ---------------------------------------------------------- myWin:Window
class myWin:CWindow
{
// Constructor/Destructor
declareÂÃ,  myWin();
declare _myWin();
// Overridden methods
declare OnClose(), int;
declare OnCreate(), int;
declare OnMenuPick(int nID), int;
// New methods
declare CenteredText(int y, string s),void;
declare CreateDialog(), int;
declare ShowAbout(), int;
// Member variables
int m_run;
dlg *d1;
}

myWin::myWin()
{
return;
}

myWin::_myWin()
{
return;
}

myWin::OnClose(), int
{
m_run = 0;
delete d1;
return true;
}

myWin::OnCreate(), int
{
CenterWindow();
CreateDialog();
m_run = 1;
return true;
}

myWin::CenteredText(int y, string s),void
{
int x;
rect clientRect;
point myPoint;

myPoint = GetTextSize(s);
clientRect = GetClientRect();
x = (clientRect.right - clientRect.left - myPoint.x) / 2;
WriteText(x, y, s);
return;
}

myWin::CreateDialog(), int
{
d1 = new(dlg,1);
d1->Create(0,0,300,202,0x80C80080,0,"Input",0);
d1->AddControl(CTEDIT,"",ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  84, 71,133,20,
AWS_VISIBLE|AWS_CHILD|AWS_TABSTOP,0,txtD1Edit);
d1->AddControl(CTBUTTON,"OK",ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  58,143, 70,20,
AWS_VISIBLE|AWS_CHILD|AWS_TABSTOP,0,cmdD1OK);
d1->AddControl(CTBUTTON,"Cancel",ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, 173,143, 70,20,
AWS_VISIBLE|AWS_CHILD|AWS_TABSTOP,0,cmdD1Cancel);
d1->AddControl(CTSTATIC,"Enter Heading",115, 19, 70,20,
AWS_VISIBLE|AWS_CHILDÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ,0,lblD1Heading);
return true;
}

myWin::OnMenuPick(int nID)
{
select nID
{
case FILE_NEW:
d1->DoModal();
case FILE_QUIT:
m_run = 0;
case HELP_ABOUT:
ShowAbout();
}
return true;
}

myWin::ShowAbout(),int
{
string msg;
msg = "Header Maker\n\n"ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, +
"(c) 2006 Bruce Peaslee\n" +
"All Rights Reserved";
MessageBox(this,msg,"About...",0);
return true;
}

// *****************************************************************************
// *ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  MAIN ROUTINEÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, *
// *****************************************************************************

global sub main(), int
{
myWin win;
menu m;

m.BeginMenu();
m.MenuTitle("&File");
m.MenuItem("New\tCtrl+N",ÂÃ,  ÂÃ, AMF_ENABLED, FILE_NEW);
M.Separator();
m.MenuItem("&Open",ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, AMF_GRAYED,ÂÃ,  FILE_OPEN);
m.MenuItem("&Close",ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  AMF_GRAYED,ÂÃ,  FILE_CLOSE);
m.Separator();
m.MenuItem("&Quit\tCtrl+Q", AMF_ENABLED, FILE_QUIT);
m.MenuTitle("&Edit");
m.MenuItem("Cut\tCtrl+X",ÂÃ,  ÂÃ, AMF_GRAYED,ÂÃ,  EDIT_CUT);
m.MenuItem("Copy\tCtrl+C",ÂÃ,  AMF_GRAYED,ÂÃ,  EDIT_COPY);
m.MenuItem("Paste\tCtrl+V", AMF_GRAYED,ÂÃ,  EDIT_PASTE);
m.MenuTitle("&Help");
m.MenuItem("&About...",ÂÃ,  ÂÃ,  ÂÃ, AMF_ENABLED, HELP_ABOUT);
m.MenuItem("&Help",ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, AMF_GRAYED,ÂÃ,  HELP_HELP);
m.EndMenu();

win.Create(0,0,640,400,
AWS_MINIMIZEBOX|AWS_CAPTION|AWS_SYSMENU|AWS_VISIBLE,
0,"Header Maker",0);
win.SetWindowColor(GetSysColor(15));
win.SetMenu(m.Detach());
win.AddAccelerator(FCONTROL|FVIRTKEY,ASC("N"),FILE_NEW);
win.AddAccelerator(FCONTROL|FVIRTKEY,ASC("Q"),FILE_QUIT);
win.DrawMode(TRANSPARENT);
win.SetFont("Comic Sans MS",16,500);
win.CenteredText(50,"Header Maker");
win.SetFont("Ariel",10,500);
win.CenteredText(85,"Type CTRL-NÂÃ,  [or menu File:New]ÂÃ,  to begin.");

// message loop
do
{wait();
}until win.m_run = 0;

win.Destroy();
return 0;
}
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Zen

Peaslee, i like to keep my code just like yours. Nice and tidy and neat little comments so i too can come back after and not go, ohhhh noooo!!!! and have to pick your own code apart.

I hate getting hold of someone elses code and it looks like it has just been thrown there.

Im not sure i am using your code right though, went to new, entered a heading but then nothing happens unless i... "CTRL-V"

// *****************************************************************************
// *                               Lewis Lambert                               *
// *****************************************************************************

... Oh, very cool bruce.

Lewis

Bruce Peaslee

Right. The program puts the heading on the clipboard and you paste it in your code.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

QuotePeaslee, i like to keep my code just like yours. Nice and tidy and neat little comments so i too can come back after and not go, ohhhh noooo!!!! and have to pick your own code apart.

I've found some of my early IBasic code, and it's near impossible to read, even only ~70 lines, since there's no indentation, no comments, and weird variable names :-[

Zen

Some code you sent me parker, i think its quite old because it took me a  while to decode it all

Lewis

Parker

Sorry :-[
I've gotten into better habits of commenting recently, but most of my IBasic (Pro too) code was pretty hard for me to get any meaning out of when I looked back at it.

Zen

Hmm ive always tried to keep code tidy. To me it seems that way but i guess everyone has there own way.

Lewis

Bruce Peaslee

I wrote an Access database application for my boss. I hated VBA so I switched to IBasic Pro. Nine months later the new year started and dates wouldn't sort correctly - but I hadn't looked at the VBA source code (or any VBA code for that matter) in all that time. If I hadn't coded that way I do, I never would have found the problem and would have been forced to redesign the application in another language under considerable time pressure.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Zen

Yes it does pay off in situations like that.

Lewis

kryton9

This is really cool, will motivate me to write cleaner code, but I will never get as neat as Bruce's

If you get an error, the code needs to change where it says.

In  global sub main()   change menu m;   to Cmenu m;