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 #if (defined(__WXGTK__)) || (defined(__WXMSW__) && wxUSE_XPM_IN_MSW)
821 wxItemResource
*item
= table
->FindResource(name
);
824 wxLogWarning(_("Failed to find XPM resource %s.\n"
825 "Forgot to use wxResourceLoadBitmapData?"), (const char*) name
);
828 return wxBitmap(item
->GetValue1());
830 wxLogWarning(_("No XPM facility available!"));
836 return wxBitmap(name
, bitmapType
);
844 wxLogWarning(_("Bitmap resource specification %s not found."), (const char*) resource
);
850 * Load an icon from a wxWindows resource, choosing an optimum
851 * depth and appropriate type.
854 wxIcon
wxResourceCreateIcon(const wxString
& resource
, wxResourceTable
*table
)
857 table
= wxDefaultResourceTable
;
859 wxItemResource
*item
= table
->FindResource(resource
);
862 if ((item
->GetType() == "") || (item
->GetType() != "wxIcon"))
864 wxLogWarning(_("%s not an icon resource specification."), (const char*) resource
);
867 int thisDepth
= wxDisplayDepth();
868 long thisNoColours
= (long)pow(2.0, (double)thisDepth
);
870 wxItemResource
*optResource
= (wxItemResource
*) NULL
;
872 // Try to find optimum icon for this platform/colour depth
873 wxNode
*node
= item
->GetChildren().First();
876 wxItemResource
*child
= (wxItemResource
*)node
->Data();
877 int platform
= (int)child
->GetValue2();
878 int noColours
= (int)child
->GetValue3();
880 char *name = child->GetName();
881 int bitmapType = (int)child->GetValue1();
882 int xRes = child->GetWidth();
883 int yRes = child->GetHeight();
888 case RESOURCE_PLATFORM_ANY
:
890 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
894 // Maximise the number of colours.
895 // If noColours is zero (unspecified), then assume this
897 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
903 case RESOURCE_PLATFORM_WINDOWS
:
905 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
909 // Maximise the number of colours
910 if ((noColours
> 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
917 case RESOURCE_PLATFORM_X
:
919 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
923 // Maximise the number of colours
924 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
931 case RESOURCE_PLATFORM_MAC
:
933 if (!optResource
&& ((noColours
== 0) || (noColours
<= thisNoColours
)))
937 // Maximise the number of colours
938 if ((noColours
== 0) || ((noColours
<= thisNoColours
) && (noColours
> optResource
->GetValue3())))
949 // If no matching resource, fail.
953 wxString name
= optResource
->GetName();
954 int bitmapType
= (int)optResource
->GetValue1();
957 case wxBITMAP_TYPE_XBM_DATA
:
960 wxItemResource
*item
= table
->FindResource(name
);
963 wxLogWarning(_("Failed to find XBM resource %s.\n"
964 "Forgot to use wxResourceLoadIconData?"), (const char*) name
);
967 return wxIcon((const char **)item
->GetValue1(), (int)item
->GetValue2(), (int)item
->GetValue3());
969 wxLogWarning(_("No XBM facility available!"));
973 case wxBITMAP_TYPE_XPM_DATA
:
975 // *** XPM ICON NOT YET IMPLEMENTED IN WXWINDOWS ***
977 #if (defined(__WXGTK__)) || (defined(__WXMSW__) && wxUSE_XPM_IN_MSW)
978 wxItemResource *item = table->FindResource(name);
982 sprintf(buf, _("Failed to find XPM resource %s.\nForgot to use wxResourceLoadIconData?"), name);
986 return wxIcon((char **)item->GetValue1());
988 wxLogWarning(_("No XPM facility available!"));
991 wxLogWarning(_("No XPM icon facility available!"));
997 wxLogWarning(_("Icon resource specification %s not found."), (const char*) resource
);
999 return wxIcon(name
, bitmapType
);
1008 wxLogWarning(_("Icon resource specification %s not found."), (const char*) resource
);
1013 wxMenu
*wxResourceCreateMenu(wxItemResource
*item
)
1015 wxMenu
*menu
= new wxMenu
;
1016 wxNode
*node
= item
->GetChildren().First();
1019 wxItemResource
*child
= (wxItemResource
*)node
->Data();
1020 if ((child
->GetType() != "") && (child
->GetType() == "wxMenuSeparator"))
1021 menu
->AppendSeparator();
1022 else if (child
->GetChildren().Number() > 0)
1024 wxMenu
*subMenu
= wxResourceCreateMenu(child
);
1026 menu
->Append((int)child
->GetValue1(), child
->GetTitle(), subMenu
, child
->GetValue4());
1030 menu
->Append((int)child
->GetValue1(), child
->GetTitle(), child
->GetValue4(), (child
->GetValue2() != 0));
1032 node
= node
->Next();
1037 wxMenuBar
*wxResourceCreateMenuBar(const wxString
& resource
, wxResourceTable
*table
, wxMenuBar
*menuBar
)
1040 table
= wxDefaultResourceTable
;
1042 wxItemResource
*menuResource
= table
->FindResource(resource
);
1043 if (menuResource
&& (menuResource
->GetType() != "") && (menuResource
->GetType() == "wxMenu"))
1046 menuBar
= new wxMenuBar
;
1047 wxNode
*node
= menuResource
->GetChildren().First();
1050 wxItemResource
*child
= (wxItemResource
*)node
->Data();
1051 wxMenu
*menu
= wxResourceCreateMenu(child
);
1053 menuBar
->Append(menu
, child
->GetTitle());
1054 node
= node
->Next();
1058 return (wxMenuBar
*) NULL
;
1061 wxMenu
*wxResourceCreateMenu(const wxString
& resource
, wxResourceTable
*table
)
1064 table
= wxDefaultResourceTable
;
1066 wxItemResource
*menuResource
= table
->FindResource(resource
);
1067 if (menuResource
&& (menuResource
->GetType() != "") && (menuResource
->GetType() == "wxMenu"))
1068 // if (menuResource && (menuResource->GetType() == wxTYPE_MENU))
1069 return wxResourceCreateMenu(menuResource
);
1070 return (wxMenu
*) NULL
;
1073 // Global equivalents (so don't have to refer to default table explicitly)
1074 bool wxResourceParseData(const wxString
& resource
, wxResourceTable
*table
)
1077 table
= wxDefaultResourceTable
;
1079 return table
->ParseResourceData(resource
);
1082 bool wxResourceParseFile(const wxString
& filename
, wxResourceTable
*table
)
1085 table
= wxDefaultResourceTable
;
1087 return table
->ParseResourceFile(filename
);
1090 // Register XBM/XPM data
1091 bool wxResourceRegisterBitmapData(const wxString
& name
, char bits
[], int width
, int height
, wxResourceTable
*table
)
1094 table
= wxDefaultResourceTable
;
1096 return table
->RegisterResourceBitmapData(name
, bits
, width
, height
);
1099 bool wxResourceRegisterBitmapData(const wxString
& name
, char **data
, wxResourceTable
*table
)
1102 table
= wxDefaultResourceTable
;
1104 return table
->RegisterResourceBitmapData(name
, data
);
1107 void wxResourceClear(wxResourceTable
*table
)
1110 table
= wxDefaultResourceTable
;
1112 table
->ClearTable();
1119 bool wxResourceAddIdentifier(const wxString
& name
, int value
, wxResourceTable
*table
)
1122 table
= wxDefaultResourceTable
;
1124 table
->identifiers
.Put(name
, (wxObject
*)value
);
1128 int wxResourceGetIdentifier(const wxString
& name
, wxResourceTable
*table
)
1131 table
= wxDefaultResourceTable
;
1133 return (int)table
->identifiers
.Get(name
);
1137 * Parse #include file for #defines (only)
1140 bool wxResourceParseIncludeFile(const wxString
& f
, wxResourceTable
*table
)
1143 table
= wxDefaultResourceTable
;
1145 FILE *fd
= fopen(f
, "r");
1150 while (wxGetResourceToken(fd
))
1152 if (strcmp(wxResourceBuffer
, "#define") == 0)
1154 wxGetResourceToken(fd
);
1155 char *name
= copystring(wxResourceBuffer
);
1156 wxGetResourceToken(fd
);
1157 char *value
= copystring(wxResourceBuffer
);
1158 if (isdigit(value
[0]))
1160 int val
= (int)atol(value
);
1161 wxResourceAddIdentifier(name
, val
, table
);
1172 * Reading strings as if they were .wxr files
1175 static int getc_string(char *s
)
1177 int ch
= s
[wxResourceStringPtr
];
1182 wxResourceStringPtr
++;
1187 static int ungetc_string()
1189 wxResourceStringPtr
--;
1193 bool wxEatWhiteSpaceString(char *s
)
1195 int ch
= getc_string(s
);
1199 if ((ch
!= ' ') && (ch
!= '/') && (ch
!= ' ') && (ch
!= 10) && (ch
!= 13) && (ch
!= 9))
1206 while (ch
== ' ' || ch
== 10 || ch
== 13 || ch
== 9)
1207 ch
= getc_string(s
);
1208 // Check for comment
1211 ch
= getc_string(s
);
1214 bool finished
= FALSE
;
1217 ch
= getc_string(s
);
1222 int newCh
= getc_string(s
);
1237 return wxEatWhiteSpaceString(s
);
1240 bool wxGetResourceTokenString(char *s
)
1242 if (!wxResourceBuffer
)
1243 wxReallocateResourceBuffer();
1244 wxResourceBuffer
[0] = 0;
1245 wxEatWhiteSpaceString(s
);
1247 int ch
= getc_string(s
);
1251 wxResourceBufferCount
= 0;
1252 ch
= getc_string(s
);
1258 wxResourceBuffer
[wxResourceBufferCount
] = 0;
1261 // Escaped characters
1262 else if (ch
== '\\')
1264 int newCh
= getc_string(s
);
1267 else if (newCh
== 10)
1275 if (wxResourceBufferCount
>= wxResourceBufferSize
-1)
1276 wxReallocateResourceBuffer();
1277 wxResourceBuffer
[wxResourceBufferCount
] = (char)actualCh
;
1278 wxResourceBufferCount
++;
1279 ch
= getc_string(s
);
1281 wxResourceBuffer
[wxResourceBufferCount
] = 0;
1285 wxResourceBufferCount
= 0;
1287 while (ch
!= ' ' && ch
!= EOF
&& ch
!= ' ' && ch
!= 13 && ch
!= 9 && ch
!= 10)
1289 if (wxResourceBufferCount
>= wxResourceBufferSize
-1)
1290 wxReallocateResourceBuffer();
1291 wxResourceBuffer
[wxResourceBufferCount
] = (char)ch
;
1292 wxResourceBufferCount
++;
1294 ch
= getc_string(s
);
1296 wxResourceBuffer
[wxResourceBufferCount
] = 0;
1304 * Files are in form:
1305 static char *name = "....";
1306 with possible comments.
1309 bool wxResourceReadOneResourceString(char *s
, wxExprDatabase
& db
, bool *eof
, wxResourceTable
*table
)
1312 table
= wxDefaultResourceTable
;
1314 // static or #define
1315 if (!wxGetResourceTokenString(s
))
1321 if (strcmp(wxResourceBuffer
, "#define") == 0)
1323 wxGetResourceTokenString(s
);
1324 char *name
= copystring(wxResourceBuffer
);
1325 wxGetResourceTokenString(s
);
1326 char *value
= copystring(wxResourceBuffer
);
1327 if (isalpha(value
[0]))
1329 int val
= (int)atol(value
);
1330 wxResourceAddIdentifier(name
, val
, table
);
1334 wxLogWarning(_("#define %s must be an integer."), name
);
1345 else if (strcmp(wxResourceBuffer, "#include") == 0)
1347 wxGetResourceTokenString(s);
1348 char *name = copystring(wxResourceBuffer);
1349 char *actualName = name;
1351 actualName = name + 1;
1352 int len = strlen(name);
1353 if ((len > 0) && (name[len-1] == '"'))
1355 if (!wxResourceParseIncludeFile(actualName, table))
1358 sprintf(buf, _("Could not find resource include file %s."), actualName);
1365 else if (strcmp(wxResourceBuffer
, "static") != 0)
1368 strcpy(buf
, _("Found "));
1369 strncat(buf
, wxResourceBuffer
, 30);
1370 strcat(buf
, _(", expected static, #include or #define\nwhilst parsing resource."));
1376 if (!wxGetResourceTokenString(s
))
1378 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1383 if (strcmp(wxResourceBuffer
, "char") != 0)
1385 wxLogWarning(_("Expected 'char' whilst parsing resource."));
1390 if (!wxGetResourceTokenString(s
))
1392 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1397 if (wxResourceBuffer
[0] != '*')
1399 wxLogWarning(_("Expected '*' whilst parsing resource."));
1403 strncpy(nameBuf
, wxResourceBuffer
+1, 99);
1406 if (!wxGetResourceTokenString(s
))
1408 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1413 if (strcmp(wxResourceBuffer
, "=") != 0)
1415 wxLogWarning(_("Expected '=' whilst parsing resource."));
1420 if (!wxGetResourceTokenString(s
))
1422 wxLogWarning(_("Unexpected end of file whilst parsing resource."));
1428 if (!db
.ReadPrologFromString(wxResourceBuffer
))
1430 wxLogWarning(_("%s: ill-formed resource file syntax."), nameBuf
);
1435 if (!wxGetResourceTokenString(s
))
1442 bool wxResourceParseString(char *s
, wxResourceTable
*table
)
1445 table
= wxDefaultResourceTable
;
1450 // Turn backslashes into spaces
1453 int len
= strlen(s
);
1455 for (i
= 0; i
< len
; i
++)
1456 if (s
[i
] == 92 && s
[i
+1] == 13)
1464 wxResourceStringPtr
= 0;
1467 while (wxResourceReadOneResourceString(s
, db
, &eof
, table
) && !eof
)
1471 return wxResourceInterpretResources(*table
, db
);
1475 * resource loading facility
1478 bool wxWindow::LoadFromResource(wxWindow
*parent
, const wxString
& resourceName
, const wxResourceTable
*table
)
1481 table
= wxDefaultResourceTable
;
1483 wxItemResource
*resource
= table
->FindResource((const char *)resourceName
);
1484 // if (!resource || (resource->GetType() != wxTYPE_DIALOG_BOX))
1485 if (!resource
|| (resource
->GetType() == "") ||
1486 ! ((strcmp(resource
->GetType(), "wxDialog") == 0) || (strcmp(resource
->GetType(), "wxPanel") == 0)))
1489 wxString
title(resource
->GetTitle());
1490 long theWindowStyle
= resource
->GetStyle();
1491 bool isModal
= (resource
->GetValue1() != 0);
1492 int x
= resource
->GetX();
1493 int y
= resource
->GetY();
1494 int width
= resource
->GetWidth();
1495 int height
= resource
->GetHeight();
1496 wxString name
= resource
->GetName();
1498 if (IsKindOf(CLASSINFO(wxDialog
)))
1500 wxDialog
*dialogBox
= (wxDialog
*)this;
1501 long modalStyle
= isModal
? wxDIALOG_MODAL
: 0;
1502 if (!dialogBox
->Create(parent
, -1, title
, wxPoint(x
, y
), wxSize(width
, height
), theWindowStyle
|modalStyle
, name
))
1505 // Only reset the client size if we know we're not going to do it again below.
1506 if ((resource
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) == 0)
1507 dialogBox
->SetClientSize(width
, height
);
1509 else if (IsKindOf(CLASSINFO(wxPanel
)))
1511 wxPanel
* panel
= (wxPanel
*)this;
1512 if (!panel
->Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), theWindowStyle
, name
))
1517 if (!this->Create(parent
, -1, wxPoint(x
, y
), wxSize(width
, height
), theWindowStyle
, name
))
1521 if ((resource
->GetResourceStyle() & wxRESOURCE_USE_DEFAULTS
) != 0)
1523 // No need to do this since it's done in wxPanel or wxDialog constructor.
1524 // SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
1528 if (resource
->GetFont().Ok())
1529 SetFont(resource
->GetFont());
1530 if (resource
->GetBackgroundColour().Ok())
1531 SetBackgroundColour(resource
->GetBackgroundColour());
1534 // Should have some kind of font at this point
1535 if (!GetFont().Ok())
1536 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
1537 if (!GetBackgroundColour().Ok())
1538 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
1540 // Only when we've created the window and set the font can we set the correct size,
1541 // if based on dialog units.
1542 if ((resource
->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS
) != 0)
1544 wxSize sz
= ConvertDialogToPixels(wxSize(width
, height
));
1545 SetClientSize(sz
.x
, sz
.y
);
1547 wxPoint pt
= ConvertDialogToPixels(wxPoint(x
, y
));
1551 // Now create children
1552 wxNode
*node
= resource
->GetChildren().First();
1555 wxItemResource
*childResource
= (wxItemResource
*)node
->Data();
1557 (void) CreateItem(childResource
, resource
, table
);
1559 node
= node
->Next();
1564 wxControl
*wxWindow::CreateItem(const wxItemResource
*resource
, const wxItemResource
* parentResource
, const wxResourceTable
*table
)
1567 table
= wxDefaultResourceTable
;
1568 return table
->CreateItem((wxWindow
*)this, resource
, parentResource
);
1572 #pragma warning(default:4706) // assignment within conditional expression
1577 #endif // wxUSE_WX_RESOURCES