]> git.saurik.com Git - wxWidgets.git/blob - src/common/gdicmn.cpp
* Added source file info in utils/serialize/*
[wxWidgets.git] / src / common / gdicmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gdicmn.cpp
3 // Purpose: Common GDI classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "gdicmn.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/gdicmn.h"
24 #include "wx/brush.h"
25 #include "wx/pen.h"
26 #include "wx/bitmap.h"
27 #include "wx/icon.h"
28 #include "wx/cursor.h"
29 #include "wx/font.h"
30 #include "wx/palette.h"
31
32 #include <string.h>
33
34 #ifdef __WXMSW__
35 #include <windows.h>
36 #endif
37
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_CLASS(wxColourDatabase, wxList)
40 IMPLEMENT_DYNAMIC_CLASS(wxFontList, wxList)
41 IMPLEMENT_DYNAMIC_CLASS(wxPenList, wxList)
42 IMPLEMENT_DYNAMIC_CLASS(wxBrushList, wxList)
43 IMPLEMENT_DYNAMIC_CLASS(wxBitmapList, wxList)
44 /*
45 IMPLEMENT_DYNAMIC_CLASS(wxRect, wxObject)
46 IMPLEMENT_DYNAMIC_CLASS(wxPoint, wxObject)
47 IMPLEMENT_DYNAMIC_CLASS(wxRealPoint, wxObject)
48 */
49 #endif
50
51 wxRect::wxRect()
52 {
53 x = 0; y = 0; width = 0; height = 0;
54 }
55
56 wxRect::wxRect(long xx, long yy, long w, long h)
57 {
58 x = xx; y = yy; width = w; height = h;
59 }
60
61 wxRect::wxRect(const wxPoint& topLeft, const wxPoint& bottomRight)
62 {
63 x = topLeft.x;
64 y = topLeft.y;
65 width = bottomRight.x - topLeft.x;
66 height = bottomRight.y - topLeft.y;
67
68 if (width < 0)
69 {
70 width = -width;
71 x -= width;
72 }
73
74 if (height < 0)
75 {
76 height = -height;
77 x -= height;
78 }
79 }
80
81 wxRect::wxRect(const wxPoint& point, const wxSize& size)
82 {
83 x = point.x; y = point.y;
84 width = size.x; height = size.y;
85 }
86
87 wxRect::wxRect(const wxRect& rect)
88 {
89 x = rect.x;
90 y = rect.y;
91 width = rect.width;
92 height = rect.height;
93 }
94
95 wxRect& wxRect::operator = (const wxRect& rect)
96 {
97 x = rect.x; y = rect.y; width = rect.width; height = rect.height;
98 return *this;
99 }
100
101 bool wxRect::operator == (const wxRect& rect)
102 {
103 return ((x == rect.x) &&
104 (y == rect.y) &&
105 (width == rect.width) &&
106 (height == rect.height));
107 }
108
109 bool wxRect::operator != (const wxRect& rect)
110 {
111 return ((x != rect.x) ||
112 (y != rect.y) ||
113 (width != rect.width) ||
114 (height != rect.height));
115 }
116
117 wxColourDatabase::wxColourDatabase (int type):
118 wxList (type)
119 {
120 }
121
122 wxColourDatabase::~wxColourDatabase ()
123 {
124 // Cleanup Colour allocated in Initialize()
125 wxNode *node = First ();
126 while (node)
127 {
128 wxColour *col = (wxColour *) node->Data ();
129 wxNode *next = node->Next ();
130 delete col;
131 node = next;
132 }
133 }
134
135 // Colour database stuff
136 void wxColourDatabase::Initialize ()
137 {
138 // Don't initialize for X: colours are found
139 // in FindColour below.
140 // Added: Not all
141
142 struct cdef {
143 char *name;
144 int r,g,b;
145 };
146 cdef cc;
147 static cdef table[]={
148
149 // #ifdef __WXMSW__
150 {"AQUAMARINE",112, 219, 147},
151 {"BLACK",0, 0, 0},
152 {"BLUE", 0, 0, 255},
153 {"BLUE VIOLET", 159, 95, 159},
154 {"BROWN", 165, 42, 42},
155 {"CADET BLUE", 95, 159, 159},
156 {"CORAL", 255, 127, 0},
157 {"CORNFLOWER BLUE", 66, 66, 111},
158 {"CYAN", 0, 255, 255},
159 {"DARK GREY", 47, 47, 47}, // ?
160
161 {"DARK GREEN", 47, 79, 47},
162 {"DARK OLIVE GREEN", 79, 79, 47},
163 {"DARK ORCHID", 153, 50, 204},
164 {"DARK SLATE BLUE", 107, 35, 142},
165 {"DARK SLATE GREY", 47, 79, 79},
166 {"DARK TURQUOISE", 112, 147, 219},
167 {"DIM GREY", 84, 84, 84},
168 {"FIREBRICK", 142, 35, 35},
169 {"FOREST GREEN", 35, 142, 35},
170 {"GOLD", 204, 127, 50},
171 {"GOLDENROD", 219, 219, 112},
172 {"GREY", 128, 128, 128},
173 {"GREEN", 0, 255, 0},
174 {"GREEN YELLOW", 147, 219, 112},
175 {"INDIAN RED", 79, 47, 47},
176 {"KHAKI", 159, 159, 95},
177 {"LIGHT BLUE", 191, 216, 216},
178 {"LIGHT GREY", 192, 192, 192},
179 {"LIGHT STEEL BLUE", 143, 143, 188},
180 {"LIME GREEN", 50, 204, 50},
181 {"LIGHT MAGENTA", 255, 0, 255},
182 {"MAGENTA", 255, 0, 255},
183 {"MAROON", 142, 35, 107},
184 {"MEDIUM AQUAMARINE", 50, 204, 153},
185 {"MEDIUM GREY", 100, 100, 100},
186 {"MEDIUM BLUE", 50, 50, 204},
187 {"MEDIUM FOREST GREEN", 107, 142, 35},
188 {"MEDIUM GOLDENROD", 234, 234, 173},
189 {"MEDIUM ORCHID", 147, 112, 219},
190 {"MEDIUM SEA GREEN", 66, 111, 66},
191 {"MEDIUM SLATE BLUE", 127, 0, 255},
192 {"MEDIUM SPRING GREEN", 127, 255, 0},
193 {"MEDIUM TURQUOISE", 112, 219, 219},
194 {"MEDIUM VIOLET RED", 219, 112, 147},
195 {"MIDNIGHT BLUE", 47, 47, 79},
196 {"NAVY", 35, 35, 142},
197 {"ORANGE", 204, 50, 50},
198 {"ORANGE RED", 255, 0, 127},
199 {"ORCHID", 219, 112, 219},
200 {"PALE GREEN", 143, 188, 143},
201 {"PINK", 188, 143, 234},
202 {"PLUM", 234, 173, 234},
203 {"PURPLE", 176, 0, 255},
204 {"RED", 255, 0, 0},
205 {"SALMON", 111, 66, 66},
206 {"SEA GREEN", 35, 142, 107},
207 {"SIENNA", 142, 107, 35},
208 {"SKY BLUE", 50, 153, 204},
209 {"SLATE BLUE", 0, 127, 255},
210 {"SPRING GREEN", 0, 255, 127},
211 {"STEEL BLUE", 35, 107, 142},
212 {"TAN", 219, 147, 112},
213 {"THISTLE", 216, 191, 216},
214 {"TURQUOISE", 173, 234, 234},
215 {"VIOLET", 79, 47, 79},
216 {"VIOLET RED", 204, 50, 153},
217 {"WHEAT", 216, 216, 191},
218 {"WHITE", 255, 255, 255},
219 {"YELLOW", 255, 255, 0},
220 {"YELLOW GREEN", 153, 204, 50},
221 // #endif
222
223 #if defined(__WXGTK__) || defined(__X__)
224 {"MEDIUM GOLDENROD", 234, 234, 173},
225 {"MEDIUM FOREST GREEN", 107, 142, 35},
226 {"LIGHT MAGENTA", 255, 0, 255},
227 {"MEDIUM GREY", 100, 100, 100},
228 #endif
229
230 {0,0,0,0}
231 };
232 int i;
233 for (i=0;cc=table[i],cc.name!=0;i++)
234 {
235 Append(cc.name,new wxColour(cc.r,cc.g,cc.b));
236 }
237
238 }
239
240 /*
241 * Changed by Ian Brown, July 1994.
242 *
243 * When running under X, the Colour Database starts off empty. The X server
244 * is queried for the colour first time after which it is entered into the
245 * database. This allows our client to use the server colour database which
246 * is hopefully gamma corrected for the display being used.
247 */
248
249 wxColour *wxColourDatabase::FindColour(const wxString& colour)
250 {
251 wxNode *node = Find((char *) (const char *)colour);
252 if (node)
253 return (wxColour *)node->Data();
254
255 #ifdef __WXMSW__
256 else return NULL;
257 #endif
258
259 #ifdef __WXGTK__
260 else {
261 wxColour *col = new wxColour( colour );
262
263 if (!(col->Ok())) {
264 delete col;
265 return NULL;
266 }
267 Append( colour, col );
268 return col;
269 }
270 #endif
271
272 #ifdef __X__
273 else {
274 XColor xcolour;
275
276 #ifdef __WXMOTIF__
277 Display *display = XtDisplay(wxTheApp->topLevel) ;
278 #endif
279 #ifdef __XVIEW__
280 Xv_Screen screen = xv_get(xview_server, SERVER_NTH_SCREEN, 0);
281 Xv_opaque root_window = xv_get(screen, XV_ROOT);
282 Display *display = (Display *)xv_get(root_window, XV_DISPLAY);
283 #endif
284
285 /* MATTHEW: [4] Use wxGetMainColormap */
286 if (!XParseColor(display, wxGetMainColormap(display), colour,&xcolour))
287 return NULL;
288
289 unsigned char r = (unsigned char)(xcolour.red >> 8);
290 unsigned char g = (unsigned char)(xcolour.green >> 8);
291 unsigned char b = (unsigned char)(xcolour.blue >> 8);
292
293 wxColour *col = new wxColour(r, g, b);
294 Append(colour, col);
295
296 return col;
297 }
298 #endif
299 }
300
301 wxString wxColourDatabase::FindName (const wxColour& colour) const
302 {
303 unsigned char red = colour.Red ();
304 unsigned char green = colour.Green ();
305 unsigned char blue = colour.Blue ();
306
307 for (wxNode * node = First (); node; node = node->Next ())
308 {
309 wxColour *col = (wxColour *) node->Data ();
310
311 if (col->Red () == red && col->Green () == green && col->Blue () == blue)
312 {
313 char *found = node->key.string;
314 if (found)
315 return wxString(found);
316 }
317 }
318 return wxString(""); // Not Found
319
320 }
321
322 void
323 wxInitializeStockObjects ()
324 {
325 wxTheBrushList = new wxBrushList;
326 wxThePenList = new wxPenList;
327 wxTheFontList = new wxFontList;
328 wxTheBitmapList = new wxBitmapList;
329
330 #ifdef __WXMOTIF__
331 #endif
332 #ifdef __X__
333 wxFontPool = new XFontPool;
334 #endif
335
336 wxNORMAL_FONT = new wxFont (12, wxMODERN, wxNORMAL, wxNORMAL);
337 wxSMALL_FONT = new wxFont (10, wxSWISS, wxNORMAL, wxNORMAL);
338 wxITALIC_FONT = new wxFont (12, wxROMAN, wxITALIC, wxNORMAL);
339 wxSWISS_FONT = new wxFont (12, wxSWISS, wxNORMAL, wxNORMAL);
340
341 wxRED_PEN = new wxPen ("RED", 1, wxSOLID);
342 wxCYAN_PEN = new wxPen ("CYAN", 1, wxSOLID);
343 wxGREEN_PEN = new wxPen ("GREEN", 1, wxSOLID);
344 wxBLACK_PEN = new wxPen ("BLACK", 1, wxSOLID);
345 wxWHITE_PEN = new wxPen ("WHITE", 1, wxSOLID);
346 wxTRANSPARENT_PEN = new wxPen ("BLACK", 1, wxTRANSPARENT);
347 wxBLACK_DASHED_PEN = new wxPen ("BLACK", 1, wxSHORT_DASH);
348 wxGREY_PEN = new wxPen ("GREY", 1, wxSOLID);
349 wxMEDIUM_GREY_PEN = new wxPen ("MEDIUM GREY", 1, wxSOLID);
350 wxLIGHT_GREY_PEN = new wxPen ("LIGHT GREY", 1, wxSOLID);
351
352 wxBLUE_BRUSH = new wxBrush ("BLUE", wxSOLID);
353 wxGREEN_BRUSH = new wxBrush ("GREEN", wxSOLID);
354 wxWHITE_BRUSH = new wxBrush ("WHITE", wxSOLID);
355 wxBLACK_BRUSH = new wxBrush ("BLACK", wxSOLID);
356 wxTRANSPARENT_BRUSH = new wxBrush ("BLACK", wxTRANSPARENT);
357 wxCYAN_BRUSH = new wxBrush ("CYAN", wxSOLID);
358 wxRED_BRUSH = new wxBrush ("RED", wxSOLID);
359 wxGREY_BRUSH = new wxBrush ("GREY", wxSOLID);
360 wxMEDIUM_GREY_BRUSH = new wxBrush ("MEDIUM GREY", wxSOLID);
361 wxLIGHT_GREY_BRUSH = new wxBrush ("LIGHT GREY", wxSOLID);
362
363 wxBLACK = new wxColour ("BLACK");
364 wxWHITE = new wxColour ("WHITE");
365 wxRED = new wxColour ("RED");
366 wxBLUE = new wxColour ("BLUE");
367 wxGREEN = new wxColour ("GREEN");
368 wxCYAN = new wxColour ("CYAN");
369 wxLIGHT_GREY = new wxColour ("LIGHT GREY");
370
371 wxSTANDARD_CURSOR = new wxCursor (wxCURSOR_ARROW);
372 wxHOURGLASS_CURSOR = new wxCursor (wxCURSOR_WAIT);
373 wxCROSS_CURSOR = new wxCursor (wxCURSOR_CROSS);
374 }
375
376 void
377 wxDeleteStockObjects ()
378 {
379 DELETEP(wxNORMAL_FONT);
380 DELETEP(wxSMALL_FONT);
381 DELETEP(wxITALIC_FONT);
382 DELETEP(wxSWISS_FONT);
383
384 DELETEP(wxRED_PEN);
385 DELETEP(wxCYAN_PEN);
386 DELETEP(wxGREEN_PEN);
387 DELETEP(wxBLACK_PEN);
388 DELETEP(wxWHITE_PEN);
389 DELETEP(wxTRANSPARENT_PEN);
390 DELETEP(wxBLACK_DASHED_PEN);
391 DELETEP(wxGREY_PEN);
392 DELETEP(wxMEDIUM_GREY_PEN);
393 DELETEP(wxLIGHT_GREY_PEN);
394
395 DELETEP(wxBLUE_BRUSH);
396 DELETEP(wxGREEN_BRUSH);
397 DELETEP(wxWHITE_BRUSH);
398 DELETEP(wxBLACK_BRUSH);
399 DELETEP(wxTRANSPARENT_BRUSH);
400 DELETEP(wxCYAN_BRUSH);
401 DELETEP(wxRED_BRUSH);
402 DELETEP(wxGREY_BRUSH);
403 DELETEP(wxMEDIUM_GREY_BRUSH);
404 DELETEP(wxLIGHT_GREY_BRUSH);
405
406 DELETEP(wxBLACK);
407 DELETEP(wxWHITE);
408 DELETEP(wxRED);
409 DELETEP(wxBLUE);
410 DELETEP(wxGREEN);
411 DELETEP(wxCYAN);
412 DELETEP(wxLIGHT_GREY);
413
414 DELETEP(wxSTANDARD_CURSOR);
415 DELETEP(wxHOURGLASS_CURSOR);
416 DELETEP(wxCROSS_CURSOR);
417 }
418
419 wxBitmapList::wxBitmapList ()
420 {
421 }
422
423 wxBitmapList::~wxBitmapList ()
424 {
425 wxNode *node = First ();
426 while (node)
427 {
428 wxBitmap *bitmap = (wxBitmap *) node->Data ();
429 wxNode *next = node->Next ();
430 delete bitmap;
431 // bitmap->FreeResource(TRUE);
432 node = next;
433 }
434 }
435
436 // Pen and Brush lists
437 wxPenList::~wxPenList ()
438 {
439 wxNode *node = First ();
440 while (node)
441 {
442 wxPen *pen = (wxPen *) node->Data ();
443 wxNode *next = node->Next ();
444 delete pen;
445 // pen->FreeResource(TRUE);
446 node = next;
447 }
448 }
449
450 void wxPenList::AddPen (wxPen * pen)
451 {
452 Append (pen);
453 }
454
455 void wxPenList::RemovePen (wxPen * pen)
456 {
457 DeleteObject (pen);
458 }
459
460 wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
461 {
462 for (wxNode * node = First (); node; node = node->Next ())
463 {
464 wxPen *each_pen = (wxPen *) node->Data ();
465 if (each_pen && each_pen->GetVisible() &&
466 each_pen->GetWidth () == width &&
467 each_pen->GetStyle () == style &&
468 each_pen->GetColour ().Red () == colour.Red () &&
469 each_pen->GetColour ().Green () == colour.Green () &&
470 each_pen->GetColour ().Blue () == colour.Blue ())
471 return each_pen;
472 }
473 wxPen *pen = new wxPen (colour, width, style);
474
475 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
476 // because we created it within FindOrCreatePen. Safeguards against
477 // returning a pointer to an automatic variable and hanging on to it
478 // (dangling pointer).
479 pen->SetVisible(TRUE);
480 return pen;
481 }
482
483 wxPen *wxPenList::FindOrCreatePen (const wxString& colour, int width, int style)
484 {
485 wxColour *the_colour = wxTheColourDatabase->FindColour (colour);
486 if (the_colour)
487 return FindOrCreatePen (*the_colour, width, style);
488 else
489 return NULL;
490 }
491
492 wxBrushList::~wxBrushList ()
493 {
494 wxNode *node = First ();
495 while (node)
496 {
497 wxBrush *brush = (wxBrush *) node->Data ();
498 wxNode *next = node->Next ();
499 delete brush;
500 node = next;
501 }
502 }
503
504 void wxBrushList::AddBrush (wxBrush * brush)
505 {
506 Append (brush);
507 }
508
509 wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
510 {
511 for (wxNode * node = First (); node; node = node->Next ())
512 {
513 wxBrush *each_brush = (wxBrush *) node->Data ();
514 if (each_brush && each_brush->GetVisible() &&
515 each_brush->GetStyle () == style &&
516 each_brush->GetColour ().Red () == colour.Red () &&
517 each_brush->GetColour ().Green () == colour.Green () &&
518 each_brush->GetColour ().Blue () == colour.Blue ())
519 return each_brush;
520 }
521 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
522 // because we created it within FindOrCreateBrush. Safeguards against
523 // returning a pointer to an automatic variable and hanging on to it
524 // (dangling pointer).
525 wxBrush *brush = new wxBrush (colour, style);
526 brush->SetVisible(TRUE);
527 return brush;
528 }
529
530 wxBrush *wxBrushList::FindOrCreateBrush (const wxString& colour, int style)
531 {
532 wxColour *the_colour = wxTheColourDatabase->FindColour (colour);
533 if (the_colour)
534 return FindOrCreateBrush (*the_colour, style);
535 else
536 return NULL;
537 }
538
539 void wxBrushList::RemoveBrush (wxBrush * brush)
540 {
541 DeleteObject (brush);
542 }
543
544 wxFontList::~wxFontList ()
545 {
546 #ifdef __WXMSW__
547 wxNode *node = First ();
548 while (node)
549 {
550 /*
551 wxFont *font = (wxFont *) node->Data ();
552 wxNode *next = node->Next ();
553 delete font;
554 node = next;
555 */
556 // New for 2.0: don't delete the font (it may be a member
557 // of a wxDC, for example)
558 wxFont *font = (wxFont *) node->Data ();
559 wxNode *next = node->Next ();
560
561 // Force the font to be deleted
562 font->FreeResource(TRUE);
563 node = next;
564 }
565 #endif
566 }
567
568 void wxFontList::AddFont (wxFont * font)
569 {
570 Append (font);
571 }
572
573 void wxFontList::RemoveFont (wxFont * font)
574 {
575 DeleteObject (font);
576 }
577
578 wxFont *wxFontList::
579 FindOrCreateFont (int PointSize, int FamilyOrFontId, int Style, int Weight, bool underline, const wxString& Face)
580 {
581 for (wxNode * node = First (); node; node = node->Next ())
582 {
583 wxFont *each_font = (wxFont *) node->Data ();
584 if (each_font && each_font->GetVisible() && each_font->Ok() &&
585 each_font->GetPointSize () == PointSize &&
586 each_font->GetStyle () == Style &&
587 each_font->GetWeight () == Weight &&
588 each_font->GetUnderlined () == underline &&
589 #if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
590 each_font->GetFontId () == FamilyOrFontId) /* New font system */
591 #else
592 each_font->GetFamily () == FamilyOrFontId &&
593 (!each_font->GetFaceName() || each_font->GetFaceName() == Face))
594 #endif
595 return each_font;
596 }
597 wxFont *font = new wxFont (PointSize, FamilyOrFontId, Style, Weight, underline, Face);
598 font->SetVisible(TRUE);
599 return font;
600 }
601
602 void wxBitmapList::AddBitmap(wxBitmap *bitmap)
603 { Append(bitmap); }
604 void wxBitmapList::RemoveBitmap(wxBitmap *bitmap)
605 { DeleteObject(bitmap); }
606
607 wxSize wxGetDisplaySize()
608 {
609 int x, y;
610 wxDisplaySize(& x, & y);
611 return wxSize(x, y);
612 }
613