]> git.saurik.com Git - wxWidgets.git/blob - src/common/gdicmn.cpp
fixed GetSelection() inversion bug
[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 // TODO for other implementations. This should really go into
261 // platform-specific directories.
262 #ifdef __WXSTUBS__
263 else return NULL;
264 #endif
265
266 #ifdef __WXGTK__
267 else {
268 wxColour *col = new wxColour( colour );
269
270 if (!(col->Ok())) {
271 delete col;
272 return NULL;
273 }
274 Append( colour, col );
275 return col;
276 }
277 #endif
278
279 #ifdef __X__
280 else {
281 XColor xcolour;
282
283 #ifdef __WXMOTIF__
284 Display *display = XtDisplay(wxTheApp->topLevel) ;
285 #endif
286 #ifdef __XVIEW__
287 Xv_Screen screen = xv_get(xview_server, SERVER_NTH_SCREEN, 0);
288 Xv_opaque root_window = xv_get(screen, XV_ROOT);
289 Display *display = (Display *)xv_get(root_window, XV_DISPLAY);
290 #endif
291
292 /* MATTHEW: [4] Use wxGetMainColormap */
293 if (!XParseColor(display, wxGetMainColormap(display), colour,&xcolour))
294 return NULL;
295
296 unsigned char r = (unsigned char)(xcolour.red >> 8);
297 unsigned char g = (unsigned char)(xcolour.green >> 8);
298 unsigned char b = (unsigned char)(xcolour.blue >> 8);
299
300 wxColour *col = new wxColour(r, g, b);
301 Append(colour, col);
302
303 return col;
304 }
305 #endif
306 }
307
308 wxString wxColourDatabase::FindName (const wxColour& colour) const
309 {
310 unsigned char red = colour.Red ();
311 unsigned char green = colour.Green ();
312 unsigned char blue = colour.Blue ();
313
314 for (wxNode * node = First (); node; node = node->Next ())
315 {
316 wxColour *col = (wxColour *) node->Data ();
317
318 if (col->Red () == red && col->Green () == green && col->Blue () == blue)
319 {
320 char *found = node->key.string;
321 if (found)
322 return wxString(found);
323 }
324 }
325 return wxString(""); // Not Found
326
327 }
328
329 void wxInitializeStockLists () {
330 wxTheBrushList = new wxBrushList;
331 wxThePenList = new wxPenList;
332 wxTheFontList = new wxFontList;
333 wxTheBitmapList = new wxBitmapList;
334 }
335
336 void wxInitializeStockObjects ()
337 {
338 #ifdef __WXMOTIF__
339 #endif
340 #ifdef __X__
341 wxFontPool = new XFontPool;
342 #endif
343
344 wxNORMAL_FONT = new wxFont (12, wxMODERN, wxNORMAL, wxNORMAL);
345 wxSMALL_FONT = new wxFont (10, wxSWISS, wxNORMAL, wxNORMAL);
346 wxITALIC_FONT = new wxFont (12, wxROMAN, wxITALIC, wxNORMAL);
347 wxSWISS_FONT = new wxFont (12, wxSWISS, wxNORMAL, wxNORMAL);
348
349 wxRED_PEN = new wxPen ("RED", 1, wxSOLID);
350 wxCYAN_PEN = new wxPen ("CYAN", 1, wxSOLID);
351 wxGREEN_PEN = new wxPen ("GREEN", 1, wxSOLID);
352 wxBLACK_PEN = new wxPen ("BLACK", 1, wxSOLID);
353 wxWHITE_PEN = new wxPen ("WHITE", 1, wxSOLID);
354 wxTRANSPARENT_PEN = new wxPen ("BLACK", 1, wxTRANSPARENT);
355 wxBLACK_DASHED_PEN = new wxPen ("BLACK", 1, wxSHORT_DASH);
356 wxGREY_PEN = new wxPen ("GREY", 1, wxSOLID);
357 wxMEDIUM_GREY_PEN = new wxPen ("MEDIUM GREY", 1, wxSOLID);
358 wxLIGHT_GREY_PEN = new wxPen ("LIGHT GREY", 1, wxSOLID);
359
360 wxBLUE_BRUSH = new wxBrush ("BLUE", wxSOLID);
361 wxGREEN_BRUSH = new wxBrush ("GREEN", wxSOLID);
362 wxWHITE_BRUSH = new wxBrush ("WHITE", wxSOLID);
363 wxBLACK_BRUSH = new wxBrush ("BLACK", wxSOLID);
364 wxTRANSPARENT_BRUSH = new wxBrush ("BLACK", wxTRANSPARENT);
365 wxCYAN_BRUSH = new wxBrush ("CYAN", wxSOLID);
366 wxRED_BRUSH = new wxBrush ("RED", wxSOLID);
367 wxGREY_BRUSH = new wxBrush ("GREY", wxSOLID);
368 wxMEDIUM_GREY_BRUSH = new wxBrush ("MEDIUM GREY", wxSOLID);
369 wxLIGHT_GREY_BRUSH = new wxBrush ("LIGHT GREY", wxSOLID);
370
371 wxBLACK = new wxColour ("BLACK");
372 wxWHITE = new wxColour ("WHITE");
373 wxRED = new wxColour ("RED");
374 wxBLUE = new wxColour ("BLUE");
375 wxGREEN = new wxColour ("GREEN");
376 wxCYAN = new wxColour ("CYAN");
377 wxLIGHT_GREY = new wxColour ("LIGHT GREY");
378
379 wxSTANDARD_CURSOR = new wxCursor (wxCURSOR_ARROW);
380 wxHOURGLASS_CURSOR = new wxCursor (wxCURSOR_WAIT);
381 wxCROSS_CURSOR = new wxCursor (wxCURSOR_CROSS);
382 }
383
384 void
385 wxDeleteStockObjects ()
386 {
387
388 wxDELETE(wxNORMAL_FONT);
389 wxDELETE(wxSMALL_FONT);
390 wxDELETE(wxITALIC_FONT);
391 wxDELETE(wxSWISS_FONT);
392
393 wxDELETE(wxRED_PEN);
394 wxDELETE(wxCYAN_PEN);
395 wxDELETE(wxGREEN_PEN);
396 wxDELETE(wxBLACK_PEN);
397 wxDELETE(wxWHITE_PEN);
398 wxDELETE(wxTRANSPARENT_PEN);
399 wxDELETE(wxBLACK_DASHED_PEN);
400 wxDELETE(wxGREY_PEN);
401 wxDELETE(wxMEDIUM_GREY_PEN);
402 wxDELETE(wxLIGHT_GREY_PEN);
403
404 wxDELETE(wxBLUE_BRUSH);
405 wxDELETE(wxGREEN_BRUSH);
406 wxDELETE(wxWHITE_BRUSH);
407 wxDELETE(wxBLACK_BRUSH);
408 wxDELETE(wxTRANSPARENT_BRUSH);
409 wxDELETE(wxCYAN_BRUSH);
410 wxDELETE(wxRED_BRUSH);
411 wxDELETE(wxGREY_BRUSH);
412 wxDELETE(wxMEDIUM_GREY_BRUSH);
413 wxDELETE(wxLIGHT_GREY_BRUSH);
414
415 wxDELETE(wxBLACK);
416 wxDELETE(wxWHITE);
417 wxDELETE(wxRED);
418 wxDELETE(wxBLUE);
419 wxDELETE(wxGREEN);
420 wxDELETE(wxCYAN);
421 wxDELETE(wxLIGHT_GREY);
422
423 wxDELETE(wxSTANDARD_CURSOR);
424 wxDELETE(wxHOURGLASS_CURSOR);
425 wxDELETE(wxCROSS_CURSOR);
426 }
427
428 void wxDeleteStockLists() {
429 wxDELETE(wxTheBrushList);
430 wxDELETE(wxThePenList);
431 wxDELETE(wxTheFontList);
432 wxDELETE(wxTheBitmapList);
433 }
434
435 wxBitmapList::wxBitmapList ()
436 {
437 }
438
439 wxBitmapList::~wxBitmapList ()
440 {
441 wxNode *node = First ();
442 while (node)
443 {
444 wxBitmap *bitmap = (wxBitmap *) node->Data ();
445 wxNode *next = node->Next ();
446 delete bitmap;
447 // bitmap->FreeResource(TRUE);
448 node = next;
449 }
450 }
451
452 // Pen and Brush lists
453 wxPenList::~wxPenList ()
454 {
455 wxNode *node = First ();
456 while (node)
457 {
458 wxPen *pen = (wxPen *) node->Data ();
459 wxNode *next = node->Next ();
460 delete pen;
461 // pen->FreeResource(TRUE);
462 node = next;
463 }
464 }
465
466 void wxPenList::AddPen (wxPen * pen)
467 {
468 Append (pen);
469 }
470
471 void wxPenList::RemovePen (wxPen * pen)
472 {
473 DeleteObject (pen);
474 }
475
476 wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
477 {
478 for (wxNode * node = First (); node; node = node->Next ())
479 {
480 wxPen *each_pen = (wxPen *) node->Data ();
481 if (each_pen && each_pen->GetVisible() &&
482 each_pen->GetWidth () == width &&
483 each_pen->GetStyle () == style &&
484 each_pen->GetColour ().Red () == colour.Red () &&
485 each_pen->GetColour ().Green () == colour.Green () &&
486 each_pen->GetColour ().Blue () == colour.Blue ())
487 return each_pen;
488 }
489 wxPen *pen = new wxPen (colour, width, style);
490
491 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
492 // because we created it within FindOrCreatePen. Safeguards against
493 // returning a pointer to an automatic variable and hanging on to it
494 // (dangling pointer).
495 pen->SetVisible(TRUE);
496 return pen;
497 }
498
499 wxPen *wxPenList::FindOrCreatePen (const wxString& colour, int width, int style)
500 {
501 wxColour *the_colour = wxTheColourDatabase->FindColour (colour);
502 if (the_colour)
503 return FindOrCreatePen (*the_colour, width, style);
504 else
505 return NULL;
506 }
507
508 wxBrushList::~wxBrushList ()
509 {
510 wxNode *node = First ();
511 while (node)
512 {
513 wxBrush *brush = (wxBrush *) node->Data ();
514 wxNode *next = node->Next ();
515 delete brush;
516 node = next;
517 }
518 }
519
520 void wxBrushList::AddBrush (wxBrush * brush)
521 {
522 Append (brush);
523 }
524
525 wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
526 {
527 for (wxNode * node = First (); node; node = node->Next ())
528 {
529 wxBrush *each_brush = (wxBrush *) node->Data ();
530 if (each_brush && each_brush->GetVisible() &&
531 each_brush->GetStyle () == style &&
532 each_brush->GetColour ().Red () == colour.Red () &&
533 each_brush->GetColour ().Green () == colour.Green () &&
534 each_brush->GetColour ().Blue () == colour.Blue ())
535 return each_brush;
536 }
537 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
538 // because we created it within FindOrCreateBrush. Safeguards against
539 // returning a pointer to an automatic variable and hanging on to it
540 // (dangling pointer).
541 wxBrush *brush = new wxBrush (colour, style);
542 brush->SetVisible(TRUE);
543 return brush;
544 }
545
546 wxBrush *wxBrushList::FindOrCreateBrush (const wxString& colour, int style)
547 {
548 wxColour *the_colour = wxTheColourDatabase->FindColour (colour);
549 if (the_colour)
550 return FindOrCreateBrush (*the_colour, style);
551 else
552 return NULL;
553 }
554
555 void wxBrushList::RemoveBrush (wxBrush * brush)
556 {
557 DeleteObject (brush);
558 }
559
560 wxFontList::~wxFontList ()
561 {
562 #ifdef __WXMSW__
563 wxNode *node = First ();
564 while (node)
565 {
566 /*
567 wxFont *font = (wxFont *) node->Data ();
568 wxNode *next = node->Next ();
569 delete font;
570 node = next;
571 */
572 // New for 2.0: don't delete the font (it may be a member
573 // of a wxDC, for example)
574 wxFont *font = (wxFont *) node->Data ();
575 wxNode *next = node->Next ();
576
577 // Force the font to be deleted
578 font->FreeResource(TRUE);
579 node = next;
580 }
581 #endif
582 }
583
584 void wxFontList::AddFont (wxFont * font)
585 {
586 Append (font);
587 }
588
589 void wxFontList::RemoveFont (wxFont * font)
590 {
591 DeleteObject (font);
592 }
593
594 wxFont *wxFontList::
595 FindOrCreateFont (int PointSize, int FamilyOrFontId, int Style, int Weight, bool underline, const wxString& Face)
596 {
597 for (wxNode * node = First (); node; node = node->Next ())
598 {
599 wxFont *each_font = (wxFont *) node->Data ();
600 if (each_font && each_font->GetVisible() && each_font->Ok() &&
601 each_font->GetPointSize () == PointSize &&
602 each_font->GetStyle () == Style &&
603 each_font->GetWeight () == Weight &&
604 each_font->GetUnderlined () == underline &&
605 #if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
606 each_font->GetFontId () == FamilyOrFontId) /* New font system */
607 #else
608 each_font->GetFamily () == FamilyOrFontId &&
609 (!each_font->GetFaceName() || each_font->GetFaceName() == Face))
610 #endif
611 return each_font;
612 }
613 wxFont *font = new wxFont (PointSize, FamilyOrFontId, Style, Weight, underline, Face);
614 font->SetVisible(TRUE);
615 return font;
616 }
617
618 void wxBitmapList::AddBitmap(wxBitmap *bitmap)
619 { Append(bitmap); }
620 void wxBitmapList::RemoveBitmap(wxBitmap *bitmap)
621 { DeleteObject(bitmap); }
622
623 wxSize wxGetDisplaySize()
624 {
625 int x, y;
626 wxDisplaySize(& x, & y);
627 return wxSize(x, y);
628 }
629
630 wxResourceCache::wxResourceCache () : wxList() {
631 }
632
633 wxResourceCache::wxResourceCache (const unsigned int the_key_type) : wxList(the_key_type) {
634 }
635
636 wxResourceCache::~wxResourceCache () {
637 wxNode *node = First ();
638 while (node) {
639 wxGDIObject *item = (wxGDIObject *)node->Data();
640 if (item->IsKindOf(CLASSINFO(wxBrush))) {
641 wxBrush *brush = (wxBrush *)item;
642 delete brush;
643 }
644
645 if (item->IsKindOf(CLASSINFO(wxFont))) {
646 wxFont *font = (wxFont *)item;
647 delete font;
648 }
649
650 if (item->IsKindOf(CLASSINFO(wxBitmap))) {
651 wxBitmap *bitmap = (wxBitmap *)item;
652 delete bitmap;
653 }
654
655 if (item->IsKindOf(CLASSINFO(wxColour))) {
656 wxColour *colour = (wxColour *)item;
657 delete colour;
658 }
659
660 wxNode *next = node->Next ();
661 node = next;
662 }
663 }
664