home *** CD-ROM | disk | FTP | other *** search
- _C PROGRAMMING COLUMN_
- by Al Stevens
-
-
- [LISTING ONE]
-
- /* ----------- clipbord.c ------------ */
- #include "dflat.h"
-
- char *Clipboard;
- int ClipboardLength;
-
- void CopyToClipboard(WINDOW wnd)
- {
- if (TextBlockMarked(wnd)) {
- char *bbl=TextLine(wnd,wnd->BlkBegLine)+wnd->BlkBegCol;
- char *bel=TextLine(wnd,wnd->BlkEndLine)+wnd->BlkEndCol;
- ClipboardLength = (int) (bel - bbl);
- Clipboard = realloc(Clipboard, ClipboardLength);
- if (Clipboard != NULL)
- memmove(Clipboard, bbl, ClipboardLength);
- }
- }
-
- void PasteText(WINDOW wnd, char *SaveTo, int len)
- {
- if (SaveTo != NULL && len > 0) {
- int plen = strlen(wnd->text) + len;
- char *bl, *el;
-
- if (plen > wnd->textlen) {
- wnd->text = realloc(wnd->text, plen+2);
- wnd->textlen = plen;
- }
- if (wnd->text != NULL) {
- bl = CurrChar;
- el = bl+len;
- memmove(el, bl, strlen(bl)+1);
- memmove(bl, SaveTo, len);
- BuildTextPointers(wnd);
- wnd->TextChanged = TRUE;
- }
- }
- }
-
-
-
- [LISTING TWO]
-
- /* ---------------- search.c ------------- */
- #include "dflat.h"
-
- extern DBOX SearchTextDB;
- extern DBOX ReplaceTextDB;
- static int CheckCase = TRUE;
-
- /* - case-insensitive, white-space-normalized char compare - */
- static int SearchCmp(int a, int b)
- {
- if (b == '\n')
- b = ' ';
- if (CheckCase)
- return a != b;
- return tolower(a) != tolower(b);
- }
-
- /* ----- replace a matching block of text ----- */
- static void replacetext(WINDOW wnd, char *cp1, DBOX *db)
- {
- char *cr = GetEditBoxText(db, ID_REPLACEWITH);
- char *cp = GetEditBoxText(db, ID_SEARCHFOR);
- int oldlen = strlen(cp); /* length of text being replaced */
- int newlen = strlen(cr); /* length of replacing text */
- int dif;
- if (oldlen < newlen) {
- /* ---- new text expands text size ---- */
- dif = newlen-oldlen;
- if (wnd->textlen < strlen(wnd->text)+dif) {
- /* ---- need to reallocate the text buffer ---- */
- int offset = (int)(cp1-wnd->text);
- wnd->textlen += dif;
- wnd->text = realloc(wnd->text, wnd->textlen+2);
- if (wnd->text == NULL)
- return;
- cp1 = wnd->text + offset;
- }
- memmove(cp1+dif, cp1, strlen(cp1)+1);
- }
- else if (oldlen > newlen) {
- /* ---- new text collapses text size ---- */
- dif = oldlen-newlen;
- memmove(cp1, cp1+dif, strlen(cp1)+1);
- }
- strncpy(cp1, cr, newlen);
- }
-
- /* ------- search for the occurrance of a string ------- */
- static void SearchTextBox(WINDOW wnd, int Replacing, int incr)
- {
- char *s1, *s2, *cp1;
- DBOX *db = Replacing ? &ReplaceTextDB : &SearchTextDB;
- char *cp = GetEditBoxText(db, ID_SEARCHFOR);
- int rpl = TRUE, FoundOne = FALSE;
-
- while (rpl == TRUE && cp != NULL) {
- rpl = Replacing ?
- CheckBoxSetting(&ReplaceTextDB, ID_REPLACEALL) :
- FALSE;
- if (TextBlockMarked(wnd)) {
- ClearTextBlock(wnd);
- SendMessage(wnd, PAINT, 0, 0);
- }
- /* search for a match starting at cursor position */
- cp1 = CurrChar;
- if (incr)
- cp1++; /* start past the last hit */
- /* --- compare at each character position --- */
- while (*cp1) {
- s1 = cp;
- s2 = cp1;
- while (*s1 && *s1 != '\n') {
- if (SearchCmp(*s1, *s2))
- break;
- s1++, s2++;
- }
- if (*s1 == '\0' || *s1 == '\n')
- break;
- cp1++;
- }
- if (*s1 == 0 || *s1 == '\n') {
- /* ----- match at *cp1 ------- */
- FoundOne = TRUE;
-
- /* mark a block at beginning of matching text */
- wnd->BlkEndLine = TextLineNumber(wnd, s2);
- wnd->BlkBegLine = TextLineNumber(wnd, cp1);
- if (wnd->BlkEndLine < wnd->BlkBegLine)
- wnd->BlkEndLine = wnd->BlkBegLine;
- wnd->BlkEndCol =
- (int)(s2 - TextLine(wnd, wnd->BlkEndLine));
- wnd->BlkBegCol =
- (int)(cp1 - TextLine(wnd, wnd->BlkBegLine));
-
- /* position the cursor at the matching text */
- wnd->CurrCol = wnd->BlkBegCol;
- wnd->CurrLine = wnd->BlkBegLine;
- wnd->WndRow = wnd->CurrLine - wnd->wtop;
-
- /* align the window scroll to matching text */
- if (WndCol > ClientWidth(wnd)-1)
- wnd->wleft = wnd->CurrCol;
- if (wnd->WndRow > ClientHeight(wnd)-1) {
- wnd->wtop = wnd->CurrLine;
- wnd->WndRow = 0;
- }
-
- SendMessage(wnd, PAINT, 0, 0);
- SendMessage(wnd, KEYBOARD_CURSOR,
- WndCol, wnd->WndRow);
-
- if (Replacing) {
- if (rpl || YesNoBox("Replace the text?")) {
- replacetext(wnd, cp1, db);
- wnd->TextChanged = TRUE;
- BuildTextPointers(wnd);
- }
- if (rpl) {
- incr = TRUE;
- continue;
- }
- ClearTextBlock(wnd);
- SendMessage(wnd, PAINT, 0, 0);
- }
- return;
- }
- break;
- }
- if (!FoundOne)
- MessageBox("Search/Replace Text", "No match found");
- }
-
- /* ------- search for the occurrance of a string,
- replace it with a specified string ------- */
- void ReplaceText(WINDOW wnd)
- {
- if (CheckCase)
- SetCheckBox(&ReplaceTextDB, ID_MATCHCASE);
- if (DialogBox(wnd, &ReplaceTextDB, TRUE, NULL)) {
- CheckCase=CheckBoxSetting(&ReplaceTextDB,ID_MATCHCASE);
- SearchTextBox(wnd, TRUE, FALSE);
- }
- }
-
- /* ------- search for the first occurrance of a string ------ */
- void SearchText(WINDOW wnd)
- {
- if (CheckCase)
- SetCheckBox(&SearchTextDB, ID_MATCHCASE);
- if (DialogBox(wnd, &SearchTextDB, TRUE, NULL)) {
- CheckCase=CheckBoxSetting(&SearchTextDB,ID_MATCHCASE);
- SearchTextBox(wnd, FALSE, FALSE);
- }
- }
-
- /* ------- search for the next occurrance of a string ------- */
- void SearchNext(WINDOW wnd)
- {
- SearchTextBox(wnd, FALSE, TRUE);
- }
-
-
-
-
- [LISTING THREE]
-
- /* -------------- pictbox.c -------------- */
-
- #include "dflat.h"
-
- typedef struct {
- enum VectTypes vt;
- RECT rc;
- } VECT;
-
- unsigned char CharInWnd[] = "D3Z?Y@EC4AB";
-
- unsigned char VectCvt[3][11][2][4] = {
- { /* --- first character in collision vector --- */
- /* ( drawing D ) ( drawing 3 ) */
- {{"DDD"}, {"ZC@"}},
- {{"ZB?"}, {"333"}},
- {{"ZBB"}, {"ZCC"}},
- {{"???"}, {"???"}},
- {{"YYY"}, {"YYY"}},
- {{"@AA"}, {"CC@"}},
- {{"EEE"}, {"EEE"}},
- {{"CEE"}, {"CCC"}},
- {{"444"}, {"444"}},
- {{"AAA"}, {"AAA"}},
- {{"BBB"}, {"BEE"}} },
- { /* --- middle character in collision vector --- */
- /* ( drawing D ) ( drawing 3 ) */
- {{"DDD"}, {"BEA"}},
- {{"CE4"}, {"333"}},
- {{"ZZZ"}, {"ZZZ"}},
- {{"???"}, {"???"}},
- {{"YYY"}, {"YYY"}},
- {{"@@@"}, {"@@@"}},
- {{"EEE"}, {"EEE"}},
- {{"CCC"}, {"CCC"}},
- {{"EE4"}, {"444"}},
- {{"AAA"}, {"EEA"}},
- {{"BBB"}, {"BBB"}} },
- { /* --- last character in collision vector --- */
- /* ( drawing D ) ( drawing 3 ) */
- {{"DDD"}, {"?4Y"}},
- {{"@AY"}, {"333"}},
- {{"ZZZ"}, {"ZZZ"}},
- {{"BB?"}, {"?44"}},
- {{"AAY"}, {"44Y"}},
- {{"@@@"}, {"@@@"}},
- {{"EEE"}, {"EEE"}},
- {{"CCC"}, {"CCC"}},
- {{"EE4"}, {"444"}},
- {{"AAA"}, {"EEA"}},
- {{"BBB"}, {"BBB"}} }
- };
-
- /* -- compute whether character is first, middle, or last -- */
- static int FindVector(WINDOW wnd, RECT rc, int x, int y)
- {
- RECT rcc;
- VECT *vc = wnd->VectorList;
- int i, coll = -1;
- for (i = 0; i < wnd->VectorCount; i++) {
- if ((vc+i)->vt == VECTOR) {
- rcc = (vc+i)->rc;
- /* --- skip the colliding vector --- */
- if (rcc.lf == rc.lf && rcc.rt == rc.rt &&
- rcc.tp == rc.tp && rc.bt == rcc.bt)
- continue;
- if (rcc.tp == rcc.bt) {
- /* ---- horizontal vector,
- see if character is in it --- */
- if (rc.lf+x >= rcc.lf && rc.lf+x <= rcc.rt &&
- rc.tp+y == rcc.tp) {
- /* --- it is --- */
- if (rc.lf+x == rcc.lf)
- coll = 0;
- else if (rc.lf+x == rcc.rt)
- coll = 2;
- else
- coll = 1;
- }
- }
- else {
- /* ---- vertical vector,
- see if character is in it --- */
- if (rc.tp+y >= rcc.tp && rc.tp+y <= rcc.bt &&
- rc.lf+x == rcc.lf) {
- /* --- it is --- */
- if (rc.tp+y == rcc.tp)
- coll = 0;
- else if (rc.tp+y == rcc.bt)
- coll = 2;
- else
- coll = 1;
- }
- }
- }
- }
- return coll;
- }
-
- static void PaintVector(WINDOW wnd, RECT rc)
- {
- int i, cw, fml, vertvect, coll, len;
- unsigned int ch, nc;
-
- if (rc.rt == rc.lf) {
- /* ------ vertical vector ------- */
- nc = '3';
- vertvect = 1;
- len = rc.bt-rc.tp+1;
- }
- else {
- /* ------ horizontal vector ------- */
- nc = 'D';
- vertvect = 0;
- len = rc.rt-rc.lf+1;
- }
-
- for (i = 0; i < len; i++) {
- unsigned int newch = nc;
- int xi = 0, yi = 0;
- if (vertvect)
- yi = i;
- else
- xi = i;
- ch = videochar(GetClientLeft(wnd)+rc.lf+xi,
- GetClientTop(wnd)+rc.tp+yi);
- for (cw = 0; cw < sizeof(CharInWnd); cw++) {
- if (ch == CharInWnd[cw]) {
- /* ---- hit another vector character ---- */
- if ((coll=FindVector(wnd, rc, xi, yi)) != -1) {
- /* compute first/middle/last subscript */
- if (i == len-1)
- fml = 2;
- else if (i == 0)
- fml = 0;
- else
- fml = 1;
- newch = VectCvt[coll][cw][vertvect][fml];
- }
- }
- }
- PutWindowChar(wnd, newch, rc.lf+xi, rc.tp+yi);
- }
- }
-
- static void PaintBar(WINDOW wnd, RECT rc, enum VectTypes vt)
- {
- int i, vertbar, len;
- unsigned int tys[] = {'[', '2', '1', '0'};
- unsigned int nc = tys[vt-1];
-
- if (rc.rt == rc.lf) {
- /* ------ vertical bar ------- */
- vertbar = 1;
- len = rc.bt-rc.tp+1;
- }
- else {
- /* ------ horizontal bar ------- */
- vertbar = 0;
- len = rc.rt-rc.lf+1;
- }
-
- for (i = 0; i < len; i++) {
- int xi = 0, yi = 0;
- if (vertbar)
- yi = i;
- else
- xi = i;
- PutWindowChar(wnd, nc, rc.lf+xi, rc.tp+yi);
- }
- }
-
- static void PaintMsg(WINDOW wnd)
- {
- int i;
- VECT *vc = wnd->VectorList;
- for (i = 0; i < wnd->VectorCount; i++) {
- if (vc->vt == VECTOR)
- PaintVector(wnd, vc->rc);
- else
- PaintBar(wnd, vc->rc, vc->vt);
- vc++;
- }
- }
-
- static void DrawVectorMsg(WINDOW wnd,PARAM p1,enum VectTypes vt)
- {
- if (p1) {
- wnd->VectorList = realloc(wnd->VectorList,
- sizeof(VECT) * (wnd->VectorCount + 1));
- if (wnd->VectorList != NULL) {
- VECT vc;
- vc.vt = vt;
- vc.rc = *(RECT *)p1;
- *(((VECT *)(wnd->VectorList))+wnd->VectorCount)=vc;
- wnd->VectorCount++;
- }
- }
- }
-
- static void DrawBoxMsg(WINDOW wnd, PARAM p1)
- {
- if (p1) {
- RECT rc = *(RECT *)p1;
- rc.bt = rc.tp;
- SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, TRUE);
- rc = *(RECT *)p1;
- rc.lf = rc.rt;
- SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, FALSE);
- rc = *(RECT *)p1;
- rc.tp = rc.bt;
- SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, TRUE);
- rc = *(RECT *)p1;
- rc.rt = rc.lf;
- SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, FALSE);
- }
- }
-
- int PictureProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
- {
- switch (msg) {
- case PAINT:
- BaseWndProc(PICTUREBOX, wnd, msg, p1, p2);
- PaintMsg(wnd);
- return TRUE;
- case DRAWVECTOR:
- DrawVectorMsg(wnd, p1, VECTOR);
- return TRUE;
- case DRAWBOX:
- DrawBoxMsg(wnd, p1);
- return TRUE;
- case DRAWBAR:
- DrawVectorMsg(wnd, p1, p2);
- return TRUE;
- case CLOSE_WINDOW:
- if (wnd->VectorList != NULL)
- free(wnd->VectorList);
- break;
- default:
- break;
- }
- return BaseWndProc(PICTUREBOX, wnd, msg, p1, p2);
- }
-
- static RECT PictureRect(int x, int y, int len, int hv)
- {
- RECT rc;
- rc.lf = rc.rt = x;
- rc.tp = rc.bt = y;
- if (hv)
- /* ---- horizontal vector ---- */
- rc.rt += len-1;
- else
- /* ---- vertical vector ---- */
- rc.bt += len-1;
- return rc;
- }
-
- void DrawVector(WINDOW wnd, int x, int y, int len, int hv)
- {
- RECT rc = PictureRect(x,y,len,hv);
- SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, 0);
- }
-
- void DrawBox(WINDOW wnd, int x, int y, int ht, int wd)
- {
- RECT rc;
- rc.lf = x;
- rc.tp = y;
- rc.rt = x+wd-1;
- rc.bt = y+ht-1;
- SendMessage(wnd, DRAWBOX, (PARAM) &rc, 0);
- }
-
- void DrawBar(WINDOW wnd,enum VectTypes vt,int x,int y,int len,int hv)
- {
- RECT rc = PictureRect(x,y,len,hv);
- SendMessage(wnd, DRAWBAR, (PARAM) &rc, (PARAM) vt);
- }
-
-
-
-
-
- [LISTING FOUR]
-
- /* ------------- calendar.c ------------- */
- #include "dflat.h"
-
- #ifndef TURBOC
-
- #define CALHEIGHT 17
- #define CALWIDTH 33
-
- static int DyMo[] = {31,28,31,30,31,30,31,31,30,31,30,31};
- static struct tm ttm;
- static int dys[42];
- static WINDOW Cwnd;
-
- static void FixDate(void)
- {
- /* ---- adjust Feb for leap year ---- */
- DyMo[1] = (ttm.tm_year % 4) ? 28 : 29;
- #ifndef BCPP
- /* bug in the Borland C++ mktime function prohibits this */
- ttm.tm_mday = min(ttm.tm_mday, DyMo[ttm.tm_mon]);
- #endif
- }
-
- /* ---- build calendar dates array ---- */
- static void BuildDateArray(void)
- {
- int offset, dy = 0;
- memset(dys, 0, sizeof dys);
- FixDate();
- /* ----- compute the weekday for the 1st ----- */
- offset = ((ttm.tm_mday-1) - ttm.tm_wday) % 7;
- if (offset < 0)
- offset += 7;
- if (offset)
- offset = (offset - 7) * -1;
- /* ----- build the dates into the array ---- */
- for (dy = 1; dy <= DyMo[ttm.tm_mon]; dy++)
- dys[offset++] = dy;
- }
- static void CreateWindowMsg(WINDOW wnd)
- {
- int x, y;
- DrawBox(wnd, 1, 2, CALHEIGHT-4, CALWIDTH-4);
- for (x = 5; x < CALWIDTH-4; x += 4)
- DrawVector(wnd, x, 2, CALHEIGHT-4, FALSE);
- for (y = 4; y < CALHEIGHT-3; y+=2)
- DrawVector(wnd, 1, y, CALWIDTH-4, TRUE);
- }
- static void DisplayDates(WINDOW wnd)
- {
- int week, day;
- char dyln[10];
- int offset;
- char banner[CALWIDTH-1];
- char banner1[30];
-
- SetStandardColor(wnd);
- PutWindowLine(wnd, "Sun Mon Tue Wed Thu Fri Sat", 2, 1);
- memset(banner, ' ', CALWIDTH-2);
- strftime(banner1, 16, "%B, %Y", &ttm);
- offset = (CALWIDTH-2 - strlen(banner1)) / 2;
- strcpy(banner+offset, banner1);
- strcat(banner, " ");
- PutWindowLine(wnd, banner, 0, 0);
- BuildDateArray();
- for (week = 0; week < 6; week++) {
- for (day = 0; day < 7; day++) {
- int dy = dys[week*7+day];
- if (dy == 0)
- strcpy(dyln, " ");
- else {
- if (dy == ttm.tm_mday)
- sprintf(dyln, "%c%c%c%2d %c",
- CHANGECOLOR,
- SelectForeground(wnd)+0x80,
- SelectBackground(wnd)+0x80,
- dy, RESETCOLOR);
- else
- sprintf(dyln, "%2d ", dy);
- }
- SetStandardColor(wnd);
- PutWindowLine(wnd, dyln, 2 + day * 4, 3 + week*2);
- }
- }
- }
- static int KeyboardMsg(WINDOW wnd, PARAM p1)
- {
- switch ((int)p1) {
- case PGUP:
- if (ttm.tm_mon == 0) {
- ttm.tm_mon = 12;
- ttm.tm_year--;
- }
- ttm.tm_mon--;
- FixDate();
- mktime(&ttm);
- DisplayDates(wnd);
- return TRUE;
- case PGDN:
- ttm.tm_mon++;
- if (ttm.tm_mon == 12) {
- ttm.tm_mon = 0;
- ttm.tm_year++;
- }
- FixDate();
- mktime(&ttm);
- DisplayDates(wnd);
- return TRUE;
- default:
- break;
- }
- return FALSE;
- }
- static int CalendarProc(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
- {
- switch (msg) {
- case CREATE_WINDOW:
- DefaultWndProc(wnd, msg, p1, p2);
- CreateWindowMsg(wnd);
- return TRUE;
- case KEYBOARD:
- if (KeyboardMsg(wnd, p1))
- return TRUE;
- break;
- case PAINT:
- DefaultWndProc(wnd, msg, p1, p2);
- DisplayDates(wnd);
- return TRUE;
- case COMMAND:
- if ((int)p1 == ID_HELP) {
- DisplayHelp(wnd, "Calendar");
- return TRUE;
- }
- break;
- case CLOSE_WINDOW:
- Cwnd = NULL;
- break;
- default:
- break;
- }
- return DefaultWndProc(wnd, msg, p1, p2);
- }
- void Calendar(WINDOW pwnd)
- {
- if (Cwnd == NULL) {
- time_t tim = time(NULL);
- ttm = *localtime(&tim);
- Cwnd = CreateWindow(PICTUREBOX,
- "Calendar",
- -1, -1, CALHEIGHT, CALWIDTH,
- NULL, pwnd, CalendarProc,
- SHADOW |
- MINMAXBOX |
- CONTROLBOX |
- MOVEABLE |
- HASBORDER
- );
- }
- SendMessage(Cwnd, SETFOCUS, TRUE, 0);
- }
-
- #endif
-
-
-
-
- [LISTING FIVE]
-
- /* ------------ barchart.c ----------- */
- #include "dflat.h"
-
- #define BCHEIGHT 12
- #define BCWIDTH 44
- #define COLWIDTH 4
-
- static WINDOW Bwnd;
-
- /* ------- project schedule array ------- */
- static struct ProjChart {
- char *prj;
- int start, stop;
- } projs[] = {
- {"Center St", 0,3},
- {"City Hall", 0,5},
- {"Rt 395 ", 1,4},
- {"Sky Condo", 2,3},
- {"Out Hs ", 0,4},
- {"Bk Palace", 1,5}
- };
-
- static char *Title = " PROJECT SCHEDULE";
- static char *Months = " Jan Feb Mar Apr May Jun";
-
- static int BarChartProc(WINDOW wnd, MESSAGE msg,PARAM p1, PARAM p2)
- {
- switch (msg) {
- case COMMAND:
- if ((int)p1 == ID_HELP) {
- DisplayHelp(wnd, "BarChart");
- return TRUE;
- }
- break;
- case CLOSE_WINDOW:
- Bwnd = NULL;
- break;
- default:
- break;
- }
- return DefaultWndProc(wnd, msg, p1, p2);
- }
- void BarChart(WINDOW pwnd)
- {
- int pct = sizeof projs / sizeof(struct ProjChart);
- int i;
-
- if (Bwnd == NULL) {
- Bwnd = CreateWindow(PICTUREBOX,
- "BarChart",
- -1, -1, BCHEIGHT, BCWIDTH,
- NULL, pwnd, BarChartProc,
- SHADOW |
- CONTROLBOX |
- MOVEABLE |
- HASBORDER
- );
- SendMessage(Bwnd, ADDTEXT, (PARAM) Title, 0);
- SendMessage(Bwnd, ADDTEXT, (PARAM) "", 0);
- for (i = 0; i < pct; i++) {
- SendMessage(Bwnd,ADDTEXT,(PARAM)projs[i].prj,0);
- DrawBar(Bwnd, SOLIDBAR+(i%4),
- 11+projs[i].start*COLWIDTH, 2+i,
- (1 + projs[i].stop-projs[i].start) * COLWIDTH,
- TRUE);
- }
- SendMessage(Bwnd, ADDTEXT, (PARAM) "", 0);
- SendMessage(Bwnd, ADDTEXT, (PARAM) Months, 0);
- DrawBox(Bwnd, 10, 1, pct+2, 25);
- }
- SendMessage(Bwnd, SETFOCUS, TRUE, 0);
- }
-
-
-