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