1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
///////////////////////////////////////////////////////////////////////////////
// $Id: BasicWidget.cxx,v 1.3 2000/01/10 23:31:38 bwmott Exp $
///////////////////////////////////////////////////////////////////////////////
//
// BasicWidget.cxx - Abstract base class for all widgets
//
//
// Bradford W. Mott
// Copyright (C) 1994
// December 11,1994
//
///////////////////////////////////////////////////////////////////////////////
// $Log: BasicWidget.cxx,v $
// Revision 1.3 2000/01/10 23:31:38 bwmott
// Modified pixmap operations to take the display depth into account
//
// Revision 1.2 1996/01/06 05:09:12 bwmott
// Changed all NULLs to 0
//
// Revision 1.1 1995/01/12 02:09:06 bmott
// Initial revision
//
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <assert.h>
#include "BasicWidget.hxx"
#include "UIApplication.hxx"
#include "ContainerWidget.hxx"
///////////////////////////////////////////////////////////////////////////////
// Protected constructor to prevent instantiation
///////////////////////////////////////////////////////////////////////////////
BasicWidget::BasicWidget(ContainerWidget *const parent, const char *const name,
int x, int y, int width, int height)
: myName(name),
myParent(parent),
myX(x),
myY(y),
myWidth(width),
myHeight(height)
{
assert(name != 0);
// If I have a parent then tell him about myself
if(myParent != 0)
{
myParent->addChild(this);
}
// Invalidate my window
myWindow = 0;
// I start off unmanaged
myManageState = Unmanaged;
// Setup my default foreground and background colors
myForeground = application->blackPixel();
myBackground = application->whitePixel();
}
///////////////////////////////////////////////////////////////////////////////
// Destructor
///////////////////////////////////////////////////////////////////////////////
BasicWidget::~BasicWidget()
{
// If I have a parent then tell him I'm going away
if(myParent != 0)
{
myParent->removeChild(this);
}
// Close myWindow
if(myWindow)
XDestroyWindow(application->display(), myWindow);
}
///////////////////////////////////////////////////////////////////////////////
// Manage the entire widget subtree that I represented
///////////////////////////////////////////////////////////////////////////////
void BasicWidget::manage()
{
assert(myWindow != 0);
// Manage myself
XMapWindow(application->display(), myWindow);
myManageState = Managed;
}
///////////////////////////////////////////////////////////////////////////////
// Unmanage the entire widget subtree that I represented
///////////////////////////////////////////////////////////////////////////////
void BasicWidget::unmanage()
{
assert(myWindow != 0);
// Unmanage myself
XUnmapWindow(application->display(), myWindow);
myManageState = Unmanaged;
}
///////////////////////////////////////////////////////////////////////////////
// Set my background to the named color
///////////////////////////////////////////////////////////////////////////////
void BasicWidget::background(const char* name)
{
XColor desiredColor, color;
int r,g,b;
const int delta = 7680;
XAllocNamedColor(application->display(), application->colormap(),
name, &color, &desiredColor);
myBackground = color.pixel;
XSetWindowBackground(application->display(), myWindow, color.pixel);
if ((r = desiredColor.red - delta) < 0)
r = 0;
if ((g = desiredColor.green - delta) < 0)
g = 0;
if ((b = desiredColor.blue - delta) < 0)
b = 0;
color.red = r;
color.green = g;
color.blue = b;
XAllocColor(application->display(), application->colormap(), &color);
myBackgroundLow = color.pixel;
if ((r = desiredColor.red + delta) > 65535)
r = 65535;
if ((g = desiredColor.green + delta) > 65535)
g = 65535;
if ((b = desiredColor.blue + delta) > 65535)
b = 65535;
color.red = r;
color.green = g;
color.blue = b;
XAllocColor(application->display(), application->colormap(), &color);
myBackgroundHigh = color.pixel;
// Make sure the window refreshes
XClearWindow(application->display(), myWindow);
}
///////////////////////////////////////////////////////////////////////////////
// Set my background to the given sprite
///////////////////////////////////////////////////////////////////////////////
void BasicWidget::background(const Sprite *const sprite)
{
// Create pixmap
Pixmap pixmap = XCreatePixmap(application->display(), myWindow,
sprite->width(), sprite->height(), application->depth());
// Copy image into pixmap
XPutImage(application->display(), pixmap, application->gc(),
sprite->image(), 0, 0, 0, 0, sprite->width(), sprite->height());
// Install the pixmap on myself
XSetWindowBackgroundPixmap(application->display(), myWindow, pixmap);
// Free the pixmap
XFreePixmap(application->display(), pixmap);
// Make sure the window refreshes
XClearWindow(application->display(), myWindow);
}
///////////////////////////////////////////////////////////////////////////////
// Set my foreground to the named color
///////////////////////////////////////////////////////////////////////////////
void BasicWidget::foreground(const char* name)
{
XColor color;
XAllocNamedColor(application->display(), application->colormap(),
name, &color, &color);
myForeground = color.pixel;
}
///////////////////////////////////////////////////////////////////////////////
// Called whenever an event arrives for me
///////////////////////////////////////////////////////////////////////////////
void BasicWidget::handleEvent(XEvent* event)
{
// The default action is to do nothing
}
///////////////////////////////////////////////////////////////////////////////
// If I manage the given window then answer myself
///////////////////////////////////////////////////////////////////////////////
BasicWidget* BasicWidget::findWidget(Window window)
{
if (myWindow == window)
return(this);
else
return((BasicWidget*)0);
}
///////////////////////////////////////////////////////////////////////////////
// Resize myself to the given width and height
///////////////////////////////////////////////////////////////////////////////
void BasicWidget::resize(int width, int height)
{
myWidth = width;
myHeight = height;
// Tell X to resize the widget
XResizeWindow(application->display(), myWindow, width, height);
}
///////////////////////////////////////////////////////////////////////////////
// Move myself to the given x and y location
///////////////////////////////////////////////////////////////////////////////
void BasicWidget::move(int x, int y)
{
myX = x;
myY = y;
// Tell X to resize the widget
XMoveWindow(application->display(), myWindow, x, y);
}
|