X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7c74e7fe1de14f32dcd6f3c9cdd727de540bbd0e..399b60a0ad232265cd74ce8bf6a53a1f2cc57ff2:/src/common/resource.cpp diff --git a/src/common/resource.cpp b/src/common/resource.cpp index e070a680b4..27305db69a 100644 --- a/src/common/resource.cpp +++ b/src/common/resource.cpp @@ -79,6 +79,7 @@ #include "wx/wxexpr.h" #include "wx/settings.h" +#include "wx/stream.h" // Forward (private) declarations bool wxResourceInterpretResources(wxResourceTable& table, wxExprDatabase& db); @@ -93,6 +94,7 @@ wxItemResource *wxResourceInterpretIcon(wxResourceTable& table, wxExpr *expr); wxFont wxResourceInterpretFontSpec(wxExpr *expr); bool wxResourceReadOneResource(FILE *fd, wxExprDatabase& db, bool *eof, wxResourceTable *table = (wxResourceTable *) NULL); +bool wxResourceReadOneResource(wxInputStream *fd, wxExprDatabase& db, bool *eof, wxResourceTable *table) ; bool wxResourceParseIncludeFile(const wxString& f, wxResourceTable *table = (wxResourceTable *) NULL); wxResourceTable *wxDefaultResourceTable = (wxResourceTable *) NULL; @@ -119,10 +121,8 @@ void wxLogWarning(char *msg) wxMessageBox(msg, _("Warning"), wxOK); } -#if !USE_SHARED_LIBRARY IMPLEMENT_DYNAMIC_CLASS(wxItemResource, wxObject) IMPLEMENT_DYNAMIC_CLASS(wxResourceTable, wxHashTable) -#endif wxItemResource::wxItemResource() { @@ -210,6 +210,19 @@ bool wxResourceTable::DeleteResource(const wxString& name) return FALSE; } +bool wxResourceTable::ParseResourceFile( wxInputStream *is ) +{ + wxExprDatabase db; + int len = is->StreamSize() ; + + bool eof = FALSE; + while ( is->TellI() + 10 < len) // it's a hack because the streams dont support EOF + { + wxResourceReadOneResource(is, db, &eof, this) ; + } + return wxResourceInterpretResources(*this, db); +} + bool wxResourceTable::ParseResourceFile(const wxString& filename) { wxExprDatabase db; @@ -324,7 +337,7 @@ wxControl *wxResourceTable::CreateItem(wxWindow *parent, const wxItemResource* c } if (bitmap.Ok()) control = new wxBitmapButton(parent, id, bitmap, pos, size, - childResource->GetStyle(), wxDefaultValidator, childResource->GetName()); + childResource->GetStyle() | wxBU_AUTODRAW, wxDefaultValidator, childResource->GetName()); } else // Normal, text button @@ -334,7 +347,7 @@ wxControl *wxResourceTable::CreateItem(wxWindow *parent, const wxItemResource* c else if (itemType == wxString(wxT("wxMessage")) || itemType == wxString(wxT("wxStaticText")) || itemType == wxString(wxT("wxStaticBitmap"))) { - if (childResource->GetValue4() != wxT("")) + if (childResource->GetValue4() != wxT("") || itemType == wxString(wxT("wxStaticBitmap")) ) { // Bitmap message wxBitmap bitmap = childResource->GetBitmap(); @@ -344,6 +357,12 @@ wxControl *wxResourceTable::CreateItem(wxWindow *parent, const wxItemResource* c ((wxItemResource*) childResource)->SetBitmap(bitmap); } #if wxUSE_BITMAP_MESSAGE +#ifdef __WXMSW__ + // Use a default bitmap + if (!bitmap.Ok()) + bitmap.LoadFile("cross_bmp", wxBITMAP_TYPE_BMP_RESOURCE); +#endif + if (bitmap.Ok()) control = new wxStaticBitmap(parent, id, bitmap, pos, size, childResource->GetStyle(), childResource->GetName()); @@ -518,7 +537,17 @@ wxControl *wxResourceTable::CreateItem(wxWindow *parent, const wxItemResource* c else { if (control && childResource->GetFont().Ok()) + { control->SetFont(childResource->GetFont()); + +#ifdef __WXMSW__ + // Force the layout algorithm since the size changes the layout + if (control->IsKindOf(CLASSINFO(wxRadioBox))) + { + control->SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH|wxSIZE_AUTO_HEIGHT); + } +#endif + } } return control; } @@ -645,7 +674,7 @@ wxItemResource *wxResourceInterpretDialog(wxResourceTable& table, wxExpr *expr, if (useDefaults != 0) dialogItem->SetResourceStyle(dialogItem->GetResourceStyle() | wxRESOURCE_USE_DEFAULTS); - long id = 0; + int id = 0; expr->GetAttributeValue(wxT("id"), id); dialogItem->SetId(id); @@ -889,11 +918,11 @@ wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr) if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) { - // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count))); - // Do nothing - no label font any more - count ++; - if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) - controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); + // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count))); + // Skip past the obsolete label font spec if there are two consecutive specs + if (expr->Nth(count+1) && expr->Nth(count+1)->Type() == PrologList) + count ++; + controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); } } } @@ -943,12 +972,10 @@ wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr) if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) { - // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count))); - // Do nothing - count ++; - - if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) - controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); + // Skip past the obsolete label font spec if there are two consecutive specs + if (expr->Nth(count+1) && expr->Nth(count+1)->Type() == PrologList) + count ++; + controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); } } } @@ -1048,10 +1075,10 @@ wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr) } if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) { - // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count))); - count ++; - if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) - controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); + // Skip past the obsolete label font spec if there are two consecutive specs + if (expr->Nth(count+1) && expr->Nth(count+1)->Type() == PrologList) + count ++; + controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); } } } @@ -1074,11 +1101,10 @@ wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr) if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) { - // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count))); - count ++; - - if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) - controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); + // Skip past the obsolete label font spec if there are two consecutive specs + if (expr->Nth(count+1) && expr->Nth(count+1)->Type() == PrologList) + count ++; + controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); } } } @@ -1110,11 +1136,10 @@ wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr) if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) { - // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count))); - count ++; - - if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) - controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); + // Skip past the obsolete label font spec if there are two consecutive specs + if (expr->Nth(count+1) && expr->Nth(count+1)->Type() == PrologList) + count ++; + controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); } } } @@ -1148,11 +1173,10 @@ wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr) if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) { - // controlItem->SetLabelFont(wxResourceInterpretFontSpec(expr->Nth(count))); - count ++; - - if (expr->Nth(count) && expr->Nth(count)->Type() == PrologList) - controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); + // Skip past the obsolete label font spec if there are two consecutive specs + if (expr->Nth(count+1) && expr->Nth(count+1)->Type() == PrologList) + count ++; + controlItem->SetFont(wxResourceInterpretFontSpec(expr->Nth(count))); } } } @@ -1539,6 +1563,49 @@ static bool wxEatWhiteSpace(FILE *fd) } return FALSE; } +static bool wxEatWhiteSpace(wxInputStream *is) +{ + int ch = is->GetC() ; + if ((ch != ' ') && (ch != '/') && (ch != ' ') && (ch != 10) && (ch != 13) && (ch != 9)) + { + is->Ungetch(ch); + return TRUE; + } + + // Eat whitespace + while (ch == ' ' || ch == 10 || ch == 13 || ch == 9) + ch = is->GetC(); + // Check for comment + if (ch == '/') + { + ch = is->GetC(); + if (ch == '*') + { + bool finished = FALSE; + while (!finished) + { + ch = is->GetC(); + if (ch == EOF) + return FALSE; + if (ch == '*') + { + int newCh = is->GetC(); + if (newCh == '/') + finished = TRUE; + else + { + is->Ungetch(ch); + } + } + } + } + else // False alarm + return FALSE; + } + else + is->Ungetch(ch); + return wxEatWhiteSpace(is); +} bool wxGetResourceToken(FILE *fd) { @@ -1603,6 +1670,71 @@ bool wxGetResourceToken(FILE *fd) return TRUE; } +bool wxGetResourceToken(wxInputStream *is) +{ + if (!wxResourceBuffer) + wxReallocateResourceBuffer(); + wxResourceBuffer[0] = 0; + wxEatWhiteSpace(is); + + int ch = is->GetC() ; + if (ch == '"') + { + // Get string + wxResourceBufferCount = 0; + ch = is->GetC(); + while (ch != '"') + { + int actualCh = ch; + if (ch == EOF) + { + wxResourceBuffer[wxResourceBufferCount] = 0; + return FALSE; + } + // Escaped characters + else if (ch == '\\') + { + int newCh = is->GetC(); + if (newCh == '"') + actualCh = '"'; + else if (newCh == 10) + actualCh = 10; + else if (newCh == 13) // mac + actualCh = 10; + else + { + is->Ungetch(newCh); + } + } + + if (wxResourceBufferCount >= wxResourceBufferSize-1) + wxReallocateResourceBuffer(); + wxResourceBuffer[wxResourceBufferCount] = (char)actualCh; + wxResourceBufferCount ++; + ch = is->GetC(); + } + wxResourceBuffer[wxResourceBufferCount] = 0; + } + else + { + wxResourceBufferCount = 0; + // Any other token + while (ch != ' ' && ch != EOF && ch != ' ' && ch != 13 && ch != 9 && ch != 10) + { + if (wxResourceBufferCount >= wxResourceBufferSize-1) + wxReallocateResourceBuffer(); + wxResourceBuffer[wxResourceBufferCount] = (char)ch; + wxResourceBufferCount ++; + + ch = is->GetC(); + } + wxResourceBuffer[wxResourceBufferCount] = 0; + if (ch == EOF) + return FALSE; + } + return TRUE; +} + /* * Files are in form: static char *name = "...."; @@ -1739,6 +1871,135 @@ bool wxResourceReadOneResource(FILE *fd, wxExprDatabase& db, bool *eof, wxResour return TRUE; } +bool wxResourceReadOneResource(wxInputStream *fd, wxExprDatabase& db, bool *eof, wxResourceTable *table) +{ + if (!table) + table = wxDefaultResourceTable; + + // static or #define + if (!wxGetResourceToken(fd)) + { + *eof = TRUE; + return FALSE; + } + + if (strcmp(wxResourceBuffer, "#define") == 0) + { + wxGetResourceToken(fd); + char *name = copystring(wxResourceBuffer); + wxGetResourceToken(fd); + char *value = copystring(wxResourceBuffer); + if (isalpha(value[0])) + { + int val = (int)atol(value); + wxResourceAddIdentifier(name, val, table); + } + else + { + wxLogWarning(_("#define %s must be an integer."), name); + delete[] name; + delete[] value; + return FALSE; + } + delete[] name; + delete[] value; + + return TRUE; + } + else if (strcmp(wxResourceBuffer, "#include") == 0) + { + wxGetResourceToken(fd); + char *name = copystring(wxResourceBuffer); + char *actualName = name; + if (name[0] == '"') + actualName = name + 1; + int len = strlen(name); + if ((len > 0) && (name[len-1] == '"')) + name[len-1] = 0; + if (!wxResourceParseIncludeFile(actualName, table)) + { + wxLogWarning(_("Could not find resource include file %s."), actualName); + } + delete[] name; + return TRUE; + } + else if (strcmp(wxResourceBuffer, "static") != 0) + { + char buf[300]; + strcpy(buf, _("Found ")); + strncat(buf, wxResourceBuffer, 30); + strcat(buf, _(", expected static, #include or #define\nwhilst parsing resource.")); + wxLogWarning(buf); + return FALSE; + } + + // char + if (!wxGetResourceToken(fd)) + { + wxLogWarning(_("Unexpected end of file whilst parsing resource.")); + *eof = TRUE; + return FALSE; + } + + if (strcmp(wxResourceBuffer, "char") != 0) + { + wxLogWarning(_("Expected 'char' whilst parsing resource.")); + return FALSE; + } + + // *name + if (!wxGetResourceToken(fd)) + { + wxLogWarning(_("Unexpected end of file whilst parsing resource.")); + *eof = TRUE; + return FALSE; + } + + if (wxResourceBuffer[0] != '*') + { + wxLogWarning(_("Expected '*' whilst parsing resource.")); + return FALSE; + } + char nameBuf[100]; + strncpy(nameBuf, wxResourceBuffer+1, 99); + + // = + if (!wxGetResourceToken(fd)) + { + wxLogWarning(_("Unexpected end of file whilst parsing resource.")); + *eof = TRUE; + return FALSE; + } + + if (strcmp(wxResourceBuffer, "=") != 0) + { + wxLogWarning(_("Expected '=' whilst parsing resource.")); + return FALSE; + } + + // String + if (!wxGetResourceToken(fd)) + { + wxLogWarning(_("Unexpected end of file whilst parsing resource.")); + *eof = TRUE; + return FALSE; + } + else + { + if (!db.ReadPrologFromString(wxResourceBuffer)) + { + wxLogWarning(_("%s: ill-formed resource file syntax."), nameBuf); + return FALSE; + } + } + // Semicolon + if (!wxGetResourceToken(fd)) + { + *eof = TRUE; + } + return TRUE; +} + /* * Parses string window style into integer window style */ @@ -2192,8 +2453,8 @@ wxBitmap wxResourceCreateBitmap(const wxString& resource, wxResourceTable *table return wxBitmap(item->GetValue1(), (int)item->GetValue2(), (int)item->GetValue3()) ; #else wxLogWarning(_("No XBM facility available!")); -#endif break; +#endif } case wxBITMAP_TYPE_XPM_DATA: { @@ -2208,16 +2469,17 @@ wxBitmap wxResourceCreateBitmap(const wxString& resource, wxResourceTable *table return wxBitmap((char **)item->GetValue1()); #else wxLogWarning(_("No XPM facility available!")); -#endif break; +#endif } default: { return wxBitmap(name, bitmapType); - break; } } - return wxNullBitmap; +#ifndef __WXGTK__ + return wxNullBitmap; +#endif } else { @@ -2347,8 +2609,8 @@ wxIcon wxResourceCreateIcon(const wxString& resource, wxResourceTable *table) return wxIcon((const char **)item->GetValue1(), (int)item->GetValue2(), (int)item->GetValue3()); #else wxLogWarning(_("No XBM facility available!")); -#endif break; +#endif } case wxBITMAP_TYPE_XPM_DATA: {