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