openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

Button.cpp (1407B)


      1 #include <win/Event.h>
      2 #include <win/Button.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 Button::Button(const char *text)
      7 	:	fMouseDown(false),
      8 		fOverButton(false)
      9 {
     10 	fText = (char*) malloc(strlen(text) + 1);	
     11 	memcpy(fText, text, strlen(text) + 1);
     12 }
     13 
     14 Button::~Button()
     15 {
     16 	free(fText);
     17 }
     18 
     19 void Button::Invoke()
     20 {
     21 }
     22 
     23 void Button::Repaint(long, long, long, long)
     24 {
     25 	long left, top, right, bottom;
     26 	GetBounds(&left, &top, &right, &bottom);
     27 
     28 	if (fMouseDown && fOverButton) {
     29 		left++;
     30 		top++;
     31 	} else {
     32 		right--;
     33 		bottom--;
     34 	}
     35 
     36 	SetPenColor(5);
     37 	DrawLine(left, top, right, top);
     38 	DrawLine(left, bottom, right, bottom);
     39 	DrawLine(left, top, left, bottom);
     40 	DrawLine(right, top, right, bottom);
     41 	SetPenColor(10);
     42 	FillRect(left + 1, top + 1, right - 1, bottom - 1);
     43 	DrawString(left + 1, top + 1, fText);
     44 }
     45 
     46 void Button::EventReceived(const Event *event)
     47 {
     48 	switch (event->what) {
     49 		case EVT_MOUSE_DOWN:
     50 			LockMouseFocus();
     51 			fMouseDown = true;
     52 			fOverButton = true;
     53 			Invalidate();
     54 			break;
     55 			
     56 		case EVT_MOUSE_UP:
     57 			if (fOverButton)
     58 				Invoke();
     59 				
     60 			fMouseDown = false;
     61 			fOverButton = false;
     62 			Invalidate();
     63 			break;
     64 			
     65 		case EVT_MOUSE_ENTER:
     66 			if (fMouseDown) {
     67 				fOverButton = true; 
     68 				Invalidate();
     69 			}
     70 			
     71 			break;
     72 			
     73 		case EVT_MOUSE_LEAVE:
     74 			if (fMouseDown) {
     75 				fOverButton = false;
     76 				Invalidate();
     77 			}
     78 			
     79 			break;
     80 
     81 		default:
     82 			Canvas::EventReceived(event);
     83 	}
     84 }
     85