]> git.saurik.com Git - wxWidgets.git/blob - src/common/gdicmn.cpp
Removed redundant auto-setting code in wxDC; corrected utilscmn.cpp pragma;
[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 __WINDOWS__
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 __WINDOWS__
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(__GTK__) || 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 __WINDOWS__
256 else return NULL;
257 #endif
258
259 #ifdef __GTK__
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 __MOTIF__
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 if (col->Red () == red && col->Green () == green && col->Blue () == blue)
311 {
312 char *found = node->key.string;
313 if (found)
314 return wxString(found);
315 }
316 }
317 return wxString(""); // Not Found
318
319 }
320
321 void
322 wxInitializeStockObjects ()
323 {
324 wxTheBrushList = new wxBrushList;
325 wxThePenList = new wxPenList;
326 wxTheFontList = new wxFontList;
327 wxTheBitmapList = new wxBitmapList;
328
329 #ifdef __MOTIF__
330 #endif
331 #ifdef __X__
332 wxFontPool = new XFontPool;
333 #endif
334
335 wxNORMAL_FONT = new wxFont (12, wxMODERN, wxNORMAL, wxNORMAL);
336 wxSMALL_FONT = new wxFont (10, wxSWISS, wxNORMAL, wxNORMAL);
337 wxITALIC_FONT = new wxFont (12, wxROMAN, wxITALIC, wxNORMAL);
338 wxSWISS_FONT = new wxFont (12, wxSWISS, wxNORMAL, wxNORMAL);
339
340 wxRED_PEN = new wxPen ("RED", 1, wxSOLID);
341 wxCYAN_PEN = new wxPen ("CYAN", 1, wxSOLID);
342 wxGREEN_PEN = new wxPen ("GREEN", 1, wxSOLID);
343 wxBLACK_PEN = new wxPen ("BLACK", 1, wxSOLID);
344 wxWHITE_PEN = new wxPen ("WHITE", 1, wxSOLID);
345 wxTRANSPARENT_PEN = new wxPen ("BLACK", 1, wxTRANSPARENT);
346 wxBLACK_DASHED_PEN = new wxPen ("BLACK", 1, wxSHORT_DASH);
347 wxGREY_PEN = new wxPen ("GREY", 1, wxSOLID);
348 wxMEDIUM_GREY_PEN = new wxPen ("MEDIUM GREY", 1, wxSOLID);
349 wxLIGHT_GREY_PEN = new wxPen ("LIGHT GREY", 1, wxSOLID);
350
351 wxBLUE_BRUSH = new wxBrush ("BLUE", wxSOLID);
352 wxGREEN_BRUSH = new wxBrush ("GREEN", wxSOLID);
353 wxWHITE_BRUSH = new wxBrush ("WHITE", wxSOLID);
354 wxBLACK_BRUSH = new wxBrush ("BLACK", wxSOLID);
355 wxTRANSPARENT_BRUSH = new wxBrush ("BLACK", wxTRANSPARENT);
356 wxCYAN_BRUSH = new wxBrush ("CYAN", wxSOLID);
357 wxRED_BRUSH = new wxBrush ("RED", wxSOLID);
358 wxGREY_BRUSH = new wxBrush ("GREY", wxSOLID);
359 wxMEDIUM_GREY_BRUSH = new wxBrush ("MEDIUM GREY", wxSOLID);
360 wxLIGHT_GREY_BRUSH = new wxBrush ("LIGHT GREY", wxSOLID);
361
362 wxBLACK = new wxColour ("BLACK");
363 wxWHITE = new wxColour ("WHITE");
364 wxRED = new wxColour ("RED");
365 wxBLUE = new wxColour ("BLUE");
366 wxGREEN = new wxColour ("GREEN");
367 wxCYAN = new wxColour ("CYAN");
368 wxLIGHT_GREY = new wxColour ("LIGHT GREY");
369
370 wxSTANDARD_CURSOR = new wxCursor (wxCURSOR_ARROW);
371 wxHOURGLASS_CURSOR = new wxCursor (wxCURSOR_WAIT);
372 wxCROSS_CURSOR = new wxCursor (wxCURSOR_CROSS);
373 }
374
375 void
376 wxDeleteStockObjects ()
377 {
378 DELETEP(wxNORMAL_FONT);
379 DELETEP(wxSMALL_FONT);
380 DELETEP(wxITALIC_FONT);
381 DELETEP(wxSWISS_FONT);
382
383 DELETEP(wxRED_PEN);
384 DELETEP(wxCYAN_PEN);
385 DELETEP(wxGREEN_PEN);
386 DELETEP(wxBLACK_PEN);
387 DELETEP(wxWHITE_PEN);
388 DELETEP(wxTRANSPARENT_PEN);
389 DELETEP(wxBLACK_DASHED_PEN);
390 DELETEP(wxGREY_PEN);
391 DELETEP(wxMEDIUM_GREY_PEN);
392 DELETEP(wxLIGHT_GREY_PEN);
393
394 DELETEP(wxBLUE_BRUSH);
395 DELETEP(wxGREEN_BRUSH);
396 DELETEP(wxWHITE_BRUSH);
397 DELETEP(wxBLACK_BRUSH);
398 DELETEP(wxTRANSPARENT_BRUSH);
399 DELETEP(wxCYAN_BRUSH);
400 DELETEP(wxRED_BRUSH);
401 DELETEP(wxGREY_BRUSH);
402 DELETEP(wxMEDIUM_GREY_BRUSH);
403 DELETEP(wxLIGHT_GREY_BRUSH);
404
405 DELETEP(wxBLACK);
406 DELETEP(wxWHITE);
407 DELETEP(wxRED);
408 DELETEP(wxBLUE);
409 DELETEP(wxGREEN);
410 DELETEP(wxCYAN);
411 DELETEP(wxLIGHT_GREY);
412
413 DELETEP(wxSTANDARD_CURSOR);
414 DELETEP(wxHOURGLASS_CURSOR);
415 DELETEP(wxCROSS_CURSOR);
416 }
417
418 wxBitmapList::wxBitmapList ()
419 {
420 }
421
422 wxBitmapList::~wxBitmapList ()
423 {
424 wxNode *node = First ();
425 while (node)
426 {
427 wxBitmap *bitmap = (wxBitmap *) node->Data ();
428 wxNode *next = node->Next ();
429 delete bitmap;
430 // bitmap->FreeResource(TRUE);
431 node = next;
432 }
433 }
434
435 // Pen and Brush lists
436 wxPenList::~wxPenList ()
437 {
438 wxNode *node = First ();
439 while (node)
440 {
441 wxPen *pen = (wxPen *) node->Data ();
442 wxNode *next = node->Next ();
443 delete pen;
444 // pen->FreeResource(TRUE);
445 node = next;
446 }
447 }
448
449 void wxPenList::AddPen (wxPen * pen)
450 {
451 Append (pen);
452 }
453
454 void wxPenList::RemovePen (wxPen * pen)
455 {
456 DeleteObject (pen);
457 }
458
459 wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
460 {
461 for (wxNode * node = First (); node; node = node->Next ())
462 {
463 wxPen *each_pen = (wxPen *) node->Data ();
464 if (each_pen && each_pen->GetVisible() &&
465 each_pen->GetWidth () == width &&
466 each_pen->GetStyle () == style &&
467 each_pen->GetColour ().Red () == colour.Red () &&
468 each_pen->GetColour ().Green () == colour.Green () &&
469 each_pen->GetColour ().Blue () == colour.Blue ())
470 return each_pen;
471 }
472 wxPen *pen = new wxPen (colour, width, style);
473
474 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
475 // because we created it within FindOrCreatePen. Safeguards against
476 // returning a pointer to an automatic variable and hanging on to it
477 // (dangling pointer).
478 pen->SetVisible(TRUE);
479 return pen;
480 }
481
482 wxPen *wxPenList::FindOrCreatePen (const wxString& colour, int width, int style)
483 {
484 wxColour *the_colour = wxTheColourDatabase->FindColour (colour);
485 if (the_colour)
486 return FindOrCreatePen (*the_colour, width, style);
487 else
488 return NULL;
489 }
490
491 wxBrushList::~wxBrushList ()
492 {
493 wxNode *node = First ();
494 while (node)
495 {
496 wxBrush *brush = (wxBrush *) node->Data ();
497 wxNode *next = node->Next ();
498 delete brush;
499 node = next;
500 }
501 }
502
503 void wxBrushList::AddBrush (wxBrush * brush)
504 {
505 Append (brush);
506 }
507
508 wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
509 {
510 for (wxNode * node = First (); node; node = node->Next ())
511 {
512 wxBrush *each_brush = (wxBrush *) node->Data ();
513 if (each_brush && each_brush->GetVisible() &&
514 each_brush->GetStyle () == style &&
515 each_brush->GetColour ().Red () == colour.Red () &&
516 each_brush->GetColour ().Green () == colour.Green () &&
517 each_brush->GetColour ().Blue () == colour.Blue ())
518 return each_brush;
519 }
520 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
521 // because we created it within FindOrCreateBrush. Safeguards against
522 // returning a pointer to an automatic variable and hanging on to it
523 // (dangling pointer).
524 wxBrush *brush = new wxBrush (colour, style);
525 brush->SetVisible(TRUE);
526 return brush;
527 }
528
529 wxBrush *wxBrushList::FindOrCreateBrush (const wxString& colour, int style)
530 {
531 wxColour *the_colour = wxTheColourDatabase->FindColour (colour);
532 if (the_colour)
533 return FindOrCreateBrush (*the_colour, style);
534 else
535 return NULL;
536 }
537
538 void wxBrushList::RemoveBrush (wxBrush * brush)
539 {
540 DeleteObject (brush);
541 }
542
543 wxFontList::~wxFontList ()
544 {
545 #ifdef __WINDOWS__
546 wxNode *node = First ();
547 while (node)
548 {
549 /*
550 wxFont *font = (wxFont *) node->Data ();
551 wxNode *next = node->Next ();
552 delete font;
553 node = next;
554 */
555 // New for 2.0: don't delete the font (it may be a member
556 // of a wxDC, for example)
557 wxFont *font = (wxFont *) node->Data ();
558 wxNode *next = node->Next ();
559
560 // Force the font to be deleted
561 font->FreeResource(TRUE);
562 node = next;
563 }
564 #endif
565 }
566
567 void wxFontList::AddFont (wxFont * font)
568 {
569 Append (font);
570 }
571
572 void wxFontList::RemoveFont (wxFont * font)
573 {
574 DeleteObject (font);
575 }
576
577 wxFont *wxFontList::
578 FindOrCreateFont (int PointSize, int FamilyOrFontId, int Style, int Weight, bool underline, const wxString& Face)
579 {
580 for (wxNode * node = First (); node; node = node->Next ())
581 {
582 wxFont *each_font = (wxFont *) node->Data ();
583 if (each_font && each_font->GetVisible() && each_font->Ok() &&
584 each_font->GetPointSize () == PointSize &&
585 each_font->GetStyle () == Style &&
586 each_font->GetWeight () == Weight &&
587 each_font->GetUnderlined () == underline &&
588 #if defined(__X__) || (defined(__WINDOWS__) && USE_PORTABLE_FONTS_IN_MSW)
589 each_font->GetFontId () == FamilyOrFontId) /* New font system */
590 #else
591 each_font->GetFamily () == FamilyOrFontId &&
592 (!each_font->GetFaceName() || each_font->GetFaceName() == Face))
593 #endif
594 return each_font;
595 }
596 wxFont *font = new wxFont (PointSize, FamilyOrFontId, Style, Weight, underline, Face);
597 font->SetVisible(TRUE);
598 return font;
599 }
600
601 void wxBitmapList::AddBitmap(wxBitmap *bitmap)
602 { Append(bitmap); }
603 void wxBitmapList::RemoveBitmap(wxBitmap *bitmap)
604 { DeleteObject(bitmap); }
605
606 wxSize wxGetDisplaySize()
607 {
608 int x, y;
609 wxDisplaySize(& x, & y);
610 return wxSize(x, y);
611 }
612