1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Resource system (2nd file). Only required for 16-bit BC++.
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "resource.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_WX_RESOURCES
26 #pragma warning(disable:4706) // assignment within conditional expression
34 #include "wx/gdicmn.h"
38 #include "wx/stattext.h"
39 #include "wx/button.h"
40 #include "wx/bmpbuttn.h"
41 #include "wx/radiobox.h"
42 #include "wx/listbox.h"
43 #include "wx/choice.h"
44 #include "wx/checkbox.h"
45 #include "wx/settings.h"
46 #include "wx/slider.h"
47 #include "wx/statbox.h"
51 #include "wx/textctrl.h"
52 #include "wx/msgdlg.h"
57 #include "wx/scrolbar.h"
61 #include "wx/combobox.h"
64 #include "wx/validate.h"
73 #include "wx/resource.h"
74 #include "wx/string.h"
75 #include "wx/wxexpr.h"
77 #include "wx/settings.h"
79 #if ((defined(__BORLANDC__) || defined(__SC__)) && defined(__WIN16__))
81 // Forward (private) declarations
82 bool wxResourceInterpretResources(wxResourceTable
& table
, wxExprDatabase
& db
);
83 wxItemResource
*wxResourceInterpretDialog(wxResourceTable
& table
, wxExpr
*expr
, bool isPanel
= FALSE
);
84 wxItemResource
*wxResourceInterpretControl(wxResourceTable
& table
, wxExpr
*expr
);
85 wxItemResource
*wxResourceInterpretMenu(wxResourceTable
& table
, wxExpr
*expr
);
86 wxItemResource
*wxResourceInterpretMenuBar(wxResourceTable
& table
, wxExpr
*expr
);
87 wxItemResource
*wxResourceInterpretString(wxResourceTable
& table
, wxExpr
*expr
);
88 wxItemResource
*wxResourceInterpretBitmap(wxResourceTable
& table
, wxExpr
*expr
);
89 wxItemResource
*wxResourceInterpretIcon(wxResourceTable
& table
, wxExpr
*expr
);
90 // Interpret list expression
91 wxFont
wxResourceInterpretFontSpec(wxExpr
*expr
);
93 bool wxResourceReadOneResource(FILE *fd
, wxExprDatabase
& db
, bool *eof
, wxResourceTable
*table
= (wxResourceTable
*) NULL
);
94 bool wxResourceParseIncludeFile(const wxString
& f
, wxResourceTable
*table
= (wxResourceTable
*) NULL
);
96 extern wxResourceTable
*wxDefaultResourceTable
;
98 extern char *wxResourceBuffer
;
99 extern long wxResourceBufferSize
;
100 extern long wxResourceBufferCount
;
101 extern int wxResourceStringPtr
;
104 * (Re)allocate buffer for reading in from resource file
107 bool wxReallocateResourceBuffer()
109 if (!wxResourceBuffer
)
111 wxResourceBufferSize
= 1000;
112 wxResourceBuffer
= new char[wxResourceBufferSize
];
115 if (wxResourceBuffer
)
117 long newSize
= wxResourceBufferSize
+ 1000;
118 char *tmp
= new char[(int)newSize
];
119 strncpy(tmp
, wxResourceBuffer
, (int)wxResourceBufferCount
);
120 delete[] wxResourceBuffer
;
121 wxResourceBuffer
= tmp
;
122 wxResourceBufferSize
= newSize
;
127 static bool wxEatWhiteSpace(FILE *fd
)
130 if ((ch
!= ' ') && (ch
!= '/') && (ch
!= ' ') && (ch
!= 10) && (ch
!= 13) && (ch
!= 9))
137 while (ch
== ' ' || ch
== 10 || ch
== 13 || ch
== 9)
145 bool finished
= FALSE
;
153 int newCh
= getc(fd
);
168 return wxEatWhiteSpace(fd
);
171 bool wxGetResourceToken(FILE *fd
)
173 if (!wxResourceBuffer
)
174 wxReallocateResourceBuffer();
175 wxResourceBuffer
[0] = 0;
182 wxResourceBufferCount
= 0;
189 wxResourceBuffer
[wxResourceBufferCount
] = 0;
192 // Escaped characters
195 int newCh
= getc(fd
);
198 else if (newCh
== 10)
206 if (wxResourceBufferCount
>= wxResourceBufferSize
-1)
207 wxReallocateResourceBuffer();
208 wxResourceBuffer
[wxResourceBufferCount
] = (char)actualCh
;
209 wxResourceBufferCount
++;
212 wxResourceBuffer
[wxResourceBufferCount
] = 0;
216 wxResourceBufferCount
= 0;
218 while (ch
!= ' ' && ch
!= EOF
&& ch
!= ' ' && ch
!= 13 && ch
!= 9 && ch
!= 10)
220 if (wxResourceBufferCount
>= wxResourceBufferSize
-1)
221 wxReallocateResourceBuffer();
222 wxResourceBuffer
[wxResourceBufferCount
] = (char)ch
;
223 wxResourceBufferCount
++;
227 wxResourceBuffer
[wxResourceBufferCount
] = 0;
236 static char *name = "....";
237 with possible comments.
240 bool wxResourceReadOneResource(FILE *fd
, wxExprDatabase
& db
, bool *eof
, wxResourceTable
*table
)
243 table
= wxDefaultResourceTable
;
246 if (!wxGetResourceToken(fd
))
252 if (strcmp(wxResourceBuffer
, "#define") == 0)
254 wxGetResourceToken(fd
);
255 char *name
= copystring(wxResourceBuffer
);
256 wxGetResourceToken(fd
);
257 char *value
= copystring(wxResourceBuffer
);
258 if (isalpha(value
[0]))
260 int val
= (int)atol(value
);
261 wxResourceAddIdentifier(name
, val
, table
);
265 wxLogWarning(_("#define %s must be an integer."), name
);
275 else if (strcmp(wxResourceBuffer
, "#include") == 0)
277 wxGetResourceToken(fd
);
278 char *name
= copystring(wxResourceBuffer
);
279 char *actualName
= name
;
281 actualName
= name
+ 1;
282 int len
= strlen(name
);
283 if ((len
> 0) && (name
[len
-1] == '"'))
285 if (!wxResourceParseIncludeFile(actualName
, table
))
287 wxLogWarning(_("Could not find resource include file %s."), actualName
);
292 else if (strcmp(wxResourceBuffer
, "static") != 0)
295 strcpy(buf
, _("Found "));
296 strncat(buf
, wxResourceBuffer
, 30);
297 strcat(buf
, _(", expected static, #include or #define\nwhilst parsing resource."));
303 if (!wxGetResourceToken(fd
))
305 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
310 if (strcmp(wxResourceBuffer
, "char") != 0)
312 wxLogWarning(_("Expected 'char' whilst parsing resource."));
317 if (!wxGetResourceToken(fd
))
319 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
324 if (wxResourceBuffer
[0] != '*')
326 wxLogWarning(_("Expected '*' whilst parsing resource."));
330 strncpy(nameBuf
, wxResourceBuffer
+1, 99);
333 if (!wxGetResourceToken(fd
))
335 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
340 if (strcmp(wxResourceBuffer
, "=") != 0)
342 wxLogWarning(_("Expected '=' whilst parsing resource."));
347 if (!wxGetResourceToken(fd
))
349 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
355 if (!db
.ReadPrologFromString(wxResourceBuffer
))
357 wxLogWarning(_("%s: ill-formed resource file syntax."), nameBuf
);
362 if (!wxGetResourceToken(fd
))
370 * Parses string window style into integer window style
374 * Style flag parsing, e.g.
375 * "wxSYSTEM_MENU | wxBORDER" -> integer
378 char* wxResourceParseWord(char*s
, int *i
)
383 static char buf
[150];
387 while ((ii
< len
) && (isalpha(s
[ii
]) || (s
[ii
] == '_')))
395 // Eat whitespace and conjunction characters
397 ((s
[ii
] == ' ') || (s
[ii
] == '|') || (s
[ii
] == ',')))
408 struct wxResourceBitListStruct
414 static wxResourceBitListStruct wxResourceBitListTable
[] =
417 { "wxSINGLE", wxLB_SINGLE
},
418 { "wxMULTIPLE", wxLB_MULTIPLE
},
419 { "wxEXTENDED", wxLB_EXTENDED
},
420 { "wxLB_SINGLE", wxLB_SINGLE
},
421 { "wxLB_MULTIPLE", wxLB_MULTIPLE
},
422 { "wxLB_EXTENDED", wxLB_EXTENDED
},
423 { "wxLB_NEEDED_SB", wxLB_NEEDED_SB
},
424 { "wxLB_ALWAYS_SB", wxLB_ALWAYS_SB
},
425 { "wxLB_SORT", wxLB_SORT
},
426 { "wxLB_OWNERDRAW", wxLB_OWNERDRAW
},
427 { "wxLB_HSCROLL", wxLB_HSCROLL
},
430 { "wxCB_SIMPLE", wxCB_SIMPLE
},
431 { "wxCB_DROPDOWN", wxCB_DROPDOWN
},
432 { "wxCB_READONLY", wxCB_READONLY
},
433 { "wxCB_SORT", wxCB_SORT
},
436 { "wxGA_PROGRESSBAR", wxGA_PROGRESSBAR
},
437 { "wxGA_HORIZONTAL", wxGA_HORIZONTAL
},
438 { "wxGA_VERTICAL", wxGA_VERTICAL
},
441 { "wxPASSWORD", wxPASSWORD
},
442 { "wxPROCESS_ENTER", wxPROCESS_ENTER
},
443 { "wxTE_PASSWORD", wxTE_PASSWORD
},
444 { "wxTE_READONLY", wxTE_READONLY
},
445 { "wxTE_PROCESS_ENTER", wxTE_PROCESS_ENTER
},
446 { "wxTE_MULTILINE", wxTE_MULTILINE
},
448 /* wxRadioBox/wxRadioButton */
449 { "wxRB_GROUP", wxRB_GROUP
},
450 { "wxRA_SPECIFY_COLS", wxRA_SPECIFY_COLS
},
451 { "wxRA_SPECIFY_ROWS", wxRA_SPECIFY_ROWS
},
452 { "wxRA_HORIZONTAL", wxRA_HORIZONTAL
},
453 { "wxRA_VERTICAL", wxRA_VERTICAL
},
456 { "wxSL_HORIZONTAL", wxSL_HORIZONTAL
},
457 { "wxSL_VERTICAL", wxSL_VERTICAL
},
458 { "wxSL_AUTOTICKS", wxSL_AUTOTICKS
},
459 { "wxSL_LABELS", wxSL_LABELS
},
460 { "wxSL_LEFT", wxSL_LEFT
},
461 { "wxSL_TOP", wxSL_TOP
},
462 { "wxSL_RIGHT", wxSL_RIGHT
},
463 { "wxSL_BOTTOM", wxSL_BOTTOM
},
464 { "wxSL_BOTH", wxSL_BOTH
},
465 { "wxSL_SELRANGE", wxSL_SELRANGE
},
468 { "wxSB_HORIZONTAL", wxSB_HORIZONTAL
},
469 { "wxSB_VERTICAL", wxSB_VERTICAL
},
472 { "wxBU_AUTODRAW", wxBU_AUTODRAW
},
473 { "wxBU_NOAUTODRAW", wxBU_NOAUTODRAW
},
476 { "wxTR_HAS_BUTTONS", wxTR_HAS_BUTTONS
},
477 { "wxTR_EDIT_LABELS", wxTR_EDIT_LABELS
},
478 { "wxTR_LINES_AT_ROOT", wxTR_LINES_AT_ROOT
},
481 { "wxLC_ICON", wxLC_ICON
},
482 { "wxLC_SMALL_ICON", wxLC_SMALL_ICON
},
483 { "wxLC_LIST", wxLC_LIST
},
484 { "wxLC_REPORT", wxLC_REPORT
},
485 { "wxLC_ALIGN_TOP", wxLC_ALIGN_TOP
},
486 { "wxLC_ALIGN_LEFT", wxLC_ALIGN_LEFT
},
487 { "wxLC_AUTOARRANGE", wxLC_AUTOARRANGE
},
488 { "wxLC_USER_TEXT", wxLC_USER_TEXT
},
489 { "wxLC_EDIT_LABELS", wxLC_EDIT_LABELS
},
490 { "wxLC_NO_HEADER", wxLC_NO_HEADER
},
491 { "wxLC_NO_SORT_HEADER", wxLC_NO_SORT_HEADER
},
492 { "wxLC_SINGLE_SEL", wxLC_SINGLE_SEL
},
493 { "wxLC_SORT_ASCENDING", wxLC_SORT_ASCENDING
},
494 { "wxLC_SORT_DESCENDING", wxLC_SORT_DESCENDING
},
497 { "wxSP_VERTICAL", wxSP_VERTICAL
},
498 { "wxSP_HORIZONTAL", wxSP_HORIZONTAL
},
499 { "wxSP_ARROW_KEYS", wxSP_ARROW_KEYS
},
500 { "wxSP_WRAP", wxSP_WRAP
},
503 { "wxSP_NOBORDER", wxSP_NOBORDER
},
504 { "wxSP_3D", wxSP_3D
},
505 { "wxSP_BORDER", wxSP_BORDER
},
508 { "wxTC_MULTILINE", wxTC_MULTILINE
},
509 { "wxTC_RIGHTJUSTIFY", wxTC_RIGHTJUSTIFY
},
510 { "wxTC_FIXEDWIDTH", wxTC_FIXEDWIDTH
},
511 { "wxTC_OWNERDRAW", wxTC_OWNERDRAW
},
514 { "wxST_SIZEGRIP", wxST_SIZEGRIP
},
517 { "wxFIXED_LENGTH", wxFIXED_LENGTH
},
518 { "wxALIGN_LEFT", wxALIGN_LEFT
},
519 { "wxALIGN_CENTER", wxALIGN_CENTER
},
520 { "wxALIGN_CENTRE", wxALIGN_CENTRE
},
521 { "wxALIGN_RIGHT", wxALIGN_RIGHT
},
522 { "wxCOLOURED", wxCOLOURED
},
525 { "wxTB_3DBUTTONS", wxTB_3DBUTTONS
},
526 { "wxTB_HORIZONTAL", wxTB_HORIZONTAL
},
527 { "wxTB_VERTICAL", wxTB_VERTICAL
},
528 { "wxTB_FLAT", wxTB_FLAT
},
531 { "wxVSCROLL", wxVSCROLL
},
532 { "wxHSCROLL", wxHSCROLL
},
533 { "wxCAPTION", wxCAPTION
},
534 { "wxSTAY_ON_TOP", wxSTAY_ON_TOP
},
535 { "wxICONIZE", wxICONIZE
},
536 { "wxMINIMIZE", wxICONIZE
},
537 { "wxMAXIMIZE", wxMAXIMIZE
},
539 { "wxMDI_PARENT", 0},
541 { "wxTHICK_FRAME", wxTHICK_FRAME
},
542 { "wxRESIZE_BORDER", wxRESIZE_BORDER
},
543 { "wxSYSTEM_MENU", wxSYSTEM_MENU
},
544 { "wxMINIMIZE_BOX", wxMINIMIZE_BOX
},
545 { "wxMAXIMIZE_BOX", wxMAXIMIZE_BOX
},
546 { "wxRESIZE_BOX", wxRESIZE_BOX
},
547 { "wxDEFAULT_FRAME_STYLE", wxDEFAULT_FRAME_STYLE
},
548 { "wxDEFAULT_FRAME", wxDEFAULT_FRAME_STYLE
},
549 { "wxDEFAULT_DIALOG_STYLE", wxDEFAULT_DIALOG_STYLE
},
550 { "wxBORDER", wxBORDER
},
551 { "wxRETAINED", wxRETAINED
},
552 { "wxNATIVE_IMPL", 0},
553 { "wxEXTENDED_IMPL", 0},
554 { "wxBACKINGSTORE", wxBACKINGSTORE
},
555 // { "wxFLAT", wxFLAT},
556 // { "wxMOTIF_RESIZE", wxMOTIF_RESIZE},
557 { "wxFIXED_LENGTH", 0},
558 { "wxDOUBLE_BORDER", wxDOUBLE_BORDER
},
559 { "wxSUNKEN_BORDER", wxSUNKEN_BORDER
},
560 { "wxRAISED_BORDER", wxRAISED_BORDER
},
561 { "wxSIMPLE_BORDER", wxSIMPLE_BORDER
},
562 { "wxSTATIC_BORDER", wxSTATIC_BORDER
},
563 { "wxTRANSPARENT_WINDOW", wxTRANSPARENT_WINDOW
},
564 { "wxNO_BORDER", wxNO_BORDER
},
565 { "wxCLIP_CHILDREN", wxCLIP_CHILDREN
},
567 { "wxTINY_CAPTION_HORIZ", wxTINY_CAPTION_HORIZ
},
568 { "wxTINY_CAPTION_VERT", wxTINY_CAPTION_VERT
},
570 // Text font families
571 { "wxDEFAULT", wxDEFAULT
},
572 { "wxDECORATIVE", wxDECORATIVE
},
573 { "wxROMAN", wxROMAN
},
574 { "wxSCRIPT", wxSCRIPT
},
575 { "wxSWISS", wxSWISS
},
576 { "wxMODERN", wxMODERN
},
577 { "wxTELETYPE", wxTELETYPE
},
578 { "wxVARIABLE", wxVARIABLE
},
579 { "wxFIXED", wxFIXED
},
580 { "wxNORMAL", wxNORMAL
},
581 { "wxLIGHT", wxLIGHT
},
583 { "wxITALIC", wxITALIC
},
584 { "wxSLANT", wxSLANT
},
585 { "wxSOLID", wxSOLID
},
587 { "wxLONG_DASH", wxLONG_DASH
},
588 { "wxSHORT_DASH", wxSHORT_DASH
},
589 { "wxDOT_DASH", wxDOT_DASH
},
590 { "wxUSER_DASH", wxUSER_DASH
},
591 { "wxTRANSPARENT", wxTRANSPARENT
},
592 { "wxSTIPPLE", wxSTIPPLE
},
593 { "wxBDIAGONAL_HATCH", wxBDIAGONAL_HATCH
},
594 { "wxCROSSDIAG_HATCH", wxCROSSDIAG_HATCH
},
595 { "wxFDIAGONAL_HATCH", wxFDIAGONAL_HATCH
},
596 { "wxCROSS_HATCH", wxCROSS_HATCH
},
597 { "wxHORIZONTAL_HATCH", wxHORIZONTAL_HATCH
},
598 { "wxVERTICAL_HATCH", wxVERTICAL_HATCH
},
599 { "wxJOIN_BEVEL", wxJOIN_BEVEL
},
600 { "wxJOIN_MITER", wxJOIN_MITER
},
601 { "wxJOIN_ROUND", wxJOIN_ROUND
},
602 { "wxCAP_ROUND", wxCAP_ROUND
},
603 { "wxCAP_PROJECTING", wxCAP_PROJECTING
},
604 { "wxCAP_BUTT", wxCAP_BUTT
},
607 { "wxCLEAR", wxCLEAR
},
609 { "wxINVERT", wxINVERT
},
610 { "wxOR_REVERSE", wxOR_REVERSE
},
611 { "wxAND_REVERSE", wxAND_REVERSE
},
614 { "wxAND_INVERT", wxAND_INVERT
},
615 { "wxNO_OP", wxNO_OP
},
617 { "wxEQUIV", wxEQUIV
},
618 { "wxSRC_INVERT", wxSRC_INVERT
},
619 { "wxOR_INVERT", wxOR_INVERT
},
624 { "wxFLOOD_SURFACE", wxFLOOD_SURFACE
},
625 { "wxFLOOD_BORDER", wxFLOOD_BORDER
},
626 { "wxODDEVEN_RULE", wxODDEVEN_RULE
},
627 { "wxWINDING_RULE", wxWINDING_RULE
},
628 { "wxHORIZONTAL", wxHORIZONTAL
},
629 { "wxVERTICAL", wxVERTICAL
},
631 { "wxCENTER_FRAME", wxCENTER_FRAME
},
633 { "wxYES_NO", wxYES_NO
},
634 { "wxCANCEL", wxCANCEL
},
637 { "wxICON_EXCLAMATION", wxICON_EXCLAMATION
},
638 { "wxICON_HAND", wxICON_HAND
},
639 { "wxICON_QUESTION", wxICON_QUESTION
},
640 { "wxICON_INFORMATION", wxICON_INFORMATION
},
641 { "wxICON_STOP", wxICON_STOP
},
642 { "wxICON_ASTERISK", wxICON_ASTERISK
},
643 { "wxICON_MASK", wxICON_MASK
},
644 { "wxCENTRE", wxCENTRE
},
645 { "wxCENTER", wxCENTRE
},
646 { "wxUSER_COLOURS", wxUSER_COLOURS
},
647 { "wxVERTICAL_LABEL", 0},
648 { "wxHORIZONTAL_LABEL", 0},
650 // Bitmap types (not strictly styles)
651 { "wxBITMAP_TYPE_XPM", wxBITMAP_TYPE_XPM
},
652 { "wxBITMAP_TYPE_XBM", wxBITMAP_TYPE_XBM
},
653 { "wxBITMAP_TYPE_BMP", wxBITMAP_TYPE_BMP
},
654 { "wxBITMAP_TYPE_RESOURCE", wxBITMAP_TYPE_BMP_RESOURCE
},
655 { "wxBITMAP_TYPE_BMP_RESOURCE", wxBITMAP_TYPE_BMP_RESOURCE
},
656 { "wxBITMAP_TYPE_GIF", wxBITMAP_TYPE_GIF
},
657 { "wxBITMAP_TYPE_TIF", wxBITMAP_TYPE_TIF
},
658 { "wxBITMAP_TYPE_ICO", wxBITMAP_TYPE_ICO
},
659 { "wxBITMAP_TYPE_ICO_RESOURCE", wxBITMAP_TYPE_ICO_RESOURCE
},
660 { "wxBITMAP_TYPE_CUR", wxBITMAP_TYPE_CUR
},
661 { "wxBITMAP_TYPE_CUR_RESOURCE", wxBITMAP_TYPE_CUR_RESOURCE
},
662 { "wxBITMAP_TYPE_XBM_DATA", wxBITMAP_TYPE_XBM_DATA
},
663 { "wxBITMAP_TYPE_XPM_DATA", wxBITMAP_TYPE_XPM_DATA
},
664 { "wxBITMAP_TYPE_ANY", wxBITMAP_TYPE_ANY
}
667 static int wxResourceBitListCount
= (sizeof(wxResourceBitListTable
)/sizeof(wxResourceBitListStruct
));
669 long wxParseWindowStyle(const wxString
& bitListString
)
674 while ((word
= wxResourceParseWord((char*) (const char*) bitListString
, &i
)))
678 for (j
= 0; j
< wxResourceBitListCount
; j
++)
679 if (strcmp(wxResourceBitListTable
[j
].word
, word
) == 0)
681 bitList
|= wxResourceBitListTable
[j
].bits
;
687 wxLogWarning(_("Unrecognized style %s whilst parsing resource."), word
);
695 * Load a bitmap from a wxWindows resource, choosing an optimum
696 * depth and appropriate type.
699 wxBitmap
wxResourceCreateBitmap(const wxString
& resource
, wxResourceTable
*table
)
702 table
= wxDefaultResourceTable
;
704 wxItemResource
*item
= table
->FindResource(resource
);
707 if ((item
->GetType() == "") || (item
->GetType() != "wxBitmap"))
709 wxLogWarning(_("%s not a bitmap resource specification."), (const char*) resource
);
712 int thisDepth
= wxDisplayDepth();
713 long thisNoColours
= (long)pow(2.0, (double)thisDepth
);
715 wxItemResource
*optResource
= (wxItemResource
*) NULL
;
717 // Try to find optimum bitmap for this platform/colour depth
718 wxNode
*node
= item
->GetChildren().First();
721 wxItemResource
*child
= (wxItemResource
*)node
->Data();
722 int platform
= (int)child
->GetValue2();
723 int noColours
= (int)child
->GetValue3();
725 char *name = child->GetName();
726 int bitmapType = (int)child->GetValue1();
727 int xRes = child->GetWidth();
728 int yRes = child->GetHeight();
733 case RESOURCE_PLATFORM_ANY
:
735 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
739 // Maximise the number of colours.
740 // If noColours is zero (unspecified), then assume this
742 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
748 case RESOURCE_PLATFORM_WINDOWS
:
750 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
754 // Maximise the number of colours
755 if ((noColours
> 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
762 case RESOURCE_PLATFORM_X
:
764 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
768 // Maximise the number of colours
769 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
776 case RESOURCE_PLATFORM_MAC
:
778 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
782 // Maximise the number of colours
783 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
794 // If no matching resource, fail.
798 wxString name
= optResource
->GetName();
799 int bitmapType
= (int)optResource
->GetValue1();
802 case wxBITMAP_TYPE_XBM_DATA
:
805 wxItemResource
*item
= table
->FindResource(name
);
808 wxLogWarning(_("Failed to find XBM resource %s.\n"
809 "Forgot to use wxResourceLoadBitmapData?"), (const char*) name
);
812 return wxBitmap(item
->GetValue1(), (int)item
->GetValue2(), (int)item
->GetValue3()) ;
814 wxLogWarning(_("No XBM facility available!"));
818 case wxBITMAP_TYPE_XPM_DATA
:
820 wxItemResource
*item
= table
->FindResource(name
);
823 wxLogWarning(_("Failed to find XPM resource %s.\n"
824 "Forgot to use wxResourceLoadBitmapData?"), (const char*) name
);
827 return wxBitmap(item
->GetValue1());
832 return wxBitmap(name
, bitmapType
);
840 wxLogWarning(_("Bitmap resource specification %s not found."), (const char*) resource
);
846 * Load an icon from a wxWindows resource, choosing an optimum
847 * depth and appropriate type.
850 wxIcon
wxResourceCreateIcon(const wxString
& resource
, wxResourceTable
*table
)
853 table
= wxDefaultResourceTable
;
855 wxItemResource
*item
= table
->FindResource(resource
);
858 if ((item
->GetType() == "") || (item
->GetType() != "wxIcon"))
860 wxLogWarning(_("%s not an icon resource specification."), (const char*) resource
);
863 int thisDepth
= wxDisplayDepth();
864 long thisNoColours
= (long)pow(2.0, (double)thisDepth
);
866 wxItemResource
*optResource
= (wxItemResource
*) NULL
;
868 // Try to find optimum icon for this platform/colour depth
869 wxNode
*node
= item
->GetChildren().First();
872 wxItemResource
*child
= (wxItemResource
*)node
->Data();
873 int platform
= (int)child
->GetValue2();
874 int noColours
= (int)child
->GetValue3();
876 char *name = child->GetName();
877 int bitmapType = (int)child->GetValue1();
878 int xRes = child->GetWidth();
879 int yRes = child->GetHeight();
884 case RESOURCE_PLATFORM_ANY
:
886 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
890 // Maximise the number of colours.
891 // If noColours is zero (unspecified), then assume this
893 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
899 case RESOURCE_PLATFORM_WINDOWS
:
901 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
905 // Maximise the number of colours
906 if ((noColours
> 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
913 case RESOURCE_PLATFORM_X
:
915 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
919 // Maximise the number of colours
920 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
927 case RESOURCE_PLATFORM_MAC
:
929 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
933 // Maximise the number of colours
934 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
945 // If no matching resource, fail.
949 wxString name
= optResource
->GetName();
950 int bitmapType
= (int)optResource
->GetValue1();
953 case wxBITMAP_TYPE_XBM_DATA
:
956 wxItemResource
*item
= table
->FindResource(name
);
959 wxLogWarning(_("Failed to find XBM resource %s.\n"
960 "Forgot to use wxResourceLoadIconData?"), (const char*) name
);
963 return wxIcon((const char **)item
->GetValue1(), (int)item
->GetValue2(), (int)item
->GetValue3());
965 wxLogWarning(_("No XBM facility available!"));
969 case wxBITMAP_TYPE_XPM_DATA
:
971 // *** XPM ICON NOT YET IMPLEMENTED IN WXWINDOWS ***
973 wxItemResource *item = table->FindResource(name);
977 sprintf(buf, _("Failed to find XPM resource %s.\nForgot to use wxResourceLoadIconData?"), name);
981 return wxIcon((char **)item->GetValue1());
983 wxLogWarning(_("No XPM icon facility available!"));
989 wxLogWarning(_("Icon resource specification %s not found."), (const char*) resource
);
991 return wxIcon(name
, bitmapType
);
1000 wxLogWarning(_("Icon resource specification %s not found."), (const char*) resource
);
1005 wxMenu
*wxResourceCreateMenu(wxItemResource
*item
)
1007 wxMenu
*menu
= new wxMenu
;
1008 wxNode
*node
= item
->GetChildren().First();
1011 wxItemResource
*child
= (wxItemResource
*)node
->Data();
1012 if ((child
->GetType() != "") && (child
->GetType() == "wxMenuSeparator"))
1013 menu
->AppendSeparator();
1014 else if (child
->GetChildren().Number() > 0)
1016 wxMenu
*subMenu
= wxResourceCreateMenu(child
);
1018 menu
->Append((int)child
->GetValue1(), child
->GetTitle(), subMenu
, child
->GetValue4());
1022 menu
->Append((int)child
->GetValue1(), child
->GetTitle(), child
->GetValue4(), (child
->GetValue2() != 0));
1024 node
= node
->Next();
1029 wxMenuBar
*wxResourceCreateMenuBar(const wxString
& resource
, wxResourceTable
*table
, wxMenuBar
*menuBar
)
1032 table
= wxDefaultResourceTable
;
1034 wxItemResource
*menuResource
= table
->FindResource(resource
);
1035 if (menuResource
&& (menuResource
->GetType() != "") && (menuResource
->GetType() == "wxMenu"))
1038 menuBar
= new wxMenuBar
;
1039 wxNode
*node
= menuResource
->GetChildren().First();
1042 wxItemResource
*child
= (wxItemResource
*)node
->Data();
1043 wxMenu
*menu
= wxResourceCreateMenu(child
);
1045 menuBar
->Append(menu
, child
->GetTitle());
1046 node
= node
->Next();
1050 return (wxMenuBar
*) NULL
;
1053 wxMenu
*wxResourceCreateMenu(const wxString
& resource
, wxResourceTable
*table
)
1056 table
= wxDefaultResourceTable
;
1058 wxItemResource
*menuResource
= table
->FindResource(resource
);
1059 if (menuResource
&& (menuResource
->GetType() != "") && (menuResource
->GetType() == "wxMenu"))
1060 // if (menuResource && (menuResource->GetType() == wxTYPE_MENU))
1061 return wxResourceCreateMenu(menuResource
);
1062 return (wxMenu
*) NULL
;
1065 // Global equivalents (so don't have to refer to default table explicitly)
1066 bool wxResourceParseData(const wxString
& resource
, wxResourceTable
*table
)
1069 table
= wxDefaultResourceTable
;
1071 return table
->ParseResourceData(resource
);
1074 bool wxResourceParseFile(const wxString
& filename
, wxResourceTable
*table
)
1077 table
= wxDefaultResourceTable
;
1079 return table
->ParseResourceFile(filename
);
1082 // Register XBM/XPM data
1083 bool wxResourceRegisterBitmapData(const wxString
& name
, char bits
[], int width
, int height
, wxResourceTable
*table
)
1086 table
= wxDefaultResourceTable
;
1088 return table
->RegisterResourceBitmapData(name
, bits
, width
, height
);
1091 bool wxResourceRegisterBitmapData(const wxString
& name
, char **data
, wxResourceTable
*table
)
1094 table
= wxDefaultResourceTable
;
1096 return table
->RegisterResourceBitmapData(name
, data
);
1099 void wxResourceClear(wxResourceTable
*table
)
1102 table
= wxDefaultResourceTable
;
1104 table
->ClearTable();
1111 bool wxResourceAddIdentifier(const wxString
& name
, int value
, wxResourceTable
*table
)
1114 table
= wxDefaultResourceTable
;
1116 table
->identifiers
.Put(name
, (wxObject
*)value
);
1120 int wxResourceGetIdentifier(const wxString
& name
, wxResourceTable
*table
)
1123 table
= wxDefaultResourceTable
;
1125 return (int)table
->identifiers
.Get(name
);
1129 * Parse #include file for #defines (only)
1132 bool wxResourceParseIncludeFile(const wxString
& f
, wxResourceTable
*table
)
1135 table
= wxDefaultResourceTable
;
1137 FILE *fd
= fopen(f
, "r");
1142 while (wxGetResourceToken(fd
))
1144 if (strcmp(wxResourceBuffer
, "#define") == 0)
1146 wxGetResourceToken(fd
);
1147 char *name
= copystring(wxResourceBuffer
);
1148 wxGetResourceToken(fd
);
1149 char *value
= copystring(wxResourceBuffer
);
1150 if (isdigit(value
[0]))
1152 int val
= (int)atol(value
);
1153 wxResourceAddIdentifier(name
, val
, table
);
1164 * Reading strings as if they were .wxr files
1167 static int getc_string(char *s
)
1169 int ch
= s
[wxResourceStringPtr
];
1174 wxResourceStringPtr
++;
1179 static int ungetc_string()
1181 wxResourceStringPtr
--;
1185 bool wxEatWhiteSpaceString(char *s
)
1187 int ch
= getc_string(s
);
1191 if ((ch
!= ' ') && (ch
!= '/') && (ch
!= ' ') && (ch
!= 10) && (ch
!= 13) && (ch
!= 9))
1198 while (ch
== ' ' || ch
== 10 || ch
== 13 || ch
== 9)
1199 ch
= getc_string(s
);
1200 // Check for comment
1203 ch
= getc_string(s
);
1206 bool finished
= FALSE
;
1209 ch
= getc_string(s
);
1214 int newCh
= getc_string(s
);
1229 return wxEatWhiteSpaceString(s
);
1232 bool wxGetResourceTokenString(char *s
)
1234 if (!wxResourceBuffer
)
1235 wxReallocateResourceBuffer();
1236 wxResourceBuffer
[0] = 0;
1237 wxEatWhiteSpaceString(s
);
1239 int ch
= getc_string(s
);
1243 wxResourceBufferCount
= 0;
1244 ch
= getc_string(s
);
1250 wxResourceBuffer
[wxResourceBufferCount
] = 0;
1253 // Escaped characters
1254 else if (ch
== '\\')
1256 int newCh
= getc_string(s
);
1259 else if (newCh
== 10)
1267 if (wxResourceBufferCount
>= wxResourceBufferSize
-1)
1268 wxReallocateResourceBuffer();
1269 wxResourceBuffer
[wxResourceBufferCount
] = (char)actualCh
;
1270 wxResourceBufferCount
++;
1271 ch
= getc_string(s
);
1273 wxResourceBuffer
[wxResourceBufferCount
] = 0;
1277 wxResourceBufferCount
= 0;
1279 while (ch
!= ' ' && ch
!= EOF
&& ch
!= ' ' && ch
!= 13 && ch
!= 9 && ch
!= 10)
1281 if (wxResourceBufferCount
>= wxResourceBufferSize
-1)
1282 wxReallocateResourceBuffer();
1283 wxResourceBuffer
[wxResourceBufferCount
] = (char)ch
;
1284 wxResourceBufferCount
++;
1286 ch
= getc_string(s
);
1288 wxResourceBuffer
[wxResourceBufferCount
] = 0;
1296 * Files are in form:
1297 static char *name = "....";
1298 with possible comments.
1301 bool wxResourceReadOneResourceString(char *s
, wxExprDatabase
& db
, bool *eof
, wxResourceTable
*table
)
1304 table
= wxDefaultResourceTable
;
1306 // static or #define
1307 if (!wxGetResourceTokenString(s
))
1313 if (strcmp(wxResourceBuffer
, "#define") == 0)
1315 wxGetResourceTokenString(s
);
1316 char *name
= copystring(wxResourceBuffer
);
1317 wxGetResourceTokenString(s
);
1318 char *value
= copystring(wxResourceBuffer
);
1319 if (isalpha(value
[0]))
1321 int val
= (int)atol(value
);
1322 wxResourceAddIdentifier(name
, val
, table
);
1326 wxLogWarning(_("#define %s must be an integer."), name
);
1337 else if (strcmp(wxResourceBuffer, "#include") == 0)
1339 wxGetResourceTokenString(s);
1340 char *name = copystring(wxResourceBuffer);
1341 char *actualName = name;
1343 actualName = name + 1;
1344 int len = strlen(name);
1345 if ((len > 0) && (name[len-1] == '"'))
1347 if (!wxResourceParseIncludeFile(actualName, table))
1350 sprintf(buf, _("Could not find resource include file %s."), actualName);
1357 else if (strcmp(wxResourceBuffer
, "static") != 0)
1360 strcpy(buf
, _("Found "));
1361 strncat(buf
, wxResourceBuffer
, 30);
1362 strcat(buf
, _(", expected static, #include or #define\nwhilst parsing resource."));
1368 if (!wxGetResourceTokenString(s
))
1370 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1375 if (strcmp(wxResourceBuffer
, "char") != 0)
1377 wxLogWarning(_("Expected 'char' whilst parsing resource."));
1382 if (!wxGetResourceTokenString(s
))
1384 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1389 if (wxResourceBuffer
[0] != '*')
1391 wxLogWarning(_("Expected '*' whilst parsing resource."));
1395 strncpy(nameBuf
, wxResourceBuffer
+1, 99);
1398 if (!wxGetResourceTokenString(s
))
1400 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1405 if (strcmp(wxResourceBuffer
, "=") != 0)
1407 wxLogWarning(_("Expected '=' whilst parsing resource."));
1412 if (!wxGetResourceTokenString(s
))
1414 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1420 if (!db
.ReadPrologFromString(wxResourceBuffer
))
1422 wxLogWarning(_("%s: ill-formed resource file syntax."), nameBuf
);
1427 if (!wxGetResourceTokenString(s
))
1434 bool wxResourceParseString(char *s
, wxResourceTable
*table
)
1437 table
= wxDefaultResourceTable
;
1442 // Turn backslashes into spaces
1445 int len
= strlen(s
);
1447 for (i
= 0; i
< len
; i
++)
1448 if (s
[i
] == 92 && s
[i
+1] == 13)
1456 wxResourceStringPtr
= 0;
1459 while (wxResourceReadOneResourceString(s
, db
, &eof
, table
) && !eof
)
1463 return wxResourceInterpretResources(*table
, db
);
1467 * resource loading facility
1470 bool wxWindow::LoadFromResource(wxWindow
*parent
, const wxString
& resourceName
, const wxResourceTable
*table
)
1473 table
= wxDefaultResourceTable
;
1475 wxItemResource
*resource
= table
->FindResource((const char *)resourceName
);
1476 // if (!resource || (resource->GetType() != wxTYPE_DIALOG_BOX))
1477 if (!resource
|| (resource
->GetType() == "") ||
1478 ! ((strcmp(resource
->GetType(), "wxDialog") == 0) || (strcmp(resource
->GetType(), "wxPanel") == 0)))
1481 wxString
title(resource
->GetTitle());
1482 long theWindowStyle
= resource
->GetStyle();
1483 bool isModal
= (resource
->GetValue1() != 0);
1484 int x
= resource
->GetX();
1485 int y
= resource
->GetY();
1486 int width
= resource
->GetWidth();
1487 int height
= resource
->GetHeight();
1488 wxString name
= resource
->GetName();
1490 if (IsKindOf(CLASSINFO(wxDialog
)))
1492 wxDialog
*dialogBox
= (wxDialog
*)this;
1493 long modalStyle
= isModal
? wxDIALOG_MODAL
: 0;
1494 if (!dialogBox
->Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), theWindowStyle
|modalStyle
, name
))
1497 // Only reset the client size if we know we're not going to do it again below.
1498 if ((resource
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) == 0)
1499 dialogBox
->SetClientSize(width
, height
);
1501 else if (IsKindOf(CLASSINFO(wxPanel
)))
1503 wxPanel
* panel
= (wxPanel
*)this;
1504 if (!panel
->Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), theWindowStyle
, name
))
1509 if (!this->Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), theWindowStyle
, name
))
1513 if ((resource
->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS
) != 0)
1515 // No need to do this since it's done in wxPanel or wxDialog constructor.
1516 // SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
1520 if (resource
->GetFont().Ok())
1521 SetFont(resource
->GetFont());
1522 if (resource
->GetBackgroundColour().Ok())
1523 SetBackgroundColour(resource
->GetBackgroundColour());
1526 // Should have some kind of font at this point
1527 if (!GetFont().Ok())
1528 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
1529 if (!GetBackgroundColour().Ok())
1530 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
1532 // Only when we've created the window and set the font can we set the correct size,
1533 // if based on dialog units.
1534 if ((resource
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) != 0)
1536 wxSize sz
= ConvertDialogToPixels(wxSize(width
, height
));
1537 SetClientSize(sz
.x
, sz
.y
);
1539 wxPoint pt
= ConvertDialogToPixels(wxPoint(x
, y
));
1541 int mbarheight
= 2 * LMGetMBarHeight() ;
1542 pt
.y
+= mbarheight
;
1547 // Now create children
1548 wxNode
*node
= resource
->GetChildren().First();
1551 wxItemResource
*childResource
= (wxItemResource
*)node
->Data();
1553 (void) CreateItem(childResource
, resource
, table
);
1555 node
= node
->Next();
1560 wxControl
*wxWindow::CreateItem(const wxItemResource
*resource
, const wxItemResource
* parentResource
, const wxResourceTable
*table
)
1563 table
= wxDefaultResourceTable
;
1564 return table
->CreateItem((wxWindow
*)this, resource
, parentResource
);
1568 #pragma warning(default:4706) // assignment within conditional expression
1573 #endif // wxUSE_WX_RESOURCES